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;
1026 static const struct got_error *
1027 get_file_status(unsigned char *status, struct stat *sb,
1028 struct got_fileindex_entry *ie, const char *abspath,
1029 struct got_repository *repo)
1031 const struct got_error *err = NULL;
1032 struct got_object_id id;
1036 struct got_blob_object *blob = NULL;
1039 *status = GOT_STATUS_NO_CHANGE;
1041 if (lstat(abspath, sb) == -1) {
1042 if (errno == ENOENT) {
1044 if (got_fileindex_entry_has_file_on_disk(ie))
1045 *status = GOT_STATUS_MISSING;
1047 *status = GOT_STATUS_DELETE;
1049 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1050 & (S_IRWXU | S_IRWXG | S_IRWXO));
1052 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1055 return got_error_from_errno2("lstat", abspath);
1058 if (!S_ISREG(sb->st_mode)) {
1059 *status = GOT_STATUS_OBSTRUCTED;
1066 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1067 *status = GOT_STATUS_DELETE;
1069 } else if (!got_fileindex_entry_has_blob(ie)) {
1070 *status = GOT_STATUS_ADD;
1074 if (ie->ctime_sec == sb->st_ctime &&
1075 ie->ctime_nsec == sb->st_ctimensec &&
1076 ie->mtime_sec == sb->st_mtime &&
1077 ie->mtime_nsec == sb->st_mtimensec &&
1078 ie->size == (sb->st_size & 0xffffffff))
1081 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1082 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1086 f = fopen(abspath, "r");
1088 err = got_error_from_errno2("fopen", abspath);
1091 hdrlen = got_object_blob_get_hdrlen(blob);
1093 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1094 err = got_object_blob_read_block(&blen, blob);
1097 /* Skip length of blob object header first time around. */
1098 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1099 if (flen == 0 && ferror(f)) {
1100 err = got_error_from_errno("fread");
1105 *status = GOT_STATUS_MODIFY;
1107 } else if (flen == 0) {
1109 *status = GOT_STATUS_MODIFY;
1111 } else if (blen - hdrlen == flen) {
1112 /* Skip blob object header first time around. */
1113 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1114 *status = GOT_STATUS_MODIFY;
1118 *status = GOT_STATUS_MODIFY;
1124 if (*status == GOT_STATUS_MODIFY) {
1126 err = get_modified_file_content_status(status, f);
1130 got_object_blob_close(blob);
1136 static const struct got_error *
1137 update_blob(struct got_worktree *worktree,
1138 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1139 struct got_tree_entry *te, const char *path,
1140 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1143 const struct got_error *err = NULL;
1144 struct got_blob_object *blob = NULL;
1146 unsigned char status = GOT_STATUS_NO_CHANGE;
1149 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1150 return got_error_from_errno("asprintf");
1152 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1156 if (status == GOT_STATUS_OBSTRUCTED) {
1157 err = (*progress_cb)(progress_arg, status, path);
1161 if (ie && status != GOT_STATUS_MISSING) {
1162 if (got_fileindex_entry_has_commit(ie) &&
1163 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1164 SHA1_DIGEST_LENGTH) == 0) {
1165 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1169 if (got_fileindex_entry_has_blob(ie) &&
1170 memcmp(ie->blob_sha1, te->id->sha1,
1171 SHA1_DIGEST_LENGTH) == 0)
1175 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1179 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1180 int update_timestamps;
1181 struct got_blob_object *blob2 = NULL;
1182 if (got_fileindex_entry_has_blob(ie)) {
1183 struct got_object_id id2;
1184 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1185 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1189 err = merge_blob(&update_timestamps, worktree, blob2,
1190 ondisk_path, path, sb.st_mode, blob,
1191 worktree->base_commit_id, repo,
1192 progress_cb, progress_arg);
1194 got_object_blob_close(blob2);
1196 * Do not update timestamps of files with local changes.
1197 * Otherwise, a future status walk would treat them as
1198 * unmodified files again.
1200 err = got_fileindex_entry_update(ie, ondisk_path,
1201 blob->id.sha1, worktree->base_commit_id->sha1,
1203 } else if (status == GOT_STATUS_DELETE) {
1204 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1207 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1208 ondisk_path, path, blob, 0);
1212 err = install_blob(worktree, ondisk_path, path, te->mode,
1213 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1214 repo, progress_cb, progress_arg);
1217 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1218 ondisk_path, path, blob, 1);
1222 got_object_blob_close(blob);
1228 static const struct got_error *
1229 remove_ondisk_file(const char *root_path, const char *path)
1231 const struct got_error *err = NULL;
1232 char *ondisk_path = NULL;
1234 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1235 return got_error_from_errno("asprintf");
1237 if (unlink(ondisk_path) == -1) {
1238 if (errno != ENOENT)
1239 err = got_error_from_errno2("unlink", ondisk_path);
1241 char *parent = dirname(ondisk_path);
1242 while (parent && strcmp(parent, root_path) != 0) {
1243 if (rmdir(parent) == -1) {
1244 if (errno != ENOTEMPTY)
1245 err = got_error_from_errno2("rmdir",
1249 parent = dirname(parent);
1256 static const struct got_error *
1257 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1258 struct got_fileindex_entry *ie, struct got_repository *repo,
1259 got_worktree_checkout_cb progress_cb, void *progress_arg)
1261 const struct got_error *err = NULL;
1262 unsigned char status;
1266 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1268 return got_error_from_errno("asprintf");
1270 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1274 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1275 status == GOT_STATUS_ADD) {
1276 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1280 * Preserve the working file and change the deleted blob's
1281 * entry into a schedule-add entry.
1283 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1288 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1291 if (status == GOT_STATUS_NO_CHANGE) {
1292 err = remove_ondisk_file(worktree->root_path, ie->path);
1296 got_fileindex_entry_remove(fileindex, ie);
1302 struct diff_cb_arg {
1303 struct got_fileindex *fileindex;
1304 struct got_worktree *worktree;
1305 struct got_repository *repo;
1306 got_worktree_checkout_cb progress_cb;
1308 got_worktree_cancel_cb cancel_cb;
1312 static const struct got_error *
1313 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1314 struct got_tree_entry *te, const char *parent_path)
1316 struct diff_cb_arg *a = arg;
1318 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1319 return got_error(GOT_ERR_CANCELLED);
1321 return update_blob(a->worktree, a->fileindex, ie, te,
1322 ie->path, a->repo, a->progress_cb, a->progress_arg);
1325 static const struct got_error *
1326 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1328 struct diff_cb_arg *a = arg;
1330 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1331 return got_error(GOT_ERR_CANCELLED);
1333 return delete_blob(a->worktree, a->fileindex, ie,
1334 a->repo, a->progress_cb, a->progress_arg);
1337 static const struct got_error *
1338 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1340 struct diff_cb_arg *a = arg;
1341 const struct got_error *err;
1344 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1345 return got_error(GOT_ERR_CANCELLED);
1347 if (asprintf(&path, "%s%s%s", parent_path,
1348 parent_path[0] ? "/" : "", te->name)
1350 return got_error_from_errno("asprintf");
1352 if (S_ISDIR(te->mode))
1353 err = add_dir_on_disk(a->worktree, path);
1355 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1356 a->repo, a->progress_cb, a->progress_arg);
1362 static const struct got_error *
1363 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1365 const struct got_error *err = NULL;
1366 char *uuidstr = NULL;
1367 uint32_t uuid_status;
1371 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1372 if (uuid_status != uuid_s_ok)
1373 return got_error_uuid(uuid_status);
1375 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1377 err = got_error_from_errno("asprintf");
1384 const struct got_error *
1385 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1387 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1390 static const struct got_error *
1391 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1393 return get_ref_name(refname, worktree,
1394 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1397 static const struct got_error *
1398 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1400 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1403 static const struct got_error *
1404 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1406 return get_ref_name(refname, worktree,
1407 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1410 static const struct got_error *
1411 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1413 return get_ref_name(refname, worktree,
1414 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1419 * Prevent Git's garbage collector from deleting our base commit by
1420 * setting a reference to our base commit's ID.
1422 static const struct got_error *
1423 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1425 const struct got_error *err = NULL;
1426 struct got_reference *ref = NULL;
1429 err = got_worktree_get_base_ref_name(&refname, worktree);
1433 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1437 err = got_ref_write(ref, repo);
1445 static const struct got_error *
1446 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1447 struct got_worktree *worktree)
1449 const struct got_error *err = NULL;
1452 *fileindex_path = NULL;
1453 *fileindex = got_fileindex_alloc();
1454 if (*fileindex == NULL)
1455 return got_error_from_errno("got_fileindex_alloc");
1457 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1458 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1459 err = got_error_from_errno("asprintf");
1460 *fileindex_path = NULL;
1464 index = fopen(*fileindex_path, "rb");
1465 if (index == NULL) {
1466 if (errno != ENOENT)
1467 err = got_error_from_errno2("fopen", *fileindex_path);
1469 err = got_fileindex_read(*fileindex, index);
1470 if (fclose(index) != 0 && err == NULL)
1471 err = got_error_from_errno("fclose");
1475 free(*fileindex_path);
1476 *fileindex_path = NULL;
1477 got_fileindex_free(*fileindex);
1483 struct bump_base_commit_id_arg {
1484 struct got_object_id *base_commit_id;
1487 const char *entry_name;
1488 got_worktree_checkout_cb progress_cb;
1492 /* Bump base commit ID of all files within an updated part of the work tree. */
1493 static const struct got_error *
1494 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1496 const struct got_error *err;
1497 struct bump_base_commit_id_arg *a = arg;
1499 if (a->entry_name) {
1500 if (strcmp(ie->path, a->path) != 0)
1502 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1505 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1506 SHA1_DIGEST_LENGTH) == 0)
1509 if (a->progress_cb) {
1510 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1515 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1519 static const struct got_error *
1520 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1522 const struct got_error *err = NULL;
1523 char *new_fileindex_path = NULL;
1524 FILE *new_index = NULL;
1526 err = got_opentemp_named(&new_fileindex_path, &new_index,
1531 err = got_fileindex_write(fileindex, new_index);
1535 if (rename(new_fileindex_path, fileindex_path) != 0) {
1536 err = got_error_from_errno3("rename", new_fileindex_path,
1538 unlink(new_fileindex_path);
1543 free(new_fileindex_path);
1547 static const struct got_error *
1548 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1549 struct got_object_id **tree_id, const char *wt_relpath,
1550 struct got_worktree *worktree, struct got_repository *repo)
1552 const struct got_error *err = NULL;
1553 struct got_object_id *id = NULL;
1554 char *in_repo_path = NULL;
1555 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1557 *entry_type = GOT_OBJ_TYPE_ANY;
1558 *tree_relpath = NULL;
1561 if (wt_relpath[0] == '\0') {
1562 /* Check out all files within the work tree. */
1563 *entry_type = GOT_OBJ_TYPE_TREE;
1564 *tree_relpath = strdup("");
1565 if (*tree_relpath == NULL) {
1566 err = got_error_from_errno("strdup");
1569 err = got_object_id_by_path(tree_id, repo,
1570 worktree->base_commit_id, worktree->path_prefix);
1576 /* Check out a subset of files in the work tree. */
1578 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1579 is_root_wt ? "" : "/", wt_relpath) == -1) {
1580 err = got_error_from_errno("asprintf");
1584 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1590 in_repo_path = NULL;
1592 err = got_object_get_type(entry_type, repo, id);
1596 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1597 /* Check out a single file. */
1598 if (strchr(wt_relpath, '/') == NULL) {
1599 /* Check out a single file in work tree's root dir. */
1600 in_repo_path = strdup(worktree->path_prefix);
1601 if (in_repo_path == NULL) {
1602 err = got_error_from_errno("strdup");
1605 *tree_relpath = strdup("");
1606 if (*tree_relpath == NULL) {
1607 err = got_error_from_errno("strdup");
1611 /* Check out a single file in a subdirectory. */
1612 err = got_path_dirname(tree_relpath, wt_relpath);
1615 if (asprintf(&in_repo_path, "%s%s%s",
1616 worktree->path_prefix, is_root_wt ? "" : "/",
1617 *tree_relpath) == -1) {
1618 err = got_error_from_errno("asprintf");
1622 err = got_object_id_by_path(tree_id, repo,
1623 worktree->base_commit_id, in_repo_path);
1625 /* Check out all files within a subdirectory. */
1626 *tree_id = got_object_id_dup(id);
1627 if (*tree_id == NULL) {
1628 err = got_error_from_errno("got_object_id_dup");
1631 *tree_relpath = strdup(wt_relpath);
1632 if (*tree_relpath == NULL) {
1633 err = got_error_from_errno("strdup");
1641 *entry_type = GOT_OBJ_TYPE_ANY;
1642 free(*tree_relpath);
1643 *tree_relpath = NULL;
1650 static const struct got_error *
1651 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1652 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1653 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1654 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1656 const struct got_error *err = NULL;
1657 struct got_commit_object *commit = NULL;
1658 struct got_tree_object *tree = NULL;
1659 struct got_fileindex_diff_tree_cb diff_cb;
1660 struct diff_cb_arg arg;
1662 err = ref_base_commit(worktree, repo);
1666 err = got_object_open_as_commit(&commit, repo,
1667 worktree->base_commit_id);
1671 err = got_object_open_as_tree(&tree, repo, tree_id);
1676 got_object_tree_find_entry(tree, entry_name) == NULL) {
1677 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1681 diff_cb.diff_old_new = diff_old_new;
1682 diff_cb.diff_old = diff_old;
1683 diff_cb.diff_new = diff_new;
1684 arg.fileindex = fileindex;
1685 arg.worktree = worktree;
1687 arg.progress_cb = progress_cb;
1688 arg.progress_arg = progress_arg;
1689 arg.cancel_cb = cancel_cb;
1690 arg.cancel_arg = cancel_arg;
1691 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1692 entry_name, repo, &diff_cb, &arg);
1695 got_object_tree_close(tree);
1697 got_object_commit_close(commit);
1701 const struct got_error *
1702 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1703 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1704 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1706 const struct got_error *err = NULL, *sync_err, *unlockerr;
1707 struct got_commit_object *commit = NULL;
1708 struct got_object_id *tree_id = NULL;
1709 struct got_tree_object *tree = NULL;
1710 struct got_fileindex *fileindex = NULL;
1711 char *fileindex_path = NULL;
1712 char *relpath = NULL, *entry_name = NULL;
1715 err = lock_worktree(worktree, LOCK_EX);
1719 err = find_tree_entry_for_checkout(&entry_type, &relpath, &tree_id,
1720 path, worktree, repo);
1724 if (entry_type == GOT_OBJ_TYPE_BLOB) {
1725 entry_name = basename(path);
1726 if (entry_name == NULL) {
1727 err = got_error_from_errno2("basename", path);
1733 * Read the file index.
1734 * Checking out files is supposed to be an idempotent operation.
1735 * If the on-disk file index is incomplete we will try to complete it.
1737 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1741 err = checkout_files(worktree, fileindex, relpath, tree_id, entry_name,
1742 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1744 struct bump_base_commit_id_arg bbc_arg;
1745 bbc_arg.base_commit_id = worktree->base_commit_id;
1746 bbc_arg.entry_name = entry_name;
1747 bbc_arg.path = path;
1748 bbc_arg.path_len = strlen(path);
1749 bbc_arg.progress_cb = progress_cb;
1750 bbc_arg.progress_arg = progress_arg;
1751 err = got_fileindex_for_each_entry_safe(fileindex,
1752 bump_base_commit_id, &bbc_arg);
1754 sync_err = sync_fileindex(fileindex, fileindex_path);
1755 if (sync_err && err == NULL)
1758 free(fileindex_path);
1761 got_object_tree_close(tree);
1763 got_object_commit_close(commit);
1765 got_fileindex_free(fileindex);
1766 unlockerr = lock_worktree(worktree, LOCK_SH);
1767 if (unlockerr && err == NULL)
1772 struct merge_file_cb_arg {
1773 struct got_worktree *worktree;
1774 struct got_fileindex *fileindex;
1775 got_worktree_checkout_cb progress_cb;
1777 got_worktree_cancel_cb cancel_cb;
1779 struct got_object_id *commit_id2;
1782 static const struct got_error *
1783 merge_file_cb(void *arg, struct got_blob_object *blob1,
1784 struct got_blob_object *blob2, struct got_object_id *id1,
1785 struct got_object_id *id2, const char *path1, const char *path2,
1786 struct got_repository *repo)
1788 static const struct got_error *err = NULL;
1789 struct merge_file_cb_arg *a = arg;
1790 struct got_fileindex_entry *ie;
1791 char *ondisk_path = NULL;
1793 unsigned char status;
1794 int local_changes_subsumed;
1796 if (blob1 && blob2) {
1797 ie = got_fileindex_entry_get(a->fileindex, path2);
1799 return (*a->progress_cb)(a->progress_arg,
1800 GOT_STATUS_MISSING, path2);
1802 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1804 return got_error_from_errno("asprintf");
1806 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1810 if (status == GOT_STATUS_DELETE) {
1811 err = (*a->progress_cb)(a->progress_arg,
1812 GOT_STATUS_MERGE, path2);
1815 if (status != GOT_STATUS_NO_CHANGE &&
1816 status != GOT_STATUS_MODIFY &&
1817 status != GOT_STATUS_CONFLICT &&
1818 status != GOT_STATUS_ADD) {
1819 err = (*a->progress_cb)(a->progress_arg, status, path2);
1823 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1824 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1825 a->progress_cb, a->progress_arg);
1827 ie = got_fileindex_entry_get(a->fileindex, path1);
1829 return (*a->progress_cb)(a->progress_arg,
1830 GOT_STATUS_MISSING, path2);
1832 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1834 return got_error_from_errno("asprintf");
1836 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1841 case GOT_STATUS_NO_CHANGE:
1842 err = (*a->progress_cb)(a->progress_arg,
1843 GOT_STATUS_DELETE, path1);
1846 err = remove_ondisk_file(a->worktree->root_path, path1);
1850 got_fileindex_entry_mark_deleted_from_disk(ie);
1852 case GOT_STATUS_DELETE:
1853 case GOT_STATUS_MISSING:
1854 err = (*a->progress_cb)(a->progress_arg,
1855 GOT_STATUS_DELETE, path1);
1859 got_fileindex_entry_mark_deleted_from_disk(ie);
1861 case GOT_STATUS_ADD:
1862 case GOT_STATUS_MODIFY:
1863 case GOT_STATUS_CONFLICT:
1864 err = (*a->progress_cb)(a->progress_arg,
1865 GOT_STATUS_CANNOT_DELETE, path1);
1869 case GOT_STATUS_OBSTRUCTED:
1870 err = (*a->progress_cb)(a->progress_arg, status, path1);
1878 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1880 return got_error_from_errno("asprintf");
1881 ie = got_fileindex_entry_get(a->fileindex, path2);
1883 err = get_file_status(&status, &sb, ie, ondisk_path,
1887 if (status != GOT_STATUS_NO_CHANGE &&
1888 status != GOT_STATUS_MODIFY &&
1889 status != GOT_STATUS_CONFLICT &&
1890 status != GOT_STATUS_ADD) {
1891 err = (*a->progress_cb)(a->progress_arg,
1895 err = merge_blob(&local_changes_subsumed, a->worktree,
1896 NULL, ondisk_path, path2, sb.st_mode, blob2,
1897 a->commit_id2, repo,
1898 a->progress_cb, a->progress_arg);
1899 if (status == GOT_STATUS_DELETE) {
1900 err = update_blob_fileindex_entry(a->worktree,
1901 a->fileindex, ie, ondisk_path, ie->path,
1907 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1908 err = install_blob(a->worktree, ondisk_path, path2,
1909 /* XXX get this from parent tree! */
1910 GOT_DEFAULT_FILE_MODE,
1911 sb.st_mode, blob2, 0, 0, repo,
1912 a->progress_cb, a->progress_arg);
1915 err = got_fileindex_entry_alloc(&ie,
1916 ondisk_path, path2, NULL, NULL);
1919 err = got_fileindex_entry_add(a->fileindex, ie);
1921 got_fileindex_entry_free(ie);
1931 struct check_merge_ok_arg {
1932 struct got_worktree *worktree;
1933 struct got_repository *repo;
1936 static const struct got_error *
1937 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1939 const struct got_error *err = NULL;
1940 struct check_merge_ok_arg *a = arg;
1941 unsigned char status;
1945 /* Reject merges into a work tree with mixed base commits. */
1946 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1947 SHA1_DIGEST_LENGTH))
1948 return got_error(GOT_ERR_MIXED_COMMITS);
1950 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1952 return got_error_from_errno("asprintf");
1954 /* Reject merges into a work tree with conflicted files. */
1955 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1958 if (status == GOT_STATUS_CONFLICT)
1959 return got_error(GOT_ERR_CONFLICTS);
1964 static const struct got_error *
1965 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1966 const char *fileindex_path, struct got_object_id *commit_id1,
1967 struct got_object_id *commit_id2, struct got_repository *repo,
1968 got_worktree_checkout_cb progress_cb, void *progress_arg,
1969 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1971 const struct got_error *err = NULL, *sync_err;
1972 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1973 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1974 struct merge_file_cb_arg arg;
1977 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1978 worktree->path_prefix);
1982 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1987 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1988 worktree->path_prefix);
1992 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1996 arg.worktree = worktree;
1997 arg.fileindex = fileindex;
1998 arg.progress_cb = progress_cb;
1999 arg.progress_arg = progress_arg;
2000 arg.cancel_cb = cancel_cb;
2001 arg.cancel_arg = cancel_arg;
2002 arg.commit_id2 = commit_id2;
2003 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2004 sync_err = sync_fileindex(fileindex, fileindex_path);
2005 if (sync_err && err == NULL)
2009 got_object_tree_close(tree1);
2011 got_object_tree_close(tree2);
2015 const struct got_error *
2016 got_worktree_merge_files(struct got_worktree *worktree,
2017 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2018 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2019 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2021 const struct got_error *err, *unlockerr;
2022 char *fileindex_path = NULL;
2023 struct got_fileindex *fileindex = NULL;
2024 struct check_merge_ok_arg mok_arg;
2026 err = lock_worktree(worktree, LOCK_EX);
2030 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2034 mok_arg.worktree = worktree;
2035 mok_arg.repo = repo;
2036 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2041 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2042 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2045 got_fileindex_free(fileindex);
2046 free(fileindex_path);
2047 unlockerr = lock_worktree(worktree, LOCK_SH);
2048 if (unlockerr && err == NULL)
2053 struct diff_dir_cb_arg {
2054 struct got_fileindex *fileindex;
2055 struct got_worktree *worktree;
2056 const char *status_path;
2057 size_t status_path_len;
2058 struct got_repository *repo;
2059 got_worktree_status_cb status_cb;
2061 got_worktree_cancel_cb cancel_cb;
2065 static const struct got_error *
2066 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2067 got_worktree_status_cb status_cb, void *status_arg,
2068 struct got_repository *repo)
2070 const struct got_error *err = NULL;
2071 unsigned char status = GOT_STATUS_NO_CHANGE;
2073 struct got_object_id blob_id, commit_id;
2075 err = get_file_status(&status, &sb, ie, abspath, repo);
2076 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2077 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2078 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2079 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2085 static const struct got_error *
2086 status_old_new(void *arg, struct got_fileindex_entry *ie,
2087 struct dirent *de, const char *parent_path)
2089 const struct got_error *err = NULL;
2090 struct diff_dir_cb_arg *a = arg;
2093 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2094 return got_error(GOT_ERR_CANCELLED);
2096 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2097 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2100 if (parent_path[0]) {
2101 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2102 parent_path, de->d_name) == -1)
2103 return got_error_from_errno("asprintf");
2105 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2107 return got_error_from_errno("asprintf");
2110 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2116 static const struct got_error *
2117 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2119 struct diff_dir_cb_arg *a = arg;
2120 struct got_object_id blob_id, commit_id;
2121 unsigned char status;
2123 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2124 return got_error(GOT_ERR_CANCELLED);
2126 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2129 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2130 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2131 if (got_fileindex_entry_has_file_on_disk(ie))
2132 status = GOT_STATUS_MISSING;
2134 status = GOT_STATUS_DELETE;
2135 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2139 static const struct got_error *
2140 status_new(void *arg, struct dirent *de, const char *parent_path)
2142 const struct got_error *err = NULL;
2143 struct diff_dir_cb_arg *a = arg;
2146 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 return got_error(GOT_ERR_CANCELLED);
2149 if (de->d_type == DT_DIR)
2152 /* XXX ignore symlinks for now */
2153 if (de->d_type == DT_LNK)
2156 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2159 if (parent_path[0]) {
2160 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2161 return got_error_from_errno("asprintf");
2166 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2173 static const struct got_error *
2174 worktree_status(struct got_worktree *worktree, const char *path,
2175 struct got_fileindex *fileindex, struct got_repository *repo,
2176 got_worktree_status_cb status_cb, void *status_arg,
2177 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2179 const struct got_error *err = NULL;
2180 DIR *workdir = NULL;
2181 struct got_fileindex_diff_dir_cb fdiff_cb;
2182 struct diff_dir_cb_arg arg;
2183 char *ondisk_path = NULL;
2185 if (asprintf(&ondisk_path, "%s%s%s",
2186 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2187 err = got_error_from_errno("asprintf");
2190 workdir = opendir(ondisk_path);
2191 if (workdir == NULL) {
2192 if (errno == ENOTDIR || errno == ENOENT) {
2193 struct got_fileindex_entry *ie;
2194 ie = got_fileindex_entry_get(fileindex, path);
2196 err = got_error(GOT_ERR_BAD_PATH);
2199 err = report_file_status(ie, ondisk_path,
2200 status_cb, status_arg, repo);
2203 err = got_error_from_errno2("opendir", ondisk_path);
2207 fdiff_cb.diff_old_new = status_old_new;
2208 fdiff_cb.diff_old = status_old;
2209 fdiff_cb.diff_new = status_new;
2210 arg.fileindex = fileindex;
2211 arg.worktree = worktree;
2212 arg.status_path = path;
2213 arg.status_path_len = strlen(path);
2215 arg.status_cb = status_cb;
2216 arg.status_arg = status_arg;
2217 arg.cancel_cb = cancel_cb;
2218 arg.cancel_arg = cancel_arg;
2219 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2220 path, repo, &fdiff_cb, &arg);
2228 const struct got_error *
2229 got_worktree_status(struct got_worktree *worktree, const char *path,
2230 struct got_repository *repo, got_worktree_status_cb status_cb,
2231 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2233 const struct got_error *err = NULL;
2234 char *fileindex_path = NULL;
2235 struct got_fileindex *fileindex = NULL;
2237 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2241 err = worktree_status(worktree, path, fileindex, repo,
2242 status_cb, status_arg, cancel_cb, cancel_arg);
2243 free(fileindex_path);
2244 got_fileindex_free(fileindex);
2248 const struct got_error *
2249 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2252 const struct got_error *err = NULL;
2253 char *resolved, *path = NULL;
2258 resolved = realpath(arg, NULL);
2259 if (resolved == NULL)
2260 return got_error_from_errno2("realpath", arg);
2262 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2263 strlen(got_worktree_get_root_path(worktree)))) {
2264 err = got_error(GOT_ERR_BAD_PATH);
2268 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2269 err = got_path_skip_common_ancestor(&path,
2270 got_worktree_get_root_path(worktree), resolved);
2276 err = got_error_from_errno("strdup");
2281 /* XXX status walk can't deal with trailing slash! */
2283 while (path[len - 1] == '/') {
2284 path[len - 1] = '\0';
2296 static const struct got_error *
2297 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2298 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2299 struct got_repository *repo)
2301 const struct got_error *err = NULL;
2302 struct got_fileindex_entry *ie;
2304 /* Re-adding an existing entry is a no-op. */
2305 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2308 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2312 err = got_fileindex_entry_add(fileindex, ie);
2314 got_fileindex_entry_free(ie);
2318 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2321 const struct got_error *
2322 got_worktree_schedule_add(struct got_worktree *worktree,
2323 struct got_pathlist_head *ondisk_paths,
2324 got_worktree_status_cb status_cb, void *status_arg,
2325 struct got_repository *repo)
2327 struct got_fileindex *fileindex = NULL;
2328 char *fileindex_path = NULL;
2329 const struct got_error *err = NULL, *sync_err, *unlockerr;
2330 struct got_pathlist_entry *pe;
2332 err = lock_worktree(worktree, LOCK_EX);
2336 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2340 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2342 err = got_path_skip_common_ancestor(&relpath,
2343 got_worktree_get_root_path(worktree), pe->path);
2346 err = schedule_addition(pe->path, fileindex, relpath,
2347 status_cb, status_arg, repo);
2352 sync_err = sync_fileindex(fileindex, fileindex_path);
2353 if (sync_err && err == NULL)
2356 free(fileindex_path);
2358 got_fileindex_free(fileindex);
2359 unlockerr = lock_worktree(worktree, LOCK_SH);
2360 if (unlockerr && err == NULL)
2365 static const struct got_error *
2366 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2367 const char *relpath, int delete_local_mods,
2368 got_worktree_status_cb status_cb, void *status_arg,
2369 struct got_repository *repo)
2371 const struct got_error *err = NULL;
2372 struct got_fileindex_entry *ie = NULL;
2373 unsigned char status;
2376 ie = got_fileindex_entry_get(fileindex, relpath);
2378 return got_error(GOT_ERR_BAD_PATH);
2380 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2384 if (status != GOT_STATUS_NO_CHANGE) {
2385 if (status == GOT_STATUS_DELETE)
2386 return got_error_set_errno(ENOENT, ondisk_path);
2387 if (status != GOT_STATUS_MODIFY)
2388 return got_error(GOT_ERR_FILE_STATUS);
2389 if (!delete_local_mods)
2390 return got_error(GOT_ERR_FILE_MODIFIED);
2393 if (unlink(ondisk_path) != 0)
2394 return got_error_from_errno2("unlink", ondisk_path);
2396 got_fileindex_entry_mark_deleted_from_disk(ie);
2397 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2400 const struct got_error *
2401 got_worktree_schedule_delete(struct got_worktree *worktree,
2402 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2403 got_worktree_status_cb status_cb, void *status_arg,
2404 struct got_repository *repo)
2406 struct got_fileindex *fileindex = NULL;
2407 char *fileindex_path = NULL;
2408 const struct got_error *err = NULL, *sync_err, *unlockerr;
2409 struct got_pathlist_entry *pe;
2411 err = lock_worktree(worktree, LOCK_EX);
2415 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2419 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2421 err = got_path_skip_common_ancestor(&relpath,
2422 got_worktree_get_root_path(worktree), pe->path);
2425 err = schedule_for_deletion(pe->path, fileindex, relpath,
2426 delete_local_mods, status_cb, status_arg, repo);
2431 sync_err = sync_fileindex(fileindex, fileindex_path);
2432 if (sync_err && err == NULL)
2435 free(fileindex_path);
2437 got_fileindex_free(fileindex);
2438 unlockerr = lock_worktree(worktree, LOCK_SH);
2439 if (unlockerr && err == NULL)
2444 static const struct got_error *
2445 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2446 const char *ondisk_path,
2447 got_worktree_checkout_cb progress_cb, void *progress_arg,
2448 struct got_repository *repo)
2450 const struct got_error *err = NULL;
2451 char *relpath = NULL, *parent_path = NULL;
2452 struct got_fileindex_entry *ie;
2453 struct got_tree_object *tree = NULL;
2454 struct got_object_id *tree_id = NULL;
2455 const struct got_tree_entry *te;
2456 char *tree_path = NULL, *te_name;
2457 struct got_blob_object *blob = NULL;
2458 unsigned char status;
2461 err = got_path_skip_common_ancestor(&relpath,
2462 got_worktree_get_root_path(worktree), ondisk_path);
2466 ie = got_fileindex_entry_get(fileindex, relpath);
2468 err = got_error(GOT_ERR_BAD_PATH);
2472 /* Construct in-repository path of tree which contains this blob. */
2473 err = got_path_dirname(&parent_path, ie->path);
2475 if (err->code != GOT_ERR_BAD_PATH)
2477 parent_path = strdup("/");
2478 if (parent_path == NULL) {
2479 err = got_error_from_errno("strdup");
2483 if (got_path_is_root_dir(worktree->path_prefix)) {
2484 tree_path = strdup(parent_path);
2485 if (tree_path == NULL) {
2486 err = got_error_from_errno("strdup");
2490 if (got_path_is_root_dir(parent_path)) {
2491 tree_path = strdup(worktree->path_prefix);
2492 if (tree_path == NULL) {
2493 err = got_error_from_errno("strdup");
2497 if (asprintf(&tree_path, "%s/%s",
2498 worktree->path_prefix, parent_path) == -1) {
2499 err = got_error_from_errno("asprintf");
2505 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2510 err = got_object_open_as_tree(&tree, repo, tree_id);
2514 te_name = basename(ie->path);
2515 if (te_name == NULL) {
2516 err = got_error_from_errno2("basename", ie->path);
2520 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2524 te = got_object_tree_find_entry(tree, te_name);
2525 if (te == NULL && status != GOT_STATUS_ADD) {
2526 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2531 case GOT_STATUS_ADD:
2532 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2535 got_fileindex_entry_remove(fileindex, ie);
2537 case GOT_STATUS_DELETE:
2538 case GOT_STATUS_MODIFY:
2539 case GOT_STATUS_CONFLICT:
2540 case GOT_STATUS_MISSING: {
2541 struct got_object_id id;
2542 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2543 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2546 err = install_blob(worktree, ondisk_path, ie->path,
2547 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2551 if (status == GOT_STATUS_DELETE) {
2552 err = update_blob_fileindex_entry(worktree,
2553 fileindex, ie, ondisk_path, ie->path, blob, 1);
2567 got_object_blob_close(blob);
2569 got_object_tree_close(tree);
2574 const struct got_error *
2575 got_worktree_revert(struct got_worktree *worktree,
2576 struct got_pathlist_head *ondisk_paths,
2577 got_worktree_checkout_cb progress_cb, void *progress_arg,
2578 struct got_repository *repo)
2580 struct got_fileindex *fileindex = NULL;
2581 char *fileindex_path = NULL;
2582 const struct got_error *err = NULL, *unlockerr = NULL;
2583 const struct got_error *sync_err = NULL;
2584 struct got_pathlist_entry *pe;
2586 err = lock_worktree(worktree, LOCK_EX);
2590 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2594 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2595 err = revert_file(worktree, fileindex, pe->path,
2596 progress_cb, progress_arg, repo);
2600 sync_err = sync_fileindex(fileindex, fileindex_path);
2601 if (sync_err && err == NULL)
2604 free(fileindex_path);
2606 got_fileindex_free(fileindex);
2607 unlockerr = lock_worktree(worktree, LOCK_SH);
2608 if (unlockerr && err == NULL)
2614 free_commitable(struct got_commitable *ct)
2617 free(ct->in_repo_path);
2618 free(ct->ondisk_path);
2620 free(ct->base_blob_id);
2621 free(ct->base_commit_id);
2625 struct collect_commitables_arg {
2626 struct got_pathlist_head *commitable_paths;
2627 struct got_repository *repo;
2628 struct got_worktree *worktree;
2631 static const struct got_error *
2632 collect_commitables(void *arg, unsigned char status, const char *relpath,
2633 struct got_object_id *blob_id, struct got_object_id *commit_id)
2635 struct collect_commitables_arg *a = arg;
2636 const struct got_error *err = NULL;
2637 struct got_commitable *ct = NULL;
2638 struct got_pathlist_entry *new = NULL;
2639 char *parent_path = NULL, *path = NULL;
2642 if (status == GOT_STATUS_CONFLICT)
2643 return got_error(GOT_ERR_COMMIT_CONFLICT);
2645 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2646 status != GOT_STATUS_DELETE)
2649 if (asprintf(&path, "/%s", relpath) == -1) {
2650 err = got_error_from_errno("asprintf");
2653 if (strcmp(path, "/") == 0) {
2654 parent_path = strdup("");
2655 if (parent_path == NULL)
2656 return got_error_from_errno("strdup");
2658 err = got_path_dirname(&parent_path, path);
2663 ct = calloc(1, sizeof(*ct));
2665 err = got_error_from_errno("calloc");
2669 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2671 err = got_error_from_errno("asprintf");
2674 if (status == GOT_STATUS_DELETE) {
2675 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2677 if (lstat(ct->ondisk_path, &sb) != 0) {
2678 err = got_error_from_errno2("lstat", ct->ondisk_path);
2681 ct->mode = sb.st_mode;
2684 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2685 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2687 err = got_error_from_errno("asprintf");
2691 ct->status = status;
2692 ct->blob_id = NULL; /* will be filled in when blob gets created */
2693 if (ct->status != GOT_STATUS_ADD) {
2694 ct->base_blob_id = got_object_id_dup(blob_id);
2695 if (ct->base_blob_id == NULL) {
2696 err = got_error_from_errno("got_object_id_dup");
2699 ct->base_commit_id = got_object_id_dup(commit_id);
2700 if (ct->base_commit_id == NULL) {
2701 err = got_error_from_errno("got_object_id_dup");
2705 ct->path = strdup(path);
2706 if (ct->path == NULL) {
2707 err = got_error_from_errno("strdup");
2710 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2712 if (ct && (err || new == NULL))
2713 free_commitable(ct);
2719 static const struct got_error *write_tree(struct got_object_id **,
2720 struct got_tree_object *, const char *, struct got_pathlist_head *,
2721 got_worktree_status_cb status_cb, void *status_arg,
2722 struct got_repository *);
2724 static const struct got_error *
2725 write_subtree(struct got_object_id **new_subtree_id,
2726 struct got_tree_entry *te, const char *parent_path,
2727 struct got_pathlist_head *commitable_paths,
2728 got_worktree_status_cb status_cb, void *status_arg,
2729 struct got_repository *repo)
2731 const struct got_error *err = NULL;
2732 struct got_tree_object *subtree;
2735 if (asprintf(&subpath, "%s%s%s", parent_path,
2736 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2737 return got_error_from_errno("asprintf");
2739 err = got_object_open_as_tree(&subtree, repo, te->id);
2743 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2744 status_cb, status_arg, repo);
2745 got_object_tree_close(subtree);
2750 static const struct got_error *
2751 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2753 const struct got_error *err = NULL;
2754 char *ct_parent_path = NULL;
2758 if (strchr(ct->path, '/') == NULL) {
2759 *match = got_path_is_root_dir(path);
2763 err = got_path_dirname(&ct_parent_path, ct->path);
2766 *match = (strcmp(path, ct_parent_path) == 0);
2767 free(ct_parent_path);
2772 get_ct_file_mode(struct got_commitable *ct)
2774 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2777 static const struct got_error *
2778 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2779 struct got_tree_entry *te, struct got_commitable *ct)
2781 const struct got_error *err = NULL;
2785 err = got_object_tree_entry_dup(new_te, te);
2789 (*new_te)->mode = get_ct_file_mode(ct);
2791 free((*new_te)->id);
2792 (*new_te)->id = got_object_id_dup(ct->blob_id);
2793 if ((*new_te)->id == NULL) {
2794 err = got_error_from_errno("got_object_id_dup");
2798 if (err && *new_te) {
2799 got_object_tree_entry_close(*new_te);
2805 static const struct got_error *
2806 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2807 struct got_commitable *ct)
2809 const struct got_error *err = NULL;
2814 *new_te = calloc(1, sizeof(**new_te));
2815 if (*new_te == NULL)
2816 return got_error_from_errno("calloc");
2818 ct_name = basename(ct->path);
2819 if (ct_name == NULL) {
2820 err = got_error_from_errno2("basename", ct->path);
2823 (*new_te)->name = strdup(ct_name);
2824 if ((*new_te)->name == NULL) {
2825 err = got_error_from_errno("strdup");
2829 (*new_te)->mode = get_ct_file_mode(ct);
2831 (*new_te)->id = got_object_id_dup(ct->blob_id);
2832 if ((*new_te)->id == NULL) {
2833 err = got_error_from_errno("got_object_id_dup");
2837 if (err && *new_te) {
2838 got_object_tree_entry_close(*new_te);
2844 static const struct got_error *
2845 insert_tree_entry(struct got_tree_entry *new_te,
2846 struct got_pathlist_head *paths)
2848 const struct got_error *err = NULL;
2849 struct got_pathlist_entry *new_pe;
2851 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2855 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2859 static const struct got_error *
2860 report_ct_status(struct got_commitable *ct,
2861 got_worktree_status_cb status_cb, void *status_arg)
2863 const char *ct_path = ct->path;
2864 while (ct_path[0] == '/')
2866 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2869 static const struct got_error *
2870 match_modified_subtree(int *modified, struct got_tree_entry *te,
2871 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2873 const struct got_error *err = NULL;
2874 struct got_pathlist_entry *pe;
2879 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2880 got_path_is_root_dir(base_tree_path) ? "" : "/",
2882 return got_error_from_errno("asprintf");
2884 TAILQ_FOREACH(pe, commitable_paths, entry) {
2885 struct got_commitable *ct = pe->data;
2886 *modified = got_path_is_child(ct->in_repo_path, te_path,
2896 static const struct got_error *
2897 match_deleted_or_modified_ct(struct got_commitable **ctp,
2898 struct got_tree_entry *te, const char *base_tree_path,
2899 struct got_pathlist_head *commitable_paths)
2901 const struct got_error *err = NULL;
2902 struct got_pathlist_entry *pe;
2906 TAILQ_FOREACH(pe, commitable_paths, entry) {
2907 struct got_commitable *ct = pe->data;
2908 char *ct_name = NULL;
2911 if (ct->status != GOT_STATUS_MODIFY &&
2912 ct->status != GOT_STATUS_DELETE)
2915 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2918 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2924 ct_name = basename(pe->path);
2925 if (ct_name == NULL)
2926 return got_error_from_errno2("basename", pe->path);
2928 if (strcmp(te->name, ct_name) != 0)
2938 static const struct got_error *
2939 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2940 const char *child_path, const char *path_base_tree,
2941 struct got_pathlist_head *commitable_paths,
2942 got_worktree_status_cb status_cb, void *status_arg,
2943 struct got_repository *repo)
2945 const struct got_error *err = NULL;
2946 struct got_tree_entry *new_te;
2951 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2952 got_path_is_root_dir(path_base_tree) ? "" : "/",
2954 return got_error_from_errno("asprintf");
2956 new_te = calloc(1, sizeof(*new_te));
2957 new_te->mode = S_IFDIR;
2958 new_te->name = strdup(child_path);
2959 if (new_te->name == NULL) {
2960 err = got_error_from_errno("strdup");
2961 got_object_tree_entry_close(new_te);
2964 err = write_tree(&new_te->id, NULL, subtree_path,
2965 commitable_paths, status_cb, status_arg, repo);
2967 got_object_tree_entry_close(new_te);
2977 static const struct got_error *
2978 write_tree(struct got_object_id **new_tree_id,
2979 struct got_tree_object *base_tree, const char *path_base_tree,
2980 struct got_pathlist_head *commitable_paths,
2981 got_worktree_status_cb status_cb, void *status_arg,
2982 struct got_repository *repo)
2984 const struct got_error *err = NULL;
2985 const struct got_tree_entries *base_entries = NULL;
2986 struct got_pathlist_head paths;
2987 struct got_tree_entries new_tree_entries;
2988 struct got_tree_entry *te, *new_te = NULL;
2989 struct got_pathlist_entry *pe;
2992 new_tree_entries.nentries = 0;
2993 SIMPLEQ_INIT(&new_tree_entries.head);
2995 /* Insert, and recurse into, newly added entries first. */
2996 TAILQ_FOREACH(pe, commitable_paths, entry) {
2997 struct got_commitable *ct = pe->data;
2998 char *child_path = NULL, *slash;
3000 if (ct->status != GOT_STATUS_ADD ||
3001 (ct->flags & GOT_COMMITABLE_ADDED))
3004 if (!got_path_is_child(pe->path, path_base_tree,
3005 strlen(path_base_tree)))
3008 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3013 slash = strchr(child_path, '/');
3014 if (slash == NULL) {
3015 err = alloc_added_blob_tree_entry(&new_te, ct);
3018 err = report_ct_status(ct, status_cb, status_arg);
3021 ct->flags |= GOT_COMMITABLE_ADDED;
3022 err = insert_tree_entry(new_te, &paths);
3026 *slash = '\0'; /* trim trailing path components */
3027 if (base_tree == NULL ||
3028 got_object_tree_find_entry(base_tree, child_path)
3030 err = make_subtree_for_added_blob(&new_te,
3031 child_path, path_base_tree,
3032 commitable_paths, status_cb, status_arg,
3036 err = insert_tree_entry(new_te, &paths);
3044 /* Handle modified and deleted entries. */
3045 base_entries = got_object_tree_get_entries(base_tree);
3046 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3047 struct got_commitable *ct = NULL;
3049 if (S_ISDIR(te->mode)) {
3051 err = got_object_tree_entry_dup(&new_te, te);
3054 err = match_modified_subtree(&modified, te,
3055 path_base_tree, commitable_paths);
3058 /* Avoid recursion into unmodified subtrees. */
3061 err = write_subtree(&new_te->id, te,
3062 path_base_tree, commitable_paths,
3063 status_cb, status_arg, repo);
3067 err = insert_tree_entry(new_te, &paths);
3073 err = match_deleted_or_modified_ct(&ct, te,
3074 path_base_tree, commitable_paths);
3076 /* NB: Deleted entries get dropped here. */
3077 if (ct->status == GOT_STATUS_MODIFY) {
3078 err = alloc_modified_blob_tree_entry(
3082 err = insert_tree_entry(new_te, &paths);
3086 err = report_ct_status(ct, status_cb,
3091 /* Entry is unchanged; just copy it. */
3092 err = got_object_tree_entry_dup(&new_te, te);
3095 err = insert_tree_entry(new_te, &paths);
3102 /* Write new list of entries; deleted entries have been dropped. */
3103 TAILQ_FOREACH(pe, &paths, entry) {
3104 struct got_tree_entry *te = pe->data;
3105 new_tree_entries.nentries++;
3106 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3108 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3110 got_object_tree_entries_close(&new_tree_entries);
3111 got_pathlist_free(&paths);
3115 static const struct got_error *
3116 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3117 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3119 const struct got_error *err = NULL;
3120 struct got_pathlist_entry *pe;
3122 TAILQ_FOREACH(pe, commitable_paths, entry) {
3123 struct got_fileindex_entry *ie;
3124 struct got_commitable *ct = pe->data;
3126 ie = got_fileindex_entry_get(fileindex, pe->path);
3128 if (ct->status == GOT_STATUS_DELETE) {
3129 got_fileindex_entry_remove(fileindex, ie);
3130 got_fileindex_entry_free(ie);
3132 err = got_fileindex_entry_update(ie,
3133 ct->ondisk_path, ct->blob_id->sha1,
3134 new_base_commit_id->sha1, 1);
3136 err = got_fileindex_entry_alloc(&ie,
3137 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3138 new_base_commit_id->sha1);
3141 err = got_fileindex_entry_add(fileindex, ie);
3149 static const struct got_error *
3150 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3151 struct got_object_id *head_commit_id)
3153 const struct got_error *err = NULL;
3154 struct got_object_id *id_in_head = NULL, *id = NULL;
3155 struct got_commit_object *commit = NULL;
3157 const char *ct_path = ct->in_repo_path;
3159 while (ct_path[0] == '/')
3163 * Ensure that no modifications were made to files *and their parents*
3164 * in commits between the file's base commit and the branch head.
3166 * Checking the parents is important for detecting conflicting tree
3167 * configurations (files or parent folders might have been moved,
3168 * deleted, added again, etc.). Such changes need to be merged with
3169 * local changes before a commit can occur.
3171 * The implication is that the file's (parent) entry in the root
3172 * directory must have the same ID in all relevant commits.
3174 if (ct->status != GOT_STATUS_ADD) {
3175 struct got_object_qid *pid;
3177 struct got_object_id *root_entry_id = NULL;
3179 /* Trivial case: base commit == head commit */
3180 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3183 /* Compute the path to the root directory's entry. */
3184 path = strdup(ct_path);
3186 err = got_error_from_errno("strdup");
3189 slash = strchr(path, '/');
3193 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3197 err = got_object_id_by_path(&root_entry_id, repo,
3198 head_commit_id, path);
3202 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3204 struct got_commit_object *pcommit;
3206 err = got_object_id_by_path(&id, repo, pid->id, path);
3208 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3214 err = got_object_id_by_path(&id, repo, pid->id, path);
3218 if (got_object_id_cmp(id, root_entry_id) != 0) {
3219 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3223 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3224 break; /* all relevant commits scanned */
3226 err = got_object_open_as_commit(&pcommit, repo,
3231 got_object_commit_close(commit);
3233 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3237 /* Require that added files don't exist in the branch head. */
3238 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3240 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3242 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3246 got_object_commit_close(commit);
3253 const struct got_error *
3254 commit_worktree(struct got_object_id **new_commit_id,
3255 struct got_pathlist_head *commitable_paths,
3256 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3257 const char *ondisk_path, const char *author, const char *committer,
3258 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3259 got_worktree_status_cb status_cb, void *status_arg,
3260 struct got_repository *repo)
3262 const struct got_error *err = NULL, *unlockerr = NULL;
3263 struct got_pathlist_entry *pe;
3264 const char *head_ref_name = NULL;
3265 struct got_commit_object *head_commit = NULL;
3266 struct got_reference *head_ref2 = NULL;
3267 struct got_object_id *head_commit_id2 = NULL;
3268 struct got_tree_object *head_tree = NULL;
3269 struct got_object_id *new_tree_id = NULL;
3270 struct got_object_id_queue parent_ids;
3271 struct got_object_qid *pid = NULL;
3272 char *logmsg = NULL;
3274 *new_commit_id = NULL;
3276 SIMPLEQ_INIT(&parent_ids);
3278 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3282 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3286 if (commit_msg_cb != NULL) {
3287 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3292 if (logmsg == NULL || strlen(logmsg) == 0) {
3293 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3297 /* Create blobs from added and modified files and record their IDs. */
3298 TAILQ_FOREACH(pe, commitable_paths, entry) {
3299 struct got_commitable *ct = pe->data;
3302 if (ct->status != GOT_STATUS_ADD &&
3303 ct->status != GOT_STATUS_MODIFY)
3306 if (asprintf(&ondisk_path, "%s/%s",
3307 worktree->root_path, pe->path) == -1) {
3308 err = got_error_from_errno("asprintf");
3311 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3317 /* Recursively write new tree objects. */
3318 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3319 status_cb, status_arg, repo);
3323 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3326 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3327 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3328 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3329 got_object_qid_free(pid);
3335 /* Check if a concurrent commit to our branch has occurred. */
3336 head_ref_name = got_worktree_get_head_ref_name(worktree);
3337 if (head_ref_name == NULL) {
3338 err = got_error_from_errno("got_worktree_get_head_ref_name");
3341 /* Lock the reference here to prevent concurrent modification. */
3342 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3345 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3348 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3349 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3352 /* Update branch head in repository. */
3353 err = got_ref_change_ref(head_ref2, *new_commit_id);
3356 err = got_ref_write(head_ref2, repo);
3360 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3364 err = ref_base_commit(worktree, repo);
3369 got_object_tree_close(head_tree);
3371 got_object_commit_close(head_commit);
3372 free(head_commit_id2);
3374 unlockerr = got_ref_unlock(head_ref2);
3375 if (unlockerr && err == NULL)
3377 got_ref_close(head_ref2);
3382 const struct got_error *
3383 got_worktree_commit(struct got_object_id **new_commit_id,
3384 struct got_worktree *worktree, const char *ondisk_path,
3385 const char *author, const char *committer,
3386 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3387 got_worktree_status_cb status_cb, void *status_arg,
3388 struct got_repository *repo)
3390 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3391 struct got_fileindex *fileindex = NULL;
3392 char *fileindex_path = NULL, *relpath = NULL;
3393 struct got_pathlist_head commitable_paths;
3394 struct collect_commitables_arg cc_arg;
3395 struct got_pathlist_entry *pe;
3396 struct got_reference *head_ref = NULL;
3397 struct got_object_id *head_commit_id = NULL;
3399 *new_commit_id = NULL;
3401 TAILQ_INIT(&commitable_paths);
3403 err = lock_worktree(worktree, LOCK_EX);
3407 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3411 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3416 err = got_path_skip_common_ancestor(&relpath,
3417 worktree->root_path, ondisk_path);
3422 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3426 cc_arg.commitable_paths = &commitable_paths;
3427 cc_arg.worktree = worktree;
3429 err = worktree_status(worktree, relpath ? relpath : "",
3430 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3434 if (TAILQ_EMPTY(&commitable_paths)) {
3435 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3439 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3440 struct got_commitable *ct = pe->data;
3441 err = check_ct_out_of_date(ct, repo, head_commit_id);
3446 err = commit_worktree(new_commit_id, &commitable_paths,
3447 head_commit_id, worktree, ondisk_path, author, committer,
3448 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3452 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3454 sync_err = sync_fileindex(fileindex, fileindex_path);
3455 if (sync_err && err == NULL)
3459 got_fileindex_free(fileindex);
3460 free(fileindex_path);
3462 unlockerr = lock_worktree(worktree, LOCK_SH);
3463 if (unlockerr && err == NULL)
3465 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3466 struct got_commitable *ct = pe->data;
3467 free_commitable(ct);
3469 got_pathlist_free(&commitable_paths);
3474 got_commitable_get_path(struct got_commitable *ct)
3480 got_commitable_get_status(struct got_commitable *ct)
3485 struct check_rebase_ok_arg {
3486 struct got_worktree *worktree;
3487 struct got_repository *repo;
3488 int rebase_in_progress;
3491 static const struct got_error *
3492 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3494 const struct got_error *err = NULL;
3495 struct check_rebase_ok_arg *a = arg;
3496 unsigned char status;
3500 if (!a->rebase_in_progress) {
3501 /* Reject rebase of a work tree with mixed base commits. */
3502 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3503 SHA1_DIGEST_LENGTH))
3504 return got_error(GOT_ERR_MIXED_COMMITS);
3507 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3509 return got_error_from_errno("asprintf");
3511 /* Reject rebase of a work tree with modified or conflicted files. */
3512 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3517 if (a->rebase_in_progress) {
3518 if (status == GOT_STATUS_CONFLICT)
3519 return got_error(GOT_ERR_CONFLICTS);
3520 } else if (status != GOT_STATUS_NO_CHANGE)
3521 return got_error(GOT_ERR_MODIFIED);
3526 const struct got_error *
3527 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3528 struct got_reference **tmp_branch, struct got_worktree *worktree,
3529 struct got_reference *branch, struct got_repository *repo)
3531 const struct got_error *err = NULL;
3532 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3533 char *branch_ref_name = NULL;
3534 struct got_fileindex *fileindex = NULL;
3535 char *fileindex_path = NULL;
3536 struct check_rebase_ok_arg ok_arg;
3537 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3539 *new_base_branch_ref = NULL;
3542 err = lock_worktree(worktree, LOCK_EX);
3546 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3550 ok_arg.worktree = worktree;
3552 ok_arg.rebase_in_progress = 0;
3553 err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3558 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3562 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3566 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3570 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3575 err = got_ref_alloc_symref(new_base_branch_ref,
3576 new_base_branch_ref_name, wt_branch);
3579 err = got_ref_write(*new_base_branch_ref, repo);
3583 /* TODO Lock original branch's ref while rebasing? */
3585 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3589 err = got_ref_write(branch_ref, repo);
3593 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3594 worktree->base_commit_id);
3597 err = got_ref_write(*tmp_branch, repo);
3601 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3605 free(fileindex_path);
3607 got_fileindex_free(fileindex);
3608 free(tmp_branch_name);
3609 free(new_base_branch_ref_name);
3610 free(branch_ref_name);
3612 got_ref_close(branch_ref);
3614 got_ref_close(wt_branch);
3616 if (*new_base_branch_ref) {
3617 got_ref_close(*new_base_branch_ref);
3618 *new_base_branch_ref = NULL;
3621 got_ref_close(*tmp_branch);
3624 lock_worktree(worktree, LOCK_SH);
3629 const struct got_error *
3630 got_worktree_rebase_continue(struct got_object_id **commit_id,
3631 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3632 struct got_reference **branch, struct got_worktree *worktree,
3633 struct got_repository *repo)
3635 const struct got_error *err;
3636 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3637 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3638 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3642 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3646 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3650 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3654 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3658 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3662 err = got_ref_open(branch, repo,
3663 got_ref_get_symref_target(branch_ref), 0);
3667 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3671 err = got_ref_resolve(commit_id, repo, commit_ref);
3675 err = got_ref_open(new_base_branch, repo,
3676 new_base_branch_ref_name, 0);
3680 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3684 free(commit_ref_name);
3685 free(branch_ref_name);
3687 got_ref_close(commit_ref);
3689 got_ref_close(branch_ref);
3694 got_ref_close(*tmp_branch);
3697 if (*new_base_branch) {
3698 got_ref_close(*new_base_branch);
3699 *new_base_branch = NULL;
3702 got_ref_close(*branch);
3709 const struct got_error *
3710 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3712 const struct got_error *err;
3713 char *tmp_branch_name = NULL;
3715 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3719 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3720 free(tmp_branch_name);
3724 static const struct got_error *
3725 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3726 char **logmsg, void *arg)
3728 struct got_commit_object *commit = arg;
3730 *logmsg = strdup(got_object_commit_get_logmsg(commit));
3731 if (*logmsg == NULL)
3732 return got_error_from_errno("strdup");
3737 static const struct got_error *
3738 rebase_status(void *arg, unsigned char status, const char *path,
3739 struct got_object_id *blob_id, struct got_object_id *commit_id)
3744 struct collect_merged_paths_arg {
3745 got_worktree_checkout_cb progress_cb;
3747 struct got_pathlist_head *merged_paths;
3750 static const struct got_error *
3751 collect_merged_paths(void *arg, unsigned char status, const char *path)
3753 const struct got_error *err;
3754 struct collect_merged_paths_arg *a = arg;
3756 struct got_pathlist_entry *new;
3758 err = (*a->progress_cb)(a->progress_arg, status, path);
3762 if (status != GOT_STATUS_MERGE &&
3763 status != GOT_STATUS_ADD &&
3764 status != GOT_STATUS_DELETE &&
3765 status != GOT_STATUS_CONFLICT)
3770 return got_error_from_errno("strdup");
3772 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3773 if (err || new == NULL)
3779 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3781 struct got_pathlist_entry *pe;
3783 TAILQ_FOREACH(pe, merged_paths, entry)
3784 free((char *)pe->path);
3786 got_pathlist_free(merged_paths);
3789 const struct got_error *
3790 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3791 struct got_worktree *worktree, struct got_object_id *parent_commit_id,
3792 struct got_object_id *commit_id, struct got_repository *repo,
3793 got_worktree_checkout_cb progress_cb, void *progress_arg,
3794 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3796 const struct got_error *err;
3797 struct got_fileindex *fileindex;
3798 char *fileindex_path, *commit_ref_name = NULL;
3799 struct got_reference *commit_ref = NULL;
3800 struct collect_merged_paths_arg cmp_arg;
3802 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3804 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3808 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3811 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3813 if (err->code != GOT_ERR_NOT_REF)
3815 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3818 err = got_ref_write(commit_ref, repo);
3822 struct got_object_id *stored_id;
3825 err = got_ref_resolve(&stored_id, repo, commit_ref);
3828 cmp = got_object_id_cmp(commit_id, stored_id);
3831 err = got_error(GOT_ERR_REBASE_COMMITID);
3836 cmp_arg.progress_cb = progress_cb;
3837 cmp_arg.progress_arg = progress_arg;
3838 cmp_arg.merged_paths = merged_paths;
3839 err = merge_files(worktree, fileindex, fileindex_path,
3840 parent_commit_id, commit_id, repo, collect_merged_paths,
3841 &cmp_arg, cancel_cb, cancel_arg);
3843 got_fileindex_free(fileindex);
3844 free(fileindex_path);
3846 got_ref_close(commit_ref);
3850 const struct got_error *
3851 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3852 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
3853 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
3854 struct got_object_id *orig_commit_id, struct got_repository *repo)
3856 const struct got_error *err, *sync_err;
3857 struct got_pathlist_head commitable_paths;
3858 struct collect_commitables_arg cc_arg;
3859 struct got_fileindex *fileindex = NULL;
3860 char *fileindex_path = NULL, *commit_ref_name = NULL;
3861 struct got_reference *head_ref = NULL;
3862 struct got_object_id *head_commit_id = NULL;
3863 struct got_reference *commit_ref = NULL;
3864 struct got_object_id *commit_id = NULL;
3866 TAILQ_INIT(&commitable_paths);
3867 *new_commit_id = NULL;
3869 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3871 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3874 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3877 err = got_ref_resolve(&commit_id, repo, commit_ref);
3880 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3881 err = got_error(GOT_ERR_REBASE_COMMITID);
3885 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3889 cc_arg.commitable_paths = &commitable_paths;
3890 cc_arg.worktree = worktree;
3893 * If possible get the status of individual files directly to
3894 * avoid crawling the entire work tree once per rebased commit.
3895 * TODO: Ideally, merged_paths would contain a list of commitables
3896 * we could use so we could skip worktree_status() entirely.
3899 struct got_pathlist_entry *pe;
3900 TAILQ_FOREACH(pe, merged_paths, entry) {
3901 err = worktree_status(worktree, pe->path, fileindex,
3902 repo, collect_commitables, &cc_arg, NULL, NULL);
3907 err = worktree_status(worktree, "", fileindex, repo,
3908 collect_commitables, &cc_arg, NULL, NULL);
3913 if (TAILQ_EMPTY(&commitable_paths)) {
3914 /* No-op change; commit will be elided. */
3915 err = got_ref_delete(commit_ref, repo);
3918 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3922 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3926 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3930 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
3931 worktree, NULL, got_object_commit_get_author(orig_commit),
3932 got_object_commit_get_committer(orig_commit),
3933 collect_rebase_commit_msg, orig_commit,
3934 rebase_status, NULL, repo);
3938 err = got_ref_change_ref(tmp_branch, *new_commit_id);
3942 err = got_ref_delete(commit_ref, repo);
3946 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3948 sync_err = sync_fileindex(fileindex, fileindex_path);
3949 if (sync_err && err == NULL)
3953 got_fileindex_free(fileindex);
3954 free(fileindex_path);
3955 free(commit_ref_name);
3957 got_ref_close(commit_ref);
3958 free(head_commit_id);
3960 got_ref_close(head_ref);
3962 free(*new_commit_id);
3963 *new_commit_id = NULL;
3968 const struct got_error *
3969 got_worktree_rebase_postpone(struct got_worktree *worktree)
3971 return lock_worktree(worktree, LOCK_SH);
3974 static const struct got_error *
3975 delete_ref(const char *name, struct got_repository *repo)
3977 const struct got_error *err;
3978 struct got_reference *ref;
3980 err = got_ref_open(&ref, repo, name, 0);
3982 if (err->code == GOT_ERR_NOT_REF)
3987 err = got_ref_delete(ref, repo);
3992 static const struct got_error *
3993 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
3995 const struct got_error *err;
3996 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3997 char *branch_ref_name = NULL, *commit_ref_name = NULL;
3999 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4002 err = delete_ref(tmp_branch_name, repo);
4006 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4009 err = delete_ref(new_base_branch_ref_name, repo);
4013 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4016 err = delete_ref(branch_ref_name, repo);
4020 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4023 err = delete_ref(commit_ref_name, repo);
4028 free(tmp_branch_name);
4029 free(new_base_branch_ref_name);
4030 free(branch_ref_name);
4031 free(commit_ref_name);
4035 const struct got_error *
4036 got_worktree_rebase_complete(struct got_worktree *worktree,
4037 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
4038 struct got_reference *rebased_branch,
4039 struct got_repository *repo)
4041 const struct got_error *err, *unlockerr;
4042 struct got_object_id *new_head_commit_id = NULL;
4044 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4048 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4052 err = got_ref_write(rebased_branch, repo);
4056 err = got_worktree_set_head_ref(worktree, rebased_branch);
4060 err = delete_rebase_refs(worktree, repo);
4062 free(new_head_commit_id);
4063 unlockerr = lock_worktree(worktree, LOCK_SH);
4064 if (unlockerr && err == NULL)
4069 struct collect_revertible_paths_arg {
4070 struct got_pathlist_head *revertible_paths;
4071 struct got_worktree *worktree;
4074 static const struct got_error *
4075 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4076 struct got_object_id *blob_id, struct got_object_id *commit_id)
4078 struct collect_revertible_paths_arg *a = arg;
4079 const struct got_error *err = NULL;
4080 struct got_pathlist_entry *new = NULL;
4083 if (status != GOT_STATUS_ADD &&
4084 status != GOT_STATUS_DELETE &&
4085 status != GOT_STATUS_MODIFY &&
4086 status != GOT_STATUS_CONFLICT &&
4087 status != GOT_STATUS_MISSING)
4090 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4091 return got_error_from_errno("asprintf");
4093 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4094 if (err || new == NULL)
4099 const struct got_error *
4100 got_worktree_rebase_abort(struct got_worktree *worktree,
4101 struct got_repository *repo, struct got_reference *new_base_branch,
4102 got_worktree_checkout_cb progress_cb, void *progress_arg)
4104 const struct got_error *err, *unlockerr, *sync_err;
4105 struct got_reference *resolved = NULL;
4106 struct got_object_id *commit_id = NULL;
4107 struct got_fileindex *fileindex = NULL;
4108 char *fileindex_path = NULL;
4109 struct got_pathlist_head revertible_paths;
4110 struct got_pathlist_entry *pe;
4111 struct collect_revertible_paths_arg crp_arg;
4112 struct got_object_id *tree_id = NULL;
4114 TAILQ_INIT(&revertible_paths);
4116 err = lock_worktree(worktree, LOCK_EX);
4120 err = got_ref_open(&resolved, repo,
4121 got_ref_get_symref_target(new_base_branch), 0);
4125 err = got_worktree_set_head_ref(worktree, resolved);
4130 * XXX commits to the base branch could have happened while
4131 * we were busy rebasing; should we store the original commit ID
4132 * when rebase begins and read it back here?
4134 err = got_ref_resolve(&commit_id, repo, resolved);
4138 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4142 err = got_object_id_by_path(&tree_id, repo,
4143 worktree->base_commit_id, worktree->path_prefix);
4147 err = delete_rebase_refs(worktree, repo);
4151 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4155 crp_arg.revertible_paths = &revertible_paths;
4156 crp_arg.worktree = worktree;
4157 err = worktree_status(worktree, "", fileindex, repo,
4158 collect_revertible_paths, &crp_arg, NULL, NULL);
4162 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4163 err = revert_file(worktree, fileindex, pe->path,
4164 progress_cb, progress_arg, repo);
4169 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4170 repo, progress_cb, progress_arg, NULL, NULL);
4172 sync_err = sync_fileindex(fileindex, fileindex_path);
4173 if (sync_err && err == NULL)
4176 got_ref_close(resolved);
4180 got_fileindex_free(fileindex);
4181 free(fileindex_path);
4182 TAILQ_FOREACH(pe, &revertible_paths, entry)
4183 free((char *)pe->path);
4184 got_pathlist_free(&revertible_paths);
4186 unlockerr = lock_worktree(worktree, LOCK_SH);
4187 if (unlockerr && err == NULL)