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_sec == sb->st_mtime &&
1078 ie->mtime_nsec == sb->st_mtimensec &&
1079 ie->size == (sb->st_size & 0xffffffff))
1082 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1083 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1087 f = fopen(abspath, "r");
1089 err = got_error_from_errno2("fopen", abspath);
1092 hdrlen = got_object_blob_get_hdrlen(blob);
1094 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1095 err = got_object_blob_read_block(&blen, blob);
1098 /* Skip length of blob object header first time around. */
1099 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1100 if (flen == 0 && ferror(f)) {
1101 err = got_error_from_errno("fread");
1106 *status = GOT_STATUS_MODIFY;
1108 } else if (flen == 0) {
1110 *status = GOT_STATUS_MODIFY;
1112 } else if (blen - hdrlen == flen) {
1113 /* Skip blob object header first time around. */
1114 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1115 *status = GOT_STATUS_MODIFY;
1119 *status = GOT_STATUS_MODIFY;
1125 if (*status == GOT_STATUS_MODIFY) {
1127 err = get_modified_file_content_status(status, f);
1131 got_object_blob_close(blob);
1137 static const struct got_error *
1138 update_blob(struct got_worktree *worktree,
1139 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1140 struct got_tree_entry *te, const char *path,
1141 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1144 const struct got_error *err = NULL;
1145 struct got_blob_object *blob = NULL;
1147 unsigned char status = GOT_STATUS_NO_CHANGE;
1150 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1151 return got_error_from_errno("asprintf");
1153 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1157 if (status == GOT_STATUS_OBSTRUCTED) {
1158 err = (*progress_cb)(progress_arg, status, path);
1162 if (ie && status != GOT_STATUS_MISSING) {
1163 if (got_fileindex_entry_has_commit(ie) &&
1164 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1165 SHA1_DIGEST_LENGTH) == 0) {
1166 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1170 if (got_fileindex_entry_has_blob(ie) &&
1171 memcmp(ie->blob_sha1, te->id->sha1,
1172 SHA1_DIGEST_LENGTH) == 0)
1176 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1180 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1181 int update_timestamps;
1182 struct got_blob_object *blob2 = NULL;
1183 if (got_fileindex_entry_has_blob(ie)) {
1184 struct got_object_id id2;
1185 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1186 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1190 err = merge_blob(&update_timestamps, worktree, blob2,
1191 ondisk_path, path, sb.st_mode, blob,
1192 worktree->base_commit_id, repo,
1193 progress_cb, progress_arg);
1195 got_object_blob_close(blob2);
1197 * Do not update timestamps of files with local changes.
1198 * Otherwise, a future status walk would treat them as
1199 * unmodified files again.
1201 err = got_fileindex_entry_update(ie, ondisk_path,
1202 blob->id.sha1, worktree->base_commit_id->sha1,
1204 } else if (status == GOT_STATUS_DELETE) {
1205 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1208 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1209 ondisk_path, path, blob, 0);
1213 err = install_blob(worktree, ondisk_path, path, te->mode,
1214 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1215 repo, progress_cb, progress_arg);
1218 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1219 ondisk_path, path, blob, 1);
1223 got_object_blob_close(blob);
1229 static const struct got_error *
1230 remove_ondisk_file(const char *root_path, const char *path)
1232 const struct got_error *err = NULL;
1233 char *ondisk_path = NULL;
1235 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1236 return got_error_from_errno("asprintf");
1238 if (unlink(ondisk_path) == -1) {
1239 if (errno != ENOENT)
1240 err = got_error_from_errno2("unlink", ondisk_path);
1242 char *parent = dirname(ondisk_path);
1243 while (parent && strcmp(parent, root_path) != 0) {
1244 if (rmdir(parent) == -1) {
1245 if (errno != ENOTEMPTY)
1246 err = got_error_from_errno2("rmdir",
1250 parent = dirname(parent);
1257 static const struct got_error *
1258 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1259 struct got_fileindex_entry *ie, struct got_repository *repo,
1260 got_worktree_checkout_cb progress_cb, void *progress_arg)
1262 const struct got_error *err = NULL;
1263 unsigned char status;
1267 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1269 return got_error_from_errno("asprintf");
1271 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1275 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1276 status == GOT_STATUS_ADD) {
1277 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1281 * Preserve the working file and change the deleted blob's
1282 * entry into a schedule-add entry.
1284 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1289 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1292 if (status == GOT_STATUS_NO_CHANGE) {
1293 err = remove_ondisk_file(worktree->root_path, ie->path);
1297 got_fileindex_entry_remove(fileindex, ie);
1303 struct diff_cb_arg {
1304 struct got_fileindex *fileindex;
1305 struct got_worktree *worktree;
1306 struct got_repository *repo;
1307 got_worktree_checkout_cb progress_cb;
1309 got_worktree_cancel_cb cancel_cb;
1313 static const struct got_error *
1314 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1315 struct got_tree_entry *te, const char *parent_path)
1317 struct diff_cb_arg *a = arg;
1319 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1320 return got_error(GOT_ERR_CANCELLED);
1322 return update_blob(a->worktree, a->fileindex, ie, te,
1323 ie->path, a->repo, a->progress_cb, a->progress_arg);
1326 static const struct got_error *
1327 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1329 struct diff_cb_arg *a = arg;
1331 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1332 return got_error(GOT_ERR_CANCELLED);
1334 return delete_blob(a->worktree, a->fileindex, ie,
1335 a->repo, a->progress_cb, a->progress_arg);
1338 static const struct got_error *
1339 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1341 struct diff_cb_arg *a = arg;
1342 const struct got_error *err;
1345 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1346 return got_error(GOT_ERR_CANCELLED);
1348 if (asprintf(&path, "%s%s%s", parent_path,
1349 parent_path[0] ? "/" : "", te->name)
1351 return got_error_from_errno("asprintf");
1353 if (S_ISDIR(te->mode))
1354 err = add_dir_on_disk(a->worktree, path);
1356 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1357 a->repo, a->progress_cb, a->progress_arg);
1363 static const struct got_error *
1364 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1366 const struct got_error *err = NULL;
1367 char *uuidstr = NULL;
1368 uint32_t uuid_status;
1372 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1373 if (uuid_status != uuid_s_ok)
1374 return got_error_uuid(uuid_status);
1376 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1378 err = got_error_from_errno("asprintf");
1385 const struct got_error *
1386 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1388 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1391 static const struct got_error *
1392 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1394 return get_ref_name(refname, worktree,
1395 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1398 static const struct got_error *
1399 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1401 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1404 static const struct got_error *
1405 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1407 return get_ref_name(refname, worktree,
1408 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1411 static const struct got_error *
1412 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1414 return get_ref_name(refname, worktree,
1415 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1420 * Prevent Git's garbage collector from deleting our base commit by
1421 * setting a reference to our base commit's ID.
1423 static const struct got_error *
1424 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1426 const struct got_error *err = NULL;
1427 struct got_reference *ref = NULL;
1430 err = got_worktree_get_base_ref_name(&refname, worktree);
1434 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1438 err = got_ref_write(ref, repo);
1446 static const struct got_error *
1447 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1448 struct got_worktree *worktree)
1450 const struct got_error *err = NULL;
1453 *fileindex_path = NULL;
1454 *fileindex = got_fileindex_alloc();
1455 if (*fileindex == NULL)
1456 return got_error_from_errno("got_fileindex_alloc");
1458 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1459 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1460 err = got_error_from_errno("asprintf");
1461 *fileindex_path = NULL;
1465 index = fopen(*fileindex_path, "rb");
1466 if (index == NULL) {
1467 if (errno != ENOENT)
1468 err = got_error_from_errno2("fopen", *fileindex_path);
1470 err = got_fileindex_read(*fileindex, index);
1471 if (fclose(index) != 0 && err == NULL)
1472 err = got_error_from_errno("fclose");
1476 free(*fileindex_path);
1477 *fileindex_path = NULL;
1478 got_fileindex_free(*fileindex);
1484 struct bump_base_commit_id_arg {
1485 struct got_object_id *base_commit_id;
1488 const char *entry_name;
1489 got_worktree_checkout_cb progress_cb;
1493 /* Bump base commit ID of all files within an updated part of the work tree. */
1494 static const struct got_error *
1495 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1497 const struct got_error *err;
1498 struct bump_base_commit_id_arg *a = arg;
1500 if (a->entry_name) {
1501 if (strcmp(ie->path, a->path) != 0)
1503 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1506 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1507 SHA1_DIGEST_LENGTH) == 0)
1510 if (a->progress_cb) {
1511 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1516 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1520 static const struct got_error *
1521 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1523 const struct got_error *err = NULL;
1524 char *new_fileindex_path = NULL;
1525 FILE *new_index = NULL;
1527 err = got_opentemp_named(&new_fileindex_path, &new_index,
1532 err = got_fileindex_write(fileindex, new_index);
1536 if (rename(new_fileindex_path, fileindex_path) != 0) {
1537 err = got_error_from_errno3("rename", new_fileindex_path,
1539 unlink(new_fileindex_path);
1544 free(new_fileindex_path);
1548 static const struct got_error *
1549 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1550 struct got_object_id **tree_id, const char *wt_relpath,
1551 struct got_worktree *worktree, struct got_repository *repo)
1553 const struct got_error *err = NULL;
1554 struct got_object_id *id = NULL;
1555 char *in_repo_path = NULL;
1556 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1558 *entry_type = GOT_OBJ_TYPE_ANY;
1559 *tree_relpath = NULL;
1562 if (wt_relpath[0] == '\0') {
1563 /* Check out all files within the work tree. */
1564 *entry_type = GOT_OBJ_TYPE_TREE;
1565 *tree_relpath = strdup("");
1566 if (*tree_relpath == NULL) {
1567 err = got_error_from_errno("strdup");
1570 err = got_object_id_by_path(tree_id, repo,
1571 worktree->base_commit_id, worktree->path_prefix);
1577 /* Check out a subset of files in the work tree. */
1579 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1580 is_root_wt ? "" : "/", wt_relpath) == -1) {
1581 err = got_error_from_errno("asprintf");
1585 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1591 in_repo_path = NULL;
1593 err = got_object_get_type(entry_type, repo, id);
1597 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1598 /* Check out a single file. */
1599 if (strchr(wt_relpath, '/') == NULL) {
1600 /* Check out a single file in work tree's root dir. */
1601 in_repo_path = strdup(worktree->path_prefix);
1602 if (in_repo_path == NULL) {
1603 err = got_error_from_errno("strdup");
1606 *tree_relpath = strdup("");
1607 if (*tree_relpath == NULL) {
1608 err = got_error_from_errno("strdup");
1612 /* Check out a single file in a subdirectory. */
1613 err = got_path_dirname(tree_relpath, wt_relpath);
1616 if (asprintf(&in_repo_path, "%s%s%s",
1617 worktree->path_prefix, is_root_wt ? "" : "/",
1618 *tree_relpath) == -1) {
1619 err = got_error_from_errno("asprintf");
1623 err = got_object_id_by_path(tree_id, repo,
1624 worktree->base_commit_id, in_repo_path);
1626 /* Check out all files within a subdirectory. */
1627 *tree_id = got_object_id_dup(id);
1628 if (*tree_id == NULL) {
1629 err = got_error_from_errno("got_object_id_dup");
1632 *tree_relpath = strdup(wt_relpath);
1633 if (*tree_relpath == NULL) {
1634 err = got_error_from_errno("strdup");
1642 *entry_type = GOT_OBJ_TYPE_ANY;
1643 free(*tree_relpath);
1644 *tree_relpath = NULL;
1651 static const struct got_error *
1652 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1653 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1654 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1655 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1657 const struct got_error *err = NULL;
1658 struct got_commit_object *commit = NULL;
1659 struct got_tree_object *tree = NULL;
1660 struct got_fileindex_diff_tree_cb diff_cb;
1661 struct diff_cb_arg arg;
1663 err = ref_base_commit(worktree, repo);
1667 err = got_object_open_as_commit(&commit, repo,
1668 worktree->base_commit_id);
1672 err = got_object_open_as_tree(&tree, repo, tree_id);
1677 got_object_tree_find_entry(tree, entry_name) == NULL) {
1678 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1682 diff_cb.diff_old_new = diff_old_new;
1683 diff_cb.diff_old = diff_old;
1684 diff_cb.diff_new = diff_new;
1685 arg.fileindex = fileindex;
1686 arg.worktree = worktree;
1688 arg.progress_cb = progress_cb;
1689 arg.progress_arg = progress_arg;
1690 arg.cancel_cb = cancel_cb;
1691 arg.cancel_arg = cancel_arg;
1692 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1693 entry_name, repo, &diff_cb, &arg);
1696 got_object_tree_close(tree);
1698 got_object_commit_close(commit);
1702 const struct got_error *
1703 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1704 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1705 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1707 const struct got_error *err = NULL, *sync_err, *unlockerr;
1708 struct got_commit_object *commit = NULL;
1709 struct got_object_id *tree_id = NULL;
1710 struct got_tree_object *tree = NULL;
1711 struct got_fileindex *fileindex = NULL;
1712 char *fileindex_path = NULL;
1713 char *relpath = NULL, *entry_name = NULL;
1716 err = lock_worktree(worktree, LOCK_EX);
1720 err = find_tree_entry_for_checkout(&entry_type, &relpath, &tree_id,
1721 path, worktree, repo);
1725 if (entry_type == GOT_OBJ_TYPE_BLOB) {
1726 entry_name = basename(path);
1727 if (entry_name == NULL) {
1728 err = got_error_from_errno2("basename", path);
1734 * Read the file index.
1735 * Checking out files is supposed to be an idempotent operation.
1736 * If the on-disk file index is incomplete we will try to complete it.
1738 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1742 err = checkout_files(worktree, fileindex, relpath, tree_id, entry_name,
1743 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1745 struct bump_base_commit_id_arg bbc_arg;
1746 bbc_arg.base_commit_id = worktree->base_commit_id;
1747 bbc_arg.entry_name = entry_name;
1748 bbc_arg.path = path;
1749 bbc_arg.path_len = strlen(path);
1750 bbc_arg.progress_cb = progress_cb;
1751 bbc_arg.progress_arg = progress_arg;
1752 err = got_fileindex_for_each_entry_safe(fileindex,
1753 bump_base_commit_id, &bbc_arg);
1755 sync_err = sync_fileindex(fileindex, fileindex_path);
1756 if (sync_err && err == NULL)
1759 free(fileindex_path);
1762 got_object_tree_close(tree);
1764 got_object_commit_close(commit);
1766 got_fileindex_free(fileindex);
1767 unlockerr = lock_worktree(worktree, LOCK_SH);
1768 if (unlockerr && err == NULL)
1773 struct merge_file_cb_arg {
1774 struct got_worktree *worktree;
1775 struct got_fileindex *fileindex;
1776 got_worktree_checkout_cb progress_cb;
1778 got_worktree_cancel_cb cancel_cb;
1780 struct got_object_id *commit_id2;
1783 static const struct got_error *
1784 merge_file_cb(void *arg, struct got_blob_object *blob1,
1785 struct got_blob_object *blob2, struct got_object_id *id1,
1786 struct got_object_id *id2, const char *path1, const char *path2,
1787 struct got_repository *repo)
1789 static const struct got_error *err = NULL;
1790 struct merge_file_cb_arg *a = arg;
1791 struct got_fileindex_entry *ie;
1792 char *ondisk_path = NULL;
1794 unsigned char status;
1795 int local_changes_subsumed;
1797 if (blob1 && blob2) {
1798 ie = got_fileindex_entry_get(a->fileindex, path2);
1800 return (*a->progress_cb)(a->progress_arg,
1801 GOT_STATUS_MISSING, path2);
1803 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1805 return got_error_from_errno("asprintf");
1807 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1811 if (status == GOT_STATUS_DELETE) {
1812 err = (*a->progress_cb)(a->progress_arg,
1813 GOT_STATUS_MERGE, path2);
1816 if (status != GOT_STATUS_NO_CHANGE &&
1817 status != GOT_STATUS_MODIFY &&
1818 status != GOT_STATUS_CONFLICT &&
1819 status != GOT_STATUS_ADD) {
1820 err = (*a->progress_cb)(a->progress_arg, status, path2);
1824 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1825 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1826 a->progress_cb, a->progress_arg);
1828 ie = got_fileindex_entry_get(a->fileindex, path1);
1830 return (*a->progress_cb)(a->progress_arg,
1831 GOT_STATUS_MISSING, path2);
1833 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1835 return got_error_from_errno("asprintf");
1837 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1842 case GOT_STATUS_NO_CHANGE:
1843 err = (*a->progress_cb)(a->progress_arg,
1844 GOT_STATUS_DELETE, path1);
1847 err = remove_ondisk_file(a->worktree->root_path, path1);
1851 got_fileindex_entry_mark_deleted_from_disk(ie);
1853 case GOT_STATUS_DELETE:
1854 case GOT_STATUS_MISSING:
1855 err = (*a->progress_cb)(a->progress_arg,
1856 GOT_STATUS_DELETE, path1);
1860 got_fileindex_entry_mark_deleted_from_disk(ie);
1862 case GOT_STATUS_ADD:
1863 case GOT_STATUS_MODIFY:
1864 case GOT_STATUS_CONFLICT:
1865 err = (*a->progress_cb)(a->progress_arg,
1866 GOT_STATUS_CANNOT_DELETE, path1);
1870 case GOT_STATUS_OBSTRUCTED:
1871 err = (*a->progress_cb)(a->progress_arg, status, path1);
1879 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1881 return got_error_from_errno("asprintf");
1882 ie = got_fileindex_entry_get(a->fileindex, path2);
1884 err = get_file_status(&status, &sb, ie, ondisk_path,
1888 if (status != GOT_STATUS_NO_CHANGE &&
1889 status != GOT_STATUS_MODIFY &&
1890 status != GOT_STATUS_CONFLICT &&
1891 status != GOT_STATUS_ADD) {
1892 err = (*a->progress_cb)(a->progress_arg,
1896 err = merge_blob(&local_changes_subsumed, a->worktree,
1897 NULL, ondisk_path, path2, sb.st_mode, blob2,
1898 a->commit_id2, repo,
1899 a->progress_cb, a->progress_arg);
1900 if (status == GOT_STATUS_DELETE) {
1901 err = update_blob_fileindex_entry(a->worktree,
1902 a->fileindex, ie, ondisk_path, ie->path,
1908 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1909 err = install_blob(a->worktree, ondisk_path, path2,
1910 /* XXX get this from parent tree! */
1911 GOT_DEFAULT_FILE_MODE,
1912 sb.st_mode, blob2, 0, 0, repo,
1913 a->progress_cb, a->progress_arg);
1916 err = got_fileindex_entry_alloc(&ie,
1917 ondisk_path, path2, NULL, NULL);
1920 err = got_fileindex_entry_add(a->fileindex, ie);
1922 got_fileindex_entry_free(ie);
1932 struct check_merge_ok_arg {
1933 struct got_worktree *worktree;
1934 struct got_repository *repo;
1937 static const struct got_error *
1938 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1940 const struct got_error *err = NULL;
1941 struct check_merge_ok_arg *a = arg;
1942 unsigned char status;
1946 /* Reject merges into a work tree with mixed base commits. */
1947 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1948 SHA1_DIGEST_LENGTH))
1949 return got_error(GOT_ERR_MIXED_COMMITS);
1951 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1953 return got_error_from_errno("asprintf");
1955 /* Reject merges into a work tree with conflicted files. */
1956 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1959 if (status == GOT_STATUS_CONFLICT)
1960 return got_error(GOT_ERR_CONFLICTS);
1965 static const struct got_error *
1966 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1967 const char *fileindex_path, struct got_object_id *commit_id1,
1968 struct got_object_id *commit_id2, struct got_repository *repo,
1969 got_worktree_checkout_cb progress_cb, void *progress_arg,
1970 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1972 const struct got_error *err = NULL, *sync_err;
1973 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1974 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1975 struct merge_file_cb_arg arg;
1978 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1979 worktree->path_prefix);
1983 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1988 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1989 worktree->path_prefix);
1993 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1997 arg.worktree = worktree;
1998 arg.fileindex = fileindex;
1999 arg.progress_cb = progress_cb;
2000 arg.progress_arg = progress_arg;
2001 arg.cancel_cb = cancel_cb;
2002 arg.cancel_arg = cancel_arg;
2003 arg.commit_id2 = commit_id2;
2004 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2005 sync_err = sync_fileindex(fileindex, fileindex_path);
2006 if (sync_err && err == NULL)
2010 got_object_tree_close(tree1);
2012 got_object_tree_close(tree2);
2016 const struct got_error *
2017 got_worktree_merge_files(struct got_worktree *worktree,
2018 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2019 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2020 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2022 const struct got_error *err, *unlockerr;
2023 char *fileindex_path = NULL;
2024 struct got_fileindex *fileindex = NULL;
2025 struct check_merge_ok_arg mok_arg;
2027 err = lock_worktree(worktree, LOCK_EX);
2031 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2035 mok_arg.worktree = worktree;
2036 mok_arg.repo = repo;
2037 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2042 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2043 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2046 got_fileindex_free(fileindex);
2047 free(fileindex_path);
2048 unlockerr = lock_worktree(worktree, LOCK_SH);
2049 if (unlockerr && err == NULL)
2054 struct diff_dir_cb_arg {
2055 struct got_fileindex *fileindex;
2056 struct got_worktree *worktree;
2057 const char *status_path;
2058 size_t status_path_len;
2059 struct got_repository *repo;
2060 got_worktree_status_cb status_cb;
2062 got_worktree_cancel_cb cancel_cb;
2066 static const struct got_error *
2067 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2068 got_worktree_status_cb status_cb, void *status_arg,
2069 struct got_repository *repo)
2071 const struct got_error *err = NULL;
2072 unsigned char status = GOT_STATUS_NO_CHANGE;
2074 struct got_object_id blob_id, commit_id;
2076 err = get_file_status(&status, &sb, ie, abspath, repo);
2077 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2078 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2079 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2080 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2086 static const struct got_error *
2087 status_old_new(void *arg, struct got_fileindex_entry *ie,
2088 struct dirent *de, const char *parent_path)
2090 const struct got_error *err = NULL;
2091 struct diff_dir_cb_arg *a = arg;
2094 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2095 return got_error(GOT_ERR_CANCELLED);
2097 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2098 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2101 if (parent_path[0]) {
2102 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2103 parent_path, de->d_name) == -1)
2104 return got_error_from_errno("asprintf");
2106 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2108 return got_error_from_errno("asprintf");
2111 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2117 static const struct got_error *
2118 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2120 struct diff_dir_cb_arg *a = arg;
2121 struct got_object_id blob_id, commit_id;
2122 unsigned char status;
2124 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2125 return got_error(GOT_ERR_CANCELLED);
2127 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2130 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2131 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2132 if (got_fileindex_entry_has_file_on_disk(ie))
2133 status = GOT_STATUS_MISSING;
2135 status = GOT_STATUS_DELETE;
2136 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2140 static const struct got_error *
2141 status_new(void *arg, struct dirent *de, const char *parent_path)
2143 const struct got_error *err = NULL;
2144 struct diff_dir_cb_arg *a = arg;
2147 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2148 return got_error(GOT_ERR_CANCELLED);
2150 if (de->d_type == DT_DIR)
2153 /* XXX ignore symlinks for now */
2154 if (de->d_type == DT_LNK)
2157 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2160 if (parent_path[0]) {
2161 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2162 return got_error_from_errno("asprintf");
2167 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2174 static const struct got_error *
2175 worktree_status(struct got_worktree *worktree, const char *path,
2176 struct got_fileindex *fileindex, struct got_repository *repo,
2177 got_worktree_status_cb status_cb, void *status_arg,
2178 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2180 const struct got_error *err = NULL;
2181 DIR *workdir = NULL;
2182 struct got_fileindex_diff_dir_cb fdiff_cb;
2183 struct diff_dir_cb_arg arg;
2184 char *ondisk_path = NULL;
2186 if (asprintf(&ondisk_path, "%s%s%s",
2187 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2188 err = got_error_from_errno("asprintf");
2191 workdir = opendir(ondisk_path);
2192 if (workdir == NULL) {
2193 if (errno == ENOTDIR || errno == ENOENT) {
2194 struct got_fileindex_entry *ie;
2195 ie = got_fileindex_entry_get(fileindex, path);
2197 err = got_error(GOT_ERR_BAD_PATH);
2200 err = report_file_status(ie, ondisk_path,
2201 status_cb, status_arg, repo);
2204 err = got_error_from_errno2("opendir", ondisk_path);
2208 fdiff_cb.diff_old_new = status_old_new;
2209 fdiff_cb.diff_old = status_old;
2210 fdiff_cb.diff_new = status_new;
2211 arg.fileindex = fileindex;
2212 arg.worktree = worktree;
2213 arg.status_path = path;
2214 arg.status_path_len = strlen(path);
2216 arg.status_cb = status_cb;
2217 arg.status_arg = status_arg;
2218 arg.cancel_cb = cancel_cb;
2219 arg.cancel_arg = cancel_arg;
2220 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2221 path, repo, &fdiff_cb, &arg);
2229 const struct got_error *
2230 got_worktree_status(struct got_worktree *worktree, const char *path,
2231 struct got_repository *repo, got_worktree_status_cb status_cb,
2232 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2234 const struct got_error *err = NULL;
2235 char *fileindex_path = NULL;
2236 struct got_fileindex *fileindex = NULL;
2238 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2242 err = worktree_status(worktree, path, fileindex, repo,
2243 status_cb, status_arg, cancel_cb, cancel_arg);
2244 free(fileindex_path);
2245 got_fileindex_free(fileindex);
2249 const struct got_error *
2250 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2253 const struct got_error *err = NULL;
2254 char *resolved, *path = NULL;
2259 resolved = realpath(arg, NULL);
2260 if (resolved == NULL)
2261 return got_error_from_errno2("realpath", arg);
2263 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2264 strlen(got_worktree_get_root_path(worktree)))) {
2265 err = got_error(GOT_ERR_BAD_PATH);
2269 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2270 err = got_path_skip_common_ancestor(&path,
2271 got_worktree_get_root_path(worktree), resolved);
2277 err = got_error_from_errno("strdup");
2282 /* XXX status walk can't deal with trailing slash! */
2284 while (path[len - 1] == '/') {
2285 path[len - 1] = '\0';
2297 static const struct got_error *
2298 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2299 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2300 struct got_repository *repo)
2302 const struct got_error *err = NULL;
2303 struct got_fileindex_entry *ie;
2305 /* Re-adding an existing entry is a no-op. */
2306 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2309 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2313 err = got_fileindex_entry_add(fileindex, ie);
2315 got_fileindex_entry_free(ie);
2319 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2322 const struct got_error *
2323 got_worktree_schedule_add(struct got_worktree *worktree,
2324 struct got_pathlist_head *ondisk_paths,
2325 got_worktree_status_cb status_cb, void *status_arg,
2326 struct got_repository *repo)
2328 struct got_fileindex *fileindex = NULL;
2329 char *fileindex_path = NULL;
2330 const struct got_error *err = NULL, *sync_err, *unlockerr;
2331 struct got_pathlist_entry *pe;
2333 err = lock_worktree(worktree, LOCK_EX);
2337 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2341 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2343 err = got_path_skip_common_ancestor(&relpath,
2344 got_worktree_get_root_path(worktree), pe->path);
2347 err = schedule_addition(pe->path, fileindex, relpath,
2348 status_cb, status_arg, repo);
2353 sync_err = sync_fileindex(fileindex, fileindex_path);
2354 if (sync_err && err == NULL)
2357 free(fileindex_path);
2359 got_fileindex_free(fileindex);
2360 unlockerr = lock_worktree(worktree, LOCK_SH);
2361 if (unlockerr && err == NULL)
2366 static const struct got_error *
2367 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2368 const char *relpath, int delete_local_mods,
2369 got_worktree_status_cb status_cb, void *status_arg,
2370 struct got_repository *repo)
2372 const struct got_error *err = NULL;
2373 struct got_fileindex_entry *ie = NULL;
2374 unsigned char status;
2377 ie = got_fileindex_entry_get(fileindex, relpath);
2379 return got_error(GOT_ERR_BAD_PATH);
2381 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2385 if (status != GOT_STATUS_NO_CHANGE) {
2386 if (status == GOT_STATUS_DELETE)
2387 return got_error_set_errno(ENOENT, ondisk_path);
2388 if (status != GOT_STATUS_MODIFY)
2389 return got_error(GOT_ERR_FILE_STATUS);
2390 if (!delete_local_mods)
2391 return got_error(GOT_ERR_FILE_MODIFIED);
2394 if (unlink(ondisk_path) != 0)
2395 return got_error_from_errno2("unlink", ondisk_path);
2397 got_fileindex_entry_mark_deleted_from_disk(ie);
2398 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2401 const struct got_error *
2402 got_worktree_schedule_delete(struct got_worktree *worktree,
2403 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2404 got_worktree_status_cb status_cb, void *status_arg,
2405 struct got_repository *repo)
2407 struct got_fileindex *fileindex = NULL;
2408 char *fileindex_path = NULL;
2409 const struct got_error *err = NULL, *sync_err, *unlockerr;
2410 struct got_pathlist_entry *pe;
2412 err = lock_worktree(worktree, LOCK_EX);
2416 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2420 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2422 err = got_path_skip_common_ancestor(&relpath,
2423 got_worktree_get_root_path(worktree), pe->path);
2426 err = schedule_for_deletion(pe->path, fileindex, relpath,
2427 delete_local_mods, status_cb, status_arg, repo);
2432 sync_err = sync_fileindex(fileindex, fileindex_path);
2433 if (sync_err && err == NULL)
2436 free(fileindex_path);
2438 got_fileindex_free(fileindex);
2439 unlockerr = lock_worktree(worktree, LOCK_SH);
2440 if (unlockerr && err == NULL)
2445 static const struct got_error *
2446 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2447 const char *ondisk_path,
2448 got_worktree_checkout_cb progress_cb, void *progress_arg,
2449 struct got_repository *repo)
2451 const struct got_error *err = NULL;
2452 char *relpath = NULL, *parent_path = NULL;
2453 struct got_fileindex_entry *ie;
2454 struct got_tree_object *tree = NULL;
2455 struct got_object_id *tree_id = NULL;
2456 const struct got_tree_entry *te;
2457 char *tree_path = NULL, *te_name;
2458 struct got_blob_object *blob = NULL;
2459 unsigned char status;
2462 err = got_path_skip_common_ancestor(&relpath,
2463 got_worktree_get_root_path(worktree), ondisk_path);
2467 ie = got_fileindex_entry_get(fileindex, relpath);
2469 err = got_error(GOT_ERR_BAD_PATH);
2473 /* Construct in-repository path of tree which contains this blob. */
2474 err = got_path_dirname(&parent_path, ie->path);
2476 if (err->code != GOT_ERR_BAD_PATH)
2478 parent_path = strdup("/");
2479 if (parent_path == NULL) {
2480 err = got_error_from_errno("strdup");
2484 if (got_path_is_root_dir(worktree->path_prefix)) {
2485 tree_path = strdup(parent_path);
2486 if (tree_path == NULL) {
2487 err = got_error_from_errno("strdup");
2491 if (got_path_is_root_dir(parent_path)) {
2492 tree_path = strdup(worktree->path_prefix);
2493 if (tree_path == NULL) {
2494 err = got_error_from_errno("strdup");
2498 if (asprintf(&tree_path, "%s/%s",
2499 worktree->path_prefix, parent_path) == -1) {
2500 err = got_error_from_errno("asprintf");
2506 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2511 err = got_object_open_as_tree(&tree, repo, tree_id);
2515 te_name = basename(ie->path);
2516 if (te_name == NULL) {
2517 err = got_error_from_errno2("basename", ie->path);
2521 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2525 te = got_object_tree_find_entry(tree, te_name);
2526 if (te == NULL && status != GOT_STATUS_ADD) {
2527 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2532 case GOT_STATUS_ADD:
2533 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2536 got_fileindex_entry_remove(fileindex, ie);
2538 case GOT_STATUS_DELETE:
2539 case GOT_STATUS_MODIFY:
2540 case GOT_STATUS_CONFLICT:
2541 case GOT_STATUS_MISSING: {
2542 struct got_object_id id;
2543 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2544 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2547 err = install_blob(worktree, ondisk_path, ie->path,
2548 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2552 if (status == GOT_STATUS_DELETE) {
2553 err = update_blob_fileindex_entry(worktree,
2554 fileindex, ie, ondisk_path, ie->path, blob, 1);
2568 got_object_blob_close(blob);
2570 got_object_tree_close(tree);
2575 const struct got_error *
2576 got_worktree_revert(struct got_worktree *worktree,
2577 struct got_pathlist_head *ondisk_paths,
2578 got_worktree_checkout_cb progress_cb, void *progress_arg,
2579 struct got_repository *repo)
2581 struct got_fileindex *fileindex = NULL;
2582 char *fileindex_path = NULL;
2583 const struct got_error *err = NULL, *unlockerr = NULL;
2584 const struct got_error *sync_err = NULL;
2585 struct got_pathlist_entry *pe;
2587 err = lock_worktree(worktree, LOCK_EX);
2591 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2595 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2596 err = revert_file(worktree, fileindex, pe->path,
2597 progress_cb, progress_arg, repo);
2601 sync_err = sync_fileindex(fileindex, fileindex_path);
2602 if (sync_err && err == NULL)
2605 free(fileindex_path);
2607 got_fileindex_free(fileindex);
2608 unlockerr = lock_worktree(worktree, LOCK_SH);
2609 if (unlockerr && err == NULL)
2615 free_commitable(struct got_commitable *ct)
2618 free(ct->in_repo_path);
2619 free(ct->ondisk_path);
2621 free(ct->base_blob_id);
2622 free(ct->base_commit_id);
2626 struct collect_commitables_arg {
2627 struct got_pathlist_head *commitable_paths;
2628 struct got_repository *repo;
2629 struct got_worktree *worktree;
2632 static const struct got_error *
2633 collect_commitables(void *arg, unsigned char status, const char *relpath,
2634 struct got_object_id *blob_id, struct got_object_id *commit_id)
2636 struct collect_commitables_arg *a = arg;
2637 const struct got_error *err = NULL;
2638 struct got_commitable *ct = NULL;
2639 struct got_pathlist_entry *new = NULL;
2640 char *parent_path = NULL, *path = NULL;
2643 if (status == GOT_STATUS_CONFLICT)
2644 return got_error(GOT_ERR_COMMIT_CONFLICT);
2646 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2647 status != GOT_STATUS_DELETE)
2650 if (asprintf(&path, "/%s", relpath) == -1) {
2651 err = got_error_from_errno("asprintf");
2654 if (strcmp(path, "/") == 0) {
2655 parent_path = strdup("");
2656 if (parent_path == NULL)
2657 return got_error_from_errno("strdup");
2659 err = got_path_dirname(&parent_path, path);
2664 ct = calloc(1, sizeof(*ct));
2666 err = got_error_from_errno("calloc");
2670 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2672 err = got_error_from_errno("asprintf");
2675 if (status == GOT_STATUS_DELETE) {
2676 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2678 if (lstat(ct->ondisk_path, &sb) != 0) {
2679 err = got_error_from_errno2("lstat", ct->ondisk_path);
2682 ct->mode = sb.st_mode;
2685 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2686 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2688 err = got_error_from_errno("asprintf");
2692 ct->status = status;
2693 ct->blob_id = NULL; /* will be filled in when blob gets created */
2694 if (ct->status != GOT_STATUS_ADD) {
2695 ct->base_blob_id = got_object_id_dup(blob_id);
2696 if (ct->base_blob_id == NULL) {
2697 err = got_error_from_errno("got_object_id_dup");
2700 ct->base_commit_id = got_object_id_dup(commit_id);
2701 if (ct->base_commit_id == NULL) {
2702 err = got_error_from_errno("got_object_id_dup");
2706 ct->path = strdup(path);
2707 if (ct->path == NULL) {
2708 err = got_error_from_errno("strdup");
2711 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2713 if (ct && (err || new == NULL))
2714 free_commitable(ct);
2720 static const struct got_error *write_tree(struct got_object_id **,
2721 struct got_tree_object *, const char *, struct got_pathlist_head *,
2722 got_worktree_status_cb status_cb, void *status_arg,
2723 struct got_repository *);
2725 static const struct got_error *
2726 write_subtree(struct got_object_id **new_subtree_id,
2727 struct got_tree_entry *te, const char *parent_path,
2728 struct got_pathlist_head *commitable_paths,
2729 got_worktree_status_cb status_cb, void *status_arg,
2730 struct got_repository *repo)
2732 const struct got_error *err = NULL;
2733 struct got_tree_object *subtree;
2736 if (asprintf(&subpath, "%s%s%s", parent_path,
2737 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2738 return got_error_from_errno("asprintf");
2740 err = got_object_open_as_tree(&subtree, repo, te->id);
2744 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2745 status_cb, status_arg, repo);
2746 got_object_tree_close(subtree);
2751 static const struct got_error *
2752 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2754 const struct got_error *err = NULL;
2755 char *ct_parent_path = NULL;
2759 if (strchr(ct->path, '/') == NULL) {
2760 *match = got_path_is_root_dir(path);
2764 err = got_path_dirname(&ct_parent_path, ct->path);
2767 *match = (strcmp(path, ct_parent_path) == 0);
2768 free(ct_parent_path);
2773 get_ct_file_mode(struct got_commitable *ct)
2775 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2778 static const struct got_error *
2779 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2780 struct got_tree_entry *te, struct got_commitable *ct)
2782 const struct got_error *err = NULL;
2786 err = got_object_tree_entry_dup(new_te, te);
2790 (*new_te)->mode = get_ct_file_mode(ct);
2792 free((*new_te)->id);
2793 (*new_te)->id = got_object_id_dup(ct->blob_id);
2794 if ((*new_te)->id == NULL) {
2795 err = got_error_from_errno("got_object_id_dup");
2799 if (err && *new_te) {
2800 got_object_tree_entry_close(*new_te);
2806 static const struct got_error *
2807 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2808 struct got_commitable *ct)
2810 const struct got_error *err = NULL;
2815 *new_te = calloc(1, sizeof(**new_te));
2816 if (*new_te == NULL)
2817 return got_error_from_errno("calloc");
2819 ct_name = basename(ct->path);
2820 if (ct_name == NULL) {
2821 err = got_error_from_errno2("basename", ct->path);
2824 (*new_te)->name = strdup(ct_name);
2825 if ((*new_te)->name == NULL) {
2826 err = got_error_from_errno("strdup");
2830 (*new_te)->mode = get_ct_file_mode(ct);
2832 (*new_te)->id = got_object_id_dup(ct->blob_id);
2833 if ((*new_te)->id == NULL) {
2834 err = got_error_from_errno("got_object_id_dup");
2838 if (err && *new_te) {
2839 got_object_tree_entry_close(*new_te);
2845 static const struct got_error *
2846 insert_tree_entry(struct got_tree_entry *new_te,
2847 struct got_pathlist_head *paths)
2849 const struct got_error *err = NULL;
2850 struct got_pathlist_entry *new_pe;
2852 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2856 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2860 static const struct got_error *
2861 report_ct_status(struct got_commitable *ct,
2862 got_worktree_status_cb status_cb, void *status_arg)
2864 const char *ct_path = ct->path;
2865 while (ct_path[0] == '/')
2867 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2870 static const struct got_error *
2871 match_modified_subtree(int *modified, struct got_tree_entry *te,
2872 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2874 const struct got_error *err = NULL;
2875 struct got_pathlist_entry *pe;
2880 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2881 got_path_is_root_dir(base_tree_path) ? "" : "/",
2883 return got_error_from_errno("asprintf");
2885 TAILQ_FOREACH(pe, commitable_paths, entry) {
2886 struct got_commitable *ct = pe->data;
2887 *modified = got_path_is_child(ct->in_repo_path, te_path,
2897 static const struct got_error *
2898 match_deleted_or_modified_ct(struct got_commitable **ctp,
2899 struct got_tree_entry *te, const char *base_tree_path,
2900 struct got_pathlist_head *commitable_paths)
2902 const struct got_error *err = NULL;
2903 struct got_pathlist_entry *pe;
2907 TAILQ_FOREACH(pe, commitable_paths, entry) {
2908 struct got_commitable *ct = pe->data;
2909 char *ct_name = NULL;
2912 if (ct->status != GOT_STATUS_MODIFY &&
2913 ct->status != GOT_STATUS_DELETE)
2916 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2919 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2925 ct_name = basename(pe->path);
2926 if (ct_name == NULL)
2927 return got_error_from_errno2("basename", pe->path);
2929 if (strcmp(te->name, ct_name) != 0)
2939 static const struct got_error *
2940 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2941 const char *child_path, const char *path_base_tree,
2942 struct got_pathlist_head *commitable_paths,
2943 got_worktree_status_cb status_cb, void *status_arg,
2944 struct got_repository *repo)
2946 const struct got_error *err = NULL;
2947 struct got_tree_entry *new_te;
2952 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2953 got_path_is_root_dir(path_base_tree) ? "" : "/",
2955 return got_error_from_errno("asprintf");
2957 new_te = calloc(1, sizeof(*new_te));
2958 new_te->mode = S_IFDIR;
2959 new_te->name = strdup(child_path);
2960 if (new_te->name == NULL) {
2961 err = got_error_from_errno("strdup");
2962 got_object_tree_entry_close(new_te);
2965 err = write_tree(&new_te->id, NULL, subtree_path,
2966 commitable_paths, status_cb, status_arg, repo);
2968 got_object_tree_entry_close(new_te);
2978 static const struct got_error *
2979 write_tree(struct got_object_id **new_tree_id,
2980 struct got_tree_object *base_tree, const char *path_base_tree,
2981 struct got_pathlist_head *commitable_paths,
2982 got_worktree_status_cb status_cb, void *status_arg,
2983 struct got_repository *repo)
2985 const struct got_error *err = NULL;
2986 const struct got_tree_entries *base_entries = NULL;
2987 struct got_pathlist_head paths;
2988 struct got_tree_entries new_tree_entries;
2989 struct got_tree_entry *te, *new_te = NULL;
2990 struct got_pathlist_entry *pe;
2993 new_tree_entries.nentries = 0;
2994 SIMPLEQ_INIT(&new_tree_entries.head);
2996 /* Insert, and recurse into, newly added entries first. */
2997 TAILQ_FOREACH(pe, commitable_paths, entry) {
2998 struct got_commitable *ct = pe->data;
2999 char *child_path = NULL, *slash;
3001 if (ct->status != GOT_STATUS_ADD ||
3002 (ct->flags & GOT_COMMITABLE_ADDED))
3005 if (!got_path_is_child(pe->path, path_base_tree,
3006 strlen(path_base_tree)))
3009 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3014 slash = strchr(child_path, '/');
3015 if (slash == NULL) {
3016 err = alloc_added_blob_tree_entry(&new_te, ct);
3019 err = report_ct_status(ct, status_cb, status_arg);
3022 ct->flags |= GOT_COMMITABLE_ADDED;
3023 err = insert_tree_entry(new_te, &paths);
3027 *slash = '\0'; /* trim trailing path components */
3028 if (base_tree == NULL ||
3029 got_object_tree_find_entry(base_tree, child_path)
3031 err = make_subtree_for_added_blob(&new_te,
3032 child_path, path_base_tree,
3033 commitable_paths, status_cb, status_arg,
3037 err = insert_tree_entry(new_te, &paths);
3045 /* Handle modified and deleted entries. */
3046 base_entries = got_object_tree_get_entries(base_tree);
3047 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3048 struct got_commitable *ct = NULL;
3050 if (S_ISDIR(te->mode)) {
3052 err = got_object_tree_entry_dup(&new_te, te);
3055 err = match_modified_subtree(&modified, te,
3056 path_base_tree, commitable_paths);
3059 /* Avoid recursion into unmodified subtrees. */
3062 err = write_subtree(&new_te->id, te,
3063 path_base_tree, commitable_paths,
3064 status_cb, status_arg, repo);
3068 err = insert_tree_entry(new_te, &paths);
3074 err = match_deleted_or_modified_ct(&ct, te,
3075 path_base_tree, commitable_paths);
3077 /* NB: Deleted entries get dropped here. */
3078 if (ct->status == GOT_STATUS_MODIFY) {
3079 err = alloc_modified_blob_tree_entry(
3083 err = insert_tree_entry(new_te, &paths);
3087 err = report_ct_status(ct, status_cb,
3092 /* Entry is unchanged; just copy it. */
3093 err = got_object_tree_entry_dup(&new_te, te);
3096 err = insert_tree_entry(new_te, &paths);
3103 /* Write new list of entries; deleted entries have been dropped. */
3104 TAILQ_FOREACH(pe, &paths, entry) {
3105 struct got_tree_entry *te = pe->data;
3106 new_tree_entries.nentries++;
3107 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3109 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3111 got_object_tree_entries_close(&new_tree_entries);
3112 got_pathlist_free(&paths);
3116 static const struct got_error *
3117 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3118 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3120 const struct got_error *err = NULL;
3121 struct got_pathlist_entry *pe;
3123 TAILQ_FOREACH(pe, commitable_paths, entry) {
3124 struct got_fileindex_entry *ie;
3125 struct got_commitable *ct = pe->data;
3127 ie = got_fileindex_entry_get(fileindex, pe->path);
3129 if (ct->status == GOT_STATUS_DELETE) {
3130 got_fileindex_entry_remove(fileindex, ie);
3131 got_fileindex_entry_free(ie);
3133 err = got_fileindex_entry_update(ie,
3134 ct->ondisk_path, ct->blob_id->sha1,
3135 new_base_commit_id->sha1, 1);
3137 err = got_fileindex_entry_alloc(&ie,
3138 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3139 new_base_commit_id->sha1);
3142 err = got_fileindex_entry_add(fileindex, ie);
3150 static const struct got_error *
3151 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3152 struct got_object_id *head_commit_id)
3154 const struct got_error *err = NULL;
3155 struct got_object_id *id_in_head = NULL, *id = NULL;
3156 struct got_commit_object *commit = NULL;
3158 const char *ct_path = ct->in_repo_path;
3160 while (ct_path[0] == '/')
3164 * Ensure that no modifications were made to files *and their parents*
3165 * in commits between the file's base commit and the branch head.
3167 * Checking the parents is important for detecting conflicting tree
3168 * configurations (files or parent folders might have been moved,
3169 * deleted, added again, etc.). Such changes need to be merged with
3170 * local changes before a commit can occur.
3172 * The implication is that the file's (parent) entry in the root
3173 * directory must have the same ID in all relevant commits.
3175 if (ct->status != GOT_STATUS_ADD) {
3176 struct got_object_qid *pid;
3178 struct got_object_id *root_entry_id = NULL;
3180 /* Trivial case: base commit == head commit */
3181 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3184 /* Compute the path to the root directory's entry. */
3185 path = strdup(ct_path);
3187 err = got_error_from_errno("strdup");
3190 slash = strchr(path, '/');
3194 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3198 err = got_object_id_by_path(&root_entry_id, repo,
3199 head_commit_id, path);
3203 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3205 struct got_commit_object *pcommit;
3207 err = got_object_id_by_path(&id, repo, pid->id, path);
3209 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3215 err = got_object_id_by_path(&id, repo, pid->id, path);
3219 if (got_object_id_cmp(id, root_entry_id) != 0) {
3220 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3224 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3225 break; /* all relevant commits scanned */
3227 err = got_object_open_as_commit(&pcommit, repo,
3232 got_object_commit_close(commit);
3234 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3238 /* Require that added files don't exist in the branch head. */
3239 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3241 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3243 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3247 got_object_commit_close(commit);
3254 const struct got_error *
3255 commit_worktree(struct got_object_id **new_commit_id,
3256 struct got_pathlist_head *commitable_paths,
3257 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3258 const char *ondisk_path, const char *author, const char *committer,
3259 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3260 got_worktree_status_cb status_cb, void *status_arg,
3261 struct got_repository *repo)
3263 const struct got_error *err = NULL, *unlockerr = NULL;
3264 struct got_pathlist_entry *pe;
3265 const char *head_ref_name = NULL;
3266 struct got_commit_object *head_commit = NULL;
3267 struct got_reference *head_ref2 = NULL;
3268 struct got_object_id *head_commit_id2 = NULL;
3269 struct got_tree_object *head_tree = NULL;
3270 struct got_object_id *new_tree_id = NULL;
3271 struct got_object_id_queue parent_ids;
3272 struct got_object_qid *pid = NULL;
3273 char *logmsg = NULL;
3275 *new_commit_id = NULL;
3277 SIMPLEQ_INIT(&parent_ids);
3279 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3283 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3287 if (commit_msg_cb != NULL) {
3288 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3293 if (logmsg == NULL || strlen(logmsg) == 0) {
3294 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3298 /* Create blobs from added and modified files and record their IDs. */
3299 TAILQ_FOREACH(pe, commitable_paths, entry) {
3300 struct got_commitable *ct = pe->data;
3303 if (ct->status != GOT_STATUS_ADD &&
3304 ct->status != GOT_STATUS_MODIFY)
3307 if (asprintf(&ondisk_path, "%s/%s",
3308 worktree->root_path, pe->path) == -1) {
3309 err = got_error_from_errno("asprintf");
3312 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3318 /* Recursively write new tree objects. */
3319 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3320 status_cb, status_arg, repo);
3324 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3327 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3328 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3329 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3330 got_object_qid_free(pid);
3336 /* Check if a concurrent commit to our branch has occurred. */
3337 head_ref_name = got_worktree_get_head_ref_name(worktree);
3338 if (head_ref_name == NULL) {
3339 err = got_error_from_errno("got_worktree_get_head_ref_name");
3342 /* Lock the reference here to prevent concurrent modification. */
3343 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3346 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3349 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3350 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3353 /* Update branch head in repository. */
3354 err = got_ref_change_ref(head_ref2, *new_commit_id);
3357 err = got_ref_write(head_ref2, repo);
3361 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3365 err = ref_base_commit(worktree, repo);
3370 got_object_tree_close(head_tree);
3372 got_object_commit_close(head_commit);
3373 free(head_commit_id2);
3375 unlockerr = got_ref_unlock(head_ref2);
3376 if (unlockerr && err == NULL)
3378 got_ref_close(head_ref2);
3383 const struct got_error *
3384 got_worktree_commit(struct got_object_id **new_commit_id,
3385 struct got_worktree *worktree, const char *ondisk_path,
3386 const char *author, const char *committer,
3387 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3388 got_worktree_status_cb status_cb, void *status_arg,
3389 struct got_repository *repo)
3391 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3392 struct got_fileindex *fileindex = NULL;
3393 char *fileindex_path = NULL, *relpath = NULL;
3394 struct got_pathlist_head commitable_paths;
3395 struct collect_commitables_arg cc_arg;
3396 struct got_pathlist_entry *pe;
3397 struct got_reference *head_ref = NULL;
3398 struct got_object_id *head_commit_id = NULL;
3400 *new_commit_id = NULL;
3402 TAILQ_INIT(&commitable_paths);
3404 err = lock_worktree(worktree, LOCK_EX);
3408 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3412 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3417 err = got_path_skip_common_ancestor(&relpath,
3418 worktree->root_path, ondisk_path);
3423 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3427 cc_arg.commitable_paths = &commitable_paths;
3428 cc_arg.worktree = worktree;
3430 err = worktree_status(worktree, relpath ? relpath : "",
3431 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3435 if (TAILQ_EMPTY(&commitable_paths)) {
3436 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3440 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3441 struct got_commitable *ct = pe->data;
3442 err = check_ct_out_of_date(ct, repo, head_commit_id);
3447 err = commit_worktree(new_commit_id, &commitable_paths,
3448 head_commit_id, worktree, ondisk_path, author, committer,
3449 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3453 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3455 sync_err = sync_fileindex(fileindex, fileindex_path);
3456 if (sync_err && err == NULL)
3460 got_fileindex_free(fileindex);
3461 free(fileindex_path);
3463 unlockerr = lock_worktree(worktree, LOCK_SH);
3464 if (unlockerr && err == NULL)
3466 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3467 struct got_commitable *ct = pe->data;
3468 free_commitable(ct);
3470 got_pathlist_free(&commitable_paths);
3475 got_commitable_get_path(struct got_commitable *ct)
3481 got_commitable_get_status(struct got_commitable *ct)
3486 struct check_rebase_ok_arg {
3487 struct got_worktree *worktree;
3488 struct got_repository *repo;
3489 int rebase_in_progress;
3492 static const struct got_error *
3493 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3495 const struct got_error *err = NULL;
3496 struct check_rebase_ok_arg *a = arg;
3497 unsigned char status;
3501 if (!a->rebase_in_progress) {
3502 /* Reject rebase of a work tree with mixed base commits. */
3503 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3504 SHA1_DIGEST_LENGTH))
3505 return got_error(GOT_ERR_MIXED_COMMITS);
3508 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3510 return got_error_from_errno("asprintf");
3512 /* Reject rebase of a work tree with modified or conflicted files. */
3513 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3518 if (a->rebase_in_progress) {
3519 if (status == GOT_STATUS_CONFLICT)
3520 return got_error(GOT_ERR_CONFLICTS);
3521 } else if (status != GOT_STATUS_NO_CHANGE)
3522 return got_error(GOT_ERR_MODIFIED);
3527 const struct got_error *
3528 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3529 struct got_reference **tmp_branch, struct got_worktree *worktree,
3530 struct got_reference *branch, struct got_repository *repo)
3532 const struct got_error *err = NULL;
3533 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3534 char *branch_ref_name = NULL;
3535 struct got_fileindex *fileindex = NULL;
3536 char *fileindex_path = NULL;
3537 struct check_rebase_ok_arg ok_arg;
3538 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3540 *new_base_branch_ref = NULL;
3543 err = lock_worktree(worktree, LOCK_EX);
3547 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3551 ok_arg.worktree = worktree;
3553 ok_arg.rebase_in_progress = 0;
3554 err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3559 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3563 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3567 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3571 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3576 err = got_ref_alloc_symref(new_base_branch_ref,
3577 new_base_branch_ref_name, wt_branch);
3580 err = got_ref_write(*new_base_branch_ref, repo);
3584 /* TODO Lock original branch's ref while rebasing? */
3586 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3590 err = got_ref_write(branch_ref, repo);
3594 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3595 worktree->base_commit_id);
3598 err = got_ref_write(*tmp_branch, repo);
3602 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3606 free(fileindex_path);
3608 got_fileindex_free(fileindex);
3609 free(tmp_branch_name);
3610 free(new_base_branch_ref_name);
3611 free(branch_ref_name);
3613 got_ref_close(branch_ref);
3615 got_ref_close(wt_branch);
3617 if (*new_base_branch_ref) {
3618 got_ref_close(*new_base_branch_ref);
3619 *new_base_branch_ref = NULL;
3622 got_ref_close(*tmp_branch);
3625 lock_worktree(worktree, LOCK_SH);
3630 const struct got_error *
3631 got_worktree_rebase_continue(struct got_object_id **commit_id,
3632 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3633 struct got_reference **branch, struct got_worktree *worktree,
3634 struct got_repository *repo)
3636 const struct got_error *err;
3637 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3638 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3639 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3643 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3647 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3651 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3655 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3659 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3663 err = got_ref_open(branch, repo,
3664 got_ref_get_symref_target(branch_ref), 0);
3668 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3672 err = got_ref_resolve(commit_id, repo, commit_ref);
3676 err = got_ref_open(new_base_branch, repo,
3677 new_base_branch_ref_name, 0);
3681 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3685 free(commit_ref_name);
3686 free(branch_ref_name);
3688 got_ref_close(commit_ref);
3690 got_ref_close(branch_ref);
3695 got_ref_close(*tmp_branch);
3698 if (*new_base_branch) {
3699 got_ref_close(*new_base_branch);
3700 *new_base_branch = NULL;
3703 got_ref_close(*branch);
3710 const struct got_error *
3711 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3713 const struct got_error *err;
3714 char *tmp_branch_name = NULL;
3716 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3720 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3721 free(tmp_branch_name);
3725 static const struct got_error *
3726 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3727 char **logmsg, void *arg)
3729 struct got_commit_object *commit = arg;
3731 *logmsg = strdup(got_object_commit_get_logmsg(commit));
3732 if (*logmsg == NULL)
3733 return got_error_from_errno("strdup");
3738 static const struct got_error *
3739 rebase_status(void *arg, unsigned char status, const char *path,
3740 struct got_object_id *blob_id, struct got_object_id *commit_id)
3745 struct collect_merged_paths_arg {
3746 got_worktree_checkout_cb progress_cb;
3748 struct got_pathlist_head *merged_paths;
3751 static const struct got_error *
3752 collect_merged_paths(void *arg, unsigned char status, const char *path)
3754 const struct got_error *err;
3755 struct collect_merged_paths_arg *a = arg;
3757 struct got_pathlist_entry *new;
3759 err = (*a->progress_cb)(a->progress_arg, status, path);
3763 if (status != GOT_STATUS_MERGE &&
3764 status != GOT_STATUS_ADD &&
3765 status != GOT_STATUS_DELETE &&
3766 status != GOT_STATUS_CONFLICT)
3771 return got_error_from_errno("strdup");
3773 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3774 if (err || new == NULL)
3780 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3782 struct got_pathlist_entry *pe;
3784 TAILQ_FOREACH(pe, merged_paths, entry)
3785 free((char *)pe->path);
3787 got_pathlist_free(merged_paths);
3790 const struct got_error *
3791 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3792 struct got_worktree *worktree, struct got_object_id *parent_commit_id,
3793 struct got_object_id *commit_id, struct got_repository *repo,
3794 got_worktree_checkout_cb progress_cb, void *progress_arg,
3795 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3797 const struct got_error *err;
3798 struct got_fileindex *fileindex;
3799 char *fileindex_path, *commit_ref_name = NULL;
3800 struct got_reference *commit_ref = NULL;
3801 struct collect_merged_paths_arg cmp_arg;
3803 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3805 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3809 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3812 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3814 if (err->code != GOT_ERR_NOT_REF)
3816 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3819 err = got_ref_write(commit_ref, repo);
3823 struct got_object_id *stored_id;
3826 err = got_ref_resolve(&stored_id, repo, commit_ref);
3829 cmp = got_object_id_cmp(commit_id, stored_id);
3832 err = got_error(GOT_ERR_REBASE_COMMITID);
3837 cmp_arg.progress_cb = progress_cb;
3838 cmp_arg.progress_arg = progress_arg;
3839 cmp_arg.merged_paths = merged_paths;
3840 err = merge_files(worktree, fileindex, fileindex_path,
3841 parent_commit_id, commit_id, repo, collect_merged_paths,
3842 &cmp_arg, cancel_cb, cancel_arg);
3844 got_fileindex_free(fileindex);
3845 free(fileindex_path);
3847 got_ref_close(commit_ref);
3851 const struct got_error *
3852 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3853 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
3854 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
3855 struct got_object_id *orig_commit_id, struct got_repository *repo)
3857 const struct got_error *err, *sync_err;
3858 struct got_pathlist_head commitable_paths;
3859 struct collect_commitables_arg cc_arg;
3860 struct got_fileindex *fileindex = NULL;
3861 char *fileindex_path = NULL, *commit_ref_name = NULL;
3862 struct got_reference *head_ref = NULL;
3863 struct got_object_id *head_commit_id = NULL;
3864 struct got_reference *commit_ref = NULL;
3865 struct got_object_id *commit_id = NULL;
3867 TAILQ_INIT(&commitable_paths);
3868 *new_commit_id = NULL;
3870 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3872 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3875 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3878 err = got_ref_resolve(&commit_id, repo, commit_ref);
3881 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3882 err = got_error(GOT_ERR_REBASE_COMMITID);
3886 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3890 cc_arg.commitable_paths = &commitable_paths;
3891 cc_arg.worktree = worktree;
3894 * If possible get the status of individual files directly to
3895 * avoid crawling the entire work tree once per rebased commit.
3896 * TODO: Ideally, merged_paths would contain a list of commitables
3897 * we could use so we could skip worktree_status() entirely.
3900 struct got_pathlist_entry *pe;
3901 TAILQ_FOREACH(pe, merged_paths, entry) {
3902 err = worktree_status(worktree, pe->path, fileindex,
3903 repo, collect_commitables, &cc_arg, NULL, NULL);
3908 err = worktree_status(worktree, "", fileindex, repo,
3909 collect_commitables, &cc_arg, NULL, NULL);
3914 if (TAILQ_EMPTY(&commitable_paths)) {
3915 /* No-op change; commit will be elided. */
3916 err = got_ref_delete(commit_ref, repo);
3919 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3923 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3927 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3931 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
3932 worktree, NULL, got_object_commit_get_author(orig_commit),
3933 got_object_commit_get_committer(orig_commit),
3934 collect_rebase_commit_msg, orig_commit,
3935 rebase_status, NULL, repo);
3939 err = got_ref_change_ref(tmp_branch, *new_commit_id);
3943 err = got_ref_delete(commit_ref, repo);
3947 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3949 sync_err = sync_fileindex(fileindex, fileindex_path);
3950 if (sync_err && err == NULL)
3954 got_fileindex_free(fileindex);
3955 free(fileindex_path);
3956 free(commit_ref_name);
3958 got_ref_close(commit_ref);
3959 free(head_commit_id);
3961 got_ref_close(head_ref);
3963 free(*new_commit_id);
3964 *new_commit_id = NULL;
3969 const struct got_error *
3970 got_worktree_rebase_postpone(struct got_worktree *worktree)
3972 return lock_worktree(worktree, LOCK_SH);
3975 static const struct got_error *
3976 delete_ref(const char *name, struct got_repository *repo)
3978 const struct got_error *err;
3979 struct got_reference *ref;
3981 err = got_ref_open(&ref, repo, name, 0);
3983 if (err->code == GOT_ERR_NOT_REF)
3988 err = got_ref_delete(ref, repo);
3993 static const struct got_error *
3994 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
3996 const struct got_error *err;
3997 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3998 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4000 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4003 err = delete_ref(tmp_branch_name, repo);
4007 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4010 err = delete_ref(new_base_branch_ref_name, repo);
4014 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4017 err = delete_ref(branch_ref_name, repo);
4021 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4024 err = delete_ref(commit_ref_name, repo);
4029 free(tmp_branch_name);
4030 free(new_base_branch_ref_name);
4031 free(branch_ref_name);
4032 free(commit_ref_name);
4036 const struct got_error *
4037 got_worktree_rebase_complete(struct got_worktree *worktree,
4038 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
4039 struct got_reference *rebased_branch,
4040 struct got_repository *repo)
4042 const struct got_error *err, *unlockerr;
4043 struct got_object_id *new_head_commit_id = NULL;
4045 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4049 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4053 err = got_ref_write(rebased_branch, repo);
4057 err = got_worktree_set_head_ref(worktree, rebased_branch);
4061 err = delete_rebase_refs(worktree, repo);
4063 free(new_head_commit_id);
4064 unlockerr = lock_worktree(worktree, LOCK_SH);
4065 if (unlockerr && err == NULL)
4070 struct collect_revertible_paths_arg {
4071 struct got_pathlist_head *revertible_paths;
4072 struct got_worktree *worktree;
4075 static const struct got_error *
4076 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4077 struct got_object_id *blob_id, struct got_object_id *commit_id)
4079 struct collect_revertible_paths_arg *a = arg;
4080 const struct got_error *err = NULL;
4081 struct got_pathlist_entry *new = NULL;
4084 if (status != GOT_STATUS_ADD &&
4085 status != GOT_STATUS_DELETE &&
4086 status != GOT_STATUS_MODIFY &&
4087 status != GOT_STATUS_CONFLICT &&
4088 status != GOT_STATUS_MISSING)
4091 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4092 return got_error_from_errno("asprintf");
4094 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4095 if (err || new == NULL)
4100 const struct got_error *
4101 got_worktree_rebase_abort(struct got_worktree *worktree,
4102 struct got_repository *repo, struct got_reference *new_base_branch,
4103 got_worktree_checkout_cb progress_cb, void *progress_arg)
4105 const struct got_error *err, *unlockerr, *sync_err;
4106 struct got_reference *resolved = NULL;
4107 struct got_object_id *commit_id = NULL;
4108 struct got_fileindex *fileindex = NULL;
4109 char *fileindex_path = NULL;
4110 struct got_pathlist_head revertible_paths;
4111 struct got_pathlist_entry *pe;
4112 struct collect_revertible_paths_arg crp_arg;
4113 struct got_object_id *tree_id = NULL;
4115 TAILQ_INIT(&revertible_paths);
4117 err = lock_worktree(worktree, LOCK_EX);
4121 err = got_ref_open(&resolved, repo,
4122 got_ref_get_symref_target(new_base_branch), 0);
4126 err = got_worktree_set_head_ref(worktree, resolved);
4131 * XXX commits to the base branch could have happened while
4132 * we were busy rebasing; should we store the original commit ID
4133 * when rebase begins and read it back here?
4135 err = got_ref_resolve(&commit_id, repo, resolved);
4139 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4143 err = got_object_id_by_path(&tree_id, repo,
4144 worktree->base_commit_id, worktree->path_prefix);
4148 err = delete_rebase_refs(worktree, repo);
4152 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4156 crp_arg.revertible_paths = &revertible_paths;
4157 crp_arg.worktree = worktree;
4158 err = worktree_status(worktree, "", fileindex, repo,
4159 collect_revertible_paths, &crp_arg, NULL, NULL);
4163 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4164 err = revert_file(worktree, fileindex, pe->path,
4165 progress_cb, progress_arg, repo);
4170 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4171 repo, progress_cb, progress_arg, NULL, NULL);
4173 sync_err = sync_fileindex(fileindex, fileindex_path);
4174 if (sync_err && err == NULL)
4177 got_ref_close(resolved);
4181 got_fileindex_free(fileindex);
4182 free(fileindex_path);
4183 TAILQ_FOREACH(pe, &revertible_paths, entry)
4184 free((char *)pe->path);
4185 got_pathlist_free(&revertible_paths);
4187 unlockerr = lock_worktree(worktree, LOCK_SH);
4188 if (unlockerr && err == NULL)