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 unsigned char
1038 get_staged_status(struct got_fileindex_entry *ie)
1040 switch (got_fileindex_entry_stage_get(ie)) {
1041 case GOT_FILEIDX_STAGE_ADD:
1042 return GOT_STATUS_ADD;
1043 case GOT_FILEIDX_STAGE_DELETE:
1044 return GOT_STATUS_DELETE;
1045 case GOT_FILEIDX_STAGE_MODIFY:
1046 return GOT_STATUS_MODIFY;
1048 return GOT_STATUS_NO_CHANGE;
1052 static const struct got_error *
1053 get_file_status(unsigned char *status, struct stat *sb,
1054 struct got_fileindex_entry *ie, const char *abspath,
1055 struct got_repository *repo)
1057 const struct got_error *err = NULL;
1058 struct got_object_id id;
1062 struct got_blob_object *blob = NULL;
1064 unsigned char staged_status = get_staged_status(ie);
1066 *status = GOT_STATUS_NO_CHANGE;
1068 if (lstat(abspath, sb) == -1) {
1069 if (errno == ENOENT) {
1070 if (got_fileindex_entry_has_file_on_disk(ie))
1071 *status = GOT_STATUS_MISSING;
1073 *status = GOT_STATUS_DELETE;
1076 return got_error_from_errno2("lstat", abspath);
1079 if (!S_ISREG(sb->st_mode)) {
1080 *status = GOT_STATUS_OBSTRUCTED;
1084 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1085 *status = GOT_STATUS_DELETE;
1087 } else if (!got_fileindex_entry_has_blob(ie)) {
1088 *status = GOT_STATUS_ADD;
1092 if (!stat_info_differs(ie, sb))
1095 if (staged_status == GOT_STATUS_MODIFY ||
1096 staged_status == GOT_STATUS_ADD)
1097 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1099 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1101 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1105 f = fopen(abspath, "r");
1107 err = got_error_from_errno2("fopen", abspath);
1110 hdrlen = got_object_blob_get_hdrlen(blob);
1112 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1113 err = got_object_blob_read_block(&blen, blob);
1116 /* Skip length of blob object header first time around. */
1117 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1118 if (flen == 0 && ferror(f)) {
1119 err = got_error_from_errno("fread");
1124 *status = GOT_STATUS_MODIFY;
1126 } else if (flen == 0) {
1128 *status = GOT_STATUS_MODIFY;
1130 } else if (blen - hdrlen == flen) {
1131 /* Skip blob object header first time around. */
1132 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1133 *status = GOT_STATUS_MODIFY;
1137 *status = GOT_STATUS_MODIFY;
1143 if (*status == GOT_STATUS_MODIFY) {
1145 err = get_modified_file_content_status(status, f);
1149 got_object_blob_close(blob);
1156 * Update timestamps in the file index if a file is unmodified and
1157 * we had to run a full content comparison to find out.
1159 static const struct got_error *
1160 sync_timestamps(char *ondisk_path, unsigned char status,
1161 struct got_fileindex_entry *ie, struct stat *sb)
1163 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1164 return got_fileindex_entry_update(ie, ondisk_path,
1165 ie->blob_sha1, ie->commit_sha1, 1);
1170 static const struct got_error *
1171 update_blob(struct got_worktree *worktree,
1172 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1173 struct got_tree_entry *te, const char *path,
1174 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1177 const struct got_error *err = NULL;
1178 struct got_blob_object *blob = NULL;
1180 unsigned char status = GOT_STATUS_NO_CHANGE;
1183 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1184 return got_error_from_errno("asprintf");
1187 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1190 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1191 sb.st_mode = got_fileindex_perms_to_st(ie);
1193 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1195 if (status == GOT_STATUS_OBSTRUCTED) {
1196 err = (*progress_cb)(progress_arg, status, path);
1200 if (ie && status != GOT_STATUS_MISSING) {
1201 if (got_fileindex_entry_has_commit(ie) &&
1202 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1203 SHA1_DIGEST_LENGTH) == 0) {
1204 err = sync_timestamps(ondisk_path, status, ie, &sb);
1207 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1211 if (got_fileindex_entry_has_blob(ie) &&
1212 memcmp(ie->blob_sha1, te->id->sha1,
1213 SHA1_DIGEST_LENGTH) == 0) {
1214 err = sync_timestamps(ondisk_path, status, ie, &sb);
1219 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1223 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1224 int update_timestamps;
1225 struct got_blob_object *blob2 = NULL;
1226 if (got_fileindex_entry_has_blob(ie)) {
1227 struct got_object_id id2;
1228 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1229 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1233 err = merge_blob(&update_timestamps, worktree, blob2,
1234 ondisk_path, path, sb.st_mode, blob,
1235 worktree->base_commit_id, repo,
1236 progress_cb, progress_arg);
1238 got_object_blob_close(blob2);
1240 * Do not update timestamps of files with local changes.
1241 * Otherwise, a future status walk would treat them as
1242 * unmodified files again.
1244 err = got_fileindex_entry_update(ie, ondisk_path,
1245 blob->id.sha1, worktree->base_commit_id->sha1,
1247 } else if (status == GOT_STATUS_DELETE) {
1248 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1251 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1252 ondisk_path, path, blob, 0);
1256 err = install_blob(worktree, ondisk_path, path, te->mode,
1257 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1258 repo, progress_cb, progress_arg);
1261 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1262 ondisk_path, path, blob, 1);
1266 got_object_blob_close(blob);
1272 static const struct got_error *
1273 remove_ondisk_file(const char *root_path, const char *path)
1275 const struct got_error *err = NULL;
1276 char *ondisk_path = NULL;
1278 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1279 return got_error_from_errno("asprintf");
1281 if (unlink(ondisk_path) == -1) {
1282 if (errno != ENOENT)
1283 err = got_error_from_errno2("unlink", ondisk_path);
1285 char *parent = dirname(ondisk_path);
1286 while (parent && strcmp(parent, root_path) != 0) {
1287 if (rmdir(parent) == -1) {
1288 if (errno != ENOTEMPTY)
1289 err = got_error_from_errno2("rmdir",
1293 parent = dirname(parent);
1300 static const struct got_error *
1301 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1302 struct got_fileindex_entry *ie, struct got_repository *repo,
1303 got_worktree_checkout_cb progress_cb, void *progress_arg)
1305 const struct got_error *err = NULL;
1306 unsigned char status;
1310 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1312 return got_error_from_errno("asprintf");
1314 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1318 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1319 status == GOT_STATUS_ADD) {
1320 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1324 * Preserve the working file and change the deleted blob's
1325 * entry into a schedule-add entry.
1327 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1332 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1335 if (status == GOT_STATUS_NO_CHANGE) {
1336 err = remove_ondisk_file(worktree->root_path, ie->path);
1340 got_fileindex_entry_remove(fileindex, ie);
1346 struct diff_cb_arg {
1347 struct got_fileindex *fileindex;
1348 struct got_worktree *worktree;
1349 struct got_repository *repo;
1350 got_worktree_checkout_cb progress_cb;
1352 got_worktree_cancel_cb cancel_cb;
1356 static const struct got_error *
1357 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1358 struct got_tree_entry *te, const char *parent_path)
1360 struct diff_cb_arg *a = arg;
1362 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1363 return got_error(GOT_ERR_CANCELLED);
1365 return update_blob(a->worktree, a->fileindex, ie, te,
1366 ie->path, a->repo, a->progress_cb, a->progress_arg);
1369 static const struct got_error *
1370 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1372 struct diff_cb_arg *a = arg;
1374 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1375 return got_error(GOT_ERR_CANCELLED);
1377 return delete_blob(a->worktree, a->fileindex, ie,
1378 a->repo, a->progress_cb, a->progress_arg);
1381 static const struct got_error *
1382 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1384 struct diff_cb_arg *a = arg;
1385 const struct got_error *err;
1388 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1389 return got_error(GOT_ERR_CANCELLED);
1391 if (asprintf(&path, "%s%s%s", parent_path,
1392 parent_path[0] ? "/" : "", te->name)
1394 return got_error_from_errno("asprintf");
1396 if (S_ISDIR(te->mode))
1397 err = add_dir_on_disk(a->worktree, path);
1399 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1400 a->repo, a->progress_cb, a->progress_arg);
1406 static const struct got_error *
1407 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1409 const struct got_error *err = NULL;
1410 char *uuidstr = NULL;
1411 uint32_t uuid_status;
1415 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1416 if (uuid_status != uuid_s_ok)
1417 return got_error_uuid(uuid_status);
1419 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1421 err = got_error_from_errno("asprintf");
1428 const struct got_error *
1429 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1431 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1434 static const struct got_error *
1435 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1437 return get_ref_name(refname, worktree,
1438 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1441 static const struct got_error *
1442 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1444 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1447 static const struct got_error *
1448 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1450 return get_ref_name(refname, worktree,
1451 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1454 static const struct got_error *
1455 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1457 return get_ref_name(refname, worktree,
1458 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1461 static const struct got_error *
1462 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1464 return get_ref_name(refname, worktree,
1465 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1468 static const struct got_error *
1469 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1471 return get_ref_name(refname, worktree,
1472 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1475 static const struct got_error *
1476 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1478 return get_ref_name(refname, worktree,
1479 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1482 static const struct got_error *
1483 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1485 return get_ref_name(refname, worktree,
1486 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1489 const struct got_error *
1490 got_worktree_get_histedit_script_path(char **path,
1491 struct got_worktree *worktree)
1493 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1494 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1496 return got_error_from_errno("asprintf");
1502 * Prevent Git's garbage collector from deleting our base commit by
1503 * setting a reference to our base commit's ID.
1505 static const struct got_error *
1506 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1508 const struct got_error *err = NULL;
1509 struct got_reference *ref = NULL;
1512 err = got_worktree_get_base_ref_name(&refname, worktree);
1516 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1520 err = got_ref_write(ref, repo);
1528 static const struct got_error *
1529 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1531 const struct got_error *err = NULL;
1533 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1534 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1535 err = got_error_from_errno("asprintf");
1536 *fileindex_path = NULL;
1542 static const struct got_error *
1543 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1544 struct got_worktree *worktree)
1546 const struct got_error *err = NULL;
1549 *fileindex_path = NULL;
1550 *fileindex = got_fileindex_alloc();
1551 if (*fileindex == NULL)
1552 return got_error_from_errno("got_fileindex_alloc");
1554 err = get_fileindex_path(fileindex_path, worktree);
1558 index = fopen(*fileindex_path, "rb");
1559 if (index == NULL) {
1560 if (errno != ENOENT)
1561 err = got_error_from_errno2("fopen", *fileindex_path);
1563 err = got_fileindex_read(*fileindex, index);
1564 if (fclose(index) != 0 && err == NULL)
1565 err = got_error_from_errno("fclose");
1569 free(*fileindex_path);
1570 *fileindex_path = NULL;
1571 got_fileindex_free(*fileindex);
1577 struct bump_base_commit_id_arg {
1578 struct got_object_id *base_commit_id;
1581 const char *entry_name;
1582 got_worktree_checkout_cb progress_cb;
1586 /* Bump base commit ID of all files within an updated part of the work tree. */
1587 static const struct got_error *
1588 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1590 const struct got_error *err;
1591 struct bump_base_commit_id_arg *a = arg;
1593 if (a->entry_name) {
1594 if (strcmp(ie->path, a->path) != 0)
1596 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1599 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1600 SHA1_DIGEST_LENGTH) == 0)
1603 if (a->progress_cb) {
1604 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1609 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1613 static const struct got_error *
1614 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1616 const struct got_error *err = NULL;
1617 char *new_fileindex_path = NULL;
1618 FILE *new_index = NULL;
1620 err = got_opentemp_named(&new_fileindex_path, &new_index,
1625 err = got_fileindex_write(fileindex, new_index);
1629 if (rename(new_fileindex_path, fileindex_path) != 0) {
1630 err = got_error_from_errno3("rename", new_fileindex_path,
1632 unlink(new_fileindex_path);
1637 free(new_fileindex_path);
1641 static const struct got_error *
1642 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1643 struct got_object_id **tree_id, const char *wt_relpath,
1644 struct got_worktree *worktree, struct got_repository *repo)
1646 const struct got_error *err = NULL;
1647 struct got_object_id *id = NULL;
1648 char *in_repo_path = NULL;
1649 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1651 *entry_type = GOT_OBJ_TYPE_ANY;
1652 *tree_relpath = NULL;
1655 if (wt_relpath[0] == '\0') {
1656 /* Check out all files within the work tree. */
1657 *entry_type = GOT_OBJ_TYPE_TREE;
1658 *tree_relpath = strdup("");
1659 if (*tree_relpath == NULL) {
1660 err = got_error_from_errno("strdup");
1663 err = got_object_id_by_path(tree_id, repo,
1664 worktree->base_commit_id, worktree->path_prefix);
1670 /* Check out a subset of files in the work tree. */
1672 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1673 is_root_wt ? "" : "/", wt_relpath) == -1) {
1674 err = got_error_from_errno("asprintf");
1678 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1684 in_repo_path = NULL;
1686 err = got_object_get_type(entry_type, repo, id);
1690 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1691 /* Check out a single file. */
1692 if (strchr(wt_relpath, '/') == NULL) {
1693 /* Check out a single file in work tree's root dir. */
1694 in_repo_path = strdup(worktree->path_prefix);
1695 if (in_repo_path == NULL) {
1696 err = got_error_from_errno("strdup");
1699 *tree_relpath = strdup("");
1700 if (*tree_relpath == NULL) {
1701 err = got_error_from_errno("strdup");
1705 /* Check out a single file in a subdirectory. */
1706 err = got_path_dirname(tree_relpath, wt_relpath);
1709 if (asprintf(&in_repo_path, "%s%s%s",
1710 worktree->path_prefix, is_root_wt ? "" : "/",
1711 *tree_relpath) == -1) {
1712 err = got_error_from_errno("asprintf");
1716 err = got_object_id_by_path(tree_id, repo,
1717 worktree->base_commit_id, in_repo_path);
1719 /* Check out all files within a subdirectory. */
1720 *tree_id = got_object_id_dup(id);
1721 if (*tree_id == NULL) {
1722 err = got_error_from_errno("got_object_id_dup");
1725 *tree_relpath = strdup(wt_relpath);
1726 if (*tree_relpath == NULL) {
1727 err = got_error_from_errno("strdup");
1735 *entry_type = GOT_OBJ_TYPE_ANY;
1736 free(*tree_relpath);
1737 *tree_relpath = NULL;
1744 static const struct got_error *
1745 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1746 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1747 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1748 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1750 const struct got_error *err = NULL;
1751 struct got_commit_object *commit = NULL;
1752 struct got_tree_object *tree = NULL;
1753 struct got_fileindex_diff_tree_cb diff_cb;
1754 struct diff_cb_arg arg;
1756 err = ref_base_commit(worktree, repo);
1760 err = got_object_open_as_commit(&commit, repo,
1761 worktree->base_commit_id);
1765 err = got_object_open_as_tree(&tree, repo, tree_id);
1770 got_object_tree_find_entry(tree, entry_name) == NULL) {
1771 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1775 diff_cb.diff_old_new = diff_old_new;
1776 diff_cb.diff_old = diff_old;
1777 diff_cb.diff_new = diff_new;
1778 arg.fileindex = fileindex;
1779 arg.worktree = worktree;
1781 arg.progress_cb = progress_cb;
1782 arg.progress_arg = progress_arg;
1783 arg.cancel_cb = cancel_cb;
1784 arg.cancel_arg = cancel_arg;
1785 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1786 entry_name, repo, &diff_cb, &arg);
1789 got_object_tree_close(tree);
1791 got_object_commit_close(commit);
1795 const struct got_error *
1796 got_worktree_checkout_files(struct got_worktree *worktree,
1797 struct got_pathlist_head *paths, struct got_repository *repo,
1798 got_worktree_checkout_cb progress_cb, void *progress_arg,
1799 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1801 const struct got_error *err = NULL, *sync_err, *unlockerr;
1802 struct got_commit_object *commit = NULL;
1803 struct got_tree_object *tree = NULL;
1804 struct got_fileindex *fileindex = NULL;
1805 char *fileindex_path = NULL;
1806 struct got_pathlist_entry *pe;
1807 struct tree_path_data {
1808 SIMPLEQ_ENTRY(tree_path_data) entry;
1809 struct got_object_id *tree_id;
1814 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1816 SIMPLEQ_INIT(&tree_paths);
1818 err = lock_worktree(worktree, LOCK_EX);
1822 /* Map all specified paths to in-repository trees. */
1823 TAILQ_FOREACH(pe, paths, entry) {
1824 tpd = malloc(sizeof(*tpd));
1826 err = got_error_from_errno("malloc");
1830 err = find_tree_entry_for_checkout(&tpd->entry_type,
1831 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1837 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1838 err = got_path_basename(&tpd->entry_name, pe->path);
1846 tpd->entry_name = NULL;
1848 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1852 * Read the file index.
1853 * Checking out files is supposed to be an idempotent operation.
1854 * If the on-disk file index is incomplete we will try to complete it.
1856 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1860 tpd = SIMPLEQ_FIRST(&tree_paths);
1861 TAILQ_FOREACH(pe, paths, entry) {
1862 struct bump_base_commit_id_arg bbc_arg;
1864 err = checkout_files(worktree, fileindex, tpd->relpath,
1865 tpd->tree_id, tpd->entry_name, repo,
1866 progress_cb, progress_arg, cancel_cb, cancel_arg);
1870 bbc_arg.base_commit_id = worktree->base_commit_id;
1871 bbc_arg.entry_name = tpd->entry_name;
1872 bbc_arg.path = pe->path;
1873 bbc_arg.path_len = pe->path_len;
1874 bbc_arg.progress_cb = progress_cb;
1875 bbc_arg.progress_arg = progress_arg;
1876 err = got_fileindex_for_each_entry_safe(fileindex,
1877 bump_base_commit_id, &bbc_arg);
1881 tpd = SIMPLEQ_NEXT(tpd, entry);
1883 sync_err = sync_fileindex(fileindex, fileindex_path);
1884 if (sync_err && err == NULL)
1887 free(fileindex_path);
1889 got_object_tree_close(tree);
1891 got_object_commit_close(commit);
1893 got_fileindex_free(fileindex);
1894 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1895 tpd = SIMPLEQ_FIRST(&tree_paths);
1896 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1901 unlockerr = lock_worktree(worktree, LOCK_SH);
1902 if (unlockerr && err == NULL)
1907 struct merge_file_cb_arg {
1908 struct got_worktree *worktree;
1909 struct got_fileindex *fileindex;
1910 got_worktree_checkout_cb progress_cb;
1912 got_worktree_cancel_cb cancel_cb;
1914 struct got_object_id *commit_id2;
1917 static const struct got_error *
1918 merge_file_cb(void *arg, struct got_blob_object *blob1,
1919 struct got_blob_object *blob2, struct got_object_id *id1,
1920 struct got_object_id *id2, const char *path1, const char *path2,
1921 struct got_repository *repo)
1923 static const struct got_error *err = NULL;
1924 struct merge_file_cb_arg *a = arg;
1925 struct got_fileindex_entry *ie;
1926 char *ondisk_path = NULL;
1928 unsigned char status;
1929 int local_changes_subsumed;
1931 if (blob1 && blob2) {
1932 ie = got_fileindex_entry_get(a->fileindex, path2,
1935 return (*a->progress_cb)(a->progress_arg,
1936 GOT_STATUS_MISSING, path2);
1938 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1940 return got_error_from_errno("asprintf");
1942 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1946 if (status == GOT_STATUS_DELETE) {
1947 err = (*a->progress_cb)(a->progress_arg,
1948 GOT_STATUS_MERGE, path2);
1951 if (status != GOT_STATUS_NO_CHANGE &&
1952 status != GOT_STATUS_MODIFY &&
1953 status != GOT_STATUS_CONFLICT &&
1954 status != GOT_STATUS_ADD) {
1955 err = (*a->progress_cb)(a->progress_arg, status, path2);
1959 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1960 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1961 a->progress_cb, a->progress_arg);
1963 ie = got_fileindex_entry_get(a->fileindex, path1,
1966 return (*a->progress_cb)(a->progress_arg,
1967 GOT_STATUS_MISSING, path2);
1969 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1971 return got_error_from_errno("asprintf");
1973 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1978 case GOT_STATUS_NO_CHANGE:
1979 err = (*a->progress_cb)(a->progress_arg,
1980 GOT_STATUS_DELETE, path1);
1983 err = remove_ondisk_file(a->worktree->root_path, path1);
1987 got_fileindex_entry_mark_deleted_from_disk(ie);
1989 case GOT_STATUS_DELETE:
1990 case GOT_STATUS_MISSING:
1991 err = (*a->progress_cb)(a->progress_arg,
1992 GOT_STATUS_DELETE, path1);
1996 got_fileindex_entry_mark_deleted_from_disk(ie);
1998 case GOT_STATUS_ADD:
1999 case GOT_STATUS_MODIFY:
2000 case GOT_STATUS_CONFLICT:
2001 err = (*a->progress_cb)(a->progress_arg,
2002 GOT_STATUS_CANNOT_DELETE, path1);
2006 case GOT_STATUS_OBSTRUCTED:
2007 err = (*a->progress_cb)(a->progress_arg, status, path1);
2015 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2017 return got_error_from_errno("asprintf");
2018 ie = got_fileindex_entry_get(a->fileindex, path2,
2021 err = get_file_status(&status, &sb, ie, ondisk_path,
2025 if (status != GOT_STATUS_NO_CHANGE &&
2026 status != GOT_STATUS_MODIFY &&
2027 status != GOT_STATUS_CONFLICT &&
2028 status != GOT_STATUS_ADD) {
2029 err = (*a->progress_cb)(a->progress_arg,
2033 err = merge_blob(&local_changes_subsumed, a->worktree,
2034 NULL, ondisk_path, path2, sb.st_mode, blob2,
2035 a->commit_id2, repo,
2036 a->progress_cb, a->progress_arg);
2037 if (status == GOT_STATUS_DELETE) {
2038 err = update_blob_fileindex_entry(a->worktree,
2039 a->fileindex, ie, ondisk_path, ie->path,
2045 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2046 err = install_blob(a->worktree, ondisk_path, path2,
2047 /* XXX get this from parent tree! */
2048 GOT_DEFAULT_FILE_MODE,
2049 sb.st_mode, blob2, 0, 0, repo,
2050 a->progress_cb, a->progress_arg);
2053 err = got_fileindex_entry_alloc(&ie,
2054 ondisk_path, path2, NULL, NULL);
2057 err = got_fileindex_entry_add(a->fileindex, ie);
2059 got_fileindex_entry_free(ie);
2069 struct check_merge_ok_arg {
2070 struct got_worktree *worktree;
2071 struct got_repository *repo;
2074 static const struct got_error *
2075 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2077 const struct got_error *err = NULL;
2078 struct check_merge_ok_arg *a = arg;
2079 unsigned char status;
2083 /* Reject merges into a work tree with mixed base commits. */
2084 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2085 SHA1_DIGEST_LENGTH))
2086 return got_error(GOT_ERR_MIXED_COMMITS);
2088 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2090 return got_error_from_errno("asprintf");
2092 /* Reject merges into a work tree with conflicted files. */
2093 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2096 if (status == GOT_STATUS_CONFLICT)
2097 return got_error(GOT_ERR_CONFLICTS);
2102 static const struct got_error *
2103 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2104 const char *fileindex_path, struct got_object_id *commit_id1,
2105 struct got_object_id *commit_id2, struct got_repository *repo,
2106 got_worktree_checkout_cb progress_cb, void *progress_arg,
2107 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2109 const struct got_error *err = NULL, *sync_err;
2110 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2111 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2112 struct merge_file_cb_arg arg;
2115 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2116 worktree->path_prefix);
2120 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2125 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2126 worktree->path_prefix);
2130 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2134 arg.worktree = worktree;
2135 arg.fileindex = fileindex;
2136 arg.progress_cb = progress_cb;
2137 arg.progress_arg = progress_arg;
2138 arg.cancel_cb = cancel_cb;
2139 arg.cancel_arg = cancel_arg;
2140 arg.commit_id2 = commit_id2;
2141 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2142 sync_err = sync_fileindex(fileindex, fileindex_path);
2143 if (sync_err && err == NULL)
2147 got_object_tree_close(tree1);
2149 got_object_tree_close(tree2);
2153 const struct got_error *
2154 got_worktree_merge_files(struct got_worktree *worktree,
2155 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2156 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2157 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2159 const struct got_error *err, *unlockerr;
2160 char *fileindex_path = NULL;
2161 struct got_fileindex *fileindex = NULL;
2162 struct check_merge_ok_arg mok_arg;
2164 err = lock_worktree(worktree, LOCK_EX);
2168 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2172 mok_arg.worktree = worktree;
2173 mok_arg.repo = repo;
2174 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2179 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2180 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2183 got_fileindex_free(fileindex);
2184 free(fileindex_path);
2185 unlockerr = lock_worktree(worktree, LOCK_SH);
2186 if (unlockerr && err == NULL)
2191 struct diff_dir_cb_arg {
2192 struct got_fileindex *fileindex;
2193 struct got_worktree *worktree;
2194 const char *status_path;
2195 size_t status_path_len;
2196 struct got_repository *repo;
2197 got_worktree_status_cb status_cb;
2199 got_worktree_cancel_cb cancel_cb;
2203 static const struct got_error *
2204 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2205 got_worktree_status_cb status_cb, void *status_arg,
2206 struct got_repository *repo)
2208 const struct got_error *err = NULL;
2209 unsigned char status = GOT_STATUS_NO_CHANGE;
2210 unsigned char staged_status = get_staged_status(ie);
2212 struct got_object_id blob_id, commit_id;
2214 err = get_file_status(&status, &sb, ie, abspath, repo);
2215 if (err == NULL && (status != GOT_STATUS_NO_CHANGE ||
2216 staged_status != GOT_STATUS_NO_CHANGE)) {
2217 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2218 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2219 err = (*status_cb)(status_arg, status, staged_status,
2220 ie->path, &blob_id, &commit_id);
2225 static const struct got_error *
2226 status_old_new(void *arg, struct got_fileindex_entry *ie,
2227 struct dirent *de, const char *parent_path)
2229 const struct got_error *err = NULL;
2230 struct diff_dir_cb_arg *a = arg;
2233 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2234 return got_error(GOT_ERR_CANCELLED);
2236 if (got_path_cmp(parent_path, a->status_path,
2237 strlen(parent_path), a->status_path_len) != 0 &&
2238 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2241 if (parent_path[0]) {
2242 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2243 parent_path, de->d_name) == -1)
2244 return got_error_from_errno("asprintf");
2246 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2248 return got_error_from_errno("asprintf");
2251 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2257 static const struct got_error *
2258 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2260 struct diff_dir_cb_arg *a = arg;
2261 struct got_object_id blob_id, commit_id;
2262 unsigned char status;
2264 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2265 return got_error(GOT_ERR_CANCELLED);
2267 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2270 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2271 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2272 if (got_fileindex_entry_has_file_on_disk(ie))
2273 status = GOT_STATUS_MISSING;
2275 status = GOT_STATUS_DELETE;
2276 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2277 ie->path, &blob_id, &commit_id);
2280 static const struct got_error *
2281 status_new(void *arg, struct dirent *de, const char *parent_path)
2283 const struct got_error *err = NULL;
2284 struct diff_dir_cb_arg *a = arg;
2287 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2288 return got_error(GOT_ERR_CANCELLED);
2290 if (de->d_type == DT_DIR)
2293 /* XXX ignore symlinks for now */
2294 if (de->d_type == DT_LNK)
2297 if (parent_path[0]) {
2298 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2299 return got_error_from_errno("asprintf");
2304 if (got_path_is_child(path, a->status_path, a->status_path_len))
2305 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2306 GOT_STATUS_NO_CHANGE, path, NULL, NULL);
2312 static const struct got_error *
2313 report_single_file_status(const char *path, const char *ondisk_path,
2314 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2315 void *status_arg, struct got_repository *repo)
2317 struct got_fileindex_entry *ie;
2320 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2322 return report_file_status(ie, ondisk_path, status_cb,
2325 if (lstat(ondisk_path, &sb) == -1) {
2326 if (errno != ENOENT)
2327 return got_error_from_errno2("lstat", ondisk_path);
2331 if (S_ISREG(sb.st_mode))
2332 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2333 GOT_STATUS_NO_CHANGE, path, NULL, NULL);
2338 static const struct got_error *
2339 worktree_status(struct got_worktree *worktree, const char *path,
2340 struct got_fileindex *fileindex, struct got_repository *repo,
2341 got_worktree_status_cb status_cb, void *status_arg,
2342 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2344 const struct got_error *err = NULL;
2345 DIR *workdir = NULL;
2346 struct got_fileindex_diff_dir_cb fdiff_cb;
2347 struct diff_dir_cb_arg arg;
2348 char *ondisk_path = NULL;
2350 if (asprintf(&ondisk_path, "%s%s%s",
2351 worktree->root_path, path[0] ? "/" : "", path) == -1)
2352 return got_error_from_errno("asprintf");
2354 workdir = opendir(ondisk_path);
2355 if (workdir == NULL) {
2356 if (errno != ENOTDIR && errno != ENOENT)
2357 err = got_error_from_errno2("opendir", ondisk_path);
2359 err = report_single_file_status(path, ondisk_path,
2360 fileindex, status_cb, status_arg, repo);
2362 fdiff_cb.diff_old_new = status_old_new;
2363 fdiff_cb.diff_old = status_old;
2364 fdiff_cb.diff_new = status_new;
2365 arg.fileindex = fileindex;
2366 arg.worktree = worktree;
2367 arg.status_path = path;
2368 arg.status_path_len = strlen(path);
2370 arg.status_cb = status_cb;
2371 arg.status_arg = status_arg;
2372 arg.cancel_cb = cancel_cb;
2373 arg.cancel_arg = cancel_arg;
2374 err = got_fileindex_diff_dir(fileindex, workdir,
2375 worktree->root_path, path, repo, &fdiff_cb, &arg);
2384 const struct got_error *
2385 got_worktree_status(struct got_worktree *worktree,
2386 struct got_pathlist_head *paths, struct got_repository *repo,
2387 got_worktree_status_cb status_cb, void *status_arg,
2388 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2390 const struct got_error *err = NULL;
2391 char *fileindex_path = NULL;
2392 struct got_fileindex *fileindex = NULL;
2393 struct got_pathlist_entry *pe;
2395 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2399 TAILQ_FOREACH(pe, paths, entry) {
2400 err = worktree_status(worktree, pe->path, fileindex, repo,
2401 status_cb, status_arg, cancel_cb, cancel_arg);
2405 free(fileindex_path);
2406 got_fileindex_free(fileindex);
2410 const struct got_error *
2411 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2414 const struct got_error *err = NULL;
2415 char *resolved, *cwd = NULL, *path = NULL;
2420 resolved = realpath(arg, NULL);
2421 if (resolved == NULL) {
2422 if (errno != ENOENT)
2423 return got_error_from_errno2("realpath", arg);
2424 cwd = getcwd(NULL, 0);
2426 return got_error_from_errno("getcwd");
2427 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2428 err = got_error_from_errno("asprintf");
2433 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2434 strlen(got_worktree_get_root_path(worktree)))) {
2435 err = got_error(GOT_ERR_BAD_PATH);
2439 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2440 err = got_path_skip_common_ancestor(&path,
2441 got_worktree_get_root_path(worktree), resolved);
2447 err = got_error_from_errno("strdup");
2452 /* XXX status walk can't deal with trailing slash! */
2454 while (len > 0 && path[len - 1] == '/') {
2455 path[len - 1] = '\0';
2468 static const struct got_error *
2469 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2470 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2471 struct got_repository *repo)
2473 const struct got_error *err = NULL;
2474 struct got_fileindex_entry *ie;
2476 /* Re-adding an existing entry is a no-op. */
2477 if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
2480 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2484 err = got_fileindex_entry_add(fileindex, ie);
2486 got_fileindex_entry_free(ie);
2490 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2493 const struct got_error *
2494 got_worktree_schedule_add(struct got_worktree *worktree,
2495 struct got_pathlist_head *ondisk_paths,
2496 got_worktree_status_cb status_cb, void *status_arg,
2497 struct got_repository *repo)
2499 struct got_fileindex *fileindex = NULL;
2500 char *fileindex_path = NULL;
2501 const struct got_error *err = NULL, *sync_err, *unlockerr;
2502 struct got_pathlist_entry *pe;
2504 err = lock_worktree(worktree, LOCK_EX);
2508 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2512 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2514 err = got_path_skip_common_ancestor(&relpath,
2515 got_worktree_get_root_path(worktree), pe->path);
2518 err = schedule_addition(pe->path, fileindex, relpath,
2519 status_cb, status_arg, repo);
2524 sync_err = sync_fileindex(fileindex, fileindex_path);
2525 if (sync_err && err == NULL)
2528 free(fileindex_path);
2530 got_fileindex_free(fileindex);
2531 unlockerr = lock_worktree(worktree, LOCK_SH);
2532 if (unlockerr && err == NULL)
2537 static const struct got_error *
2538 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2539 const char *relpath, int delete_local_mods,
2540 got_worktree_status_cb status_cb, void *status_arg,
2541 struct got_repository *repo)
2543 const struct got_error *err = NULL;
2544 struct got_fileindex_entry *ie = NULL;
2545 unsigned char status;
2548 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2550 return got_error(GOT_ERR_BAD_PATH);
2552 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2556 if (status != GOT_STATUS_NO_CHANGE) {
2557 if (status == GOT_STATUS_DELETE)
2559 if (status != GOT_STATUS_MODIFY)
2560 return got_error(GOT_ERR_FILE_STATUS);
2561 if (!delete_local_mods)
2562 return got_error(GOT_ERR_FILE_MODIFIED);
2565 if (unlink(ondisk_path) != 0)
2566 return got_error_from_errno2("unlink", ondisk_path);
2568 got_fileindex_entry_mark_deleted_from_disk(ie);
2569 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2572 const struct got_error *
2573 got_worktree_schedule_delete(struct got_worktree *worktree,
2574 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2575 got_worktree_status_cb status_cb, void *status_arg,
2576 struct got_repository *repo)
2578 struct got_fileindex *fileindex = NULL;
2579 char *fileindex_path = NULL;
2580 const struct got_error *err = NULL, *sync_err, *unlockerr;
2581 struct got_pathlist_entry *pe;
2583 err = lock_worktree(worktree, LOCK_EX);
2587 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2591 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2593 err = got_path_skip_common_ancestor(&relpath,
2594 got_worktree_get_root_path(worktree), pe->path);
2597 err = schedule_for_deletion(pe->path, fileindex, relpath,
2598 delete_local_mods, status_cb, status_arg, repo);
2603 sync_err = sync_fileindex(fileindex, fileindex_path);
2604 if (sync_err && err == NULL)
2607 free(fileindex_path);
2609 got_fileindex_free(fileindex);
2610 unlockerr = lock_worktree(worktree, LOCK_SH);
2611 if (unlockerr && err == NULL)
2616 static const struct got_error *
2617 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2618 const char *ondisk_path,
2619 got_worktree_checkout_cb progress_cb, void *progress_arg,
2620 struct got_repository *repo)
2622 const struct got_error *err = NULL;
2623 char *relpath = NULL, *parent_path = NULL;
2624 struct got_fileindex_entry *ie;
2625 struct got_tree_object *tree = NULL;
2626 struct got_object_id *tree_id = NULL;
2627 const struct got_tree_entry *te;
2628 char *tree_path = NULL, *te_name;
2629 struct got_blob_object *blob = NULL;
2630 unsigned char status;
2633 err = got_path_skip_common_ancestor(&relpath,
2634 got_worktree_get_root_path(worktree), ondisk_path);
2638 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2640 err = got_error(GOT_ERR_BAD_PATH);
2644 /* Construct in-repository path of tree which contains this blob. */
2645 err = got_path_dirname(&parent_path, ie->path);
2647 if (err->code != GOT_ERR_BAD_PATH)
2649 parent_path = strdup("/");
2650 if (parent_path == NULL) {
2651 err = got_error_from_errno("strdup");
2655 if (got_path_is_root_dir(worktree->path_prefix)) {
2656 tree_path = strdup(parent_path);
2657 if (tree_path == NULL) {
2658 err = got_error_from_errno("strdup");
2662 if (got_path_is_root_dir(parent_path)) {
2663 tree_path = strdup(worktree->path_prefix);
2664 if (tree_path == NULL) {
2665 err = got_error_from_errno("strdup");
2669 if (asprintf(&tree_path, "%s/%s",
2670 worktree->path_prefix, parent_path) == -1) {
2671 err = got_error_from_errno("asprintf");
2677 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2680 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2681 sb.st_mode = got_fileindex_perms_to_st(ie);
2683 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2686 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2687 status == GOT_STATUS_ADD))
2690 err = got_object_open_as_tree(&tree, repo, tree_id);
2694 te_name = basename(ie->path);
2695 if (te_name == NULL) {
2696 err = got_error_from_errno2("basename", ie->path);
2700 te = got_object_tree_find_entry(tree, te_name);
2701 if (te == NULL && status != GOT_STATUS_ADD) {
2702 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2708 case GOT_STATUS_ADD:
2709 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2712 got_fileindex_entry_remove(fileindex, ie);
2714 case GOT_STATUS_DELETE:
2715 case GOT_STATUS_MODIFY:
2716 case GOT_STATUS_CONFLICT:
2717 case GOT_STATUS_MISSING: {
2718 struct got_object_id id;
2719 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2720 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2723 err = install_blob(worktree, ondisk_path, ie->path,
2724 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2728 if (status == GOT_STATUS_DELETE) {
2729 err = update_blob_fileindex_entry(worktree,
2730 fileindex, ie, ondisk_path, ie->path, blob, 1);
2744 got_object_blob_close(blob);
2746 got_object_tree_close(tree);
2751 const struct got_error *
2752 got_worktree_revert(struct got_worktree *worktree,
2753 struct got_pathlist_head *ondisk_paths,
2754 got_worktree_checkout_cb progress_cb, void *progress_arg,
2755 struct got_repository *repo)
2757 struct got_fileindex *fileindex = NULL;
2758 char *fileindex_path = NULL;
2759 const struct got_error *err = NULL, *unlockerr = NULL;
2760 const struct got_error *sync_err = NULL;
2761 struct got_pathlist_entry *pe;
2763 err = lock_worktree(worktree, LOCK_EX);
2767 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2771 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2772 err = revert_file(worktree, fileindex, pe->path,
2773 progress_cb, progress_arg, repo);
2777 sync_err = sync_fileindex(fileindex, fileindex_path);
2778 if (sync_err && err == NULL)
2781 free(fileindex_path);
2783 got_fileindex_free(fileindex);
2784 unlockerr = lock_worktree(worktree, LOCK_SH);
2785 if (unlockerr && err == NULL)
2791 free_commitable(struct got_commitable *ct)
2794 free(ct->in_repo_path);
2795 free(ct->ondisk_path);
2797 free(ct->base_blob_id);
2798 free(ct->base_commit_id);
2802 struct collect_commitables_arg {
2803 struct got_pathlist_head *commitable_paths;
2804 struct got_repository *repo;
2805 struct got_worktree *worktree;
2808 static const struct got_error *
2809 collect_commitables(void *arg, unsigned char status,
2810 unsigned char staged_status, const char *relpath,
2811 struct got_object_id *blob_id, struct got_object_id *commit_id)
2813 struct collect_commitables_arg *a = arg;
2814 const struct got_error *err = NULL;
2815 struct got_commitable *ct = NULL;
2816 struct got_pathlist_entry *new = NULL;
2817 char *parent_path = NULL, *path = NULL;
2820 if (status == GOT_STATUS_CONFLICT)
2821 return got_error(GOT_ERR_COMMIT_CONFLICT);
2823 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2824 status != GOT_STATUS_DELETE)
2827 if (asprintf(&path, "/%s", relpath) == -1) {
2828 err = got_error_from_errno("asprintf");
2831 if (strcmp(path, "/") == 0) {
2832 parent_path = strdup("");
2833 if (parent_path == NULL)
2834 return got_error_from_errno("strdup");
2836 err = got_path_dirname(&parent_path, path);
2841 ct = calloc(1, sizeof(*ct));
2843 err = got_error_from_errno("calloc");
2847 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2849 err = got_error_from_errno("asprintf");
2852 if (status == GOT_STATUS_DELETE) {
2853 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2855 if (lstat(ct->ondisk_path, &sb) != 0) {
2856 err = got_error_from_errno2("lstat", ct->ondisk_path);
2859 ct->mode = sb.st_mode;
2862 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2863 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2865 err = got_error_from_errno("asprintf");
2869 ct->status = status;
2870 ct->blob_id = NULL; /* will be filled in when blob gets created */
2871 if (ct->status != GOT_STATUS_ADD) {
2872 ct->base_blob_id = got_object_id_dup(blob_id);
2873 if (ct->base_blob_id == NULL) {
2874 err = got_error_from_errno("got_object_id_dup");
2877 ct->base_commit_id = got_object_id_dup(commit_id);
2878 if (ct->base_commit_id == NULL) {
2879 err = got_error_from_errno("got_object_id_dup");
2883 ct->path = strdup(path);
2884 if (ct->path == NULL) {
2885 err = got_error_from_errno("strdup");
2888 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2890 if (ct && (err || new == NULL))
2891 free_commitable(ct);
2897 static const struct got_error *write_tree(struct got_object_id **,
2898 struct got_tree_object *, const char *, struct got_pathlist_head *,
2899 got_worktree_status_cb status_cb, void *status_arg,
2900 struct got_repository *);
2902 static const struct got_error *
2903 write_subtree(struct got_object_id **new_subtree_id,
2904 struct got_tree_entry *te, const char *parent_path,
2905 struct got_pathlist_head *commitable_paths,
2906 got_worktree_status_cb status_cb, void *status_arg,
2907 struct got_repository *repo)
2909 const struct got_error *err = NULL;
2910 struct got_tree_object *subtree;
2913 if (asprintf(&subpath, "%s%s%s", parent_path,
2914 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2915 return got_error_from_errno("asprintf");
2917 err = got_object_open_as_tree(&subtree, repo, te->id);
2921 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2922 status_cb, status_arg, repo);
2923 got_object_tree_close(subtree);
2928 static const struct got_error *
2929 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2931 const struct got_error *err = NULL;
2932 char *ct_parent_path = NULL;
2936 if (strchr(ct->in_repo_path, '/') == NULL) {
2937 *match = got_path_is_root_dir(path);
2941 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2944 *match = (strcmp(path, ct_parent_path) == 0);
2945 free(ct_parent_path);
2950 get_ct_file_mode(struct got_commitable *ct)
2952 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2955 static const struct got_error *
2956 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2957 struct got_tree_entry *te, struct got_commitable *ct)
2959 const struct got_error *err = NULL;
2963 err = got_object_tree_entry_dup(new_te, te);
2967 (*new_te)->mode = get_ct_file_mode(ct);
2969 free((*new_te)->id);
2970 (*new_te)->id = got_object_id_dup(ct->blob_id);
2971 if ((*new_te)->id == NULL) {
2972 err = got_error_from_errno("got_object_id_dup");
2976 if (err && *new_te) {
2977 got_object_tree_entry_close(*new_te);
2983 static const struct got_error *
2984 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2985 struct got_commitable *ct)
2987 const struct got_error *err = NULL;
2992 *new_te = calloc(1, sizeof(**new_te));
2993 if (*new_te == NULL)
2994 return got_error_from_errno("calloc");
2996 ct_name = basename(ct->path);
2997 if (ct_name == NULL) {
2998 err = got_error_from_errno2("basename", ct->path);
3001 (*new_te)->name = strdup(ct_name);
3002 if ((*new_te)->name == NULL) {
3003 err = got_error_from_errno("strdup");
3007 (*new_te)->mode = get_ct_file_mode(ct);
3009 (*new_te)->id = got_object_id_dup(ct->blob_id);
3010 if ((*new_te)->id == NULL) {
3011 err = got_error_from_errno("got_object_id_dup");
3015 if (err && *new_te) {
3016 got_object_tree_entry_close(*new_te);
3022 static const struct got_error *
3023 insert_tree_entry(struct got_tree_entry *new_te,
3024 struct got_pathlist_head *paths)
3026 const struct got_error *err = NULL;
3027 struct got_pathlist_entry *new_pe;
3029 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3033 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3037 static const struct got_error *
3038 report_ct_status(struct got_commitable *ct,
3039 got_worktree_status_cb status_cb, void *status_arg)
3041 const char *ct_path = ct->path;
3042 while (ct_path[0] == '/')
3044 return (*status_cb)(status_arg, ct->status, GOT_STATUS_NO_CHANGE,
3045 ct_path, ct->blob_id, NULL);
3048 static const struct got_error *
3049 match_modified_subtree(int *modified, struct got_tree_entry *te,
3050 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3052 const struct got_error *err = NULL;
3053 struct got_pathlist_entry *pe;
3058 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3059 got_path_is_root_dir(base_tree_path) ? "" : "/",
3061 return got_error_from_errno("asprintf");
3063 TAILQ_FOREACH(pe, commitable_paths, entry) {
3064 struct got_commitable *ct = pe->data;
3065 *modified = got_path_is_child(ct->in_repo_path, te_path,
3075 static const struct got_error *
3076 match_deleted_or_modified_ct(struct got_commitable **ctp,
3077 struct got_tree_entry *te, const char *base_tree_path,
3078 struct got_pathlist_head *commitable_paths)
3080 const struct got_error *err = NULL;
3081 struct got_pathlist_entry *pe;
3085 TAILQ_FOREACH(pe, commitable_paths, entry) {
3086 struct got_commitable *ct = pe->data;
3087 char *ct_name = NULL;
3090 if (ct->status != GOT_STATUS_MODIFY &&
3091 ct->status != GOT_STATUS_DELETE)
3094 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3097 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3103 ct_name = basename(pe->path);
3104 if (ct_name == NULL)
3105 return got_error_from_errno2("basename", pe->path);
3107 if (strcmp(te->name, ct_name) != 0)
3117 static const struct got_error *
3118 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3119 const char *child_path, const char *path_base_tree,
3120 struct got_pathlist_head *commitable_paths,
3121 got_worktree_status_cb status_cb, void *status_arg,
3122 struct got_repository *repo)
3124 const struct got_error *err = NULL;
3125 struct got_tree_entry *new_te;
3130 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3131 got_path_is_root_dir(path_base_tree) ? "" : "/",
3133 return got_error_from_errno("asprintf");
3135 new_te = calloc(1, sizeof(*new_te));
3136 new_te->mode = S_IFDIR;
3137 new_te->name = strdup(child_path);
3138 if (new_te->name == NULL) {
3139 err = got_error_from_errno("strdup");
3140 got_object_tree_entry_close(new_te);
3143 err = write_tree(&new_te->id, NULL, subtree_path,
3144 commitable_paths, status_cb, status_arg, repo);
3146 got_object_tree_entry_close(new_te);
3156 static const struct got_error *
3157 write_tree(struct got_object_id **new_tree_id,
3158 struct got_tree_object *base_tree, const char *path_base_tree,
3159 struct got_pathlist_head *commitable_paths,
3160 got_worktree_status_cb status_cb, void *status_arg,
3161 struct got_repository *repo)
3163 const struct got_error *err = NULL;
3164 const struct got_tree_entries *base_entries = NULL;
3165 struct got_pathlist_head paths;
3166 struct got_tree_entries new_tree_entries;
3167 struct got_tree_entry *te, *new_te = NULL;
3168 struct got_pathlist_entry *pe;
3171 new_tree_entries.nentries = 0;
3172 SIMPLEQ_INIT(&new_tree_entries.head);
3174 /* Insert, and recurse into, newly added entries first. */
3175 TAILQ_FOREACH(pe, commitable_paths, entry) {
3176 struct got_commitable *ct = pe->data;
3177 char *child_path = NULL, *slash;
3179 if (ct->status != GOT_STATUS_ADD ||
3180 (ct->flags & GOT_COMMITABLE_ADDED))
3183 if (!got_path_is_child(pe->path, path_base_tree,
3184 strlen(path_base_tree)))
3187 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3192 slash = strchr(child_path, '/');
3193 if (slash == NULL) {
3194 err = alloc_added_blob_tree_entry(&new_te, ct);
3197 err = report_ct_status(ct, status_cb, status_arg);
3200 ct->flags |= GOT_COMMITABLE_ADDED;
3201 err = insert_tree_entry(new_te, &paths);
3205 *slash = '\0'; /* trim trailing path components */
3206 if (base_tree == NULL ||
3207 got_object_tree_find_entry(base_tree, child_path)
3209 err = make_subtree_for_added_blob(&new_te,
3210 child_path, path_base_tree,
3211 commitable_paths, status_cb, status_arg,
3215 err = insert_tree_entry(new_te, &paths);
3223 /* Handle modified and deleted entries. */
3224 base_entries = got_object_tree_get_entries(base_tree);
3225 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3226 struct got_commitable *ct = NULL;
3228 if (S_ISDIR(te->mode)) {
3230 err = got_object_tree_entry_dup(&new_te, te);
3233 err = match_modified_subtree(&modified, te,
3234 path_base_tree, commitable_paths);
3237 /* Avoid recursion into unmodified subtrees. */
3240 err = write_subtree(&new_te->id, te,
3241 path_base_tree, commitable_paths,
3242 status_cb, status_arg, repo);
3246 err = insert_tree_entry(new_te, &paths);
3252 err = match_deleted_or_modified_ct(&ct, te,
3253 path_base_tree, commitable_paths);
3255 /* NB: Deleted entries get dropped here. */
3256 if (ct->status == GOT_STATUS_MODIFY) {
3257 err = alloc_modified_blob_tree_entry(
3261 err = insert_tree_entry(new_te, &paths);
3265 err = report_ct_status(ct, status_cb,
3270 /* Entry is unchanged; just copy it. */
3271 err = got_object_tree_entry_dup(&new_te, te);
3274 err = insert_tree_entry(new_te, &paths);
3281 /* Write new list of entries; deleted entries have been dropped. */
3282 TAILQ_FOREACH(pe, &paths, entry) {
3283 struct got_tree_entry *te = pe->data;
3284 new_tree_entries.nentries++;
3285 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3287 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3289 got_object_tree_entries_close(&new_tree_entries);
3290 got_pathlist_free(&paths);
3294 static const struct got_error *
3295 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3296 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3298 const struct got_error *err = NULL;
3299 struct got_pathlist_entry *pe;
3301 TAILQ_FOREACH(pe, commitable_paths, entry) {
3302 struct got_fileindex_entry *ie;
3303 struct got_commitable *ct = pe->data;
3305 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3307 if (ct->status == GOT_STATUS_DELETE) {
3308 got_fileindex_entry_remove(fileindex, ie);
3309 got_fileindex_entry_free(ie);
3311 err = got_fileindex_entry_update(ie,
3312 ct->ondisk_path, ct->blob_id->sha1,
3313 new_base_commit_id->sha1, 1);
3315 err = got_fileindex_entry_alloc(&ie,
3316 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3317 new_base_commit_id->sha1);
3320 err = got_fileindex_entry_add(fileindex, ie);
3328 static const struct got_error *
3329 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3330 struct got_object_id *head_commit_id)
3332 const struct got_error *err = NULL;
3333 struct got_object_id *id = NULL;
3334 struct got_commit_object *commit = NULL;
3335 const char *ct_path = ct->in_repo_path;
3337 while (ct_path[0] == '/')
3340 if (ct->status != GOT_STATUS_ADD) {
3341 /* Trivial case: base commit == head commit */
3342 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3345 * Ensure file content which local changes were based
3346 * on matches file content in the branch head.
3348 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3350 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3352 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3354 } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3355 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3357 /* Require that added files don't exist in the branch head. */
3358 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3359 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3361 err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3365 got_object_commit_close(commit);
3370 const struct got_error *
3371 commit_worktree(struct got_object_id **new_commit_id,
3372 struct got_pathlist_head *commitable_paths,
3373 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3374 const char *author, const char *committer,
3375 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3376 got_worktree_status_cb status_cb, void *status_arg,
3377 struct got_repository *repo)
3379 const struct got_error *err = NULL, *unlockerr = NULL;
3380 struct got_pathlist_entry *pe;
3381 const char *head_ref_name = NULL;
3382 struct got_commit_object *head_commit = NULL;
3383 struct got_reference *head_ref2 = NULL;
3384 struct got_object_id *head_commit_id2 = NULL;
3385 struct got_tree_object *head_tree = NULL;
3386 struct got_object_id *new_tree_id = NULL;
3387 struct got_object_id_queue parent_ids;
3388 struct got_object_qid *pid = NULL;
3389 char *logmsg = NULL;
3391 *new_commit_id = NULL;
3393 SIMPLEQ_INIT(&parent_ids);
3395 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3399 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3403 if (commit_msg_cb != NULL) {
3404 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3409 if (logmsg == NULL || strlen(logmsg) == 0) {
3410 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3414 /* Create blobs from added and modified files and record their IDs. */
3415 TAILQ_FOREACH(pe, commitable_paths, entry) {
3416 struct got_commitable *ct = pe->data;
3419 if (ct->status != GOT_STATUS_ADD &&
3420 ct->status != GOT_STATUS_MODIFY)
3423 if (asprintf(&ondisk_path, "%s/%s",
3424 worktree->root_path, pe->path) == -1) {
3425 err = got_error_from_errno("asprintf");
3428 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3434 /* Recursively write new tree objects. */
3435 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3436 status_cb, status_arg, repo);
3440 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3443 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3444 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3445 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3446 got_object_qid_free(pid);
3452 /* Check if a concurrent commit to our branch has occurred. */
3453 head_ref_name = got_worktree_get_head_ref_name(worktree);
3454 if (head_ref_name == NULL) {
3455 err = got_error_from_errno("got_worktree_get_head_ref_name");
3458 /* Lock the reference here to prevent concurrent modification. */
3459 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3462 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3465 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3466 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3469 /* Update branch head in repository. */
3470 err = got_ref_change_ref(head_ref2, *new_commit_id);
3473 err = got_ref_write(head_ref2, repo);
3477 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3481 err = ref_base_commit(worktree, repo);
3486 got_object_tree_close(head_tree);
3488 got_object_commit_close(head_commit);
3489 free(head_commit_id2);
3491 unlockerr = got_ref_unlock(head_ref2);
3492 if (unlockerr && err == NULL)
3494 got_ref_close(head_ref2);
3499 static const struct got_error *
3500 check_path_is_commitable(const char *path,
3501 struct got_pathlist_head *commitable_paths)
3503 struct got_pathlist_entry *cpe = NULL;
3504 size_t path_len = strlen(path);
3506 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3507 struct got_commitable *ct = cpe->data;
3508 const char *ct_path = ct->path;
3510 while (ct_path[0] == '/')
3513 if (strcmp(path, ct_path) == 0 ||
3514 got_path_is_child(ct_path, path, path_len))
3519 return got_error_path(path, GOT_ERR_BAD_PATH);
3524 const struct got_error *
3525 got_worktree_commit(struct got_object_id **new_commit_id,
3526 struct got_worktree *worktree, struct got_pathlist_head *paths,
3527 const char *author, const char *committer,
3528 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3529 got_worktree_status_cb status_cb, void *status_arg,
3530 struct got_repository *repo)
3532 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3533 struct got_fileindex *fileindex = NULL;
3534 char *fileindex_path = NULL;
3535 struct got_pathlist_head commitable_paths;
3536 struct collect_commitables_arg cc_arg;
3537 struct got_pathlist_entry *pe;
3538 struct got_reference *head_ref = NULL;
3539 struct got_object_id *head_commit_id = NULL;
3541 *new_commit_id = NULL;
3543 TAILQ_INIT(&commitable_paths);
3545 err = lock_worktree(worktree, LOCK_EX);
3549 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3553 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3557 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3561 cc_arg.commitable_paths = &commitable_paths;
3562 cc_arg.worktree = worktree;
3564 TAILQ_FOREACH(pe, paths, entry) {
3565 err = worktree_status(worktree, pe->path, fileindex, repo,
3566 collect_commitables, &cc_arg, NULL, NULL);
3571 if (TAILQ_EMPTY(&commitable_paths)) {
3572 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3576 TAILQ_FOREACH(pe, paths, entry) {
3577 err = check_path_is_commitable(pe->path, &commitable_paths);
3582 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3583 struct got_commitable *ct = pe->data;
3584 err = check_ct_out_of_date(ct, repo, head_commit_id);
3589 err = commit_worktree(new_commit_id, &commitable_paths,
3590 head_commit_id, worktree, author, committer,
3591 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3595 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3597 sync_err = sync_fileindex(fileindex, fileindex_path);
3598 if (sync_err && err == NULL)
3602 got_fileindex_free(fileindex);
3603 free(fileindex_path);
3604 unlockerr = lock_worktree(worktree, LOCK_SH);
3605 if (unlockerr && err == NULL)
3607 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3608 struct got_commitable *ct = pe->data;
3609 free_commitable(ct);
3611 got_pathlist_free(&commitable_paths);
3616 got_commitable_get_path(struct got_commitable *ct)
3622 got_commitable_get_status(struct got_commitable *ct)
3627 struct check_rebase_ok_arg {
3628 struct got_worktree *worktree;
3629 struct got_repository *repo;
3632 static const struct got_error *
3633 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3635 const struct got_error *err = NULL;
3636 struct check_rebase_ok_arg *a = arg;
3637 unsigned char status;
3641 /* Reject rebase of a work tree with mixed base commits. */
3642 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3643 SHA1_DIGEST_LENGTH))
3644 return got_error(GOT_ERR_MIXED_COMMITS);
3646 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3648 return got_error_from_errno("asprintf");
3650 /* Reject rebase of a work tree with modified or conflicted files. */
3651 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3656 if (status != GOT_STATUS_NO_CHANGE)
3657 return got_error(GOT_ERR_MODIFIED);
3662 const struct got_error *
3663 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3664 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3665 struct got_worktree *worktree, struct got_reference *branch,
3666 struct got_repository *repo)
3668 const struct got_error *err = NULL;
3669 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3670 char *branch_ref_name = NULL;
3671 char *fileindex_path = NULL;
3672 struct check_rebase_ok_arg ok_arg;
3673 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3675 *new_base_branch_ref = NULL;
3679 err = lock_worktree(worktree, LOCK_EX);
3683 err = open_fileindex(fileindex, &fileindex_path, worktree);
3687 ok_arg.worktree = worktree;
3689 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3694 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3698 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3702 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3706 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3711 err = got_ref_alloc_symref(new_base_branch_ref,
3712 new_base_branch_ref_name, wt_branch);
3715 err = got_ref_write(*new_base_branch_ref, repo);
3719 /* TODO Lock original branch's ref while rebasing? */
3721 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3725 err = got_ref_write(branch_ref, repo);
3729 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3730 worktree->base_commit_id);
3733 err = got_ref_write(*tmp_branch, repo);
3737 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3741 free(fileindex_path);
3742 free(tmp_branch_name);
3743 free(new_base_branch_ref_name);
3744 free(branch_ref_name);
3746 got_ref_close(branch_ref);
3748 got_ref_close(wt_branch);
3750 if (*new_base_branch_ref) {
3751 got_ref_close(*new_base_branch_ref);
3752 *new_base_branch_ref = NULL;
3755 got_ref_close(*tmp_branch);
3759 got_fileindex_free(*fileindex);
3762 lock_worktree(worktree, LOCK_SH);
3767 const struct got_error *
3768 got_worktree_rebase_continue(struct got_object_id **commit_id,
3769 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3770 struct got_reference **branch, struct got_fileindex **fileindex,
3771 struct got_worktree *worktree, struct got_repository *repo)
3773 const struct got_error *err;
3774 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3775 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3776 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3777 char *fileindex_path = NULL;
3780 *new_base_branch = NULL;
3785 err = lock_worktree(worktree, LOCK_EX);
3789 err = open_fileindex(fileindex, &fileindex_path, worktree);
3793 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3797 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3801 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3805 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3809 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3813 err = got_ref_open(branch, repo,
3814 got_ref_get_symref_target(branch_ref), 0);
3818 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3822 err = got_ref_resolve(commit_id, repo, commit_ref);
3826 err = got_ref_open(new_base_branch, repo,
3827 new_base_branch_ref_name, 0);
3831 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3835 free(commit_ref_name);
3836 free(branch_ref_name);
3837 free(fileindex_path);
3839 got_ref_close(commit_ref);
3841 got_ref_close(branch_ref);
3846 got_ref_close(*tmp_branch);
3849 if (*new_base_branch) {
3850 got_ref_close(*new_base_branch);
3851 *new_base_branch = NULL;
3854 got_ref_close(*branch);
3858 got_fileindex_free(*fileindex);
3861 lock_worktree(worktree, LOCK_SH);
3866 const struct got_error *
3867 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3869 const struct got_error *err;
3870 char *tmp_branch_name = NULL;
3872 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3876 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3877 free(tmp_branch_name);
3881 static const struct got_error *
3882 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3883 char **logmsg, void *arg)
3889 static const struct got_error *
3890 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
3891 const char *path, struct got_object_id *blob_id,
3892 struct got_object_id *commit_id)
3897 struct collect_merged_paths_arg {
3898 got_worktree_checkout_cb progress_cb;
3900 struct got_pathlist_head *merged_paths;
3903 static const struct got_error *
3904 collect_merged_paths(void *arg, unsigned char status, const char *path)
3906 const struct got_error *err;
3907 struct collect_merged_paths_arg *a = arg;
3909 struct got_pathlist_entry *new;
3911 err = (*a->progress_cb)(a->progress_arg, status, path);
3915 if (status != GOT_STATUS_MERGE &&
3916 status != GOT_STATUS_ADD &&
3917 status != GOT_STATUS_DELETE &&
3918 status != GOT_STATUS_CONFLICT)
3923 return got_error_from_errno("strdup");
3925 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3926 if (err || new == NULL)
3932 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3934 struct got_pathlist_entry *pe;
3936 TAILQ_FOREACH(pe, merged_paths, entry)
3937 free((char *)pe->path);
3939 got_pathlist_free(merged_paths);
3942 static const struct got_error *
3943 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3944 struct got_repository *repo)
3946 const struct got_error *err;
3947 struct got_reference *commit_ref = NULL;
3949 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3951 if (err->code != GOT_ERR_NOT_REF)
3953 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3956 err = got_ref_write(commit_ref, repo);
3960 struct got_object_id *stored_id;
3963 err = got_ref_resolve(&stored_id, repo, commit_ref);
3966 cmp = got_object_id_cmp(commit_id, stored_id);
3969 err = got_error(GOT_ERR_REBASE_COMMITID);
3975 got_ref_close(commit_ref);
3979 static const struct got_error *
3980 rebase_merge_files(struct got_pathlist_head *merged_paths,
3981 const char *commit_ref_name, struct got_worktree *worktree,
3982 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3983 struct got_object_id *commit_id, struct got_repository *repo,
3984 got_worktree_checkout_cb progress_cb, void *progress_arg,
3985 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3987 const struct got_error *err;
3988 struct got_reference *commit_ref = NULL;
3989 struct collect_merged_paths_arg cmp_arg;
3990 char *fileindex_path;
3992 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3994 err = get_fileindex_path(&fileindex_path, worktree);
3998 cmp_arg.progress_cb = progress_cb;
3999 cmp_arg.progress_arg = progress_arg;
4000 cmp_arg.merged_paths = merged_paths;
4001 err = merge_files(worktree, fileindex, fileindex_path,
4002 parent_commit_id, commit_id, repo, collect_merged_paths,
4003 &cmp_arg, cancel_cb, cancel_arg);
4005 got_ref_close(commit_ref);
4009 const struct got_error *
4010 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4011 struct got_worktree *worktree, struct got_fileindex *fileindex,
4012 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4013 struct got_repository *repo,
4014 got_worktree_checkout_cb progress_cb, void *progress_arg,
4015 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4017 const struct got_error *err;
4018 char *commit_ref_name;
4020 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4024 err = store_commit_id(commit_ref_name, commit_id, repo);
4028 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4029 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4030 progress_arg, cancel_cb, cancel_arg);
4032 free(commit_ref_name);
4036 const struct got_error *
4037 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4038 struct got_worktree *worktree, struct got_fileindex *fileindex,
4039 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4040 struct got_repository *repo,
4041 got_worktree_checkout_cb progress_cb, void *progress_arg,
4042 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4044 const struct got_error *err;
4045 char *commit_ref_name;
4047 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4051 err = store_commit_id(commit_ref_name, commit_id, repo);
4055 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4056 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4057 progress_arg, cancel_cb, cancel_arg);
4059 free(commit_ref_name);
4063 static const struct got_error *
4064 rebase_commit(struct got_object_id **new_commit_id,
4065 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4066 struct got_worktree *worktree, struct got_fileindex *fileindex,
4067 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4068 const char *new_logmsg, struct got_repository *repo)
4070 const struct got_error *err, *sync_err;
4071 struct got_pathlist_head commitable_paths;
4072 struct collect_commitables_arg cc_arg;
4073 char *fileindex_path = NULL;
4074 struct got_reference *head_ref = NULL;
4075 struct got_object_id *head_commit_id = NULL;
4076 char *logmsg = NULL;
4078 TAILQ_INIT(&commitable_paths);
4079 *new_commit_id = NULL;
4081 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4083 err = get_fileindex_path(&fileindex_path, worktree);
4087 cc_arg.commitable_paths = &commitable_paths;
4088 cc_arg.worktree = worktree;
4091 * If possible get the status of individual files directly to
4092 * avoid crawling the entire work tree once per rebased commit.
4093 * TODO: Ideally, merged_paths would contain a list of commitables
4094 * we could use so we could skip worktree_status() entirely.
4097 struct got_pathlist_entry *pe;
4098 if (TAILQ_EMPTY(merged_paths)) {
4099 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4102 TAILQ_FOREACH(pe, merged_paths, entry) {
4103 err = worktree_status(worktree, pe->path, fileindex,
4104 repo, collect_commitables, &cc_arg, NULL, NULL);
4109 err = worktree_status(worktree, "", fileindex, repo,
4110 collect_commitables, &cc_arg, NULL, NULL);
4115 if (TAILQ_EMPTY(&commitable_paths)) {
4116 /* No-op change; commit will be elided. */
4117 err = got_ref_delete(commit_ref, repo);
4120 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4124 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4128 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4133 logmsg = strdup(new_logmsg);
4135 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4137 return got_error_from_errno("strdup");
4139 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4140 worktree, got_object_commit_get_author(orig_commit),
4141 got_object_commit_get_committer(orig_commit),
4142 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4146 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4150 err = got_ref_delete(commit_ref, repo);
4154 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4156 sync_err = sync_fileindex(fileindex, fileindex_path);
4157 if (sync_err && err == NULL)
4160 free(fileindex_path);
4161 free(head_commit_id);
4163 got_ref_close(head_ref);
4165 free(*new_commit_id);
4166 *new_commit_id = NULL;
4171 const struct got_error *
4172 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4173 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4174 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4175 struct got_commit_object *orig_commit,
4176 struct got_object_id *orig_commit_id, struct got_repository *repo)
4178 const struct got_error *err;
4179 char *commit_ref_name;
4180 struct got_reference *commit_ref = NULL;
4181 struct got_object_id *commit_id = NULL;
4183 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4187 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4190 err = got_ref_resolve(&commit_id, repo, commit_ref);
4193 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4194 err = got_error(GOT_ERR_REBASE_COMMITID);
4198 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4199 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4202 got_ref_close(commit_ref);
4203 free(commit_ref_name);
4208 const struct got_error *
4209 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4210 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4211 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4212 struct got_commit_object *orig_commit,
4213 struct got_object_id *orig_commit_id, const char *new_logmsg,
4214 struct got_repository *repo)
4216 const struct got_error *err;
4217 char *commit_ref_name;
4218 struct got_reference *commit_ref = NULL;
4219 struct got_object_id *commit_id = NULL;
4221 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4225 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4228 err = got_ref_resolve(&commit_id, repo, commit_ref);
4231 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4232 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4236 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4237 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4240 got_ref_close(commit_ref);
4241 free(commit_ref_name);
4246 const struct got_error *
4247 got_worktree_rebase_postpone(struct got_worktree *worktree,
4248 struct got_fileindex *fileindex)
4251 got_fileindex_free(fileindex);
4252 return lock_worktree(worktree, LOCK_SH);
4255 static const struct got_error *
4256 delete_ref(const char *name, struct got_repository *repo)
4258 const struct got_error *err;
4259 struct got_reference *ref;
4261 err = got_ref_open(&ref, repo, name, 0);
4263 if (err->code == GOT_ERR_NOT_REF)
4268 err = got_ref_delete(ref, repo);
4273 static const struct got_error *
4274 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4276 const struct got_error *err;
4277 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4278 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4280 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4283 err = delete_ref(tmp_branch_name, repo);
4287 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4290 err = delete_ref(new_base_branch_ref_name, repo);
4294 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4297 err = delete_ref(branch_ref_name, repo);
4301 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4304 err = delete_ref(commit_ref_name, repo);
4309 free(tmp_branch_name);
4310 free(new_base_branch_ref_name);
4311 free(branch_ref_name);
4312 free(commit_ref_name);
4316 const struct got_error *
4317 got_worktree_rebase_complete(struct got_worktree *worktree,
4318 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4319 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4320 struct got_repository *repo)
4322 const struct got_error *err, *unlockerr;
4323 struct got_object_id *new_head_commit_id = NULL;
4325 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4329 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4333 err = got_ref_write(rebased_branch, repo);
4337 err = got_worktree_set_head_ref(worktree, rebased_branch);
4341 err = delete_rebase_refs(worktree, repo);
4344 got_fileindex_free(fileindex);
4345 free(new_head_commit_id);
4346 unlockerr = lock_worktree(worktree, LOCK_SH);
4347 if (unlockerr && err == NULL)
4352 struct collect_revertible_paths_arg {
4353 struct got_pathlist_head *revertible_paths;
4354 struct got_worktree *worktree;
4357 static const struct got_error *
4358 collect_revertible_paths(void *arg, unsigned char status,
4359 unsigned char staged_status, const char *relpath,
4360 struct got_object_id *blob_id, struct got_object_id *commit_id)
4362 struct collect_revertible_paths_arg *a = arg;
4363 const struct got_error *err = NULL;
4364 struct got_pathlist_entry *new = NULL;
4367 if (status != GOT_STATUS_ADD &&
4368 status != GOT_STATUS_DELETE &&
4369 status != GOT_STATUS_MODIFY &&
4370 status != GOT_STATUS_CONFLICT &&
4371 status != GOT_STATUS_MISSING)
4374 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4375 return got_error_from_errno("asprintf");
4377 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4378 if (err || new == NULL)
4383 const struct got_error *
4384 got_worktree_rebase_abort(struct got_worktree *worktree,
4385 struct got_fileindex *fileindex, struct got_repository *repo,
4386 struct got_reference *new_base_branch,
4387 got_worktree_checkout_cb progress_cb, void *progress_arg)
4389 const struct got_error *err, *unlockerr, *sync_err;
4390 struct got_reference *resolved = NULL;
4391 struct got_object_id *commit_id = NULL;
4392 char *fileindex_path = NULL;
4393 struct got_pathlist_head revertible_paths;
4394 struct got_pathlist_entry *pe;
4395 struct collect_revertible_paths_arg crp_arg;
4396 struct got_object_id *tree_id = NULL;
4398 TAILQ_INIT(&revertible_paths);
4400 err = lock_worktree(worktree, LOCK_EX);
4404 err = got_ref_open(&resolved, repo,
4405 got_ref_get_symref_target(new_base_branch), 0);
4409 err = got_worktree_set_head_ref(worktree, resolved);
4414 * XXX commits to the base branch could have happened while
4415 * we were busy rebasing; should we store the original commit ID
4416 * when rebase begins and read it back here?
4418 err = got_ref_resolve(&commit_id, repo, resolved);
4422 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4426 err = got_object_id_by_path(&tree_id, repo,
4427 worktree->base_commit_id, worktree->path_prefix);
4431 err = delete_rebase_refs(worktree, repo);
4435 err = get_fileindex_path(&fileindex_path, worktree);
4439 crp_arg.revertible_paths = &revertible_paths;
4440 crp_arg.worktree = worktree;
4441 err = worktree_status(worktree, "", fileindex, repo,
4442 collect_revertible_paths, &crp_arg, NULL, NULL);
4446 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4447 err = revert_file(worktree, fileindex, pe->path,
4448 progress_cb, progress_arg, repo);
4453 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4454 repo, progress_cb, progress_arg, NULL, NULL);
4456 sync_err = sync_fileindex(fileindex, fileindex_path);
4457 if (sync_err && err == NULL)
4460 got_ref_close(resolved);
4464 got_fileindex_free(fileindex);
4465 free(fileindex_path);
4466 TAILQ_FOREACH(pe, &revertible_paths, entry)
4467 free((char *)pe->path);
4468 got_pathlist_free(&revertible_paths);
4470 unlockerr = lock_worktree(worktree, LOCK_SH);
4471 if (unlockerr && err == NULL)
4476 const struct got_error *
4477 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4478 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4479 struct got_fileindex **fileindex, struct got_worktree *worktree,
4480 struct got_repository *repo)
4482 const struct got_error *err = NULL;
4483 char *tmp_branch_name = NULL;
4484 char *branch_ref_name = NULL;
4485 char *base_commit_ref_name = NULL;
4486 char *fileindex_path = NULL;
4487 struct check_rebase_ok_arg ok_arg;
4488 struct got_reference *wt_branch = NULL;
4489 struct got_reference *base_commit_ref = NULL;
4493 *base_commit_id = NULL;
4496 err = lock_worktree(worktree, LOCK_EX);
4500 err = open_fileindex(fileindex, &fileindex_path, worktree);
4504 ok_arg.worktree = worktree;
4506 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4511 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4515 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4519 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4524 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4529 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4533 err = got_ref_write(*branch_ref, repo);
4537 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4538 worktree->base_commit_id);
4541 err = got_ref_write(base_commit_ref, repo);
4544 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4545 if (*base_commit_id == NULL) {
4546 err = got_error_from_errno("got_object_id_dup");
4550 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4551 worktree->base_commit_id);
4554 err = got_ref_write(*tmp_branch, repo);
4558 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4562 free(fileindex_path);
4563 free(tmp_branch_name);
4564 free(branch_ref_name);
4565 free(base_commit_ref_name);
4567 got_ref_close(wt_branch);
4570 got_ref_close(*branch_ref);
4574 got_ref_close(*tmp_branch);
4577 free(*base_commit_id);
4579 got_fileindex_free(*fileindex);
4582 lock_worktree(worktree, LOCK_SH);
4587 const struct got_error *
4588 got_worktree_histedit_postpone(struct got_worktree *worktree,
4589 struct got_fileindex *fileindex)
4592 got_fileindex_free(fileindex);
4593 return lock_worktree(worktree, LOCK_SH);
4596 const struct got_error *
4597 got_worktree_histedit_in_progress(int *in_progress,
4598 struct got_worktree *worktree)
4600 const struct got_error *err;
4601 char *tmp_branch_name = NULL;
4603 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4607 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4608 free(tmp_branch_name);
4612 const struct got_error *
4613 got_worktree_histedit_continue(struct got_object_id **commit_id,
4614 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4615 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4616 struct got_worktree *worktree, struct got_repository *repo)
4618 const struct got_error *err;
4619 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4620 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4621 struct got_reference *commit_ref = NULL;
4622 struct got_reference *base_commit_ref = NULL;
4623 char *fileindex_path = NULL;
4627 *base_commit_id = NULL;
4630 err = lock_worktree(worktree, LOCK_EX);
4634 err = open_fileindex(fileindex, &fileindex_path, worktree);
4638 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4642 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4646 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4650 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4655 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4659 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4662 err = got_ref_resolve(commit_id, repo, commit_ref);
4666 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4669 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4673 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4677 free(commit_ref_name);
4678 free(branch_ref_name);
4679 free(fileindex_path);
4681 got_ref_close(commit_ref);
4682 if (base_commit_ref)
4683 got_ref_close(base_commit_ref);
4687 free(*base_commit_id);
4688 *base_commit_id = NULL;
4690 got_ref_close(*tmp_branch);
4694 got_fileindex_free(*fileindex);
4697 lock_worktree(worktree, LOCK_EX);
4702 static const struct got_error *
4703 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4705 const struct got_error *err;
4706 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4707 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4709 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4712 err = delete_ref(tmp_branch_name, repo);
4716 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4720 err = delete_ref(base_commit_ref_name, repo);
4724 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4727 err = delete_ref(branch_ref_name, repo);
4731 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4734 err = delete_ref(commit_ref_name, repo);
4738 free(tmp_branch_name);
4739 free(base_commit_ref_name);
4740 free(branch_ref_name);
4741 free(commit_ref_name);
4745 const struct got_error *
4746 got_worktree_histedit_abort(struct got_worktree *worktree,
4747 struct got_fileindex *fileindex, struct got_repository *repo,
4748 struct got_reference *branch, struct got_object_id *base_commit_id,
4749 got_worktree_checkout_cb progress_cb, void *progress_arg)
4751 const struct got_error *err, *unlockerr, *sync_err;
4752 struct got_reference *resolved = NULL;
4753 char *fileindex_path = NULL;
4754 struct got_pathlist_head revertible_paths;
4755 struct got_pathlist_entry *pe;
4756 struct collect_revertible_paths_arg crp_arg;
4757 struct got_object_id *tree_id = NULL;
4759 TAILQ_INIT(&revertible_paths);
4761 err = lock_worktree(worktree, LOCK_EX);
4765 err = got_ref_open(&resolved, repo,
4766 got_ref_get_symref_target(branch), 0);
4770 err = got_worktree_set_head_ref(worktree, resolved);
4774 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4778 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4779 worktree->path_prefix);
4783 err = delete_histedit_refs(worktree, repo);
4787 err = get_fileindex_path(&fileindex_path, worktree);
4791 crp_arg.revertible_paths = &revertible_paths;
4792 crp_arg.worktree = worktree;
4793 err = worktree_status(worktree, "", fileindex, repo,
4794 collect_revertible_paths, &crp_arg, NULL, NULL);
4798 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4799 err = revert_file(worktree, fileindex, pe->path,
4800 progress_cb, progress_arg, repo);
4805 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4806 repo, progress_cb, progress_arg, NULL, NULL);
4808 sync_err = sync_fileindex(fileindex, fileindex_path);
4809 if (sync_err && err == NULL)
4812 got_ref_close(resolved);
4814 free(fileindex_path);
4815 TAILQ_FOREACH(pe, &revertible_paths, entry)
4816 free((char *)pe->path);
4817 got_pathlist_free(&revertible_paths);
4819 unlockerr = lock_worktree(worktree, LOCK_SH);
4820 if (unlockerr && err == NULL)
4825 const struct got_error *
4826 got_worktree_histedit_complete(struct got_worktree *worktree,
4827 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4828 struct got_reference *edited_branch, struct got_repository *repo)
4830 const struct got_error *err, *unlockerr;
4831 struct got_object_id *new_head_commit_id = NULL;
4832 struct got_reference *resolved = NULL;
4834 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4838 err = got_ref_open(&resolved, repo,
4839 got_ref_get_symref_target(edited_branch), 0);
4843 err = got_ref_change_ref(resolved, new_head_commit_id);
4847 err = got_ref_write(resolved, repo);
4851 err = got_worktree_set_head_ref(worktree, resolved);
4855 err = delete_histedit_refs(worktree, repo);
4858 got_fileindex_free(fileindex);
4859 free(new_head_commit_id);
4860 unlockerr = lock_worktree(worktree, LOCK_SH);
4861 if (unlockerr && err == NULL)
4866 const struct got_error *
4867 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4868 struct got_object_id *commit_id, struct got_repository *repo)
4870 const struct got_error *err;
4871 char *commit_ref_name;
4873 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4877 err = store_commit_id(commit_ref_name, commit_id, repo);
4881 err = delete_ref(commit_ref_name, repo);
4883 free(commit_ref_name);
4887 static const struct got_error *
4888 stage_path(const char *relpath, const char *ondisk_path,
4889 const char *path_content, struct got_worktree *worktree,
4890 struct got_fileindex *fileindex, struct got_repository *repo,
4891 got_worktree_status_cb status_cb, void *status_arg)
4893 const struct got_error *err = NULL;
4894 struct got_fileindex_entry *ie;
4895 unsigned char status;
4897 struct got_object_id *blob_id = NULL;
4900 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
4902 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4906 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
4911 case GOT_STATUS_ADD:
4912 case GOT_STATUS_MODIFY:
4913 err = got_object_blob_create(&blob_id,
4914 path_content ? path_content : ondisk_path, repo);
4917 memcpy(ie->staged_blob_sha1, blob_id->sha1,
4918 SHA1_DIGEST_LENGTH);
4919 if (status == GOT_STATUS_ADD)
4920 stage = GOT_FILEIDX_STAGE_ADD;
4922 stage = GOT_FILEIDX_STAGE_MODIFY;
4924 case GOT_STATUS_DELETE:
4925 stage = GOT_FILEIDX_STAGE_DELETE;
4928 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4932 got_fileindex_entry_stage_set(ie, stage);
4933 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4934 get_staged_status(ie), relpath, blob_id, NULL);
4940 const struct got_error *
4941 got_worktree_stage(struct got_worktree *worktree,
4942 struct got_pathlist_head *paths,
4943 got_worktree_status_cb status_cb, void *status_arg,
4944 struct got_repository *repo)
4946 const struct got_error *err = NULL, *sync_err, *unlockerr;
4947 struct got_pathlist_entry *pe;
4948 struct got_fileindex *fileindex = NULL;
4949 char *fileindex_path = NULL;
4951 err = lock_worktree(worktree, LOCK_EX);
4955 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4959 TAILQ_FOREACH(pe, paths, entry) {
4961 err = got_path_skip_common_ancestor(&relpath,
4962 got_worktree_get_root_path(worktree), pe->path);
4965 err = stage_path(relpath, pe->path,
4966 (const char *)pe->data, worktree, fileindex, repo,
4967 status_cb, status_arg);
4973 sync_err = sync_fileindex(fileindex, fileindex_path);
4974 if (sync_err && err == NULL)
4977 free(fileindex_path);
4979 got_fileindex_free(fileindex);
4980 unlockerr = lock_worktree(worktree, LOCK_SH);
4981 if (unlockerr && err == NULL)