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_cancel.h"
43 #include "got_worktree.h"
44 #include "got_opentemp.h"
47 #include "got_lib_worktree.h"
48 #include "got_lib_sha1.h"
49 #include "got_lib_fileindex.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_object_idset.h"
56 #include "got_lib_diff.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 static const struct got_error *
63 create_meta_file(const char *path_got, const char *name, const char *content)
65 const struct got_error *err = NULL;
68 if (asprintf(&path, "%s/%s", path_got, name) == -1)
69 return got_error_from_errno("asprintf");
71 err = got_path_create_file(path, content);
76 static const struct got_error *
77 update_meta_file(const char *path_got, const char *name, const char *content)
79 const struct got_error *err = NULL;
84 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
85 err = got_error_from_errno("asprintf");
90 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 int len = fprintf(tmpfile, "%s\n", content);
96 if (len != strlen(content) + 1) {
97 err = got_error_from_errno2("fprintf", tmppath);
102 if (rename(tmppath, path) != 0) {
103 err = got_error_from_errno3("rename", tmppath, path);
109 if (fclose(tmpfile) != 0 && err == NULL)
110 err = got_error_from_errno2("fclose", tmppath);
115 static const struct got_error *
116 read_meta_file(char **content, const char *path_got, const char *name)
118 const struct got_error *err = NULL;
126 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
127 err = got_error_from_errno("asprintf");
132 fd = open(path, O_RDONLY | O_NOFOLLOW);
135 err = got_error_path(path, GOT_ERR_WORKTREE_META);
137 err = got_error_from_errno2("open", path);
140 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
141 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
142 : got_error_from_errno2("flock", path));
146 if (fstat(fd, &sb) != 0) {
147 err = got_error_from_errno2("fstat", path);
150 *content = calloc(1, sb.st_size);
151 if (*content == NULL) {
152 err = got_error_from_errno("calloc");
156 n = read(fd, *content, sb.st_size);
157 if (n != sb.st_size) {
158 err = (n == -1 ? got_error_from_errno2("read", path) :
159 got_error_path(path, GOT_ERR_WORKTREE_META));
162 if ((*content)[sb.st_size - 1] != '\n') {
163 err = got_error_path(path, GOT_ERR_WORKTREE_META);
166 (*content)[sb.st_size - 1] = '\0';
169 if (fd != -1 && close(fd) == -1 && err == NULL)
170 err = got_error_from_errno2("close", path_got);
179 static const struct got_error *
180 write_head_ref(const char *path_got, struct got_reference *head_ref)
182 const struct got_error *err = NULL;
185 if (got_ref_is_symbolic(head_ref)) {
186 refstr = got_ref_to_str(head_ref);
188 return got_error_from_errno("got_ref_to_str");
190 refstr = strdup(got_ref_get_name(head_ref));
192 return got_error_from_errno("strdup");
194 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 const struct got_error *
200 got_worktree_init(const char *path, struct got_reference *head_ref,
201 const char *prefix, struct got_repository *repo)
203 const struct got_error *err = NULL;
204 struct got_object_id *commit_id = NULL;
206 uint32_t uuid_status;
208 char *path_got = NULL;
209 char *formatstr = NULL;
210 char *absprefix = NULL;
211 char *basestr = NULL;
212 char *uuidstr = NULL;
214 if (strcmp(path, got_repo_get_path(repo)) == 0) {
215 err = got_error(GOT_ERR_WORKTREE_REPO);
219 err = got_ref_resolve(&commit_id, repo, head_ref);
222 err = got_object_get_type(&obj_type, repo, commit_id);
225 if (obj_type != GOT_OBJ_TYPE_COMMIT)
226 return got_error(GOT_ERR_OBJ_TYPE);
228 if (!got_path_is_absolute(prefix)) {
229 if (asprintf(&absprefix, "/%s", prefix) == -1)
230 return got_error_from_errno("asprintf");
233 /* Create top-level directory (may already exist). */
234 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
235 err = got_error_from_errno2("mkdir", path);
239 /* Create .got directory (may already exist). */
240 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
241 err = got_error_from_errno("asprintf");
244 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
245 err = got_error_from_errno2("mkdir", path_got);
249 /* Create an empty lock file. */
250 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
254 /* Create an empty file index. */
255 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
259 /* Write the HEAD reference. */
260 err = write_head_ref(path_got, head_ref);
264 /* Record our base commit. */
265 err = got_object_id_str(&basestr, commit_id);
268 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
272 /* Store path to repository. */
273 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
274 got_repo_get_path(repo));
278 /* Store in-repository path prefix. */
279 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
280 absprefix ? absprefix : prefix);
285 uuid_create(&uuid, &uuid_status);
286 if (uuid_status != uuid_s_ok) {
287 err = got_error_uuid(uuid_status);
290 uuid_to_string(&uuid, &uuidstr, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status);
295 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
299 /* Stamp work tree with format file. */
300 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
301 err = got_error_from_errno("asprintf");
304 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
318 static const struct got_error *
319 open_worktree(struct got_worktree **worktree, const char *path)
321 const struct got_error *err = NULL;
323 char *formatstr = NULL;
324 char *uuidstr = NULL;
325 char *path_lock = NULL;
326 char *base_commit_id_str = NULL;
327 int version, fd = -1;
329 struct got_repository *repo = NULL;
330 uint32_t uuid_status;
334 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
335 err = got_error_from_errno("asprintf");
340 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
341 err = got_error_from_errno("asprintf");
346 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
348 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
349 : got_error_from_errno2("open", path_lock));
353 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
357 version = strtonum(formatstr, 1, INT_MAX, &errstr);
359 err = got_error_msg(GOT_ERR_WORKTREE_META,
360 "could not parse work tree format version number");
363 if (version != GOT_WORKTREE_FORMAT_VERSION) {
364 err = got_error(GOT_ERR_WORKTREE_VERS);
368 *worktree = calloc(1, sizeof(**worktree));
369 if (*worktree == NULL) {
370 err = got_error_from_errno("calloc");
373 (*worktree)->lockfd = -1;
375 (*worktree)->root_path = strdup(path);
376 if ((*worktree)->root_path == NULL) {
377 err = got_error_from_errno("strdup");
380 err = read_meta_file(&(*worktree)->repo_path, path_got,
381 GOT_WORKTREE_REPOSITORY);
385 err = read_meta_file(&(*worktree)->path_prefix, path_got,
386 GOT_WORKTREE_PATH_PREFIX);
390 err = read_meta_file(&base_commit_id_str, path_got,
391 GOT_WORKTREE_BASE_COMMIT);
395 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
398 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
399 if (uuid_status != uuid_s_ok) {
400 err = got_error_uuid(uuid_status);
404 err = got_repo_open(&repo, (*worktree)->repo_path);
408 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
414 GOT_WORKTREE_HEAD_REF);
417 got_repo_close(repo);
420 free(base_commit_id_str);
426 if (*worktree != NULL)
427 got_worktree_close(*worktree);
430 (*worktree)->lockfd = fd;
435 const struct got_error *
436 got_worktree_open(struct got_worktree **worktree, const char *path)
438 const struct got_error *err = NULL;
441 err = open_worktree(worktree, path);
442 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
446 path = dirname(path);
448 return got_error_from_errno2("dirname", path);
449 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
451 return got_error(GOT_ERR_NOT_WORKTREE);
454 const struct got_error *
455 got_worktree_close(struct got_worktree *worktree)
457 const struct got_error *err = NULL;
458 free(worktree->root_path);
459 free(worktree->repo_path);
460 free(worktree->path_prefix);
461 free(worktree->base_commit_id);
462 free(worktree->head_ref_name);
463 if (worktree->lockfd != -1)
464 if (close(worktree->lockfd) != 0)
465 err = got_error_from_errno2("close",
466 got_worktree_get_root_path(worktree));
472 got_worktree_get_root_path(struct got_worktree *worktree)
474 return worktree->root_path;
478 got_worktree_get_repo_path(struct got_worktree *worktree)
480 return worktree->repo_path;
484 got_worktree_get_path_prefix(struct got_worktree *worktree)
486 return worktree->path_prefix;
489 const struct got_error *
490 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
491 const char *path_prefix)
493 char *absprefix = NULL;
495 if (!got_path_is_absolute(path_prefix)) {
496 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
497 return got_error_from_errno("asprintf");
499 *match = (strcmp(absprefix ? absprefix : path_prefix,
500 worktree->path_prefix) == 0);
506 got_worktree_get_head_ref_name(struct got_worktree *worktree)
508 return worktree->head_ref_name;
511 const struct got_error *
512 got_worktree_set_head_ref(struct got_worktree *worktree,
513 struct got_reference *head_ref)
515 const struct got_error *err = NULL;
516 char *path_got = NULL, *head_ref_name = NULL;
518 if (asprintf(&path_got, "%s/%s", worktree->root_path,
519 GOT_WORKTREE_GOT_DIR) == -1) {
520 err = got_error_from_errno("asprintf");
525 head_ref_name = strdup(got_ref_get_name(head_ref));
526 if (head_ref_name == NULL) {
527 err = got_error_from_errno("strdup");
531 err = write_head_ref(path_got, head_ref);
535 free(worktree->head_ref_name);
536 worktree->head_ref_name = head_ref_name;
544 struct got_object_id *
545 got_worktree_get_base_commit_id(struct got_worktree *worktree)
547 return worktree->base_commit_id;
550 const struct got_error *
551 got_worktree_set_base_commit_id(struct got_worktree *worktree,
552 struct got_repository *repo, struct got_object_id *commit_id)
554 const struct got_error *err;
555 struct got_object *obj = NULL;
557 char *path_got = NULL;
559 if (asprintf(&path_got, "%s/%s", worktree->root_path,
560 GOT_WORKTREE_GOT_DIR) == -1) {
561 err = got_error_from_errno("asprintf");
566 err = got_object_open(&obj, repo, commit_id);
570 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
571 err = got_error(GOT_ERR_OBJ_TYPE);
575 /* Record our base commit. */
576 err = got_object_id_str(&id_str, commit_id);
579 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
583 free(worktree->base_commit_id);
584 worktree->base_commit_id = got_object_id_dup(commit_id);
585 if (worktree->base_commit_id == NULL) {
586 err = got_error_from_errno("got_object_id_dup");
591 got_object_close(obj);
597 static const struct got_error *
598 lock_worktree(struct got_worktree *worktree, int operation)
600 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
601 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
602 : got_error_from_errno2("flock",
603 got_worktree_get_root_path(worktree)));
607 static const struct got_error *
608 add_dir_on_disk(struct got_worktree *worktree, const char *path)
610 const struct got_error *err = NULL;
613 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
614 return got_error_from_errno("asprintf");
616 err = got_path_mkdir(abspath);
617 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
620 if (lstat(abspath, &sb) == -1) {
621 err = got_error_from_errno2("lstat", abspath);
622 } else if (!S_ISDIR(sb.st_mode)) {
623 /* TODO directory is obstructed; do something */
624 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
631 static const struct got_error *
632 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
634 const struct got_error *err = NULL;
637 size_t flen1 = 0, flen2 = 0;
642 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
643 if (flen1 == 0 && ferror(f1)) {
644 err = got_error_from_errno("fread");
647 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
648 if (flen2 == 0 && ferror(f2)) {
649 err = got_error_from_errno("fread");
656 } else if (flen2 == 0) {
660 } else if (flen1 == flen2) {
661 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
674 static const struct got_error *
675 check_files_equal(int *same, const char *f1_path, const char *f2_path)
677 const struct got_error *err = NULL;
680 FILE *f1 = NULL, *f2 = NULL;
684 if (lstat(f1_path, &sb) != 0) {
685 err = got_error_from_errno2("lstat", f1_path);
690 if (lstat(f2_path, &sb) != 0) {
691 err = got_error_from_errno2("lstat", f2_path);
696 if (size1 != size2) {
701 f1 = fopen(f1_path, "r");
703 return got_error_from_errno2("open", f1_path);
705 f2 = fopen(f2_path, "r");
707 err = got_error_from_errno2("open", f2_path);
711 err = check_file_contents_equal(same, f1, f2);
713 if (f1 && fclose(f1) != 0 && err == NULL)
714 err = got_error_from_errno("fclose");
715 if (f2 && fclose(f2) != 0 && err == NULL)
716 err = got_error_from_errno("fclose");
722 * Perform a 3-way merge where blob_orig acts as the common ancestor,
723 * the file at deriv_path acts as the first derived version, and the
724 * file on disk acts as the second derived version.
726 static const struct got_error *
727 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
728 struct got_blob_object *blob_orig, const char *ondisk_path,
729 const char *path, uint16_t st_mode, const char *deriv_path,
730 const char *label_deriv, struct got_repository *repo,
731 got_worktree_checkout_cb progress_cb, void *progress_arg)
733 const struct got_error *err = NULL;
736 char *blob_orig_path = NULL;
737 char *merged_path = NULL, *base_path = NULL;
741 *local_changes_subsumed = 0;
743 parent = dirname(ondisk_path);
745 return got_error_from_errno2("dirname", ondisk_path);
747 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
748 return got_error_from_errno("asprintf");
750 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
755 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
756 err = got_error_from_errno("asprintf");
761 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
765 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 * If the file has no blob, this is an "add vs add" conflict,
772 * and we simply use an empty ancestor file to make both files
773 * appear in the merged result in their entirety.
777 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
778 blob_orig_path, ondisk_path, label_deriv, path);
782 err = (*progress_cb)(progress_arg,
783 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
787 if (fsync(merged_fd) != 0) {
788 err = got_error_from_errno("fsync");
792 /* Check if a clean merge has subsumed all local changes. */
793 if (overlapcnt == 0) {
794 err = check_files_equal(local_changes_subsumed, deriv_path,
800 if (chmod(merged_path, st_mode) != 0) {
801 err = got_error_from_errno2("chmod", merged_path);
805 if (rename(merged_path, ondisk_path) != 0) {
806 err = got_error_from_errno3("rename", merged_path,
813 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
814 err = got_error_from_errno("close");
815 if (f_orig && fclose(f_orig) != 0 && err == NULL)
816 err = got_error_from_errno("fclose");
819 if (blob_orig_path) {
820 unlink(blob_orig_path);
821 free(blob_orig_path);
827 * Perform a 3-way merge where blob_orig acts as the common ancestor,
828 * blob_deriv acts as the first derived version, and the file on disk
829 * acts as the second derived version.
831 static const struct got_error *
832 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
833 struct got_blob_object *blob_orig, const char *ondisk_path,
834 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
835 struct got_object_id *deriv_base_commit_id,
836 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
839 const struct got_error *err = NULL;
840 FILE *f_deriv = NULL;
841 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
842 char *label_deriv = NULL, *parent;
844 *local_changes_subsumed = 0;
846 parent = dirname(ondisk_path);
848 return got_error_from_errno2("dirname", ondisk_path);
851 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
852 err = got_error_from_errno("asprintf");
857 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
860 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
865 err = got_object_id_str(&id_str, deriv_base_commit_id);
868 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
869 err = got_error_from_errno("asprintf");
873 err = merge_file(local_changes_subsumed, worktree, blob_orig,
874 ondisk_path, path, st_mode, blob_deriv_path, label_deriv,
875 repo, progress_cb, progress_arg);
877 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
878 err = got_error_from_errno("fclose");
880 if (blob_deriv_path) {
881 unlink(blob_deriv_path);
882 free(blob_deriv_path);
889 static const struct got_error *
890 update_blob_fileindex_entry(struct got_worktree *worktree,
891 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
892 const char *ondisk_path, const char *path, struct got_blob_object *blob,
893 int update_timestamps)
895 const struct got_error *err = NULL;
898 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
900 err = got_fileindex_entry_update(ie, ondisk_path,
901 blob->id.sha1, worktree->base_commit_id->sha1,
904 struct got_fileindex_entry *new_ie;
905 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
906 path, blob->id.sha1, worktree->base_commit_id->sha1);
908 err = got_fileindex_entry_add(fileindex, new_ie);
913 static const struct got_error *
914 install_blob(struct got_worktree *worktree, const char *ondisk_path,
915 const char *path, uint16_t te_mode, uint16_t st_mode,
916 struct got_blob_object *blob, int restoring_missing_file,
917 int reverting_versioned_file, struct got_repository *repo,
918 got_worktree_checkout_cb progress_cb, void *progress_arg)
920 const struct got_error *err = NULL;
924 char *tmppath = NULL;
926 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
927 GOT_DEFAULT_FILE_MODE);
929 if (errno == ENOENT) {
930 char *parent = dirname(path);
932 return got_error_from_errno2("dirname", path);
933 err = add_dir_on_disk(worktree, parent);
936 fd = open(ondisk_path,
937 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
938 GOT_DEFAULT_FILE_MODE);
940 return got_error_from_errno2("open",
942 } else if (errno == EEXIST) {
943 if (!S_ISREG(st_mode)) {
944 /* TODO file is obstructed; do something */
945 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
948 err = got_opentemp_named_fd(&tmppath, &fd,
955 return got_error_from_errno2("open", ondisk_path);
958 if (restoring_missing_file)
959 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
960 else if (reverting_versioned_file)
961 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
963 err = (*progress_cb)(progress_arg,
964 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
968 hdrlen = got_object_blob_get_hdrlen(blob);
970 const uint8_t *buf = got_object_blob_get_read_buf(blob);
971 err = got_object_blob_read_block(&len, blob);
975 /* Skip blob object header first time around. */
976 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
978 err = got_error_from_errno("write");
980 } else if (outlen != len - hdrlen) {
981 err = got_error(GOT_ERR_IO);
988 if (fsync(fd) != 0) {
989 err = got_error_from_errno("fsync");
994 if (rename(tmppath, ondisk_path) != 0) {
995 err = got_error_from_errno3("rename", tmppath,
1002 if (te_mode & S_IXUSR) {
1003 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
1004 err = got_error_from_errno2("chmod", ondisk_path);
1008 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1009 err = got_error_from_errno2("chmod", ondisk_path);
1015 if (fd != -1 && close(fd) != 0 && err == NULL)
1016 err = got_error_from_errno("close");
1021 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1022 static const struct got_error *
1023 get_modified_file_content_status(unsigned char *status, FILE *f)
1025 const struct got_error *err = NULL;
1026 const char *markers[3] = {
1027 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1028 GOT_DIFF_CONFLICT_MARKER_SEP,
1029 GOT_DIFF_CONFLICT_MARKER_END
1034 const char delim[3] = {'\0', '\0', '\0'};
1036 while (*status == GOT_STATUS_MODIFY) {
1037 line = fparseln(f, &len, NULL, delim, 0);
1041 err = got_ferror(f, GOT_ERR_IO);
1045 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1046 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1048 *status = GOT_STATUS_CONFLICT;
1058 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1060 return !(ie->ctime_sec == sb->st_ctime &&
1061 ie->ctime_nsec == sb->st_ctimensec &&
1062 ie->mtime_sec == sb->st_mtime &&
1063 ie->mtime_nsec == sb->st_mtimensec &&
1064 ie->size == (sb->st_size & 0xffffffff));
1067 static unsigned char
1068 get_staged_status(struct got_fileindex_entry *ie)
1070 switch (got_fileindex_entry_stage_get(ie)) {
1071 case GOT_FILEIDX_STAGE_ADD:
1072 return GOT_STATUS_ADD;
1073 case GOT_FILEIDX_STAGE_DELETE:
1074 return GOT_STATUS_DELETE;
1075 case GOT_FILEIDX_STAGE_MODIFY:
1076 return GOT_STATUS_MODIFY;
1078 return GOT_STATUS_NO_CHANGE;
1082 static const struct got_error *
1083 get_file_status(unsigned char *status, struct stat *sb,
1084 struct got_fileindex_entry *ie, const char *abspath,
1085 struct got_repository *repo)
1087 const struct got_error *err = NULL;
1088 struct got_object_id id;
1092 struct got_blob_object *blob = NULL;
1094 unsigned char staged_status = get_staged_status(ie);
1096 *status = GOT_STATUS_NO_CHANGE;
1098 if (lstat(abspath, sb) == -1) {
1099 if (errno == ENOENT) {
1100 if (got_fileindex_entry_has_file_on_disk(ie))
1101 *status = GOT_STATUS_MISSING;
1103 *status = GOT_STATUS_DELETE;
1106 return got_error_from_errno2("lstat", abspath);
1109 if (!S_ISREG(sb->st_mode)) {
1110 *status = GOT_STATUS_OBSTRUCTED;
1114 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1115 *status = GOT_STATUS_DELETE;
1117 } else if (!got_fileindex_entry_has_blob(ie) &&
1118 staged_status != GOT_STATUS_ADD) {
1119 *status = GOT_STATUS_ADD;
1123 if (!stat_info_differs(ie, sb))
1126 if (staged_status == GOT_STATUS_MODIFY ||
1127 staged_status == GOT_STATUS_ADD)
1128 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1130 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1132 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1136 f = fopen(abspath, "r");
1138 err = got_error_from_errno2("fopen", abspath);
1141 hdrlen = got_object_blob_get_hdrlen(blob);
1143 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1144 err = got_object_blob_read_block(&blen, blob);
1147 /* Skip length of blob object header first time around. */
1148 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1149 if (flen == 0 && ferror(f)) {
1150 err = got_error_from_errno("fread");
1155 *status = GOT_STATUS_MODIFY;
1157 } else if (flen == 0) {
1159 *status = GOT_STATUS_MODIFY;
1161 } else if (blen - hdrlen == flen) {
1162 /* Skip blob object header first time around. */
1163 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1164 *status = GOT_STATUS_MODIFY;
1168 *status = GOT_STATUS_MODIFY;
1174 if (*status == GOT_STATUS_MODIFY) {
1176 err = get_modified_file_content_status(status, f);
1180 got_object_blob_close(blob);
1187 * Update timestamps in the file index if a file is unmodified and
1188 * we had to run a full content comparison to find out.
1190 static const struct got_error *
1191 sync_timestamps(char *ondisk_path, unsigned char status,
1192 struct got_fileindex_entry *ie, struct stat *sb)
1194 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1195 return got_fileindex_entry_update(ie, ondisk_path,
1196 ie->blob_sha1, ie->commit_sha1, 1);
1201 static const struct got_error *
1202 update_blob(struct got_worktree *worktree,
1203 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1204 struct got_tree_entry *te, const char *path,
1205 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1208 const struct got_error *err = NULL;
1209 struct got_blob_object *blob = NULL;
1211 unsigned char status = GOT_STATUS_NO_CHANGE;
1214 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1215 return got_error_from_errno("asprintf");
1218 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1219 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1222 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1225 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1226 sb.st_mode = got_fileindex_perms_to_st(ie);
1228 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1230 if (status == GOT_STATUS_OBSTRUCTED) {
1231 err = (*progress_cb)(progress_arg, status, path);
1235 if (ie && status != GOT_STATUS_MISSING) {
1236 if (got_fileindex_entry_has_commit(ie) &&
1237 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1238 SHA1_DIGEST_LENGTH) == 0) {
1239 err = sync_timestamps(ondisk_path, status, ie, &sb);
1242 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1246 if (got_fileindex_entry_has_blob(ie) &&
1247 memcmp(ie->blob_sha1, te->id->sha1,
1248 SHA1_DIGEST_LENGTH) == 0) {
1249 err = sync_timestamps(ondisk_path, status, ie, &sb);
1254 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1258 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1259 int update_timestamps;
1260 struct got_blob_object *blob2 = NULL;
1261 if (got_fileindex_entry_has_blob(ie)) {
1262 struct got_object_id id2;
1263 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1264 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1268 err = merge_blob(&update_timestamps, worktree, blob2,
1269 ondisk_path, path, sb.st_mode, blob,
1270 worktree->base_commit_id, repo,
1271 progress_cb, progress_arg);
1273 got_object_blob_close(blob2);
1275 * Do not update timestamps of files with local changes.
1276 * Otherwise, a future status walk would treat them as
1277 * unmodified files again.
1279 err = got_fileindex_entry_update(ie, ondisk_path,
1280 blob->id.sha1, worktree->base_commit_id->sha1,
1282 } else if (status == GOT_STATUS_DELETE) {
1283 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1286 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1287 ondisk_path, path, blob, 0);
1291 err = install_blob(worktree, ondisk_path, path, te->mode,
1292 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1293 repo, progress_cb, progress_arg);
1296 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1297 ondisk_path, path, blob, 1);
1301 got_object_blob_close(blob);
1307 static const struct got_error *
1308 remove_ondisk_file(const char *root_path, const char *path)
1310 const struct got_error *err = NULL;
1311 char *ondisk_path = NULL;
1313 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1314 return got_error_from_errno("asprintf");
1316 if (unlink(ondisk_path) == -1) {
1317 if (errno != ENOENT)
1318 err = got_error_from_errno2("unlink", ondisk_path);
1320 char *parent = dirname(ondisk_path);
1321 while (parent && strcmp(parent, root_path) != 0) {
1322 if (rmdir(parent) == -1) {
1323 if (errno != ENOTEMPTY)
1324 err = got_error_from_errno2("rmdir",
1328 parent = dirname(parent);
1335 static const struct got_error *
1336 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1337 struct got_fileindex_entry *ie, struct got_repository *repo,
1338 got_worktree_checkout_cb progress_cb, void *progress_arg)
1340 const struct got_error *err = NULL;
1341 unsigned char status;
1345 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1346 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1348 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1350 return got_error_from_errno("asprintf");
1352 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1356 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1357 status == GOT_STATUS_ADD) {
1358 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1362 * Preserve the working file and change the deleted blob's
1363 * entry into a schedule-add entry.
1365 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1370 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1373 if (status == GOT_STATUS_NO_CHANGE) {
1374 err = remove_ondisk_file(worktree->root_path, ie->path);
1378 got_fileindex_entry_remove(fileindex, ie);
1384 struct diff_cb_arg {
1385 struct got_fileindex *fileindex;
1386 struct got_worktree *worktree;
1387 struct got_repository *repo;
1388 got_worktree_checkout_cb progress_cb;
1390 got_cancel_cb cancel_cb;
1394 static const struct got_error *
1395 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1396 struct got_tree_entry *te, const char *parent_path)
1398 struct diff_cb_arg *a = arg;
1400 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1401 return got_error(GOT_ERR_CANCELLED);
1403 return update_blob(a->worktree, a->fileindex, ie, te,
1404 ie->path, a->repo, a->progress_cb, a->progress_arg);
1407 static const struct got_error *
1408 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1410 struct diff_cb_arg *a = arg;
1412 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1413 return got_error(GOT_ERR_CANCELLED);
1415 return delete_blob(a->worktree, a->fileindex, ie,
1416 a->repo, a->progress_cb, a->progress_arg);
1419 static const struct got_error *
1420 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1422 struct diff_cb_arg *a = arg;
1423 const struct got_error *err;
1426 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1427 return got_error(GOT_ERR_CANCELLED);
1429 if (asprintf(&path, "%s%s%s", parent_path,
1430 parent_path[0] ? "/" : "", te->name)
1432 return got_error_from_errno("asprintf");
1434 if (S_ISDIR(te->mode))
1435 err = add_dir_on_disk(a->worktree, path);
1437 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1438 a->repo, a->progress_cb, a->progress_arg);
1444 static const struct got_error *
1445 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1447 const struct got_error *err = NULL;
1448 char *uuidstr = NULL;
1449 uint32_t uuid_status;
1453 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1454 if (uuid_status != uuid_s_ok)
1455 return got_error_uuid(uuid_status);
1457 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1459 err = got_error_from_errno("asprintf");
1466 const struct got_error *
1467 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1469 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1472 static const struct got_error *
1473 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1475 return get_ref_name(refname, worktree,
1476 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1479 static const struct got_error *
1480 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1482 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1485 static const struct got_error *
1486 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1488 return get_ref_name(refname, worktree,
1489 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1492 static const struct got_error *
1493 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1495 return get_ref_name(refname, worktree,
1496 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1499 static const struct got_error *
1500 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1502 return get_ref_name(refname, worktree,
1503 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1506 static const struct got_error *
1507 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1509 return get_ref_name(refname, worktree,
1510 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1513 static const struct got_error *
1514 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1516 return get_ref_name(refname, worktree,
1517 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1520 static const struct got_error *
1521 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1523 return get_ref_name(refname, worktree,
1524 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1527 const struct got_error *
1528 got_worktree_get_histedit_script_path(char **path,
1529 struct got_worktree *worktree)
1531 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1532 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1534 return got_error_from_errno("asprintf");
1540 * Prevent Git's garbage collector from deleting our base commit by
1541 * setting a reference to our base commit's ID.
1543 static const struct got_error *
1544 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1546 const struct got_error *err = NULL;
1547 struct got_reference *ref = NULL;
1550 err = got_worktree_get_base_ref_name(&refname, worktree);
1554 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1558 err = got_ref_write(ref, repo);
1566 static const struct got_error *
1567 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1569 const struct got_error *err = NULL;
1571 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1572 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1573 err = got_error_from_errno("asprintf");
1574 *fileindex_path = NULL;
1580 static const struct got_error *
1581 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1582 struct got_worktree *worktree)
1584 const struct got_error *err = NULL;
1587 *fileindex_path = NULL;
1588 *fileindex = got_fileindex_alloc();
1589 if (*fileindex == NULL)
1590 return got_error_from_errno("got_fileindex_alloc");
1592 err = get_fileindex_path(fileindex_path, worktree);
1596 index = fopen(*fileindex_path, "rb");
1597 if (index == NULL) {
1598 if (errno != ENOENT)
1599 err = got_error_from_errno2("fopen", *fileindex_path);
1601 err = got_fileindex_read(*fileindex, index);
1602 if (fclose(index) != 0 && err == NULL)
1603 err = got_error_from_errno("fclose");
1607 free(*fileindex_path);
1608 *fileindex_path = NULL;
1609 got_fileindex_free(*fileindex);
1615 struct bump_base_commit_id_arg {
1616 struct got_object_id *base_commit_id;
1619 const char *entry_name;
1620 got_worktree_checkout_cb progress_cb;
1624 /* Bump base commit ID of all files within an updated part of the work tree. */
1625 static const struct got_error *
1626 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1628 const struct got_error *err;
1629 struct bump_base_commit_id_arg *a = arg;
1631 if (a->entry_name) {
1632 if (strcmp(ie->path, a->path) != 0)
1634 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1637 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1638 SHA1_DIGEST_LENGTH) == 0)
1641 if (a->progress_cb) {
1642 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1647 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1651 static const struct got_error *
1652 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1654 const struct got_error *err = NULL;
1655 char *new_fileindex_path = NULL;
1656 FILE *new_index = NULL;
1658 err = got_opentemp_named(&new_fileindex_path, &new_index,
1663 err = got_fileindex_write(fileindex, new_index);
1667 if (rename(new_fileindex_path, fileindex_path) != 0) {
1668 err = got_error_from_errno3("rename", new_fileindex_path,
1670 unlink(new_fileindex_path);
1675 free(new_fileindex_path);
1679 static const struct got_error *
1680 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1681 struct got_object_id **tree_id, const char *wt_relpath,
1682 struct got_worktree *worktree, struct got_repository *repo)
1684 const struct got_error *err = NULL;
1685 struct got_object_id *id = NULL;
1686 char *in_repo_path = NULL;
1687 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1689 *entry_type = GOT_OBJ_TYPE_ANY;
1690 *tree_relpath = NULL;
1693 if (wt_relpath[0] == '\0') {
1694 /* Check out all files within the work tree. */
1695 *entry_type = GOT_OBJ_TYPE_TREE;
1696 *tree_relpath = strdup("");
1697 if (*tree_relpath == NULL) {
1698 err = got_error_from_errno("strdup");
1701 err = got_object_id_by_path(tree_id, repo,
1702 worktree->base_commit_id, worktree->path_prefix);
1708 /* Check out a subset of files in the work tree. */
1710 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1711 is_root_wt ? "" : "/", wt_relpath) == -1) {
1712 err = got_error_from_errno("asprintf");
1716 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1722 in_repo_path = NULL;
1724 err = got_object_get_type(entry_type, repo, id);
1728 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1729 /* Check out a single file. */
1730 if (strchr(wt_relpath, '/') == NULL) {
1731 /* Check out a single file in work tree's root dir. */
1732 in_repo_path = strdup(worktree->path_prefix);
1733 if (in_repo_path == NULL) {
1734 err = got_error_from_errno("strdup");
1737 *tree_relpath = strdup("");
1738 if (*tree_relpath == NULL) {
1739 err = got_error_from_errno("strdup");
1743 /* Check out a single file in a subdirectory. */
1744 err = got_path_dirname(tree_relpath, wt_relpath);
1747 if (asprintf(&in_repo_path, "%s%s%s",
1748 worktree->path_prefix, is_root_wt ? "" : "/",
1749 *tree_relpath) == -1) {
1750 err = got_error_from_errno("asprintf");
1754 err = got_object_id_by_path(tree_id, repo,
1755 worktree->base_commit_id, in_repo_path);
1757 /* Check out all files within a subdirectory. */
1758 *tree_id = got_object_id_dup(id);
1759 if (*tree_id == NULL) {
1760 err = got_error_from_errno("got_object_id_dup");
1763 *tree_relpath = strdup(wt_relpath);
1764 if (*tree_relpath == NULL) {
1765 err = got_error_from_errno("strdup");
1773 *entry_type = GOT_OBJ_TYPE_ANY;
1774 free(*tree_relpath);
1775 *tree_relpath = NULL;
1782 static const struct got_error *
1783 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1784 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1785 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1786 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1788 const struct got_error *err = NULL;
1789 struct got_commit_object *commit = NULL;
1790 struct got_tree_object *tree = NULL;
1791 struct got_fileindex_diff_tree_cb diff_cb;
1792 struct diff_cb_arg arg;
1794 err = ref_base_commit(worktree, repo);
1798 err = got_object_open_as_commit(&commit, repo,
1799 worktree->base_commit_id);
1803 err = got_object_open_as_tree(&tree, repo, tree_id);
1808 got_object_tree_find_entry(tree, entry_name) == NULL) {
1809 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1813 diff_cb.diff_old_new = diff_old_new;
1814 diff_cb.diff_old = diff_old;
1815 diff_cb.diff_new = diff_new;
1816 arg.fileindex = fileindex;
1817 arg.worktree = worktree;
1819 arg.progress_cb = progress_cb;
1820 arg.progress_arg = progress_arg;
1821 arg.cancel_cb = cancel_cb;
1822 arg.cancel_arg = cancel_arg;
1823 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1824 entry_name, repo, &diff_cb, &arg);
1827 got_object_tree_close(tree);
1829 got_object_commit_close(commit);
1833 const struct got_error *
1834 got_worktree_checkout_files(struct got_worktree *worktree,
1835 struct got_pathlist_head *paths, struct got_repository *repo,
1836 got_worktree_checkout_cb progress_cb, void *progress_arg,
1837 got_cancel_cb cancel_cb, void *cancel_arg)
1839 const struct got_error *err = NULL, *sync_err, *unlockerr;
1840 struct got_commit_object *commit = NULL;
1841 struct got_tree_object *tree = NULL;
1842 struct got_fileindex *fileindex = NULL;
1843 char *fileindex_path = NULL;
1844 struct got_pathlist_entry *pe;
1845 struct tree_path_data {
1846 SIMPLEQ_ENTRY(tree_path_data) entry;
1847 struct got_object_id *tree_id;
1852 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1854 SIMPLEQ_INIT(&tree_paths);
1856 err = lock_worktree(worktree, LOCK_EX);
1860 /* Map all specified paths to in-repository trees. */
1861 TAILQ_FOREACH(pe, paths, entry) {
1862 tpd = malloc(sizeof(*tpd));
1864 err = got_error_from_errno("malloc");
1868 err = find_tree_entry_for_checkout(&tpd->entry_type,
1869 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1875 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1876 err = got_path_basename(&tpd->entry_name, pe->path);
1884 tpd->entry_name = NULL;
1886 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1890 * Read the file index.
1891 * Checking out files is supposed to be an idempotent operation.
1892 * If the on-disk file index is incomplete we will try to complete it.
1894 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1898 tpd = SIMPLEQ_FIRST(&tree_paths);
1899 TAILQ_FOREACH(pe, paths, entry) {
1900 struct bump_base_commit_id_arg bbc_arg;
1902 err = checkout_files(worktree, fileindex, tpd->relpath,
1903 tpd->tree_id, tpd->entry_name, repo,
1904 progress_cb, progress_arg, cancel_cb, cancel_arg);
1908 bbc_arg.base_commit_id = worktree->base_commit_id;
1909 bbc_arg.entry_name = tpd->entry_name;
1910 bbc_arg.path = pe->path;
1911 bbc_arg.path_len = pe->path_len;
1912 bbc_arg.progress_cb = progress_cb;
1913 bbc_arg.progress_arg = progress_arg;
1914 err = got_fileindex_for_each_entry_safe(fileindex,
1915 bump_base_commit_id, &bbc_arg);
1919 tpd = SIMPLEQ_NEXT(tpd, entry);
1921 sync_err = sync_fileindex(fileindex, fileindex_path);
1922 if (sync_err && err == NULL)
1925 free(fileindex_path);
1927 got_object_tree_close(tree);
1929 got_object_commit_close(commit);
1931 got_fileindex_free(fileindex);
1932 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1933 tpd = SIMPLEQ_FIRST(&tree_paths);
1934 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1939 unlockerr = lock_worktree(worktree, LOCK_SH);
1940 if (unlockerr && err == NULL)
1945 struct merge_file_cb_arg {
1946 struct got_worktree *worktree;
1947 struct got_fileindex *fileindex;
1948 got_worktree_checkout_cb progress_cb;
1950 got_cancel_cb cancel_cb;
1952 struct got_object_id *commit_id2;
1955 static const struct got_error *
1956 merge_file_cb(void *arg, struct got_blob_object *blob1,
1957 struct got_blob_object *blob2, struct got_object_id *id1,
1958 struct got_object_id *id2, const char *path1, const char *path2,
1959 struct got_repository *repo)
1961 static const struct got_error *err = NULL;
1962 struct merge_file_cb_arg *a = arg;
1963 struct got_fileindex_entry *ie;
1964 char *ondisk_path = NULL;
1966 unsigned char status;
1967 int local_changes_subsumed;
1969 if (blob1 && blob2) {
1970 ie = got_fileindex_entry_get(a->fileindex, path2,
1973 return (*a->progress_cb)(a->progress_arg,
1974 GOT_STATUS_MISSING, path2);
1976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1978 return got_error_from_errno("asprintf");
1980 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1984 if (status == GOT_STATUS_DELETE) {
1985 err = (*a->progress_cb)(a->progress_arg,
1986 GOT_STATUS_MERGE, path2);
1989 if (status != GOT_STATUS_NO_CHANGE &&
1990 status != GOT_STATUS_MODIFY &&
1991 status != GOT_STATUS_CONFLICT &&
1992 status != GOT_STATUS_ADD) {
1993 err = (*a->progress_cb)(a->progress_arg, status, path2);
1997 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1998 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1999 a->progress_cb, a->progress_arg);
2001 ie = got_fileindex_entry_get(a->fileindex, path1,
2004 return (*a->progress_cb)(a->progress_arg,
2005 GOT_STATUS_MISSING, path2);
2007 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2009 return got_error_from_errno("asprintf");
2011 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2016 case GOT_STATUS_NO_CHANGE:
2017 err = (*a->progress_cb)(a->progress_arg,
2018 GOT_STATUS_DELETE, path1);
2021 err = remove_ondisk_file(a->worktree->root_path, path1);
2025 got_fileindex_entry_mark_deleted_from_disk(ie);
2027 case GOT_STATUS_DELETE:
2028 case GOT_STATUS_MISSING:
2029 err = (*a->progress_cb)(a->progress_arg,
2030 GOT_STATUS_DELETE, path1);
2034 got_fileindex_entry_mark_deleted_from_disk(ie);
2036 case GOT_STATUS_ADD:
2037 case GOT_STATUS_MODIFY:
2038 case GOT_STATUS_CONFLICT:
2039 err = (*a->progress_cb)(a->progress_arg,
2040 GOT_STATUS_CANNOT_DELETE, path1);
2044 case GOT_STATUS_OBSTRUCTED:
2045 err = (*a->progress_cb)(a->progress_arg, status, path1);
2053 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2055 return got_error_from_errno("asprintf");
2056 ie = got_fileindex_entry_get(a->fileindex, path2,
2059 err = get_file_status(&status, &sb, ie, ondisk_path,
2063 if (status != GOT_STATUS_NO_CHANGE &&
2064 status != GOT_STATUS_MODIFY &&
2065 status != GOT_STATUS_CONFLICT &&
2066 status != GOT_STATUS_ADD) {
2067 err = (*a->progress_cb)(a->progress_arg,
2071 err = merge_blob(&local_changes_subsumed, a->worktree,
2072 NULL, ondisk_path, path2, sb.st_mode, blob2,
2073 a->commit_id2, repo,
2074 a->progress_cb, a->progress_arg);
2075 if (status == GOT_STATUS_DELETE) {
2076 err = update_blob_fileindex_entry(a->worktree,
2077 a->fileindex, ie, ondisk_path, ie->path,
2083 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2084 err = install_blob(a->worktree, ondisk_path, path2,
2085 /* XXX get this from parent tree! */
2086 GOT_DEFAULT_FILE_MODE,
2087 sb.st_mode, blob2, 0, 0, repo,
2088 a->progress_cb, a->progress_arg);
2091 err = got_fileindex_entry_alloc(&ie,
2092 ondisk_path, path2, NULL, NULL);
2095 err = got_fileindex_entry_add(a->fileindex, ie);
2097 got_fileindex_entry_free(ie);
2107 struct check_merge_ok_arg {
2108 struct got_worktree *worktree;
2109 struct got_repository *repo;
2112 static const struct got_error *
2113 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2115 const struct got_error *err = NULL;
2116 struct check_merge_ok_arg *a = arg;
2117 unsigned char status;
2121 /* Reject merges into a work tree with mixed base commits. */
2122 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2123 SHA1_DIGEST_LENGTH))
2124 return got_error(GOT_ERR_MIXED_COMMITS);
2126 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2128 return got_error_from_errno("asprintf");
2130 /* Reject merges into a work tree with conflicted files. */
2131 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2134 if (status == GOT_STATUS_CONFLICT)
2135 return got_error(GOT_ERR_CONFLICTS);
2140 static const struct got_error *
2141 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2142 const char *fileindex_path, struct got_object_id *commit_id1,
2143 struct got_object_id *commit_id2, struct got_repository *repo,
2144 got_worktree_checkout_cb progress_cb, void *progress_arg,
2145 got_cancel_cb cancel_cb, void *cancel_arg)
2147 const struct got_error *err = NULL, *sync_err;
2148 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2149 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2150 struct merge_file_cb_arg arg;
2153 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2154 worktree->path_prefix);
2158 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2163 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2164 worktree->path_prefix);
2168 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2172 arg.worktree = worktree;
2173 arg.fileindex = fileindex;
2174 arg.progress_cb = progress_cb;
2175 arg.progress_arg = progress_arg;
2176 arg.cancel_cb = cancel_cb;
2177 arg.cancel_arg = cancel_arg;
2178 arg.commit_id2 = commit_id2;
2179 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2180 sync_err = sync_fileindex(fileindex, fileindex_path);
2181 if (sync_err && err == NULL)
2185 got_object_tree_close(tree1);
2187 got_object_tree_close(tree2);
2191 const struct got_error *
2192 got_worktree_merge_files(struct got_worktree *worktree,
2193 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2194 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2195 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2197 const struct got_error *err, *unlockerr;
2198 char *fileindex_path = NULL;
2199 struct got_fileindex *fileindex = NULL;
2200 struct check_merge_ok_arg mok_arg;
2202 err = lock_worktree(worktree, LOCK_EX);
2206 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2210 mok_arg.worktree = worktree;
2211 mok_arg.repo = repo;
2212 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2217 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2218 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2221 got_fileindex_free(fileindex);
2222 free(fileindex_path);
2223 unlockerr = lock_worktree(worktree, LOCK_SH);
2224 if (unlockerr && err == NULL)
2229 struct diff_dir_cb_arg {
2230 struct got_fileindex *fileindex;
2231 struct got_worktree *worktree;
2232 const char *status_path;
2233 size_t status_path_len;
2234 struct got_repository *repo;
2235 got_worktree_status_cb status_cb;
2237 got_cancel_cb cancel_cb;
2239 /* A pathlist containing per-directory pathlists of ignore patterns. */
2240 struct got_pathlist_head ignores;
2243 static const struct got_error *
2244 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2245 got_worktree_status_cb status_cb, void *status_arg,
2246 struct got_repository *repo)
2248 const struct got_error *err = NULL;
2249 unsigned char status = GOT_STATUS_NO_CHANGE;
2250 unsigned char staged_status = get_staged_status(ie);
2252 struct got_object_id blob_id, commit_id, staged_blob_id;
2253 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2254 struct got_object_id *staged_blob_idp = NULL;
2256 err = get_file_status(&status, &sb, ie, abspath, repo);
2260 if (status == GOT_STATUS_NO_CHANGE &&
2261 staged_status == GOT_STATUS_NO_CHANGE)
2264 if (got_fileindex_entry_has_blob(ie)) {
2265 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2266 blob_idp = &blob_id;
2268 if (got_fileindex_entry_has_commit(ie)) {
2269 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2270 commit_idp = &commit_id;
2272 if (staged_status == GOT_STATUS_ADD ||
2273 staged_status == GOT_STATUS_MODIFY) {
2274 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2275 SHA1_DIGEST_LENGTH);
2276 staged_blob_idp = &staged_blob_id;
2279 return (*status_cb)(status_arg, status, staged_status,
2280 ie->path, blob_idp, staged_blob_idp, commit_idp);
2283 static const struct got_error *
2284 status_old_new(void *arg, struct got_fileindex_entry *ie,
2285 struct dirent *de, const char *parent_path)
2287 const struct got_error *err = NULL;
2288 struct diff_dir_cb_arg *a = arg;
2291 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2292 return got_error(GOT_ERR_CANCELLED);
2294 if (got_path_cmp(parent_path, a->status_path,
2295 strlen(parent_path), a->status_path_len) != 0 &&
2296 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2299 if (parent_path[0]) {
2300 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2301 parent_path, de->d_name) == -1)
2302 return got_error_from_errno("asprintf");
2304 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2306 return got_error_from_errno("asprintf");
2309 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2315 static const struct got_error *
2316 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2318 struct diff_dir_cb_arg *a = arg;
2319 struct got_object_id blob_id, commit_id;
2320 unsigned char status;
2322 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2323 return got_error(GOT_ERR_CANCELLED);
2325 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2328 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2329 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2330 if (got_fileindex_entry_has_file_on_disk(ie))
2331 status = GOT_STATUS_MISSING;
2333 status = GOT_STATUS_DELETE;
2334 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2335 ie->path, &blob_id, NULL, &commit_id);
2339 free_ignorelist(struct got_pathlist_head *ignorelist)
2341 struct got_pathlist_entry *pe;
2343 TAILQ_FOREACH(pe, ignorelist, entry)
2344 free((char *)pe->path);
2345 got_pathlist_free(ignorelist);
2349 free_ignores(struct got_pathlist_head *ignores)
2351 struct got_pathlist_entry *pe;
2353 TAILQ_FOREACH(pe, ignores, entry) {
2354 struct got_pathlist_head *ignorelist = pe->data;
2355 free_ignorelist(ignorelist);
2356 free((char *)pe->path);
2358 got_pathlist_free(ignores);
2361 static const struct got_error *
2362 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2364 const struct got_error *err = NULL;
2365 struct got_pathlist_entry *pe = NULL;
2366 struct got_pathlist_head *ignorelist;
2367 char *line = NULL, *pattern, *dirpath = NULL;
2368 size_t linesize = 0;
2371 ignorelist = calloc(1, sizeof(*ignorelist));
2372 if (ignorelist == NULL)
2373 return got_error_from_errno("calloc");
2374 TAILQ_INIT(ignorelist);
2376 while ((linelen = getline(&line, &linesize, f)) != -1) {
2377 if (linelen > 0 && line[linelen - 1] == '\n')
2378 line[linelen - 1] = '\0';
2379 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2381 err = got_error_from_errno("asprintf");
2384 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2389 err = got_error_from_errno("getline");
2393 dirpath = strdup(path);
2394 if (dirpath == NULL) {
2395 err = got_error_from_errno("strdup");
2398 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2401 if (err || pe == NULL) {
2403 free_ignorelist(ignorelist);
2409 match_ignores(struct got_pathlist_head *ignores, const char *path)
2411 struct got_pathlist_entry *pe;
2414 * The ignores pathlist contains ignore lists from children before
2415 * parents, so we can find the most specific ignorelist by walking
2416 * ignores backwards.
2418 pe = TAILQ_LAST(ignores, got_pathlist_head);
2420 if (got_path_is_child(path, pe->path, pe->path_len)) {
2421 struct got_pathlist_head *ignorelist = pe->data;
2422 struct got_pathlist_entry *pi;
2423 TAILQ_FOREACH(pi, ignorelist, entry) {
2424 if (fnmatch(pi->path, path,
2425 FNM_PATHNAME | FNM_LEADING_DIR))
2430 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2436 static const struct got_error *
2437 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2440 const struct got_error *err = NULL;
2442 FILE *ignoresfile = NULL;
2444 /* TODO: read .gitignores as well... */
2445 if (asprintf(&ignorespath, "%s/%s%s.cvsignore", root_path, path,
2446 path[0] ? "/" : "") == -1)
2447 return got_error_from_errno("asprintf");
2449 ignoresfile = fopen(ignorespath, "r");
2450 if (ignoresfile == NULL) {
2451 if (errno != ENOENT)
2452 err = got_error_from_errno2("fopen",
2455 err = read_ignores(ignores, path, ignoresfile);
2457 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2458 err = got_error_from_errno2("flose", path);
2463 static const struct got_error *
2464 status_new(void *arg, struct dirent *de, const char *parent_path)
2466 const struct got_error *err = NULL;
2467 struct diff_dir_cb_arg *a = arg;
2470 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2471 return got_error(GOT_ERR_CANCELLED);
2473 /* XXX ignore symlinks for now */
2474 if (de->d_type == DT_LNK)
2477 if (parent_path[0]) {
2478 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2479 return got_error_from_errno("asprintf");
2484 if (de->d_type == DT_DIR)
2485 err = add_ignores(&a->ignores, a->worktree->root_path, path);
2486 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2487 && !match_ignores(&a->ignores, path))
2488 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2489 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2495 static const struct got_error *
2496 report_single_file_status(const char *path, const char *ondisk_path,
2497 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2498 void *status_arg, struct got_repository *repo)
2500 struct got_fileindex_entry *ie;
2503 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2505 return report_file_status(ie, ondisk_path, status_cb,
2508 if (lstat(ondisk_path, &sb) == -1) {
2509 if (errno != ENOENT)
2510 return got_error_from_errno2("lstat", ondisk_path);
2514 if (S_ISREG(sb.st_mode))
2515 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2516 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2521 static const struct got_error *
2522 worktree_status(struct got_worktree *worktree, const char *path,
2523 struct got_fileindex *fileindex, struct got_repository *repo,
2524 got_worktree_status_cb status_cb, void *status_arg,
2525 got_cancel_cb cancel_cb, void *cancel_arg)
2527 const struct got_error *err = NULL;
2528 DIR *workdir = NULL;
2529 struct got_fileindex_diff_dir_cb fdiff_cb;
2530 struct diff_dir_cb_arg arg;
2531 char *ondisk_path = NULL;
2533 if (asprintf(&ondisk_path, "%s%s%s",
2534 worktree->root_path, path[0] ? "/" : "", path) == -1)
2535 return got_error_from_errno("asprintf");
2537 workdir = opendir(ondisk_path);
2538 if (workdir == NULL) {
2539 if (errno != ENOTDIR && errno != ENOENT)
2540 err = got_error_from_errno2("opendir", ondisk_path);
2542 err = report_single_file_status(path, ondisk_path,
2543 fileindex, status_cb, status_arg, repo);
2545 fdiff_cb.diff_old_new = status_old_new;
2546 fdiff_cb.diff_old = status_old;
2547 fdiff_cb.diff_new = status_new;
2548 arg.fileindex = fileindex;
2549 arg.worktree = worktree;
2550 arg.status_path = path;
2551 arg.status_path_len = strlen(path);
2553 arg.status_cb = status_cb;
2554 arg.status_arg = status_arg;
2555 arg.cancel_cb = cancel_cb;
2556 arg.cancel_arg = cancel_arg;
2557 TAILQ_INIT(&arg.ignores);
2558 err = add_ignores(&arg.ignores, worktree->root_path, path);
2560 err = got_fileindex_diff_dir(fileindex, workdir,
2561 worktree->root_path, path, repo, &fdiff_cb, &arg);
2562 free_ignores(&arg.ignores);
2571 const struct got_error *
2572 got_worktree_status(struct got_worktree *worktree,
2573 struct got_pathlist_head *paths, struct got_repository *repo,
2574 got_worktree_status_cb status_cb, void *status_arg,
2575 got_cancel_cb cancel_cb, void *cancel_arg)
2577 const struct got_error *err = NULL;
2578 char *fileindex_path = NULL;
2579 struct got_fileindex *fileindex = NULL;
2580 struct got_pathlist_entry *pe;
2582 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2586 TAILQ_FOREACH(pe, paths, entry) {
2587 err = worktree_status(worktree, pe->path, fileindex, repo,
2588 status_cb, status_arg, cancel_cb, cancel_arg);
2592 free(fileindex_path);
2593 got_fileindex_free(fileindex);
2597 const struct got_error *
2598 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2601 const struct got_error *err = NULL;
2602 char *resolved, *cwd = NULL, *path = NULL;
2607 resolved = realpath(arg, NULL);
2608 if (resolved == NULL) {
2609 if (errno != ENOENT)
2610 return got_error_from_errno2("realpath", arg);
2611 cwd = getcwd(NULL, 0);
2613 return got_error_from_errno("getcwd");
2614 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2615 err = got_error_from_errno("asprintf");
2620 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2621 strlen(got_worktree_get_root_path(worktree)))) {
2622 err = got_error(GOT_ERR_BAD_PATH);
2626 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2627 err = got_path_skip_common_ancestor(&path,
2628 got_worktree_get_root_path(worktree), resolved);
2634 err = got_error_from_errno("strdup");
2639 /* XXX status walk can't deal with trailing slash! */
2641 while (len > 0 && path[len - 1] == '/') {
2642 path[len - 1] = '\0';
2655 static const struct got_error *
2656 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2657 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2658 struct got_repository *repo)
2660 const struct got_error *err = NULL;
2661 struct got_fileindex_entry *ie;
2662 unsigned char status;
2665 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2667 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2670 /* Re-adding an existing entry is a no-op. */
2671 if (status == GOT_STATUS_ADD)
2673 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2676 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2680 err = got_fileindex_entry_add(fileindex, ie);
2682 got_fileindex_entry_free(ie);
2686 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2689 const struct got_error *
2690 got_worktree_schedule_add(struct got_worktree *worktree,
2691 struct got_pathlist_head *paths,
2692 got_worktree_status_cb status_cb, void *status_arg,
2693 struct got_repository *repo)
2695 struct got_fileindex *fileindex = NULL;
2696 char *fileindex_path = NULL;
2697 const struct got_error *err = NULL, *sync_err, *unlockerr;
2698 struct got_pathlist_entry *pe;
2700 err = lock_worktree(worktree, LOCK_EX);
2704 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2708 TAILQ_FOREACH(pe, paths, entry) {
2710 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2712 return got_error_from_errno("asprintf");
2713 err = schedule_addition(ondisk_path, fileindex, pe->path,
2714 status_cb, status_arg, repo);
2719 sync_err = sync_fileindex(fileindex, fileindex_path);
2720 if (sync_err && err == NULL)
2723 free(fileindex_path);
2725 got_fileindex_free(fileindex);
2726 unlockerr = lock_worktree(worktree, LOCK_SH);
2727 if (unlockerr && err == NULL)
2732 static const struct got_error *
2733 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2734 const char *relpath, int delete_local_mods,
2735 got_worktree_status_cb status_cb, void *status_arg,
2736 struct got_repository *repo)
2738 const struct got_error *err = NULL;
2739 struct got_fileindex_entry *ie = NULL;
2740 unsigned char status, staged_status;
2743 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2745 return got_error(GOT_ERR_BAD_PATH);
2747 staged_status = get_staged_status(ie);
2748 if (staged_status != GOT_STATUS_NO_CHANGE) {
2749 if (staged_status == GOT_STATUS_DELETE)
2751 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2754 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2758 if (status != GOT_STATUS_NO_CHANGE) {
2759 if (status == GOT_STATUS_DELETE)
2761 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2762 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2763 if (status != GOT_STATUS_MODIFY &&
2764 status != GOT_STATUS_MISSING)
2765 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2768 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2769 return got_error_from_errno2("unlink", ondisk_path);
2771 got_fileindex_entry_mark_deleted_from_disk(ie);
2772 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2775 const struct got_error *
2776 got_worktree_schedule_delete(struct got_worktree *worktree,
2777 struct got_pathlist_head *paths, int delete_local_mods,
2778 got_worktree_status_cb status_cb, void *status_arg,
2779 struct got_repository *repo)
2781 struct got_fileindex *fileindex = NULL;
2782 char *fileindex_path = NULL;
2783 const struct got_error *err = NULL, *sync_err, *unlockerr;
2784 struct got_pathlist_entry *pe;
2786 err = lock_worktree(worktree, LOCK_EX);
2790 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2794 TAILQ_FOREACH(pe, paths, entry) {
2796 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2798 return got_error_from_errno("asprintf");
2799 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2800 delete_local_mods, status_cb, status_arg, repo);
2805 sync_err = sync_fileindex(fileindex, fileindex_path);
2806 if (sync_err && err == NULL)
2809 free(fileindex_path);
2811 got_fileindex_free(fileindex);
2812 unlockerr = lock_worktree(worktree, LOCK_SH);
2813 if (unlockerr && err == NULL)
2818 static const struct got_error *
2819 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2821 const struct got_error *err = NULL;
2823 size_t linesize = 0, n;
2826 linelen = getline(&line, &linesize, infile);
2827 if (linelen == -1) {
2828 if (ferror(infile)) {
2829 err = got_error_from_errno("getline");
2835 n = fwrite(line, 1, linelen, outfile);
2837 err = got_ferror(outfile, GOT_ERR_IO);
2842 n = fwrite(line, 1, linelen, rejectfile);
2844 err = got_ferror(outfile, GOT_ERR_IO);
2851 static const struct got_error *
2852 skip_one_line(FILE *f)
2855 size_t linesize = 0;
2858 linelen = getline(&line, &linesize, f);
2860 if (linelen == -1 && ferror(f))
2861 return got_error_from_errno("getline");
2865 static const struct got_error *
2866 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2867 int start_old, int end_old, int start_new, int end_new,
2868 FILE *outfile, FILE *rejectfile)
2870 const struct got_error *err;
2872 /* Copy old file's lines leading up to patch. */
2873 while (!feof(f1) && *line_cur1 < start_old) {
2874 err = copy_one_line(f1, outfile, NULL);
2879 /* Skip new file's lines leading up to patch. */
2880 while (!feof(f2) && *line_cur2 < start_new) {
2882 err = copy_one_line(f2, NULL, rejectfile);
2884 err = skip_one_line(f2);
2889 /* Copy patched lines. */
2890 while (!feof(f2) && *line_cur2 <= end_new) {
2891 err = copy_one_line(f2, outfile, NULL);
2896 /* Skip over old file's replaced lines. */
2897 while (!feof(f1) && *line_cur1 <= end_old) {
2899 err = copy_one_line(f1, NULL, rejectfile);
2901 err = skip_one_line(f1);
2910 static const struct got_error *
2911 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
2912 FILE *outfile, FILE *rejectfile)
2914 const struct got_error *err;
2917 /* Copy old file's lines until EOF. */
2919 err = copy_one_line(f1, outfile, NULL);
2926 /* Copy new file's lines until EOF. */
2928 err = copy_one_line(f2, NULL, rejectfile);
2938 static const struct got_error *
2939 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
2940 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
2941 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
2942 int *line_cur2, FILE *outfile, FILE *rejectfile,
2943 got_worktree_patch_cb patch_cb, void *patch_arg)
2945 const struct got_error *err = NULL;
2946 int start_old = change->cv.a;
2947 int end_old = change->cv.b;
2948 int start_new = change->cv.c;
2949 int end_new = change->cv.d;
2953 *choice = GOT_PATCH_CHOICE_NONE;
2955 hunkfile = got_opentemp();
2956 if (hunkfile == NULL)
2957 return got_error_from_errno("got_opentemp");
2962 /* XXX TODO needs error checking */
2963 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
2965 if (fseek(f1, pos1, SEEK_SET) == -1) {
2966 err = got_ferror(f1, GOT_ERR_IO);
2969 if (fseek(f2, pos2, SEEK_SET) == -1) {
2970 err = got_ferror(f1, GOT_ERR_IO);
2973 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
2974 err = got_ferror(hunkfile, GOT_ERR_IO);
2978 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
2979 hunkfile, n, nchanges);
2984 case GOT_PATCH_CHOICE_YES:
2985 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2986 end_old, start_new, end_new, outfile, rejectfile);
2988 case GOT_PATCH_CHOICE_NO:
2989 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
2990 end_old, start_new, end_new, rejectfile, outfile);
2992 case GOT_PATCH_CHOICE_QUIT:
2995 err = got_error(GOT_ERR_PATCH_CHOICE);
2999 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3000 err = got_error_from_errno("fclose");
3004 struct revert_file_args {
3005 struct got_worktree *worktree;
3006 struct got_fileindex *fileindex;
3007 got_worktree_checkout_cb progress_cb;
3009 got_worktree_patch_cb patch_cb;
3011 struct got_repository *repo;
3014 static const struct got_error *
3015 create_patched_content(char **path_outfile, int reverse_patch,
3016 struct got_object_id *blob_id, const char *path2,
3017 const char *relpath, struct got_repository *repo,
3018 got_worktree_patch_cb patch_cb, void *patch_arg)
3020 const struct got_error *err;
3021 struct got_blob_object *blob = NULL;
3022 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3023 char *path1 = NULL, *id_str = NULL;
3024 struct stat sb1, sb2;
3025 struct got_diff_changes *changes = NULL;
3026 struct got_diff_state *ds = NULL;
3027 struct got_diff_args *args = NULL;
3028 struct got_diff_change *change;
3029 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3032 *path_outfile = NULL;
3034 err = got_object_id_str(&id_str, blob_id);
3038 f2 = fopen(path2, "r");
3040 err = got_error_from_errno2("fopen", path2);
3044 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3048 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3052 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3056 if (stat(path1, &sb1) == -1) {
3057 err = got_error_from_errno2("stat", path1);
3060 if (stat(path2, &sb2) == -1) {
3061 err = got_error_from_errno2("stat", path2);
3065 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3066 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3070 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3074 if (fseek(f1, 0L, SEEK_SET) == -1)
3075 return got_ferror(f1, GOT_ERR_IO);
3076 if (fseek(f2, 0L, SEEK_SET) == -1)
3077 return got_ferror(f2, GOT_ERR_IO);
3078 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3080 err = apply_or_reject_change(&choice, change, ++n,
3081 changes->nchanges, ds, args, diff_flags, relpath,
3082 f1, f2, &line_cur1, &line_cur2,
3083 reverse_patch ? NULL : outfile,
3084 reverse_patch ? outfile : NULL,
3085 patch_cb, patch_arg);
3088 if (choice == GOT_PATCH_CHOICE_YES)
3090 else if (choice == GOT_PATCH_CHOICE_QUIT)
3094 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3095 reverse_patch ? NULL : outfile,
3096 reverse_patch ? outfile : NULL);
3100 got_object_blob_close(blob);
3101 if (f1 && fclose(f1) == EOF && err == NULL)
3102 err = got_error_from_errno2("fclose", path1);
3103 if (f2 && fclose(f2) == EOF && err == NULL)
3104 err = got_error_from_errno2("fclose", path2);
3105 if (outfile && fclose(outfile) == EOF && err == NULL)
3106 err = got_error_from_errno2("fclose", *path_outfile);
3107 if (path1 && unlink(path1) == -1 && err == NULL)
3108 err = got_error_from_errno2("unlink", path1);
3109 if (err || !have_content) {
3110 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3111 err = got_error_from_errno2("unlink", *path_outfile);
3112 free(*path_outfile);
3113 *path_outfile = NULL;
3117 got_diff_state_free(ds);
3121 got_diff_free_changes(changes);
3126 static const struct got_error *
3127 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3128 const char *relpath, struct got_object_id *blob_id,
3129 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3131 struct revert_file_args *a = arg;
3132 const struct got_error *err = NULL;
3133 char *parent_path = NULL;
3134 struct got_fileindex_entry *ie;
3135 struct got_tree_object *tree = NULL;
3136 struct got_object_id *tree_id = NULL;
3137 const struct got_tree_entry *te = NULL;
3138 char *tree_path = NULL, *te_name;
3139 char *ondisk_path = NULL, *path_content = NULL;
3140 struct got_blob_object *blob = NULL;
3142 /* Reverting a staged deletion is a no-op. */
3143 if (status == GOT_STATUS_DELETE &&
3144 staged_status != GOT_STATUS_NO_CHANGE)
3147 if (status == GOT_STATUS_UNVERSIONED)
3148 return (*a->progress_cb)(a->progress_arg,
3149 GOT_STATUS_UNVERSIONED, relpath);
3151 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3153 return got_error(GOT_ERR_BAD_PATH);
3155 /* Construct in-repository path of tree which contains this blob. */
3156 err = got_path_dirname(&parent_path, ie->path);
3158 if (err->code != GOT_ERR_BAD_PATH)
3160 parent_path = strdup("/");
3161 if (parent_path == NULL) {
3162 err = got_error_from_errno("strdup");
3166 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3167 tree_path = strdup(parent_path);
3168 if (tree_path == NULL) {
3169 err = got_error_from_errno("strdup");
3173 if (got_path_is_root_dir(parent_path)) {
3174 tree_path = strdup(a->worktree->path_prefix);
3175 if (tree_path == NULL) {
3176 err = got_error_from_errno("strdup");
3180 if (asprintf(&tree_path, "%s/%s",
3181 a->worktree->path_prefix, parent_path) == -1) {
3182 err = got_error_from_errno("asprintf");
3188 err = got_object_id_by_path(&tree_id, a->repo,
3189 a->worktree->base_commit_id, tree_path);
3191 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3192 (status == GOT_STATUS_ADD ||
3193 staged_status == GOT_STATUS_ADD)))
3196 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3200 te_name = basename(ie->path);
3201 if (te_name == NULL) {
3202 err = got_error_from_errno2("basename", ie->path);
3206 te = got_object_tree_find_entry(tree, te_name);
3207 if (te == NULL && status != GOT_STATUS_ADD &&
3208 staged_status != GOT_STATUS_ADD) {
3209 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3215 case GOT_STATUS_ADD:
3217 int choice = GOT_PATCH_CHOICE_NONE;
3218 err = (*a->patch_cb)(&choice, a->patch_arg,
3219 status, ie->path, NULL, 1, 1);
3222 if (choice != GOT_PATCH_CHOICE_YES)
3225 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3229 got_fileindex_entry_remove(a->fileindex, ie);
3231 case GOT_STATUS_DELETE:
3233 int choice = GOT_PATCH_CHOICE_NONE;
3234 err = (*a->patch_cb)(&choice, a->patch_arg,
3235 status, ie->path, NULL, 1, 1);
3238 if (choice != GOT_PATCH_CHOICE_YES)
3242 case GOT_STATUS_MODIFY:
3243 case GOT_STATUS_CONFLICT:
3244 case GOT_STATUS_MISSING: {
3245 struct got_object_id id;
3246 if (staged_status == GOT_STATUS_ADD ||
3247 staged_status == GOT_STATUS_MODIFY) {
3248 memcpy(id.sha1, ie->staged_blob_sha1,
3249 SHA1_DIGEST_LENGTH);
3251 memcpy(id.sha1, ie->blob_sha1,
3252 SHA1_DIGEST_LENGTH);
3253 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3257 if (asprintf(&ondisk_path, "%s/%s",
3258 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3259 err = got_error_from_errno("asprintf");
3263 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3264 status == GOT_STATUS_CONFLICT)) {
3265 err = create_patched_content(&path_content, 1, &id,
3266 ondisk_path, ie->path, a->repo,
3267 a->patch_cb, a->patch_arg);
3268 if (err || path_content == NULL)
3270 if (rename(path_content, ondisk_path) == -1) {
3271 err = got_error_from_errno3("rename",
3272 path_content, ondisk_path);
3276 err = install_blob(a->worktree, ondisk_path, ie->path,
3277 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3278 got_fileindex_perms_to_st(ie), blob, 0, 1,
3279 a->repo, a->progress_cb, a->progress_arg);
3282 if (status == GOT_STATUS_DELETE) {
3283 err = update_blob_fileindex_entry(a->worktree,
3284 a->fileindex, ie, ondisk_path, ie->path,
3301 got_object_blob_close(blob);
3303 got_object_tree_close(tree);
3308 const struct got_error *
3309 got_worktree_revert(struct got_worktree *worktree,
3310 struct got_pathlist_head *paths,
3311 got_worktree_checkout_cb progress_cb, void *progress_arg,
3312 got_worktree_patch_cb patch_cb, void *patch_arg,
3313 struct got_repository *repo)
3315 struct got_fileindex *fileindex = NULL;
3316 char *fileindex_path = NULL;
3317 const struct got_error *err = NULL, *unlockerr = NULL;
3318 const struct got_error *sync_err = NULL;
3319 struct got_pathlist_entry *pe;
3320 struct revert_file_args rfa;
3322 err = lock_worktree(worktree, LOCK_EX);
3326 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3330 rfa.worktree = worktree;
3331 rfa.fileindex = fileindex;
3332 rfa.progress_cb = progress_cb;
3333 rfa.progress_arg = progress_arg;
3334 rfa.patch_cb = patch_cb;
3335 rfa.patch_arg = patch_arg;
3337 TAILQ_FOREACH(pe, paths, entry) {
3338 err = worktree_status(worktree, pe->path, fileindex, repo,
3339 revert_file, &rfa, NULL, NULL);
3343 sync_err = sync_fileindex(fileindex, fileindex_path);
3344 if (sync_err && err == NULL)
3347 free(fileindex_path);
3349 got_fileindex_free(fileindex);
3350 unlockerr = lock_worktree(worktree, LOCK_SH);
3351 if (unlockerr && err == NULL)
3357 free_commitable(struct got_commitable *ct)
3360 free(ct->in_repo_path);
3361 free(ct->ondisk_path);
3363 free(ct->base_blob_id);
3364 free(ct->staged_blob_id);
3365 free(ct->base_commit_id);
3369 struct collect_commitables_arg {
3370 struct got_pathlist_head *commitable_paths;
3371 struct got_repository *repo;
3372 struct got_worktree *worktree;
3373 int have_staged_files;
3376 static const struct got_error *
3377 collect_commitables(void *arg, unsigned char status,
3378 unsigned char staged_status, const char *relpath,
3379 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3380 struct got_object_id *commit_id)
3382 struct collect_commitables_arg *a = arg;
3383 const struct got_error *err = NULL;
3384 struct got_commitable *ct = NULL;
3385 struct got_pathlist_entry *new = NULL;
3386 char *parent_path = NULL, *path = NULL;
3389 if (a->have_staged_files) {
3390 if (staged_status != GOT_STATUS_MODIFY &&
3391 staged_status != GOT_STATUS_ADD &&
3392 staged_status != GOT_STATUS_DELETE)
3395 if (status == GOT_STATUS_CONFLICT)
3396 return got_error(GOT_ERR_COMMIT_CONFLICT);
3398 if (status != GOT_STATUS_MODIFY &&
3399 status != GOT_STATUS_ADD &&
3400 status != GOT_STATUS_DELETE)
3404 if (asprintf(&path, "/%s", relpath) == -1) {
3405 err = got_error_from_errno("asprintf");
3408 if (strcmp(path, "/") == 0) {
3409 parent_path = strdup("");
3410 if (parent_path == NULL)
3411 return got_error_from_errno("strdup");
3413 err = got_path_dirname(&parent_path, path);
3418 ct = calloc(1, sizeof(*ct));
3420 err = got_error_from_errno("calloc");
3424 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3426 err = got_error_from_errno("asprintf");
3429 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3430 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3432 if (lstat(ct->ondisk_path, &sb) != 0) {
3433 err = got_error_from_errno2("lstat", ct->ondisk_path);
3436 ct->mode = sb.st_mode;
3439 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3440 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3442 err = got_error_from_errno("asprintf");
3446 ct->status = status;
3447 ct->staged_status = staged_status;
3448 ct->blob_id = NULL; /* will be filled in when blob gets created */
3449 if (ct->status != GOT_STATUS_ADD &&
3450 ct->staged_status != GOT_STATUS_ADD) {
3451 ct->base_blob_id = got_object_id_dup(blob_id);
3452 if (ct->base_blob_id == NULL) {
3453 err = got_error_from_errno("got_object_id_dup");
3456 ct->base_commit_id = got_object_id_dup(commit_id);
3457 if (ct->base_commit_id == NULL) {
3458 err = got_error_from_errno("got_object_id_dup");
3462 if (ct->staged_status == GOT_STATUS_ADD ||
3463 ct->staged_status == GOT_STATUS_MODIFY) {
3464 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3465 if (ct->staged_blob_id == NULL) {
3466 err = got_error_from_errno("got_object_id_dup");
3470 ct->path = strdup(path);
3471 if (ct->path == NULL) {
3472 err = got_error_from_errno("strdup");
3475 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3477 if (ct && (err || new == NULL))
3478 free_commitable(ct);
3484 static const struct got_error *write_tree(struct got_object_id **,
3485 struct got_tree_object *, const char *, struct got_pathlist_head *,
3486 got_worktree_status_cb status_cb, void *status_arg,
3487 struct got_repository *);
3489 static const struct got_error *
3490 write_subtree(struct got_object_id **new_subtree_id,
3491 struct got_tree_entry *te, const char *parent_path,
3492 struct got_pathlist_head *commitable_paths,
3493 got_worktree_status_cb status_cb, void *status_arg,
3494 struct got_repository *repo)
3496 const struct got_error *err = NULL;
3497 struct got_tree_object *subtree;
3500 if (asprintf(&subpath, "%s%s%s", parent_path,
3501 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3502 return got_error_from_errno("asprintf");
3504 err = got_object_open_as_tree(&subtree, repo, te->id);
3508 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3509 status_cb, status_arg, repo);
3510 got_object_tree_close(subtree);
3515 static const struct got_error *
3516 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3518 const struct got_error *err = NULL;
3519 char *ct_parent_path = NULL;
3523 if (strchr(ct->in_repo_path, '/') == NULL) {
3524 *match = got_path_is_root_dir(path);
3528 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3531 *match = (strcmp(path, ct_parent_path) == 0);
3532 free(ct_parent_path);
3537 get_ct_file_mode(struct got_commitable *ct)
3539 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3542 static const struct got_error *
3543 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3544 struct got_tree_entry *te, struct got_commitable *ct)
3546 const struct got_error *err = NULL;
3550 err = got_object_tree_entry_dup(new_te, te);
3554 (*new_te)->mode = get_ct_file_mode(ct);
3556 free((*new_te)->id);
3557 if (ct->staged_status == GOT_STATUS_MODIFY)
3558 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3560 (*new_te)->id = got_object_id_dup(ct->blob_id);
3561 if ((*new_te)->id == NULL) {
3562 err = got_error_from_errno("got_object_id_dup");
3566 if (err && *new_te) {
3567 got_object_tree_entry_close(*new_te);
3573 static const struct got_error *
3574 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3575 struct got_commitable *ct)
3577 const struct got_error *err = NULL;
3582 *new_te = calloc(1, sizeof(**new_te));
3583 if (*new_te == NULL)
3584 return got_error_from_errno("calloc");
3586 ct_name = basename(ct->path);
3587 if (ct_name == NULL) {
3588 err = got_error_from_errno2("basename", ct->path);
3591 (*new_te)->name = strdup(ct_name);
3592 if ((*new_te)->name == NULL) {
3593 err = got_error_from_errno("strdup");
3597 (*new_te)->mode = get_ct_file_mode(ct);
3599 if (ct->staged_status == GOT_STATUS_ADD)
3600 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3602 (*new_te)->id = got_object_id_dup(ct->blob_id);
3603 if ((*new_te)->id == NULL) {
3604 err = got_error_from_errno("got_object_id_dup");
3608 if (err && *new_te) {
3609 got_object_tree_entry_close(*new_te);
3615 static const struct got_error *
3616 insert_tree_entry(struct got_tree_entry *new_te,
3617 struct got_pathlist_head *paths)
3619 const struct got_error *err = NULL;
3620 struct got_pathlist_entry *new_pe;
3622 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3626 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3630 static const struct got_error *
3631 report_ct_status(struct got_commitable *ct,
3632 got_worktree_status_cb status_cb, void *status_arg)
3634 const char *ct_path = ct->path;
3635 unsigned char status;
3637 while (ct_path[0] == '/')
3640 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3641 status = ct->staged_status;
3643 status = ct->status;
3645 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3646 ct_path, ct->blob_id, NULL, NULL);
3649 static const struct got_error *
3650 match_modified_subtree(int *modified, struct got_tree_entry *te,
3651 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3653 const struct got_error *err = NULL;
3654 struct got_pathlist_entry *pe;
3659 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3660 got_path_is_root_dir(base_tree_path) ? "" : "/",
3662 return got_error_from_errno("asprintf");
3664 TAILQ_FOREACH(pe, commitable_paths, entry) {
3665 struct got_commitable *ct = pe->data;
3666 *modified = got_path_is_child(ct->in_repo_path, te_path,
3676 static const struct got_error *
3677 match_deleted_or_modified_ct(struct got_commitable **ctp,
3678 struct got_tree_entry *te, const char *base_tree_path,
3679 struct got_pathlist_head *commitable_paths)
3681 const struct got_error *err = NULL;
3682 struct got_pathlist_entry *pe;
3686 TAILQ_FOREACH(pe, commitable_paths, entry) {
3687 struct got_commitable *ct = pe->data;
3688 char *ct_name = NULL;
3691 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3692 if (ct->status != GOT_STATUS_MODIFY &&
3693 ct->status != GOT_STATUS_DELETE)
3696 if (ct->staged_status != GOT_STATUS_MODIFY &&
3697 ct->staged_status != GOT_STATUS_DELETE)
3701 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3704 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3710 ct_name = basename(pe->path);
3711 if (ct_name == NULL)
3712 return got_error_from_errno2("basename", pe->path);
3714 if (strcmp(te->name, ct_name) != 0)
3724 static const struct got_error *
3725 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3726 const char *child_path, const char *path_base_tree,
3727 struct got_pathlist_head *commitable_paths,
3728 got_worktree_status_cb status_cb, void *status_arg,
3729 struct got_repository *repo)
3731 const struct got_error *err = NULL;
3732 struct got_tree_entry *new_te;
3737 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3738 got_path_is_root_dir(path_base_tree) ? "" : "/",
3740 return got_error_from_errno("asprintf");
3742 new_te = calloc(1, sizeof(*new_te));
3743 new_te->mode = S_IFDIR;
3744 new_te->name = strdup(child_path);
3745 if (new_te->name == NULL) {
3746 err = got_error_from_errno("strdup");
3747 got_object_tree_entry_close(new_te);
3750 err = write_tree(&new_te->id, NULL, subtree_path,
3751 commitable_paths, status_cb, status_arg, repo);
3753 got_object_tree_entry_close(new_te);
3763 static const struct got_error *
3764 write_tree(struct got_object_id **new_tree_id,
3765 struct got_tree_object *base_tree, const char *path_base_tree,
3766 struct got_pathlist_head *commitable_paths,
3767 got_worktree_status_cb status_cb, void *status_arg,
3768 struct got_repository *repo)
3770 const struct got_error *err = NULL;
3771 const struct got_tree_entries *base_entries = NULL;
3772 struct got_pathlist_head paths;
3773 struct got_tree_entries new_tree_entries;
3774 struct got_tree_entry *te, *new_te = NULL;
3775 struct got_pathlist_entry *pe;
3778 new_tree_entries.nentries = 0;
3779 SIMPLEQ_INIT(&new_tree_entries.head);
3781 /* Insert, and recurse into, newly added entries first. */
3782 TAILQ_FOREACH(pe, commitable_paths, entry) {
3783 struct got_commitable *ct = pe->data;
3784 char *child_path = NULL, *slash;
3786 if ((ct->status != GOT_STATUS_ADD &&
3787 ct->staged_status != GOT_STATUS_ADD) ||
3788 (ct->flags & GOT_COMMITABLE_ADDED))
3791 if (!got_path_is_child(pe->path, path_base_tree,
3792 strlen(path_base_tree)))
3795 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3800 slash = strchr(child_path, '/');
3801 if (slash == NULL) {
3802 err = alloc_added_blob_tree_entry(&new_te, ct);
3805 err = report_ct_status(ct, status_cb, status_arg);
3808 ct->flags |= GOT_COMMITABLE_ADDED;
3809 err = insert_tree_entry(new_te, &paths);
3813 *slash = '\0'; /* trim trailing path components */
3814 if (base_tree == NULL ||
3815 got_object_tree_find_entry(base_tree, child_path)
3817 err = make_subtree_for_added_blob(&new_te,
3818 child_path, path_base_tree,
3819 commitable_paths, status_cb, status_arg,
3823 err = insert_tree_entry(new_te, &paths);
3831 /* Handle modified and deleted entries. */
3832 base_entries = got_object_tree_get_entries(base_tree);
3833 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3834 struct got_commitable *ct = NULL;
3836 if (S_ISDIR(te->mode)) {
3838 err = got_object_tree_entry_dup(&new_te, te);
3841 err = match_modified_subtree(&modified, te,
3842 path_base_tree, commitable_paths);
3845 /* Avoid recursion into unmodified subtrees. */
3848 err = write_subtree(&new_te->id, te,
3849 path_base_tree, commitable_paths,
3850 status_cb, status_arg, repo);
3854 err = insert_tree_entry(new_te, &paths);
3860 err = match_deleted_or_modified_ct(&ct, te,
3861 path_base_tree, commitable_paths);
3863 /* NB: Deleted entries get dropped here. */
3864 if (ct->status == GOT_STATUS_MODIFY ||
3865 ct->staged_status == GOT_STATUS_MODIFY) {
3866 err = alloc_modified_blob_tree_entry(
3870 err = insert_tree_entry(new_te, &paths);
3874 err = report_ct_status(ct, status_cb,
3879 /* Entry is unchanged; just copy it. */
3880 err = got_object_tree_entry_dup(&new_te, te);
3883 err = insert_tree_entry(new_te, &paths);
3890 /* Write new list of entries; deleted entries have been dropped. */
3891 TAILQ_FOREACH(pe, &paths, entry) {
3892 struct got_tree_entry *te = pe->data;
3893 new_tree_entries.nentries++;
3894 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3896 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3898 got_object_tree_entries_close(&new_tree_entries);
3899 got_pathlist_free(&paths);
3903 static const struct got_error *
3904 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3905 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3907 const struct got_error *err = NULL;
3908 struct got_pathlist_entry *pe;
3910 TAILQ_FOREACH(pe, commitable_paths, entry) {
3911 struct got_fileindex_entry *ie;
3912 struct got_commitable *ct = pe->data;
3914 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3916 if (ct->status == GOT_STATUS_DELETE ||
3917 ct->staged_status == GOT_STATUS_DELETE) {
3918 got_fileindex_entry_remove(fileindex, ie);
3919 got_fileindex_entry_free(ie);
3920 } else if (ct->staged_status == GOT_STATUS_ADD ||
3921 ct->staged_status == GOT_STATUS_MODIFY) {
3922 got_fileindex_entry_stage_set(ie,
3923 GOT_FILEIDX_STAGE_NONE);
3924 err = got_fileindex_entry_update(ie,
3925 ct->ondisk_path, ct->staged_blob_id->sha1,
3926 new_base_commit_id->sha1, 1);
3928 err = got_fileindex_entry_update(ie,
3929 ct->ondisk_path, ct->blob_id->sha1,
3930 new_base_commit_id->sha1, 1);
3932 err = got_fileindex_entry_alloc(&ie,
3933 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3934 new_base_commit_id->sha1);
3937 err = got_fileindex_entry_add(fileindex, ie);
3946 static const struct got_error *
3947 check_out_of_date(const char *in_repo_path, unsigned char status,
3948 unsigned char staged_status, struct got_object_id *base_blob_id,
3949 struct got_object_id *base_commit_id,
3950 struct got_object_id *head_commit_id, struct got_repository *repo,
3953 const struct got_error *err = NULL;
3954 struct got_object_id *id = NULL;
3956 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3957 /* Trivial case: base commit == head commit */
3958 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3961 * Ensure file content which local changes were based
3962 * on matches file content in the branch head.
3964 err = got_object_id_by_path(&id, repo, head_commit_id,
3967 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3968 err = got_error(ood_errcode);
3970 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3971 err = got_error(ood_errcode);
3973 /* Require that added files don't exist in the branch head. */
3974 err = got_object_id_by_path(&id, repo, head_commit_id,
3976 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3978 err = id ? got_error(ood_errcode) : NULL;
3985 const struct got_error *
3986 commit_worktree(struct got_object_id **new_commit_id,
3987 struct got_pathlist_head *commitable_paths,
3988 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3989 const char *author, const char *committer,
3990 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3991 got_worktree_status_cb status_cb, void *status_arg,
3992 struct got_repository *repo)
3994 const struct got_error *err = NULL, *unlockerr = NULL;
3995 struct got_pathlist_entry *pe;
3996 const char *head_ref_name = NULL;
3997 struct got_commit_object *head_commit = NULL;
3998 struct got_reference *head_ref2 = NULL;
3999 struct got_object_id *head_commit_id2 = NULL;
4000 struct got_tree_object *head_tree = NULL;
4001 struct got_object_id *new_tree_id = NULL;
4002 struct got_object_id_queue parent_ids;
4003 struct got_object_qid *pid = NULL;
4004 char *logmsg = NULL;
4006 *new_commit_id = NULL;
4008 SIMPLEQ_INIT(&parent_ids);
4010 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4014 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4018 if (commit_msg_cb != NULL) {
4019 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4024 if (logmsg == NULL || strlen(logmsg) == 0) {
4025 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4029 /* Create blobs from added and modified files and record their IDs. */
4030 TAILQ_FOREACH(pe, commitable_paths, entry) {
4031 struct got_commitable *ct = pe->data;
4034 /* Blobs for staged files already exist. */
4035 if (ct->staged_status == GOT_STATUS_ADD ||
4036 ct->staged_status == GOT_STATUS_MODIFY)
4039 if (ct->status != GOT_STATUS_ADD &&
4040 ct->status != GOT_STATUS_MODIFY)
4043 if (asprintf(&ondisk_path, "%s/%s",
4044 worktree->root_path, pe->path) == -1) {
4045 err = got_error_from_errno("asprintf");
4048 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4054 /* Recursively write new tree objects. */
4055 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4056 status_cb, status_arg, repo);
4060 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4063 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4064 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4065 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4066 got_object_qid_free(pid);
4072 /* Check if a concurrent commit to our branch has occurred. */
4073 head_ref_name = got_worktree_get_head_ref_name(worktree);
4074 if (head_ref_name == NULL) {
4075 err = got_error_from_errno("got_worktree_get_head_ref_name");
4078 /* Lock the reference here to prevent concurrent modification. */
4079 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4082 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4085 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4086 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4089 /* Update branch head in repository. */
4090 err = got_ref_change_ref(head_ref2, *new_commit_id);
4093 err = got_ref_write(head_ref2, repo);
4097 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4101 err = ref_base_commit(worktree, repo);
4106 got_object_tree_close(head_tree);
4108 got_object_commit_close(head_commit);
4109 free(head_commit_id2);
4111 unlockerr = got_ref_unlock(head_ref2);
4112 if (unlockerr && err == NULL)
4114 got_ref_close(head_ref2);
4119 static const struct got_error *
4120 check_path_is_commitable(const char *path,
4121 struct got_pathlist_head *commitable_paths)
4123 struct got_pathlist_entry *cpe = NULL;
4124 size_t path_len = strlen(path);
4126 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4127 struct got_commitable *ct = cpe->data;
4128 const char *ct_path = ct->path;
4130 while (ct_path[0] == '/')
4133 if (strcmp(path, ct_path) == 0 ||
4134 got_path_is_child(ct_path, path, path_len))
4139 return got_error_path(path, GOT_ERR_BAD_PATH);
4144 static const struct got_error *
4145 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4147 int *have_staged_files = arg;
4149 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4150 *have_staged_files = 1;
4151 return got_error(GOT_ERR_CANCELLED);
4157 static const struct got_error *
4158 check_non_staged_files(struct got_fileindex *fileindex,
4159 struct got_pathlist_head *paths)
4161 struct got_pathlist_entry *pe;
4162 struct got_fileindex_entry *ie;
4164 TAILQ_FOREACH(pe, paths, entry) {
4165 if (pe->path[0] == '\0')
4167 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4169 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4170 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4171 return got_error_path(pe->path,
4172 GOT_ERR_FILE_NOT_STAGED);
4178 const struct got_error *
4179 got_worktree_commit(struct got_object_id **new_commit_id,
4180 struct got_worktree *worktree, struct got_pathlist_head *paths,
4181 const char *author, const char *committer,
4182 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4183 got_worktree_status_cb status_cb, void *status_arg,
4184 struct got_repository *repo)
4186 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4187 struct got_fileindex *fileindex = NULL;
4188 char *fileindex_path = NULL;
4189 struct got_pathlist_head commitable_paths;
4190 struct collect_commitables_arg cc_arg;
4191 struct got_pathlist_entry *pe;
4192 struct got_reference *head_ref = NULL;
4193 struct got_object_id *head_commit_id = NULL;
4194 int have_staged_files = 0;
4196 *new_commit_id = NULL;
4198 TAILQ_INIT(&commitable_paths);
4200 err = lock_worktree(worktree, LOCK_EX);
4204 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4208 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4212 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4216 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4217 &have_staged_files);
4218 if (err && err->code != GOT_ERR_CANCELLED)
4220 if (have_staged_files) {
4221 err = check_non_staged_files(fileindex, paths);
4226 cc_arg.commitable_paths = &commitable_paths;
4227 cc_arg.worktree = worktree;
4229 cc_arg.have_staged_files = have_staged_files;
4230 TAILQ_FOREACH(pe, paths, entry) {
4231 err = worktree_status(worktree, pe->path, fileindex, repo,
4232 collect_commitables, &cc_arg, NULL, NULL);
4237 if (TAILQ_EMPTY(&commitable_paths)) {
4238 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4242 TAILQ_FOREACH(pe, paths, entry) {
4243 err = check_path_is_commitable(pe->path, &commitable_paths);
4248 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4249 struct got_commitable *ct = pe->data;
4250 const char *ct_path = ct->in_repo_path;
4252 while (ct_path[0] == '/')
4254 err = check_out_of_date(ct_path, ct->status,
4255 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4256 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4262 err = commit_worktree(new_commit_id, &commitable_paths,
4263 head_commit_id, worktree, author, committer,
4264 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4268 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4270 sync_err = sync_fileindex(fileindex, fileindex_path);
4271 if (sync_err && err == NULL)
4275 got_fileindex_free(fileindex);
4276 free(fileindex_path);
4277 unlockerr = lock_worktree(worktree, LOCK_SH);
4278 if (unlockerr && err == NULL)
4280 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4281 struct got_commitable *ct = pe->data;
4282 free_commitable(ct);
4284 got_pathlist_free(&commitable_paths);
4289 got_commitable_get_path(struct got_commitable *ct)
4295 got_commitable_get_status(struct got_commitable *ct)
4300 struct check_rebase_ok_arg {
4301 struct got_worktree *worktree;
4302 struct got_repository *repo;
4305 static const struct got_error *
4306 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4308 const struct got_error *err = NULL;
4309 struct check_rebase_ok_arg *a = arg;
4310 unsigned char status;
4314 /* Reject rebase of a work tree with mixed base commits. */
4315 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4316 SHA1_DIGEST_LENGTH))
4317 return got_error(GOT_ERR_MIXED_COMMITS);
4319 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4321 return got_error_from_errno("asprintf");
4323 /* Reject rebase of a work tree with modified or staged files. */
4324 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4329 if (status != GOT_STATUS_NO_CHANGE)
4330 return got_error(GOT_ERR_MODIFIED);
4331 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4332 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4337 const struct got_error *
4338 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4339 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4340 struct got_worktree *worktree, struct got_reference *branch,
4341 struct got_repository *repo)
4343 const struct got_error *err = NULL;
4344 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4345 char *branch_ref_name = NULL;
4346 char *fileindex_path = NULL;
4347 struct check_rebase_ok_arg ok_arg;
4348 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4350 *new_base_branch_ref = NULL;
4354 err = lock_worktree(worktree, LOCK_EX);
4358 err = open_fileindex(fileindex, &fileindex_path, worktree);
4362 ok_arg.worktree = worktree;
4364 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4369 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4373 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4377 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4381 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4386 err = got_ref_alloc_symref(new_base_branch_ref,
4387 new_base_branch_ref_name, wt_branch);
4390 err = got_ref_write(*new_base_branch_ref, repo);
4394 /* TODO Lock original branch's ref while rebasing? */
4396 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4400 err = got_ref_write(branch_ref, repo);
4404 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4405 worktree->base_commit_id);
4408 err = got_ref_write(*tmp_branch, repo);
4412 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4416 free(fileindex_path);
4417 free(tmp_branch_name);
4418 free(new_base_branch_ref_name);
4419 free(branch_ref_name);
4421 got_ref_close(branch_ref);
4423 got_ref_close(wt_branch);
4425 if (*new_base_branch_ref) {
4426 got_ref_close(*new_base_branch_ref);
4427 *new_base_branch_ref = NULL;
4430 got_ref_close(*tmp_branch);
4434 got_fileindex_free(*fileindex);
4437 lock_worktree(worktree, LOCK_SH);
4442 const struct got_error *
4443 got_worktree_rebase_continue(struct got_object_id **commit_id,
4444 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4445 struct got_reference **branch, struct got_fileindex **fileindex,
4446 struct got_worktree *worktree, struct got_repository *repo)
4448 const struct got_error *err;
4449 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4450 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4451 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4452 char *fileindex_path = NULL;
4453 int have_staged_files = 0;
4456 *new_base_branch = NULL;
4461 err = lock_worktree(worktree, LOCK_EX);
4465 err = open_fileindex(fileindex, &fileindex_path, worktree);
4469 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4470 &have_staged_files);
4471 if (err && err->code != GOT_ERR_CANCELLED)
4473 if (have_staged_files) {
4474 err = got_error(GOT_ERR_STAGED_PATHS);
4478 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4482 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4486 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4490 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4494 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4498 err = got_ref_open(branch, repo,
4499 got_ref_get_symref_target(branch_ref), 0);
4503 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4507 err = got_ref_resolve(commit_id, repo, commit_ref);
4511 err = got_ref_open(new_base_branch, repo,
4512 new_base_branch_ref_name, 0);
4516 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4520 free(commit_ref_name);
4521 free(branch_ref_name);
4522 free(fileindex_path);
4524 got_ref_close(commit_ref);
4526 got_ref_close(branch_ref);
4531 got_ref_close(*tmp_branch);
4534 if (*new_base_branch) {
4535 got_ref_close(*new_base_branch);
4536 *new_base_branch = NULL;
4539 got_ref_close(*branch);
4543 got_fileindex_free(*fileindex);
4546 lock_worktree(worktree, LOCK_SH);
4551 const struct got_error *
4552 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4554 const struct got_error *err;
4555 char *tmp_branch_name = NULL;
4557 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4561 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4562 free(tmp_branch_name);
4566 static const struct got_error *
4567 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4568 char **logmsg, void *arg)
4574 static const struct got_error *
4575 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4576 const char *path, struct got_object_id *blob_id,
4577 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4582 struct collect_merged_paths_arg {
4583 got_worktree_checkout_cb progress_cb;
4585 struct got_pathlist_head *merged_paths;
4588 static const struct got_error *
4589 collect_merged_paths(void *arg, unsigned char status, const char *path)
4591 const struct got_error *err;
4592 struct collect_merged_paths_arg *a = arg;
4594 struct got_pathlist_entry *new;
4596 err = (*a->progress_cb)(a->progress_arg, status, path);
4600 if (status != GOT_STATUS_MERGE &&
4601 status != GOT_STATUS_ADD &&
4602 status != GOT_STATUS_DELETE &&
4603 status != GOT_STATUS_CONFLICT)
4608 return got_error_from_errno("strdup");
4610 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4611 if (err || new == NULL)
4617 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4619 struct got_pathlist_entry *pe;
4621 TAILQ_FOREACH(pe, merged_paths, entry)
4622 free((char *)pe->path);
4624 got_pathlist_free(merged_paths);
4627 static const struct got_error *
4628 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4629 struct got_repository *repo)
4631 const struct got_error *err;
4632 struct got_reference *commit_ref = NULL;
4634 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4636 if (err->code != GOT_ERR_NOT_REF)
4638 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4641 err = got_ref_write(commit_ref, repo);
4645 struct got_object_id *stored_id;
4648 err = got_ref_resolve(&stored_id, repo, commit_ref);
4651 cmp = got_object_id_cmp(commit_id, stored_id);
4654 err = got_error(GOT_ERR_REBASE_COMMITID);
4660 got_ref_close(commit_ref);
4664 static const struct got_error *
4665 rebase_merge_files(struct got_pathlist_head *merged_paths,
4666 const char *commit_ref_name, struct got_worktree *worktree,
4667 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4668 struct got_object_id *commit_id, struct got_repository *repo,
4669 got_worktree_checkout_cb progress_cb, void *progress_arg,
4670 got_cancel_cb cancel_cb, void *cancel_arg)
4672 const struct got_error *err;
4673 struct got_reference *commit_ref = NULL;
4674 struct collect_merged_paths_arg cmp_arg;
4675 char *fileindex_path;
4677 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4679 err = get_fileindex_path(&fileindex_path, worktree);
4683 cmp_arg.progress_cb = progress_cb;
4684 cmp_arg.progress_arg = progress_arg;
4685 cmp_arg.merged_paths = merged_paths;
4686 err = merge_files(worktree, fileindex, fileindex_path,
4687 parent_commit_id, commit_id, repo, collect_merged_paths,
4688 &cmp_arg, cancel_cb, cancel_arg);
4690 got_ref_close(commit_ref);
4694 const struct got_error *
4695 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4696 struct got_worktree *worktree, struct got_fileindex *fileindex,
4697 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4698 struct got_repository *repo,
4699 got_worktree_checkout_cb progress_cb, void *progress_arg,
4700 got_cancel_cb cancel_cb, void *cancel_arg)
4702 const struct got_error *err;
4703 char *commit_ref_name;
4705 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4709 err = store_commit_id(commit_ref_name, commit_id, repo);
4713 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4714 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4715 progress_arg, cancel_cb, cancel_arg);
4717 free(commit_ref_name);
4721 const struct got_error *
4722 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4723 struct got_worktree *worktree, struct got_fileindex *fileindex,
4724 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4725 struct got_repository *repo,
4726 got_worktree_checkout_cb progress_cb, void *progress_arg,
4727 got_cancel_cb cancel_cb, void *cancel_arg)
4729 const struct got_error *err;
4730 char *commit_ref_name;
4732 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4736 err = store_commit_id(commit_ref_name, commit_id, repo);
4740 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4741 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4742 progress_arg, cancel_cb, cancel_arg);
4744 free(commit_ref_name);
4748 static const struct got_error *
4749 rebase_commit(struct got_object_id **new_commit_id,
4750 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4751 struct got_worktree *worktree, struct got_fileindex *fileindex,
4752 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4753 const char *new_logmsg, struct got_repository *repo)
4755 const struct got_error *err, *sync_err;
4756 struct got_pathlist_head commitable_paths;
4757 struct collect_commitables_arg cc_arg;
4758 char *fileindex_path = NULL;
4759 struct got_reference *head_ref = NULL;
4760 struct got_object_id *head_commit_id = NULL;
4761 char *logmsg = NULL;
4763 TAILQ_INIT(&commitable_paths);
4764 *new_commit_id = NULL;
4766 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4768 err = get_fileindex_path(&fileindex_path, worktree);
4772 cc_arg.commitable_paths = &commitable_paths;
4773 cc_arg.worktree = worktree;
4775 cc_arg.have_staged_files = 0;
4777 * If possible get the status of individual files directly to
4778 * avoid crawling the entire work tree once per rebased commit.
4779 * TODO: Ideally, merged_paths would contain a list of commitables
4780 * we could use so we could skip worktree_status() entirely.
4783 struct got_pathlist_entry *pe;
4784 if (TAILQ_EMPTY(merged_paths)) {
4785 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4788 TAILQ_FOREACH(pe, merged_paths, entry) {
4789 err = worktree_status(worktree, pe->path, fileindex,
4790 repo, collect_commitables, &cc_arg, NULL, NULL);
4795 err = worktree_status(worktree, "", fileindex, repo,
4796 collect_commitables, &cc_arg, NULL, NULL);
4801 if (TAILQ_EMPTY(&commitable_paths)) {
4802 /* No-op change; commit will be elided. */
4803 err = got_ref_delete(commit_ref, repo);
4806 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4810 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4814 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4819 logmsg = strdup(new_logmsg);
4820 if (logmsg == NULL) {
4821 err = got_error_from_errno("strdup");
4825 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
4830 /* NB: commit_worktree will call free(logmsg) */
4831 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4832 worktree, got_object_commit_get_author(orig_commit),
4833 got_object_commit_get_committer(orig_commit),
4834 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4838 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4842 err = got_ref_delete(commit_ref, repo);
4846 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4848 sync_err = sync_fileindex(fileindex, fileindex_path);
4849 if (sync_err && err == NULL)
4852 free(fileindex_path);
4853 free(head_commit_id);
4855 got_ref_close(head_ref);
4857 free(*new_commit_id);
4858 *new_commit_id = NULL;
4863 const struct got_error *
4864 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4865 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4866 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4867 struct got_commit_object *orig_commit,
4868 struct got_object_id *orig_commit_id, struct got_repository *repo)
4870 const struct got_error *err;
4871 char *commit_ref_name;
4872 struct got_reference *commit_ref = NULL;
4873 struct got_object_id *commit_id = NULL;
4875 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4879 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4882 err = got_ref_resolve(&commit_id, repo, commit_ref);
4885 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4886 err = got_error(GOT_ERR_REBASE_COMMITID);
4890 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4891 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4894 got_ref_close(commit_ref);
4895 free(commit_ref_name);
4900 const struct got_error *
4901 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4902 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4903 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4904 struct got_commit_object *orig_commit,
4905 struct got_object_id *orig_commit_id, const char *new_logmsg,
4906 struct got_repository *repo)
4908 const struct got_error *err;
4909 char *commit_ref_name;
4910 struct got_reference *commit_ref = NULL;
4911 struct got_object_id *commit_id = NULL;
4913 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4917 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4920 err = got_ref_resolve(&commit_id, repo, commit_ref);
4923 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4924 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4928 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4929 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4932 got_ref_close(commit_ref);
4933 free(commit_ref_name);
4938 const struct got_error *
4939 got_worktree_rebase_postpone(struct got_worktree *worktree,
4940 struct got_fileindex *fileindex)
4943 got_fileindex_free(fileindex);
4944 return lock_worktree(worktree, LOCK_SH);
4947 static const struct got_error *
4948 delete_ref(const char *name, struct got_repository *repo)
4950 const struct got_error *err;
4951 struct got_reference *ref;
4953 err = got_ref_open(&ref, repo, name, 0);
4955 if (err->code == GOT_ERR_NOT_REF)
4960 err = got_ref_delete(ref, repo);
4965 static const struct got_error *
4966 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4968 const struct got_error *err;
4969 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4970 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4972 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4975 err = delete_ref(tmp_branch_name, repo);
4979 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4982 err = delete_ref(new_base_branch_ref_name, repo);
4986 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4989 err = delete_ref(branch_ref_name, repo);
4993 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4996 err = delete_ref(commit_ref_name, repo);
5001 free(tmp_branch_name);
5002 free(new_base_branch_ref_name);
5003 free(branch_ref_name);
5004 free(commit_ref_name);
5008 const struct got_error *
5009 got_worktree_rebase_complete(struct got_worktree *worktree,
5010 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5011 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5012 struct got_repository *repo)
5014 const struct got_error *err, *unlockerr;
5015 struct got_object_id *new_head_commit_id = NULL;
5017 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5021 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5025 err = got_ref_write(rebased_branch, repo);
5029 err = got_worktree_set_head_ref(worktree, rebased_branch);
5033 err = delete_rebase_refs(worktree, repo);
5036 got_fileindex_free(fileindex);
5037 free(new_head_commit_id);
5038 unlockerr = lock_worktree(worktree, LOCK_SH);
5039 if (unlockerr && err == NULL)
5044 const struct got_error *
5045 got_worktree_rebase_abort(struct got_worktree *worktree,
5046 struct got_fileindex *fileindex, struct got_repository *repo,
5047 struct got_reference *new_base_branch,
5048 got_worktree_checkout_cb progress_cb, void *progress_arg)
5050 const struct got_error *err, *unlockerr, *sync_err;
5051 struct got_reference *resolved = NULL;
5052 struct got_object_id *commit_id = NULL;
5053 char *fileindex_path = NULL;
5054 struct revert_file_args rfa;
5055 struct got_object_id *tree_id = NULL;
5057 err = lock_worktree(worktree, LOCK_EX);
5061 err = got_ref_open(&resolved, repo,
5062 got_ref_get_symref_target(new_base_branch), 0);
5066 err = got_worktree_set_head_ref(worktree, resolved);
5071 * XXX commits to the base branch could have happened while
5072 * we were busy rebasing; should we store the original commit ID
5073 * when rebase begins and read it back here?
5075 err = got_ref_resolve(&commit_id, repo, resolved);
5079 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5083 err = got_object_id_by_path(&tree_id, repo,
5084 worktree->base_commit_id, worktree->path_prefix);
5088 err = delete_rebase_refs(worktree, repo);
5092 err = get_fileindex_path(&fileindex_path, worktree);
5096 rfa.worktree = worktree;
5097 rfa.fileindex = fileindex;
5098 rfa.progress_cb = progress_cb;
5099 rfa.progress_arg = progress_arg;
5100 rfa.patch_cb = NULL;
5101 rfa.patch_arg = NULL;
5103 err = worktree_status(worktree, "", fileindex, repo,
5104 revert_file, &rfa, NULL, NULL);
5108 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5109 repo, progress_cb, progress_arg, NULL, NULL);
5111 sync_err = sync_fileindex(fileindex, fileindex_path);
5112 if (sync_err && err == NULL)
5115 got_ref_close(resolved);
5119 got_fileindex_free(fileindex);
5120 free(fileindex_path);
5122 unlockerr = lock_worktree(worktree, LOCK_SH);
5123 if (unlockerr && err == NULL)
5128 const struct got_error *
5129 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5130 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5131 struct got_fileindex **fileindex, struct got_worktree *worktree,
5132 struct got_repository *repo)
5134 const struct got_error *err = NULL;
5135 char *tmp_branch_name = NULL;
5136 char *branch_ref_name = NULL;
5137 char *base_commit_ref_name = NULL;
5138 char *fileindex_path = NULL;
5139 struct check_rebase_ok_arg ok_arg;
5140 struct got_reference *wt_branch = NULL;
5141 struct got_reference *base_commit_ref = NULL;
5145 *base_commit_id = NULL;
5148 err = lock_worktree(worktree, LOCK_EX);
5152 err = open_fileindex(fileindex, &fileindex_path, worktree);
5156 ok_arg.worktree = worktree;
5158 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5163 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5167 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5171 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5176 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5181 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5185 err = got_ref_write(*branch_ref, repo);
5189 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5190 worktree->base_commit_id);
5193 err = got_ref_write(base_commit_ref, repo);
5196 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5197 if (*base_commit_id == NULL) {
5198 err = got_error_from_errno("got_object_id_dup");
5202 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5203 worktree->base_commit_id);
5206 err = got_ref_write(*tmp_branch, repo);
5210 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5214 free(fileindex_path);
5215 free(tmp_branch_name);
5216 free(branch_ref_name);
5217 free(base_commit_ref_name);
5219 got_ref_close(wt_branch);
5222 got_ref_close(*branch_ref);
5226 got_ref_close(*tmp_branch);
5229 free(*base_commit_id);
5231 got_fileindex_free(*fileindex);
5234 lock_worktree(worktree, LOCK_SH);
5239 const struct got_error *
5240 got_worktree_histedit_postpone(struct got_worktree *worktree,
5241 struct got_fileindex *fileindex)
5244 got_fileindex_free(fileindex);
5245 return lock_worktree(worktree, LOCK_SH);
5248 const struct got_error *
5249 got_worktree_histedit_in_progress(int *in_progress,
5250 struct got_worktree *worktree)
5252 const struct got_error *err;
5253 char *tmp_branch_name = NULL;
5255 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5259 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5260 free(tmp_branch_name);
5264 const struct got_error *
5265 got_worktree_histedit_continue(struct got_object_id **commit_id,
5266 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5267 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5268 struct got_worktree *worktree, struct got_repository *repo)
5270 const struct got_error *err;
5271 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5272 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5273 struct got_reference *commit_ref = NULL;
5274 struct got_reference *base_commit_ref = NULL;
5275 char *fileindex_path = NULL;
5276 int have_staged_files = 0;
5280 *base_commit_id = NULL;
5283 err = lock_worktree(worktree, LOCK_EX);
5287 err = open_fileindex(fileindex, &fileindex_path, worktree);
5291 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5292 &have_staged_files);
5293 if (err && err->code != GOT_ERR_CANCELLED)
5295 if (have_staged_files) {
5296 err = got_error(GOT_ERR_STAGED_PATHS);
5300 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5304 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5308 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5312 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5317 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5321 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5324 err = got_ref_resolve(commit_id, repo, commit_ref);
5328 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5331 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5335 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5339 free(commit_ref_name);
5340 free(branch_ref_name);
5341 free(fileindex_path);
5343 got_ref_close(commit_ref);
5344 if (base_commit_ref)
5345 got_ref_close(base_commit_ref);
5349 free(*base_commit_id);
5350 *base_commit_id = NULL;
5352 got_ref_close(*tmp_branch);
5356 got_fileindex_free(*fileindex);
5359 lock_worktree(worktree, LOCK_EX);
5364 static const struct got_error *
5365 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5367 const struct got_error *err;
5368 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5369 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5371 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5374 err = delete_ref(tmp_branch_name, repo);
5378 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5382 err = delete_ref(base_commit_ref_name, repo);
5386 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5389 err = delete_ref(branch_ref_name, repo);
5393 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5396 err = delete_ref(commit_ref_name, repo);
5400 free(tmp_branch_name);
5401 free(base_commit_ref_name);
5402 free(branch_ref_name);
5403 free(commit_ref_name);
5407 const struct got_error *
5408 got_worktree_histedit_abort(struct got_worktree *worktree,
5409 struct got_fileindex *fileindex, struct got_repository *repo,
5410 struct got_reference *branch, struct got_object_id *base_commit_id,
5411 got_worktree_checkout_cb progress_cb, void *progress_arg)
5413 const struct got_error *err, *unlockerr, *sync_err;
5414 struct got_reference *resolved = NULL;
5415 char *fileindex_path = NULL;
5416 struct got_object_id *tree_id = NULL;
5417 struct revert_file_args rfa;
5419 err = lock_worktree(worktree, LOCK_EX);
5423 err = got_ref_open(&resolved, repo,
5424 got_ref_get_symref_target(branch), 0);
5428 err = got_worktree_set_head_ref(worktree, resolved);
5432 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5436 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5437 worktree->path_prefix);
5441 err = delete_histedit_refs(worktree, repo);
5445 err = get_fileindex_path(&fileindex_path, worktree);
5449 rfa.worktree = worktree;
5450 rfa.fileindex = fileindex;
5451 rfa.progress_cb = progress_cb;
5452 rfa.progress_arg = progress_arg;
5453 rfa.patch_cb = NULL;
5454 rfa.patch_arg = NULL;
5456 err = worktree_status(worktree, "", fileindex, repo,
5457 revert_file, &rfa, NULL, NULL);
5461 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5462 repo, progress_cb, progress_arg, NULL, NULL);
5464 sync_err = sync_fileindex(fileindex, fileindex_path);
5465 if (sync_err && err == NULL)
5468 got_ref_close(resolved);
5470 free(fileindex_path);
5472 unlockerr = lock_worktree(worktree, LOCK_SH);
5473 if (unlockerr && err == NULL)
5478 const struct got_error *
5479 got_worktree_histedit_complete(struct got_worktree *worktree,
5480 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5481 struct got_reference *edited_branch, struct got_repository *repo)
5483 const struct got_error *err, *unlockerr;
5484 struct got_object_id *new_head_commit_id = NULL;
5485 struct got_reference *resolved = NULL;
5487 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5491 err = got_ref_open(&resolved, repo,
5492 got_ref_get_symref_target(edited_branch), 0);
5496 err = got_ref_change_ref(resolved, new_head_commit_id);
5500 err = got_ref_write(resolved, repo);
5504 err = got_worktree_set_head_ref(worktree, resolved);
5508 err = delete_histedit_refs(worktree, repo);
5511 got_fileindex_free(fileindex);
5512 free(new_head_commit_id);
5513 unlockerr = lock_worktree(worktree, LOCK_SH);
5514 if (unlockerr && err == NULL)
5519 const struct got_error *
5520 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5521 struct got_object_id *commit_id, struct got_repository *repo)
5523 const struct got_error *err;
5524 char *commit_ref_name;
5526 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5530 err = store_commit_id(commit_ref_name, commit_id, repo);
5534 err = delete_ref(commit_ref_name, repo);
5536 free(commit_ref_name);
5540 struct check_stage_ok_arg {
5541 struct got_object_id *head_commit_id;
5542 struct got_worktree *worktree;
5543 struct got_fileindex *fileindex;
5544 struct got_repository *repo;
5548 const struct got_error *
5549 check_stage_ok(void *arg, unsigned char status,
5550 unsigned char staged_status, const char *relpath,
5551 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5552 struct got_object_id *commit_id)
5554 struct check_stage_ok_arg *a = arg;
5555 const struct got_error *err = NULL;
5556 struct got_fileindex_entry *ie;
5557 struct got_object_id base_commit_id;
5558 struct got_object_id *base_commit_idp = NULL;
5559 char *in_repo_path = NULL, *p;
5561 if (status == GOT_STATUS_UNVERSIONED)
5564 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5566 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5568 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5569 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5571 return got_error_from_errno("asprintf");
5573 if (got_fileindex_entry_has_commit(ie)) {
5574 memcpy(base_commit_id.sha1, ie->commit_sha1,
5575 SHA1_DIGEST_LENGTH);
5576 base_commit_idp = &base_commit_id;
5579 if (status == GOT_STATUS_NO_CHANGE) {
5580 err = got_error_path(ie->path, GOT_ERR_STAGE_NO_CHANGE);
5582 } else if (status == GOT_STATUS_CONFLICT) {
5583 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5585 } else if (status != GOT_STATUS_ADD &&
5586 status != GOT_STATUS_MODIFY &&
5587 status != GOT_STATUS_DELETE) {
5588 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5592 a->have_changes = 1;
5597 err = check_out_of_date(p, status, staged_status,
5598 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5599 GOT_ERR_STAGE_OUT_OF_DATE);
5605 struct stage_path_arg {
5606 struct got_worktree *worktree;
5607 struct got_fileindex *fileindex;
5608 struct got_repository *repo;
5609 got_worktree_status_cb status_cb;
5611 got_worktree_patch_cb patch_cb;
5615 static const struct got_error *
5616 stage_path(void *arg, unsigned char status,
5617 unsigned char staged_status, const char *relpath,
5618 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5619 struct got_object_id *commit_id)
5621 struct stage_path_arg *a = arg;
5622 const struct got_error *err = NULL;
5623 struct got_fileindex_entry *ie;
5624 char *ondisk_path = NULL, *path_content = NULL;
5626 struct got_object_id *new_staged_blob_id = NULL;
5628 if (status == GOT_STATUS_UNVERSIONED)
5631 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5633 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5635 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5637 return got_error_from_errno("asprintf");
5640 case GOT_STATUS_ADD:
5641 case GOT_STATUS_MODIFY:
5643 if (status == GOT_STATUS_ADD) {
5644 int choice = GOT_PATCH_CHOICE_NONE;
5645 err = (*a->patch_cb)(&choice, a->patch_arg,
5646 status, ie->path, NULL, 1, 1);
5649 if (choice != GOT_PATCH_CHOICE_YES)
5652 err = create_patched_content(&path_content, 0,
5653 staged_blob_id ? staged_blob_id : blob_id,
5654 ondisk_path, ie->path, a->repo,
5655 a->patch_cb, a->patch_arg);
5656 if (err || path_content == NULL)
5660 err = got_object_blob_create(&new_staged_blob_id,
5661 path_content ? path_content : ondisk_path, a->repo);
5664 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
5665 SHA1_DIGEST_LENGTH);
5666 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5667 stage = GOT_FILEIDX_STAGE_ADD;
5669 stage = GOT_FILEIDX_STAGE_MODIFY;
5670 got_fileindex_entry_stage_set(ie, stage);
5671 if (a->status_cb == NULL)
5673 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5674 get_staged_status(ie), relpath, blob_id,
5675 new_staged_blob_id, NULL);
5677 case GOT_STATUS_DELETE:
5678 if (staged_status == GOT_STATUS_DELETE)
5681 int choice = GOT_PATCH_CHOICE_NONE;
5682 err = (*a->patch_cb)(&choice, a->patch_arg, status,
5683 ie->path, NULL, 1, 1);
5686 if (choice == GOT_PATCH_CHOICE_NO)
5688 if (choice != GOT_PATCH_CHOICE_YES) {
5689 err = got_error(GOT_ERR_PATCH_CHOICE);
5693 stage = GOT_FILEIDX_STAGE_DELETE;
5694 got_fileindex_entry_stage_set(ie, stage);
5695 if (a->status_cb == NULL)
5697 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
5698 get_staged_status(ie), relpath, NULL, NULL, NULL);
5700 case GOT_STATUS_NO_CHANGE:
5701 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5703 case GOT_STATUS_CONFLICT:
5704 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5707 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5711 if (path_content && unlink(path_content) == -1 && err == NULL)
5712 err = got_error_from_errno2("unlink", path_content);
5715 free(new_staged_blob_id);
5719 const struct got_error *
5720 got_worktree_stage(struct got_worktree *worktree,
5721 struct got_pathlist_head *paths,
5722 got_worktree_status_cb status_cb, void *status_arg,
5723 got_worktree_patch_cb patch_cb, void *patch_arg,
5724 struct got_repository *repo)
5726 const struct got_error *err = NULL, *sync_err, *unlockerr;
5727 struct got_pathlist_entry *pe;
5728 struct got_fileindex *fileindex = NULL;
5729 char *fileindex_path = NULL;
5730 struct got_reference *head_ref = NULL;
5731 struct got_object_id *head_commit_id = NULL;
5732 struct check_stage_ok_arg oka;
5733 struct stage_path_arg spa;
5735 err = lock_worktree(worktree, LOCK_EX);
5739 err = got_ref_open(&head_ref, repo,
5740 got_worktree_get_head_ref_name(worktree), 0);
5743 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5746 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5750 /* Check pre-conditions before staging anything. */
5751 oka.head_commit_id = head_commit_id;
5752 oka.worktree = worktree;
5753 oka.fileindex = fileindex;
5755 oka.have_changes = 0;
5756 TAILQ_FOREACH(pe, paths, entry) {
5757 err = worktree_status(worktree, pe->path, fileindex, repo,
5758 check_stage_ok, &oka, NULL, NULL);
5762 if (!oka.have_changes) {
5763 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
5767 spa.worktree = worktree;
5768 spa.fileindex = fileindex;
5770 spa.patch_cb = patch_cb;
5771 spa.patch_arg = patch_arg;
5772 spa.status_cb = status_cb;
5773 spa.status_arg = status_arg;
5774 TAILQ_FOREACH(pe, paths, entry) {
5775 err = worktree_status(worktree, pe->path, fileindex, repo,
5776 stage_path, &spa, NULL, NULL);
5781 sync_err = sync_fileindex(fileindex, fileindex_path);
5782 if (sync_err && err == NULL)
5786 got_ref_close(head_ref);
5787 free(head_commit_id);
5788 free(fileindex_path);
5790 got_fileindex_free(fileindex);
5791 unlockerr = lock_worktree(worktree, LOCK_SH);
5792 if (unlockerr && err == NULL)
5797 struct unstage_path_arg {
5798 struct got_worktree *worktree;
5799 struct got_fileindex *fileindex;
5800 struct got_repository *repo;
5801 got_worktree_checkout_cb progress_cb;
5803 got_worktree_patch_cb patch_cb;
5807 static const struct got_error *
5808 create_unstaged_content(char **path_unstaged_content,
5809 char **path_new_staged_content, struct got_object_id *blob_id,
5810 struct got_object_id *staged_blob_id, const char *relpath,
5811 struct got_repository *repo,
5812 got_worktree_patch_cb patch_cb, void *patch_arg)
5814 const struct got_error *err;
5815 struct got_blob_object *blob = NULL, *staged_blob = NULL;
5816 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
5817 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
5818 struct stat sb1, sb2;
5819 struct got_diff_changes *changes = NULL;
5820 struct got_diff_state *ds = NULL;
5821 struct got_diff_args *args = NULL;
5822 struct got_diff_change *change;
5823 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
5824 int have_content = 0, have_rejected_content = 0;
5826 *path_unstaged_content = NULL;
5827 *path_new_staged_content = NULL;
5829 err = got_object_id_str(&label1, blob_id);
5832 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
5836 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
5840 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5844 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
5848 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
5852 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
5856 if (stat(path1, &sb1) == -1) {
5857 err = got_error_from_errno2("stat", path1);
5861 if (stat(path2, &sb2) == -1) {
5862 err = got_error_from_errno2("stat", path2);
5866 err = got_diff_files(&changes, &ds, &args, &diff_flags,
5867 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
5871 err = got_opentemp_named(path_unstaged_content, &outfile,
5872 "got-unstaged-content");
5875 err = got_opentemp_named(path_new_staged_content, &rejectfile,
5876 "got-new-staged-content");
5880 if (fseek(f1, 0L, SEEK_SET) == -1) {
5881 err = got_ferror(f1, GOT_ERR_IO);
5884 if (fseek(f2, 0L, SEEK_SET) == -1) {
5885 err = got_ferror(f2, GOT_ERR_IO);
5888 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
5890 err = apply_or_reject_change(&choice, change, ++n,
5891 changes->nchanges, ds, args, diff_flags, relpath,
5892 f1, f2, &line_cur1, &line_cur2,
5893 outfile, rejectfile, patch_cb, patch_arg);
5896 if (choice == GOT_PATCH_CHOICE_YES)
5899 have_rejected_content = 1;
5900 if (choice == GOT_PATCH_CHOICE_QUIT)
5903 if (have_content || have_rejected_content)
5904 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5905 outfile, rejectfile);
5909 got_object_blob_close(blob);
5911 got_object_blob_close(staged_blob);
5912 if (f1 && fclose(f1) == EOF && err == NULL)
5913 err = got_error_from_errno2("fclose", path1);
5914 if (f2 && fclose(f2) == EOF && err == NULL)
5915 err = got_error_from_errno2("fclose", path2);
5916 if (outfile && fclose(outfile) == EOF && err == NULL)
5917 err = got_error_from_errno2("fclose", *path_unstaged_content);
5918 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
5919 err = got_error_from_errno2("fclose", *path_new_staged_content);
5920 if (path1 && unlink(path1) == -1 && err == NULL)
5921 err = got_error_from_errno2("unlink", path1);
5922 if (path2 && unlink(path2) == -1 && err == NULL)
5923 err = got_error_from_errno2("unlink", path2);
5924 if (err || !have_content) {
5925 if (*path_unstaged_content &&
5926 unlink(*path_unstaged_content) == -1 && err == NULL)
5927 err = got_error_from_errno2("unlink",
5928 *path_unstaged_content);
5929 free(*path_unstaged_content);
5930 *path_unstaged_content = NULL;
5932 if (err || !have_rejected_content) {
5933 if (*path_new_staged_content &&
5934 unlink(*path_new_staged_content) == -1 && err == NULL)
5935 err = got_error_from_errno2("unlink",
5936 *path_new_staged_content);
5937 free(*path_new_staged_content);
5938 *path_new_staged_content = NULL;
5942 got_diff_state_free(ds);
5946 got_diff_free_changes(changes);
5952 static const struct got_error *
5953 unstage_path(void *arg, unsigned char status,
5954 unsigned char staged_status, const char *relpath,
5955 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5956 struct got_object_id *commit_id)
5958 const struct got_error *err = NULL;
5959 struct unstage_path_arg *a = arg;
5960 struct got_fileindex_entry *ie;
5961 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5962 char *ondisk_path = NULL, *path_unstaged_content = NULL;
5963 char *path_new_staged_content = NULL;
5964 int local_changes_subsumed;
5967 if (staged_status != GOT_STATUS_ADD &&
5968 staged_status != GOT_STATUS_MODIFY &&
5969 staged_status != GOT_STATUS_DELETE)
5972 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5974 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
5978 return got_error_from_errno("asprintf");
5980 switch (staged_status) {
5981 case GOT_STATUS_MODIFY:
5982 err = got_object_open_as_blob(&blob_base, a->repo,
5987 case GOT_STATUS_ADD:
5989 if (staged_status == GOT_STATUS_ADD) {
5990 int choice = GOT_PATCH_CHOICE_NONE;
5991 err = (*a->patch_cb)(&choice, a->patch_arg,
5992 staged_status, ie->path, NULL, 1, 1);
5995 if (choice != GOT_PATCH_CHOICE_YES)
5998 err = create_unstaged_content(
5999 &path_unstaged_content,
6000 &path_new_staged_content, blob_id,
6001 staged_blob_id, ie->path, a->repo,
6002 a->patch_cb, a->patch_arg);
6003 if (err || path_unstaged_content == NULL)
6005 if (path_new_staged_content) {
6006 err = got_object_blob_create(
6008 path_new_staged_content,
6012 memcpy(ie->staged_blob_sha1,
6013 staged_blob_id->sha1,
6014 SHA1_DIGEST_LENGTH);
6016 err = merge_file(&local_changes_subsumed,
6017 a->worktree, blob_base, ondisk_path,
6018 relpath, got_fileindex_perms_to_st(ie),
6019 path_unstaged_content, "unstaged",
6020 a->repo, a->progress_cb, a->progress_arg);
6022 path_new_staged_content == NULL)
6023 got_fileindex_entry_stage_set(ie,
6024 GOT_FILEIDX_STAGE_NONE);
6025 break; /* Done with this file. */
6028 err = got_object_open_as_blob(&blob_staged, a->repo,
6029 staged_blob_id, 8192);
6032 err = merge_blob(&local_changes_subsumed, a->worktree,
6033 blob_base, ondisk_path, relpath,
6034 got_fileindex_perms_to_st(ie), blob_staged,
6035 commit_id ? commit_id : a->worktree->base_commit_id,
6036 a->repo, a->progress_cb, a->progress_arg);
6038 got_fileindex_entry_stage_set(ie,
6039 GOT_FILEIDX_STAGE_NONE);
6041 case GOT_STATUS_DELETE:
6043 int choice = GOT_PATCH_CHOICE_NONE;
6044 err = (*a->patch_cb)(&choice, a->patch_arg,
6045 staged_status, ie->path, NULL, 1, 1);
6048 if (choice == GOT_PATCH_CHOICE_NO)
6050 if (choice != GOT_PATCH_CHOICE_YES) {
6051 err = got_error(GOT_ERR_PATCH_CHOICE);
6055 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6056 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6059 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6064 if (path_unstaged_content &&
6065 unlink(path_unstaged_content) == -1 && err == NULL)
6066 err = got_error_from_errno2("unlink", path_unstaged_content);
6067 if (path_new_staged_content &&
6068 unlink(path_new_staged_content) == -1 && err == NULL)
6069 err = got_error_from_errno2("unlink", path_new_staged_content);
6070 free(path_unstaged_content);
6071 free(path_new_staged_content);
6073 got_object_blob_close(blob_base);
6075 got_object_blob_close(blob_staged);
6079 const struct got_error *
6080 got_worktree_unstage(struct got_worktree *worktree,
6081 struct got_pathlist_head *paths,
6082 got_worktree_checkout_cb progress_cb, void *progress_arg,
6083 got_worktree_patch_cb patch_cb, void *patch_arg,
6084 struct got_repository *repo)
6086 const struct got_error *err = NULL, *sync_err, *unlockerr;
6087 struct got_pathlist_entry *pe;
6088 struct got_fileindex *fileindex = NULL;
6089 char *fileindex_path = NULL;
6090 struct unstage_path_arg upa;
6092 err = lock_worktree(worktree, LOCK_EX);
6096 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6100 upa.worktree = worktree;
6101 upa.fileindex = fileindex;
6103 upa.progress_cb = progress_cb;
6104 upa.progress_arg = progress_arg;
6105 upa.patch_cb = patch_cb;
6106 upa.patch_arg = patch_arg;
6107 TAILQ_FOREACH(pe, paths, entry) {
6108 err = worktree_status(worktree, pe->path, fileindex, repo,
6109 unstage_path, &upa, NULL, NULL);
6114 sync_err = sync_fileindex(fileindex, fileindex_path);
6115 if (sync_err && err == NULL)
6118 free(fileindex_path);
6120 got_fileindex_free(fileindex);
6121 unlockerr = lock_worktree(worktree, LOCK_SH);
6122 if (unlockerr && err == NULL)