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, staged_blob_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 if (staged_status == GOT_STATUS_ADD ||
2220 staged_status == GOT_STATUS_MODIFY) {
2221 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2222 SHA1_DIGEST_LENGTH);
2223 err = (*status_cb)(status_arg, status, staged_status,
2224 ie->path, &blob_id, &staged_blob_id, &commit_id);
2226 err = (*status_cb)(status_arg, status, staged_status,
2227 ie->path, &blob_id, NULL, &commit_id);
2232 static const struct got_error *
2233 status_old_new(void *arg, struct got_fileindex_entry *ie,
2234 struct dirent *de, const char *parent_path)
2236 const struct got_error *err = NULL;
2237 struct diff_dir_cb_arg *a = arg;
2240 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2241 return got_error(GOT_ERR_CANCELLED);
2243 if (got_path_cmp(parent_path, a->status_path,
2244 strlen(parent_path), a->status_path_len) != 0 &&
2245 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2248 if (parent_path[0]) {
2249 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2250 parent_path, de->d_name) == -1)
2251 return got_error_from_errno("asprintf");
2253 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2255 return got_error_from_errno("asprintf");
2258 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2264 static const struct got_error *
2265 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2267 struct diff_dir_cb_arg *a = arg;
2268 struct got_object_id blob_id, commit_id;
2269 unsigned char status;
2271 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2272 return got_error(GOT_ERR_CANCELLED);
2274 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2277 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2278 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2279 if (got_fileindex_entry_has_file_on_disk(ie))
2280 status = GOT_STATUS_MISSING;
2282 status = GOT_STATUS_DELETE;
2283 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2284 ie->path, &blob_id, NULL, &commit_id);
2287 static const struct got_error *
2288 status_new(void *arg, struct dirent *de, const char *parent_path)
2290 const struct got_error *err = NULL;
2291 struct diff_dir_cb_arg *a = arg;
2294 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2295 return got_error(GOT_ERR_CANCELLED);
2297 if (de->d_type == DT_DIR)
2300 /* XXX ignore symlinks for now */
2301 if (de->d_type == DT_LNK)
2304 if (parent_path[0]) {
2305 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2306 return got_error_from_errno("asprintf");
2311 if (got_path_is_child(path, a->status_path, a->status_path_len))
2312 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2313 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2319 static const struct got_error *
2320 report_single_file_status(const char *path, const char *ondisk_path,
2321 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2322 void *status_arg, struct got_repository *repo)
2324 struct got_fileindex_entry *ie;
2327 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2329 return report_file_status(ie, ondisk_path, status_cb,
2332 if (lstat(ondisk_path, &sb) == -1) {
2333 if (errno != ENOENT)
2334 return got_error_from_errno2("lstat", ondisk_path);
2338 if (S_ISREG(sb.st_mode))
2339 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2340 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2345 static const struct got_error *
2346 worktree_status(struct got_worktree *worktree, const char *path,
2347 struct got_fileindex *fileindex, struct got_repository *repo,
2348 got_worktree_status_cb status_cb, void *status_arg,
2349 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2351 const struct got_error *err = NULL;
2352 DIR *workdir = NULL;
2353 struct got_fileindex_diff_dir_cb fdiff_cb;
2354 struct diff_dir_cb_arg arg;
2355 char *ondisk_path = NULL;
2357 if (asprintf(&ondisk_path, "%s%s%s",
2358 worktree->root_path, path[0] ? "/" : "", path) == -1)
2359 return got_error_from_errno("asprintf");
2361 workdir = opendir(ondisk_path);
2362 if (workdir == NULL) {
2363 if (errno != ENOTDIR && errno != ENOENT)
2364 err = got_error_from_errno2("opendir", ondisk_path);
2366 err = report_single_file_status(path, ondisk_path,
2367 fileindex, status_cb, status_arg, repo);
2369 fdiff_cb.diff_old_new = status_old_new;
2370 fdiff_cb.diff_old = status_old;
2371 fdiff_cb.diff_new = status_new;
2372 arg.fileindex = fileindex;
2373 arg.worktree = worktree;
2374 arg.status_path = path;
2375 arg.status_path_len = strlen(path);
2377 arg.status_cb = status_cb;
2378 arg.status_arg = status_arg;
2379 arg.cancel_cb = cancel_cb;
2380 arg.cancel_arg = cancel_arg;
2381 err = got_fileindex_diff_dir(fileindex, workdir,
2382 worktree->root_path, path, repo, &fdiff_cb, &arg);
2391 const struct got_error *
2392 got_worktree_status(struct got_worktree *worktree,
2393 struct got_pathlist_head *paths, struct got_repository *repo,
2394 got_worktree_status_cb status_cb, void *status_arg,
2395 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2397 const struct got_error *err = NULL;
2398 char *fileindex_path = NULL;
2399 struct got_fileindex *fileindex = NULL;
2400 struct got_pathlist_entry *pe;
2402 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2406 TAILQ_FOREACH(pe, paths, entry) {
2407 err = worktree_status(worktree, pe->path, fileindex, repo,
2408 status_cb, status_arg, cancel_cb, cancel_arg);
2412 free(fileindex_path);
2413 got_fileindex_free(fileindex);
2417 const struct got_error *
2418 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2421 const struct got_error *err = NULL;
2422 char *resolved, *cwd = NULL, *path = NULL;
2427 resolved = realpath(arg, NULL);
2428 if (resolved == NULL) {
2429 if (errno != ENOENT)
2430 return got_error_from_errno2("realpath", arg);
2431 cwd = getcwd(NULL, 0);
2433 return got_error_from_errno("getcwd");
2434 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2435 err = got_error_from_errno("asprintf");
2440 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2441 strlen(got_worktree_get_root_path(worktree)))) {
2442 err = got_error(GOT_ERR_BAD_PATH);
2446 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2447 err = got_path_skip_common_ancestor(&path,
2448 got_worktree_get_root_path(worktree), resolved);
2454 err = got_error_from_errno("strdup");
2459 /* XXX status walk can't deal with trailing slash! */
2461 while (len > 0 && path[len - 1] == '/') {
2462 path[len - 1] = '\0';
2475 static const struct got_error *
2476 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2477 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2478 struct got_repository *repo)
2480 const struct got_error *err = NULL;
2481 struct got_fileindex_entry *ie;
2483 /* Re-adding an existing entry is a no-op. */
2484 if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
2487 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2491 err = got_fileindex_entry_add(fileindex, ie);
2493 got_fileindex_entry_free(ie);
2497 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2500 const struct got_error *
2501 got_worktree_schedule_add(struct got_worktree *worktree,
2502 struct got_pathlist_head *ondisk_paths,
2503 got_worktree_status_cb status_cb, void *status_arg,
2504 struct got_repository *repo)
2506 struct got_fileindex *fileindex = NULL;
2507 char *fileindex_path = NULL;
2508 const struct got_error *err = NULL, *sync_err, *unlockerr;
2509 struct got_pathlist_entry *pe;
2511 err = lock_worktree(worktree, LOCK_EX);
2515 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2519 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2521 err = got_path_skip_common_ancestor(&relpath,
2522 got_worktree_get_root_path(worktree), pe->path);
2525 err = schedule_addition(pe->path, fileindex, relpath,
2526 status_cb, status_arg, repo);
2531 sync_err = sync_fileindex(fileindex, fileindex_path);
2532 if (sync_err && err == NULL)
2535 free(fileindex_path);
2537 got_fileindex_free(fileindex);
2538 unlockerr = lock_worktree(worktree, LOCK_SH);
2539 if (unlockerr && err == NULL)
2544 static const struct got_error *
2545 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2546 const char *relpath, int delete_local_mods,
2547 got_worktree_status_cb status_cb, void *status_arg,
2548 struct got_repository *repo)
2550 const struct got_error *err = NULL;
2551 struct got_fileindex_entry *ie = NULL;
2552 unsigned char status;
2555 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2557 return got_error(GOT_ERR_BAD_PATH);
2559 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2563 if (status != GOT_STATUS_NO_CHANGE) {
2564 if (status == GOT_STATUS_DELETE)
2566 if (status != GOT_STATUS_MODIFY)
2567 return got_error(GOT_ERR_FILE_STATUS);
2568 if (!delete_local_mods)
2569 return got_error(GOT_ERR_FILE_MODIFIED);
2572 if (unlink(ondisk_path) != 0)
2573 return got_error_from_errno2("unlink", ondisk_path);
2575 got_fileindex_entry_mark_deleted_from_disk(ie);
2576 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2579 const struct got_error *
2580 got_worktree_schedule_delete(struct got_worktree *worktree,
2581 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2582 got_worktree_status_cb status_cb, void *status_arg,
2583 struct got_repository *repo)
2585 struct got_fileindex *fileindex = NULL;
2586 char *fileindex_path = NULL;
2587 const struct got_error *err = NULL, *sync_err, *unlockerr;
2588 struct got_pathlist_entry *pe;
2590 err = lock_worktree(worktree, LOCK_EX);
2594 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2598 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2600 err = got_path_skip_common_ancestor(&relpath,
2601 got_worktree_get_root_path(worktree), pe->path);
2604 err = schedule_for_deletion(pe->path, fileindex, relpath,
2605 delete_local_mods, status_cb, status_arg, repo);
2610 sync_err = sync_fileindex(fileindex, fileindex_path);
2611 if (sync_err && err == NULL)
2614 free(fileindex_path);
2616 got_fileindex_free(fileindex);
2617 unlockerr = lock_worktree(worktree, LOCK_SH);
2618 if (unlockerr && err == NULL)
2623 static const struct got_error *
2624 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2625 const char *ondisk_path,
2626 got_worktree_checkout_cb progress_cb, void *progress_arg,
2627 struct got_repository *repo)
2629 const struct got_error *err = NULL;
2630 char *relpath = NULL, *parent_path = NULL;
2631 struct got_fileindex_entry *ie;
2632 struct got_tree_object *tree = NULL;
2633 struct got_object_id *tree_id = NULL;
2634 const struct got_tree_entry *te;
2635 char *tree_path = NULL, *te_name;
2636 struct got_blob_object *blob = NULL;
2637 unsigned char status;
2640 err = got_path_skip_common_ancestor(&relpath,
2641 got_worktree_get_root_path(worktree), ondisk_path);
2645 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2647 err = got_error(GOT_ERR_BAD_PATH);
2651 /* Construct in-repository path of tree which contains this blob. */
2652 err = got_path_dirname(&parent_path, ie->path);
2654 if (err->code != GOT_ERR_BAD_PATH)
2656 parent_path = strdup("/");
2657 if (parent_path == NULL) {
2658 err = got_error_from_errno("strdup");
2662 if (got_path_is_root_dir(worktree->path_prefix)) {
2663 tree_path = strdup(parent_path);
2664 if (tree_path == NULL) {
2665 err = got_error_from_errno("strdup");
2669 if (got_path_is_root_dir(parent_path)) {
2670 tree_path = strdup(worktree->path_prefix);
2671 if (tree_path == NULL) {
2672 err = got_error_from_errno("strdup");
2676 if (asprintf(&tree_path, "%s/%s",
2677 worktree->path_prefix, parent_path) == -1) {
2678 err = got_error_from_errno("asprintf");
2684 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2687 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2688 sb.st_mode = got_fileindex_perms_to_st(ie);
2690 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2693 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2694 status == GOT_STATUS_ADD))
2697 err = got_object_open_as_tree(&tree, repo, tree_id);
2701 te_name = basename(ie->path);
2702 if (te_name == NULL) {
2703 err = got_error_from_errno2("basename", ie->path);
2707 te = got_object_tree_find_entry(tree, te_name);
2708 if (te == NULL && status != GOT_STATUS_ADD) {
2709 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2715 case GOT_STATUS_ADD:
2716 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2719 got_fileindex_entry_remove(fileindex, ie);
2721 case GOT_STATUS_DELETE:
2722 case GOT_STATUS_MODIFY:
2723 case GOT_STATUS_CONFLICT:
2724 case GOT_STATUS_MISSING: {
2725 struct got_object_id id;
2726 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2727 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2730 err = install_blob(worktree, ondisk_path, ie->path,
2731 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2735 if (status == GOT_STATUS_DELETE) {
2736 err = update_blob_fileindex_entry(worktree,
2737 fileindex, ie, ondisk_path, ie->path, blob, 1);
2751 got_object_blob_close(blob);
2753 got_object_tree_close(tree);
2758 const struct got_error *
2759 got_worktree_revert(struct got_worktree *worktree,
2760 struct got_pathlist_head *ondisk_paths,
2761 got_worktree_checkout_cb progress_cb, void *progress_arg,
2762 struct got_repository *repo)
2764 struct got_fileindex *fileindex = NULL;
2765 char *fileindex_path = NULL;
2766 const struct got_error *err = NULL, *unlockerr = NULL;
2767 const struct got_error *sync_err = NULL;
2768 struct got_pathlist_entry *pe;
2770 err = lock_worktree(worktree, LOCK_EX);
2774 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2778 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2779 err = revert_file(worktree, fileindex, pe->path,
2780 progress_cb, progress_arg, repo);
2784 sync_err = sync_fileindex(fileindex, fileindex_path);
2785 if (sync_err && err == NULL)
2788 free(fileindex_path);
2790 got_fileindex_free(fileindex);
2791 unlockerr = lock_worktree(worktree, LOCK_SH);
2792 if (unlockerr && err == NULL)
2798 free_commitable(struct got_commitable *ct)
2801 free(ct->in_repo_path);
2802 free(ct->ondisk_path);
2804 free(ct->base_blob_id);
2805 free(ct->base_commit_id);
2809 struct collect_commitables_arg {
2810 struct got_pathlist_head *commitable_paths;
2811 struct got_repository *repo;
2812 struct got_worktree *worktree;
2815 static const struct got_error *
2816 collect_commitables(void *arg, unsigned char status,
2817 unsigned char staged_status, const char *relpath,
2818 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2819 struct got_object_id *commit_id)
2821 struct collect_commitables_arg *a = arg;
2822 const struct got_error *err = NULL;
2823 struct got_commitable *ct = NULL;
2824 struct got_pathlist_entry *new = NULL;
2825 char *parent_path = NULL, *path = NULL;
2828 if (status == GOT_STATUS_CONFLICT)
2829 return got_error(GOT_ERR_COMMIT_CONFLICT);
2831 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2832 status != GOT_STATUS_DELETE)
2835 if (asprintf(&path, "/%s", relpath) == -1) {
2836 err = got_error_from_errno("asprintf");
2839 if (strcmp(path, "/") == 0) {
2840 parent_path = strdup("");
2841 if (parent_path == NULL)
2842 return got_error_from_errno("strdup");
2844 err = got_path_dirname(&parent_path, path);
2849 ct = calloc(1, sizeof(*ct));
2851 err = got_error_from_errno("calloc");
2855 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2857 err = got_error_from_errno("asprintf");
2860 if (status == GOT_STATUS_DELETE) {
2861 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2863 if (lstat(ct->ondisk_path, &sb) != 0) {
2864 err = got_error_from_errno2("lstat", ct->ondisk_path);
2867 ct->mode = sb.st_mode;
2870 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2871 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2873 err = got_error_from_errno("asprintf");
2877 ct->status = status;
2878 ct->blob_id = NULL; /* will be filled in when blob gets created */
2879 if (ct->status != GOT_STATUS_ADD) {
2880 ct->base_blob_id = got_object_id_dup(blob_id);
2881 if (ct->base_blob_id == NULL) {
2882 err = got_error_from_errno("got_object_id_dup");
2885 ct->base_commit_id = got_object_id_dup(commit_id);
2886 if (ct->base_commit_id == NULL) {
2887 err = got_error_from_errno("got_object_id_dup");
2891 ct->path = strdup(path);
2892 if (ct->path == NULL) {
2893 err = got_error_from_errno("strdup");
2896 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2898 if (ct && (err || new == NULL))
2899 free_commitable(ct);
2905 static const struct got_error *write_tree(struct got_object_id **,
2906 struct got_tree_object *, const char *, struct got_pathlist_head *,
2907 got_worktree_status_cb status_cb, void *status_arg,
2908 struct got_repository *);
2910 static const struct got_error *
2911 write_subtree(struct got_object_id **new_subtree_id,
2912 struct got_tree_entry *te, const char *parent_path,
2913 struct got_pathlist_head *commitable_paths,
2914 got_worktree_status_cb status_cb, void *status_arg,
2915 struct got_repository *repo)
2917 const struct got_error *err = NULL;
2918 struct got_tree_object *subtree;
2921 if (asprintf(&subpath, "%s%s%s", parent_path,
2922 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2923 return got_error_from_errno("asprintf");
2925 err = got_object_open_as_tree(&subtree, repo, te->id);
2929 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2930 status_cb, status_arg, repo);
2931 got_object_tree_close(subtree);
2936 static const struct got_error *
2937 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2939 const struct got_error *err = NULL;
2940 char *ct_parent_path = NULL;
2944 if (strchr(ct->in_repo_path, '/') == NULL) {
2945 *match = got_path_is_root_dir(path);
2949 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2952 *match = (strcmp(path, ct_parent_path) == 0);
2953 free(ct_parent_path);
2958 get_ct_file_mode(struct got_commitable *ct)
2960 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2963 static const struct got_error *
2964 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2965 struct got_tree_entry *te, struct got_commitable *ct)
2967 const struct got_error *err = NULL;
2971 err = got_object_tree_entry_dup(new_te, te);
2975 (*new_te)->mode = get_ct_file_mode(ct);
2977 free((*new_te)->id);
2978 (*new_te)->id = got_object_id_dup(ct->blob_id);
2979 if ((*new_te)->id == NULL) {
2980 err = got_error_from_errno("got_object_id_dup");
2984 if (err && *new_te) {
2985 got_object_tree_entry_close(*new_te);
2991 static const struct got_error *
2992 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2993 struct got_commitable *ct)
2995 const struct got_error *err = NULL;
3000 *new_te = calloc(1, sizeof(**new_te));
3001 if (*new_te == NULL)
3002 return got_error_from_errno("calloc");
3004 ct_name = basename(ct->path);
3005 if (ct_name == NULL) {
3006 err = got_error_from_errno2("basename", ct->path);
3009 (*new_te)->name = strdup(ct_name);
3010 if ((*new_te)->name == NULL) {
3011 err = got_error_from_errno("strdup");
3015 (*new_te)->mode = get_ct_file_mode(ct);
3017 (*new_te)->id = got_object_id_dup(ct->blob_id);
3018 if ((*new_te)->id == NULL) {
3019 err = got_error_from_errno("got_object_id_dup");
3023 if (err && *new_te) {
3024 got_object_tree_entry_close(*new_te);
3030 static const struct got_error *
3031 insert_tree_entry(struct got_tree_entry *new_te,
3032 struct got_pathlist_head *paths)
3034 const struct got_error *err = NULL;
3035 struct got_pathlist_entry *new_pe;
3037 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3041 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3045 static const struct got_error *
3046 report_ct_status(struct got_commitable *ct,
3047 got_worktree_status_cb status_cb, void *status_arg)
3049 const char *ct_path = ct->path;
3050 while (ct_path[0] == '/')
3052 return (*status_cb)(status_arg, ct->status, GOT_STATUS_NO_CHANGE,
3053 ct_path, ct->blob_id, NULL, NULL);
3056 static const struct got_error *
3057 match_modified_subtree(int *modified, struct got_tree_entry *te,
3058 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3060 const struct got_error *err = NULL;
3061 struct got_pathlist_entry *pe;
3066 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3067 got_path_is_root_dir(base_tree_path) ? "" : "/",
3069 return got_error_from_errno("asprintf");
3071 TAILQ_FOREACH(pe, commitable_paths, entry) {
3072 struct got_commitable *ct = pe->data;
3073 *modified = got_path_is_child(ct->in_repo_path, te_path,
3083 static const struct got_error *
3084 match_deleted_or_modified_ct(struct got_commitable **ctp,
3085 struct got_tree_entry *te, const char *base_tree_path,
3086 struct got_pathlist_head *commitable_paths)
3088 const struct got_error *err = NULL;
3089 struct got_pathlist_entry *pe;
3093 TAILQ_FOREACH(pe, commitable_paths, entry) {
3094 struct got_commitable *ct = pe->data;
3095 char *ct_name = NULL;
3098 if (ct->status != GOT_STATUS_MODIFY &&
3099 ct->status != GOT_STATUS_DELETE)
3102 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3105 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3111 ct_name = basename(pe->path);
3112 if (ct_name == NULL)
3113 return got_error_from_errno2("basename", pe->path);
3115 if (strcmp(te->name, ct_name) != 0)
3125 static const struct got_error *
3126 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3127 const char *child_path, const char *path_base_tree,
3128 struct got_pathlist_head *commitable_paths,
3129 got_worktree_status_cb status_cb, void *status_arg,
3130 struct got_repository *repo)
3132 const struct got_error *err = NULL;
3133 struct got_tree_entry *new_te;
3138 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3139 got_path_is_root_dir(path_base_tree) ? "" : "/",
3141 return got_error_from_errno("asprintf");
3143 new_te = calloc(1, sizeof(*new_te));
3144 new_te->mode = S_IFDIR;
3145 new_te->name = strdup(child_path);
3146 if (new_te->name == NULL) {
3147 err = got_error_from_errno("strdup");
3148 got_object_tree_entry_close(new_te);
3151 err = write_tree(&new_te->id, NULL, subtree_path,
3152 commitable_paths, status_cb, status_arg, repo);
3154 got_object_tree_entry_close(new_te);
3164 static const struct got_error *
3165 write_tree(struct got_object_id **new_tree_id,
3166 struct got_tree_object *base_tree, const char *path_base_tree,
3167 struct got_pathlist_head *commitable_paths,
3168 got_worktree_status_cb status_cb, void *status_arg,
3169 struct got_repository *repo)
3171 const struct got_error *err = NULL;
3172 const struct got_tree_entries *base_entries = NULL;
3173 struct got_pathlist_head paths;
3174 struct got_tree_entries new_tree_entries;
3175 struct got_tree_entry *te, *new_te = NULL;
3176 struct got_pathlist_entry *pe;
3179 new_tree_entries.nentries = 0;
3180 SIMPLEQ_INIT(&new_tree_entries.head);
3182 /* Insert, and recurse into, newly added entries first. */
3183 TAILQ_FOREACH(pe, commitable_paths, entry) {
3184 struct got_commitable *ct = pe->data;
3185 char *child_path = NULL, *slash;
3187 if (ct->status != GOT_STATUS_ADD ||
3188 (ct->flags & GOT_COMMITABLE_ADDED))
3191 if (!got_path_is_child(pe->path, path_base_tree,
3192 strlen(path_base_tree)))
3195 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3200 slash = strchr(child_path, '/');
3201 if (slash == NULL) {
3202 err = alloc_added_blob_tree_entry(&new_te, ct);
3205 err = report_ct_status(ct, status_cb, status_arg);
3208 ct->flags |= GOT_COMMITABLE_ADDED;
3209 err = insert_tree_entry(new_te, &paths);
3213 *slash = '\0'; /* trim trailing path components */
3214 if (base_tree == NULL ||
3215 got_object_tree_find_entry(base_tree, child_path)
3217 err = make_subtree_for_added_blob(&new_te,
3218 child_path, path_base_tree,
3219 commitable_paths, status_cb, status_arg,
3223 err = insert_tree_entry(new_te, &paths);
3231 /* Handle modified and deleted entries. */
3232 base_entries = got_object_tree_get_entries(base_tree);
3233 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3234 struct got_commitable *ct = NULL;
3236 if (S_ISDIR(te->mode)) {
3238 err = got_object_tree_entry_dup(&new_te, te);
3241 err = match_modified_subtree(&modified, te,
3242 path_base_tree, commitable_paths);
3245 /* Avoid recursion into unmodified subtrees. */
3248 err = write_subtree(&new_te->id, te,
3249 path_base_tree, commitable_paths,
3250 status_cb, status_arg, repo);
3254 err = insert_tree_entry(new_te, &paths);
3260 err = match_deleted_or_modified_ct(&ct, te,
3261 path_base_tree, commitable_paths);
3263 /* NB: Deleted entries get dropped here. */
3264 if (ct->status == GOT_STATUS_MODIFY) {
3265 err = alloc_modified_blob_tree_entry(
3269 err = insert_tree_entry(new_te, &paths);
3273 err = report_ct_status(ct, status_cb,
3278 /* Entry is unchanged; just copy it. */
3279 err = got_object_tree_entry_dup(&new_te, te);
3282 err = insert_tree_entry(new_te, &paths);
3289 /* Write new list of entries; deleted entries have been dropped. */
3290 TAILQ_FOREACH(pe, &paths, entry) {
3291 struct got_tree_entry *te = pe->data;
3292 new_tree_entries.nentries++;
3293 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3295 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3297 got_object_tree_entries_close(&new_tree_entries);
3298 got_pathlist_free(&paths);
3302 static const struct got_error *
3303 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3304 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3306 const struct got_error *err = NULL;
3307 struct got_pathlist_entry *pe;
3309 TAILQ_FOREACH(pe, commitable_paths, entry) {
3310 struct got_fileindex_entry *ie;
3311 struct got_commitable *ct = pe->data;
3313 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3315 if (ct->status == GOT_STATUS_DELETE) {
3316 got_fileindex_entry_remove(fileindex, ie);
3317 got_fileindex_entry_free(ie);
3319 err = got_fileindex_entry_update(ie,
3320 ct->ondisk_path, ct->blob_id->sha1,
3321 new_base_commit_id->sha1, 1);
3323 err = got_fileindex_entry_alloc(&ie,
3324 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3325 new_base_commit_id->sha1);
3328 err = got_fileindex_entry_add(fileindex, ie);
3336 static const struct got_error *
3337 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3338 struct got_object_id *head_commit_id)
3340 const struct got_error *err = NULL;
3341 struct got_object_id *id = NULL;
3342 struct got_commit_object *commit = NULL;
3343 const char *ct_path = ct->in_repo_path;
3345 while (ct_path[0] == '/')
3348 if (ct->status != GOT_STATUS_ADD) {
3349 /* Trivial case: base commit == head commit */
3350 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3353 * Ensure file content which local changes were based
3354 * on matches file content in the branch head.
3356 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3358 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3360 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3362 } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3363 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3365 /* Require that added files don't exist in the branch head. */
3366 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3367 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3369 err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3373 got_object_commit_close(commit);
3378 const struct got_error *
3379 commit_worktree(struct got_object_id **new_commit_id,
3380 struct got_pathlist_head *commitable_paths,
3381 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3382 const char *author, const char *committer,
3383 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3384 got_worktree_status_cb status_cb, void *status_arg,
3385 struct got_repository *repo)
3387 const struct got_error *err = NULL, *unlockerr = NULL;
3388 struct got_pathlist_entry *pe;
3389 const char *head_ref_name = NULL;
3390 struct got_commit_object *head_commit = NULL;
3391 struct got_reference *head_ref2 = NULL;
3392 struct got_object_id *head_commit_id2 = NULL;
3393 struct got_tree_object *head_tree = NULL;
3394 struct got_object_id *new_tree_id = NULL;
3395 struct got_object_id_queue parent_ids;
3396 struct got_object_qid *pid = NULL;
3397 char *logmsg = NULL;
3399 *new_commit_id = NULL;
3401 SIMPLEQ_INIT(&parent_ids);
3403 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3407 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3411 if (commit_msg_cb != NULL) {
3412 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3417 if (logmsg == NULL || strlen(logmsg) == 0) {
3418 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3422 /* Create blobs from added and modified files and record their IDs. */
3423 TAILQ_FOREACH(pe, commitable_paths, entry) {
3424 struct got_commitable *ct = pe->data;
3427 if (ct->status != GOT_STATUS_ADD &&
3428 ct->status != GOT_STATUS_MODIFY)
3431 if (asprintf(&ondisk_path, "%s/%s",
3432 worktree->root_path, pe->path) == -1) {
3433 err = got_error_from_errno("asprintf");
3436 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3442 /* Recursively write new tree objects. */
3443 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3444 status_cb, status_arg, repo);
3448 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3451 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3452 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3453 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3454 got_object_qid_free(pid);
3460 /* Check if a concurrent commit to our branch has occurred. */
3461 head_ref_name = got_worktree_get_head_ref_name(worktree);
3462 if (head_ref_name == NULL) {
3463 err = got_error_from_errno("got_worktree_get_head_ref_name");
3466 /* Lock the reference here to prevent concurrent modification. */
3467 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3470 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3473 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3474 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3477 /* Update branch head in repository. */
3478 err = got_ref_change_ref(head_ref2, *new_commit_id);
3481 err = got_ref_write(head_ref2, repo);
3485 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3489 err = ref_base_commit(worktree, repo);
3494 got_object_tree_close(head_tree);
3496 got_object_commit_close(head_commit);
3497 free(head_commit_id2);
3499 unlockerr = got_ref_unlock(head_ref2);
3500 if (unlockerr && err == NULL)
3502 got_ref_close(head_ref2);
3507 static const struct got_error *
3508 check_path_is_commitable(const char *path,
3509 struct got_pathlist_head *commitable_paths)
3511 struct got_pathlist_entry *cpe = NULL;
3512 size_t path_len = strlen(path);
3514 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3515 struct got_commitable *ct = cpe->data;
3516 const char *ct_path = ct->path;
3518 while (ct_path[0] == '/')
3521 if (strcmp(path, ct_path) == 0 ||
3522 got_path_is_child(ct_path, path, path_len))
3527 return got_error_path(path, GOT_ERR_BAD_PATH);
3532 const struct got_error *
3533 got_worktree_commit(struct got_object_id **new_commit_id,
3534 struct got_worktree *worktree, struct got_pathlist_head *paths,
3535 const char *author, const char *committer,
3536 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3537 got_worktree_status_cb status_cb, void *status_arg,
3538 struct got_repository *repo)
3540 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3541 struct got_fileindex *fileindex = NULL;
3542 char *fileindex_path = NULL;
3543 struct got_pathlist_head commitable_paths;
3544 struct collect_commitables_arg cc_arg;
3545 struct got_pathlist_entry *pe;
3546 struct got_reference *head_ref = NULL;
3547 struct got_object_id *head_commit_id = NULL;
3549 *new_commit_id = NULL;
3551 TAILQ_INIT(&commitable_paths);
3553 err = lock_worktree(worktree, LOCK_EX);
3557 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3561 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3565 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3569 cc_arg.commitable_paths = &commitable_paths;
3570 cc_arg.worktree = worktree;
3572 TAILQ_FOREACH(pe, paths, entry) {
3573 err = worktree_status(worktree, pe->path, fileindex, repo,
3574 collect_commitables, &cc_arg, NULL, NULL);
3579 if (TAILQ_EMPTY(&commitable_paths)) {
3580 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3584 TAILQ_FOREACH(pe, paths, entry) {
3585 err = check_path_is_commitable(pe->path, &commitable_paths);
3590 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3591 struct got_commitable *ct = pe->data;
3592 err = check_ct_out_of_date(ct, repo, head_commit_id);
3597 err = commit_worktree(new_commit_id, &commitable_paths,
3598 head_commit_id, worktree, author, committer,
3599 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3603 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3605 sync_err = sync_fileindex(fileindex, fileindex_path);
3606 if (sync_err && err == NULL)
3610 got_fileindex_free(fileindex);
3611 free(fileindex_path);
3612 unlockerr = lock_worktree(worktree, LOCK_SH);
3613 if (unlockerr && err == NULL)
3615 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3616 struct got_commitable *ct = pe->data;
3617 free_commitable(ct);
3619 got_pathlist_free(&commitable_paths);
3624 got_commitable_get_path(struct got_commitable *ct)
3630 got_commitable_get_status(struct got_commitable *ct)
3635 struct check_rebase_ok_arg {
3636 struct got_worktree *worktree;
3637 struct got_repository *repo;
3640 static const struct got_error *
3641 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3643 const struct got_error *err = NULL;
3644 struct check_rebase_ok_arg *a = arg;
3645 unsigned char status;
3649 /* Reject rebase of a work tree with mixed base commits. */
3650 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3651 SHA1_DIGEST_LENGTH))
3652 return got_error(GOT_ERR_MIXED_COMMITS);
3654 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3656 return got_error_from_errno("asprintf");
3658 /* Reject rebase of a work tree with modified or conflicted files. */
3659 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3664 if (status != GOT_STATUS_NO_CHANGE)
3665 return got_error(GOT_ERR_MODIFIED);
3670 const struct got_error *
3671 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3672 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3673 struct got_worktree *worktree, struct got_reference *branch,
3674 struct got_repository *repo)
3676 const struct got_error *err = NULL;
3677 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3678 char *branch_ref_name = NULL;
3679 char *fileindex_path = NULL;
3680 struct check_rebase_ok_arg ok_arg;
3681 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3683 *new_base_branch_ref = NULL;
3687 err = lock_worktree(worktree, LOCK_EX);
3691 err = open_fileindex(fileindex, &fileindex_path, worktree);
3695 ok_arg.worktree = worktree;
3697 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3702 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3706 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3710 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3714 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3719 err = got_ref_alloc_symref(new_base_branch_ref,
3720 new_base_branch_ref_name, wt_branch);
3723 err = got_ref_write(*new_base_branch_ref, repo);
3727 /* TODO Lock original branch's ref while rebasing? */
3729 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3733 err = got_ref_write(branch_ref, repo);
3737 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3738 worktree->base_commit_id);
3741 err = got_ref_write(*tmp_branch, repo);
3745 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3749 free(fileindex_path);
3750 free(tmp_branch_name);
3751 free(new_base_branch_ref_name);
3752 free(branch_ref_name);
3754 got_ref_close(branch_ref);
3756 got_ref_close(wt_branch);
3758 if (*new_base_branch_ref) {
3759 got_ref_close(*new_base_branch_ref);
3760 *new_base_branch_ref = NULL;
3763 got_ref_close(*tmp_branch);
3767 got_fileindex_free(*fileindex);
3770 lock_worktree(worktree, LOCK_SH);
3775 const struct got_error *
3776 got_worktree_rebase_continue(struct got_object_id **commit_id,
3777 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3778 struct got_reference **branch, struct got_fileindex **fileindex,
3779 struct got_worktree *worktree, struct got_repository *repo)
3781 const struct got_error *err;
3782 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3783 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3784 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3785 char *fileindex_path = NULL;
3788 *new_base_branch = NULL;
3793 err = lock_worktree(worktree, LOCK_EX);
3797 err = open_fileindex(fileindex, &fileindex_path, worktree);
3801 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3805 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3809 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3813 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3817 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3821 err = got_ref_open(branch, repo,
3822 got_ref_get_symref_target(branch_ref), 0);
3826 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3830 err = got_ref_resolve(commit_id, repo, commit_ref);
3834 err = got_ref_open(new_base_branch, repo,
3835 new_base_branch_ref_name, 0);
3839 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3843 free(commit_ref_name);
3844 free(branch_ref_name);
3845 free(fileindex_path);
3847 got_ref_close(commit_ref);
3849 got_ref_close(branch_ref);
3854 got_ref_close(*tmp_branch);
3857 if (*new_base_branch) {
3858 got_ref_close(*new_base_branch);
3859 *new_base_branch = NULL;
3862 got_ref_close(*branch);
3866 got_fileindex_free(*fileindex);
3869 lock_worktree(worktree, LOCK_SH);
3874 const struct got_error *
3875 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3877 const struct got_error *err;
3878 char *tmp_branch_name = NULL;
3880 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3884 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3885 free(tmp_branch_name);
3889 static const struct got_error *
3890 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3891 char **logmsg, void *arg)
3897 static const struct got_error *
3898 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
3899 const char *path, struct got_object_id *blob_id,
3900 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3905 struct collect_merged_paths_arg {
3906 got_worktree_checkout_cb progress_cb;
3908 struct got_pathlist_head *merged_paths;
3911 static const struct got_error *
3912 collect_merged_paths(void *arg, unsigned char status, const char *path)
3914 const struct got_error *err;
3915 struct collect_merged_paths_arg *a = arg;
3917 struct got_pathlist_entry *new;
3919 err = (*a->progress_cb)(a->progress_arg, status, path);
3923 if (status != GOT_STATUS_MERGE &&
3924 status != GOT_STATUS_ADD &&
3925 status != GOT_STATUS_DELETE &&
3926 status != GOT_STATUS_CONFLICT)
3931 return got_error_from_errno("strdup");
3933 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3934 if (err || new == NULL)
3940 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3942 struct got_pathlist_entry *pe;
3944 TAILQ_FOREACH(pe, merged_paths, entry)
3945 free((char *)pe->path);
3947 got_pathlist_free(merged_paths);
3950 static const struct got_error *
3951 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3952 struct got_repository *repo)
3954 const struct got_error *err;
3955 struct got_reference *commit_ref = NULL;
3957 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3959 if (err->code != GOT_ERR_NOT_REF)
3961 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3964 err = got_ref_write(commit_ref, repo);
3968 struct got_object_id *stored_id;
3971 err = got_ref_resolve(&stored_id, repo, commit_ref);
3974 cmp = got_object_id_cmp(commit_id, stored_id);
3977 err = got_error(GOT_ERR_REBASE_COMMITID);
3983 got_ref_close(commit_ref);
3987 static const struct got_error *
3988 rebase_merge_files(struct got_pathlist_head *merged_paths,
3989 const char *commit_ref_name, struct got_worktree *worktree,
3990 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3991 struct got_object_id *commit_id, struct got_repository *repo,
3992 got_worktree_checkout_cb progress_cb, void *progress_arg,
3993 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3995 const struct got_error *err;
3996 struct got_reference *commit_ref = NULL;
3997 struct collect_merged_paths_arg cmp_arg;
3998 char *fileindex_path;
4000 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4002 err = get_fileindex_path(&fileindex_path, worktree);
4006 cmp_arg.progress_cb = progress_cb;
4007 cmp_arg.progress_arg = progress_arg;
4008 cmp_arg.merged_paths = merged_paths;
4009 err = merge_files(worktree, fileindex, fileindex_path,
4010 parent_commit_id, commit_id, repo, collect_merged_paths,
4011 &cmp_arg, cancel_cb, cancel_arg);
4013 got_ref_close(commit_ref);
4017 const struct got_error *
4018 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4019 struct got_worktree *worktree, struct got_fileindex *fileindex,
4020 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4021 struct got_repository *repo,
4022 got_worktree_checkout_cb progress_cb, void *progress_arg,
4023 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4025 const struct got_error *err;
4026 char *commit_ref_name;
4028 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4032 err = store_commit_id(commit_ref_name, commit_id, repo);
4036 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4037 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4038 progress_arg, cancel_cb, cancel_arg);
4040 free(commit_ref_name);
4044 const struct got_error *
4045 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4046 struct got_worktree *worktree, struct got_fileindex *fileindex,
4047 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4048 struct got_repository *repo,
4049 got_worktree_checkout_cb progress_cb, void *progress_arg,
4050 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4052 const struct got_error *err;
4053 char *commit_ref_name;
4055 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4059 err = store_commit_id(commit_ref_name, commit_id, repo);
4063 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4064 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4065 progress_arg, cancel_cb, cancel_arg);
4067 free(commit_ref_name);
4071 static const struct got_error *
4072 rebase_commit(struct got_object_id **new_commit_id,
4073 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4074 struct got_worktree *worktree, struct got_fileindex *fileindex,
4075 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4076 const char *new_logmsg, struct got_repository *repo)
4078 const struct got_error *err, *sync_err;
4079 struct got_pathlist_head commitable_paths;
4080 struct collect_commitables_arg cc_arg;
4081 char *fileindex_path = NULL;
4082 struct got_reference *head_ref = NULL;
4083 struct got_object_id *head_commit_id = NULL;
4084 char *logmsg = NULL;
4086 TAILQ_INIT(&commitable_paths);
4087 *new_commit_id = NULL;
4089 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4091 err = get_fileindex_path(&fileindex_path, worktree);
4095 cc_arg.commitable_paths = &commitable_paths;
4096 cc_arg.worktree = worktree;
4099 * If possible get the status of individual files directly to
4100 * avoid crawling the entire work tree once per rebased commit.
4101 * TODO: Ideally, merged_paths would contain a list of commitables
4102 * we could use so we could skip worktree_status() entirely.
4105 struct got_pathlist_entry *pe;
4106 if (TAILQ_EMPTY(merged_paths)) {
4107 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4110 TAILQ_FOREACH(pe, merged_paths, entry) {
4111 err = worktree_status(worktree, pe->path, fileindex,
4112 repo, collect_commitables, &cc_arg, NULL, NULL);
4117 err = worktree_status(worktree, "", fileindex, repo,
4118 collect_commitables, &cc_arg, NULL, NULL);
4123 if (TAILQ_EMPTY(&commitable_paths)) {
4124 /* No-op change; commit will be elided. */
4125 err = got_ref_delete(commit_ref, repo);
4128 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4132 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4136 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4141 logmsg = strdup(new_logmsg);
4143 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4145 return got_error_from_errno("strdup");
4147 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4148 worktree, got_object_commit_get_author(orig_commit),
4149 got_object_commit_get_committer(orig_commit),
4150 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4154 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4158 err = got_ref_delete(commit_ref, repo);
4162 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4164 sync_err = sync_fileindex(fileindex, fileindex_path);
4165 if (sync_err && err == NULL)
4168 free(fileindex_path);
4169 free(head_commit_id);
4171 got_ref_close(head_ref);
4173 free(*new_commit_id);
4174 *new_commit_id = NULL;
4179 const struct got_error *
4180 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4181 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4182 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4183 struct got_commit_object *orig_commit,
4184 struct got_object_id *orig_commit_id, struct got_repository *repo)
4186 const struct got_error *err;
4187 char *commit_ref_name;
4188 struct got_reference *commit_ref = NULL;
4189 struct got_object_id *commit_id = NULL;
4191 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4195 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4198 err = got_ref_resolve(&commit_id, repo, commit_ref);
4201 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4202 err = got_error(GOT_ERR_REBASE_COMMITID);
4206 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4207 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4210 got_ref_close(commit_ref);
4211 free(commit_ref_name);
4216 const struct got_error *
4217 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4218 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4219 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4220 struct got_commit_object *orig_commit,
4221 struct got_object_id *orig_commit_id, const char *new_logmsg,
4222 struct got_repository *repo)
4224 const struct got_error *err;
4225 char *commit_ref_name;
4226 struct got_reference *commit_ref = NULL;
4227 struct got_object_id *commit_id = NULL;
4229 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4233 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4236 err = got_ref_resolve(&commit_id, repo, commit_ref);
4239 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4240 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4244 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4245 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4248 got_ref_close(commit_ref);
4249 free(commit_ref_name);
4254 const struct got_error *
4255 got_worktree_rebase_postpone(struct got_worktree *worktree,
4256 struct got_fileindex *fileindex)
4259 got_fileindex_free(fileindex);
4260 return lock_worktree(worktree, LOCK_SH);
4263 static const struct got_error *
4264 delete_ref(const char *name, struct got_repository *repo)
4266 const struct got_error *err;
4267 struct got_reference *ref;
4269 err = got_ref_open(&ref, repo, name, 0);
4271 if (err->code == GOT_ERR_NOT_REF)
4276 err = got_ref_delete(ref, repo);
4281 static const struct got_error *
4282 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4284 const struct got_error *err;
4285 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4286 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4288 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4291 err = delete_ref(tmp_branch_name, repo);
4295 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4298 err = delete_ref(new_base_branch_ref_name, repo);
4302 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4305 err = delete_ref(branch_ref_name, repo);
4309 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4312 err = delete_ref(commit_ref_name, repo);
4317 free(tmp_branch_name);
4318 free(new_base_branch_ref_name);
4319 free(branch_ref_name);
4320 free(commit_ref_name);
4324 const struct got_error *
4325 got_worktree_rebase_complete(struct got_worktree *worktree,
4326 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4327 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4328 struct got_repository *repo)
4330 const struct got_error *err, *unlockerr;
4331 struct got_object_id *new_head_commit_id = NULL;
4333 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4337 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4341 err = got_ref_write(rebased_branch, repo);
4345 err = got_worktree_set_head_ref(worktree, rebased_branch);
4349 err = delete_rebase_refs(worktree, repo);
4352 got_fileindex_free(fileindex);
4353 free(new_head_commit_id);
4354 unlockerr = lock_worktree(worktree, LOCK_SH);
4355 if (unlockerr && err == NULL)
4360 struct collect_revertible_paths_arg {
4361 struct got_pathlist_head *revertible_paths;
4362 struct got_worktree *worktree;
4365 static const struct got_error *
4366 collect_revertible_paths(void *arg, unsigned char status,
4367 unsigned char staged_status, const char *relpath,
4368 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4369 struct got_object_id *commit_id)
4371 struct collect_revertible_paths_arg *a = arg;
4372 const struct got_error *err = NULL;
4373 struct got_pathlist_entry *new = NULL;
4376 if (status != GOT_STATUS_ADD &&
4377 status != GOT_STATUS_DELETE &&
4378 status != GOT_STATUS_MODIFY &&
4379 status != GOT_STATUS_CONFLICT &&
4380 status != GOT_STATUS_MISSING)
4383 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4384 return got_error_from_errno("asprintf");
4386 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4387 if (err || new == NULL)
4392 const struct got_error *
4393 got_worktree_rebase_abort(struct got_worktree *worktree,
4394 struct got_fileindex *fileindex, struct got_repository *repo,
4395 struct got_reference *new_base_branch,
4396 got_worktree_checkout_cb progress_cb, void *progress_arg)
4398 const struct got_error *err, *unlockerr, *sync_err;
4399 struct got_reference *resolved = NULL;
4400 struct got_object_id *commit_id = NULL;
4401 char *fileindex_path = NULL;
4402 struct got_pathlist_head revertible_paths;
4403 struct got_pathlist_entry *pe;
4404 struct collect_revertible_paths_arg crp_arg;
4405 struct got_object_id *tree_id = NULL;
4407 TAILQ_INIT(&revertible_paths);
4409 err = lock_worktree(worktree, LOCK_EX);
4413 err = got_ref_open(&resolved, repo,
4414 got_ref_get_symref_target(new_base_branch), 0);
4418 err = got_worktree_set_head_ref(worktree, resolved);
4423 * XXX commits to the base branch could have happened while
4424 * we were busy rebasing; should we store the original commit ID
4425 * when rebase begins and read it back here?
4427 err = got_ref_resolve(&commit_id, repo, resolved);
4431 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4435 err = got_object_id_by_path(&tree_id, repo,
4436 worktree->base_commit_id, worktree->path_prefix);
4440 err = delete_rebase_refs(worktree, repo);
4444 err = get_fileindex_path(&fileindex_path, worktree);
4448 crp_arg.revertible_paths = &revertible_paths;
4449 crp_arg.worktree = worktree;
4450 err = worktree_status(worktree, "", fileindex, repo,
4451 collect_revertible_paths, &crp_arg, NULL, NULL);
4455 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4456 err = revert_file(worktree, fileindex, pe->path,
4457 progress_cb, progress_arg, repo);
4462 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4463 repo, progress_cb, progress_arg, NULL, NULL);
4465 sync_err = sync_fileindex(fileindex, fileindex_path);
4466 if (sync_err && err == NULL)
4469 got_ref_close(resolved);
4473 got_fileindex_free(fileindex);
4474 free(fileindex_path);
4475 TAILQ_FOREACH(pe, &revertible_paths, entry)
4476 free((char *)pe->path);
4477 got_pathlist_free(&revertible_paths);
4479 unlockerr = lock_worktree(worktree, LOCK_SH);
4480 if (unlockerr && err == NULL)
4485 const struct got_error *
4486 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4487 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4488 struct got_fileindex **fileindex, struct got_worktree *worktree,
4489 struct got_repository *repo)
4491 const struct got_error *err = NULL;
4492 char *tmp_branch_name = NULL;
4493 char *branch_ref_name = NULL;
4494 char *base_commit_ref_name = NULL;
4495 char *fileindex_path = NULL;
4496 struct check_rebase_ok_arg ok_arg;
4497 struct got_reference *wt_branch = NULL;
4498 struct got_reference *base_commit_ref = NULL;
4502 *base_commit_id = NULL;
4505 err = lock_worktree(worktree, LOCK_EX);
4509 err = open_fileindex(fileindex, &fileindex_path, worktree);
4513 ok_arg.worktree = worktree;
4515 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4520 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4524 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4528 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4533 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4538 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4542 err = got_ref_write(*branch_ref, repo);
4546 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4547 worktree->base_commit_id);
4550 err = got_ref_write(base_commit_ref, repo);
4553 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4554 if (*base_commit_id == NULL) {
4555 err = got_error_from_errno("got_object_id_dup");
4559 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4560 worktree->base_commit_id);
4563 err = got_ref_write(*tmp_branch, repo);
4567 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4571 free(fileindex_path);
4572 free(tmp_branch_name);
4573 free(branch_ref_name);
4574 free(base_commit_ref_name);
4576 got_ref_close(wt_branch);
4579 got_ref_close(*branch_ref);
4583 got_ref_close(*tmp_branch);
4586 free(*base_commit_id);
4588 got_fileindex_free(*fileindex);
4591 lock_worktree(worktree, LOCK_SH);
4596 const struct got_error *
4597 got_worktree_histedit_postpone(struct got_worktree *worktree,
4598 struct got_fileindex *fileindex)
4601 got_fileindex_free(fileindex);
4602 return lock_worktree(worktree, LOCK_SH);
4605 const struct got_error *
4606 got_worktree_histedit_in_progress(int *in_progress,
4607 struct got_worktree *worktree)
4609 const struct got_error *err;
4610 char *tmp_branch_name = NULL;
4612 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4616 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4617 free(tmp_branch_name);
4621 const struct got_error *
4622 got_worktree_histedit_continue(struct got_object_id **commit_id,
4623 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4624 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4625 struct got_worktree *worktree, struct got_repository *repo)
4627 const struct got_error *err;
4628 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4629 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4630 struct got_reference *commit_ref = NULL;
4631 struct got_reference *base_commit_ref = NULL;
4632 char *fileindex_path = NULL;
4636 *base_commit_id = NULL;
4639 err = lock_worktree(worktree, LOCK_EX);
4643 err = open_fileindex(fileindex, &fileindex_path, worktree);
4647 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4651 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4655 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4659 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4664 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4668 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4671 err = got_ref_resolve(commit_id, repo, commit_ref);
4675 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4678 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4682 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4686 free(commit_ref_name);
4687 free(branch_ref_name);
4688 free(fileindex_path);
4690 got_ref_close(commit_ref);
4691 if (base_commit_ref)
4692 got_ref_close(base_commit_ref);
4696 free(*base_commit_id);
4697 *base_commit_id = NULL;
4699 got_ref_close(*tmp_branch);
4703 got_fileindex_free(*fileindex);
4706 lock_worktree(worktree, LOCK_EX);
4711 static const struct got_error *
4712 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4714 const struct got_error *err;
4715 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4716 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4718 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4721 err = delete_ref(tmp_branch_name, repo);
4725 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4729 err = delete_ref(base_commit_ref_name, repo);
4733 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4736 err = delete_ref(branch_ref_name, repo);
4740 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4743 err = delete_ref(commit_ref_name, repo);
4747 free(tmp_branch_name);
4748 free(base_commit_ref_name);
4749 free(branch_ref_name);
4750 free(commit_ref_name);
4754 const struct got_error *
4755 got_worktree_histedit_abort(struct got_worktree *worktree,
4756 struct got_fileindex *fileindex, struct got_repository *repo,
4757 struct got_reference *branch, struct got_object_id *base_commit_id,
4758 got_worktree_checkout_cb progress_cb, void *progress_arg)
4760 const struct got_error *err, *unlockerr, *sync_err;
4761 struct got_reference *resolved = NULL;
4762 char *fileindex_path = NULL;
4763 struct got_pathlist_head revertible_paths;
4764 struct got_pathlist_entry *pe;
4765 struct collect_revertible_paths_arg crp_arg;
4766 struct got_object_id *tree_id = NULL;
4768 TAILQ_INIT(&revertible_paths);
4770 err = lock_worktree(worktree, LOCK_EX);
4774 err = got_ref_open(&resolved, repo,
4775 got_ref_get_symref_target(branch), 0);
4779 err = got_worktree_set_head_ref(worktree, resolved);
4783 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4787 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4788 worktree->path_prefix);
4792 err = delete_histedit_refs(worktree, repo);
4796 err = get_fileindex_path(&fileindex_path, worktree);
4800 crp_arg.revertible_paths = &revertible_paths;
4801 crp_arg.worktree = worktree;
4802 err = worktree_status(worktree, "", fileindex, repo,
4803 collect_revertible_paths, &crp_arg, NULL, NULL);
4807 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4808 err = revert_file(worktree, fileindex, pe->path,
4809 progress_cb, progress_arg, repo);
4814 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4815 repo, progress_cb, progress_arg, NULL, NULL);
4817 sync_err = sync_fileindex(fileindex, fileindex_path);
4818 if (sync_err && err == NULL)
4821 got_ref_close(resolved);
4823 free(fileindex_path);
4824 TAILQ_FOREACH(pe, &revertible_paths, entry)
4825 free((char *)pe->path);
4826 got_pathlist_free(&revertible_paths);
4828 unlockerr = lock_worktree(worktree, LOCK_SH);
4829 if (unlockerr && err == NULL)
4834 const struct got_error *
4835 got_worktree_histedit_complete(struct got_worktree *worktree,
4836 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4837 struct got_reference *edited_branch, struct got_repository *repo)
4839 const struct got_error *err, *unlockerr;
4840 struct got_object_id *new_head_commit_id = NULL;
4841 struct got_reference *resolved = NULL;
4843 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4847 err = got_ref_open(&resolved, repo,
4848 got_ref_get_symref_target(edited_branch), 0);
4852 err = got_ref_change_ref(resolved, new_head_commit_id);
4856 err = got_ref_write(resolved, repo);
4860 err = got_worktree_set_head_ref(worktree, resolved);
4864 err = delete_histedit_refs(worktree, repo);
4867 got_fileindex_free(fileindex);
4868 free(new_head_commit_id);
4869 unlockerr = lock_worktree(worktree, LOCK_SH);
4870 if (unlockerr && err == NULL)
4875 const struct got_error *
4876 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4877 struct got_object_id *commit_id, struct got_repository *repo)
4879 const struct got_error *err;
4880 char *commit_ref_name;
4882 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4886 err = store_commit_id(commit_ref_name, commit_id, repo);
4890 err = delete_ref(commit_ref_name, repo);
4892 free(commit_ref_name);
4896 static const struct got_error *
4897 stage_path(const char *relpath, const char *ondisk_path,
4898 const char *path_content, struct got_worktree *worktree,
4899 struct got_fileindex *fileindex, struct got_repository *repo,
4900 got_worktree_status_cb status_cb, void *status_arg)
4902 const struct got_error *err = NULL;
4903 struct got_fileindex_entry *ie;
4904 unsigned char status;
4906 struct got_object_id blob_id, *staged_blob_id = NULL;
4909 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
4911 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4915 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
4920 case GOT_STATUS_ADD:
4921 case GOT_STATUS_MODIFY:
4922 err = got_object_blob_create(&staged_blob_id,
4923 path_content ? path_content : ondisk_path, repo);
4926 memcpy(&blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
4927 memcpy(ie->staged_blob_sha1, staged_blob_id->sha1,
4928 SHA1_DIGEST_LENGTH);
4929 if (status == GOT_STATUS_ADD)
4930 stage = GOT_FILEIDX_STAGE_ADD;
4932 stage = GOT_FILEIDX_STAGE_MODIFY;
4933 got_fileindex_entry_stage_set(ie, stage);
4934 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4935 get_staged_status(ie), relpath, &blob_id,
4936 staged_blob_id, NULL);
4938 case GOT_STATUS_DELETE:
4939 stage = GOT_FILEIDX_STAGE_DELETE;
4940 got_fileindex_entry_stage_set(ie, stage);
4941 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4942 get_staged_status(ie), relpath, NULL, NULL, NULL);
4945 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4949 free(staged_blob_id);
4953 const struct got_error *
4954 got_worktree_stage(struct got_worktree *worktree,
4955 struct got_pathlist_head *paths,
4956 got_worktree_status_cb status_cb, void *status_arg,
4957 struct got_repository *repo)
4959 const struct got_error *err = NULL, *sync_err, *unlockerr;
4960 struct got_pathlist_entry *pe;
4961 struct got_fileindex *fileindex = NULL;
4962 char *fileindex_path = NULL;
4964 err = lock_worktree(worktree, LOCK_EX);
4968 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4972 TAILQ_FOREACH(pe, paths, entry) {
4974 err = got_path_skip_common_ancestor(&relpath,
4975 got_worktree_get_root_path(worktree), pe->path);
4978 err = stage_path(relpath, pe->path,
4979 (const char *)pe->data, worktree, fileindex, repo,
4980 status_cb, status_arg);
4986 sync_err = sync_fileindex(fileindex, fileindex_path);
4987 if (sync_err && err == NULL)
4990 free(fileindex_path);
4992 got_fileindex_free(fileindex);
4993 unlockerr = lock_worktree(worktree, LOCK_SH);
4994 if (unlockerr && err == NULL)