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/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 * the file at deriv_path acts as the first derived version, and the
723 * file on disk acts as the second derived version.
725 static const struct got_error *
726 merge_file(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, const char *deriv_path,
729 const char *label_deriv, struct got_repository *repo,
730 got_worktree_checkout_cb progress_cb, void *progress_arg)
732 const struct got_error *err = NULL;
735 char *blob_orig_path = NULL;
736 char *merged_path = NULL, *base_path = NULL;
740 *local_changes_subsumed = 0;
742 parent = dirname(ondisk_path);
744 return got_error_from_errno2("dirname", ondisk_path);
746 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
747 return got_error_from_errno("asprintf");
749 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
754 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
755 err = got_error_from_errno("asprintf");
760 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
764 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
770 * If the file has no blob, this is an "add vs add" conflict,
771 * and we simply use an empty ancestor file to make both files
772 * appear in the merged result in their entirety.
776 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
777 blob_orig_path, ondisk_path, label_deriv, path);
781 err = (*progress_cb)(progress_arg,
782 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
786 if (fsync(merged_fd) != 0) {
787 err = got_error_from_errno("fsync");
791 /* Check if a clean merge has subsumed all local changes. */
792 if (overlapcnt == 0) {
793 err = check_files_equal(local_changes_subsumed, deriv_path,
799 if (chmod(merged_path, st_mode) != 0) {
800 err = got_error_from_errno2("chmod", merged_path);
804 if (rename(merged_path, ondisk_path) != 0) {
805 err = got_error_from_errno3("rename", merged_path,
812 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
813 err = got_error_from_errno("close");
814 if (f_orig && fclose(f_orig) != 0 && err == NULL)
815 err = got_error_from_errno("fclose");
818 if (blob_orig_path) {
819 unlink(blob_orig_path);
820 free(blob_orig_path);
826 * Perform a 3-way merge where blob_orig acts as the common ancestor,
827 * blob_deriv acts as the first derived version, and the file on disk
828 * acts as the second derived version.
830 static const struct got_error *
831 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
832 struct got_blob_object *blob_orig, const char *ondisk_path,
833 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
834 struct got_object_id *deriv_base_commit_id,
835 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
838 const struct got_error *err = NULL;
839 FILE *f_deriv = NULL;
840 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
841 char *label_deriv = NULL, *parent;
843 *local_changes_subsumed = 0;
845 parent = dirname(ondisk_path);
847 return got_error_from_errno2("dirname", ondisk_path);
850 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
851 err = got_error_from_errno("asprintf");
856 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
859 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
864 err = got_object_id_str(&id_str, deriv_base_commit_id);
867 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
868 err = got_error_from_errno("asprintf");
872 err = merge_file(local_changes_subsumed, worktree, blob_orig,
873 ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
874 repo, progress_cb, progress_arg);
876 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
877 err = got_error_from_errno("fclose");
879 if (blob_deriv_path) {
880 unlink(blob_deriv_path);
881 free(blob_deriv_path);
888 static const struct got_error *
889 update_blob_fileindex_entry(struct got_worktree *worktree,
890 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
891 const char *ondisk_path, const char *path, struct got_blob_object *blob,
892 int update_timestamps)
894 const struct got_error *err = NULL;
897 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
899 err = got_fileindex_entry_update(ie, ondisk_path,
900 blob->id.sha1, worktree->base_commit_id->sha1,
903 struct got_fileindex_entry *new_ie;
904 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
905 path, blob->id.sha1, worktree->base_commit_id->sha1);
907 err = got_fileindex_entry_add(fileindex, new_ie);
912 static const struct got_error *
913 install_blob(struct got_worktree *worktree, const char *ondisk_path,
914 const char *path, uint16_t te_mode, uint16_t st_mode,
915 struct got_blob_object *blob, int restoring_missing_file,
916 int reverting_versioned_file, struct got_repository *repo,
917 got_worktree_checkout_cb progress_cb, void *progress_arg)
919 const struct got_error *err = NULL;
923 char *tmppath = NULL;
925 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
926 GOT_DEFAULT_FILE_MODE);
928 if (errno == ENOENT) {
929 char *parent = dirname(path);
931 return got_error_from_errno2("dirname", path);
932 err = add_dir_on_disk(worktree, parent);
935 fd = open(ondisk_path,
936 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
937 GOT_DEFAULT_FILE_MODE);
939 return got_error_from_errno2("open",
941 } else if (errno == EEXIST) {
942 if (!S_ISREG(st_mode)) {
943 /* TODO file is obstructed; do something */
944 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
947 err = got_opentemp_named_fd(&tmppath, &fd,
954 return got_error_from_errno2("open", ondisk_path);
957 if (restoring_missing_file)
958 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
959 else if (reverting_versioned_file)
960 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
962 err = (*progress_cb)(progress_arg,
963 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
967 hdrlen = got_object_blob_get_hdrlen(blob);
969 const uint8_t *buf = got_object_blob_get_read_buf(blob);
970 err = got_object_blob_read_block(&len, blob);
974 /* Skip blob object header first time around. */
975 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
977 err = got_error_from_errno("write");
979 } else if (outlen != len - hdrlen) {
980 err = got_error(GOT_ERR_IO);
987 if (fsync(fd) != 0) {
988 err = got_error_from_errno("fsync");
993 if (rename(tmppath, ondisk_path) != 0) {
994 err = got_error_from_errno3("rename", tmppath,
1001 if (te_mode & S_IXUSR) {
1002 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1003 err = got_error_from_errno2("chmod", ondisk_path);
1007 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1008 err = got_error_from_errno2("chmod", ondisk_path);
1014 if (fd != -1 && close(fd) != 0 && err == NULL)
1015 err = got_error_from_errno("close");
1020 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1021 static const struct got_error *
1022 get_modified_file_content_status(unsigned char *status, FILE *f)
1024 const struct got_error *err = NULL;
1025 const char *markers[3] = {
1026 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1027 GOT_DIFF_CONFLICT_MARKER_SEP,
1028 GOT_DIFF_CONFLICT_MARKER_END
1033 const char delim[3] = {'\0', '\0', '\0'};
1035 while (*status == GOT_STATUS_MODIFY) {
1036 line = fparseln(f, &len, NULL, delim, 0);
1040 err = got_ferror(f, GOT_ERR_IO);
1044 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1045 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1047 *status = GOT_STATUS_CONFLICT;
1057 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1059 return !(ie->ctime_sec == sb->st_ctime &&
1060 ie->ctime_nsec == sb->st_ctimensec &&
1061 ie->mtime_sec == sb->st_mtime &&
1062 ie->mtime_nsec == sb->st_mtimensec &&
1063 ie->size == (sb->st_size & 0xffffffff));
1066 static unsigned char
1067 get_staged_status(struct got_fileindex_entry *ie)
1069 switch (got_fileindex_entry_stage_get(ie)) {
1070 case GOT_FILEIDX_STAGE_ADD:
1071 return GOT_STATUS_ADD;
1072 case GOT_FILEIDX_STAGE_DELETE:
1073 return GOT_STATUS_DELETE;
1074 case GOT_FILEIDX_STAGE_MODIFY:
1075 return GOT_STATUS_MODIFY;
1077 return GOT_STATUS_NO_CHANGE;
1081 static const struct got_error *
1082 get_file_status(unsigned char *status, struct stat *sb,
1083 struct got_fileindex_entry *ie, const char *abspath,
1084 struct got_repository *repo)
1086 const struct got_error *err = NULL;
1087 struct got_object_id id;
1091 struct got_blob_object *blob = NULL;
1093 unsigned char staged_status = get_staged_status(ie);
1095 *status = GOT_STATUS_NO_CHANGE;
1097 if (lstat(abspath, sb) == -1) {
1098 if (errno == ENOENT) {
1099 if (got_fileindex_entry_has_file_on_disk(ie))
1100 *status = GOT_STATUS_MISSING;
1102 *status = GOT_STATUS_DELETE;
1105 return got_error_from_errno2("lstat", abspath);
1108 if (!S_ISREG(sb->st_mode)) {
1109 *status = GOT_STATUS_OBSTRUCTED;
1113 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1114 *status = GOT_STATUS_DELETE;
1116 } else if (!got_fileindex_entry_has_blob(ie) &&
1117 staged_status != GOT_STATUS_ADD) {
1118 *status = GOT_STATUS_ADD;
1122 if (!stat_info_differs(ie, sb))
1125 if (staged_status == GOT_STATUS_MODIFY ||
1126 staged_status == GOT_STATUS_ADD)
1127 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1129 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1131 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1135 f = fopen(abspath, "r");
1137 err = got_error_from_errno2("fopen", abspath);
1140 hdrlen = got_object_blob_get_hdrlen(blob);
1142 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1143 err = got_object_blob_read_block(&blen, blob);
1146 /* Skip length of blob object header first time around. */
1147 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1148 if (flen == 0 && ferror(f)) {
1149 err = got_error_from_errno("fread");
1154 *status = GOT_STATUS_MODIFY;
1156 } else if (flen == 0) {
1158 *status = GOT_STATUS_MODIFY;
1160 } else if (blen - hdrlen == flen) {
1161 /* Skip blob object header first time around. */
1162 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1163 *status = GOT_STATUS_MODIFY;
1167 *status = GOT_STATUS_MODIFY;
1173 if (*status == GOT_STATUS_MODIFY) {
1175 err = get_modified_file_content_status(status, f);
1179 got_object_blob_close(blob);
1186 * Update timestamps in the file index if a file is unmodified and
1187 * we had to run a full content comparison to find out.
1189 static const struct got_error *
1190 sync_timestamps(char *ondisk_path, unsigned char status,
1191 struct got_fileindex_entry *ie, struct stat *sb)
1193 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1194 return got_fileindex_entry_update(ie, ondisk_path,
1195 ie->blob_sha1, ie->commit_sha1, 1);
1200 static const struct got_error *
1201 update_blob(struct got_worktree *worktree,
1202 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1203 struct got_tree_entry *te, const char *path,
1204 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1207 const struct got_error *err = NULL;
1208 struct got_blob_object *blob = NULL;
1210 unsigned char status = GOT_STATUS_NO_CHANGE;
1213 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1214 return got_error_from_errno("asprintf");
1217 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1218 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1221 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1224 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1225 sb.st_mode = got_fileindex_perms_to_st(ie);
1227 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1229 if (status == GOT_STATUS_OBSTRUCTED) {
1230 err = (*progress_cb)(progress_arg, status, path);
1234 if (ie && status != GOT_STATUS_MISSING) {
1235 if (got_fileindex_entry_has_commit(ie) &&
1236 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1237 SHA1_DIGEST_LENGTH) == 0) {
1238 err = sync_timestamps(ondisk_path, status, ie, &sb);
1241 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1245 if (got_fileindex_entry_has_blob(ie) &&
1246 memcmp(ie->blob_sha1, te->id->sha1,
1247 SHA1_DIGEST_LENGTH) == 0) {
1248 err = sync_timestamps(ondisk_path, status, ie, &sb);
1253 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1257 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1258 int update_timestamps;
1259 struct got_blob_object *blob2 = NULL;
1260 if (got_fileindex_entry_has_blob(ie)) {
1261 struct got_object_id id2;
1262 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1263 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1267 err = merge_blob(&update_timestamps, worktree, blob2,
1268 ondisk_path, path, sb.st_mode, blob,
1269 worktree->base_commit_id, repo,
1270 progress_cb, progress_arg);
1272 got_object_blob_close(blob2);
1274 * Do not update timestamps of files with local changes.
1275 * Otherwise, a future status walk would treat them as
1276 * unmodified files again.
1278 err = got_fileindex_entry_update(ie, ondisk_path,
1279 blob->id.sha1, worktree->base_commit_id->sha1,
1281 } else if (status == GOT_STATUS_DELETE) {
1282 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1285 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1286 ondisk_path, path, blob, 0);
1290 err = install_blob(worktree, ondisk_path, path, te->mode,
1291 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1292 repo, progress_cb, progress_arg);
1295 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1296 ondisk_path, path, blob, 1);
1300 got_object_blob_close(blob);
1306 static const struct got_error *
1307 remove_ondisk_file(const char *root_path, const char *path)
1309 const struct got_error *err = NULL;
1310 char *ondisk_path = NULL;
1312 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1313 return got_error_from_errno("asprintf");
1315 if (unlink(ondisk_path) == -1) {
1316 if (errno != ENOENT)
1317 err = got_error_from_errno2("unlink", ondisk_path);
1319 char *parent = dirname(ondisk_path);
1320 while (parent && strcmp(parent, root_path) != 0) {
1321 if (rmdir(parent) == -1) {
1322 if (errno != ENOTEMPTY)
1323 err = got_error_from_errno2("rmdir",
1327 parent = dirname(parent);
1334 static const struct got_error *
1335 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1336 struct got_fileindex_entry *ie, struct got_repository *repo,
1337 got_worktree_checkout_cb progress_cb, void *progress_arg)
1339 const struct got_error *err = NULL;
1340 unsigned char status;
1344 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1345 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1347 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1349 return got_error_from_errno("asprintf");
1351 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1355 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1356 status == GOT_STATUS_ADD) {
1357 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1361 * Preserve the working file and change the deleted blob's
1362 * entry into a schedule-add entry.
1364 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1369 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1372 if (status == GOT_STATUS_NO_CHANGE) {
1373 err = remove_ondisk_file(worktree->root_path, ie->path);
1377 got_fileindex_entry_remove(fileindex, ie);
1383 struct diff_cb_arg {
1384 struct got_fileindex *fileindex;
1385 struct got_worktree *worktree;
1386 struct got_repository *repo;
1387 got_worktree_checkout_cb progress_cb;
1389 got_worktree_cancel_cb cancel_cb;
1393 static const struct got_error *
1394 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1395 struct got_tree_entry *te, const char *parent_path)
1397 struct diff_cb_arg *a = arg;
1399 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1400 return got_error(GOT_ERR_CANCELLED);
1402 return update_blob(a->worktree, a->fileindex, ie, te,
1403 ie->path, a->repo, a->progress_cb, a->progress_arg);
1406 static const struct got_error *
1407 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1409 struct diff_cb_arg *a = arg;
1411 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1412 return got_error(GOT_ERR_CANCELLED);
1414 return delete_blob(a->worktree, a->fileindex, ie,
1415 a->repo, a->progress_cb, a->progress_arg);
1418 static const struct got_error *
1419 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1421 struct diff_cb_arg *a = arg;
1422 const struct got_error *err;
1425 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1426 return got_error(GOT_ERR_CANCELLED);
1428 if (asprintf(&path, "%s%s%s", parent_path,
1429 parent_path[0] ? "/" : "", te->name)
1431 return got_error_from_errno("asprintf");
1433 if (S_ISDIR(te->mode))
1434 err = add_dir_on_disk(a->worktree, path);
1436 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1437 a->repo, a->progress_cb, a->progress_arg);
1443 static const struct got_error *
1444 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1446 const struct got_error *err = NULL;
1447 char *uuidstr = NULL;
1448 uint32_t uuid_status;
1452 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1453 if (uuid_status != uuid_s_ok)
1454 return got_error_uuid(uuid_status);
1456 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1458 err = got_error_from_errno("asprintf");
1465 const struct got_error *
1466 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1468 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1471 static const struct got_error *
1472 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1474 return get_ref_name(refname, worktree,
1475 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1478 static const struct got_error *
1479 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1481 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1484 static const struct got_error *
1485 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1487 return get_ref_name(refname, worktree,
1488 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1491 static const struct got_error *
1492 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1494 return get_ref_name(refname, worktree,
1495 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1498 static const struct got_error *
1499 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1501 return get_ref_name(refname, worktree,
1502 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1505 static const struct got_error *
1506 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1508 return get_ref_name(refname, worktree,
1509 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1512 static const struct got_error *
1513 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1515 return get_ref_name(refname, worktree,
1516 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1519 static const struct got_error *
1520 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1522 return get_ref_name(refname, worktree,
1523 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1526 const struct got_error *
1527 got_worktree_get_histedit_script_path(char **path,
1528 struct got_worktree *worktree)
1530 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1531 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1533 return got_error_from_errno("asprintf");
1539 * Prevent Git's garbage collector from deleting our base commit by
1540 * setting a reference to our base commit's ID.
1542 static const struct got_error *
1543 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1545 const struct got_error *err = NULL;
1546 struct got_reference *ref = NULL;
1549 err = got_worktree_get_base_ref_name(&refname, worktree);
1553 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1557 err = got_ref_write(ref, repo);
1565 static const struct got_error *
1566 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1568 const struct got_error *err = NULL;
1570 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1571 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1572 err = got_error_from_errno("asprintf");
1573 *fileindex_path = NULL;
1579 static const struct got_error *
1580 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1581 struct got_worktree *worktree)
1583 const struct got_error *err = NULL;
1586 *fileindex_path = NULL;
1587 *fileindex = got_fileindex_alloc();
1588 if (*fileindex == NULL)
1589 return got_error_from_errno("got_fileindex_alloc");
1591 err = get_fileindex_path(fileindex_path, worktree);
1595 index = fopen(*fileindex_path, "rb");
1596 if (index == NULL) {
1597 if (errno != ENOENT)
1598 err = got_error_from_errno2("fopen", *fileindex_path);
1600 err = got_fileindex_read(*fileindex, index);
1601 if (fclose(index) != 0 && err == NULL)
1602 err = got_error_from_errno("fclose");
1606 free(*fileindex_path);
1607 *fileindex_path = NULL;
1608 got_fileindex_free(*fileindex);
1614 struct bump_base_commit_id_arg {
1615 struct got_object_id *base_commit_id;
1618 const char *entry_name;
1619 got_worktree_checkout_cb progress_cb;
1623 /* Bump base commit ID of all files within an updated part of the work tree. */
1624 static const struct got_error *
1625 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1627 const struct got_error *err;
1628 struct bump_base_commit_id_arg *a = arg;
1630 if (a->entry_name) {
1631 if (strcmp(ie->path, a->path) != 0)
1633 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1636 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1637 SHA1_DIGEST_LENGTH) == 0)
1640 if (a->progress_cb) {
1641 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1646 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1650 static const struct got_error *
1651 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1653 const struct got_error *err = NULL;
1654 char *new_fileindex_path = NULL;
1655 FILE *new_index = NULL;
1657 err = got_opentemp_named(&new_fileindex_path, &new_index,
1662 err = got_fileindex_write(fileindex, new_index);
1666 if (rename(new_fileindex_path, fileindex_path) != 0) {
1667 err = got_error_from_errno3("rename", new_fileindex_path,
1669 unlink(new_fileindex_path);
1674 free(new_fileindex_path);
1678 static const struct got_error *
1679 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1680 struct got_object_id **tree_id, const char *wt_relpath,
1681 struct got_worktree *worktree, struct got_repository *repo)
1683 const struct got_error *err = NULL;
1684 struct got_object_id *id = NULL;
1685 char *in_repo_path = NULL;
1686 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1688 *entry_type = GOT_OBJ_TYPE_ANY;
1689 *tree_relpath = NULL;
1692 if (wt_relpath[0] == '\0') {
1693 /* Check out all files within the work tree. */
1694 *entry_type = GOT_OBJ_TYPE_TREE;
1695 *tree_relpath = strdup("");
1696 if (*tree_relpath == NULL) {
1697 err = got_error_from_errno("strdup");
1700 err = got_object_id_by_path(tree_id, repo,
1701 worktree->base_commit_id, worktree->path_prefix);
1707 /* Check out a subset of files in the work tree. */
1709 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1710 is_root_wt ? "" : "/", wt_relpath) == -1) {
1711 err = got_error_from_errno("asprintf");
1715 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1721 in_repo_path = NULL;
1723 err = got_object_get_type(entry_type, repo, id);
1727 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1728 /* Check out a single file. */
1729 if (strchr(wt_relpath, '/') == NULL) {
1730 /* Check out a single file in work tree's root dir. */
1731 in_repo_path = strdup(worktree->path_prefix);
1732 if (in_repo_path == NULL) {
1733 err = got_error_from_errno("strdup");
1736 *tree_relpath = strdup("");
1737 if (*tree_relpath == NULL) {
1738 err = got_error_from_errno("strdup");
1742 /* Check out a single file in a subdirectory. */
1743 err = got_path_dirname(tree_relpath, wt_relpath);
1746 if (asprintf(&in_repo_path, "%s%s%s",
1747 worktree->path_prefix, is_root_wt ? "" : "/",
1748 *tree_relpath) == -1) {
1749 err = got_error_from_errno("asprintf");
1753 err = got_object_id_by_path(tree_id, repo,
1754 worktree->base_commit_id, in_repo_path);
1756 /* Check out all files within a subdirectory. */
1757 *tree_id = got_object_id_dup(id);
1758 if (*tree_id == NULL) {
1759 err = got_error_from_errno("got_object_id_dup");
1762 *tree_relpath = strdup(wt_relpath);
1763 if (*tree_relpath == NULL) {
1764 err = got_error_from_errno("strdup");
1772 *entry_type = GOT_OBJ_TYPE_ANY;
1773 free(*tree_relpath);
1774 *tree_relpath = NULL;
1781 static const struct got_error *
1782 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1783 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1784 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1785 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1787 const struct got_error *err = NULL;
1788 struct got_commit_object *commit = NULL;
1789 struct got_tree_object *tree = NULL;
1790 struct got_fileindex_diff_tree_cb diff_cb;
1791 struct diff_cb_arg arg;
1793 err = ref_base_commit(worktree, repo);
1797 err = got_object_open_as_commit(&commit, repo,
1798 worktree->base_commit_id);
1802 err = got_object_open_as_tree(&tree, repo, tree_id);
1807 got_object_tree_find_entry(tree, entry_name) == NULL) {
1808 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1812 diff_cb.diff_old_new = diff_old_new;
1813 diff_cb.diff_old = diff_old;
1814 diff_cb.diff_new = diff_new;
1815 arg.fileindex = fileindex;
1816 arg.worktree = worktree;
1818 arg.progress_cb = progress_cb;
1819 arg.progress_arg = progress_arg;
1820 arg.cancel_cb = cancel_cb;
1821 arg.cancel_arg = cancel_arg;
1822 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1823 entry_name, repo, &diff_cb, &arg);
1826 got_object_tree_close(tree);
1828 got_object_commit_close(commit);
1832 const struct got_error *
1833 got_worktree_checkout_files(struct got_worktree *worktree,
1834 struct got_pathlist_head *paths, struct got_repository *repo,
1835 got_worktree_checkout_cb progress_cb, void *progress_arg,
1836 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1838 const struct got_error *err = NULL, *sync_err, *unlockerr;
1839 struct got_commit_object *commit = NULL;
1840 struct got_tree_object *tree = NULL;
1841 struct got_fileindex *fileindex = NULL;
1842 char *fileindex_path = NULL;
1843 struct got_pathlist_entry *pe;
1844 struct tree_path_data {
1845 SIMPLEQ_ENTRY(tree_path_data) entry;
1846 struct got_object_id *tree_id;
1851 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1853 SIMPLEQ_INIT(&tree_paths);
1855 err = lock_worktree(worktree, LOCK_EX);
1859 /* Map all specified paths to in-repository trees. */
1860 TAILQ_FOREACH(pe, paths, entry) {
1861 tpd = malloc(sizeof(*tpd));
1863 err = got_error_from_errno("malloc");
1867 err = find_tree_entry_for_checkout(&tpd->entry_type,
1868 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1874 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1875 err = got_path_basename(&tpd->entry_name, pe->path);
1883 tpd->entry_name = NULL;
1885 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1889 * Read the file index.
1890 * Checking out files is supposed to be an idempotent operation.
1891 * If the on-disk file index is incomplete we will try to complete it.
1893 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1897 tpd = SIMPLEQ_FIRST(&tree_paths);
1898 TAILQ_FOREACH(pe, paths, entry) {
1899 struct bump_base_commit_id_arg bbc_arg;
1901 err = checkout_files(worktree, fileindex, tpd->relpath,
1902 tpd->tree_id, tpd->entry_name, repo,
1903 progress_cb, progress_arg, cancel_cb, cancel_arg);
1907 bbc_arg.base_commit_id = worktree->base_commit_id;
1908 bbc_arg.entry_name = tpd->entry_name;
1909 bbc_arg.path = pe->path;
1910 bbc_arg.path_len = pe->path_len;
1911 bbc_arg.progress_cb = progress_cb;
1912 bbc_arg.progress_arg = progress_arg;
1913 err = got_fileindex_for_each_entry_safe(fileindex,
1914 bump_base_commit_id, &bbc_arg);
1918 tpd = SIMPLEQ_NEXT(tpd, entry);
1920 sync_err = sync_fileindex(fileindex, fileindex_path);
1921 if (sync_err && err == NULL)
1924 free(fileindex_path);
1926 got_object_tree_close(tree);
1928 got_object_commit_close(commit);
1930 got_fileindex_free(fileindex);
1931 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1932 tpd = SIMPLEQ_FIRST(&tree_paths);
1933 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1938 unlockerr = lock_worktree(worktree, LOCK_SH);
1939 if (unlockerr && err == NULL)
1944 struct merge_file_cb_arg {
1945 struct got_worktree *worktree;
1946 struct got_fileindex *fileindex;
1947 got_worktree_checkout_cb progress_cb;
1949 got_worktree_cancel_cb cancel_cb;
1951 struct got_object_id *commit_id2;
1954 static const struct got_error *
1955 merge_file_cb(void *arg, struct got_blob_object *blob1,
1956 struct got_blob_object *blob2, struct got_object_id *id1,
1957 struct got_object_id *id2, const char *path1, const char *path2,
1958 struct got_repository *repo)
1960 static const struct got_error *err = NULL;
1961 struct merge_file_cb_arg *a = arg;
1962 struct got_fileindex_entry *ie;
1963 char *ondisk_path = NULL;
1965 unsigned char status;
1966 int local_changes_subsumed;
1968 if (blob1 && blob2) {
1969 ie = got_fileindex_entry_get(a->fileindex, path2,
1972 return (*a->progress_cb)(a->progress_arg,
1973 GOT_STATUS_MISSING, path2);
1975 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1977 return got_error_from_errno("asprintf");
1979 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1983 if (status == GOT_STATUS_DELETE) {
1984 err = (*a->progress_cb)(a->progress_arg,
1985 GOT_STATUS_MERGE, path2);
1988 if (status != GOT_STATUS_NO_CHANGE &&
1989 status != GOT_STATUS_MODIFY &&
1990 status != GOT_STATUS_CONFLICT &&
1991 status != GOT_STATUS_ADD) {
1992 err = (*a->progress_cb)(a->progress_arg, status, path2);
1996 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1997 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1998 a->progress_cb, a->progress_arg);
2000 ie = got_fileindex_entry_get(a->fileindex, path1,
2003 return (*a->progress_cb)(a->progress_arg,
2004 GOT_STATUS_MISSING, path2);
2006 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2008 return got_error_from_errno("asprintf");
2010 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2015 case GOT_STATUS_NO_CHANGE:
2016 err = (*a->progress_cb)(a->progress_arg,
2017 GOT_STATUS_DELETE, path1);
2020 err = remove_ondisk_file(a->worktree->root_path, path1);
2024 got_fileindex_entry_mark_deleted_from_disk(ie);
2026 case GOT_STATUS_DELETE:
2027 case GOT_STATUS_MISSING:
2028 err = (*a->progress_cb)(a->progress_arg,
2029 GOT_STATUS_DELETE, path1);
2033 got_fileindex_entry_mark_deleted_from_disk(ie);
2035 case GOT_STATUS_ADD:
2036 case GOT_STATUS_MODIFY:
2037 case GOT_STATUS_CONFLICT:
2038 err = (*a->progress_cb)(a->progress_arg,
2039 GOT_STATUS_CANNOT_DELETE, path1);
2043 case GOT_STATUS_OBSTRUCTED:
2044 err = (*a->progress_cb)(a->progress_arg, status, path1);
2052 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2054 return got_error_from_errno("asprintf");
2055 ie = got_fileindex_entry_get(a->fileindex, path2,
2058 err = get_file_status(&status, &sb, ie, ondisk_path,
2062 if (status != GOT_STATUS_NO_CHANGE &&
2063 status != GOT_STATUS_MODIFY &&
2064 status != GOT_STATUS_CONFLICT &&
2065 status != GOT_STATUS_ADD) {
2066 err = (*a->progress_cb)(a->progress_arg,
2070 err = merge_blob(&local_changes_subsumed, a->worktree,
2071 NULL, ondisk_path, path2, sb.st_mode, blob2,
2072 a->commit_id2, repo,
2073 a->progress_cb, a->progress_arg);
2074 if (status == GOT_STATUS_DELETE) {
2075 err = update_blob_fileindex_entry(a->worktree,
2076 a->fileindex, ie, ondisk_path, ie->path,
2082 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2083 err = install_blob(a->worktree, ondisk_path, path2,
2084 /* XXX get this from parent tree! */
2085 GOT_DEFAULT_FILE_MODE,
2086 sb.st_mode, blob2, 0, 0, repo,
2087 a->progress_cb, a->progress_arg);
2090 err = got_fileindex_entry_alloc(&ie,
2091 ondisk_path, path2, NULL, NULL);
2094 err = got_fileindex_entry_add(a->fileindex, ie);
2096 got_fileindex_entry_free(ie);
2106 struct check_merge_ok_arg {
2107 struct got_worktree *worktree;
2108 struct got_repository *repo;
2111 static const struct got_error *
2112 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2114 const struct got_error *err = NULL;
2115 struct check_merge_ok_arg *a = arg;
2116 unsigned char status;
2120 /* Reject merges into a work tree with mixed base commits. */
2121 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2122 SHA1_DIGEST_LENGTH))
2123 return got_error(GOT_ERR_MIXED_COMMITS);
2125 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2127 return got_error_from_errno("asprintf");
2129 /* Reject merges into a work tree with conflicted files. */
2130 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2133 if (status == GOT_STATUS_CONFLICT)
2134 return got_error(GOT_ERR_CONFLICTS);
2139 static const struct got_error *
2140 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2141 const char *fileindex_path, struct got_object_id *commit_id1,
2142 struct got_object_id *commit_id2, struct got_repository *repo,
2143 got_worktree_checkout_cb progress_cb, void *progress_arg,
2144 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2146 const struct got_error *err = NULL, *sync_err;
2147 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2148 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2149 struct merge_file_cb_arg arg;
2152 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2153 worktree->path_prefix);
2157 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2162 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2163 worktree->path_prefix);
2167 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2171 arg.worktree = worktree;
2172 arg.fileindex = fileindex;
2173 arg.progress_cb = progress_cb;
2174 arg.progress_arg = progress_arg;
2175 arg.cancel_cb = cancel_cb;
2176 arg.cancel_arg = cancel_arg;
2177 arg.commit_id2 = commit_id2;
2178 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2179 sync_err = sync_fileindex(fileindex, fileindex_path);
2180 if (sync_err && err == NULL)
2184 got_object_tree_close(tree1);
2186 got_object_tree_close(tree2);
2190 const struct got_error *
2191 got_worktree_merge_files(struct got_worktree *worktree,
2192 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2193 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2194 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2196 const struct got_error *err, *unlockerr;
2197 char *fileindex_path = NULL;
2198 struct got_fileindex *fileindex = NULL;
2199 struct check_merge_ok_arg mok_arg;
2201 err = lock_worktree(worktree, LOCK_EX);
2205 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2209 mok_arg.worktree = worktree;
2210 mok_arg.repo = repo;
2211 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2216 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2217 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2220 got_fileindex_free(fileindex);
2221 free(fileindex_path);
2222 unlockerr = lock_worktree(worktree, LOCK_SH);
2223 if (unlockerr && err == NULL)
2228 struct diff_dir_cb_arg {
2229 struct got_fileindex *fileindex;
2230 struct got_worktree *worktree;
2231 const char *status_path;
2232 size_t status_path_len;
2233 struct got_repository *repo;
2234 got_worktree_status_cb status_cb;
2236 got_worktree_cancel_cb cancel_cb;
2238 /* A pathlist containing per-directory pathlists of ignore patterns. */
2239 struct got_pathlist_head ignores;
2242 static const struct got_error *
2243 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2244 got_worktree_status_cb status_cb, void *status_arg,
2245 struct got_repository *repo)
2247 const struct got_error *err = NULL;
2248 unsigned char status = GOT_STATUS_NO_CHANGE;
2249 unsigned char staged_status = get_staged_status(ie);
2251 struct got_object_id blob_id, commit_id, staged_blob_id;
2252 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2253 struct got_object_id *staged_blob_idp = NULL;
2255 err = get_file_status(&status, &sb, ie, abspath, repo);
2259 if (status == GOT_STATUS_NO_CHANGE &&
2260 staged_status == GOT_STATUS_NO_CHANGE)
2263 if (got_fileindex_entry_has_blob(ie)) {
2264 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2265 blob_idp = &blob_id;
2267 if (got_fileindex_entry_has_commit(ie)) {
2268 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2269 commit_idp = &commit_id;
2271 if (staged_status == GOT_STATUS_ADD ||
2272 staged_status == GOT_STATUS_MODIFY) {
2273 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2274 SHA1_DIGEST_LENGTH);
2275 staged_blob_idp = &staged_blob_id;
2278 return (*status_cb)(status_arg, status, staged_status,
2279 ie->path, blob_idp, staged_blob_idp, commit_idp);
2282 static const struct got_error *
2283 status_old_new(void *arg, struct got_fileindex_entry *ie,
2284 struct dirent *de, const char *parent_path)
2286 const struct got_error *err = NULL;
2287 struct diff_dir_cb_arg *a = arg;
2290 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 return got_error(GOT_ERR_CANCELLED);
2293 if (got_path_cmp(parent_path, a->status_path,
2294 strlen(parent_path), a->status_path_len) != 0 &&
2295 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2298 if (parent_path[0]) {
2299 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2300 parent_path, de->d_name) == -1)
2301 return got_error_from_errno("asprintf");
2303 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2305 return got_error_from_errno("asprintf");
2308 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2314 static const struct got_error *
2315 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2317 struct diff_dir_cb_arg *a = arg;
2318 struct got_object_id blob_id, commit_id;
2319 unsigned char status;
2321 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2322 return got_error(GOT_ERR_CANCELLED);
2324 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2327 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2328 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2329 if (got_fileindex_entry_has_file_on_disk(ie))
2330 status = GOT_STATUS_MISSING;
2332 status = GOT_STATUS_DELETE;
2333 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2334 ie->path, &blob_id, NULL, &commit_id);
2338 free_ignorelist(struct got_pathlist_head *ignorelist)
2340 struct got_pathlist_entry *pe;
2342 TAILQ_FOREACH(pe, ignorelist, entry)
2343 free((char *)pe->path);
2344 got_pathlist_free(ignorelist);
2348 free_ignores(struct got_pathlist_head *ignores)
2350 struct got_pathlist_entry *pe;
2352 TAILQ_FOREACH(pe, ignores, entry) {
2353 struct got_pathlist_head *ignorelist = pe->data;
2354 free_ignorelist(ignorelist);
2355 free((char *)pe->path);
2357 got_pathlist_free(ignores);
2360 static const struct got_error *
2361 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2363 const struct got_error *err = NULL;
2364 struct got_pathlist_entry *pe = NULL;
2365 struct got_pathlist_head *ignorelist;
2366 char *line = NULL, *pattern, *dirpath = NULL;
2367 size_t linesize = 0;
2370 ignorelist = calloc(1, sizeof(*ignorelist));
2371 if (ignorelist == NULL)
2372 return got_error_from_errno("calloc");
2373 TAILQ_INIT(ignorelist);
2375 while ((linelen = getline(&line, &linesize, f)) != -1) {
2376 if (linelen > 0 && line[linelen - 1] == '\n')
2377 line[linelen - 1] = '\0';
2378 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2380 err = got_error_from_errno("asprintf");
2383 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2388 err = got_error_from_errno("getline");
2392 dirpath = strdup(path);
2393 if (dirpath == NULL) {
2394 err = got_error_from_errno("strdup");
2397 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2400 if (err || pe == NULL) {
2402 free_ignorelist(ignorelist);
2408 match_ignores(struct got_pathlist_head *ignores, const char *path)
2410 struct got_pathlist_entry *pe;
2413 * The ignores pathlist contains ignore lists from children before
2414 * parents, so we can find the most specific ignorelist by walking
2415 * ignores backwards.
2417 pe = TAILQ_LAST(ignores, got_pathlist_head);
2419 if (got_path_is_child(path, pe->path, pe->path_len)) {
2420 struct got_pathlist_head *ignorelist = pe->data;
2421 struct got_pathlist_entry *pi;
2422 TAILQ_FOREACH(pi, ignorelist, entry) {
2423 if (fnmatch(pi->path, path,
2424 FNM_PATHNAME | FNM_LEADING_DIR))
2429 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2435 static const struct got_error *
2436 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2439 const struct got_error *err = NULL;
2441 FILE *ignoresfile = NULL;
2443 /* TODO: read .gitignores as well... */
2444 if (asprintf(&ignorespath, "%s/%s%s.cvsignore", root_path, path,
2445 path[0] ? "/" : "") == -1)
2446 return got_error_from_errno("asprintf");
2448 ignoresfile = fopen(ignorespath, "r");
2449 if (ignoresfile == NULL) {
2450 if (errno != ENOENT)
2451 err = got_error_from_errno2("fopen",
2454 err = read_ignores(ignores, path, ignoresfile);
2456 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2457 err = got_error_from_errno2("flose", path);
2462 static const struct got_error *
2463 status_new(void *arg, struct dirent *de, const char *parent_path)
2465 const struct got_error *err = NULL;
2466 struct diff_dir_cb_arg *a = arg;
2469 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2470 return got_error(GOT_ERR_CANCELLED);
2472 /* XXX ignore symlinks for now */
2473 if (de->d_type == DT_LNK)
2476 if (parent_path[0]) {
2477 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2478 return got_error_from_errno("asprintf");
2483 if (de->d_type == DT_DIR)
2484 err = add_ignores(&a->ignores, a->worktree->root_path, path);
2485 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2486 && !match_ignores(&a->ignores, path))
2487 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2488 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2494 static const struct got_error *
2495 report_single_file_status(const char *path, const char *ondisk_path,
2496 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2497 void *status_arg, struct got_repository *repo)
2499 struct got_fileindex_entry *ie;
2502 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2504 return report_file_status(ie, ondisk_path, status_cb,
2507 if (lstat(ondisk_path, &sb) == -1) {
2508 if (errno != ENOENT)
2509 return got_error_from_errno2("lstat", ondisk_path);
2513 if (S_ISREG(sb.st_mode))
2514 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2515 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2520 static const struct got_error *
2521 worktree_status(struct got_worktree *worktree, const char *path,
2522 struct got_fileindex *fileindex, struct got_repository *repo,
2523 got_worktree_status_cb status_cb, void *status_arg,
2524 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2526 const struct got_error *err = NULL;
2527 DIR *workdir = NULL;
2528 struct got_fileindex_diff_dir_cb fdiff_cb;
2529 struct diff_dir_cb_arg arg;
2530 char *ondisk_path = NULL;
2532 if (asprintf(&ondisk_path, "%s%s%s",
2533 worktree->root_path, path[0] ? "/" : "", path) == -1)
2534 return got_error_from_errno("asprintf");
2536 workdir = opendir(ondisk_path);
2537 if (workdir == NULL) {
2538 if (errno != ENOTDIR && errno != ENOENT)
2539 err = got_error_from_errno2("opendir", ondisk_path);
2541 err = report_single_file_status(path, ondisk_path,
2542 fileindex, status_cb, status_arg, repo);
2544 fdiff_cb.diff_old_new = status_old_new;
2545 fdiff_cb.diff_old = status_old;
2546 fdiff_cb.diff_new = status_new;
2547 arg.fileindex = fileindex;
2548 arg.worktree = worktree;
2549 arg.status_path = path;
2550 arg.status_path_len = strlen(path);
2552 arg.status_cb = status_cb;
2553 arg.status_arg = status_arg;
2554 arg.cancel_cb = cancel_cb;
2555 arg.cancel_arg = cancel_arg;
2556 TAILQ_INIT(&arg.ignores);
2557 err = add_ignores(&arg.ignores, worktree->root_path, path);
2559 err = got_fileindex_diff_dir(fileindex, workdir,
2560 worktree->root_path, path, repo, &fdiff_cb, &arg);
2561 free_ignores(&arg.ignores);
2570 const struct got_error *
2571 got_worktree_status(struct got_worktree *worktree,
2572 struct got_pathlist_head *paths, struct got_repository *repo,
2573 got_worktree_status_cb status_cb, void *status_arg,
2574 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2576 const struct got_error *err = NULL;
2577 char *fileindex_path = NULL;
2578 struct got_fileindex *fileindex = NULL;
2579 struct got_pathlist_entry *pe;
2581 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2585 TAILQ_FOREACH(pe, paths, entry) {
2586 err = worktree_status(worktree, pe->path, fileindex, repo,
2587 status_cb, status_arg, cancel_cb, cancel_arg);
2591 free(fileindex_path);
2592 got_fileindex_free(fileindex);
2596 const struct got_error *
2597 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2600 const struct got_error *err = NULL;
2601 char *resolved, *cwd = NULL, *path = NULL;
2606 resolved = realpath(arg, NULL);
2607 if (resolved == NULL) {
2608 if (errno != ENOENT)
2609 return got_error_from_errno2("realpath", arg);
2610 cwd = getcwd(NULL, 0);
2612 return got_error_from_errno("getcwd");
2613 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2614 err = got_error_from_errno("asprintf");
2619 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2620 strlen(got_worktree_get_root_path(worktree)))) {
2621 err = got_error(GOT_ERR_BAD_PATH);
2625 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2626 err = got_path_skip_common_ancestor(&path,
2627 got_worktree_get_root_path(worktree), resolved);
2633 err = got_error_from_errno("strdup");
2638 /* XXX status walk can't deal with trailing slash! */
2640 while (len > 0 && path[len - 1] == '/') {
2641 path[len - 1] = '\0';
2654 static const struct got_error *
2655 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2656 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2657 struct got_repository *repo)
2659 const struct got_error *err = NULL;
2660 struct got_fileindex_entry *ie;
2661 unsigned char status;
2664 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2666 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2669 /* Re-adding an existing entry is a no-op. */
2670 if (status == GOT_STATUS_ADD)
2672 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2675 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2679 err = got_fileindex_entry_add(fileindex, ie);
2681 got_fileindex_entry_free(ie);
2685 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2688 const struct got_error *
2689 got_worktree_schedule_add(struct got_worktree *worktree,
2690 struct got_pathlist_head *paths,
2691 got_worktree_status_cb status_cb, void *status_arg,
2692 struct got_repository *repo)
2694 struct got_fileindex *fileindex = NULL;
2695 char *fileindex_path = NULL;
2696 const struct got_error *err = NULL, *sync_err, *unlockerr;
2697 struct got_pathlist_entry *pe;
2699 err = lock_worktree(worktree, LOCK_EX);
2703 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2707 TAILQ_FOREACH(pe, paths, entry) {
2709 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2711 return got_error_from_errno("asprintf");
2712 err = schedule_addition(ondisk_path, fileindex, pe->path,
2713 status_cb, status_arg, repo);
2718 sync_err = sync_fileindex(fileindex, fileindex_path);
2719 if (sync_err && err == NULL)
2722 free(fileindex_path);
2724 got_fileindex_free(fileindex);
2725 unlockerr = lock_worktree(worktree, LOCK_SH);
2726 if (unlockerr && err == NULL)
2731 static const struct got_error *
2732 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2733 const char *relpath, int delete_local_mods,
2734 got_worktree_status_cb status_cb, void *status_arg,
2735 struct got_repository *repo)
2737 const struct got_error *err = NULL;
2738 struct got_fileindex_entry *ie = NULL;
2739 unsigned char status, staged_status;
2742 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2744 return got_error(GOT_ERR_BAD_PATH);
2746 staged_status = get_staged_status(ie);
2747 if (staged_status != GOT_STATUS_NO_CHANGE) {
2748 if (staged_status == GOT_STATUS_DELETE)
2750 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2753 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2757 if (status != GOT_STATUS_NO_CHANGE) {
2758 if (status == GOT_STATUS_DELETE)
2760 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2761 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2762 if (status != GOT_STATUS_MODIFY &&
2763 status != GOT_STATUS_MISSING)
2764 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2767 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2768 return got_error_from_errno2("unlink", ondisk_path);
2770 got_fileindex_entry_mark_deleted_from_disk(ie);
2771 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2774 const struct got_error *
2775 got_worktree_schedule_delete(struct got_worktree *worktree,
2776 struct got_pathlist_head *paths, int delete_local_mods,
2777 got_worktree_status_cb status_cb, void *status_arg,
2778 struct got_repository *repo)
2780 struct got_fileindex *fileindex = NULL;
2781 char *fileindex_path = NULL;
2782 const struct got_error *err = NULL, *sync_err, *unlockerr;
2783 struct got_pathlist_entry *pe;
2785 err = lock_worktree(worktree, LOCK_EX);
2789 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2793 TAILQ_FOREACH(pe, paths, entry) {
2795 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2797 return got_error_from_errno("asprintf");
2798 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2799 delete_local_mods, status_cb, status_arg, repo);
2804 sync_err = sync_fileindex(fileindex, fileindex_path);
2805 if (sync_err && err == NULL)
2808 free(fileindex_path);
2810 got_fileindex_free(fileindex);
2811 unlockerr = lock_worktree(worktree, LOCK_SH);
2812 if (unlockerr && err == NULL)
2817 static const struct got_error *
2818 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2820 const struct got_error *err = NULL;
2822 size_t linesize = 0, n;
2825 linelen = getline(&line, &linesize, infile);
2826 if (linelen == -1) {
2827 if (ferror(infile)) {
2828 err = got_error_from_errno("getline");
2834 n = fwrite(line, 1, linelen, outfile);
2836 err = got_ferror(outfile, GOT_ERR_IO);
2841 n = fwrite(line, 1, linelen, rejectfile);
2843 err = got_ferror(outfile, GOT_ERR_IO);
2850 static const struct got_error *
2851 skip_one_line(FILE *f)
2854 size_t linesize = 0;
2857 linelen = getline(&line, &linesize, f);
2859 if (linelen == -1 && ferror(f))
2860 return got_error_from_errno("getline");
2864 static const struct got_error *
2865 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2866 int start_old, int end_old, int start_new, int end_new,
2867 FILE *outfile, FILE *rejectfile)
2869 const struct got_error *err;
2871 /* Copy old file's lines leading up to patch. */
2872 while (!feof(f1) && *line_cur1 < start_old) {
2873 err = copy_one_line(f1, outfile, NULL);
2878 /* Skip new file's lines leading up to patch. */
2879 while (!feof(f2) && *line_cur2 < start_new) {
2881 err = copy_one_line(f2, NULL, rejectfile);
2883 err = skip_one_line(f2);
2888 /* Copy patched lines. */
2889 while (!feof(f2) && *line_cur2 <= end_new) {
2890 err = copy_one_line(f2, outfile, NULL);
2895 /* Skip over old file's replaced lines. */
2896 while (!feof(f1) && *line_cur1 <= end_old) {
2898 err = copy_one_line(f1, NULL, rejectfile);
2900 err = skip_one_line(f1);
2909 static const struct got_error *
2910 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2911 FILE *outfile, FILE *rejectfile)
2913 const struct got_error *err;
2916 /* Copy old file's lines until EOF. */
2918 err = copy_one_line(f1, outfile, NULL);
2925 /* Copy new file's lines until EOF. */
2927 err = copy_one_line(f2, NULL, rejectfile);
2937 static const struct got_error *
2938 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2939 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2940 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2941 int *line_cur2, FILE *outfile, FILE *rejectfile,
2942 got_worktree_patch_cb patch_cb, void *patch_arg)
2944 const struct got_error *err = NULL;
2945 int start_old = change->cv.a;
2946 int end_old = change->cv.b;
2947 int start_new = change->cv.c;
2948 int end_new = change->cv.d;
2952 *choice = GOT_PATCH_CHOICE_NONE;
2954 hunkfile = got_opentemp();
2955 if (hunkfile == NULL)
2956 return got_error_from_errno("got_opentemp");
2961 /* XXX TODO needs error checking */
2962 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2964 if (fseek(f1, pos1, SEEK_SET) == -1) {
2965 err = got_ferror(f1, GOT_ERR_IO);
2968 if (fseek(f2, pos2, SEEK_SET) == -1) {
2969 err = got_ferror(f1, GOT_ERR_IO);
2972 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2973 err = got_ferror(hunkfile, GOT_ERR_IO);
2977 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2978 hunkfile, n, nchanges);
2983 case GOT_PATCH_CHOICE_YES:
2984 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2985 end_old, start_new, end_new, outfile, rejectfile);
2987 case GOT_PATCH_CHOICE_NO:
2988 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2989 end_old, start_new, end_new, rejectfile, outfile);
2991 case GOT_PATCH_CHOICE_QUIT:
2994 err = got_error(GOT_ERR_PATCH_CHOICE);
2998 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
2999 err = got_error_from_errno("fclose");
3003 struct revert_file_args {
3004 struct got_worktree *worktree;
3005 struct got_fileindex *fileindex;
3006 got_worktree_checkout_cb progress_cb;
3008 got_worktree_patch_cb patch_cb;
3010 struct got_repository *repo;
3013 static const struct got_error *
3014 create_patched_content(char **path_outfile, int reverse_patch,
3015 struct got_object_id *blob_id, const char *path2,
3016 const char *relpath, struct got_repository *repo,
3017 got_worktree_patch_cb patch_cb, void *patch_arg)
3019 const struct got_error *err;
3020 struct got_blob_object *blob = NULL;
3021 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3022 char *path1 = NULL, *id_str = NULL;
3023 struct stat sb1, sb2;
3024 struct got_diff_changes *changes = NULL;
3025 struct got_diff_state *ds = NULL;
3026 struct got_diff_args *args = NULL;
3027 struct got_diff_change *change;
3028 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3031 *path_outfile = NULL;
3033 err = got_object_id_str(&id_str, blob_id);
3037 f2 = fopen(path2, "r");
3039 err = got_error_from_errno2("fopen", path2);
3043 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3047 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3051 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3055 if (stat(path1, &sb1) == -1) {
3056 err = got_error_from_errno2("stat", path1);
3059 if (stat(path2, &sb2) == -1) {
3060 err = got_error_from_errno2("stat", path2);
3064 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3065 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3069 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3073 if (fseek(f1, 0L, SEEK_SET) == -1)
3074 return got_ferror(f1, GOT_ERR_IO);
3075 if (fseek(f2, 0L, SEEK_SET) == -1)
3076 return got_ferror(f2, GOT_ERR_IO);
3077 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3079 err = apply_or_reject_change(&choice, change, ++n,
3080 changes->nchanges, ds, args, diff_flags, relpath,
3081 f1, f2, &line_cur1, &line_cur2,
3082 reverse_patch ? NULL : outfile,
3083 reverse_patch ? outfile : NULL,
3084 patch_cb, patch_arg);
3087 if (choice == GOT_PATCH_CHOICE_YES)
3089 else if (choice == GOT_PATCH_CHOICE_QUIT)
3093 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3094 reverse_patch ? NULL : outfile,
3095 reverse_patch ? outfile : NULL);
3099 got_object_blob_close(blob);
3100 if (f1 && fclose(f1) == EOF && err == NULL)
3101 err = got_error_from_errno2("fclose", path1);
3102 if (f2 && fclose(f2) == EOF && err == NULL)
3103 err = got_error_from_errno2("fclose", path2);
3104 if (outfile && fclose(outfile) == EOF && err == NULL)
3105 err = got_error_from_errno2("fclose", *path_outfile);
3106 if (path1 && unlink(path1) == -1 && err == NULL)
3107 err = got_error_from_errno2("unlink", path1);
3108 if (err || !have_content) {
3109 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3110 err = got_error_from_errno2("unlink", *path_outfile);
3111 free(*path_outfile);
3112 *path_outfile = NULL;
3116 got_diff_state_free(ds);
3120 got_diff_free_changes(changes);
3125 static const struct got_error *
3126 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3127 const char *relpath, struct got_object_id *blob_id,
3128 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3130 struct revert_file_args *a = arg;
3131 const struct got_error *err = NULL;
3132 char *parent_path = NULL;
3133 struct got_fileindex_entry *ie;
3134 struct got_tree_object *tree = NULL;
3135 struct got_object_id *tree_id = NULL;
3136 const struct got_tree_entry *te = NULL;
3137 char *tree_path = NULL, *te_name;
3138 char *ondisk_path = NULL, *path_content = NULL;
3139 struct got_blob_object *blob = NULL;
3141 /* Reverting a staged deletion is a no-op. */
3142 if (status == GOT_STATUS_DELETE &&
3143 staged_status != GOT_STATUS_NO_CHANGE)
3146 if (status == GOT_STATUS_UNVERSIONED)
3147 return (*a->progress_cb)(a->progress_arg,
3148 GOT_STATUS_UNVERSIONED, relpath);
3150 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3152 return got_error(GOT_ERR_BAD_PATH);
3154 /* Construct in-repository path of tree which contains this blob. */
3155 err = got_path_dirname(&parent_path, ie->path);
3157 if (err->code != GOT_ERR_BAD_PATH)
3159 parent_path = strdup("/");
3160 if (parent_path == NULL) {
3161 err = got_error_from_errno("strdup");
3165 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3166 tree_path = strdup(parent_path);
3167 if (tree_path == NULL) {
3168 err = got_error_from_errno("strdup");
3172 if (got_path_is_root_dir(parent_path)) {
3173 tree_path = strdup(a->worktree->path_prefix);
3174 if (tree_path == NULL) {
3175 err = got_error_from_errno("strdup");
3179 if (asprintf(&tree_path, "%s/%s",
3180 a->worktree->path_prefix, parent_path) == -1) {
3181 err = got_error_from_errno("asprintf");
3187 err = got_object_id_by_path(&tree_id, a->repo,
3188 a->worktree->base_commit_id, tree_path);
3190 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3191 (status == GOT_STATUS_ADD ||
3192 staged_status == GOT_STATUS_ADD)))
3195 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3199 te_name = basename(ie->path);
3200 if (te_name == NULL) {
3201 err = got_error_from_errno2("basename", ie->path);
3205 te = got_object_tree_find_entry(tree, te_name);
3206 if (te == NULL && status != GOT_STATUS_ADD &&
3207 staged_status != GOT_STATUS_ADD) {
3208 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3214 case GOT_STATUS_ADD:
3216 int choice = GOT_PATCH_CHOICE_NONE;
3217 err = (*a->patch_cb)(&choice, a->patch_arg,
3218 status, ie->path, NULL, 1, 1);
3221 if (choice != GOT_PATCH_CHOICE_YES)
3224 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3228 got_fileindex_entry_remove(a->fileindex, ie);
3230 case GOT_STATUS_DELETE:
3232 int choice = GOT_PATCH_CHOICE_NONE;
3233 err = (*a->patch_cb)(&choice, a->patch_arg,
3234 status, ie->path, NULL, 1, 1);
3237 if (choice != GOT_PATCH_CHOICE_YES)
3241 case GOT_STATUS_MODIFY:
3242 case GOT_STATUS_CONFLICT:
3243 case GOT_STATUS_MISSING: {
3244 struct got_object_id id;
3245 if (staged_status == GOT_STATUS_ADD ||
3246 staged_status == GOT_STATUS_MODIFY) {
3247 memcpy(id.sha1, ie->staged_blob_sha1,
3248 SHA1_DIGEST_LENGTH);
3250 memcpy(id.sha1, ie->blob_sha1,
3251 SHA1_DIGEST_LENGTH);
3252 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3256 if (asprintf(&ondisk_path, "%s/%s",
3257 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3258 err = got_error_from_errno("asprintf");
3262 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3263 status == GOT_STATUS_CONFLICT)) {
3264 err = create_patched_content(&path_content, 1, &id,
3265 ondisk_path, ie->path, a->repo,
3266 a->patch_cb, a->patch_arg);
3267 if (err || path_content == NULL)
3269 if (rename(path_content, ondisk_path) == -1) {
3270 err = got_error_from_errno3("rename",
3271 path_content, ondisk_path);
3275 err = install_blob(a->worktree, ondisk_path, ie->path,
3276 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3277 got_fileindex_perms_to_st(ie), blob, 0, 1,
3278 a->repo, a->progress_cb, a->progress_arg);
3281 if (status == GOT_STATUS_DELETE) {
3282 err = update_blob_fileindex_entry(a->worktree,
3283 a->fileindex, ie, ondisk_path, ie->path,
3300 got_object_blob_close(blob);
3302 got_object_tree_close(tree);
3307 const struct got_error *
3308 got_worktree_revert(struct got_worktree *worktree,
3309 struct got_pathlist_head *paths,
3310 got_worktree_checkout_cb progress_cb, void *progress_arg,
3311 got_worktree_patch_cb patch_cb, void *patch_arg,
3312 struct got_repository *repo)
3314 struct got_fileindex *fileindex = NULL;
3315 char *fileindex_path = NULL;
3316 const struct got_error *err = NULL, *unlockerr = NULL;
3317 const struct got_error *sync_err = NULL;
3318 struct got_pathlist_entry *pe;
3319 struct revert_file_args rfa;
3321 err = lock_worktree(worktree, LOCK_EX);
3325 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3329 rfa.worktree = worktree;
3330 rfa.fileindex = fileindex;
3331 rfa.progress_cb = progress_cb;
3332 rfa.progress_arg = progress_arg;
3333 rfa.patch_cb = patch_cb;
3334 rfa.patch_arg = patch_arg;
3336 TAILQ_FOREACH(pe, paths, entry) {
3337 err = worktree_status(worktree, pe->path, fileindex, repo,
3338 revert_file, &rfa, NULL, NULL);
3342 sync_err = sync_fileindex(fileindex, fileindex_path);
3343 if (sync_err && err == NULL)
3346 free(fileindex_path);
3348 got_fileindex_free(fileindex);
3349 unlockerr = lock_worktree(worktree, LOCK_SH);
3350 if (unlockerr && err == NULL)
3356 free_commitable(struct got_commitable *ct)
3359 free(ct->in_repo_path);
3360 free(ct->ondisk_path);
3362 free(ct->base_blob_id);
3363 free(ct->staged_blob_id);
3364 free(ct->base_commit_id);
3368 struct collect_commitables_arg {
3369 struct got_pathlist_head *commitable_paths;
3370 struct got_repository *repo;
3371 struct got_worktree *worktree;
3372 int have_staged_files;
3375 static const struct got_error *
3376 collect_commitables(void *arg, unsigned char status,
3377 unsigned char staged_status, const char *relpath,
3378 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3379 struct got_object_id *commit_id)
3381 struct collect_commitables_arg *a = arg;
3382 const struct got_error *err = NULL;
3383 struct got_commitable *ct = NULL;
3384 struct got_pathlist_entry *new = NULL;
3385 char *parent_path = NULL, *path = NULL;
3388 if (a->have_staged_files) {
3389 if (staged_status != GOT_STATUS_MODIFY &&
3390 staged_status != GOT_STATUS_ADD &&
3391 staged_status != GOT_STATUS_DELETE)
3394 if (status == GOT_STATUS_CONFLICT)
3395 return got_error(GOT_ERR_COMMIT_CONFLICT);
3397 if (status != GOT_STATUS_MODIFY &&
3398 status != GOT_STATUS_ADD &&
3399 status != GOT_STATUS_DELETE)
3403 if (asprintf(&path, "/%s", relpath) == -1) {
3404 err = got_error_from_errno("asprintf");
3407 if (strcmp(path, "/") == 0) {
3408 parent_path = strdup("");
3409 if (parent_path == NULL)
3410 return got_error_from_errno("strdup");
3412 err = got_path_dirname(&parent_path, path);
3417 ct = calloc(1, sizeof(*ct));
3419 err = got_error_from_errno("calloc");
3423 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3425 err = got_error_from_errno("asprintf");
3428 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3429 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3431 if (lstat(ct->ondisk_path, &sb) != 0) {
3432 err = got_error_from_errno2("lstat", ct->ondisk_path);
3435 ct->mode = sb.st_mode;
3438 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3439 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3441 err = got_error_from_errno("asprintf");
3445 ct->status = status;
3446 ct->staged_status = staged_status;
3447 ct->blob_id = NULL; /* will be filled in when blob gets created */
3448 if (ct->status != GOT_STATUS_ADD &&
3449 ct->staged_status != GOT_STATUS_ADD) {
3450 ct->base_blob_id = got_object_id_dup(blob_id);
3451 if (ct->base_blob_id == NULL) {
3452 err = got_error_from_errno("got_object_id_dup");
3455 ct->base_commit_id = got_object_id_dup(commit_id);
3456 if (ct->base_commit_id == NULL) {
3457 err = got_error_from_errno("got_object_id_dup");
3461 if (ct->staged_status == GOT_STATUS_ADD ||
3462 ct->staged_status == GOT_STATUS_MODIFY) {
3463 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3464 if (ct->staged_blob_id == NULL) {
3465 err = got_error_from_errno("got_object_id_dup");
3469 ct->path = strdup(path);
3470 if (ct->path == NULL) {
3471 err = got_error_from_errno("strdup");
3474 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3476 if (ct && (err || new == NULL))
3477 free_commitable(ct);
3483 static const struct got_error *write_tree(struct got_object_id **,
3484 struct got_tree_object *, const char *, struct got_pathlist_head *,
3485 got_worktree_status_cb status_cb, void *status_arg,
3486 struct got_repository *);
3488 static const struct got_error *
3489 write_subtree(struct got_object_id **new_subtree_id,
3490 struct got_tree_entry *te, const char *parent_path,
3491 struct got_pathlist_head *commitable_paths,
3492 got_worktree_status_cb status_cb, void *status_arg,
3493 struct got_repository *repo)
3495 const struct got_error *err = NULL;
3496 struct got_tree_object *subtree;
3499 if (asprintf(&subpath, "%s%s%s", parent_path,
3500 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3501 return got_error_from_errno("asprintf");
3503 err = got_object_open_as_tree(&subtree, repo, te->id);
3507 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3508 status_cb, status_arg, repo);
3509 got_object_tree_close(subtree);
3514 static const struct got_error *
3515 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3517 const struct got_error *err = NULL;
3518 char *ct_parent_path = NULL;
3522 if (strchr(ct->in_repo_path, '/') == NULL) {
3523 *match = got_path_is_root_dir(path);
3527 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3530 *match = (strcmp(path, ct_parent_path) == 0);
3531 free(ct_parent_path);
3536 get_ct_file_mode(struct got_commitable *ct)
3538 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3541 static const struct got_error *
3542 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3543 struct got_tree_entry *te, struct got_commitable *ct)
3545 const struct got_error *err = NULL;
3549 err = got_object_tree_entry_dup(new_te, te);
3553 (*new_te)->mode = get_ct_file_mode(ct);
3555 free((*new_te)->id);
3556 if (ct->staged_status == GOT_STATUS_MODIFY)
3557 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3559 (*new_te)->id = got_object_id_dup(ct->blob_id);
3560 if ((*new_te)->id == NULL) {
3561 err = got_error_from_errno("got_object_id_dup");
3565 if (err && *new_te) {
3566 got_object_tree_entry_close(*new_te);
3572 static const struct got_error *
3573 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3574 struct got_commitable *ct)
3576 const struct got_error *err = NULL;
3581 *new_te = calloc(1, sizeof(**new_te));
3582 if (*new_te == NULL)
3583 return got_error_from_errno("calloc");
3585 ct_name = basename(ct->path);
3586 if (ct_name == NULL) {
3587 err = got_error_from_errno2("basename", ct->path);
3590 (*new_te)->name = strdup(ct_name);
3591 if ((*new_te)->name == NULL) {
3592 err = got_error_from_errno("strdup");
3596 (*new_te)->mode = get_ct_file_mode(ct);
3598 if (ct->staged_status == GOT_STATUS_ADD)
3599 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3601 (*new_te)->id = got_object_id_dup(ct->blob_id);
3602 if ((*new_te)->id == NULL) {
3603 err = got_error_from_errno("got_object_id_dup");
3607 if (err && *new_te) {
3608 got_object_tree_entry_close(*new_te);
3614 static const struct got_error *
3615 insert_tree_entry(struct got_tree_entry *new_te,
3616 struct got_pathlist_head *paths)
3618 const struct got_error *err = NULL;
3619 struct got_pathlist_entry *new_pe;
3621 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3625 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3629 static const struct got_error *
3630 report_ct_status(struct got_commitable *ct,
3631 got_worktree_status_cb status_cb, void *status_arg)
3633 const char *ct_path = ct->path;
3634 unsigned char status;
3636 while (ct_path[0] == '/')
3639 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3640 status = ct->staged_status;
3642 status = ct->status;
3644 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3645 ct_path, ct->blob_id, NULL, NULL);
3648 static const struct got_error *
3649 match_modified_subtree(int *modified, struct got_tree_entry *te,
3650 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3652 const struct got_error *err = NULL;
3653 struct got_pathlist_entry *pe;
3658 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3659 got_path_is_root_dir(base_tree_path) ? "" : "/",
3661 return got_error_from_errno("asprintf");
3663 TAILQ_FOREACH(pe, commitable_paths, entry) {
3664 struct got_commitable *ct = pe->data;
3665 *modified = got_path_is_child(ct->in_repo_path, te_path,
3675 static const struct got_error *
3676 match_deleted_or_modified_ct(struct got_commitable **ctp,
3677 struct got_tree_entry *te, const char *base_tree_path,
3678 struct got_pathlist_head *commitable_paths)
3680 const struct got_error *err = NULL;
3681 struct got_pathlist_entry *pe;
3685 TAILQ_FOREACH(pe, commitable_paths, entry) {
3686 struct got_commitable *ct = pe->data;
3687 char *ct_name = NULL;
3690 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3691 if (ct->status != GOT_STATUS_MODIFY &&
3692 ct->status != GOT_STATUS_DELETE)
3695 if (ct->staged_status != GOT_STATUS_MODIFY &&
3696 ct->staged_status != GOT_STATUS_DELETE)
3700 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3703 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3709 ct_name = basename(pe->path);
3710 if (ct_name == NULL)
3711 return got_error_from_errno2("basename", pe->path);
3713 if (strcmp(te->name, ct_name) != 0)
3723 static const struct got_error *
3724 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3725 const char *child_path, const char *path_base_tree,
3726 struct got_pathlist_head *commitable_paths,
3727 got_worktree_status_cb status_cb, void *status_arg,
3728 struct got_repository *repo)
3730 const struct got_error *err = NULL;
3731 struct got_tree_entry *new_te;
3736 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3737 got_path_is_root_dir(path_base_tree) ? "" : "/",
3739 return got_error_from_errno("asprintf");
3741 new_te = calloc(1, sizeof(*new_te));
3742 new_te->mode = S_IFDIR;
3743 new_te->name = strdup(child_path);
3744 if (new_te->name == NULL) {
3745 err = got_error_from_errno("strdup");
3746 got_object_tree_entry_close(new_te);
3749 err = write_tree(&new_te->id, NULL, subtree_path,
3750 commitable_paths, status_cb, status_arg, repo);
3752 got_object_tree_entry_close(new_te);
3762 static const struct got_error *
3763 write_tree(struct got_object_id **new_tree_id,
3764 struct got_tree_object *base_tree, const char *path_base_tree,
3765 struct got_pathlist_head *commitable_paths,
3766 got_worktree_status_cb status_cb, void *status_arg,
3767 struct got_repository *repo)
3769 const struct got_error *err = NULL;
3770 const struct got_tree_entries *base_entries = NULL;
3771 struct got_pathlist_head paths;
3772 struct got_tree_entries new_tree_entries;
3773 struct got_tree_entry *te, *new_te = NULL;
3774 struct got_pathlist_entry *pe;
3777 new_tree_entries.nentries = 0;
3778 SIMPLEQ_INIT(&new_tree_entries.head);
3780 /* Insert, and recurse into, newly added entries first. */
3781 TAILQ_FOREACH(pe, commitable_paths, entry) {
3782 struct got_commitable *ct = pe->data;
3783 char *child_path = NULL, *slash;
3785 if ((ct->status != GOT_STATUS_ADD &&
3786 ct->staged_status != GOT_STATUS_ADD) ||
3787 (ct->flags & GOT_COMMITABLE_ADDED))
3790 if (!got_path_is_child(pe->path, path_base_tree,
3791 strlen(path_base_tree)))
3794 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3799 slash = strchr(child_path, '/');
3800 if (slash == NULL) {
3801 err = alloc_added_blob_tree_entry(&new_te, ct);
3804 err = report_ct_status(ct, status_cb, status_arg);
3807 ct->flags |= GOT_COMMITABLE_ADDED;
3808 err = insert_tree_entry(new_te, &paths);
3812 *slash = '\0'; /* trim trailing path components */
3813 if (base_tree == NULL ||
3814 got_object_tree_find_entry(base_tree, child_path)
3816 err = make_subtree_for_added_blob(&new_te,
3817 child_path, path_base_tree,
3818 commitable_paths, status_cb, status_arg,
3822 err = insert_tree_entry(new_te, &paths);
3830 /* Handle modified and deleted entries. */
3831 base_entries = got_object_tree_get_entries(base_tree);
3832 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3833 struct got_commitable *ct = NULL;
3835 if (S_ISDIR(te->mode)) {
3837 err = got_object_tree_entry_dup(&new_te, te);
3840 err = match_modified_subtree(&modified, te,
3841 path_base_tree, commitable_paths);
3844 /* Avoid recursion into unmodified subtrees. */
3847 err = write_subtree(&new_te->id, te,
3848 path_base_tree, commitable_paths,
3849 status_cb, status_arg, repo);
3853 err = insert_tree_entry(new_te, &paths);
3859 err = match_deleted_or_modified_ct(&ct, te,
3860 path_base_tree, commitable_paths);
3862 /* NB: Deleted entries get dropped here. */
3863 if (ct->status == GOT_STATUS_MODIFY ||
3864 ct->staged_status == GOT_STATUS_MODIFY) {
3865 err = alloc_modified_blob_tree_entry(
3869 err = insert_tree_entry(new_te, &paths);
3873 err = report_ct_status(ct, status_cb,
3878 /* Entry is unchanged; just copy it. */
3879 err = got_object_tree_entry_dup(&new_te, te);
3882 err = insert_tree_entry(new_te, &paths);
3889 /* Write new list of entries; deleted entries have been dropped. */
3890 TAILQ_FOREACH(pe, &paths, entry) {
3891 struct got_tree_entry *te = pe->data;
3892 new_tree_entries.nentries++;
3893 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3895 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3897 got_object_tree_entries_close(&new_tree_entries);
3898 got_pathlist_free(&paths);
3902 static const struct got_error *
3903 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3904 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3906 const struct got_error *err = NULL;
3907 struct got_pathlist_entry *pe;
3909 TAILQ_FOREACH(pe, commitable_paths, entry) {
3910 struct got_fileindex_entry *ie;
3911 struct got_commitable *ct = pe->data;
3913 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3915 if (ct->status == GOT_STATUS_DELETE ||
3916 ct->staged_status == GOT_STATUS_DELETE) {
3917 got_fileindex_entry_remove(fileindex, ie);
3918 got_fileindex_entry_free(ie);
3919 } else if (ct->staged_status == GOT_STATUS_ADD ||
3920 ct->staged_status == GOT_STATUS_MODIFY) {
3921 got_fileindex_entry_stage_set(ie,
3922 GOT_FILEIDX_STAGE_NONE);
3923 err = got_fileindex_entry_update(ie,
3924 ct->ondisk_path, ct->staged_blob_id->sha1,
3925 new_base_commit_id->sha1, 1);
3927 err = got_fileindex_entry_update(ie,
3928 ct->ondisk_path, ct->blob_id->sha1,
3929 new_base_commit_id->sha1, 1);
3931 err = got_fileindex_entry_alloc(&ie,
3932 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3933 new_base_commit_id->sha1);
3936 err = got_fileindex_entry_add(fileindex, ie);
3945 static const struct got_error *
3946 check_out_of_date(const char *in_repo_path, unsigned char status,
3947 unsigned char staged_status, struct got_object_id *base_blob_id,
3948 struct got_object_id *base_commit_id,
3949 struct got_object_id *head_commit_id, struct got_repository *repo,
3952 const struct got_error *err = NULL;
3953 struct got_object_id *id = NULL;
3955 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3956 /* Trivial case: base commit == head commit */
3957 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3960 * Ensure file content which local changes were based
3961 * on matches file content in the branch head.
3963 err = got_object_id_by_path(&id, repo, head_commit_id,
3966 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3967 err = got_error(ood_errcode);
3969 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3970 err = got_error(ood_errcode);
3972 /* Require that added files don't exist in the branch head. */
3973 err = got_object_id_by_path(&id, repo, head_commit_id,
3975 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3977 err = id ? got_error(ood_errcode) : NULL;
3984 const struct got_error *
3985 commit_worktree(struct got_object_id **new_commit_id,
3986 struct got_pathlist_head *commitable_paths,
3987 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3988 const char *author, const char *committer,
3989 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3990 got_worktree_status_cb status_cb, void *status_arg,
3991 struct got_repository *repo)
3993 const struct got_error *err = NULL, *unlockerr = NULL;
3994 struct got_pathlist_entry *pe;
3995 const char *head_ref_name = NULL;
3996 struct got_commit_object *head_commit = NULL;
3997 struct got_reference *head_ref2 = NULL;
3998 struct got_object_id *head_commit_id2 = NULL;
3999 struct got_tree_object *head_tree = NULL;
4000 struct got_object_id *new_tree_id = NULL;
4001 struct got_object_id_queue parent_ids;
4002 struct got_object_qid *pid = NULL;
4003 char *logmsg = NULL;
4005 *new_commit_id = NULL;
4007 SIMPLEQ_INIT(&parent_ids);
4009 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4013 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4017 if (commit_msg_cb != NULL) {
4018 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4023 if (logmsg == NULL || strlen(logmsg) == 0) {
4024 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4028 /* Create blobs from added and modified files and record their IDs. */
4029 TAILQ_FOREACH(pe, commitable_paths, entry) {
4030 struct got_commitable *ct = pe->data;
4033 /* Blobs for staged files already exist. */
4034 if (ct->staged_status == GOT_STATUS_ADD ||
4035 ct->staged_status == GOT_STATUS_MODIFY)
4038 if (ct->status != GOT_STATUS_ADD &&
4039 ct->status != GOT_STATUS_MODIFY)
4042 if (asprintf(&ondisk_path, "%s/%s",
4043 worktree->root_path, pe->path) == -1) {
4044 err = got_error_from_errno("asprintf");
4047 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4053 /* Recursively write new tree objects. */
4054 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4055 status_cb, status_arg, repo);
4059 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4062 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4063 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4064 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4065 got_object_qid_free(pid);
4071 /* Check if a concurrent commit to our branch has occurred. */
4072 head_ref_name = got_worktree_get_head_ref_name(worktree);
4073 if (head_ref_name == NULL) {
4074 err = got_error_from_errno("got_worktree_get_head_ref_name");
4077 /* Lock the reference here to prevent concurrent modification. */
4078 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4081 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4084 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4085 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4088 /* Update branch head in repository. */
4089 err = got_ref_change_ref(head_ref2, *new_commit_id);
4092 err = got_ref_write(head_ref2, repo);
4096 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4100 err = ref_base_commit(worktree, repo);
4105 got_object_tree_close(head_tree);
4107 got_object_commit_close(head_commit);
4108 free(head_commit_id2);
4110 unlockerr = got_ref_unlock(head_ref2);
4111 if (unlockerr && err == NULL)
4113 got_ref_close(head_ref2);
4118 static const struct got_error *
4119 check_path_is_commitable(const char *path,
4120 struct got_pathlist_head *commitable_paths)
4122 struct got_pathlist_entry *cpe = NULL;
4123 size_t path_len = strlen(path);
4125 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4126 struct got_commitable *ct = cpe->data;
4127 const char *ct_path = ct->path;
4129 while (ct_path[0] == '/')
4132 if (strcmp(path, ct_path) == 0 ||
4133 got_path_is_child(ct_path, path, path_len))
4138 return got_error_path(path, GOT_ERR_BAD_PATH);
4143 static const struct got_error *
4144 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4146 int *have_staged_files = arg;
4148 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4149 *have_staged_files = 1;
4150 return got_error(GOT_ERR_CANCELLED);
4156 static const struct got_error *
4157 check_non_staged_files(struct got_fileindex *fileindex,
4158 struct got_pathlist_head *paths)
4160 struct got_pathlist_entry *pe;
4161 struct got_fileindex_entry *ie;
4163 TAILQ_FOREACH(pe, paths, entry) {
4164 if (pe->path[0] == '\0')
4166 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4168 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4169 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4170 return got_error_path(pe->path,
4171 GOT_ERR_FILE_NOT_STAGED);
4177 const struct got_error *
4178 got_worktree_commit(struct got_object_id **new_commit_id,
4179 struct got_worktree *worktree, struct got_pathlist_head *paths,
4180 const char *author, const char *committer,
4181 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4182 got_worktree_status_cb status_cb, void *status_arg,
4183 struct got_repository *repo)
4185 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4186 struct got_fileindex *fileindex = NULL;
4187 char *fileindex_path = NULL;
4188 struct got_pathlist_head commitable_paths;
4189 struct collect_commitables_arg cc_arg;
4190 struct got_pathlist_entry *pe;
4191 struct got_reference *head_ref = NULL;
4192 struct got_object_id *head_commit_id = NULL;
4193 int have_staged_files = 0;
4195 *new_commit_id = NULL;
4197 TAILQ_INIT(&commitable_paths);
4199 err = lock_worktree(worktree, LOCK_EX);
4203 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4207 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4211 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4215 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4216 &have_staged_files);
4217 if (err && err->code != GOT_ERR_CANCELLED)
4219 if (have_staged_files) {
4220 err = check_non_staged_files(fileindex, paths);
4225 cc_arg.commitable_paths = &commitable_paths;
4226 cc_arg.worktree = worktree;
4228 cc_arg.have_staged_files = have_staged_files;
4229 TAILQ_FOREACH(pe, paths, entry) {
4230 err = worktree_status(worktree, pe->path, fileindex, repo,
4231 collect_commitables, &cc_arg, NULL, NULL);
4236 if (TAILQ_EMPTY(&commitable_paths)) {
4237 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4241 TAILQ_FOREACH(pe, paths, entry) {
4242 err = check_path_is_commitable(pe->path, &commitable_paths);
4247 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4248 struct got_commitable *ct = pe->data;
4249 const char *ct_path = ct->in_repo_path;
4251 while (ct_path[0] == '/')
4253 err = check_out_of_date(ct_path, ct->status,
4254 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4255 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4261 err = commit_worktree(new_commit_id, &commitable_paths,
4262 head_commit_id, worktree, author, committer,
4263 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4267 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4269 sync_err = sync_fileindex(fileindex, fileindex_path);
4270 if (sync_err && err == NULL)
4274 got_fileindex_free(fileindex);
4275 free(fileindex_path);
4276 unlockerr = lock_worktree(worktree, LOCK_SH);
4277 if (unlockerr && err == NULL)
4279 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4280 struct got_commitable *ct = pe->data;
4281 free_commitable(ct);
4283 got_pathlist_free(&commitable_paths);
4288 got_commitable_get_path(struct got_commitable *ct)
4294 got_commitable_get_status(struct got_commitable *ct)
4299 struct check_rebase_ok_arg {
4300 struct got_worktree *worktree;
4301 struct got_repository *repo;
4304 static const struct got_error *
4305 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4307 const struct got_error *err = NULL;
4308 struct check_rebase_ok_arg *a = arg;
4309 unsigned char status;
4313 /* Reject rebase of a work tree with mixed base commits. */
4314 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4315 SHA1_DIGEST_LENGTH))
4316 return got_error(GOT_ERR_MIXED_COMMITS);
4318 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4320 return got_error_from_errno("asprintf");
4322 /* Reject rebase of a work tree with modified or staged files. */
4323 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4328 if (status != GOT_STATUS_NO_CHANGE)
4329 return got_error(GOT_ERR_MODIFIED);
4330 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4331 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4336 const struct got_error *
4337 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4338 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4339 struct got_worktree *worktree, struct got_reference *branch,
4340 struct got_repository *repo)
4342 const struct got_error *err = NULL;
4343 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4344 char *branch_ref_name = NULL;
4345 char *fileindex_path = NULL;
4346 struct check_rebase_ok_arg ok_arg;
4347 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4349 *new_base_branch_ref = NULL;
4353 err = lock_worktree(worktree, LOCK_EX);
4357 err = open_fileindex(fileindex, &fileindex_path, worktree);
4361 ok_arg.worktree = worktree;
4363 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4368 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4372 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4376 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4380 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4385 err = got_ref_alloc_symref(new_base_branch_ref,
4386 new_base_branch_ref_name, wt_branch);
4389 err = got_ref_write(*new_base_branch_ref, repo);
4393 /* TODO Lock original branch's ref while rebasing? */
4395 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4399 err = got_ref_write(branch_ref, repo);
4403 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4404 worktree->base_commit_id);
4407 err = got_ref_write(*tmp_branch, repo);
4411 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4415 free(fileindex_path);
4416 free(tmp_branch_name);
4417 free(new_base_branch_ref_name);
4418 free(branch_ref_name);
4420 got_ref_close(branch_ref);
4422 got_ref_close(wt_branch);
4424 if (*new_base_branch_ref) {
4425 got_ref_close(*new_base_branch_ref);
4426 *new_base_branch_ref = NULL;
4429 got_ref_close(*tmp_branch);
4433 got_fileindex_free(*fileindex);
4436 lock_worktree(worktree, LOCK_SH);
4441 const struct got_error *
4442 got_worktree_rebase_continue(struct got_object_id **commit_id,
4443 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4444 struct got_reference **branch, struct got_fileindex **fileindex,
4445 struct got_worktree *worktree, struct got_repository *repo)
4447 const struct got_error *err;
4448 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4449 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4450 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4451 char *fileindex_path = NULL;
4452 int have_staged_files = 0;
4455 *new_base_branch = NULL;
4460 err = lock_worktree(worktree, LOCK_EX);
4464 err = open_fileindex(fileindex, &fileindex_path, worktree);
4468 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4469 &have_staged_files);
4470 if (err && err->code != GOT_ERR_CANCELLED)
4472 if (have_staged_files) {
4473 err = got_error(GOT_ERR_STAGED_PATHS);
4477 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4481 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4485 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4489 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4493 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4497 err = got_ref_open(branch, repo,
4498 got_ref_get_symref_target(branch_ref), 0);
4502 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4506 err = got_ref_resolve(commit_id, repo, commit_ref);
4510 err = got_ref_open(new_base_branch, repo,
4511 new_base_branch_ref_name, 0);
4515 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4519 free(commit_ref_name);
4520 free(branch_ref_name);
4521 free(fileindex_path);
4523 got_ref_close(commit_ref);
4525 got_ref_close(branch_ref);
4530 got_ref_close(*tmp_branch);
4533 if (*new_base_branch) {
4534 got_ref_close(*new_base_branch);
4535 *new_base_branch = NULL;
4538 got_ref_close(*branch);
4542 got_fileindex_free(*fileindex);
4545 lock_worktree(worktree, LOCK_SH);
4550 const struct got_error *
4551 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4553 const struct got_error *err;
4554 char *tmp_branch_name = NULL;
4556 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4560 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4561 free(tmp_branch_name);
4565 static const struct got_error *
4566 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4567 char **logmsg, void *arg)
4573 static const struct got_error *
4574 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4575 const char *path, struct got_object_id *blob_id,
4576 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4581 struct collect_merged_paths_arg {
4582 got_worktree_checkout_cb progress_cb;
4584 struct got_pathlist_head *merged_paths;
4587 static const struct got_error *
4588 collect_merged_paths(void *arg, unsigned char status, const char *path)
4590 const struct got_error *err;
4591 struct collect_merged_paths_arg *a = arg;
4593 struct got_pathlist_entry *new;
4595 err = (*a->progress_cb)(a->progress_arg, status, path);
4599 if (status != GOT_STATUS_MERGE &&
4600 status != GOT_STATUS_ADD &&
4601 status != GOT_STATUS_DELETE &&
4602 status != GOT_STATUS_CONFLICT)
4607 return got_error_from_errno("strdup");
4609 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4610 if (err || new == NULL)
4616 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4618 struct got_pathlist_entry *pe;
4620 TAILQ_FOREACH(pe, merged_paths, entry)
4621 free((char *)pe->path);
4623 got_pathlist_free(merged_paths);
4626 static const struct got_error *
4627 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4628 struct got_repository *repo)
4630 const struct got_error *err;
4631 struct got_reference *commit_ref = NULL;
4633 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4635 if (err->code != GOT_ERR_NOT_REF)
4637 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4640 err = got_ref_write(commit_ref, repo);
4644 struct got_object_id *stored_id;
4647 err = got_ref_resolve(&stored_id, repo, commit_ref);
4650 cmp = got_object_id_cmp(commit_id, stored_id);
4653 err = got_error(GOT_ERR_REBASE_COMMITID);
4659 got_ref_close(commit_ref);
4663 static const struct got_error *
4664 rebase_merge_files(struct got_pathlist_head *merged_paths,
4665 const char *commit_ref_name, struct got_worktree *worktree,
4666 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4667 struct got_object_id *commit_id, struct got_repository *repo,
4668 got_worktree_checkout_cb progress_cb, void *progress_arg,
4669 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4671 const struct got_error *err;
4672 struct got_reference *commit_ref = NULL;
4673 struct collect_merged_paths_arg cmp_arg;
4674 char *fileindex_path;
4676 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4678 err = get_fileindex_path(&fileindex_path, worktree);
4682 cmp_arg.progress_cb = progress_cb;
4683 cmp_arg.progress_arg = progress_arg;
4684 cmp_arg.merged_paths = merged_paths;
4685 err = merge_files(worktree, fileindex, fileindex_path,
4686 parent_commit_id, commit_id, repo, collect_merged_paths,
4687 &cmp_arg, cancel_cb, cancel_arg);
4689 got_ref_close(commit_ref);
4693 const struct got_error *
4694 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4695 struct got_worktree *worktree, struct got_fileindex *fileindex,
4696 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4697 struct got_repository *repo,
4698 got_worktree_checkout_cb progress_cb, void *progress_arg,
4699 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4701 const struct got_error *err;
4702 char *commit_ref_name;
4704 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4708 err = store_commit_id(commit_ref_name, commit_id, repo);
4712 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4713 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4714 progress_arg, cancel_cb, cancel_arg);
4716 free(commit_ref_name);
4720 const struct got_error *
4721 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4722 struct got_worktree *worktree, struct got_fileindex *fileindex,
4723 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4724 struct got_repository *repo,
4725 got_worktree_checkout_cb progress_cb, void *progress_arg,
4726 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4728 const struct got_error *err;
4729 char *commit_ref_name;
4731 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4735 err = store_commit_id(commit_ref_name, commit_id, repo);
4739 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4740 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4741 progress_arg, cancel_cb, cancel_arg);
4743 free(commit_ref_name);
4747 static const struct got_error *
4748 rebase_commit(struct got_object_id **new_commit_id,
4749 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4750 struct got_worktree *worktree, struct got_fileindex *fileindex,
4751 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4752 const char *new_logmsg, struct got_repository *repo)
4754 const struct got_error *err, *sync_err;
4755 struct got_pathlist_head commitable_paths;
4756 struct collect_commitables_arg cc_arg;
4757 char *fileindex_path = NULL;
4758 struct got_reference *head_ref = NULL;
4759 struct got_object_id *head_commit_id = NULL;
4760 char *logmsg = NULL;
4762 TAILQ_INIT(&commitable_paths);
4763 *new_commit_id = NULL;
4765 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4767 err = get_fileindex_path(&fileindex_path, worktree);
4771 cc_arg.commitable_paths = &commitable_paths;
4772 cc_arg.worktree = worktree;
4774 cc_arg.have_staged_files = 0;
4776 * If possible get the status of individual files directly to
4777 * avoid crawling the entire work tree once per rebased commit.
4778 * TODO: Ideally, merged_paths would contain a list of commitables
4779 * we could use so we could skip worktree_status() entirely.
4782 struct got_pathlist_entry *pe;
4783 if (TAILQ_EMPTY(merged_paths)) {
4784 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4787 TAILQ_FOREACH(pe, merged_paths, entry) {
4788 err = worktree_status(worktree, pe->path, fileindex,
4789 repo, collect_commitables, &cc_arg, NULL, NULL);
4794 err = worktree_status(worktree, "", fileindex, repo,
4795 collect_commitables, &cc_arg, NULL, NULL);
4800 if (TAILQ_EMPTY(&commitable_paths)) {
4801 /* No-op change; commit will be elided. */
4802 err = got_ref_delete(commit_ref, repo);
4805 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4809 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4813 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4818 logmsg = strdup(new_logmsg);
4819 if (logmsg == NULL) {
4820 err = got_error_from_errno("strdup");
4824 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4829 /* NB: commit_worktree will call free(logmsg) */
4830 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4831 worktree, got_object_commit_get_author(orig_commit),
4832 got_object_commit_get_committer(orig_commit),
4833 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4837 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4841 err = got_ref_delete(commit_ref, repo);
4845 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4847 sync_err = sync_fileindex(fileindex, fileindex_path);
4848 if (sync_err && err == NULL)
4851 free(fileindex_path);
4852 free(head_commit_id);
4854 got_ref_close(head_ref);
4856 free(*new_commit_id);
4857 *new_commit_id = NULL;
4862 const struct got_error *
4863 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4864 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4865 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4866 struct got_commit_object *orig_commit,
4867 struct got_object_id *orig_commit_id, struct got_repository *repo)
4869 const struct got_error *err;
4870 char *commit_ref_name;
4871 struct got_reference *commit_ref = NULL;
4872 struct got_object_id *commit_id = NULL;
4874 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4878 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4881 err = got_ref_resolve(&commit_id, repo, commit_ref);
4884 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4885 err = got_error(GOT_ERR_REBASE_COMMITID);
4889 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4890 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4893 got_ref_close(commit_ref);
4894 free(commit_ref_name);
4899 const struct got_error *
4900 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4901 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4902 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4903 struct got_commit_object *orig_commit,
4904 struct got_object_id *orig_commit_id, const char *new_logmsg,
4905 struct got_repository *repo)
4907 const struct got_error *err;
4908 char *commit_ref_name;
4909 struct got_reference *commit_ref = NULL;
4910 struct got_object_id *commit_id = NULL;
4912 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4916 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4919 err = got_ref_resolve(&commit_id, repo, commit_ref);
4922 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4923 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4927 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4928 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4931 got_ref_close(commit_ref);
4932 free(commit_ref_name);
4937 const struct got_error *
4938 got_worktree_rebase_postpone(struct got_worktree *worktree,
4939 struct got_fileindex *fileindex)
4942 got_fileindex_free(fileindex);
4943 return lock_worktree(worktree, LOCK_SH);
4946 static const struct got_error *
4947 delete_ref(const char *name, struct got_repository *repo)
4949 const struct got_error *err;
4950 struct got_reference *ref;
4952 err = got_ref_open(&ref, repo, name, 0);
4954 if (err->code == GOT_ERR_NOT_REF)
4959 err = got_ref_delete(ref, repo);
4964 static const struct got_error *
4965 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4967 const struct got_error *err;
4968 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4969 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4971 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4974 err = delete_ref(tmp_branch_name, repo);
4978 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4981 err = delete_ref(new_base_branch_ref_name, repo);
4985 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4988 err = delete_ref(branch_ref_name, repo);
4992 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4995 err = delete_ref(commit_ref_name, repo);
5000 free(tmp_branch_name);
5001 free(new_base_branch_ref_name);
5002 free(branch_ref_name);
5003 free(commit_ref_name);
5007 const struct got_error *
5008 got_worktree_rebase_complete(struct got_worktree *worktree,
5009 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5010 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5011 struct got_repository *repo)
5013 const struct got_error *err, *unlockerr;
5014 struct got_object_id *new_head_commit_id = NULL;
5016 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5020 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5024 err = got_ref_write(rebased_branch, repo);
5028 err = got_worktree_set_head_ref(worktree, rebased_branch);
5032 err = delete_rebase_refs(worktree, repo);
5035 got_fileindex_free(fileindex);
5036 free(new_head_commit_id);
5037 unlockerr = lock_worktree(worktree, LOCK_SH);
5038 if (unlockerr && err == NULL)
5043 const struct got_error *
5044 got_worktree_rebase_abort(struct got_worktree *worktree,
5045 struct got_fileindex *fileindex, struct got_repository *repo,
5046 struct got_reference *new_base_branch,
5047 got_worktree_checkout_cb progress_cb, void *progress_arg)
5049 const struct got_error *err, *unlockerr, *sync_err;
5050 struct got_reference *resolved = NULL;
5051 struct got_object_id *commit_id = NULL;
5052 char *fileindex_path = NULL;
5053 struct revert_file_args rfa;
5054 struct got_object_id *tree_id = NULL;
5056 err = lock_worktree(worktree, LOCK_EX);
5060 err = got_ref_open(&resolved, repo,
5061 got_ref_get_symref_target(new_base_branch), 0);
5065 err = got_worktree_set_head_ref(worktree, resolved);
5070 * XXX commits to the base branch could have happened while
5071 * we were busy rebasing; should we store the original commit ID
5072 * when rebase begins and read it back here?
5074 err = got_ref_resolve(&commit_id, repo, resolved);
5078 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5082 err = got_object_id_by_path(&tree_id, repo,
5083 worktree->base_commit_id, worktree->path_prefix);
5087 err = delete_rebase_refs(worktree, repo);
5091 err = get_fileindex_path(&fileindex_path, worktree);
5095 rfa.worktree = worktree;
5096 rfa.fileindex = fileindex;
5097 rfa.progress_cb = progress_cb;
5098 rfa.progress_arg = progress_arg;
5099 rfa.patch_cb = NULL;
5100 rfa.patch_arg = NULL;
5102 err = worktree_status(worktree, "", fileindex, repo,
5103 revert_file, &rfa, NULL, NULL);
5107 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5108 repo, progress_cb, progress_arg, NULL, NULL);
5110 sync_err = sync_fileindex(fileindex, fileindex_path);
5111 if (sync_err && err == NULL)
5114 got_ref_close(resolved);
5118 got_fileindex_free(fileindex);
5119 free(fileindex_path);
5121 unlockerr = lock_worktree(worktree, LOCK_SH);
5122 if (unlockerr && err == NULL)
5127 const struct got_error *
5128 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5129 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5130 struct got_fileindex **fileindex, struct got_worktree *worktree,
5131 struct got_repository *repo)
5133 const struct got_error *err = NULL;
5134 char *tmp_branch_name = NULL;
5135 char *branch_ref_name = NULL;
5136 char *base_commit_ref_name = NULL;
5137 char *fileindex_path = NULL;
5138 struct check_rebase_ok_arg ok_arg;
5139 struct got_reference *wt_branch = NULL;
5140 struct got_reference *base_commit_ref = NULL;
5144 *base_commit_id = NULL;
5147 err = lock_worktree(worktree, LOCK_EX);
5151 err = open_fileindex(fileindex, &fileindex_path, worktree);
5155 ok_arg.worktree = worktree;
5157 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5162 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5166 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5170 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5175 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5180 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5184 err = got_ref_write(*branch_ref, repo);
5188 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5189 worktree->base_commit_id);
5192 err = got_ref_write(base_commit_ref, repo);
5195 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5196 if (*base_commit_id == NULL) {
5197 err = got_error_from_errno("got_object_id_dup");
5201 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5202 worktree->base_commit_id);
5205 err = got_ref_write(*tmp_branch, repo);
5209 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5213 free(fileindex_path);
5214 free(tmp_branch_name);
5215 free(branch_ref_name);
5216 free(base_commit_ref_name);
5218 got_ref_close(wt_branch);
5221 got_ref_close(*branch_ref);
5225 got_ref_close(*tmp_branch);
5228 free(*base_commit_id);
5230 got_fileindex_free(*fileindex);
5233 lock_worktree(worktree, LOCK_SH);
5238 const struct got_error *
5239 got_worktree_histedit_postpone(struct got_worktree *worktree,
5240 struct got_fileindex *fileindex)
5243 got_fileindex_free(fileindex);
5244 return lock_worktree(worktree, LOCK_SH);
5247 const struct got_error *
5248 got_worktree_histedit_in_progress(int *in_progress,
5249 struct got_worktree *worktree)
5251 const struct got_error *err;
5252 char *tmp_branch_name = NULL;
5254 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5258 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5259 free(tmp_branch_name);
5263 const struct got_error *
5264 got_worktree_histedit_continue(struct got_object_id **commit_id,
5265 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5266 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5267 struct got_worktree *worktree, struct got_repository *repo)
5269 const struct got_error *err;
5270 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5271 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5272 struct got_reference *commit_ref = NULL;
5273 struct got_reference *base_commit_ref = NULL;
5274 char *fileindex_path = NULL;
5275 int have_staged_files = 0;
5279 *base_commit_id = NULL;
5282 err = lock_worktree(worktree, LOCK_EX);
5286 err = open_fileindex(fileindex, &fileindex_path, worktree);
5290 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5291 &have_staged_files);
5292 if (err && err->code != GOT_ERR_CANCELLED)
5294 if (have_staged_files) {
5295 err = got_error(GOT_ERR_STAGED_PATHS);
5299 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5303 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5307 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5311 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5316 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5320 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5323 err = got_ref_resolve(commit_id, repo, commit_ref);
5327 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5330 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5334 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5338 free(commit_ref_name);
5339 free(branch_ref_name);
5340 free(fileindex_path);
5342 got_ref_close(commit_ref);
5343 if (base_commit_ref)
5344 got_ref_close(base_commit_ref);
5348 free(*base_commit_id);
5349 *base_commit_id = NULL;
5351 got_ref_close(*tmp_branch);
5355 got_fileindex_free(*fileindex);
5358 lock_worktree(worktree, LOCK_EX);
5363 static const struct got_error *
5364 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5366 const struct got_error *err;
5367 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5368 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5370 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5373 err = delete_ref(tmp_branch_name, repo);
5377 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5381 err = delete_ref(base_commit_ref_name, repo);
5385 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5388 err = delete_ref(branch_ref_name, repo);
5392 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5395 err = delete_ref(commit_ref_name, repo);
5399 free(tmp_branch_name);
5400 free(base_commit_ref_name);
5401 free(branch_ref_name);
5402 free(commit_ref_name);
5406 const struct got_error *
5407 got_worktree_histedit_abort(struct got_worktree *worktree,
5408 struct got_fileindex *fileindex, struct got_repository *repo,
5409 struct got_reference *branch, struct got_object_id *base_commit_id,
5410 got_worktree_checkout_cb progress_cb, void *progress_arg)
5412 const struct got_error *err, *unlockerr, *sync_err;
5413 struct got_reference *resolved = NULL;
5414 char *fileindex_path = NULL;
5415 struct got_object_id *tree_id = NULL;
5416 struct revert_file_args rfa;
5418 err = lock_worktree(worktree, LOCK_EX);
5422 err = got_ref_open(&resolved, repo,
5423 got_ref_get_symref_target(branch), 0);
5427 err = got_worktree_set_head_ref(worktree, resolved);
5431 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5435 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5436 worktree->path_prefix);
5440 err = delete_histedit_refs(worktree, repo);
5444 err = get_fileindex_path(&fileindex_path, worktree);
5448 rfa.worktree = worktree;
5449 rfa.fileindex = fileindex;
5450 rfa.progress_cb = progress_cb;
5451 rfa.progress_arg = progress_arg;
5452 rfa.patch_cb = NULL;
5453 rfa.patch_arg = NULL;
5455 err = worktree_status(worktree, "", fileindex, repo,
5456 revert_file, &rfa, NULL, NULL);
5460 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5461 repo, progress_cb, progress_arg, NULL, NULL);
5463 sync_err = sync_fileindex(fileindex, fileindex_path);
5464 if (sync_err && err == NULL)
5467 got_ref_close(resolved);
5469 free(fileindex_path);
5471 unlockerr = lock_worktree(worktree, LOCK_SH);
5472 if (unlockerr && err == NULL)
5477 const struct got_error *
5478 got_worktree_histedit_complete(struct got_worktree *worktree,
5479 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5480 struct got_reference *edited_branch, struct got_repository *repo)
5482 const struct got_error *err, *unlockerr;
5483 struct got_object_id *new_head_commit_id = NULL;
5484 struct got_reference *resolved = NULL;
5486 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5490 err = got_ref_open(&resolved, repo,
5491 got_ref_get_symref_target(edited_branch), 0);
5495 err = got_ref_change_ref(resolved, new_head_commit_id);
5499 err = got_ref_write(resolved, repo);
5503 err = got_worktree_set_head_ref(worktree, resolved);
5507 err = delete_histedit_refs(worktree, repo);
5510 got_fileindex_free(fileindex);
5511 free(new_head_commit_id);
5512 unlockerr = lock_worktree(worktree, LOCK_SH);
5513 if (unlockerr && err == NULL)
5518 const struct got_error *
5519 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5520 struct got_object_id *commit_id, struct got_repository *repo)
5522 const struct got_error *err;
5523 char *commit_ref_name;
5525 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5529 err = store_commit_id(commit_ref_name, commit_id, repo);
5533 err = delete_ref(commit_ref_name, repo);
5535 free(commit_ref_name);
5539 struct check_stage_ok_arg {
5540 struct got_object_id *head_commit_id;
5541 struct got_worktree *worktree;
5542 struct got_fileindex *fileindex;
5543 struct got_repository *repo;
5547 const struct got_error *
5548 check_stage_ok(void *arg, unsigned char status,
5549 unsigned char staged_status, const char *relpath,
5550 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5551 struct got_object_id *commit_id)
5553 struct check_stage_ok_arg *a = arg;
5554 const struct got_error *err = NULL;
5555 struct got_fileindex_entry *ie;
5556 struct got_object_id base_commit_id;
5557 struct got_object_id *base_commit_idp = NULL;
5558 char *in_repo_path = NULL, *p;
5560 if (status == GOT_STATUS_UNVERSIONED)
5563 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5565 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5567 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5568 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5570 return got_error_from_errno("asprintf");
5572 if (got_fileindex_entry_has_commit(ie)) {
5573 memcpy(base_commit_id.sha1, ie->commit_sha1,
5574 SHA1_DIGEST_LENGTH);
5575 base_commit_idp = &base_commit_id;
5578 if (status == GOT_STATUS_NO_CHANGE) {
5579 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5581 } else if (status == GOT_STATUS_CONFLICT) {
5582 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5584 } else if (status != GOT_STATUS_ADD &&
5585 status != GOT_STATUS_MODIFY &&
5586 status != GOT_STATUS_DELETE) {
5587 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5591 a->have_changes = 1;
5596 err = check_out_of_date(p, status, staged_status,
5597 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5598 GOT_ERR_STAGE_OUT_OF_DATE);
5604 struct stage_path_arg {
5605 struct got_worktree *worktree;
5606 struct got_fileindex *fileindex;
5607 struct got_repository *repo;
5608 got_worktree_status_cb status_cb;
5610 got_worktree_patch_cb patch_cb;
5614 static const struct got_error *
5615 stage_path(void *arg, unsigned char status,
5616 unsigned char staged_status, const char *relpath,
5617 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5618 struct got_object_id *commit_id)
5620 struct stage_path_arg *a = arg;
5621 const struct got_error *err = NULL;
5622 struct got_fileindex_entry *ie;
5623 char *ondisk_path = NULL, *path_content = NULL;
5625 struct got_object_id *new_staged_blob_id = NULL;
5627 if (status == GOT_STATUS_UNVERSIONED)
5630 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5632 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5634 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5636 return got_error_from_errno("asprintf");
5639 case GOT_STATUS_ADD:
5640 case GOT_STATUS_MODIFY:
5642 if (status == GOT_STATUS_ADD) {
5643 int choice = GOT_PATCH_CHOICE_NONE;
5644 err = (*a->patch_cb)(&choice, a->patch_arg,
5645 status, ie->path, NULL, 1, 1);
5648 if (choice != GOT_PATCH_CHOICE_YES)
5651 err = create_patched_content(&path_content, 0,
5652 staged_blob_id ? staged_blob_id : blob_id,
5653 ondisk_path, ie->path, a->repo,
5654 a->patch_cb, a->patch_arg);
5655 if (err || path_content == NULL)
5659 err = got_object_blob_create(&new_staged_blob_id,
5660 path_content ? path_content : ondisk_path, a->repo);
5663 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5664 SHA1_DIGEST_LENGTH);
5665 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5666 stage = GOT_FILEIDX_STAGE_ADD;
5668 stage = GOT_FILEIDX_STAGE_MODIFY;
5669 got_fileindex_entry_stage_set(ie, stage);
5670 if (a->status_cb == NULL)
5672 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5673 get_staged_status(ie), relpath, blob_id,
5674 new_staged_blob_id, NULL);
5676 case GOT_STATUS_DELETE:
5677 if (staged_status == GOT_STATUS_DELETE)
5680 int choice = GOT_PATCH_CHOICE_NONE;
5681 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5682 ie->path, NULL, 1, 1);
5685 if (choice == GOT_PATCH_CHOICE_NO)
5687 if (choice != GOT_PATCH_CHOICE_YES) {
5688 err = got_error(GOT_ERR_PATCH_CHOICE);
5692 stage = GOT_FILEIDX_STAGE_DELETE;
5693 got_fileindex_entry_stage_set(ie, stage);
5694 if (a->status_cb == NULL)
5696 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5697 get_staged_status(ie), relpath, NULL, NULL, NULL);
5699 case GOT_STATUS_NO_CHANGE:
5700 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5702 case GOT_STATUS_CONFLICT:
5703 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5706 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5710 if (path_content && unlink(path_content) == -1 && err == NULL)
5711 err = got_error_from_errno2("unlink", path_content);
5714 free(new_staged_blob_id);
5718 const struct got_error *
5719 got_worktree_stage(struct got_worktree *worktree,
5720 struct got_pathlist_head *paths,
5721 got_worktree_status_cb status_cb, void *status_arg,
5722 got_worktree_patch_cb patch_cb, void *patch_arg,
5723 struct got_repository *repo)
5725 const struct got_error *err = NULL, *sync_err, *unlockerr;
5726 struct got_pathlist_entry *pe;
5727 struct got_fileindex *fileindex = NULL;
5728 char *fileindex_path = NULL;
5729 struct got_reference *head_ref = NULL;
5730 struct got_object_id *head_commit_id = NULL;
5731 struct check_stage_ok_arg oka;
5732 struct stage_path_arg spa;
5734 err = lock_worktree(worktree, LOCK_EX);
5738 err = got_ref_open(&head_ref, repo,
5739 got_worktree_get_head_ref_name(worktree), 0);
5742 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5745 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5749 /* Check pre-conditions before staging anything. */
5750 oka.head_commit_id = head_commit_id;
5751 oka.worktree = worktree;
5752 oka.fileindex = fileindex;
5754 oka.have_changes = 0;
5755 TAILQ_FOREACH(pe, paths, entry) {
5756 err = worktree_status(worktree, pe->path, fileindex, repo,
5757 check_stage_ok, &oka, NULL, NULL);
5761 if (!oka.have_changes) {
5762 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5766 spa.worktree = worktree;
5767 spa.fileindex = fileindex;
5769 spa.patch_cb = patch_cb;
5770 spa.patch_arg = patch_arg;
5771 spa.status_cb = status_cb;
5772 spa.status_arg = status_arg;
5773 TAILQ_FOREACH(pe, paths, entry) {
5774 err = worktree_status(worktree, pe->path, fileindex, repo,
5775 stage_path, &spa, NULL, NULL);
5780 sync_err = sync_fileindex(fileindex, fileindex_path);
5781 if (sync_err && err == NULL)
5785 got_ref_close(head_ref);
5786 free(head_commit_id);
5787 free(fileindex_path);
5789 got_fileindex_free(fileindex);
5790 unlockerr = lock_worktree(worktree, LOCK_SH);
5791 if (unlockerr && err == NULL)
5796 struct unstage_path_arg {
5797 struct got_worktree *worktree;
5798 struct got_fileindex *fileindex;
5799 struct got_repository *repo;
5800 got_worktree_checkout_cb progress_cb;
5802 got_worktree_patch_cb patch_cb;
5806 static const struct got_error *
5807 create_unstaged_content(char **path_unstaged_content,
5808 char **path_new_staged_content, struct got_object_id *blob_id,
5809 struct got_object_id *staged_blob_id, const char *relpath,
5810 struct got_repository *repo,
5811 got_worktree_patch_cb patch_cb, void *patch_arg)
5813 const struct got_error *err;
5814 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5815 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5816 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5817 struct stat sb1, sb2;
5818 struct got_diff_changes *changes = NULL;
5819 struct got_diff_state *ds = NULL;
5820 struct got_diff_args *args = NULL;
5821 struct got_diff_change *change;
5822 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5823 int have_content = 0, have_rejected_content = 0;
5825 *path_unstaged_content = NULL;
5826 *path_new_staged_content = NULL;
5828 err = got_object_id_str(&label1, blob_id);
5831 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5835 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5839 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5843 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5847 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5851 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5855 if (stat(path1, &sb1) == -1) {
5856 err = got_error_from_errno2("stat", path1);
5860 if (stat(path2, &sb2) == -1) {
5861 err = got_error_from_errno2("stat", path2);
5865 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5866 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5870 err = got_opentemp_named(path_unstaged_content, &outfile,
5871 "got-unstaged-content");
5874 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5875 "got-new-staged-content");
5879 if (fseek(f1, 0L, SEEK_SET) == -1) {
5880 err = got_ferror(f1, GOT_ERR_IO);
5883 if (fseek(f2, 0L, SEEK_SET) == -1) {
5884 err = got_ferror(f2, GOT_ERR_IO);
5887 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5889 err = apply_or_reject_change(&choice, change, ++n,
5890 changes->nchanges, ds, args, diff_flags, relpath,
5891 f1, f2, &line_cur1, &line_cur2,
5892 outfile, rejectfile, patch_cb, patch_arg);
5895 if (choice == GOT_PATCH_CHOICE_YES)
5898 have_rejected_content = 1;
5899 if (choice == GOT_PATCH_CHOICE_QUIT)
5902 if (have_content || have_rejected_content)
5903 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5904 outfile, rejectfile);
5908 got_object_blob_close(blob);
5910 got_object_blob_close(staged_blob);
5911 if (f1 && fclose(f1) == EOF && err == NULL)
5912 err = got_error_from_errno2("fclose", path1);
5913 if (f2 && fclose(f2) == EOF && err == NULL)
5914 err = got_error_from_errno2("fclose", path2);
5915 if (outfile && fclose(outfile) == EOF && err == NULL)
5916 err = got_error_from_errno2("fclose", *path_unstaged_content);
5917 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5918 err = got_error_from_errno2("fclose", *path_new_staged_content);
5919 if (path1 && unlink(path1) == -1 && err == NULL)
5920 err = got_error_from_errno2("unlink", path1);
5921 if (path2 && unlink(path2) == -1 && err == NULL)
5922 err = got_error_from_errno2("unlink", path2);
5923 if (err || !have_content) {
5924 if (*path_unstaged_content &&
5925 unlink(*path_unstaged_content) == -1 && err == NULL)
5926 err = got_error_from_errno2("unlink",
5927 *path_unstaged_content);
5928 free(*path_unstaged_content);
5929 *path_unstaged_content = NULL;
5931 if (err || !have_rejected_content) {
5932 if (*path_new_staged_content &&
5933 unlink(*path_new_staged_content) == -1 && err == NULL)
5934 err = got_error_from_errno2("unlink",
5935 *path_new_staged_content);
5936 free(*path_new_staged_content);
5937 *path_new_staged_content = NULL;
5941 got_diff_state_free(ds);
5945 got_diff_free_changes(changes);
5951 static const struct got_error *
5952 unstage_path(void *arg, unsigned char status,
5953 unsigned char staged_status, const char *relpath,
5954 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5955 struct got_object_id *commit_id)
5957 const struct got_error *err = NULL;
5958 struct unstage_path_arg *a = arg;
5959 struct got_fileindex_entry *ie;
5960 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5961 char *ondisk_path = NULL, *path_unstaged_content = NULL;
5962 char *path_new_staged_content = NULL;
5963 int local_changes_subsumed;
5966 if (staged_status != GOT_STATUS_ADD &&
5967 staged_status != GOT_STATUS_MODIFY &&
5968 staged_status != GOT_STATUS_DELETE)
5971 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5973 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5975 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
5977 return got_error_from_errno("asprintf");
5979 switch (staged_status) {
5980 case GOT_STATUS_MODIFY:
5981 err = got_object_open_as_blob(&blob_base, a->repo,
5986 case GOT_STATUS_ADD:
5988 if (staged_status == GOT_STATUS_ADD) {
5989 int choice = GOT_PATCH_CHOICE_NONE;
5990 err = (*a->patch_cb)(&choice, a->patch_arg,
5991 staged_status, ie->path, NULL, 1, 1);
5994 if (choice != GOT_PATCH_CHOICE_YES)
5997 err = create_unstaged_content(
5998 &path_unstaged_content,
5999 &path_new_staged_content, blob_id,
6000 staged_blob_id, ie->path, a->repo,
6001 a->patch_cb, a->patch_arg);
6002 if (err || path_unstaged_content == NULL)
6004 if (path_new_staged_content) {
6005 err = got_object_blob_create(
6007 path_new_staged_content,
6011 memcpy(ie->staged_blob_sha1,
6012 staged_blob_id->sha1,
6013 SHA1_DIGEST_LENGTH);
6015 err = merge_file(&local_changes_subsumed,
6016 a->worktree, blob_base, ondisk_path,
6017 relpath, got_fileindex_perms_to_st(ie),
6018 path_unstaged_content, "unstaged",
6019 a->repo, a->progress_cb, a->progress_arg);
6021 path_new_staged_content == NULL)
6022 got_fileindex_entry_stage_set(ie,
6023 GOT_FILEIDX_STAGE_NONE);
6024 break; /* Done with this file. */
6027 err = got_object_open_as_blob(&blob_staged, a->repo,
6028 staged_blob_id, 8192);
6031 err = merge_blob(&local_changes_subsumed, a->worktree,
6032 blob_base, ondisk_path, relpath,
6033 got_fileindex_perms_to_st(ie), blob_staged,
6034 commit_id ? commit_id : a->worktree->base_commit_id,
6035 a->repo, a->progress_cb, a->progress_arg);
6037 got_fileindex_entry_stage_set(ie,
6038 GOT_FILEIDX_STAGE_NONE);
6040 case GOT_STATUS_DELETE:
6042 int choice = GOT_PATCH_CHOICE_NONE;
6043 err = (*a->patch_cb)(&choice, a->patch_arg,
6044 staged_status, ie->path, NULL, 1, 1);
6047 if (choice == GOT_PATCH_CHOICE_NO)
6049 if (choice != GOT_PATCH_CHOICE_YES) {
6050 err = got_error(GOT_ERR_PATCH_CHOICE);
6054 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6055 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6058 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6063 if (path_unstaged_content &&
6064 unlink(path_unstaged_content) == -1 && err == NULL)
6065 err = got_error_from_errno2("unlink", path_unstaged_content);
6066 if (path_new_staged_content &&
6067 unlink(path_new_staged_content) == -1 && err == NULL)
6068 err = got_error_from_errno2("unlink", path_new_staged_content);
6069 free(path_unstaged_content);
6070 free(path_new_staged_content);
6072 got_object_blob_close(blob_base);
6074 got_object_blob_close(blob_staged);
6078 const struct got_error *
6079 got_worktree_unstage(struct got_worktree *worktree,
6080 struct got_pathlist_head *paths,
6081 got_worktree_checkout_cb progress_cb, void *progress_arg,
6082 got_worktree_patch_cb patch_cb, void *patch_arg,
6083 struct got_repository *repo)
6085 const struct got_error *err = NULL, *sync_err, *unlockerr;
6086 struct got_pathlist_entry *pe;
6087 struct got_fileindex *fileindex = NULL;
6088 char *fileindex_path = NULL;
6089 struct unstage_path_arg upa;
6091 err = lock_worktree(worktree, LOCK_EX);
6095 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6099 upa.worktree = worktree;
6100 upa.fileindex = fileindex;
6102 upa.progress_cb = progress_cb;
6103 upa.progress_arg = progress_arg;
6104 upa.patch_cb = patch_cb;
6105 upa.patch_arg = patch_arg;
6106 TAILQ_FOREACH(pe, paths, entry) {
6107 err = worktree_status(worktree, pe->path, fileindex, repo,
6108 unstage_path, &upa, NULL, NULL);
6113 sync_err = sync_fileindex(fileindex, fileindex_path);
6114 if (sync_err && err == NULL)
6117 free(fileindex_path);
6119 got_fileindex_free(fileindex);
6120 unlockerr = lock_worktree(worktree, LOCK_SH);
6121 if (unlockerr && err == NULL)