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_path(path, 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 (fstat(fd, &sb) != 0) {
146 err = got_error_from_errno2("fstat", 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_path(path, GOT_ERR_WORKTREE_META));
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error_path(path, 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_msg(GOT_ERR_WORKTREE_META,
359 "could not parse work tree format version number");
362 if (version != GOT_WORKTREE_FORMAT_VERSION) {
363 err = got_error(GOT_ERR_WORKTREE_VERS);
367 *worktree = calloc(1, sizeof(**worktree));
368 if (*worktree == NULL) {
369 err = got_error_from_errno("calloc");
372 (*worktree)->lockfd = -1;
374 (*worktree)->root_path = strdup(path);
375 if ((*worktree)->root_path == NULL) {
376 err = got_error_from_errno("strdup");
379 err = read_meta_file(&(*worktree)->repo_path, path_got,
380 GOT_WORKTREE_REPOSITORY);
384 err = read_meta_file(&(*worktree)->path_prefix, path_got,
385 GOT_WORKTREE_PATH_PREFIX);
389 err = read_meta_file(&base_commit_id_str, path_got,
390 GOT_WORKTREE_BASE_COMMIT);
394 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
397 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
398 if (uuid_status != uuid_s_ok) {
399 err = got_error_uuid(uuid_status);
403 err = got_repo_open(&repo, (*worktree)->repo_path);
407 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
412 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
413 GOT_WORKTREE_HEAD_REF);
416 got_repo_close(repo);
419 free(base_commit_id_str);
425 if (*worktree != NULL)
426 got_worktree_close(*worktree);
429 (*worktree)->lockfd = fd;
434 const struct got_error *
435 got_worktree_open(struct got_worktree **worktree, const char *path)
437 const struct got_error *err = NULL;
440 err = open_worktree(worktree, path);
441 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
445 path = dirname(path);
447 return got_error_from_errno2("dirname", path);
448 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
450 return got_error(GOT_ERR_NOT_WORKTREE);
453 const struct got_error *
454 got_worktree_close(struct got_worktree *worktree)
456 const struct got_error *err = NULL;
457 free(worktree->root_path);
458 free(worktree->repo_path);
459 free(worktree->path_prefix);
460 free(worktree->base_commit_id);
461 free(worktree->head_ref_name);
462 if (worktree->lockfd != -1)
463 if (close(worktree->lockfd) != 0)
464 err = got_error_from_errno2("close",
465 got_worktree_get_root_path(worktree));
471 got_worktree_get_root_path(struct got_worktree *worktree)
473 return worktree->root_path;
477 got_worktree_get_repo_path(struct got_worktree *worktree)
479 return worktree->repo_path;
483 got_worktree_get_path_prefix(struct got_worktree *worktree)
485 return worktree->path_prefix;
488 const struct got_error *
489 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
490 const char *path_prefix)
492 char *absprefix = NULL;
494 if (!got_path_is_absolute(path_prefix)) {
495 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
496 return got_error_from_errno("asprintf");
498 *match = (strcmp(absprefix ? absprefix : path_prefix,
499 worktree->path_prefix) == 0);
505 got_worktree_get_head_ref_name(struct got_worktree *worktree)
507 return worktree->head_ref_name;
510 const struct got_error *
511 got_worktree_set_head_ref(struct got_worktree *worktree,
512 struct got_reference *head_ref)
514 const struct got_error *err = NULL;
515 char *path_got = NULL, *head_ref_name = NULL;
517 if (asprintf(&path_got, "%s/%s", worktree->root_path,
518 GOT_WORKTREE_GOT_DIR) == -1) {
519 err = got_error_from_errno("asprintf");
524 head_ref_name = strdup(got_ref_get_name(head_ref));
525 if (head_ref_name == NULL) {
526 err = got_error_from_errno("strdup");
530 err = write_head_ref(path_got, head_ref);
534 free(worktree->head_ref_name);
535 worktree->head_ref_name = head_ref_name;
543 struct got_object_id *
544 got_worktree_get_base_commit_id(struct got_worktree *worktree)
546 return worktree->base_commit_id;
549 const struct got_error *
550 got_worktree_set_base_commit_id(struct got_worktree *worktree,
551 struct got_repository *repo, struct got_object_id *commit_id)
553 const struct got_error *err;
554 struct got_object *obj = NULL;
556 char *path_got = NULL;
558 if (asprintf(&path_got, "%s/%s", worktree->root_path,
559 GOT_WORKTREE_GOT_DIR) == -1) {
560 err = got_error_from_errno("asprintf");
565 err = got_object_open(&obj, repo, commit_id);
569 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
570 err = got_error(GOT_ERR_OBJ_TYPE);
574 /* Record our base commit. */
575 err = got_object_id_str(&id_str, commit_id);
578 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
582 free(worktree->base_commit_id);
583 worktree->base_commit_id = got_object_id_dup(commit_id);
584 if (worktree->base_commit_id == NULL) {
585 err = got_error_from_errno("got_object_id_dup");
590 got_object_close(obj);
596 static const struct got_error *
597 lock_worktree(struct got_worktree *worktree, int operation)
599 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
600 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
601 : got_error_from_errno2("flock",
602 got_worktree_get_root_path(worktree)));
606 static const struct got_error *
607 add_dir_on_disk(struct got_worktree *worktree, const char *path)
609 const struct got_error *err = NULL;
612 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
613 return got_error_from_errno("asprintf");
615 err = got_path_mkdir(abspath);
616 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
619 if (lstat(abspath, &sb) == -1) {
620 err = got_error_from_errno2("lstat", abspath);
621 } else if (!S_ISDIR(sb.st_mode)) {
622 /* TODO directory is obstructed; do something */
623 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
630 static const struct got_error *
631 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
633 const struct got_error *err = NULL;
636 size_t flen1 = 0, flen2 = 0;
641 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
642 if (flen1 == 0 && ferror(f1)) {
643 err = got_error_from_errno("fread");
646 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
647 if (flen2 == 0 && ferror(f2)) {
648 err = got_error_from_errno("fread");
655 } else if (flen2 == 0) {
659 } else if (flen1 == flen2) {
660 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
673 static const struct got_error *
674 check_files_equal(int *same, const char *f1_path, const char *f2_path)
676 const struct got_error *err = NULL;
679 FILE *f1 = NULL, *f2 = NULL;
683 if (lstat(f1_path, &sb) != 0) {
684 err = got_error_from_errno2("lstat", f1_path);
689 if (lstat(f2_path, &sb) != 0) {
690 err = got_error_from_errno2("lstat", f2_path);
695 if (size1 != size2) {
700 f1 = fopen(f1_path, "r");
702 return got_error_from_errno2("open", f1_path);
704 f2 = fopen(f2_path, "r");
706 err = got_error_from_errno2("open", f2_path);
710 err = check_file_contents_equal(same, f1, f2);
712 if (f1 && fclose(f1) != 0 && err == NULL)
713 err = got_error_from_errno("fclose");
714 if (f2 && fclose(f2) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");
721 * Perform a 3-way merge where blob_orig acts as the common ancestor,
722 * blob_deriv acts as the first derived version, and the file on disk
723 * acts as the second derived version.
725 static const struct got_error *
726 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
727 struct got_blob_object *blob_orig, const char *ondisk_path,
728 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
729 struct got_object_id *deriv_base_commit_id,
730 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
733 const struct got_error *err = NULL;
735 FILE *f_deriv = NULL, *f_orig = NULL;
736 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
737 char *merged_path = NULL, *base_path = NULL;
739 char *label_deriv = NULL;
743 *local_changes_subsumed = 0;
745 parent = dirname(ondisk_path);
747 return got_error_from_errno2("dirname", ondisk_path);
749 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
750 return got_error_from_errno("asprintf");
752 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
757 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
758 err = got_error_from_errno("asprintf");
763 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
766 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
772 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
773 err = got_error_from_errno("asprintf");
778 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
782 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
788 * If the file has no blob, this is an "add vs add" conflict,
789 * and we simply use an empty ancestor file to make both files
790 * appear in the merged result in their entirety.
794 err = got_object_id_str(&id_str, deriv_base_commit_id);
797 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
798 err = got_error_from_errno("asprintf");
802 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
803 blob_orig_path, ondisk_path, label_deriv, path);
807 err = (*progress_cb)(progress_arg,
808 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
812 if (fsync(merged_fd) != 0) {
813 err = got_error_from_errno("fsync");
817 /* Check if a clean merge has subsumed all local changes. */
818 if (overlapcnt == 0) {
819 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
825 if (chmod(merged_path, st_mode) != 0) {
826 err = got_error_from_errno2("chmod", merged_path);
830 if (rename(merged_path, ondisk_path) != 0) {
831 err = got_error_from_errno3("rename", merged_path,
838 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
839 err = got_error_from_errno("close");
840 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
841 err = got_error_from_errno("fclose");
842 if (f_orig && fclose(f_orig) != 0 && err == NULL)
843 err = got_error_from_errno("fclose");
846 if (blob_deriv_path) {
847 unlink(blob_deriv_path);
848 free(blob_deriv_path);
850 if (blob_orig_path) {
851 unlink(blob_orig_path);
852 free(blob_orig_path);
859 static const struct got_error *
860 update_blob_fileindex_entry(struct got_worktree *worktree,
861 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
862 const char *ondisk_path, const char *path, struct got_blob_object *blob,
863 int update_timestamps)
865 const struct got_error *err = NULL;
868 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
870 err = got_fileindex_entry_update(ie, ondisk_path,
871 blob->id.sha1, worktree->base_commit_id->sha1,
874 struct got_fileindex_entry *new_ie;
875 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
876 path, blob->id.sha1, worktree->base_commit_id->sha1);
878 err = got_fileindex_entry_add(fileindex, new_ie);
883 static const struct got_error *
884 install_blob(struct got_worktree *worktree, const char *ondisk_path,
885 const char *path, uint16_t te_mode, uint16_t st_mode,
886 struct got_blob_object *blob, int restoring_missing_file,
887 int reverting_versioned_file, struct got_repository *repo,
888 got_worktree_checkout_cb progress_cb, void *progress_arg)
890 const struct got_error *err = NULL;
894 char *tmppath = NULL;
896 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
897 GOT_DEFAULT_FILE_MODE);
899 if (errno == ENOENT) {
900 char *parent = dirname(path);
902 return got_error_from_errno2("dirname", path);
903 err = add_dir_on_disk(worktree, parent);
906 fd = open(ondisk_path,
907 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
908 GOT_DEFAULT_FILE_MODE);
910 return got_error_from_errno2("open",
912 } else if (errno == EEXIST) {
913 if (!S_ISREG(st_mode)) {
914 /* TODO file is obstructed; do something */
915 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
918 err = got_opentemp_named_fd(&tmppath, &fd,
925 return got_error_from_errno2("open", ondisk_path);
928 if (restoring_missing_file)
929 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
930 else if (reverting_versioned_file)
931 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
933 err = (*progress_cb)(progress_arg,
934 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
938 hdrlen = got_object_blob_get_hdrlen(blob);
940 const uint8_t *buf = got_object_blob_get_read_buf(blob);
941 err = got_object_blob_read_block(&len, blob);
945 /* Skip blob object header first time around. */
946 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
948 err = got_error_from_errno("write");
950 } else if (outlen != len - hdrlen) {
951 err = got_error(GOT_ERR_IO);
958 if (fsync(fd) != 0) {
959 err = got_error_from_errno("fsync");
964 if (rename(tmppath, ondisk_path) != 0) {
965 err = got_error_from_errno3("rename", tmppath,
972 if (te_mode & S_IXUSR) {
973 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
974 err = got_error_from_errno2("chmod", ondisk_path);
978 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
979 err = got_error_from_errno2("chmod", ondisk_path);
985 if (fd != -1 && close(fd) != 0 && err == NULL)
986 err = got_error_from_errno("close");
991 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
992 static const struct got_error *
993 get_modified_file_content_status(unsigned char *status, FILE *f)
995 const struct got_error *err = NULL;
996 const char *markers[3] = {
997 GOT_DIFF_CONFLICT_MARKER_BEGIN,
998 GOT_DIFF_CONFLICT_MARKER_SEP,
999 GOT_DIFF_CONFLICT_MARKER_END
1004 const char delim[3] = {'\0', '\0', '\0'};
1006 while (*status == GOT_STATUS_MODIFY) {
1007 line = fparseln(f, &len, NULL, delim, 0);
1011 err = got_ferror(f, GOT_ERR_IO);
1015 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1016 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1018 *status = GOT_STATUS_CONFLICT;
1028 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1030 return !(ie->ctime_sec == sb->st_ctime &&
1031 ie->ctime_nsec == sb->st_ctimensec &&
1032 ie->mtime_sec == sb->st_mtime &&
1033 ie->mtime_nsec == sb->st_mtimensec &&
1034 ie->size == (sb->st_size & 0xffffffff));
1037 static const struct got_error *
1038 get_file_status(unsigned char *status, struct stat *sb,
1039 struct got_fileindex_entry *ie, const char *abspath,
1040 struct got_repository *repo)
1042 const struct got_error *err = NULL;
1043 struct got_object_id id;
1047 struct got_blob_object *blob = NULL;
1050 *status = GOT_STATUS_NO_CHANGE;
1052 if (lstat(abspath, sb) == -1) {
1053 if (errno == ENOENT) {
1054 if (got_fileindex_entry_has_file_on_disk(ie))
1055 *status = GOT_STATUS_MISSING;
1057 *status = GOT_STATUS_DELETE;
1060 return got_error_from_errno2("lstat", abspath);
1063 if (!S_ISREG(sb->st_mode)) {
1064 *status = GOT_STATUS_OBSTRUCTED;
1068 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1069 *status = GOT_STATUS_DELETE;
1071 } else if (!got_fileindex_entry_has_blob(ie)) {
1072 *status = GOT_STATUS_ADD;
1076 if (!stat_info_differs(ie, sb))
1079 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1080 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1084 f = fopen(abspath, "r");
1086 err = got_error_from_errno2("fopen", abspath);
1089 hdrlen = got_object_blob_get_hdrlen(blob);
1091 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1092 err = got_object_blob_read_block(&blen, blob);
1095 /* Skip length of blob object header first time around. */
1096 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1097 if (flen == 0 && ferror(f)) {
1098 err = got_error_from_errno("fread");
1103 *status = GOT_STATUS_MODIFY;
1105 } else if (flen == 0) {
1107 *status = GOT_STATUS_MODIFY;
1109 } else if (blen - hdrlen == flen) {
1110 /* Skip blob object header first time around. */
1111 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1112 *status = GOT_STATUS_MODIFY;
1116 *status = GOT_STATUS_MODIFY;
1122 if (*status == GOT_STATUS_MODIFY) {
1124 err = get_modified_file_content_status(status, f);
1128 got_object_blob_close(blob);
1135 * Update timestamps in the file index if a file is unmodified and
1136 * we had to run a full content comparison to find out.
1138 static const struct got_error *
1139 sync_timestamps(char *ondisk_path, unsigned char status,
1140 struct got_fileindex_entry *ie, struct stat *sb)
1142 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1143 return got_fileindex_entry_update(ie, ondisk_path,
1144 ie->blob_sha1, ie->commit_sha1, 1);
1149 static const struct got_error *
1150 update_blob(struct got_worktree *worktree,
1151 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1152 struct got_tree_entry *te, const char *path,
1153 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1156 const struct got_error *err = NULL;
1157 struct got_blob_object *blob = NULL;
1159 unsigned char status = GOT_STATUS_NO_CHANGE;
1162 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1163 return got_error_from_errno("asprintf");
1166 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1169 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1170 sb.st_mode = got_fileindex_perms_to_st(ie);
1172 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1174 if (status == GOT_STATUS_OBSTRUCTED) {
1175 err = (*progress_cb)(progress_arg, status, path);
1179 if (ie && status != GOT_STATUS_MISSING) {
1180 if (got_fileindex_entry_has_commit(ie) &&
1181 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1182 SHA1_DIGEST_LENGTH) == 0) {
1183 err = sync_timestamps(ondisk_path, status, ie, &sb);
1186 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1190 if (got_fileindex_entry_has_blob(ie) &&
1191 memcmp(ie->blob_sha1, te->id->sha1,
1192 SHA1_DIGEST_LENGTH) == 0) {
1193 err = sync_timestamps(ondisk_path, status, ie, &sb);
1198 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1202 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1203 int update_timestamps;
1204 struct got_blob_object *blob2 = NULL;
1205 if (got_fileindex_entry_has_blob(ie)) {
1206 struct got_object_id id2;
1207 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1208 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1212 err = merge_blob(&update_timestamps, worktree, blob2,
1213 ondisk_path, path, sb.st_mode, blob,
1214 worktree->base_commit_id, repo,
1215 progress_cb, progress_arg);
1217 got_object_blob_close(blob2);
1219 * Do not update timestamps of files with local changes.
1220 * Otherwise, a future status walk would treat them as
1221 * unmodified files again.
1223 err = got_fileindex_entry_update(ie, ondisk_path,
1224 blob->id.sha1, worktree->base_commit_id->sha1,
1226 } else if (status == GOT_STATUS_DELETE) {
1227 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1230 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1231 ondisk_path, path, blob, 0);
1235 err = install_blob(worktree, ondisk_path, path, te->mode,
1236 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1237 repo, progress_cb, progress_arg);
1240 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1241 ondisk_path, path, blob, 1);
1245 got_object_blob_close(blob);
1251 static const struct got_error *
1252 remove_ondisk_file(const char *root_path, const char *path)
1254 const struct got_error *err = NULL;
1255 char *ondisk_path = NULL;
1257 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1258 return got_error_from_errno("asprintf");
1260 if (unlink(ondisk_path) == -1) {
1261 if (errno != ENOENT)
1262 err = got_error_from_errno2("unlink", ondisk_path);
1264 char *parent = dirname(ondisk_path);
1265 while (parent && strcmp(parent, root_path) != 0) {
1266 if (rmdir(parent) == -1) {
1267 if (errno != ENOTEMPTY)
1268 err = got_error_from_errno2("rmdir",
1272 parent = dirname(parent);
1279 static const struct got_error *
1280 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1281 struct got_fileindex_entry *ie, struct got_repository *repo,
1282 got_worktree_checkout_cb progress_cb, void *progress_arg)
1284 const struct got_error *err = NULL;
1285 unsigned char status;
1289 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1291 return got_error_from_errno("asprintf");
1293 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1297 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1298 status == GOT_STATUS_ADD) {
1299 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1303 * Preserve the working file and change the deleted blob's
1304 * entry into a schedule-add entry.
1306 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1311 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1314 if (status == GOT_STATUS_NO_CHANGE) {
1315 err = remove_ondisk_file(worktree->root_path, ie->path);
1319 got_fileindex_entry_remove(fileindex, ie);
1325 struct diff_cb_arg {
1326 struct got_fileindex *fileindex;
1327 struct got_worktree *worktree;
1328 struct got_repository *repo;
1329 got_worktree_checkout_cb progress_cb;
1331 got_worktree_cancel_cb cancel_cb;
1335 static const struct got_error *
1336 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1337 struct got_tree_entry *te, const char *parent_path)
1339 struct diff_cb_arg *a = arg;
1341 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1342 return got_error(GOT_ERR_CANCELLED);
1344 return update_blob(a->worktree, a->fileindex, ie, te,
1345 ie->path, a->repo, a->progress_cb, a->progress_arg);
1348 static const struct got_error *
1349 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1351 struct diff_cb_arg *a = arg;
1353 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1354 return got_error(GOT_ERR_CANCELLED);
1356 return delete_blob(a->worktree, a->fileindex, ie,
1357 a->repo, a->progress_cb, a->progress_arg);
1360 static const struct got_error *
1361 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1363 struct diff_cb_arg *a = arg;
1364 const struct got_error *err;
1367 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1368 return got_error(GOT_ERR_CANCELLED);
1370 if (asprintf(&path, "%s%s%s", parent_path,
1371 parent_path[0] ? "/" : "", te->name)
1373 return got_error_from_errno("asprintf");
1375 if (S_ISDIR(te->mode))
1376 err = add_dir_on_disk(a->worktree, path);
1378 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1379 a->repo, a->progress_cb, a->progress_arg);
1385 static const struct got_error *
1386 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1388 const struct got_error *err = NULL;
1389 char *uuidstr = NULL;
1390 uint32_t uuid_status;
1394 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1395 if (uuid_status != uuid_s_ok)
1396 return got_error_uuid(uuid_status);
1398 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1400 err = got_error_from_errno("asprintf");
1407 const struct got_error *
1408 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1410 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1413 static const struct got_error *
1414 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1416 return get_ref_name(refname, worktree,
1417 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1420 static const struct got_error *
1421 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1423 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1426 static const struct got_error *
1427 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1429 return get_ref_name(refname, worktree,
1430 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1433 static const struct got_error *
1434 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1436 return get_ref_name(refname, worktree,
1437 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1440 static const struct got_error *
1441 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1443 return get_ref_name(refname, worktree,
1444 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1447 static const struct got_error *
1448 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1450 return get_ref_name(refname, worktree,
1451 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1454 static const struct got_error *
1455 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1457 return get_ref_name(refname, worktree,
1458 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1461 static const struct got_error *
1462 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1464 return get_ref_name(refname, worktree,
1465 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1468 const struct got_error *
1469 got_worktree_get_histedit_script_path(char **path,
1470 struct got_worktree *worktree)
1472 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1473 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1475 return got_error_from_errno("asprintf");
1481 * Prevent Git's garbage collector from deleting our base commit by
1482 * setting a reference to our base commit's ID.
1484 static const struct got_error *
1485 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 struct got_reference *ref = NULL;
1491 err = got_worktree_get_base_ref_name(&refname, worktree);
1495 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1499 err = got_ref_write(ref, repo);
1507 static const struct got_error *
1508 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1510 const struct got_error *err = NULL;
1512 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1513 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1514 err = got_error_from_errno("asprintf");
1515 *fileindex_path = NULL;
1521 static const struct got_error *
1522 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1523 struct got_worktree *worktree)
1525 const struct got_error *err = NULL;
1528 *fileindex_path = NULL;
1529 *fileindex = got_fileindex_alloc();
1530 if (*fileindex == NULL)
1531 return got_error_from_errno("got_fileindex_alloc");
1533 err = get_fileindex_path(fileindex_path, worktree);
1537 index = fopen(*fileindex_path, "rb");
1538 if (index == NULL) {
1539 if (errno != ENOENT)
1540 err = got_error_from_errno2("fopen", *fileindex_path);
1542 err = got_fileindex_read(*fileindex, index);
1543 if (fclose(index) != 0 && err == NULL)
1544 err = got_error_from_errno("fclose");
1548 free(*fileindex_path);
1549 *fileindex_path = NULL;
1550 got_fileindex_free(*fileindex);
1556 struct bump_base_commit_id_arg {
1557 struct got_object_id *base_commit_id;
1560 const char *entry_name;
1561 got_worktree_checkout_cb progress_cb;
1565 /* Bump base commit ID of all files within an updated part of the work tree. */
1566 static const struct got_error *
1567 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1569 const struct got_error *err;
1570 struct bump_base_commit_id_arg *a = arg;
1572 if (a->entry_name) {
1573 if (strcmp(ie->path, a->path) != 0)
1575 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1578 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1579 SHA1_DIGEST_LENGTH) == 0)
1582 if (a->progress_cb) {
1583 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1588 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1592 static const struct got_error *
1593 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1595 const struct got_error *err = NULL;
1596 char *new_fileindex_path = NULL;
1597 FILE *new_index = NULL;
1599 err = got_opentemp_named(&new_fileindex_path, &new_index,
1604 err = got_fileindex_write(fileindex, new_index);
1608 if (rename(new_fileindex_path, fileindex_path) != 0) {
1609 err = got_error_from_errno3("rename", new_fileindex_path,
1611 unlink(new_fileindex_path);
1616 free(new_fileindex_path);
1620 static const struct got_error *
1621 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1622 struct got_object_id **tree_id, const char *wt_relpath,
1623 struct got_worktree *worktree, struct got_repository *repo)
1625 const struct got_error *err = NULL;
1626 struct got_object_id *id = NULL;
1627 char *in_repo_path = NULL;
1628 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1630 *entry_type = GOT_OBJ_TYPE_ANY;
1631 *tree_relpath = NULL;
1634 if (wt_relpath[0] == '\0') {
1635 /* Check out all files within the work tree. */
1636 *entry_type = GOT_OBJ_TYPE_TREE;
1637 *tree_relpath = strdup("");
1638 if (*tree_relpath == NULL) {
1639 err = got_error_from_errno("strdup");
1642 err = got_object_id_by_path(tree_id, repo,
1643 worktree->base_commit_id, worktree->path_prefix);
1649 /* Check out a subset of files in the work tree. */
1651 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1652 is_root_wt ? "" : "/", wt_relpath) == -1) {
1653 err = got_error_from_errno("asprintf");
1657 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1663 in_repo_path = NULL;
1665 err = got_object_get_type(entry_type, repo, id);
1669 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1670 /* Check out a single file. */
1671 if (strchr(wt_relpath, '/') == NULL) {
1672 /* Check out a single file in work tree's root dir. */
1673 in_repo_path = strdup(worktree->path_prefix);
1674 if (in_repo_path == NULL) {
1675 err = got_error_from_errno("strdup");
1678 *tree_relpath = strdup("");
1679 if (*tree_relpath == NULL) {
1680 err = got_error_from_errno("strdup");
1684 /* Check out a single file in a subdirectory. */
1685 err = got_path_dirname(tree_relpath, wt_relpath);
1688 if (asprintf(&in_repo_path, "%s%s%s",
1689 worktree->path_prefix, is_root_wt ? "" : "/",
1690 *tree_relpath) == -1) {
1691 err = got_error_from_errno("asprintf");
1695 err = got_object_id_by_path(tree_id, repo,
1696 worktree->base_commit_id, in_repo_path);
1698 /* Check out all files within a subdirectory. */
1699 *tree_id = got_object_id_dup(id);
1700 if (*tree_id == NULL) {
1701 err = got_error_from_errno("got_object_id_dup");
1704 *tree_relpath = strdup(wt_relpath);
1705 if (*tree_relpath == NULL) {
1706 err = got_error_from_errno("strdup");
1714 *entry_type = GOT_OBJ_TYPE_ANY;
1715 free(*tree_relpath);
1716 *tree_relpath = NULL;
1723 static const struct got_error *
1724 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1725 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1726 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1727 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1729 const struct got_error *err = NULL;
1730 struct got_commit_object *commit = NULL;
1731 struct got_tree_object *tree = NULL;
1732 struct got_fileindex_diff_tree_cb diff_cb;
1733 struct diff_cb_arg arg;
1735 err = ref_base_commit(worktree, repo);
1739 err = got_object_open_as_commit(&commit, repo,
1740 worktree->base_commit_id);
1744 err = got_object_open_as_tree(&tree, repo, tree_id);
1749 got_object_tree_find_entry(tree, entry_name) == NULL) {
1750 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1754 diff_cb.diff_old_new = diff_old_new;
1755 diff_cb.diff_old = diff_old;
1756 diff_cb.diff_new = diff_new;
1757 arg.fileindex = fileindex;
1758 arg.worktree = worktree;
1760 arg.progress_cb = progress_cb;
1761 arg.progress_arg = progress_arg;
1762 arg.cancel_cb = cancel_cb;
1763 arg.cancel_arg = cancel_arg;
1764 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1765 entry_name, repo, &diff_cb, &arg);
1768 got_object_tree_close(tree);
1770 got_object_commit_close(commit);
1774 const struct got_error *
1775 got_worktree_checkout_files(struct got_worktree *worktree,
1776 struct got_pathlist_head *paths, struct got_repository *repo,
1777 got_worktree_checkout_cb progress_cb, void *progress_arg,
1778 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1780 const struct got_error *err = NULL, *sync_err, *unlockerr;
1781 struct got_commit_object *commit = NULL;
1782 struct got_tree_object *tree = NULL;
1783 struct got_fileindex *fileindex = NULL;
1784 char *fileindex_path = NULL;
1785 struct got_pathlist_entry *pe;
1786 struct tree_path_data {
1787 SIMPLEQ_ENTRY(tree_path_data) entry;
1788 struct got_object_id *tree_id;
1793 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1795 SIMPLEQ_INIT(&tree_paths);
1797 err = lock_worktree(worktree, LOCK_EX);
1801 /* Map all specified paths to in-repository trees. */
1802 TAILQ_FOREACH(pe, paths, entry) {
1803 tpd = malloc(sizeof(*tpd));
1805 err = got_error_from_errno("malloc");
1809 err = find_tree_entry_for_checkout(&tpd->entry_type,
1810 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1816 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1817 err = got_path_basename(&tpd->entry_name, pe->path);
1825 tpd->entry_name = NULL;
1827 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1831 * Read the file index.
1832 * Checking out files is supposed to be an idempotent operation.
1833 * If the on-disk file index is incomplete we will try to complete it.
1835 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1839 tpd = SIMPLEQ_FIRST(&tree_paths);
1840 TAILQ_FOREACH(pe, paths, entry) {
1841 struct bump_base_commit_id_arg bbc_arg;
1843 err = checkout_files(worktree, fileindex, tpd->relpath,
1844 tpd->tree_id, tpd->entry_name, repo,
1845 progress_cb, progress_arg, cancel_cb, cancel_arg);
1849 bbc_arg.base_commit_id = worktree->base_commit_id;
1850 bbc_arg.entry_name = tpd->entry_name;
1851 bbc_arg.path = pe->path;
1852 bbc_arg.path_len = pe->path_len;
1853 bbc_arg.progress_cb = progress_cb;
1854 bbc_arg.progress_arg = progress_arg;
1855 err = got_fileindex_for_each_entry_safe(fileindex,
1856 bump_base_commit_id, &bbc_arg);
1860 tpd = SIMPLEQ_NEXT(tpd, entry);
1862 sync_err = sync_fileindex(fileindex, fileindex_path);
1863 if (sync_err && err == NULL)
1866 free(fileindex_path);
1868 got_object_tree_close(tree);
1870 got_object_commit_close(commit);
1872 got_fileindex_free(fileindex);
1873 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1874 tpd = SIMPLEQ_FIRST(&tree_paths);
1875 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1880 unlockerr = lock_worktree(worktree, LOCK_SH);
1881 if (unlockerr && err == NULL)
1886 struct merge_file_cb_arg {
1887 struct got_worktree *worktree;
1888 struct got_fileindex *fileindex;
1889 got_worktree_checkout_cb progress_cb;
1891 got_worktree_cancel_cb cancel_cb;
1893 struct got_object_id *commit_id2;
1896 static const struct got_error *
1897 merge_file_cb(void *arg, struct got_blob_object *blob1,
1898 struct got_blob_object *blob2, struct got_object_id *id1,
1899 struct got_object_id *id2, const char *path1, const char *path2,
1900 struct got_repository *repo)
1902 static const struct got_error *err = NULL;
1903 struct merge_file_cb_arg *a = arg;
1904 struct got_fileindex_entry *ie;
1905 char *ondisk_path = NULL;
1907 unsigned char status;
1908 int local_changes_subsumed;
1910 if (blob1 && blob2) {
1911 ie = got_fileindex_entry_get(a->fileindex, path2,
1914 return (*a->progress_cb)(a->progress_arg,
1915 GOT_STATUS_MISSING, path2);
1917 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1919 return got_error_from_errno("asprintf");
1921 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1925 if (status == GOT_STATUS_DELETE) {
1926 err = (*a->progress_cb)(a->progress_arg,
1927 GOT_STATUS_MERGE, path2);
1930 if (status != GOT_STATUS_NO_CHANGE &&
1931 status != GOT_STATUS_MODIFY &&
1932 status != GOT_STATUS_CONFLICT &&
1933 status != GOT_STATUS_ADD) {
1934 err = (*a->progress_cb)(a->progress_arg, status, path2);
1938 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1939 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1940 a->progress_cb, a->progress_arg);
1942 ie = got_fileindex_entry_get(a->fileindex, path1,
1945 return (*a->progress_cb)(a->progress_arg,
1946 GOT_STATUS_MISSING, path2);
1948 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1950 return got_error_from_errno("asprintf");
1952 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1957 case GOT_STATUS_NO_CHANGE:
1958 err = (*a->progress_cb)(a->progress_arg,
1959 GOT_STATUS_DELETE, path1);
1962 err = remove_ondisk_file(a->worktree->root_path, path1);
1966 got_fileindex_entry_mark_deleted_from_disk(ie);
1968 case GOT_STATUS_DELETE:
1969 case GOT_STATUS_MISSING:
1970 err = (*a->progress_cb)(a->progress_arg,
1971 GOT_STATUS_DELETE, path1);
1975 got_fileindex_entry_mark_deleted_from_disk(ie);
1977 case GOT_STATUS_ADD:
1978 case GOT_STATUS_MODIFY:
1979 case GOT_STATUS_CONFLICT:
1980 err = (*a->progress_cb)(a->progress_arg,
1981 GOT_STATUS_CANNOT_DELETE, path1);
1985 case GOT_STATUS_OBSTRUCTED:
1986 err = (*a->progress_cb)(a->progress_arg, status, path1);
1994 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1996 return got_error_from_errno("asprintf");
1997 ie = got_fileindex_entry_get(a->fileindex, path2,
2000 err = get_file_status(&status, &sb, ie, ondisk_path,
2004 if (status != GOT_STATUS_NO_CHANGE &&
2005 status != GOT_STATUS_MODIFY &&
2006 status != GOT_STATUS_CONFLICT &&
2007 status != GOT_STATUS_ADD) {
2008 err = (*a->progress_cb)(a->progress_arg,
2012 err = merge_blob(&local_changes_subsumed, a->worktree,
2013 NULL, ondisk_path, path2, sb.st_mode, blob2,
2014 a->commit_id2, repo,
2015 a->progress_cb, a->progress_arg);
2016 if (status == GOT_STATUS_DELETE) {
2017 err = update_blob_fileindex_entry(a->worktree,
2018 a->fileindex, ie, ondisk_path, ie->path,
2024 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2025 err = install_blob(a->worktree, ondisk_path, path2,
2026 /* XXX get this from parent tree! */
2027 GOT_DEFAULT_FILE_MODE,
2028 sb.st_mode, blob2, 0, 0, repo,
2029 a->progress_cb, a->progress_arg);
2032 err = got_fileindex_entry_alloc(&ie,
2033 ondisk_path, path2, NULL, NULL);
2036 err = got_fileindex_entry_add(a->fileindex, ie);
2038 got_fileindex_entry_free(ie);
2048 struct check_merge_ok_arg {
2049 struct got_worktree *worktree;
2050 struct got_repository *repo;
2053 static const struct got_error *
2054 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2056 const struct got_error *err = NULL;
2057 struct check_merge_ok_arg *a = arg;
2058 unsigned char status;
2062 /* Reject merges into a work tree with mixed base commits. */
2063 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2064 SHA1_DIGEST_LENGTH))
2065 return got_error(GOT_ERR_MIXED_COMMITS);
2067 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2069 return got_error_from_errno("asprintf");
2071 /* Reject merges into a work tree with conflicted files. */
2072 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2075 if (status == GOT_STATUS_CONFLICT)
2076 return got_error(GOT_ERR_CONFLICTS);
2081 static const struct got_error *
2082 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2083 const char *fileindex_path, struct got_object_id *commit_id1,
2084 struct got_object_id *commit_id2, struct got_repository *repo,
2085 got_worktree_checkout_cb progress_cb, void *progress_arg,
2086 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2088 const struct got_error *err = NULL, *sync_err;
2089 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2090 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2091 struct merge_file_cb_arg arg;
2094 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2095 worktree->path_prefix);
2099 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2104 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2105 worktree->path_prefix);
2109 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2113 arg.worktree = worktree;
2114 arg.fileindex = fileindex;
2115 arg.progress_cb = progress_cb;
2116 arg.progress_arg = progress_arg;
2117 arg.cancel_cb = cancel_cb;
2118 arg.cancel_arg = cancel_arg;
2119 arg.commit_id2 = commit_id2;
2120 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2121 sync_err = sync_fileindex(fileindex, fileindex_path);
2122 if (sync_err && err == NULL)
2126 got_object_tree_close(tree1);
2128 got_object_tree_close(tree2);
2132 const struct got_error *
2133 got_worktree_merge_files(struct got_worktree *worktree,
2134 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2135 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2136 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2138 const struct got_error *err, *unlockerr;
2139 char *fileindex_path = NULL;
2140 struct got_fileindex *fileindex = NULL;
2141 struct check_merge_ok_arg mok_arg;
2143 err = lock_worktree(worktree, LOCK_EX);
2147 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2151 mok_arg.worktree = worktree;
2152 mok_arg.repo = repo;
2153 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2158 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2159 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2162 got_fileindex_free(fileindex);
2163 free(fileindex_path);
2164 unlockerr = lock_worktree(worktree, LOCK_SH);
2165 if (unlockerr && err == NULL)
2170 struct diff_dir_cb_arg {
2171 struct got_fileindex *fileindex;
2172 struct got_worktree *worktree;
2173 const char *status_path;
2174 size_t status_path_len;
2175 struct got_repository *repo;
2176 got_worktree_status_cb status_cb;
2178 got_worktree_cancel_cb cancel_cb;
2182 static const struct got_error *
2183 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2184 got_worktree_status_cb status_cb, void *status_arg,
2185 struct got_repository *repo)
2187 const struct got_error *err = NULL;
2188 unsigned char status = GOT_STATUS_NO_CHANGE;
2190 struct got_object_id blob_id, commit_id;
2192 err = get_file_status(&status, &sb, ie, abspath, repo);
2193 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2194 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2195 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2196 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2202 static const struct got_error *
2203 status_old_new(void *arg, struct got_fileindex_entry *ie,
2204 struct dirent *de, const char *parent_path)
2206 const struct got_error *err = NULL;
2207 struct diff_dir_cb_arg *a = arg;
2210 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2211 return got_error(GOT_ERR_CANCELLED);
2213 if (got_path_cmp(parent_path, a->status_path,
2214 strlen(parent_path), a->status_path_len) != 0 &&
2215 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2218 if (parent_path[0]) {
2219 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2220 parent_path, de->d_name) == -1)
2221 return got_error_from_errno("asprintf");
2223 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2225 return got_error_from_errno("asprintf");
2228 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2234 static const struct got_error *
2235 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2237 struct diff_dir_cb_arg *a = arg;
2238 struct got_object_id blob_id, commit_id;
2239 unsigned char status;
2241 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 return got_error(GOT_ERR_CANCELLED);
2244 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2247 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2248 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2249 if (got_fileindex_entry_has_file_on_disk(ie))
2250 status = GOT_STATUS_MISSING;
2252 status = GOT_STATUS_DELETE;
2253 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2257 static const struct got_error *
2258 status_new(void *arg, struct dirent *de, const char *parent_path)
2260 const struct got_error *err = NULL;
2261 struct diff_dir_cb_arg *a = arg;
2264 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2265 return got_error(GOT_ERR_CANCELLED);
2267 if (de->d_type == DT_DIR)
2270 /* XXX ignore symlinks for now */
2271 if (de->d_type == DT_LNK)
2274 if (parent_path[0]) {
2275 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2276 return got_error_from_errno("asprintf");
2281 if (got_path_is_child(path, a->status_path, a->status_path_len))
2282 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2289 static const struct got_error *
2290 report_single_file_status(const char *path, const char *ondisk_path,
2291 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2292 void *status_arg, struct got_repository *repo)
2294 struct got_fileindex_entry *ie;
2297 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2299 return report_file_status(ie, ondisk_path, status_cb,
2302 if (lstat(ondisk_path, &sb) == -1) {
2303 if (errno != ENOENT)
2304 return got_error_from_errno2("lstat", ondisk_path);
2308 if (S_ISREG(sb.st_mode))
2309 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED, path,
2315 static const struct got_error *
2316 worktree_status(struct got_worktree *worktree, const char *path,
2317 struct got_fileindex *fileindex, struct got_repository *repo,
2318 got_worktree_status_cb status_cb, void *status_arg,
2319 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2321 const struct got_error *err = NULL;
2322 DIR *workdir = NULL;
2323 struct got_fileindex_diff_dir_cb fdiff_cb;
2324 struct diff_dir_cb_arg arg;
2325 char *ondisk_path = NULL;
2327 if (asprintf(&ondisk_path, "%s%s%s",
2328 worktree->root_path, path[0] ? "/" : "", path) == -1)
2329 return got_error_from_errno("asprintf");
2331 workdir = opendir(ondisk_path);
2332 if (workdir == NULL) {
2333 if (errno != ENOTDIR && errno != ENOENT)
2334 err = got_error_from_errno2("opendir", ondisk_path);
2336 err = report_single_file_status(path, ondisk_path,
2337 fileindex, status_cb, status_arg, repo);
2339 fdiff_cb.diff_old_new = status_old_new;
2340 fdiff_cb.diff_old = status_old;
2341 fdiff_cb.diff_new = status_new;
2342 arg.fileindex = fileindex;
2343 arg.worktree = worktree;
2344 arg.status_path = path;
2345 arg.status_path_len = strlen(path);
2347 arg.status_cb = status_cb;
2348 arg.status_arg = status_arg;
2349 arg.cancel_cb = cancel_cb;
2350 arg.cancel_arg = cancel_arg;
2351 err = got_fileindex_diff_dir(fileindex, workdir,
2352 worktree->root_path, path, repo, &fdiff_cb, &arg);
2361 const struct got_error *
2362 got_worktree_status(struct got_worktree *worktree,
2363 struct got_pathlist_head *paths, struct got_repository *repo,
2364 got_worktree_status_cb status_cb, void *status_arg,
2365 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2367 const struct got_error *err = NULL;
2368 char *fileindex_path = NULL;
2369 struct got_fileindex *fileindex = NULL;
2370 struct got_pathlist_entry *pe;
2372 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2376 TAILQ_FOREACH(pe, paths, entry) {
2377 err = worktree_status(worktree, pe->path, fileindex, repo,
2378 status_cb, status_arg, cancel_cb, cancel_arg);
2382 free(fileindex_path);
2383 got_fileindex_free(fileindex);
2387 const struct got_error *
2388 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2391 const struct got_error *err = NULL;
2392 char *resolved, *cwd = NULL, *path = NULL;
2397 resolved = realpath(arg, NULL);
2398 if (resolved == NULL) {
2399 if (errno != ENOENT)
2400 return got_error_from_errno2("realpath", arg);
2401 cwd = getcwd(NULL, 0);
2403 return got_error_from_errno("getcwd");
2404 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2405 err = got_error_from_errno("asprintf");
2410 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2411 strlen(got_worktree_get_root_path(worktree)))) {
2412 err = got_error(GOT_ERR_BAD_PATH);
2416 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2417 err = got_path_skip_common_ancestor(&path,
2418 got_worktree_get_root_path(worktree), resolved);
2424 err = got_error_from_errno("strdup");
2429 /* XXX status walk can't deal with trailing slash! */
2431 while (len > 0 && path[len - 1] == '/') {
2432 path[len - 1] = '\0';
2445 static const struct got_error *
2446 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2447 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2448 struct got_repository *repo)
2450 const struct got_error *err = NULL;
2451 struct got_fileindex_entry *ie;
2453 /* Re-adding an existing entry is a no-op. */
2454 if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
2457 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2461 err = got_fileindex_entry_add(fileindex, ie);
2463 got_fileindex_entry_free(ie);
2467 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2470 const struct got_error *
2471 got_worktree_schedule_add(struct got_worktree *worktree,
2472 struct got_pathlist_head *ondisk_paths,
2473 got_worktree_status_cb status_cb, void *status_arg,
2474 struct got_repository *repo)
2476 struct got_fileindex *fileindex = NULL;
2477 char *fileindex_path = NULL;
2478 const struct got_error *err = NULL, *sync_err, *unlockerr;
2479 struct got_pathlist_entry *pe;
2481 err = lock_worktree(worktree, LOCK_EX);
2485 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2489 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2491 err = got_path_skip_common_ancestor(&relpath,
2492 got_worktree_get_root_path(worktree), pe->path);
2495 err = schedule_addition(pe->path, fileindex, relpath,
2496 status_cb, status_arg, repo);
2501 sync_err = sync_fileindex(fileindex, fileindex_path);
2502 if (sync_err && err == NULL)
2505 free(fileindex_path);
2507 got_fileindex_free(fileindex);
2508 unlockerr = lock_worktree(worktree, LOCK_SH);
2509 if (unlockerr && err == NULL)
2514 static const struct got_error *
2515 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2516 const char *relpath, int delete_local_mods,
2517 got_worktree_status_cb status_cb, void *status_arg,
2518 struct got_repository *repo)
2520 const struct got_error *err = NULL;
2521 struct got_fileindex_entry *ie = NULL;
2522 unsigned char status;
2525 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2527 return got_error(GOT_ERR_BAD_PATH);
2529 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2533 if (status != GOT_STATUS_NO_CHANGE) {
2534 if (status == GOT_STATUS_DELETE)
2536 if (status != GOT_STATUS_MODIFY)
2537 return got_error(GOT_ERR_FILE_STATUS);
2538 if (!delete_local_mods)
2539 return got_error(GOT_ERR_FILE_MODIFIED);
2542 if (unlink(ondisk_path) != 0)
2543 return got_error_from_errno2("unlink", ondisk_path);
2545 got_fileindex_entry_mark_deleted_from_disk(ie);
2546 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2549 const struct got_error *
2550 got_worktree_schedule_delete(struct got_worktree *worktree,
2551 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2552 got_worktree_status_cb status_cb, void *status_arg,
2553 struct got_repository *repo)
2555 struct got_fileindex *fileindex = NULL;
2556 char *fileindex_path = NULL;
2557 const struct got_error *err = NULL, *sync_err, *unlockerr;
2558 struct got_pathlist_entry *pe;
2560 err = lock_worktree(worktree, LOCK_EX);
2564 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2568 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2570 err = got_path_skip_common_ancestor(&relpath,
2571 got_worktree_get_root_path(worktree), pe->path);
2574 err = schedule_for_deletion(pe->path, fileindex, relpath,
2575 delete_local_mods, status_cb, status_arg, repo);
2580 sync_err = sync_fileindex(fileindex, fileindex_path);
2581 if (sync_err && err == NULL)
2584 free(fileindex_path);
2586 got_fileindex_free(fileindex);
2587 unlockerr = lock_worktree(worktree, LOCK_SH);
2588 if (unlockerr && err == NULL)
2593 static const struct got_error *
2594 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2595 const char *ondisk_path,
2596 got_worktree_checkout_cb progress_cb, void *progress_arg,
2597 struct got_repository *repo)
2599 const struct got_error *err = NULL;
2600 char *relpath = NULL, *parent_path = NULL;
2601 struct got_fileindex_entry *ie;
2602 struct got_tree_object *tree = NULL;
2603 struct got_object_id *tree_id = NULL;
2604 const struct got_tree_entry *te;
2605 char *tree_path = NULL, *te_name;
2606 struct got_blob_object *blob = NULL;
2607 unsigned char status;
2610 err = got_path_skip_common_ancestor(&relpath,
2611 got_worktree_get_root_path(worktree), ondisk_path);
2615 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2617 err = got_error(GOT_ERR_BAD_PATH);
2621 /* Construct in-repository path of tree which contains this blob. */
2622 err = got_path_dirname(&parent_path, ie->path);
2624 if (err->code != GOT_ERR_BAD_PATH)
2626 parent_path = strdup("/");
2627 if (parent_path == NULL) {
2628 err = got_error_from_errno("strdup");
2632 if (got_path_is_root_dir(worktree->path_prefix)) {
2633 tree_path = strdup(parent_path);
2634 if (tree_path == NULL) {
2635 err = got_error_from_errno("strdup");
2639 if (got_path_is_root_dir(parent_path)) {
2640 tree_path = strdup(worktree->path_prefix);
2641 if (tree_path == NULL) {
2642 err = got_error_from_errno("strdup");
2646 if (asprintf(&tree_path, "%s/%s",
2647 worktree->path_prefix, parent_path) == -1) {
2648 err = got_error_from_errno("asprintf");
2654 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2657 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2658 sb.st_mode = got_fileindex_perms_to_st(ie);
2660 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2663 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2664 status == GOT_STATUS_ADD))
2667 err = got_object_open_as_tree(&tree, repo, tree_id);
2671 te_name = basename(ie->path);
2672 if (te_name == NULL) {
2673 err = got_error_from_errno2("basename", ie->path);
2677 te = got_object_tree_find_entry(tree, te_name);
2678 if (te == NULL && status != GOT_STATUS_ADD) {
2679 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2685 case GOT_STATUS_ADD:
2686 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2689 got_fileindex_entry_remove(fileindex, ie);
2691 case GOT_STATUS_DELETE:
2692 case GOT_STATUS_MODIFY:
2693 case GOT_STATUS_CONFLICT:
2694 case GOT_STATUS_MISSING: {
2695 struct got_object_id id;
2696 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2697 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2700 err = install_blob(worktree, ondisk_path, ie->path,
2701 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2705 if (status == GOT_STATUS_DELETE) {
2706 err = update_blob_fileindex_entry(worktree,
2707 fileindex, ie, ondisk_path, ie->path, blob, 1);
2721 got_object_blob_close(blob);
2723 got_object_tree_close(tree);
2728 const struct got_error *
2729 got_worktree_revert(struct got_worktree *worktree,
2730 struct got_pathlist_head *ondisk_paths,
2731 got_worktree_checkout_cb progress_cb, void *progress_arg,
2732 struct got_repository *repo)
2734 struct got_fileindex *fileindex = NULL;
2735 char *fileindex_path = NULL;
2736 const struct got_error *err = NULL, *unlockerr = NULL;
2737 const struct got_error *sync_err = NULL;
2738 struct got_pathlist_entry *pe;
2740 err = lock_worktree(worktree, LOCK_EX);
2744 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2748 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2749 err = revert_file(worktree, fileindex, pe->path,
2750 progress_cb, progress_arg, repo);
2754 sync_err = sync_fileindex(fileindex, fileindex_path);
2755 if (sync_err && err == NULL)
2758 free(fileindex_path);
2760 got_fileindex_free(fileindex);
2761 unlockerr = lock_worktree(worktree, LOCK_SH);
2762 if (unlockerr && err == NULL)
2768 free_commitable(struct got_commitable *ct)
2771 free(ct->in_repo_path);
2772 free(ct->ondisk_path);
2774 free(ct->base_blob_id);
2775 free(ct->base_commit_id);
2779 struct collect_commitables_arg {
2780 struct got_pathlist_head *commitable_paths;
2781 struct got_repository *repo;
2782 struct got_worktree *worktree;
2785 static const struct got_error *
2786 collect_commitables(void *arg, unsigned char status, const char *relpath,
2787 struct got_object_id *blob_id, struct got_object_id *commit_id)
2789 struct collect_commitables_arg *a = arg;
2790 const struct got_error *err = NULL;
2791 struct got_commitable *ct = NULL;
2792 struct got_pathlist_entry *new = NULL;
2793 char *parent_path = NULL, *path = NULL;
2796 if (status == GOT_STATUS_CONFLICT)
2797 return got_error(GOT_ERR_COMMIT_CONFLICT);
2799 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2800 status != GOT_STATUS_DELETE)
2803 if (asprintf(&path, "/%s", relpath) == -1) {
2804 err = got_error_from_errno("asprintf");
2807 if (strcmp(path, "/") == 0) {
2808 parent_path = strdup("");
2809 if (parent_path == NULL)
2810 return got_error_from_errno("strdup");
2812 err = got_path_dirname(&parent_path, path);
2817 ct = calloc(1, sizeof(*ct));
2819 err = got_error_from_errno("calloc");
2823 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2825 err = got_error_from_errno("asprintf");
2828 if (status == GOT_STATUS_DELETE) {
2829 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2831 if (lstat(ct->ondisk_path, &sb) != 0) {
2832 err = got_error_from_errno2("lstat", ct->ondisk_path);
2835 ct->mode = sb.st_mode;
2838 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2839 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2841 err = got_error_from_errno("asprintf");
2845 ct->status = status;
2846 ct->blob_id = NULL; /* will be filled in when blob gets created */
2847 if (ct->status != GOT_STATUS_ADD) {
2848 ct->base_blob_id = got_object_id_dup(blob_id);
2849 if (ct->base_blob_id == NULL) {
2850 err = got_error_from_errno("got_object_id_dup");
2853 ct->base_commit_id = got_object_id_dup(commit_id);
2854 if (ct->base_commit_id == NULL) {
2855 err = got_error_from_errno("got_object_id_dup");
2859 ct->path = strdup(path);
2860 if (ct->path == NULL) {
2861 err = got_error_from_errno("strdup");
2864 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2866 if (ct && (err || new == NULL))
2867 free_commitable(ct);
2873 static const struct got_error *write_tree(struct got_object_id **,
2874 struct got_tree_object *, const char *, struct got_pathlist_head *,
2875 got_worktree_status_cb status_cb, void *status_arg,
2876 struct got_repository *);
2878 static const struct got_error *
2879 write_subtree(struct got_object_id **new_subtree_id,
2880 struct got_tree_entry *te, const char *parent_path,
2881 struct got_pathlist_head *commitable_paths,
2882 got_worktree_status_cb status_cb, void *status_arg,
2883 struct got_repository *repo)
2885 const struct got_error *err = NULL;
2886 struct got_tree_object *subtree;
2889 if (asprintf(&subpath, "%s%s%s", parent_path,
2890 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2891 return got_error_from_errno("asprintf");
2893 err = got_object_open_as_tree(&subtree, repo, te->id);
2897 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2898 status_cb, status_arg, repo);
2899 got_object_tree_close(subtree);
2904 static const struct got_error *
2905 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2907 const struct got_error *err = NULL;
2908 char *ct_parent_path = NULL;
2912 if (strchr(ct->in_repo_path, '/') == NULL) {
2913 *match = got_path_is_root_dir(path);
2917 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2920 *match = (strcmp(path, ct_parent_path) == 0);
2921 free(ct_parent_path);
2926 get_ct_file_mode(struct got_commitable *ct)
2928 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2931 static const struct got_error *
2932 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2933 struct got_tree_entry *te, struct got_commitable *ct)
2935 const struct got_error *err = NULL;
2939 err = got_object_tree_entry_dup(new_te, te);
2943 (*new_te)->mode = get_ct_file_mode(ct);
2945 free((*new_te)->id);
2946 (*new_te)->id = got_object_id_dup(ct->blob_id);
2947 if ((*new_te)->id == NULL) {
2948 err = got_error_from_errno("got_object_id_dup");
2952 if (err && *new_te) {
2953 got_object_tree_entry_close(*new_te);
2959 static const struct got_error *
2960 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2961 struct got_commitable *ct)
2963 const struct got_error *err = NULL;
2968 *new_te = calloc(1, sizeof(**new_te));
2969 if (*new_te == NULL)
2970 return got_error_from_errno("calloc");
2972 ct_name = basename(ct->path);
2973 if (ct_name == NULL) {
2974 err = got_error_from_errno2("basename", ct->path);
2977 (*new_te)->name = strdup(ct_name);
2978 if ((*new_te)->name == NULL) {
2979 err = got_error_from_errno("strdup");
2983 (*new_te)->mode = get_ct_file_mode(ct);
2985 (*new_te)->id = got_object_id_dup(ct->blob_id);
2986 if ((*new_te)->id == NULL) {
2987 err = got_error_from_errno("got_object_id_dup");
2991 if (err && *new_te) {
2992 got_object_tree_entry_close(*new_te);
2998 static const struct got_error *
2999 insert_tree_entry(struct got_tree_entry *new_te,
3000 struct got_pathlist_head *paths)
3002 const struct got_error *err = NULL;
3003 struct got_pathlist_entry *new_pe;
3005 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3009 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3013 static const struct got_error *
3014 report_ct_status(struct got_commitable *ct,
3015 got_worktree_status_cb status_cb, void *status_arg)
3017 const char *ct_path = ct->path;
3018 while (ct_path[0] == '/')
3020 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
3023 static const struct got_error *
3024 match_modified_subtree(int *modified, struct got_tree_entry *te,
3025 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3027 const struct got_error *err = NULL;
3028 struct got_pathlist_entry *pe;
3033 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3034 got_path_is_root_dir(base_tree_path) ? "" : "/",
3036 return got_error_from_errno("asprintf");
3038 TAILQ_FOREACH(pe, commitable_paths, entry) {
3039 struct got_commitable *ct = pe->data;
3040 *modified = got_path_is_child(ct->in_repo_path, te_path,
3050 static const struct got_error *
3051 match_deleted_or_modified_ct(struct got_commitable **ctp,
3052 struct got_tree_entry *te, const char *base_tree_path,
3053 struct got_pathlist_head *commitable_paths)
3055 const struct got_error *err = NULL;
3056 struct got_pathlist_entry *pe;
3060 TAILQ_FOREACH(pe, commitable_paths, entry) {
3061 struct got_commitable *ct = pe->data;
3062 char *ct_name = NULL;
3065 if (ct->status != GOT_STATUS_MODIFY &&
3066 ct->status != GOT_STATUS_DELETE)
3069 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3072 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3078 ct_name = basename(pe->path);
3079 if (ct_name == NULL)
3080 return got_error_from_errno2("basename", pe->path);
3082 if (strcmp(te->name, ct_name) != 0)
3092 static const struct got_error *
3093 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3094 const char *child_path, const char *path_base_tree,
3095 struct got_pathlist_head *commitable_paths,
3096 got_worktree_status_cb status_cb, void *status_arg,
3097 struct got_repository *repo)
3099 const struct got_error *err = NULL;
3100 struct got_tree_entry *new_te;
3105 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3106 got_path_is_root_dir(path_base_tree) ? "" : "/",
3108 return got_error_from_errno("asprintf");
3110 new_te = calloc(1, sizeof(*new_te));
3111 new_te->mode = S_IFDIR;
3112 new_te->name = strdup(child_path);
3113 if (new_te->name == NULL) {
3114 err = got_error_from_errno("strdup");
3115 got_object_tree_entry_close(new_te);
3118 err = write_tree(&new_te->id, NULL, subtree_path,
3119 commitable_paths, status_cb, status_arg, repo);
3121 got_object_tree_entry_close(new_te);
3131 static const struct got_error *
3132 write_tree(struct got_object_id **new_tree_id,
3133 struct got_tree_object *base_tree, const char *path_base_tree,
3134 struct got_pathlist_head *commitable_paths,
3135 got_worktree_status_cb status_cb, void *status_arg,
3136 struct got_repository *repo)
3138 const struct got_error *err = NULL;
3139 const struct got_tree_entries *base_entries = NULL;
3140 struct got_pathlist_head paths;
3141 struct got_tree_entries new_tree_entries;
3142 struct got_tree_entry *te, *new_te = NULL;
3143 struct got_pathlist_entry *pe;
3146 new_tree_entries.nentries = 0;
3147 SIMPLEQ_INIT(&new_tree_entries.head);
3149 /* Insert, and recurse into, newly added entries first. */
3150 TAILQ_FOREACH(pe, commitable_paths, entry) {
3151 struct got_commitable *ct = pe->data;
3152 char *child_path = NULL, *slash;
3154 if (ct->status != GOT_STATUS_ADD ||
3155 (ct->flags & GOT_COMMITABLE_ADDED))
3158 if (!got_path_is_child(pe->path, path_base_tree,
3159 strlen(path_base_tree)))
3162 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3167 slash = strchr(child_path, '/');
3168 if (slash == NULL) {
3169 err = alloc_added_blob_tree_entry(&new_te, ct);
3172 err = report_ct_status(ct, status_cb, status_arg);
3175 ct->flags |= GOT_COMMITABLE_ADDED;
3176 err = insert_tree_entry(new_te, &paths);
3180 *slash = '\0'; /* trim trailing path components */
3181 if (base_tree == NULL ||
3182 got_object_tree_find_entry(base_tree, child_path)
3184 err = make_subtree_for_added_blob(&new_te,
3185 child_path, path_base_tree,
3186 commitable_paths, status_cb, status_arg,
3190 err = insert_tree_entry(new_te, &paths);
3198 /* Handle modified and deleted entries. */
3199 base_entries = got_object_tree_get_entries(base_tree);
3200 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3201 struct got_commitable *ct = NULL;
3203 if (S_ISDIR(te->mode)) {
3205 err = got_object_tree_entry_dup(&new_te, te);
3208 err = match_modified_subtree(&modified, te,
3209 path_base_tree, commitable_paths);
3212 /* Avoid recursion into unmodified subtrees. */
3215 err = write_subtree(&new_te->id, te,
3216 path_base_tree, commitable_paths,
3217 status_cb, status_arg, repo);
3221 err = insert_tree_entry(new_te, &paths);
3227 err = match_deleted_or_modified_ct(&ct, te,
3228 path_base_tree, commitable_paths);
3230 /* NB: Deleted entries get dropped here. */
3231 if (ct->status == GOT_STATUS_MODIFY) {
3232 err = alloc_modified_blob_tree_entry(
3236 err = insert_tree_entry(new_te, &paths);
3240 err = report_ct_status(ct, status_cb,
3245 /* Entry is unchanged; just copy it. */
3246 err = got_object_tree_entry_dup(&new_te, te);
3249 err = insert_tree_entry(new_te, &paths);
3256 /* Write new list of entries; deleted entries have been dropped. */
3257 TAILQ_FOREACH(pe, &paths, entry) {
3258 struct got_tree_entry *te = pe->data;
3259 new_tree_entries.nentries++;
3260 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3262 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3264 got_object_tree_entries_close(&new_tree_entries);
3265 got_pathlist_free(&paths);
3269 static const struct got_error *
3270 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3271 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3273 const struct got_error *err = NULL;
3274 struct got_pathlist_entry *pe;
3276 TAILQ_FOREACH(pe, commitable_paths, entry) {
3277 struct got_fileindex_entry *ie;
3278 struct got_commitable *ct = pe->data;
3280 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3282 if (ct->status == GOT_STATUS_DELETE) {
3283 got_fileindex_entry_remove(fileindex, ie);
3284 got_fileindex_entry_free(ie);
3286 err = got_fileindex_entry_update(ie,
3287 ct->ondisk_path, ct->blob_id->sha1,
3288 new_base_commit_id->sha1, 1);
3290 err = got_fileindex_entry_alloc(&ie,
3291 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3292 new_base_commit_id->sha1);
3295 err = got_fileindex_entry_add(fileindex, ie);
3303 static const struct got_error *
3304 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3305 struct got_object_id *head_commit_id)
3307 const struct got_error *err = NULL;
3308 struct got_object_id *id = NULL;
3309 struct got_commit_object *commit = NULL;
3310 const char *ct_path = ct->in_repo_path;
3312 while (ct_path[0] == '/')
3315 if (ct->status != GOT_STATUS_ADD) {
3316 /* Trivial case: base commit == head commit */
3317 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3320 * Ensure file content which local changes were based
3321 * on matches file content in the branch head.
3323 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3325 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3327 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3329 } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3330 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3332 /* Require that added files don't exist in the branch head. */
3333 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3334 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3336 err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3340 got_object_commit_close(commit);
3345 const struct got_error *
3346 commit_worktree(struct got_object_id **new_commit_id,
3347 struct got_pathlist_head *commitable_paths,
3348 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3349 const char *author, const char *committer,
3350 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3351 got_worktree_status_cb status_cb, void *status_arg,
3352 struct got_repository *repo)
3354 const struct got_error *err = NULL, *unlockerr = NULL;
3355 struct got_pathlist_entry *pe;
3356 const char *head_ref_name = NULL;
3357 struct got_commit_object *head_commit = NULL;
3358 struct got_reference *head_ref2 = NULL;
3359 struct got_object_id *head_commit_id2 = NULL;
3360 struct got_tree_object *head_tree = NULL;
3361 struct got_object_id *new_tree_id = NULL;
3362 struct got_object_id_queue parent_ids;
3363 struct got_object_qid *pid = NULL;
3364 char *logmsg = NULL;
3366 *new_commit_id = NULL;
3368 SIMPLEQ_INIT(&parent_ids);
3370 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3374 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3378 if (commit_msg_cb != NULL) {
3379 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3384 if (logmsg == NULL || strlen(logmsg) == 0) {
3385 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3389 /* Create blobs from added and modified files and record their IDs. */
3390 TAILQ_FOREACH(pe, commitable_paths, entry) {
3391 struct got_commitable *ct = pe->data;
3394 if (ct->status != GOT_STATUS_ADD &&
3395 ct->status != GOT_STATUS_MODIFY)
3398 if (asprintf(&ondisk_path, "%s/%s",
3399 worktree->root_path, pe->path) == -1) {
3400 err = got_error_from_errno("asprintf");
3403 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3409 /* Recursively write new tree objects. */
3410 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3411 status_cb, status_arg, repo);
3415 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3418 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3419 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3420 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3421 got_object_qid_free(pid);
3427 /* Check if a concurrent commit to our branch has occurred. */
3428 head_ref_name = got_worktree_get_head_ref_name(worktree);
3429 if (head_ref_name == NULL) {
3430 err = got_error_from_errno("got_worktree_get_head_ref_name");
3433 /* Lock the reference here to prevent concurrent modification. */
3434 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3437 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3440 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3441 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3444 /* Update branch head in repository. */
3445 err = got_ref_change_ref(head_ref2, *new_commit_id);
3448 err = got_ref_write(head_ref2, repo);
3452 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3456 err = ref_base_commit(worktree, repo);
3461 got_object_tree_close(head_tree);
3463 got_object_commit_close(head_commit);
3464 free(head_commit_id2);
3466 unlockerr = got_ref_unlock(head_ref2);
3467 if (unlockerr && err == NULL)
3469 got_ref_close(head_ref2);
3474 static const struct got_error *
3475 check_path_is_commitable(const char *path,
3476 struct got_pathlist_head *commitable_paths)
3478 struct got_pathlist_entry *cpe = NULL;
3479 size_t path_len = strlen(path);
3481 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3482 struct got_commitable *ct = cpe->data;
3483 const char *ct_path = ct->path;
3485 while (ct_path[0] == '/')
3488 if (strcmp(path, ct_path) == 0 ||
3489 got_path_is_child(ct_path, path, path_len))
3494 return got_error_path(path, GOT_ERR_BAD_PATH);
3499 const struct got_error *
3500 got_worktree_commit(struct got_object_id **new_commit_id,
3501 struct got_worktree *worktree, struct got_pathlist_head *paths,
3502 const char *author, const char *committer,
3503 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3504 got_worktree_status_cb status_cb, void *status_arg,
3505 struct got_repository *repo)
3507 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3508 struct got_fileindex *fileindex = NULL;
3509 char *fileindex_path = NULL;
3510 struct got_pathlist_head commitable_paths;
3511 struct collect_commitables_arg cc_arg;
3512 struct got_pathlist_entry *pe;
3513 struct got_reference *head_ref = NULL;
3514 struct got_object_id *head_commit_id = NULL;
3516 *new_commit_id = NULL;
3518 TAILQ_INIT(&commitable_paths);
3520 err = lock_worktree(worktree, LOCK_EX);
3524 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3528 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3532 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3536 cc_arg.commitable_paths = &commitable_paths;
3537 cc_arg.worktree = worktree;
3539 TAILQ_FOREACH(pe, paths, entry) {
3540 err = worktree_status(worktree, pe->path, fileindex, repo,
3541 collect_commitables, &cc_arg, NULL, NULL);
3546 if (TAILQ_EMPTY(&commitable_paths)) {
3547 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3551 TAILQ_FOREACH(pe, paths, entry) {
3552 err = check_path_is_commitable(pe->path, &commitable_paths);
3557 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3558 struct got_commitable *ct = pe->data;
3559 err = check_ct_out_of_date(ct, repo, head_commit_id);
3564 err = commit_worktree(new_commit_id, &commitable_paths,
3565 head_commit_id, worktree, author, committer,
3566 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3570 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3572 sync_err = sync_fileindex(fileindex, fileindex_path);
3573 if (sync_err && err == NULL)
3577 got_fileindex_free(fileindex);
3578 free(fileindex_path);
3579 unlockerr = lock_worktree(worktree, LOCK_SH);
3580 if (unlockerr && err == NULL)
3582 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3583 struct got_commitable *ct = pe->data;
3584 free_commitable(ct);
3586 got_pathlist_free(&commitable_paths);
3591 got_commitable_get_path(struct got_commitable *ct)
3597 got_commitable_get_status(struct got_commitable *ct)
3602 struct check_rebase_ok_arg {
3603 struct got_worktree *worktree;
3604 struct got_repository *repo;
3607 static const struct got_error *
3608 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3610 const struct got_error *err = NULL;
3611 struct check_rebase_ok_arg *a = arg;
3612 unsigned char status;
3616 /* Reject rebase of a work tree with mixed base commits. */
3617 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3618 SHA1_DIGEST_LENGTH))
3619 return got_error(GOT_ERR_MIXED_COMMITS);
3621 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3623 return got_error_from_errno("asprintf");
3625 /* Reject rebase of a work tree with modified or conflicted files. */
3626 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3631 if (status != GOT_STATUS_NO_CHANGE)
3632 return got_error(GOT_ERR_MODIFIED);
3637 const struct got_error *
3638 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3639 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3640 struct got_worktree *worktree, struct got_reference *branch,
3641 struct got_repository *repo)
3643 const struct got_error *err = NULL;
3644 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3645 char *branch_ref_name = NULL;
3646 char *fileindex_path = NULL;
3647 struct check_rebase_ok_arg ok_arg;
3648 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3650 *new_base_branch_ref = NULL;
3654 err = lock_worktree(worktree, LOCK_EX);
3658 err = open_fileindex(fileindex, &fileindex_path, worktree);
3662 ok_arg.worktree = worktree;
3664 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3669 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3673 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3677 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3681 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3686 err = got_ref_alloc_symref(new_base_branch_ref,
3687 new_base_branch_ref_name, wt_branch);
3690 err = got_ref_write(*new_base_branch_ref, repo);
3694 /* TODO Lock original branch's ref while rebasing? */
3696 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3700 err = got_ref_write(branch_ref, repo);
3704 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3705 worktree->base_commit_id);
3708 err = got_ref_write(*tmp_branch, repo);
3712 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3716 free(fileindex_path);
3717 free(tmp_branch_name);
3718 free(new_base_branch_ref_name);
3719 free(branch_ref_name);
3721 got_ref_close(branch_ref);
3723 got_ref_close(wt_branch);
3725 if (*new_base_branch_ref) {
3726 got_ref_close(*new_base_branch_ref);
3727 *new_base_branch_ref = NULL;
3730 got_ref_close(*tmp_branch);
3734 got_fileindex_free(*fileindex);
3737 lock_worktree(worktree, LOCK_SH);
3742 const struct got_error *
3743 got_worktree_rebase_continue(struct got_object_id **commit_id,
3744 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3745 struct got_reference **branch, struct got_fileindex **fileindex,
3746 struct got_worktree *worktree, struct got_repository *repo)
3748 const struct got_error *err;
3749 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3750 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3751 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3752 char *fileindex_path = NULL;
3755 *new_base_branch = NULL;
3760 err = lock_worktree(worktree, LOCK_EX);
3764 err = open_fileindex(fileindex, &fileindex_path, worktree);
3768 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3772 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3776 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3780 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3784 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3788 err = got_ref_open(branch, repo,
3789 got_ref_get_symref_target(branch_ref), 0);
3793 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3797 err = got_ref_resolve(commit_id, repo, commit_ref);
3801 err = got_ref_open(new_base_branch, repo,
3802 new_base_branch_ref_name, 0);
3806 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3810 free(commit_ref_name);
3811 free(branch_ref_name);
3812 free(fileindex_path);
3814 got_ref_close(commit_ref);
3816 got_ref_close(branch_ref);
3821 got_ref_close(*tmp_branch);
3824 if (*new_base_branch) {
3825 got_ref_close(*new_base_branch);
3826 *new_base_branch = NULL;
3829 got_ref_close(*branch);
3833 got_fileindex_free(*fileindex);
3836 lock_worktree(worktree, LOCK_SH);
3841 const struct got_error *
3842 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3844 const struct got_error *err;
3845 char *tmp_branch_name = NULL;
3847 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3851 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3852 free(tmp_branch_name);
3856 static const struct got_error *
3857 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3858 char **logmsg, void *arg)
3864 static const struct got_error *
3865 rebase_status(void *arg, unsigned char status, const char *path,
3866 struct got_object_id *blob_id, struct got_object_id *commit_id)
3871 struct collect_merged_paths_arg {
3872 got_worktree_checkout_cb progress_cb;
3874 struct got_pathlist_head *merged_paths;
3877 static const struct got_error *
3878 collect_merged_paths(void *arg, unsigned char status, const char *path)
3880 const struct got_error *err;
3881 struct collect_merged_paths_arg *a = arg;
3883 struct got_pathlist_entry *new;
3885 err = (*a->progress_cb)(a->progress_arg, status, path);
3889 if (status != GOT_STATUS_MERGE &&
3890 status != GOT_STATUS_ADD &&
3891 status != GOT_STATUS_DELETE &&
3892 status != GOT_STATUS_CONFLICT)
3897 return got_error_from_errno("strdup");
3899 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3900 if (err || new == NULL)
3906 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3908 struct got_pathlist_entry *pe;
3910 TAILQ_FOREACH(pe, merged_paths, entry)
3911 free((char *)pe->path);
3913 got_pathlist_free(merged_paths);
3916 static const struct got_error *
3917 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3918 struct got_repository *repo)
3920 const struct got_error *err;
3921 struct got_reference *commit_ref = NULL;
3923 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3925 if (err->code != GOT_ERR_NOT_REF)
3927 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3930 err = got_ref_write(commit_ref, repo);
3934 struct got_object_id *stored_id;
3937 err = got_ref_resolve(&stored_id, repo, commit_ref);
3940 cmp = got_object_id_cmp(commit_id, stored_id);
3943 err = got_error(GOT_ERR_REBASE_COMMITID);
3949 got_ref_close(commit_ref);
3953 static const struct got_error *
3954 rebase_merge_files(struct got_pathlist_head *merged_paths,
3955 const char *commit_ref_name, struct got_worktree *worktree,
3956 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3957 struct got_object_id *commit_id, struct got_repository *repo,
3958 got_worktree_checkout_cb progress_cb, void *progress_arg,
3959 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3961 const struct got_error *err;
3962 struct got_reference *commit_ref = NULL;
3963 struct collect_merged_paths_arg cmp_arg;
3964 char *fileindex_path;
3966 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3968 err = get_fileindex_path(&fileindex_path, worktree);
3972 cmp_arg.progress_cb = progress_cb;
3973 cmp_arg.progress_arg = progress_arg;
3974 cmp_arg.merged_paths = merged_paths;
3975 err = merge_files(worktree, fileindex, fileindex_path,
3976 parent_commit_id, commit_id, repo, collect_merged_paths,
3977 &cmp_arg, cancel_cb, cancel_arg);
3979 got_ref_close(commit_ref);
3983 const struct got_error *
3984 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3985 struct got_worktree *worktree, struct got_fileindex *fileindex,
3986 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3987 struct got_repository *repo,
3988 got_worktree_checkout_cb progress_cb, void *progress_arg,
3989 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3991 const struct got_error *err;
3992 char *commit_ref_name;
3994 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3998 err = store_commit_id(commit_ref_name, commit_id, repo);
4002 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4003 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4004 progress_arg, cancel_cb, cancel_arg);
4006 free(commit_ref_name);
4010 const struct got_error *
4011 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4012 struct got_worktree *worktree, struct got_fileindex *fileindex,
4013 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4014 struct got_repository *repo,
4015 got_worktree_checkout_cb progress_cb, void *progress_arg,
4016 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4018 const struct got_error *err;
4019 char *commit_ref_name;
4021 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4025 err = store_commit_id(commit_ref_name, commit_id, repo);
4029 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4030 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4031 progress_arg, cancel_cb, cancel_arg);
4033 free(commit_ref_name);
4037 static const struct got_error *
4038 rebase_commit(struct got_object_id **new_commit_id,
4039 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4040 struct got_worktree *worktree, struct got_fileindex *fileindex,
4041 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4042 const char *new_logmsg, struct got_repository *repo)
4044 const struct got_error *err, *sync_err;
4045 struct got_pathlist_head commitable_paths;
4046 struct collect_commitables_arg cc_arg;
4047 char *fileindex_path = NULL;
4048 struct got_reference *head_ref = NULL;
4049 struct got_object_id *head_commit_id = NULL;
4050 char *logmsg = NULL;
4052 TAILQ_INIT(&commitable_paths);
4053 *new_commit_id = NULL;
4055 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4057 err = get_fileindex_path(&fileindex_path, worktree);
4061 cc_arg.commitable_paths = &commitable_paths;
4062 cc_arg.worktree = worktree;
4065 * If possible get the status of individual files directly to
4066 * avoid crawling the entire work tree once per rebased commit.
4067 * TODO: Ideally, merged_paths would contain a list of commitables
4068 * we could use so we could skip worktree_status() entirely.
4071 struct got_pathlist_entry *pe;
4072 if (TAILQ_EMPTY(merged_paths)) {
4073 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4076 TAILQ_FOREACH(pe, merged_paths, entry) {
4077 err = worktree_status(worktree, pe->path, fileindex,
4078 repo, collect_commitables, &cc_arg, NULL, NULL);
4083 err = worktree_status(worktree, "", fileindex, repo,
4084 collect_commitables, &cc_arg, NULL, NULL);
4089 if (TAILQ_EMPTY(&commitable_paths)) {
4090 /* No-op change; commit will be elided. */
4091 err = got_ref_delete(commit_ref, repo);
4094 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4098 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4102 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4107 logmsg = strdup(new_logmsg);
4109 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4111 return got_error_from_errno("strdup");
4113 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4114 worktree, got_object_commit_get_author(orig_commit),
4115 got_object_commit_get_committer(orig_commit),
4116 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4120 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4124 err = got_ref_delete(commit_ref, repo);
4128 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4130 sync_err = sync_fileindex(fileindex, fileindex_path);
4131 if (sync_err && err == NULL)
4134 free(fileindex_path);
4135 free(head_commit_id);
4137 got_ref_close(head_ref);
4139 free(*new_commit_id);
4140 *new_commit_id = NULL;
4145 const struct got_error *
4146 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4147 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4148 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4149 struct got_commit_object *orig_commit,
4150 struct got_object_id *orig_commit_id, struct got_repository *repo)
4152 const struct got_error *err;
4153 char *commit_ref_name;
4154 struct got_reference *commit_ref = NULL;
4155 struct got_object_id *commit_id = NULL;
4157 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4161 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4164 err = got_ref_resolve(&commit_id, repo, commit_ref);
4167 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4168 err = got_error(GOT_ERR_REBASE_COMMITID);
4172 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4173 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4176 got_ref_close(commit_ref);
4177 free(commit_ref_name);
4182 const struct got_error *
4183 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4184 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4185 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4186 struct got_commit_object *orig_commit,
4187 struct got_object_id *orig_commit_id, const char *new_logmsg,
4188 struct got_repository *repo)
4190 const struct got_error *err;
4191 char *commit_ref_name;
4192 struct got_reference *commit_ref = NULL;
4193 struct got_object_id *commit_id = NULL;
4195 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4199 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4202 err = got_ref_resolve(&commit_id, repo, commit_ref);
4205 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4206 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4210 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4211 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4214 got_ref_close(commit_ref);
4215 free(commit_ref_name);
4220 const struct got_error *
4221 got_worktree_rebase_postpone(struct got_worktree *worktree,
4222 struct got_fileindex *fileindex)
4225 got_fileindex_free(fileindex);
4226 return lock_worktree(worktree, LOCK_SH);
4229 static const struct got_error *
4230 delete_ref(const char *name, struct got_repository *repo)
4232 const struct got_error *err;
4233 struct got_reference *ref;
4235 err = got_ref_open(&ref, repo, name, 0);
4237 if (err->code == GOT_ERR_NOT_REF)
4242 err = got_ref_delete(ref, repo);
4247 static const struct got_error *
4248 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4250 const struct got_error *err;
4251 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4252 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4254 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4257 err = delete_ref(tmp_branch_name, repo);
4261 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4264 err = delete_ref(new_base_branch_ref_name, repo);
4268 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4271 err = delete_ref(branch_ref_name, repo);
4275 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4278 err = delete_ref(commit_ref_name, repo);
4283 free(tmp_branch_name);
4284 free(new_base_branch_ref_name);
4285 free(branch_ref_name);
4286 free(commit_ref_name);
4290 const struct got_error *
4291 got_worktree_rebase_complete(struct got_worktree *worktree,
4292 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4293 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4294 struct got_repository *repo)
4296 const struct got_error *err, *unlockerr;
4297 struct got_object_id *new_head_commit_id = NULL;
4299 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4303 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4307 err = got_ref_write(rebased_branch, repo);
4311 err = got_worktree_set_head_ref(worktree, rebased_branch);
4315 err = delete_rebase_refs(worktree, repo);
4318 got_fileindex_free(fileindex);
4319 free(new_head_commit_id);
4320 unlockerr = lock_worktree(worktree, LOCK_SH);
4321 if (unlockerr && err == NULL)
4326 struct collect_revertible_paths_arg {
4327 struct got_pathlist_head *revertible_paths;
4328 struct got_worktree *worktree;
4331 static const struct got_error *
4332 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4333 struct got_object_id *blob_id, struct got_object_id *commit_id)
4335 struct collect_revertible_paths_arg *a = arg;
4336 const struct got_error *err = NULL;
4337 struct got_pathlist_entry *new = NULL;
4340 if (status != GOT_STATUS_ADD &&
4341 status != GOT_STATUS_DELETE &&
4342 status != GOT_STATUS_MODIFY &&
4343 status != GOT_STATUS_CONFLICT &&
4344 status != GOT_STATUS_MISSING)
4347 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4348 return got_error_from_errno("asprintf");
4350 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4351 if (err || new == NULL)
4356 const struct got_error *
4357 got_worktree_rebase_abort(struct got_worktree *worktree,
4358 struct got_fileindex *fileindex, struct got_repository *repo,
4359 struct got_reference *new_base_branch,
4360 got_worktree_checkout_cb progress_cb, void *progress_arg)
4362 const struct got_error *err, *unlockerr, *sync_err;
4363 struct got_reference *resolved = NULL;
4364 struct got_object_id *commit_id = NULL;
4365 char *fileindex_path = NULL;
4366 struct got_pathlist_head revertible_paths;
4367 struct got_pathlist_entry *pe;
4368 struct collect_revertible_paths_arg crp_arg;
4369 struct got_object_id *tree_id = NULL;
4371 TAILQ_INIT(&revertible_paths);
4373 err = lock_worktree(worktree, LOCK_EX);
4377 err = got_ref_open(&resolved, repo,
4378 got_ref_get_symref_target(new_base_branch), 0);
4382 err = got_worktree_set_head_ref(worktree, resolved);
4387 * XXX commits to the base branch could have happened while
4388 * we were busy rebasing; should we store the original commit ID
4389 * when rebase begins and read it back here?
4391 err = got_ref_resolve(&commit_id, repo, resolved);
4395 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4399 err = got_object_id_by_path(&tree_id, repo,
4400 worktree->base_commit_id, worktree->path_prefix);
4404 err = delete_rebase_refs(worktree, repo);
4408 err = get_fileindex_path(&fileindex_path, worktree);
4412 crp_arg.revertible_paths = &revertible_paths;
4413 crp_arg.worktree = worktree;
4414 err = worktree_status(worktree, "", fileindex, repo,
4415 collect_revertible_paths, &crp_arg, NULL, NULL);
4419 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4420 err = revert_file(worktree, fileindex, pe->path,
4421 progress_cb, progress_arg, repo);
4426 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4427 repo, progress_cb, progress_arg, NULL, NULL);
4429 sync_err = sync_fileindex(fileindex, fileindex_path);
4430 if (sync_err && err == NULL)
4433 got_ref_close(resolved);
4437 got_fileindex_free(fileindex);
4438 free(fileindex_path);
4439 TAILQ_FOREACH(pe, &revertible_paths, entry)
4440 free((char *)pe->path);
4441 got_pathlist_free(&revertible_paths);
4443 unlockerr = lock_worktree(worktree, LOCK_SH);
4444 if (unlockerr && err == NULL)
4449 const struct got_error *
4450 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4451 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4452 struct got_fileindex **fileindex, struct got_worktree *worktree,
4453 struct got_repository *repo)
4455 const struct got_error *err = NULL;
4456 char *tmp_branch_name = NULL;
4457 char *branch_ref_name = NULL;
4458 char *base_commit_ref_name = NULL;
4459 char *fileindex_path = NULL;
4460 struct check_rebase_ok_arg ok_arg;
4461 struct got_reference *wt_branch = NULL;
4462 struct got_reference *base_commit_ref = NULL;
4466 *base_commit_id = NULL;
4469 err = lock_worktree(worktree, LOCK_EX);
4473 err = open_fileindex(fileindex, &fileindex_path, worktree);
4477 ok_arg.worktree = worktree;
4479 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4484 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4488 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4492 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4497 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4502 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4506 err = got_ref_write(*branch_ref, repo);
4510 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4511 worktree->base_commit_id);
4514 err = got_ref_write(base_commit_ref, repo);
4517 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4518 if (*base_commit_id == NULL) {
4519 err = got_error_from_errno("got_object_id_dup");
4523 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4524 worktree->base_commit_id);
4527 err = got_ref_write(*tmp_branch, repo);
4531 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4535 free(fileindex_path);
4536 free(tmp_branch_name);
4537 free(branch_ref_name);
4538 free(base_commit_ref_name);
4540 got_ref_close(wt_branch);
4543 got_ref_close(*branch_ref);
4547 got_ref_close(*tmp_branch);
4550 free(*base_commit_id);
4552 got_fileindex_free(*fileindex);
4555 lock_worktree(worktree, LOCK_SH);
4560 const struct got_error *
4561 got_worktree_histedit_postpone(struct got_worktree *worktree,
4562 struct got_fileindex *fileindex)
4565 got_fileindex_free(fileindex);
4566 return lock_worktree(worktree, LOCK_SH);
4569 const struct got_error *
4570 got_worktree_histedit_in_progress(int *in_progress,
4571 struct got_worktree *worktree)
4573 const struct got_error *err;
4574 char *tmp_branch_name = NULL;
4576 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4580 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4581 free(tmp_branch_name);
4585 const struct got_error *
4586 got_worktree_histedit_continue(struct got_object_id **commit_id,
4587 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4588 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4589 struct got_worktree *worktree, struct got_repository *repo)
4591 const struct got_error *err;
4592 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4593 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4594 struct got_reference *commit_ref = NULL;
4595 struct got_reference *base_commit_ref = NULL;
4596 char *fileindex_path = NULL;
4600 *base_commit_id = NULL;
4603 err = lock_worktree(worktree, LOCK_EX);
4607 err = open_fileindex(fileindex, &fileindex_path, worktree);
4611 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4615 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4619 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4623 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4628 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4632 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4635 err = got_ref_resolve(commit_id, repo, commit_ref);
4639 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4642 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4646 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4650 free(commit_ref_name);
4651 free(branch_ref_name);
4652 free(fileindex_path);
4654 got_ref_close(commit_ref);
4655 if (base_commit_ref)
4656 got_ref_close(base_commit_ref);
4660 free(*base_commit_id);
4661 *base_commit_id = NULL;
4663 got_ref_close(*tmp_branch);
4667 got_fileindex_free(*fileindex);
4670 lock_worktree(worktree, LOCK_EX);
4675 static const struct got_error *
4676 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4678 const struct got_error *err;
4679 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4680 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4682 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4685 err = delete_ref(tmp_branch_name, repo);
4689 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4693 err = delete_ref(base_commit_ref_name, repo);
4697 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4700 err = delete_ref(branch_ref_name, repo);
4704 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4707 err = delete_ref(commit_ref_name, repo);
4711 free(tmp_branch_name);
4712 free(base_commit_ref_name);
4713 free(branch_ref_name);
4714 free(commit_ref_name);
4718 const struct got_error *
4719 got_worktree_histedit_abort(struct got_worktree *worktree,
4720 struct got_fileindex *fileindex, struct got_repository *repo,
4721 struct got_reference *branch, struct got_object_id *base_commit_id,
4722 got_worktree_checkout_cb progress_cb, void *progress_arg)
4724 const struct got_error *err, *unlockerr, *sync_err;
4725 struct got_reference *resolved = NULL;
4726 char *fileindex_path = NULL;
4727 struct got_pathlist_head revertible_paths;
4728 struct got_pathlist_entry *pe;
4729 struct collect_revertible_paths_arg crp_arg;
4730 struct got_object_id *tree_id = NULL;
4732 TAILQ_INIT(&revertible_paths);
4734 err = lock_worktree(worktree, LOCK_EX);
4738 err = got_ref_open(&resolved, repo,
4739 got_ref_get_symref_target(branch), 0);
4743 err = got_worktree_set_head_ref(worktree, resolved);
4747 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4751 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4752 worktree->path_prefix);
4756 err = delete_histedit_refs(worktree, repo);
4760 err = get_fileindex_path(&fileindex_path, worktree);
4764 crp_arg.revertible_paths = &revertible_paths;
4765 crp_arg.worktree = worktree;
4766 err = worktree_status(worktree, "", fileindex, repo,
4767 collect_revertible_paths, &crp_arg, NULL, NULL);
4771 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4772 err = revert_file(worktree, fileindex, pe->path,
4773 progress_cb, progress_arg, repo);
4778 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4779 repo, progress_cb, progress_arg, NULL, NULL);
4781 sync_err = sync_fileindex(fileindex, fileindex_path);
4782 if (sync_err && err == NULL)
4785 got_ref_close(resolved);
4787 free(fileindex_path);
4788 TAILQ_FOREACH(pe, &revertible_paths, entry)
4789 free((char *)pe->path);
4790 got_pathlist_free(&revertible_paths);
4792 unlockerr = lock_worktree(worktree, LOCK_SH);
4793 if (unlockerr && err == NULL)
4798 const struct got_error *
4799 got_worktree_histedit_complete(struct got_worktree *worktree,
4800 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4801 struct got_reference *edited_branch, struct got_repository *repo)
4803 const struct got_error *err, *unlockerr;
4804 struct got_object_id *new_head_commit_id = NULL;
4805 struct got_reference *resolved = NULL;
4807 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4811 err = got_ref_open(&resolved, repo,
4812 got_ref_get_symref_target(edited_branch), 0);
4816 err = got_ref_change_ref(resolved, new_head_commit_id);
4820 err = got_ref_write(resolved, repo);
4824 err = got_worktree_set_head_ref(worktree, resolved);
4828 err = delete_histedit_refs(worktree, repo);
4831 got_fileindex_free(fileindex);
4832 free(new_head_commit_id);
4833 unlockerr = lock_worktree(worktree, LOCK_SH);
4834 if (unlockerr && err == NULL)
4839 const struct got_error *
4840 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4841 struct got_object_id *commit_id, struct got_repository *repo)
4843 const struct got_error *err;
4844 char *commit_ref_name;
4846 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4850 err = store_commit_id(commit_ref_name, commit_id, repo);
4854 err = delete_ref(commit_ref_name, repo);
4856 free(commit_ref_name);
4860 static const struct got_error *
4861 stage_path(const char *path, size_t path_len, struct got_worktree *worktree,
4862 struct got_fileindex *fileindex, struct got_repository *repo,
4863 got_worktree_status_cb status_cb, void *status_arg)
4865 const struct got_error *err = NULL;
4867 struct got_fileindex_entry *ie;
4868 unsigned char status;
4870 struct got_object_id *blob_id = NULL;
4873 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
4874 return got_error_from_errno("asprintf");
4876 ie = got_fileindex_entry_get(fileindex, path, path_len);
4878 err = got_error_path(path, GOT_ERR_FILE_STATUS);
4882 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
4887 case GOT_STATUS_ADD:
4888 case GOT_STATUS_MODIFY:
4889 err = got_object_blob_create(&blob_id, ondisk_path,
4893 memcpy(ie->staged_blob_sha1, blob_id->sha1,
4894 SHA1_DIGEST_LENGTH);
4895 if (status == GOT_STATUS_ADD)
4896 stage = GOT_FILEIDX_STAGE_ADD;
4898 stage = GOT_FILEIDX_STAGE_MODIFY;
4900 case GOT_STATUS_DELETE:
4901 stage = GOT_FILEIDX_STAGE_DELETE;
4904 err = got_error_path(path, GOT_ERR_FILE_STATUS);
4908 got_fileindex_entry_stage_set(ie, stage);
4910 /* XXX TODO pass 'staged' status separately */
4911 err = (*status_cb)(status_arg, status, path, blob_id, NULL);
4918 const struct got_error *
4919 got_worktree_stage_paths(struct got_worktree *worktree,
4920 struct got_pathlist_head *paths, struct got_repository *repo,
4921 got_worktree_status_cb status_cb, void *status_arg,
4922 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4924 const struct got_error *err = NULL, *sync_err, *unlockerr;
4925 struct got_pathlist_entry *pe;
4926 struct got_fileindex *fileindex = NULL;
4927 char *fileindex_path = NULL;
4929 err = lock_worktree(worktree, LOCK_EX);
4933 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4937 TAILQ_FOREACH(pe, paths, entry) {
4939 err = (*cancel_cb)(cancel_arg);
4943 err = stage_path(pe->path, pe->path_len, worktree, fileindex,
4944 repo, status_cb, status_arg);
4949 sync_err = sync_fileindex(fileindex, fileindex_path);
4950 if (sync_err && err == NULL)
4953 free(fileindex_path);
4955 got_fileindex_free(fileindex);
4956 unlockerr = lock_worktree(worktree, LOCK_SH);
4957 if (unlockerr && err == NULL)