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 (*progress_cb)(progress_arg,
807 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 if (fsync(merged_fd) != 0) {
810 err = got_error_from_errno("fsync");
814 /* Check if a clean merge has subsumed all local changes. */
815 if (overlapcnt == 0) {
816 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
822 if (chmod(merged_path, st_mode) != 0) {
823 err = got_error_from_errno2("chmod", merged_path);
827 if (rename(merged_path, ondisk_path) != 0) {
828 err = got_error_from_errno3("rename", merged_path,
835 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
836 err = got_error_from_errno("close");
837 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
838 err = got_error_from_errno("fclose");
839 if (f_orig && fclose(f_orig) != 0 && err == NULL)
840 err = got_error_from_errno("fclose");
843 if (blob_deriv_path) {
844 unlink(blob_deriv_path);
845 free(blob_deriv_path);
847 if (blob_orig_path) {
848 unlink(blob_orig_path);
849 free(blob_orig_path);
856 static const struct got_error *
857 update_blob_fileindex_entry(struct got_worktree *worktree,
858 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
859 const char *ondisk_path, const char *path, struct got_blob_object *blob,
860 int update_timestamps)
862 const struct got_error *err = NULL;
865 ie = got_fileindex_entry_get(fileindex, path);
867 err = got_fileindex_entry_update(ie, ondisk_path,
868 blob->id.sha1, worktree->base_commit_id->sha1,
871 struct got_fileindex_entry *new_ie;
872 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
873 path, blob->id.sha1, worktree->base_commit_id->sha1);
875 err = got_fileindex_entry_add(fileindex, new_ie);
880 static const struct got_error *
881 install_blob(struct got_worktree *worktree, const char *ondisk_path,
882 const char *path, uint16_t te_mode, uint16_t st_mode,
883 struct got_blob_object *blob, int restoring_missing_file,
884 int reverting_versioned_file, struct got_repository *repo,
885 got_worktree_checkout_cb progress_cb, void *progress_arg)
887 const struct got_error *err = NULL;
891 char *tmppath = NULL;
893 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
894 GOT_DEFAULT_FILE_MODE);
896 if (errno == ENOENT) {
897 char *parent = dirname(path);
899 return got_error_from_errno2("dirname", path);
900 err = add_dir_on_disk(worktree, parent);
903 fd = open(ondisk_path,
904 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
905 GOT_DEFAULT_FILE_MODE);
907 return got_error_from_errno2("open",
909 } else if (errno == EEXIST) {
910 if (!S_ISREG(st_mode)) {
911 /* TODO file is obstructed; do something */
912 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
915 err = got_opentemp_named_fd(&tmppath, &fd,
922 return got_error_from_errno2("open", ondisk_path);
925 if (restoring_missing_file)
926 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
927 else if (reverting_versioned_file)
928 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
930 (*progress_cb)(progress_arg,
931 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
933 hdrlen = got_object_blob_get_hdrlen(blob);
935 const uint8_t *buf = got_object_blob_get_read_buf(blob);
936 err = got_object_blob_read_block(&len, blob);
940 /* Skip blob object header first time around. */
941 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
943 err = got_error_from_errno("write");
945 } else if (outlen != len - hdrlen) {
946 err = got_error(GOT_ERR_IO);
953 if (fsync(fd) != 0) {
954 err = got_error_from_errno("fsync");
959 if (rename(tmppath, ondisk_path) != 0) {
960 err = got_error_from_errno3("rename", tmppath,
967 if (te_mode & S_IXUSR) {
968 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
969 err = got_error_from_errno2("chmod", ondisk_path);
973 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
974 err = got_error_from_errno2("chmod", ondisk_path);
980 if (fd != -1 && close(fd) != 0 && err == NULL)
981 err = got_error_from_errno("close");
986 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
987 static const struct got_error *
988 get_modified_file_content_status(unsigned char *status, FILE *f)
990 const struct got_error *err = NULL;
991 const char *markers[3] = {
992 GOT_DIFF_CONFLICT_MARKER_BEGIN,
993 GOT_DIFF_CONFLICT_MARKER_SEP,
994 GOT_DIFF_CONFLICT_MARKER_END
999 const char delim[3] = {'\0', '\0', '\0'};
1001 while (*status == GOT_STATUS_MODIFY) {
1002 line = fparseln(f, &len, NULL, delim, 0);
1006 err = got_ferror(f, GOT_ERR_IO);
1010 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1011 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1013 *status = GOT_STATUS_CONFLICT;
1022 static const struct got_error *
1023 get_file_status(unsigned char *status, struct stat *sb,
1024 struct got_fileindex_entry *ie, const char *abspath,
1025 struct got_repository *repo)
1027 const struct got_error *err = NULL;
1028 struct got_object_id id;
1032 struct got_blob_object *blob = NULL;
1035 *status = GOT_STATUS_NO_CHANGE;
1037 if (lstat(abspath, sb) == -1) {
1038 if (errno == ENOENT) {
1040 if (got_fileindex_entry_has_file_on_disk(ie))
1041 *status = GOT_STATUS_MISSING;
1043 *status = GOT_STATUS_DELETE;
1045 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1046 & (S_IRWXU | S_IRWXG | S_IRWXO));
1048 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1051 return got_error_from_errno2("lstat", abspath);
1054 if (!S_ISREG(sb->st_mode)) {
1055 *status = GOT_STATUS_OBSTRUCTED;
1062 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1063 *status = GOT_STATUS_DELETE;
1065 } else if (!got_fileindex_entry_has_blob(ie)) {
1066 *status = GOT_STATUS_ADD;
1070 if (ie->ctime_sec == sb->st_ctime &&
1071 ie->ctime_nsec == sb->st_ctimensec &&
1072 ie->mtime_sec == sb->st_mtime &&
1073 ie->mtime_sec == sb->st_mtime &&
1074 ie->mtime_nsec == sb->st_mtimensec &&
1075 ie->size == (sb->st_size & 0xffffffff))
1078 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1079 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1083 f = fopen(abspath, "r");
1085 err = got_error_from_errno2("fopen", abspath);
1088 hdrlen = got_object_blob_get_hdrlen(blob);
1090 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1091 err = got_object_blob_read_block(&blen, blob);
1094 /* Skip length of blob object header first time around. */
1095 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1096 if (flen == 0 && ferror(f)) {
1097 err = got_error_from_errno("fread");
1102 *status = GOT_STATUS_MODIFY;
1104 } else if (flen == 0) {
1106 *status = GOT_STATUS_MODIFY;
1108 } else if (blen - hdrlen == flen) {
1109 /* Skip blob object header first time around. */
1110 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1111 *status = GOT_STATUS_MODIFY;
1115 *status = GOT_STATUS_MODIFY;
1121 if (*status == GOT_STATUS_MODIFY) {
1123 err = get_modified_file_content_status(status, f);
1127 got_object_blob_close(blob);
1133 static const struct got_error *
1134 update_blob(struct got_worktree *worktree,
1135 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1136 struct got_tree_entry *te, const char *path,
1137 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1140 const struct got_error *err = NULL;
1141 struct got_blob_object *blob = NULL;
1143 unsigned char status = GOT_STATUS_NO_CHANGE;
1146 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1147 return got_error_from_errno("asprintf");
1149 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1153 if (status == GOT_STATUS_OBSTRUCTED) {
1154 (*progress_cb)(progress_arg, status, path);
1158 if (ie && status != GOT_STATUS_MISSING) {
1159 if (got_fileindex_entry_has_commit(ie) &&
1160 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1161 SHA1_DIGEST_LENGTH) == 0) {
1162 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1166 if (got_fileindex_entry_has_blob(ie) &&
1167 memcmp(ie->blob_sha1, te->id->sha1,
1168 SHA1_DIGEST_LENGTH) == 0)
1172 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1176 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1177 int update_timestamps;
1178 struct got_blob_object *blob2 = NULL;
1179 if (got_fileindex_entry_has_blob(ie)) {
1180 struct got_object_id id2;
1181 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1182 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1186 err = merge_blob(&update_timestamps, worktree, blob2,
1187 ondisk_path, path, sb.st_mode, blob,
1188 worktree->base_commit_id, repo,
1189 progress_cb, progress_arg);
1191 got_object_blob_close(blob2);
1193 * Do not update timestamps of files with local changes.
1194 * Otherwise, a future status walk would treat them as
1195 * unmodified files again.
1197 err = got_fileindex_entry_update(ie, ondisk_path,
1198 blob->id.sha1, worktree->base_commit_id->sha1,
1200 } else if (status == GOT_STATUS_DELETE) {
1201 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1202 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1203 ondisk_path, path, blob, 0);
1207 err = install_blob(worktree, ondisk_path, path, te->mode,
1208 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1209 repo, progress_cb, progress_arg);
1212 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1213 ondisk_path, path, blob, 1);
1217 got_object_blob_close(blob);
1223 static const struct got_error *
1224 remove_ondisk_file(const char *root_path, const char *path)
1226 const struct got_error *err = NULL;
1227 char *ondisk_path = NULL;
1229 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1230 return got_error_from_errno("asprintf");
1232 if (unlink(ondisk_path) == -1) {
1233 if (errno != ENOENT)
1234 err = got_error_from_errno2("unlink", ondisk_path);
1236 char *parent = dirname(ondisk_path);
1237 while (parent && strcmp(parent, root_path) != 0) {
1238 if (rmdir(parent) == -1) {
1239 if (errno != ENOTEMPTY)
1240 err = got_error_from_errno2("rmdir",
1244 parent = dirname(parent);
1251 static const struct got_error *
1252 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1253 struct got_fileindex_entry *ie, struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 unsigned char status;
1261 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1263 return got_error_from_errno("asprintf");
1265 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1269 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1270 status == GOT_STATUS_ADD) {
1271 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1273 * Preserve the working file and change the deleted blob's
1274 * entry into a schedule-add entry.
1276 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1281 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1282 if (status == GOT_STATUS_NO_CHANGE) {
1283 err = remove_ondisk_file(worktree->root_path, ie->path);
1287 got_fileindex_entry_remove(fileindex, ie);
1293 struct diff_cb_arg {
1294 struct got_fileindex *fileindex;
1295 struct got_worktree *worktree;
1296 struct got_repository *repo;
1297 got_worktree_checkout_cb progress_cb;
1299 got_worktree_cancel_cb cancel_cb;
1303 static const struct got_error *
1304 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1305 struct got_tree_entry *te, const char *parent_path)
1307 struct diff_cb_arg *a = arg;
1309 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1310 return got_error(GOT_ERR_CANCELLED);
1312 return update_blob(a->worktree, a->fileindex, ie, te,
1313 ie->path, a->repo, a->progress_cb, a->progress_arg);
1316 static const struct got_error *
1317 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1319 struct diff_cb_arg *a = arg;
1321 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1322 return got_error(GOT_ERR_CANCELLED);
1324 return delete_blob(a->worktree, a->fileindex, ie,
1325 a->repo, a->progress_cb, a->progress_arg);
1328 static const struct got_error *
1329 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1331 struct diff_cb_arg *a = arg;
1332 const struct got_error *err;
1335 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1336 return got_error(GOT_ERR_CANCELLED);
1338 if (asprintf(&path, "%s%s%s", parent_path,
1339 parent_path[0] ? "/" : "", te->name)
1341 return got_error_from_errno("asprintf");
1343 if (S_ISDIR(te->mode))
1344 err = add_dir_on_disk(a->worktree, path);
1346 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1347 a->repo, a->progress_cb, a->progress_arg);
1353 static const struct got_error *
1354 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1356 const struct got_error *err = NULL;
1357 char *uuidstr = NULL;
1358 uint32_t uuid_status;
1362 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1363 if (uuid_status != uuid_s_ok)
1364 return got_error_uuid(uuid_status);
1366 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1368 err = got_error_from_errno("asprintf");
1375 const struct got_error *
1376 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1378 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1381 static const struct got_error *
1382 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1384 return get_ref_name(refname, worktree,
1385 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1388 static const struct got_error *
1389 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1391 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1394 static const struct got_error *
1395 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1397 return get_ref_name(refname, worktree,
1398 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1401 static const struct got_error *
1402 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1404 return get_ref_name(refname, worktree,
1405 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1410 * Prevent Git's garbage collector from deleting our base commit by
1411 * setting a reference to our base commit's ID.
1413 static const struct got_error *
1414 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1416 const struct got_error *err = NULL;
1417 struct got_reference *ref = NULL;
1420 err = got_worktree_get_base_ref_name(&refname, worktree);
1424 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1428 err = got_ref_write(ref, repo);
1436 static const struct got_error *
1437 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1438 struct got_worktree *worktree)
1440 const struct got_error *err = NULL;
1443 *fileindex_path = NULL;
1444 *fileindex = got_fileindex_alloc();
1445 if (*fileindex == NULL)
1446 return got_error_from_errno("got_fileindex_alloc");
1448 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1449 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1450 err = got_error_from_errno("asprintf");
1451 *fileindex_path = NULL;
1455 index = fopen(*fileindex_path, "rb");
1456 if (index == NULL) {
1457 if (errno != ENOENT)
1458 err = got_error_from_errno2("fopen", *fileindex_path);
1460 err = got_fileindex_read(*fileindex, index);
1461 if (fclose(index) != 0 && err == NULL)
1462 err = got_error_from_errno("fclose");
1466 free(*fileindex_path);
1467 *fileindex_path = NULL;
1468 got_fileindex_free(*fileindex);
1474 struct bump_base_commit_id_arg {
1475 struct got_object_id *base_commit_id;
1478 const char *entry_name;
1479 got_worktree_checkout_cb progress_cb;
1483 /* Bump base commit ID of all files within an updated part of the work tree. */
1484 static const struct got_error *
1485 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1487 struct bump_base_commit_id_arg *a = arg;
1489 if (a->entry_name) {
1490 if (strcmp(ie->path, a->path) != 0)
1492 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1495 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1496 SHA1_DIGEST_LENGTH) == 0)
1500 (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1502 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1506 static const struct got_error *
1507 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1509 const struct got_error *err = NULL;
1510 char *new_fileindex_path = NULL;
1511 FILE *new_index = NULL;
1513 err = got_opentemp_named(&new_fileindex_path, &new_index,
1518 err = got_fileindex_write(fileindex, new_index);
1522 if (rename(new_fileindex_path, fileindex_path) != 0) {
1523 err = got_error_from_errno3("rename", new_fileindex_path,
1525 unlink(new_fileindex_path);
1530 free(new_fileindex_path);
1534 const struct got_error *
1535 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1536 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1537 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1539 const struct got_error *err = NULL, *sync_err, *unlockerr;
1540 struct got_commit_object *commit = NULL;
1541 struct got_object_id *tree_id = NULL;
1542 struct got_tree_object *tree = NULL;
1543 struct got_fileindex *fileindex = NULL;
1544 char *fileindex_path = NULL;
1545 struct got_fileindex_diff_tree_cb diff_cb;
1546 struct diff_cb_arg arg;
1547 char *relpath = NULL, *entry_name = NULL;
1548 struct bump_base_commit_id_arg bbc_arg;
1550 err = lock_worktree(worktree, LOCK_EX);
1555 * Read the file index.
1556 * Checking out files is supposed to be an idempotent operation.
1557 * If the on-disk file index is incomplete we will try to complete it.
1559 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1563 err = ref_base_commit(worktree, repo);
1567 err = got_object_open_as_commit(&commit, repo,
1568 worktree->base_commit_id);
1575 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1576 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1578 err = got_error_from_errno("asprintf");
1581 err = got_object_id_by_path(&tree_id, repo,
1582 worktree->base_commit_id, tree_path);
1586 err = got_object_get_type(&obj_type, repo, tree_id);
1589 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1590 /* Split provided path into parent dir + entry name. */
1591 if (strchr(path, '/') == NULL) {
1592 relpath = strdup("");
1593 if (relpath == NULL) {
1594 err = got_error_from_errno("strdup");
1597 tree_path = strdup(worktree->path_prefix);
1598 if (tree_path == NULL) {
1599 err = got_error_from_errno("strdup");
1603 err = got_path_dirname(&relpath, path);
1606 if (asprintf(&tree_path, "%s%s%s",
1607 worktree->path_prefix,
1608 got_path_is_root_dir(
1609 worktree->path_prefix) ? "" : "/",
1611 err = got_error_from_errno("asprintf");
1615 err = got_object_id_by_path(&tree_id, repo,
1616 worktree->base_commit_id, tree_path);
1620 entry_name = basename(path);
1621 if (entry_name == NULL) {
1622 err = got_error_from_errno2("basename", path);
1626 relpath = strdup(path);
1627 if (relpath == NULL) {
1628 err = got_error_from_errno("strdup");
1633 relpath = strdup("");
1634 if (relpath == NULL) {
1635 err = got_error_from_errno("strdup");
1638 err = got_object_id_by_path(&tree_id, repo,
1639 worktree->base_commit_id, worktree->path_prefix);
1644 err = got_object_open_as_tree(&tree, repo, tree_id);
1649 got_object_tree_find_entry(tree, entry_name) == NULL) {
1650 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1654 diff_cb.diff_old_new = diff_old_new;
1655 diff_cb.diff_old = diff_old;
1656 diff_cb.diff_new = diff_new;
1657 arg.fileindex = fileindex;
1658 arg.worktree = worktree;
1660 arg.progress_cb = progress_cb;
1661 arg.progress_arg = progress_arg;
1662 arg.cancel_cb = cancel_cb;
1663 arg.cancel_arg = cancel_arg;
1664 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1665 entry_name, repo, &diff_cb, &arg);
1669 bbc_arg.base_commit_id = worktree->base_commit_id;
1670 bbc_arg.entry_name = entry_name;
1671 bbc_arg.path = path;
1672 bbc_arg.path_len = strlen(path);
1673 bbc_arg.progress_cb = progress_cb;
1674 bbc_arg.progress_arg = progress_arg;
1675 err = got_fileindex_for_each_entry_safe(fileindex,
1676 bump_base_commit_id, &bbc_arg);
1678 sync_err = sync_fileindex(fileindex, fileindex_path);
1679 if (sync_err && err == NULL)
1682 free(fileindex_path);
1685 got_object_tree_close(tree);
1687 got_object_commit_close(commit);
1689 got_fileindex_free(fileindex);
1690 unlockerr = lock_worktree(worktree, LOCK_SH);
1691 if (unlockerr && err == NULL)
1696 struct merge_file_cb_arg {
1697 struct got_worktree *worktree;
1698 struct got_fileindex *fileindex;
1699 got_worktree_checkout_cb progress_cb;
1701 got_worktree_cancel_cb cancel_cb;
1703 struct got_object_id *commit_id2;
1706 static const struct got_error *
1707 merge_file_cb(void *arg, struct got_blob_object *blob1,
1708 struct got_blob_object *blob2, struct got_object_id *id1,
1709 struct got_object_id *id2, const char *path1, const char *path2,
1710 struct got_repository *repo)
1712 static const struct got_error *err = NULL;
1713 struct merge_file_cb_arg *a = arg;
1714 struct got_fileindex_entry *ie;
1715 char *ondisk_path = NULL;
1717 unsigned char status;
1718 int local_changes_subsumed;
1720 if (blob1 && blob2) {
1721 ie = got_fileindex_entry_get(a->fileindex, path2);
1723 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1728 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1730 return got_error_from_errno("asprintf");
1732 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1736 if (status == GOT_STATUS_DELETE) {
1737 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1741 if (status != GOT_STATUS_NO_CHANGE &&
1742 status != GOT_STATUS_MODIFY &&
1743 status != GOT_STATUS_CONFLICT &&
1744 status != GOT_STATUS_ADD) {
1745 (*a->progress_cb)(a->progress_arg, status, path2);
1749 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1750 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1751 a->progress_cb, a->progress_arg);
1753 ie = got_fileindex_entry_get(a->fileindex, path1);
1755 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1760 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1762 return got_error_from_errno("asprintf");
1764 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1769 case GOT_STATUS_NO_CHANGE:
1770 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1772 err = remove_ondisk_file(a->worktree->root_path, path1);
1776 got_fileindex_entry_mark_deleted_from_disk(ie);
1778 case GOT_STATUS_DELETE:
1779 case GOT_STATUS_MISSING:
1780 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1783 got_fileindex_entry_mark_deleted_from_disk(ie);
1785 case GOT_STATUS_ADD:
1786 case GOT_STATUS_MODIFY:
1787 case GOT_STATUS_CONFLICT:
1788 (*a->progress_cb)(a->progress_arg,
1789 GOT_STATUS_CANNOT_DELETE, path1);
1791 case GOT_STATUS_OBSTRUCTED:
1792 (*a->progress_cb)(a->progress_arg, status, path1);
1798 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1800 return got_error_from_errno("asprintf");
1801 ie = got_fileindex_entry_get(a->fileindex, path2);
1803 err = get_file_status(&status, &sb, ie, ondisk_path,
1807 if (status != GOT_STATUS_NO_CHANGE &&
1808 status != GOT_STATUS_MODIFY &&
1809 status != GOT_STATUS_CONFLICT &&
1810 status != GOT_STATUS_ADD) {
1811 (*a->progress_cb)(a->progress_arg, status,
1815 err = merge_blob(&local_changes_subsumed, a->worktree,
1816 NULL, ondisk_path, path2, sb.st_mode, blob2,
1817 a->commit_id2, repo,
1818 a->progress_cb, a->progress_arg);
1819 if (status == GOT_STATUS_DELETE) {
1820 err = update_blob_fileindex_entry(a->worktree,
1821 a->fileindex, ie, ondisk_path, ie->path,
1827 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1828 err = install_blob(a->worktree, ondisk_path, path2,
1829 /* XXX get this from parent tree! */
1830 GOT_DEFAULT_FILE_MODE,
1831 sb.st_mode, blob2, 0, 0, repo,
1832 a->progress_cb, a->progress_arg);
1835 err = got_fileindex_entry_alloc(&ie,
1836 ondisk_path, path2, NULL, NULL);
1839 err = got_fileindex_entry_add(a->fileindex, ie);
1841 got_fileindex_entry_free(ie);
1851 struct check_merge_ok_arg {
1852 struct got_worktree *worktree;
1853 struct got_repository *repo;
1856 static const struct got_error *
1857 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1859 const struct got_error *err = NULL;
1860 struct check_merge_ok_arg *a = arg;
1861 unsigned char status;
1865 /* Reject merges into a work tree with mixed base commits. */
1866 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1867 SHA1_DIGEST_LENGTH))
1868 return got_error(GOT_ERR_MIXED_COMMITS);
1870 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1872 return got_error_from_errno("asprintf");
1874 /* Reject merges into a work tree with conflicted files. */
1875 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1878 if (status == GOT_STATUS_CONFLICT)
1879 return got_error(GOT_ERR_CONFLICTS);
1884 static const struct got_error *
1885 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1886 const char *fileindex_path, struct got_object_id *commit_id1,
1887 struct got_object_id *commit_id2, struct got_repository *repo,
1888 got_worktree_checkout_cb progress_cb, void *progress_arg,
1889 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1891 const struct got_error *err = NULL, *sync_err;
1892 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1893 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1894 struct merge_file_cb_arg arg;
1897 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1898 worktree->path_prefix);
1902 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1907 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1908 worktree->path_prefix);
1912 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1916 arg.worktree = worktree;
1917 arg.fileindex = fileindex;
1918 arg.progress_cb = progress_cb;
1919 arg.progress_arg = progress_arg;
1920 arg.cancel_cb = cancel_cb;
1921 arg.cancel_arg = cancel_arg;
1922 arg.commit_id2 = commit_id2;
1923 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1924 sync_err = sync_fileindex(fileindex, fileindex_path);
1925 if (sync_err && err == NULL)
1929 got_object_tree_close(tree1);
1931 got_object_tree_close(tree2);
1935 const struct got_error *
1936 got_worktree_merge_files(struct got_worktree *worktree,
1937 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1938 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1939 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1941 const struct got_error *err, *unlockerr;
1942 char *fileindex_path = NULL;
1943 struct got_fileindex *fileindex = NULL;
1944 struct check_merge_ok_arg mok_arg;
1946 err = lock_worktree(worktree, LOCK_EX);
1950 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1954 mok_arg.worktree = worktree;
1955 mok_arg.repo = repo;
1956 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1961 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
1962 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1965 got_fileindex_free(fileindex);
1966 free(fileindex_path);
1967 unlockerr = lock_worktree(worktree, LOCK_SH);
1968 if (unlockerr && err == NULL)
1973 struct diff_dir_cb_arg {
1974 struct got_fileindex *fileindex;
1975 struct got_worktree *worktree;
1976 const char *status_path;
1977 size_t status_path_len;
1978 struct got_repository *repo;
1979 got_worktree_status_cb status_cb;
1981 got_worktree_cancel_cb cancel_cb;
1985 static const struct got_error *
1986 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1987 got_worktree_status_cb status_cb, void *status_arg,
1988 struct got_repository *repo)
1990 const struct got_error *err = NULL;
1991 unsigned char status = GOT_STATUS_NO_CHANGE;
1993 struct got_object_id blob_id, commit_id;
1995 err = get_file_status(&status, &sb, ie, abspath, repo);
1996 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1997 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1998 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1999 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2005 static const struct got_error *
2006 status_old_new(void *arg, struct got_fileindex_entry *ie,
2007 struct dirent *de, const char *parent_path)
2009 const struct got_error *err = NULL;
2010 struct diff_dir_cb_arg *a = arg;
2013 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2014 return got_error(GOT_ERR_CANCELLED);
2016 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2017 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2020 if (parent_path[0]) {
2021 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2022 parent_path, de->d_name) == -1)
2023 return got_error_from_errno("asprintf");
2025 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2027 return got_error_from_errno("asprintf");
2030 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2036 static const struct got_error *
2037 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2039 struct diff_dir_cb_arg *a = arg;
2040 struct got_object_id blob_id, commit_id;
2041 unsigned char status;
2043 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2044 return got_error(GOT_ERR_CANCELLED);
2046 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2049 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2050 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2051 if (got_fileindex_entry_has_file_on_disk(ie))
2052 status = GOT_STATUS_MISSING;
2054 status = GOT_STATUS_DELETE;
2055 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2059 static const struct got_error *
2060 status_new(void *arg, struct dirent *de, const char *parent_path)
2062 const struct got_error *err = NULL;
2063 struct diff_dir_cb_arg *a = arg;
2066 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2067 return got_error(GOT_ERR_CANCELLED);
2069 if (de->d_type == DT_DIR)
2072 /* XXX ignore symlinks for now */
2073 if (de->d_type == DT_LNK)
2076 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2079 if (parent_path[0]) {
2080 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2081 return got_error_from_errno("asprintf");
2086 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2093 const struct got_error *
2094 got_worktree_status(struct got_worktree *worktree, const char *path,
2095 struct got_repository *repo, got_worktree_status_cb status_cb,
2096 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2098 const struct got_error *err = NULL;
2099 DIR *workdir = NULL;
2100 char *fileindex_path = NULL;
2101 struct got_fileindex *fileindex = NULL;
2102 struct got_fileindex_diff_dir_cb fdiff_cb;
2103 struct diff_dir_cb_arg arg;
2104 char *ondisk_path = NULL;
2106 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2110 if (asprintf(&ondisk_path, "%s%s%s",
2111 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2112 err = got_error_from_errno("asprintf");
2115 workdir = opendir(ondisk_path);
2116 if (workdir == NULL) {
2117 if (errno == ENOTDIR || errno == ENOENT) {
2118 struct got_fileindex_entry *ie;
2119 ie = got_fileindex_entry_get(fileindex, path);
2121 err = got_error(GOT_ERR_BAD_PATH);
2124 err = report_file_status(ie, ondisk_path,
2125 status_cb, status_arg, repo);
2128 err = got_error_from_errno2("opendir", ondisk_path);
2132 fdiff_cb.diff_old_new = status_old_new;
2133 fdiff_cb.diff_old = status_old;
2134 fdiff_cb.diff_new = status_new;
2135 arg.fileindex = fileindex;
2136 arg.worktree = worktree;
2137 arg.status_path = path;
2138 arg.status_path_len = strlen(path);
2140 arg.status_cb = status_cb;
2141 arg.status_arg = status_arg;
2142 arg.cancel_cb = cancel_cb;
2143 arg.cancel_arg = cancel_arg;
2144 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2145 path, repo, &fdiff_cb, &arg);
2150 free(fileindex_path);
2151 got_fileindex_free(fileindex);
2155 const struct got_error *
2156 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2159 const struct got_error *err = NULL;
2160 char *resolved, *path = NULL;
2165 resolved = realpath(arg, NULL);
2166 if (resolved == NULL)
2167 return got_error_from_errno2("realpath", arg);
2169 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2170 strlen(got_worktree_get_root_path(worktree)))) {
2171 err = got_error(GOT_ERR_BAD_PATH);
2175 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2176 err = got_path_skip_common_ancestor(&path,
2177 got_worktree_get_root_path(worktree), resolved);
2183 err = got_error_from_errno("strdup");
2188 /* XXX status walk can't deal with trailing slash! */
2190 while (path[len - 1] == '/') {
2191 path[len - 1] = '\0';
2203 static const struct got_error *
2204 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2205 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2206 struct got_repository *repo)
2208 const struct got_error *err = NULL;
2209 struct got_fileindex_entry *ie;
2211 /* Re-adding an existing entry is a no-op. */
2212 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2215 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2219 err = got_fileindex_entry_add(fileindex, ie);
2221 got_fileindex_entry_free(ie);
2225 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2228 const struct got_error *
2229 got_worktree_schedule_add(struct got_worktree *worktree,
2230 struct got_pathlist_head *ondisk_paths,
2231 got_worktree_status_cb status_cb, void *status_arg,
2232 struct got_repository *repo)
2234 struct got_fileindex *fileindex = NULL;
2235 char *fileindex_path = NULL;
2236 const struct got_error *err = NULL, *sync_err, *unlockerr;
2237 struct got_pathlist_entry *pe;
2239 err = lock_worktree(worktree, LOCK_EX);
2243 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2247 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2249 err = got_path_skip_common_ancestor(&relpath,
2250 got_worktree_get_root_path(worktree), pe->path);
2253 err = schedule_addition(pe->path, fileindex, relpath,
2254 status_cb, status_arg, repo);
2259 sync_err = sync_fileindex(fileindex, fileindex_path);
2260 if (sync_err && err == NULL)
2263 free(fileindex_path);
2265 got_fileindex_free(fileindex);
2266 unlockerr = lock_worktree(worktree, LOCK_SH);
2267 if (unlockerr && err == NULL)
2272 static const struct got_error *
2273 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2274 const char *relpath, int delete_local_mods,
2275 got_worktree_status_cb status_cb, void *status_arg,
2276 struct got_repository *repo)
2278 const struct got_error *err = NULL;
2279 struct got_fileindex_entry *ie = NULL;
2280 unsigned char status;
2283 ie = got_fileindex_entry_get(fileindex, relpath);
2285 return got_error(GOT_ERR_BAD_PATH);
2287 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2291 if (status != GOT_STATUS_NO_CHANGE) {
2292 if (status == GOT_STATUS_DELETE)
2293 return got_error_set_errno(ENOENT, ondisk_path);
2294 if (status != GOT_STATUS_MODIFY)
2295 return got_error(GOT_ERR_FILE_STATUS);
2296 if (!delete_local_mods)
2297 return got_error(GOT_ERR_FILE_MODIFIED);
2300 if (unlink(ondisk_path) != 0)
2301 return got_error_from_errno2("unlink", ondisk_path);
2303 got_fileindex_entry_mark_deleted_from_disk(ie);
2304 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2307 const struct got_error *
2308 got_worktree_schedule_delete(struct got_worktree *worktree,
2309 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2310 got_worktree_status_cb status_cb, void *status_arg,
2311 struct got_repository *repo)
2313 struct got_fileindex *fileindex = NULL;
2314 char *fileindex_path = NULL;
2315 const struct got_error *err = NULL, *sync_err, *unlockerr;
2316 struct got_pathlist_entry *pe;
2318 err = lock_worktree(worktree, LOCK_EX);
2322 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2326 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2328 err = got_path_skip_common_ancestor(&relpath,
2329 got_worktree_get_root_path(worktree), pe->path);
2332 err = schedule_for_deletion(pe->path, fileindex, relpath,
2333 delete_local_mods, status_cb, status_arg, repo);
2338 sync_err = sync_fileindex(fileindex, fileindex_path);
2339 if (sync_err && err == NULL)
2342 free(fileindex_path);
2344 got_fileindex_free(fileindex);
2345 unlockerr = lock_worktree(worktree, LOCK_SH);
2346 if (unlockerr && err == NULL)
2351 static const struct got_error *
2352 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2353 const char *ondisk_path,
2354 got_worktree_checkout_cb progress_cb, void *progress_arg,
2355 struct got_repository *repo)
2357 const struct got_error *err = NULL;
2358 char *relpath = NULL, *parent_path = NULL;
2359 struct got_fileindex_entry *ie;
2360 struct got_tree_object *tree = NULL;
2361 struct got_object_id *tree_id = NULL;
2362 const struct got_tree_entry *te;
2363 char *tree_path = NULL, *te_name;
2364 struct got_blob_object *blob = NULL;
2365 unsigned char status;
2368 err = got_path_skip_common_ancestor(&relpath,
2369 got_worktree_get_root_path(worktree), ondisk_path);
2373 ie = got_fileindex_entry_get(fileindex, relpath);
2375 err = got_error(GOT_ERR_BAD_PATH);
2379 /* Construct in-repository path of tree which contains this blob. */
2380 err = got_path_dirname(&parent_path, ie->path);
2382 if (err->code != GOT_ERR_BAD_PATH)
2384 parent_path = strdup("/");
2385 if (parent_path == NULL) {
2386 err = got_error_from_errno("strdup");
2390 if (got_path_is_root_dir(worktree->path_prefix)) {
2391 tree_path = strdup(parent_path);
2392 if (tree_path == NULL) {
2393 err = got_error_from_errno("strdup");
2397 if (got_path_is_root_dir(parent_path)) {
2398 tree_path = strdup(worktree->path_prefix);
2399 if (tree_path == NULL) {
2400 err = got_error_from_errno("strdup");
2404 if (asprintf(&tree_path, "%s/%s",
2405 worktree->path_prefix, parent_path) == -1) {
2406 err = got_error_from_errno("asprintf");
2412 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2417 err = got_object_open_as_tree(&tree, repo, tree_id);
2421 te_name = basename(ie->path);
2422 if (te_name == NULL) {
2423 err = got_error_from_errno2("basename", ie->path);
2427 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2431 te = got_object_tree_find_entry(tree, te_name);
2432 if (te == NULL && status != GOT_STATUS_ADD) {
2433 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2438 case GOT_STATUS_ADD:
2439 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2440 got_fileindex_entry_remove(fileindex, ie);
2442 case GOT_STATUS_DELETE:
2443 case GOT_STATUS_MODIFY:
2444 case GOT_STATUS_CONFLICT:
2445 case GOT_STATUS_MISSING: {
2446 struct got_object_id id;
2447 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2448 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2451 err = install_blob(worktree, ondisk_path, ie->path,
2452 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2456 if (status == GOT_STATUS_DELETE) {
2457 err = update_blob_fileindex_entry(worktree,
2458 fileindex, ie, ondisk_path, ie->path, blob, 1);
2472 got_object_blob_close(blob);
2474 got_object_tree_close(tree);
2479 const struct got_error *
2480 got_worktree_revert(struct got_worktree *worktree,
2481 struct got_pathlist_head *ondisk_paths,
2482 got_worktree_checkout_cb progress_cb, void *progress_arg,
2483 struct got_repository *repo)
2485 struct got_fileindex *fileindex = NULL;
2486 char *fileindex_path = NULL;
2487 const struct got_error *err = NULL, *unlockerr = NULL;
2488 const struct got_error *sync_err = NULL;
2489 struct got_pathlist_entry *pe;
2491 err = lock_worktree(worktree, LOCK_EX);
2495 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2499 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2500 err = revert_file(worktree, fileindex, pe->path,
2501 progress_cb, progress_arg, repo);
2505 sync_err = sync_fileindex(fileindex, fileindex_path);
2506 if (sync_err && err == NULL)
2509 free(fileindex_path);
2511 got_fileindex_free(fileindex);
2512 unlockerr = lock_worktree(worktree, LOCK_SH);
2513 if (unlockerr && err == NULL)
2519 free_commitable(struct got_commitable *ct)
2522 free(ct->in_repo_path);
2523 free(ct->ondisk_path);
2525 free(ct->base_blob_id);
2526 free(ct->base_commit_id);
2530 struct collect_commitables_arg {
2531 struct got_pathlist_head *commitable_paths;
2532 struct got_repository *repo;
2533 struct got_worktree *worktree;
2536 static const struct got_error *
2537 collect_commitables(void *arg, unsigned char status, const char *relpath,
2538 struct got_object_id *blob_id, struct got_object_id *commit_id)
2540 struct collect_commitables_arg *a = arg;
2541 const struct got_error *err = NULL;
2542 struct got_commitable *ct = NULL;
2543 struct got_pathlist_entry *new = NULL;
2544 char *parent_path = NULL, *path = NULL;
2547 if (status == GOT_STATUS_CONFLICT)
2548 return got_error(GOT_ERR_COMMIT_CONFLICT);
2550 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2551 status != GOT_STATUS_DELETE)
2554 if (asprintf(&path, "/%s", relpath) == -1) {
2555 err = got_error_from_errno("asprintf");
2558 if (strcmp(path, "/") == 0) {
2559 parent_path = strdup("");
2560 if (parent_path == NULL)
2561 return got_error_from_errno("strdup");
2563 err = got_path_dirname(&parent_path, path);
2568 ct = calloc(1, sizeof(*ct));
2570 err = got_error_from_errno("calloc");
2574 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2576 err = got_error_from_errno("asprintf");
2579 if (status == GOT_STATUS_DELETE) {
2580 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2582 if (lstat(ct->ondisk_path, &sb) != 0) {
2583 err = got_error_from_errno2("lstat", ct->ondisk_path);
2586 ct->mode = sb.st_mode;
2589 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2590 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2592 err = got_error_from_errno("asprintf");
2596 ct->status = status;
2597 ct->blob_id = NULL; /* will be filled in when blob gets created */
2598 if (ct->status != GOT_STATUS_ADD) {
2599 ct->base_blob_id = got_object_id_dup(blob_id);
2600 if (ct->base_blob_id == NULL) {
2601 err = got_error_from_errno("got_object_id_dup");
2604 ct->base_commit_id = got_object_id_dup(commit_id);
2605 if (ct->base_commit_id == NULL) {
2606 err = got_error_from_errno("got_object_id_dup");
2610 ct->path = strdup(path);
2611 if (ct->path == NULL) {
2612 err = got_error_from_errno("strdup");
2615 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2617 if (ct && (err || new == NULL))
2618 free_commitable(ct);
2624 static const struct got_error *write_tree(struct got_object_id **,
2625 struct got_tree_object *, const char *, struct got_pathlist_head *,
2626 got_worktree_status_cb status_cb, void *status_arg,
2627 struct got_repository *);
2629 static const struct got_error *
2630 write_subtree(struct got_object_id **new_subtree_id,
2631 struct got_tree_entry *te, const char *parent_path,
2632 struct got_pathlist_head *commitable_paths,
2633 got_worktree_status_cb status_cb, void *status_arg,
2634 struct got_repository *repo)
2636 const struct got_error *err = NULL;
2637 struct got_tree_object *subtree;
2640 if (asprintf(&subpath, "%s%s%s", parent_path,
2641 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2642 return got_error_from_errno("asprintf");
2644 err = got_object_open_as_tree(&subtree, repo, te->id);
2648 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2649 status_cb, status_arg, repo);
2650 got_object_tree_close(subtree);
2655 static const struct got_error *
2656 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2658 const struct got_error *err = NULL;
2659 char *ct_parent_path = NULL;
2663 if (strchr(ct->path, '/') == NULL) {
2664 *match = got_path_is_root_dir(path);
2668 err = got_path_dirname(&ct_parent_path, ct->path);
2671 *match = (strcmp(path, ct_parent_path) == 0);
2672 free(ct_parent_path);
2677 get_ct_file_mode(struct got_commitable *ct)
2679 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2682 static const struct got_error *
2683 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2684 struct got_tree_entry *te, struct got_commitable *ct)
2686 const struct got_error *err = NULL;
2690 err = got_object_tree_entry_dup(new_te, te);
2694 (*new_te)->mode = get_ct_file_mode(ct);
2696 free((*new_te)->id);
2697 (*new_te)->id = got_object_id_dup(ct->blob_id);
2698 if ((*new_te)->id == NULL) {
2699 err = got_error_from_errno("got_object_id_dup");
2703 if (err && *new_te) {
2704 got_object_tree_entry_close(*new_te);
2710 static const struct got_error *
2711 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2712 struct got_commitable *ct)
2714 const struct got_error *err = NULL;
2719 *new_te = calloc(1, sizeof(**new_te));
2720 if (*new_te == NULL)
2721 return got_error_from_errno("calloc");
2723 ct_name = basename(ct->path);
2724 if (ct_name == NULL) {
2725 err = got_error_from_errno2("basename", ct->path);
2728 (*new_te)->name = strdup(ct_name);
2729 if ((*new_te)->name == NULL) {
2730 err = got_error_from_errno("strdup");
2734 (*new_te)->mode = get_ct_file_mode(ct);
2736 (*new_te)->id = got_object_id_dup(ct->blob_id);
2737 if ((*new_te)->id == NULL) {
2738 err = got_error_from_errno("got_object_id_dup");
2742 if (err && *new_te) {
2743 got_object_tree_entry_close(*new_te);
2749 static const struct got_error *
2750 insert_tree_entry(struct got_tree_entry *new_te,
2751 struct got_pathlist_head *paths)
2753 const struct got_error *err = NULL;
2754 struct got_pathlist_entry *new_pe;
2756 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2760 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2764 static const struct got_error *
2765 report_ct_status(struct got_commitable *ct,
2766 got_worktree_status_cb status_cb, void *status_arg)
2768 const char *ct_path = ct->path;
2769 while (ct_path[0] == '/')
2771 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2774 static const struct got_error *
2775 match_modified_subtree(int *modified, struct got_tree_entry *te,
2776 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2778 const struct got_error *err = NULL;
2779 struct got_pathlist_entry *pe;
2784 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2785 got_path_is_root_dir(base_tree_path) ? "" : "/",
2787 return got_error_from_errno("asprintf");
2789 TAILQ_FOREACH(pe, commitable_paths, entry) {
2790 struct got_commitable *ct = pe->data;
2791 *modified = got_path_is_child(ct->in_repo_path, te_path,
2801 static const struct got_error *
2802 match_deleted_or_modified_ct(struct got_commitable **ctp,
2803 struct got_tree_entry *te, const char *base_tree_path,
2804 struct got_pathlist_head *commitable_paths)
2806 const struct got_error *err = NULL;
2807 struct got_pathlist_entry *pe;
2811 TAILQ_FOREACH(pe, commitable_paths, entry) {
2812 struct got_commitable *ct = pe->data;
2813 char *ct_name = NULL;
2816 if (ct->status != GOT_STATUS_MODIFY &&
2817 ct->status != GOT_STATUS_DELETE)
2820 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2823 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2829 ct_name = basename(pe->path);
2830 if (ct_name == NULL)
2831 return got_error_from_errno2("basename", pe->path);
2833 if (strcmp(te->name, ct_name) != 0)
2843 static const struct got_error *
2844 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2845 const char *child_path, const char *path_base_tree,
2846 struct got_pathlist_head *commitable_paths,
2847 got_worktree_status_cb status_cb, void *status_arg,
2848 struct got_repository *repo)
2850 const struct got_error *err = NULL;
2851 struct got_tree_entry *new_te;
2856 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2857 got_path_is_root_dir(path_base_tree) ? "" : "/",
2859 return got_error_from_errno("asprintf");
2861 new_te = calloc(1, sizeof(*new_te));
2862 new_te->mode = S_IFDIR;
2863 new_te->name = strdup(child_path);
2864 if (new_te->name == NULL) {
2865 err = got_error_from_errno("strdup");
2866 got_object_tree_entry_close(new_te);
2869 err = write_tree(&new_te->id, NULL, subtree_path,
2870 commitable_paths, status_cb, status_arg, repo);
2872 got_object_tree_entry_close(new_te);
2882 static const struct got_error *
2883 write_tree(struct got_object_id **new_tree_id,
2884 struct got_tree_object *base_tree, const char *path_base_tree,
2885 struct got_pathlist_head *commitable_paths,
2886 got_worktree_status_cb status_cb, void *status_arg,
2887 struct got_repository *repo)
2889 const struct got_error *err = NULL;
2890 const struct got_tree_entries *base_entries = NULL;
2891 struct got_pathlist_head paths;
2892 struct got_tree_entries new_tree_entries;
2893 struct got_tree_entry *te, *new_te = NULL;
2894 struct got_pathlist_entry *pe;
2897 new_tree_entries.nentries = 0;
2898 SIMPLEQ_INIT(&new_tree_entries.head);
2900 /* Insert, and recurse into, newly added entries first. */
2901 TAILQ_FOREACH(pe, commitable_paths, entry) {
2902 struct got_commitable *ct = pe->data;
2903 char *child_path = NULL, *slash;
2905 if (ct->status != GOT_STATUS_ADD ||
2906 (ct->flags & GOT_COMMITABLE_ADDED))
2909 if (!got_path_is_child(pe->path, path_base_tree,
2910 strlen(path_base_tree)))
2913 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2918 slash = strchr(child_path, '/');
2919 if (slash == NULL) {
2920 err = alloc_added_blob_tree_entry(&new_te, ct);
2923 err = report_ct_status(ct, status_cb, status_arg);
2926 ct->flags |= GOT_COMMITABLE_ADDED;
2927 err = insert_tree_entry(new_te, &paths);
2931 *slash = '\0'; /* trim trailing path components */
2932 if (base_tree == NULL ||
2933 got_object_tree_find_entry(base_tree, child_path)
2935 err = make_subtree_for_added_blob(&new_te,
2936 child_path, path_base_tree,
2937 commitable_paths, status_cb, status_arg,
2941 err = insert_tree_entry(new_te, &paths);
2949 /* Handle modified and deleted entries. */
2950 base_entries = got_object_tree_get_entries(base_tree);
2951 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2952 struct got_commitable *ct = NULL;
2954 if (S_ISDIR(te->mode)) {
2956 err = got_object_tree_entry_dup(&new_te, te);
2959 err = match_modified_subtree(&modified, te,
2960 path_base_tree, commitable_paths);
2963 /* Avoid recursion into unmodified subtrees. */
2966 err = write_subtree(&new_te->id, te,
2967 path_base_tree, commitable_paths,
2968 status_cb, status_arg, repo);
2972 err = insert_tree_entry(new_te, &paths);
2978 err = match_deleted_or_modified_ct(&ct, te,
2979 path_base_tree, commitable_paths);
2981 /* NB: Deleted entries get dropped here. */
2982 if (ct->status == GOT_STATUS_MODIFY) {
2983 err = alloc_modified_blob_tree_entry(
2987 err = insert_tree_entry(new_te, &paths);
2991 err = report_ct_status(ct, status_cb,
2996 /* Entry is unchanged; just copy it. */
2997 err = got_object_tree_entry_dup(&new_te, te);
3000 err = insert_tree_entry(new_te, &paths);
3007 /* Write new list of entries; deleted entries have been dropped. */
3008 TAILQ_FOREACH(pe, &paths, entry) {
3009 struct got_tree_entry *te = pe->data;
3010 new_tree_entries.nentries++;
3011 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3013 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3015 got_object_tree_entries_close(&new_tree_entries);
3016 got_pathlist_free(&paths);
3020 static const struct got_error *
3021 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3022 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
3023 struct got_worktree *worktree)
3025 const struct got_error *err = NULL;
3026 struct got_pathlist_entry *pe;
3028 TAILQ_FOREACH(pe, commitable_paths, entry) {
3029 struct got_fileindex_entry *ie;
3030 struct got_commitable *ct = pe->data;
3032 ie = got_fileindex_entry_get(fileindex, pe->path);
3034 if (ct->status == GOT_STATUS_DELETE) {
3035 got_fileindex_entry_remove(fileindex, ie);
3036 got_fileindex_entry_free(ie);
3038 err = got_fileindex_entry_update(ie,
3039 ct->ondisk_path, ct->blob_id->sha1,
3040 new_base_commit_id->sha1, 1);
3042 err = got_fileindex_entry_alloc(&ie,
3043 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3044 new_base_commit_id->sha1);
3047 err = got_fileindex_entry_add(fileindex, ie);
3055 static const struct got_error *
3056 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3057 struct got_object_id *head_commit_id)
3059 const struct got_error *err = NULL;
3060 struct got_object_id *id_in_head = NULL, *id = NULL;
3061 struct got_commit_object *commit = NULL;
3063 const char *ct_path = ct->in_repo_path;
3065 while (ct_path[0] == '/')
3069 * Ensure that no modifications were made to files *and their parents*
3070 * in commits between the file's base commit and the branch head.
3072 * Checking the parents is important for detecting conflicting tree
3073 * configurations (files or parent folders might have been moved,
3074 * deleted, added again, etc.). Such changes need to be merged with
3075 * local changes before a commit can occur.
3077 * The implication is that the file's (parent) entry in the root
3078 * directory must have the same ID in all relevant commits.
3080 if (ct->status != GOT_STATUS_ADD) {
3081 struct got_object_qid *pid;
3083 struct got_object_id *root_entry_id = NULL;
3085 /* Trivial case: base commit == head commit */
3086 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3089 /* Compute the path to the root directory's entry. */
3090 path = strdup(ct_path);
3092 err = got_error_from_errno("strdup");
3095 slash = strchr(path, '/');
3099 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3103 err = got_object_id_by_path(&root_entry_id, repo,
3104 head_commit_id, path);
3108 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3110 struct got_commit_object *pcommit;
3112 err = got_object_id_by_path(&id, repo, pid->id, path);
3114 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3120 err = got_object_id_by_path(&id, repo, pid->id, path);
3124 if (got_object_id_cmp(id, root_entry_id) != 0) {
3125 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3129 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3130 break; /* all relevant commits scanned */
3132 err = got_object_open_as_commit(&pcommit, repo,
3137 got_object_commit_close(commit);
3139 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3143 /* Require that added files don't exist in the branch head. */
3144 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3146 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3148 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3152 got_object_commit_close(commit);
3159 const struct got_error *
3160 got_worktree_commit(struct got_object_id **new_commit_id,
3161 struct got_worktree *worktree, const char *ondisk_path,
3162 const char *author, const char *committer,
3163 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3164 got_worktree_status_cb status_cb, void *status_arg,
3165 struct got_repository *repo)
3167 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3168 struct got_fileindex *fileindex = NULL;
3169 char *fileindex_path = NULL;
3170 struct collect_commitables_arg cc_arg;
3171 struct got_pathlist_head commitable_paths;
3172 struct got_pathlist_entry *pe;
3173 char *relpath = NULL;
3174 const char *head_ref_name = NULL;
3175 struct got_reference *head_ref = NULL;
3176 struct got_commit_object *head_commit = NULL;
3177 struct got_object_id *head_commit_id = NULL;
3178 struct got_reference *head_ref2 = NULL;
3179 struct got_object_id *head_commit_id2 = NULL;
3180 struct got_tree_object *head_tree = NULL;
3181 struct got_object_id *new_tree_id = NULL;
3182 struct got_object_id_queue parent_ids;
3183 struct got_object_qid *pid = NULL;
3184 char *logmsg = NULL;
3186 *new_commit_id = NULL;
3188 TAILQ_INIT(&commitable_paths);
3189 SIMPLEQ_INIT(&parent_ids);
3192 err = got_path_skip_common_ancestor(&relpath,
3193 worktree->root_path, ondisk_path);
3198 err = lock_worktree(worktree, LOCK_EX);
3202 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3205 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3209 cc_arg.commitable_paths = &commitable_paths;
3210 cc_arg.worktree = worktree;
3212 err = got_worktree_status(worktree, relpath ? relpath : "",
3213 repo, collect_commitables, &cc_arg, NULL, NULL);
3217 if (TAILQ_EMPTY(&commitable_paths)) {
3218 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3222 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3226 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3227 struct got_commitable *ct = pe->data;
3228 err = check_ct_out_of_date(ct, repo, head_commit_id);
3233 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3237 if (commit_msg_cb != NULL) {
3238 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3243 if (logmsg == NULL || strlen(logmsg) == 0) {
3244 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3248 /* Create blobs from added and modified files and record their IDs. */
3249 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3250 struct got_commitable *ct = pe->data;
3253 if (ct->status != GOT_STATUS_ADD &&
3254 ct->status != GOT_STATUS_MODIFY)
3257 if (asprintf(&ondisk_path, "%s/%s",
3258 worktree->root_path, pe->path) == -1) {
3259 err = got_error_from_errno("asprintf");
3262 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3268 /* Recursively write new tree objects. */
3269 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3270 status_cb, status_arg, repo);
3274 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3277 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3278 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3279 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3280 got_object_qid_free(pid);
3286 /* Check if a concurrent commit to our branch has occurred. */
3287 head_ref_name = got_worktree_get_head_ref_name(worktree);
3288 if (head_ref_name == NULL) {
3289 err = got_error_from_errno("got_worktree_get_head_ref_name");
3292 /* Lock the reference here to prevent concurrent modification. */
3293 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3296 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3299 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3300 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3303 /* Update branch head in repository. */
3304 err = got_ref_change_ref(head_ref2, *new_commit_id);
3307 err = got_ref_write(head_ref2, repo);
3311 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3315 err = ref_base_commit(worktree, repo);
3319 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3323 err = update_fileindex_after_commit(&commitable_paths,
3324 *new_commit_id, fileindex, worktree);
3329 sync_err = sync_fileindex(fileindex, fileindex_path);
3330 if (sync_err && err == NULL)
3332 got_fileindex_free(fileindex);
3334 free(fileindex_path);
3335 unlockerr = lock_worktree(worktree, LOCK_SH);
3336 if (unlockerr && err == NULL)
3338 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3339 struct got_commitable *ct = pe->data;
3340 free_commitable(ct);
3342 got_pathlist_free(&commitable_paths);
3344 got_object_tree_close(head_tree);
3346 got_object_commit_close(head_commit);
3348 free(head_commit_id);
3349 free(head_commit_id2);
3351 got_ref_close(head_ref);
3353 unlockerr = got_ref_unlock(head_ref2);
3354 if (unlockerr && err == NULL)
3356 got_ref_close(head_ref2);
3362 got_commitable_get_path(struct got_commitable *ct)
3368 got_commitable_get_status(struct got_commitable *ct)
3373 struct check_rebase_ok_arg {
3374 struct got_worktree *worktree;
3375 struct got_repository *repo;
3376 int rebase_in_progress;
3379 static const struct got_error *
3380 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3382 const struct got_error *err = NULL;
3383 struct check_rebase_ok_arg *a = arg;
3384 unsigned char status;
3388 if (!a->rebase_in_progress) {
3389 /* Reject rebase of a work tree with mixed base commits. */
3390 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3391 SHA1_DIGEST_LENGTH))
3392 return got_error(GOT_ERR_MIXED_COMMITS);
3395 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3397 return got_error_from_errno("asprintf");
3399 /* Reject rebase of a work tree with modified or conflicted files. */
3400 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3405 if (a->rebase_in_progress) {
3406 if (status == GOT_STATUS_CONFLICT)
3407 return got_error(GOT_ERR_CONFLICTS);
3408 } else if (status != GOT_STATUS_NO_CHANGE)
3409 return got_error(GOT_ERR_MODIFIED);
3414 const struct got_error *
3415 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3416 struct got_reference **tmp_branch, struct got_worktree *worktree,
3417 struct got_reference *branch, struct got_repository *repo)
3419 const struct got_error *err = NULL;
3420 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3421 char *branch_ref_name = NULL;
3422 struct got_fileindex *fileindex = NULL;
3423 char *fileindex_path = NULL;
3424 struct check_rebase_ok_arg ok_arg;
3425 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3427 *new_base_branch_ref = NULL;
3430 err = lock_worktree(worktree, LOCK_EX);
3434 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3438 ok_arg.worktree = worktree;
3440 ok_arg.rebase_in_progress = 0;
3441 err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3446 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3450 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3454 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3458 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3463 err = got_ref_alloc_symref(new_base_branch_ref,
3464 new_base_branch_ref_name, wt_branch);
3467 err = got_ref_write(*new_base_branch_ref, repo);
3471 /* TODO Lock original branch's ref while rebasing? */
3473 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3477 err = got_ref_write(branch_ref, repo);
3481 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3482 worktree->base_commit_id);
3485 err = got_ref_write(*tmp_branch, repo);
3489 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3493 free(fileindex_path);
3495 got_fileindex_free(fileindex);
3496 free(tmp_branch_name);
3497 free(new_base_branch_ref_name);
3498 free(branch_ref_name);
3500 got_ref_close(branch_ref);
3502 got_ref_close(wt_branch);
3504 if (*new_base_branch_ref) {
3505 got_ref_close(*new_base_branch_ref);
3506 *new_base_branch_ref = NULL;
3509 got_ref_close(*tmp_branch);
3512 lock_worktree(worktree, LOCK_SH);
3517 const struct got_error *
3518 got_worktree_rebase_continue(struct got_object_id **commit_id,
3519 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3520 struct got_reference **branch, struct got_worktree *worktree,
3521 struct got_repository *repo)
3523 const struct got_error *err;
3524 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3525 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3526 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3530 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3534 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3538 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3542 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3546 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3550 err = got_ref_open(branch, repo,
3551 got_ref_get_symref_target(branch_ref), 0);
3555 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3559 err = got_ref_resolve(commit_id, repo, commit_ref);
3563 err = got_ref_open(new_base_branch, repo,
3564 new_base_branch_ref_name, 0);
3568 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3572 free(commit_ref_name);
3573 free(branch_ref_name);
3575 got_ref_close(commit_ref);
3577 got_ref_close(branch_ref);
3582 got_ref_close(*tmp_branch);
3585 if (*new_base_branch) {
3586 got_ref_close(*new_base_branch);
3587 *new_base_branch = NULL;
3590 got_ref_close(*branch);
3597 const struct got_error *
3598 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3600 const struct got_error *err;
3601 char *tmp_branch_name = NULL;
3603 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3607 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3608 free(tmp_branch_name);
3612 static const struct got_error *
3613 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3614 char **logmsg, void *arg)
3616 struct got_commit_object *commit = arg;
3618 *logmsg = strdup(got_object_commit_get_logmsg(commit));
3619 if (*logmsg == NULL)
3620 return got_error_from_errno("strdup");
3625 static const struct got_error *
3626 rebase_status(void *arg, unsigned char status, const char *path,
3627 struct got_object_id *blob_id, struct got_object_id *commit_id)
3632 const struct got_error *
3633 got_worktree_rebase_merge_files(struct got_worktree *worktree,
3634 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3635 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3636 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3638 const struct got_error *err;
3639 struct got_fileindex *fileindex;
3640 char *fileindex_path, *commit_ref_name = NULL;
3641 struct got_reference *commit_ref = NULL;
3643 /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3645 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3649 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3652 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3654 if (err->code != GOT_ERR_NOT_REF)
3656 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3659 err = got_ref_write(commit_ref, repo);
3663 struct got_object_id *stored_id;
3666 err = got_ref_resolve(&stored_id, repo, commit_ref);
3669 cmp = got_object_id_cmp(commit_id, stored_id);
3672 err = got_error(GOT_ERR_REBASE_COMMITID);
3677 err = merge_files(worktree, fileindex, fileindex_path,
3678 parent_commit_id, commit_id, repo, progress_cb, progress_arg,
3679 cancel_cb, cancel_arg);
3681 got_fileindex_free(fileindex);
3682 free(fileindex_path);
3684 got_ref_close(commit_ref);
3688 static const struct got_error *
3689 bump_all_base_commit_ids(struct got_worktree *worktree,
3690 struct got_object_id *base_commit_id)
3692 const struct got_error *err = NULL, *sync_err;
3693 struct got_fileindex *fileindex;
3694 char *fileindex_path;
3695 struct bump_base_commit_id_arg bbc_arg;
3697 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3701 bbc_arg.base_commit_id = base_commit_id;
3702 bbc_arg.entry_name = NULL;
3704 bbc_arg.path_len = 0;
3705 bbc_arg.progress_cb = NULL;
3706 bbc_arg.progress_arg = NULL;
3707 err = got_fileindex_for_each_entry_safe(fileindex,
3708 bump_base_commit_id, &bbc_arg);
3710 sync_err = sync_fileindex(fileindex, fileindex_path);
3711 if (sync_err && err == NULL)
3714 got_fileindex_free(fileindex);
3715 free(fileindex_path);
3719 const struct got_error *
3720 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3721 struct got_worktree *worktree, struct got_reference *tmp_branch,
3722 struct got_commit_object *orig_commit,
3723 struct got_object_id *orig_commit_id, struct got_repository *repo)
3725 const struct got_error *err;
3726 char *commit_ref_name = NULL;
3727 struct got_reference *commit_ref = NULL;
3728 struct got_object_id *commit_id = NULL;
3730 /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3732 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3735 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3738 err = got_ref_resolve(&commit_id, repo, commit_ref);
3741 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3742 err = got_error(GOT_ERR_REBASE_COMMITID);
3746 err = got_worktree_commit(new_commit_id, worktree, NULL,
3747 got_object_commit_get_author(orig_commit),
3748 got_object_commit_get_committer(orig_commit),
3749 collect_rebase_commit_msg, orig_commit,
3750 rebase_status, NULL, repo);
3752 if (err->code == GOT_ERR_COMMIT_NO_CHANGES) {
3753 /* No-op change; commit will be elided. */
3754 err = got_ref_delete(commit_ref, repo);
3757 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3762 /* Prevent out-of-date errors during rebase of subsequent commits. */
3763 err = bump_all_base_commit_ids(worktree, *new_commit_id);
3767 err = got_ref_delete(commit_ref, repo);
3771 err = got_ref_change_ref(tmp_branch, *new_commit_id);
3773 free(commit_ref_name);
3775 got_ref_close(commit_ref);
3777 free(*new_commit_id);
3778 *new_commit_id = NULL;
3783 const struct got_error *
3784 got_worktree_rebase_postpone(struct got_worktree *worktree)
3786 return lock_worktree(worktree, LOCK_SH);
3789 static const struct got_error *
3790 delete_ref(const char *name, struct got_repository *repo)
3792 const struct got_error *err;
3793 struct got_reference *ref;
3795 err = got_ref_open(&ref, repo, name, 0);
3797 if (err->code == GOT_ERR_NOT_REF)
3802 err = got_ref_delete(ref, repo);
3807 static const struct got_error *
3808 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
3810 const struct got_error *err;
3811 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3812 char *branch_ref_name = NULL, *commit_ref_name = NULL;
3814 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3817 err = delete_ref(tmp_branch_name, repo);
3821 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3824 err = delete_ref(new_base_branch_ref_name, repo);
3828 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3831 err = delete_ref(branch_ref_name, repo);
3835 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3838 err = delete_ref(commit_ref_name, repo);
3843 free(tmp_branch_name);
3844 free(new_base_branch_ref_name);
3845 free(branch_ref_name);
3846 free(commit_ref_name);
3850 const struct got_error *
3851 got_worktree_rebase_complete(struct got_worktree *worktree,
3852 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3853 struct got_reference *rebased_branch,
3854 struct got_repository *repo)
3856 const struct got_error *err, *unlockerr;
3857 struct got_object_id *new_head_commit_id = NULL;
3859 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
3863 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
3867 err = got_ref_write(rebased_branch, repo);
3871 err = got_worktree_set_head_ref(worktree, rebased_branch);
3875 err = delete_rebase_refs(worktree, repo);
3877 free(new_head_commit_id);
3878 unlockerr = lock_worktree(worktree, LOCK_SH);
3879 if (unlockerr && err == NULL)
3884 struct collect_revertible_paths_arg {
3885 struct got_pathlist_head *revertible_paths;
3886 struct got_worktree *worktree;
3889 static const struct got_error *
3890 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
3891 struct got_object_id *blob_id, struct got_object_id *commit_id)
3893 struct collect_revertible_paths_arg *a = arg;
3894 const struct got_error *err = NULL;
3895 struct got_pathlist_entry *new = NULL;
3898 if (status != GOT_STATUS_ADD &&
3899 status != GOT_STATUS_DELETE &&
3900 status != GOT_STATUS_MODIFY &&
3901 status != GOT_STATUS_CONFLICT &&
3902 status != GOT_STATUS_MISSING)
3905 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
3906 return got_error_from_errno("asprintf");
3908 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
3909 if (err || new == NULL)
3914 const struct got_error *
3915 got_worktree_rebase_abort(struct got_worktree *worktree,
3916 struct got_repository *repo, struct got_reference *new_base_branch,
3917 got_worktree_checkout_cb progress_cb, void *progress_arg)
3919 const struct got_error *err, *unlockerr;
3920 struct got_reference *resolved = NULL;
3921 struct got_object_id *commit_id = NULL;
3922 struct got_fileindex *fileindex = NULL;
3923 char *fileindex_path = NULL;
3924 struct got_pathlist_head revertible_paths;
3925 struct got_pathlist_entry *pe;
3926 struct collect_revertible_paths_arg crp_arg;
3928 TAILQ_INIT(&revertible_paths);
3930 err = lock_worktree(worktree, LOCK_EX);
3934 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3938 err = got_ref_open(&resolved, repo,
3939 got_ref_get_symref_target(new_base_branch), 0);
3943 err = got_worktree_set_head_ref(worktree, resolved);
3948 * XXX commits to the base branch could have happened while
3949 * we were busy rebasing; should we store the original commit ID
3950 * when rebase begins and read it back here?
3952 err = got_ref_resolve(&commit_id, repo, resolved);
3956 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
3960 err = got_worktree_checkout_files(worktree, "", repo, progress_cb,
3961 progress_arg, NULL, NULL);
3965 crp_arg.revertible_paths = &revertible_paths;
3966 crp_arg.worktree = worktree;
3967 err = got_worktree_status(worktree, "", repo,
3968 collect_revertible_paths, &crp_arg, NULL, NULL);
3972 TAILQ_FOREACH(pe, &revertible_paths, entry) {
3973 err = revert_file(worktree, fileindex, pe->path,
3974 progress_cb, progress_arg, repo);
3979 err = delete_rebase_refs(worktree, repo);
3981 got_ref_close(resolved);
3984 got_fileindex_free(fileindex);
3985 free(fileindex_path);
3986 TAILQ_FOREACH(pe, &revertible_paths, entry)
3987 free((char *)pe->path);
3988 got_pathlist_free(&revertible_paths);
3990 unlockerr = lock_worktree(worktree, LOCK_SH);
3991 if (unlockerr && err == NULL)