2 * Copyright (c) 2018, 2019, 2020 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>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
69 const struct got_error *err = NULL;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
83 const struct got_error *err = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 err = got_error_from_errno2("open", path);
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
170 (*content)[sb.st_size - 1] = '\0';
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
192 return got_error_from_errno("got_ref_to_str");
194 refstr = strdup(got_ref_get_name(head_ref));
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
210 uint32_t uuid_status;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
223 err = got_ref_resolve(&commit_id, repo, head_ref);
226 err = got_object_get_type(&obj_type, repo, commit_id);
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
421 got_repo_close(repo);
424 free(base_commit_id_str);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
434 (*worktree)->lockfd = fd;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
450 path = dirname(path);
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
535 err = write_head_ref(path_got, head_ref);
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
570 err = got_object_open(&obj, repo, commit_id);
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
595 got_object_close(obj);
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
641 size_t flen1 = 0, flen2 = 0;
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
660 } else if (flen2 == 0) {
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
684 FILE *f1 = NULL, *f2 = NULL;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
700 if (size1 != size2) {
705 f1 = fopen(f1_path, "r");
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
711 err = got_error_from_errno2("fopen", f2_path);
715 err = check_file_contents_equal(same, f1, f2);
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
834 * Perform a 3-way merge where blob_orig acts as the common ancestor,
835 * blob_deriv acts as the first derived version, and the file on disk
836 * acts as the second derived version.
838 static const struct got_error *
839 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
840 struct got_blob_object *blob_orig, const char *ondisk_path,
841 const char *path, uint16_t st_mode, const char *label_orig,
842 struct got_blob_object *blob_deriv,
843 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
844 got_worktree_checkout_cb progress_cb, void *progress_arg)
846 const struct got_error *err = NULL;
847 FILE *f_deriv = NULL;
848 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
849 char *label_deriv = NULL, *parent;
851 *local_changes_subsumed = 0;
853 parent = dirname(ondisk_path);
855 return got_error_from_errno2("dirname", ondisk_path);
858 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
859 err = got_error_from_errno("asprintf");
864 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
867 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
872 err = got_object_id_str(&id_str, deriv_base_commit_id);
875 if (asprintf(&label_deriv, "%s: commit %s",
876 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
877 err = got_error_from_errno("asprintf");
881 err = merge_file(local_changes_subsumed, worktree, blob_orig,
882 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
883 label_deriv, repo, progress_cb, progress_arg);
885 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
886 err = got_error_from_errno("fclose");
888 if (blob_deriv_path) {
889 unlink(blob_deriv_path);
890 free(blob_deriv_path);
897 static const struct got_error *
898 create_fileindex_entry(struct got_fileindex *fileindex,
899 struct got_object_id *base_commit_id, const char *ondisk_path,
900 const char *path, struct got_object_id *blob_id)
902 const struct got_error *err = NULL;
903 struct got_fileindex_entry *new_ie;
905 err = got_fileindex_entry_alloc(&new_ie, path);
909 err = got_fileindex_entry_update(new_ie, ondisk_path,
910 blob_id->sha1, base_commit_id->sha1, 1);
914 err = got_fileindex_entry_add(fileindex, new_ie);
917 got_fileindex_entry_free(new_ie);
922 get_ondisk_perms(int executable, mode_t st_mode)
924 mode_t xbits = S_IXUSR;
927 /* Map read bits to execute bits. */
928 if (st_mode & S_IRGRP)
930 if (st_mode & S_IROTH)
932 return st_mode | xbits;
935 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
938 /* forward declaration */
939 static const struct got_error *
940 install_blob(struct got_worktree *worktree, const char *ondisk_path,
941 const char *path, mode_t te_mode, mode_t st_mode,
942 struct got_blob_object *blob, int restoring_missing_file,
943 int reverting_versioned_file, struct got_repository *repo,
944 got_worktree_checkout_cb progress_cb, void *progress_arg);
946 static const struct got_error *
947 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
948 const char *path, mode_t te_mode, mode_t st_mode,
949 struct got_blob_object *blob, int restoring_missing_file,
950 int reverting_versioned_file, struct got_repository *repo,
951 got_worktree_checkout_cb progress_cb, void *progress_arg)
953 const struct got_error *err = NULL;
954 char target_path[PATH_MAX];
955 size_t len, target_len = 0;
956 char *resolved_path = NULL, *abspath = NULL;
957 const uint8_t *buf = got_object_blob_get_read_buf(blob);
958 size_t hdrlen = got_object_blob_get_hdrlen(blob);
961 * Blob object content specifies the target path of the link.
962 * If a symbolic link cannot be installed we instead create
963 * a regular file which contains the link target path stored
964 * in the blob object.
967 err = got_object_blob_read_block(&len, blob);
968 if (len + target_len >= sizeof(target_path)) {
969 /* Path too long; install as a regular file. */
970 got_object_blob_rewind(blob);
971 return install_blob(worktree, ondisk_path, path,
972 GOT_DEFAULT_FILE_MODE, st_mode, blob,
973 restoring_missing_file, reverting_versioned_file,
974 repo, progress_cb, progress_arg);
977 /* Skip blob object header first time around. */
978 memcpy(target_path + target_len, buf + hdrlen,
980 target_len += len - hdrlen;
984 target_path[target_len] = '\0';
987 * Relative symlink target lookup should begin at the directory
988 * in which the blob object is being installed.
990 if (!got_path_is_absolute(target_path)) {
991 char *parent = dirname(ondisk_path);
992 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
993 err = got_error_from_errno("asprintf");
999 * unveil(2) restricts our view of paths in the filesystem.
1000 * ENOENT will occur if a link target path does not exist or
1001 * if it points outside our unveiled path space.
1003 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1004 if (resolved_path == NULL) {
1005 if (errno != ENOENT)
1006 return got_error_from_errno2("realpath", target_path);
1009 /* Only allow symlinks pointing at paths within the work tree. */
1010 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1011 abspath : target_path), worktree->root_path,
1012 strlen(worktree->root_path))) {
1013 /* install as a regular file */
1014 got_object_blob_rewind(blob);
1015 err = install_blob(worktree, ondisk_path, path,
1016 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1017 restoring_missing_file, reverting_versioned_file,
1018 repo, progress_cb, progress_arg);
1022 if (symlink(target_path, ondisk_path) == -1) {
1023 if (errno == EEXIST) {
1026 char etarget[PATH_MAX];
1027 if (lstat(ondisk_path, &sb) == -1) {
1028 err = got_error_from_errno2("lstat",
1032 if (!S_ISLNK(sb.st_mode)) {
1033 err = got_error_path(ondisk_path,
1034 GOT_ERR_FILE_OBSTRUCTED);
1037 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1039 err = got_error_from_errno2("readlink",
1043 if (elen == target_len &&
1044 memcmp(etarget, target_path, target_len) == 0) {
1045 err = NULL; /* nothing to do */
1048 if (unlink(ondisk_path) == -1) {
1049 err = got_error_from_errno2("unlink",
1053 if (symlink(target_path, ondisk_path) == -1) {
1054 err = got_error_from_errno3("symlink",
1055 target_path, ondisk_path);
1059 err = (*progress_cb)(progress_arg,
1060 GOT_STATUS_UPDATE, path);
1065 if (errno == ENOENT) {
1066 char *parent = dirname(ondisk_path);
1067 if (parent == NULL) {
1068 err = got_error_from_errno2("dirname",
1072 err = add_dir_on_disk(worktree, parent);
1076 * Retry, and fall through to error handling
1077 * below if this second attempt fails.
1079 if (symlink(target_path, ondisk_path) != -1) {
1080 err = NULL; /* success */
1085 /* Handle errors from first or second creation attempt. */
1086 if (errno == ENAMETOOLONG) {
1087 /* bad target path; install as a regular file */
1088 got_object_blob_rewind(blob);
1089 err = install_blob(worktree, ondisk_path, path,
1090 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1091 restoring_missing_file, reverting_versioned_file,
1092 repo, progress_cb, progress_arg);
1093 } else if (errno == ENOTDIR) {
1094 err = got_error_path(ondisk_path,
1095 GOT_ERR_FILE_OBSTRUCTED);
1097 err = got_error_from_errno3("symlink",
1098 target_path, ondisk_path);
1101 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1103 free(resolved_path);
1108 static const struct got_error *
1109 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1110 const char *path, mode_t te_mode, mode_t st_mode,
1111 struct got_blob_object *blob, int restoring_missing_file,
1112 int reverting_versioned_file, struct got_repository *repo,
1113 got_worktree_checkout_cb progress_cb, void *progress_arg)
1115 const struct got_error *err = NULL;
1119 char *tmppath = NULL;
1121 if (S_ISLNK(te_mode))
1122 return install_symlink(worktree, ondisk_path, path, te_mode,
1123 st_mode, blob, restoring_missing_file,
1124 reverting_versioned_file, repo, progress_cb, progress_arg);
1126 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1127 GOT_DEFAULT_FILE_MODE);
1129 if (errno == ENOENT) {
1130 char *parent = dirname(path);
1132 return got_error_from_errno2("dirname", path);
1133 err = add_dir_on_disk(worktree, parent);
1136 fd = open(ondisk_path,
1137 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1138 GOT_DEFAULT_FILE_MODE);
1140 return got_error_from_errno2("open",
1142 } else if (errno == EEXIST) {
1143 if (!S_ISREG(st_mode)) {
1144 /* TODO file is obstructed; do something */
1145 err = got_error_path(ondisk_path,
1146 GOT_ERR_FILE_OBSTRUCTED);
1149 err = got_opentemp_named_fd(&tmppath, &fd,
1156 return got_error_from_errno2("open", ondisk_path);
1159 if (restoring_missing_file)
1160 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1161 else if (reverting_versioned_file)
1162 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1164 err = (*progress_cb)(progress_arg,
1165 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1169 hdrlen = got_object_blob_get_hdrlen(blob);
1171 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1172 err = got_object_blob_read_block(&len, blob);
1176 /* Skip blob object header first time around. */
1177 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1179 err = got_error_from_errno("write");
1181 } else if (outlen != len - hdrlen) {
1182 err = got_error(GOT_ERR_IO);
1189 if (fsync(fd) != 0) {
1190 err = got_error_from_errno("fsync");
1195 if (rename(tmppath, ondisk_path) != 0) {
1196 err = got_error_from_errno3("rename", tmppath,
1203 if (chmod(ondisk_path,
1204 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1205 err = got_error_from_errno2("chmod", ondisk_path);
1210 if (fd != -1 && close(fd) != 0 && err == NULL)
1211 err = got_error_from_errno("close");
1216 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1217 static const struct got_error *
1218 get_modified_file_content_status(unsigned char *status, FILE *f)
1220 const struct got_error *err = NULL;
1221 const char *markers[3] = {
1222 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1223 GOT_DIFF_CONFLICT_MARKER_SEP,
1224 GOT_DIFF_CONFLICT_MARKER_END
1229 const char delim[3] = {'\0', '\0', '\0'};
1231 while (*status == GOT_STATUS_MODIFY) {
1232 line = fparseln(f, &len, NULL, delim, 0);
1236 err = got_ferror(f, GOT_ERR_IO);
1240 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1241 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1243 *status = GOT_STATUS_CONFLICT;
1253 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1255 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1256 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1260 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1262 return !(ie->ctime_sec == sb->st_ctime &&
1263 ie->ctime_nsec == sb->st_ctimensec &&
1264 ie->mtime_sec == sb->st_mtime &&
1265 ie->mtime_nsec == sb->st_mtimensec &&
1266 ie->size == (sb->st_size & 0xffffffff) &&
1267 !xbit_differs(ie, sb->st_mode));
1270 static unsigned char
1271 get_staged_status(struct got_fileindex_entry *ie)
1273 switch (got_fileindex_entry_stage_get(ie)) {
1274 case GOT_FILEIDX_STAGE_ADD:
1275 return GOT_STATUS_ADD;
1276 case GOT_FILEIDX_STAGE_DELETE:
1277 return GOT_STATUS_DELETE;
1278 case GOT_FILEIDX_STAGE_MODIFY:
1279 return GOT_STATUS_MODIFY;
1281 return GOT_STATUS_NO_CHANGE;
1285 static const struct got_error *
1286 get_file_status(unsigned char *status, struct stat *sb,
1287 struct got_fileindex_entry *ie, const char *abspath,
1288 int dirfd, const char *de_name, struct got_repository *repo)
1290 const struct got_error *err = NULL;
1291 struct got_object_id id;
1296 struct got_blob_object *blob = NULL;
1298 unsigned char staged_status = get_staged_status(ie);
1300 *status = GOT_STATUS_NO_CHANGE;
1303 * Whenever the caller provides a directory descriptor and a
1304 * directory entry name for the file, use them! This prevents
1305 * race conditions if filesystem paths change beneath our feet.
1308 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1309 if (errno == ENOENT) {
1310 if (got_fileindex_entry_has_file_on_disk(ie))
1311 *status = GOT_STATUS_MISSING;
1313 *status = GOT_STATUS_DELETE;
1316 err = got_error_from_errno2("fstatat", abspath);
1320 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1321 if (fd == -1 && errno != ENOENT)
1322 return got_error_from_errno2("open", abspath);
1323 if (fd == -1 || fstat(fd, sb) == -1) {
1324 if (errno == ENOENT) {
1325 if (got_fileindex_entry_has_file_on_disk(ie))
1326 *status = GOT_STATUS_MISSING;
1328 *status = GOT_STATUS_DELETE;
1331 err = got_error_from_errno2("fstat", abspath);
1336 if (!S_ISREG(sb->st_mode)) {
1337 *status = GOT_STATUS_OBSTRUCTED;
1341 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1342 *status = GOT_STATUS_DELETE;
1344 } else if (!got_fileindex_entry_has_blob(ie) &&
1345 staged_status != GOT_STATUS_ADD) {
1346 *status = GOT_STATUS_ADD;
1350 if (!stat_info_differs(ie, sb))
1353 if (staged_status == GOT_STATUS_MODIFY ||
1354 staged_status == GOT_STATUS_ADD)
1355 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1357 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1359 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1364 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1366 err = got_error_from_errno2("openat", abspath);
1371 f = fdopen(fd, "r");
1373 err = got_error_from_errno2("fdopen", abspath);
1377 hdrlen = got_object_blob_get_hdrlen(blob);
1379 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1380 err = got_object_blob_read_block(&blen, blob);
1383 /* Skip length of blob object header first time around. */
1384 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1385 if (flen == 0 && ferror(f)) {
1386 err = got_error_from_errno("fread");
1391 *status = GOT_STATUS_MODIFY;
1393 } else if (flen == 0) {
1395 *status = GOT_STATUS_MODIFY;
1397 } else if (blen - hdrlen == flen) {
1398 /* Skip blob object header first time around. */
1399 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1400 *status = GOT_STATUS_MODIFY;
1404 *status = GOT_STATUS_MODIFY;
1410 if (*status == GOT_STATUS_MODIFY) {
1412 err = get_modified_file_content_status(status, f);
1413 } else if (xbit_differs(ie, sb->st_mode))
1414 *status = GOT_STATUS_MODE_CHANGE;
1417 got_object_blob_close(blob);
1418 if (f != NULL && fclose(f) == EOF && err == NULL)
1419 err = got_error_from_errno2("fclose", abspath);
1420 if (fd != -1 && close(fd) == -1 && err == NULL)
1421 err = got_error_from_errno2("close", abspath);
1426 * Update timestamps in the file index if a file is unmodified and
1427 * we had to run a full content comparison to find out.
1429 static const struct got_error *
1430 sync_timestamps(char *ondisk_path, unsigned char status,
1431 struct got_fileindex_entry *ie, struct stat *sb)
1433 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1434 return got_fileindex_entry_update(ie, ondisk_path,
1435 ie->blob_sha1, ie->commit_sha1, 1);
1440 static const struct got_error *
1441 update_blob(struct got_worktree *worktree,
1442 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1443 struct got_tree_entry *te, const char *path,
1444 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1447 const struct got_error *err = NULL;
1448 struct got_blob_object *blob = NULL;
1450 unsigned char status = GOT_STATUS_NO_CHANGE;
1453 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1454 return got_error_from_errno("asprintf");
1457 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1458 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1461 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1465 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1466 sb.st_mode = got_fileindex_perms_to_st(ie);
1468 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1470 if (status == GOT_STATUS_OBSTRUCTED) {
1471 err = (*progress_cb)(progress_arg, status, path);
1474 if (status == GOT_STATUS_CONFLICT) {
1475 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1480 if (ie && status != GOT_STATUS_MISSING &&
1481 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1482 if (got_fileindex_entry_has_commit(ie) &&
1483 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1484 SHA1_DIGEST_LENGTH) == 0) {
1485 err = sync_timestamps(ondisk_path, status, ie, &sb);
1488 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1492 if (got_fileindex_entry_has_blob(ie) &&
1493 memcmp(ie->blob_sha1, te->id.sha1,
1494 SHA1_DIGEST_LENGTH) == 0) {
1495 err = sync_timestamps(ondisk_path, status, ie, &sb);
1500 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1504 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1505 int update_timestamps;
1506 struct got_blob_object *blob2 = NULL;
1507 char *label_orig = NULL;
1508 if (got_fileindex_entry_has_blob(ie)) {
1509 struct got_object_id id2;
1510 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1511 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1515 if (got_fileindex_entry_has_commit(ie)) {
1516 char id_str[SHA1_DIGEST_STRING_LENGTH];
1517 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1518 sizeof(id_str)) == NULL) {
1519 err = got_error_path(id_str,
1520 GOT_ERR_BAD_OBJ_ID_STR);
1523 if (asprintf(&label_orig, "%s: commit %s",
1524 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1525 err = got_error_from_errno("asprintf");
1529 err = merge_blob(&update_timestamps, worktree, blob2,
1530 ondisk_path, path, sb.st_mode, label_orig, blob,
1531 worktree->base_commit_id, repo,
1532 progress_cb, progress_arg);
1535 got_object_blob_close(blob2);
1539 * Do not update timestamps of files with local changes.
1540 * Otherwise, a future status walk would treat them as
1541 * unmodified files again.
1543 err = got_fileindex_entry_update(ie, ondisk_path,
1544 blob->id.sha1, worktree->base_commit_id->sha1,
1546 } else if (status == GOT_STATUS_MODE_CHANGE) {
1547 err = got_fileindex_entry_update(ie, ondisk_path,
1548 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1549 } else if (status == GOT_STATUS_DELETE) {
1550 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1553 err = got_fileindex_entry_update(ie, ondisk_path,
1554 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1558 err = install_blob(worktree, ondisk_path, path, te->mode,
1559 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1560 repo, progress_cb, progress_arg);
1564 err = got_fileindex_entry_update(ie, ondisk_path,
1565 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1567 err = create_fileindex_entry(fileindex,
1568 worktree->base_commit_id, ondisk_path, path,
1574 got_object_blob_close(blob);
1580 static const struct got_error *
1581 remove_ondisk_file(const char *root_path, const char *path)
1583 const struct got_error *err = NULL;
1584 char *ondisk_path = NULL;
1586 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1587 return got_error_from_errno("asprintf");
1589 if (unlink(ondisk_path) == -1) {
1590 if (errno != ENOENT)
1591 err = got_error_from_errno2("unlink", ondisk_path);
1593 char *parent = dirname(ondisk_path);
1594 while (parent && strcmp(parent, root_path) != 0) {
1595 if (rmdir(parent) == -1) {
1596 if (errno != ENOTEMPTY)
1597 err = got_error_from_errno2("rmdir",
1601 parent = dirname(parent);
1608 static const struct got_error *
1609 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1610 struct got_fileindex_entry *ie, struct got_repository *repo,
1611 got_worktree_checkout_cb progress_cb, void *progress_arg)
1613 const struct got_error *err = NULL;
1614 unsigned char status;
1618 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1619 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1621 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1623 return got_error_from_errno("asprintf");
1625 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1629 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1630 status == GOT_STATUS_ADD) {
1631 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1635 * Preserve the working file and change the deleted blob's
1636 * entry into a schedule-add entry.
1638 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1641 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1644 if (status == GOT_STATUS_NO_CHANGE) {
1645 err = remove_ondisk_file(worktree->root_path, ie->path);
1649 got_fileindex_entry_remove(fileindex, ie);
1656 struct diff_cb_arg {
1657 struct got_fileindex *fileindex;
1658 struct got_worktree *worktree;
1659 struct got_repository *repo;
1660 got_worktree_checkout_cb progress_cb;
1662 got_cancel_cb cancel_cb;
1666 static const struct got_error *
1667 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1668 struct got_tree_entry *te, const char *parent_path)
1670 struct diff_cb_arg *a = arg;
1672 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1673 return got_error(GOT_ERR_CANCELLED);
1675 return update_blob(a->worktree, a->fileindex, ie, te,
1676 ie->path, a->repo, a->progress_cb, a->progress_arg);
1679 static const struct got_error *
1680 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1682 struct diff_cb_arg *a = arg;
1684 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1685 return got_error(GOT_ERR_CANCELLED);
1687 return delete_blob(a->worktree, a->fileindex, ie,
1688 a->repo, a->progress_cb, a->progress_arg);
1691 static const struct got_error *
1692 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1694 struct diff_cb_arg *a = arg;
1695 const struct got_error *err;
1698 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1699 return got_error(GOT_ERR_CANCELLED);
1701 if (got_object_tree_entry_is_submodule(te))
1704 if (asprintf(&path, "%s%s%s", parent_path,
1705 parent_path[0] ? "/" : "", te->name)
1707 return got_error_from_errno("asprintf");
1709 if (S_ISDIR(te->mode))
1710 err = add_dir_on_disk(a->worktree, path);
1712 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1713 a->repo, a->progress_cb, a->progress_arg);
1719 static const struct got_error *
1720 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1722 const struct got_error *err = NULL;
1723 char *uuidstr = NULL;
1724 uint32_t uuid_status;
1728 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1729 if (uuid_status != uuid_s_ok)
1730 return got_error_uuid(uuid_status, "uuid_to_string");
1732 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1734 err = got_error_from_errno("asprintf");
1741 const struct got_error *
1742 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1744 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1747 static const struct got_error *
1748 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1750 return get_ref_name(refname, worktree,
1751 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1754 static const struct got_error *
1755 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1757 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1760 static const struct got_error *
1761 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1763 return get_ref_name(refname, worktree,
1764 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1767 static const struct got_error *
1768 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1770 return get_ref_name(refname, worktree,
1771 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1774 static const struct got_error *
1775 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1777 return get_ref_name(refname, worktree,
1778 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1781 static const struct got_error *
1782 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1784 return get_ref_name(refname, worktree,
1785 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1788 static const struct got_error *
1789 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1791 return get_ref_name(refname, worktree,
1792 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1795 static const struct got_error *
1796 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1798 return get_ref_name(refname, worktree,
1799 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1802 const struct got_error *
1803 got_worktree_get_histedit_script_path(char **path,
1804 struct got_worktree *worktree)
1806 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1807 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1809 return got_error_from_errno("asprintf");
1815 * Prevent Git's garbage collector from deleting our base commit by
1816 * setting a reference to our base commit's ID.
1818 static const struct got_error *
1819 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1821 const struct got_error *err = NULL;
1822 struct got_reference *ref = NULL;
1825 err = got_worktree_get_base_ref_name(&refname, worktree);
1829 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1833 err = got_ref_write(ref, repo);
1841 static const struct got_error *
1842 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1844 const struct got_error *err = NULL;
1846 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1847 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1848 err = got_error_from_errno("asprintf");
1849 *fileindex_path = NULL;
1855 static const struct got_error *
1856 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1857 struct got_worktree *worktree)
1859 const struct got_error *err = NULL;
1862 *fileindex_path = NULL;
1863 *fileindex = got_fileindex_alloc();
1864 if (*fileindex == NULL)
1865 return got_error_from_errno("got_fileindex_alloc");
1867 err = get_fileindex_path(fileindex_path, worktree);
1871 index = fopen(*fileindex_path, "rb");
1872 if (index == NULL) {
1873 if (errno != ENOENT)
1874 err = got_error_from_errno2("fopen", *fileindex_path);
1876 err = got_fileindex_read(*fileindex, index);
1877 if (fclose(index) != 0 && err == NULL)
1878 err = got_error_from_errno("fclose");
1882 free(*fileindex_path);
1883 *fileindex_path = NULL;
1884 got_fileindex_free(*fileindex);
1890 struct bump_base_commit_id_arg {
1891 struct got_object_id *base_commit_id;
1894 const char *entry_name;
1895 got_worktree_checkout_cb progress_cb;
1899 /* Bump base commit ID of all files within an updated part of the work tree. */
1900 static const struct got_error *
1901 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1903 const struct got_error *err;
1904 struct bump_base_commit_id_arg *a = arg;
1906 if (a->entry_name) {
1907 if (strcmp(ie->path, a->path) != 0)
1909 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1912 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1913 SHA1_DIGEST_LENGTH) == 0)
1916 if (a->progress_cb) {
1917 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1922 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1926 static const struct got_error *
1927 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1929 const struct got_error *err = NULL;
1930 char *new_fileindex_path = NULL;
1931 FILE *new_index = NULL;
1932 struct timespec timeout;
1934 err = got_opentemp_named(&new_fileindex_path, &new_index,
1939 err = got_fileindex_write(fileindex, new_index);
1943 if (rename(new_fileindex_path, fileindex_path) != 0) {
1944 err = got_error_from_errno3("rename", new_fileindex_path,
1946 unlink(new_fileindex_path);
1950 * Sleep for a short amount of time to ensure that files modified after
1951 * this program exits have a different time stamp from the one which
1952 * was recorded in the file index.
1955 timeout.tv_nsec = 1;
1956 nanosleep(&timeout, NULL);
1960 free(new_fileindex_path);
1964 static const struct got_error *
1965 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1966 struct got_object_id **tree_id, const char *wt_relpath,
1967 struct got_worktree *worktree, struct got_repository *repo)
1969 const struct got_error *err = NULL;
1970 struct got_object_id *id = NULL;
1971 char *in_repo_path = NULL;
1972 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1974 *entry_type = GOT_OBJ_TYPE_ANY;
1975 *tree_relpath = NULL;
1978 if (wt_relpath[0] == '\0') {
1979 /* Check out all files within the work tree. */
1980 *entry_type = GOT_OBJ_TYPE_TREE;
1981 *tree_relpath = strdup("");
1982 if (*tree_relpath == NULL) {
1983 err = got_error_from_errno("strdup");
1986 err = got_object_id_by_path(tree_id, repo,
1987 worktree->base_commit_id, worktree->path_prefix);
1993 /* Check out a subset of files in the work tree. */
1995 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1996 is_root_wt ? "" : "/", wt_relpath) == -1) {
1997 err = got_error_from_errno("asprintf");
2001 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2007 in_repo_path = NULL;
2009 err = got_object_get_type(entry_type, repo, id);
2013 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2014 /* Check out a single file. */
2015 if (strchr(wt_relpath, '/') == NULL) {
2016 /* Check out a single file in work tree's root dir. */
2017 in_repo_path = strdup(worktree->path_prefix);
2018 if (in_repo_path == NULL) {
2019 err = got_error_from_errno("strdup");
2022 *tree_relpath = strdup("");
2023 if (*tree_relpath == NULL) {
2024 err = got_error_from_errno("strdup");
2028 /* Check out a single file in a subdirectory. */
2029 err = got_path_dirname(tree_relpath, wt_relpath);
2032 if (asprintf(&in_repo_path, "%s%s%s",
2033 worktree->path_prefix, is_root_wt ? "" : "/",
2034 *tree_relpath) == -1) {
2035 err = got_error_from_errno("asprintf");
2039 err = got_object_id_by_path(tree_id, repo,
2040 worktree->base_commit_id, in_repo_path);
2042 /* Check out all files within a subdirectory. */
2043 *tree_id = got_object_id_dup(id);
2044 if (*tree_id == NULL) {
2045 err = got_error_from_errno("got_object_id_dup");
2048 *tree_relpath = strdup(wt_relpath);
2049 if (*tree_relpath == NULL) {
2050 err = got_error_from_errno("strdup");
2058 *entry_type = GOT_OBJ_TYPE_ANY;
2059 free(*tree_relpath);
2060 *tree_relpath = NULL;
2067 static const struct got_error *
2068 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2069 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2070 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2071 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2073 const struct got_error *err = NULL;
2074 struct got_commit_object *commit = NULL;
2075 struct got_tree_object *tree = NULL;
2076 struct got_fileindex_diff_tree_cb diff_cb;
2077 struct diff_cb_arg arg;
2079 err = ref_base_commit(worktree, repo);
2081 if (!(err->code == GOT_ERR_ERRNO &&
2082 (errno == EACCES || errno == EROFS)))
2084 err = (*progress_cb)(progress_arg,
2085 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2090 err = got_object_open_as_commit(&commit, repo,
2091 worktree->base_commit_id);
2095 err = got_object_open_as_tree(&tree, repo, tree_id);
2100 got_object_tree_find_entry(tree, entry_name) == NULL) {
2101 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2105 diff_cb.diff_old_new = diff_old_new;
2106 diff_cb.diff_old = diff_old;
2107 diff_cb.diff_new = diff_new;
2108 arg.fileindex = fileindex;
2109 arg.worktree = worktree;
2111 arg.progress_cb = progress_cb;
2112 arg.progress_arg = progress_arg;
2113 arg.cancel_cb = cancel_cb;
2114 arg.cancel_arg = cancel_arg;
2115 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2116 entry_name, repo, &diff_cb, &arg);
2119 got_object_tree_close(tree);
2121 got_object_commit_close(commit);
2125 const struct got_error *
2126 got_worktree_checkout_files(struct got_worktree *worktree,
2127 struct got_pathlist_head *paths, struct got_repository *repo,
2128 got_worktree_checkout_cb progress_cb, void *progress_arg,
2129 got_cancel_cb cancel_cb, void *cancel_arg)
2131 const struct got_error *err = NULL, *sync_err, *unlockerr;
2132 struct got_commit_object *commit = NULL;
2133 struct got_tree_object *tree = NULL;
2134 struct got_fileindex *fileindex = NULL;
2135 char *fileindex_path = NULL;
2136 struct got_pathlist_entry *pe;
2137 struct tree_path_data {
2138 SIMPLEQ_ENTRY(tree_path_data) entry;
2139 struct got_object_id *tree_id;
2144 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2146 SIMPLEQ_INIT(&tree_paths);
2148 err = lock_worktree(worktree, LOCK_EX);
2152 /* Map all specified paths to in-repository trees. */
2153 TAILQ_FOREACH(pe, paths, entry) {
2154 tpd = malloc(sizeof(*tpd));
2156 err = got_error_from_errno("malloc");
2160 err = find_tree_entry_for_checkout(&tpd->entry_type,
2161 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2167 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2168 err = got_path_basename(&tpd->entry_name, pe->path);
2176 tpd->entry_name = NULL;
2178 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2182 * Read the file index.
2183 * Checking out files is supposed to be an idempotent operation.
2184 * If the on-disk file index is incomplete we will try to complete it.
2186 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2190 tpd = SIMPLEQ_FIRST(&tree_paths);
2191 TAILQ_FOREACH(pe, paths, entry) {
2192 struct bump_base_commit_id_arg bbc_arg;
2194 err = checkout_files(worktree, fileindex, tpd->relpath,
2195 tpd->tree_id, tpd->entry_name, repo,
2196 progress_cb, progress_arg, cancel_cb, cancel_arg);
2200 bbc_arg.base_commit_id = worktree->base_commit_id;
2201 bbc_arg.entry_name = tpd->entry_name;
2202 bbc_arg.path = pe->path;
2203 bbc_arg.path_len = pe->path_len;
2204 bbc_arg.progress_cb = progress_cb;
2205 bbc_arg.progress_arg = progress_arg;
2206 err = got_fileindex_for_each_entry_safe(fileindex,
2207 bump_base_commit_id, &bbc_arg);
2211 tpd = SIMPLEQ_NEXT(tpd, entry);
2213 sync_err = sync_fileindex(fileindex, fileindex_path);
2214 if (sync_err && err == NULL)
2217 free(fileindex_path);
2219 got_object_tree_close(tree);
2221 got_object_commit_close(commit);
2223 got_fileindex_free(fileindex);
2224 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2225 tpd = SIMPLEQ_FIRST(&tree_paths);
2226 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2231 unlockerr = lock_worktree(worktree, LOCK_SH);
2232 if (unlockerr && err == NULL)
2237 struct merge_file_cb_arg {
2238 struct got_worktree *worktree;
2239 struct got_fileindex *fileindex;
2240 got_worktree_checkout_cb progress_cb;
2242 got_cancel_cb cancel_cb;
2244 const char *label_orig;
2245 struct got_object_id *commit_id2;
2248 static const struct got_error *
2249 merge_file_cb(void *arg, struct got_blob_object *blob1,
2250 struct got_blob_object *blob2, struct got_object_id *id1,
2251 struct got_object_id *id2, const char *path1, const char *path2,
2252 mode_t mode1, mode_t mode2, struct got_repository *repo)
2254 static const struct got_error *err = NULL;
2255 struct merge_file_cb_arg *a = arg;
2256 struct got_fileindex_entry *ie;
2257 char *ondisk_path = NULL;
2259 unsigned char status;
2260 int local_changes_subsumed;
2262 if (blob1 && blob2) {
2263 ie = got_fileindex_entry_get(a->fileindex, path2,
2266 return (*a->progress_cb)(a->progress_arg,
2267 GOT_STATUS_MISSING, path2);
2269 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2271 return got_error_from_errno("asprintf");
2273 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2278 if (status == GOT_STATUS_DELETE) {
2279 err = (*a->progress_cb)(a->progress_arg,
2280 GOT_STATUS_MERGE, path2);
2283 if (status != GOT_STATUS_NO_CHANGE &&
2284 status != GOT_STATUS_MODIFY &&
2285 status != GOT_STATUS_CONFLICT &&
2286 status != GOT_STATUS_ADD) {
2287 err = (*a->progress_cb)(a->progress_arg, status, path2);
2291 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2292 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2293 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2295 ie = got_fileindex_entry_get(a->fileindex, path1,
2298 return (*a->progress_cb)(a->progress_arg,
2299 GOT_STATUS_MISSING, path1);
2301 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2303 return got_error_from_errno("asprintf");
2305 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2311 case GOT_STATUS_NO_CHANGE:
2312 err = (*a->progress_cb)(a->progress_arg,
2313 GOT_STATUS_DELETE, path1);
2316 err = remove_ondisk_file(a->worktree->root_path, path1);
2320 got_fileindex_entry_mark_deleted_from_disk(ie);
2322 case GOT_STATUS_DELETE:
2323 case GOT_STATUS_MISSING:
2324 err = (*a->progress_cb)(a->progress_arg,
2325 GOT_STATUS_DELETE, path1);
2329 got_fileindex_entry_mark_deleted_from_disk(ie);
2331 case GOT_STATUS_ADD:
2332 case GOT_STATUS_MODIFY:
2333 case GOT_STATUS_CONFLICT:
2334 err = (*a->progress_cb)(a->progress_arg,
2335 GOT_STATUS_CANNOT_DELETE, path1);
2339 case GOT_STATUS_OBSTRUCTED:
2340 err = (*a->progress_cb)(a->progress_arg, status, path1);
2348 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2350 return got_error_from_errno("asprintf");
2351 ie = got_fileindex_entry_get(a->fileindex, path2,
2354 err = get_file_status(&status, &sb, ie, ondisk_path,
2358 if (status != GOT_STATUS_NO_CHANGE &&
2359 status != GOT_STATUS_MODIFY &&
2360 status != GOT_STATUS_CONFLICT &&
2361 status != GOT_STATUS_ADD) {
2362 err = (*a->progress_cb)(a->progress_arg,
2366 err = merge_blob(&local_changes_subsumed, a->worktree,
2367 NULL, ondisk_path, path2, sb.st_mode,
2368 a->label_orig, blob2, a->commit_id2, repo,
2371 if (status == GOT_STATUS_DELETE) {
2372 err = got_fileindex_entry_update(ie,
2373 ondisk_path, blob2->id.sha1,
2374 a->worktree->base_commit_id->sha1, 0);
2379 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2380 err = install_blob(a->worktree, ondisk_path, path2,
2381 /* XXX get this from parent tree! */
2382 GOT_DEFAULT_FILE_MODE,
2383 sb.st_mode, blob2, 0, 0, repo,
2384 a->progress_cb, a->progress_arg);
2387 err = got_fileindex_entry_alloc(&ie, path2);
2390 err = got_fileindex_entry_update(ie, ondisk_path,
2393 got_fileindex_entry_free(ie);
2396 err = got_fileindex_entry_add(a->fileindex, ie);
2398 got_fileindex_entry_free(ie);
2408 struct check_merge_ok_arg {
2409 struct got_worktree *worktree;
2410 struct got_repository *repo;
2413 static const struct got_error *
2414 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2416 const struct got_error *err = NULL;
2417 struct check_merge_ok_arg *a = arg;
2418 unsigned char status;
2422 /* Reject merges into a work tree with mixed base commits. */
2423 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2424 SHA1_DIGEST_LENGTH))
2425 return got_error(GOT_ERR_MIXED_COMMITS);
2427 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2429 return got_error_from_errno("asprintf");
2431 /* Reject merges into a work tree with conflicted files. */
2432 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2435 if (status == GOT_STATUS_CONFLICT)
2436 return got_error(GOT_ERR_CONFLICTS);
2441 static const struct got_error *
2442 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2443 const char *fileindex_path, struct got_object_id *commit_id1,
2444 struct got_object_id *commit_id2, struct got_repository *repo,
2445 got_worktree_checkout_cb progress_cb, void *progress_arg,
2446 got_cancel_cb cancel_cb, void *cancel_arg)
2448 const struct got_error *err = NULL, *sync_err;
2449 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2450 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2451 struct merge_file_cb_arg arg;
2452 char *label_orig = NULL;
2457 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2458 worktree->path_prefix);
2462 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2466 err = got_object_id_str(&id_str, commit_id1);
2470 if (asprintf(&label_orig, "%s: commit %s",
2471 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2472 err = got_error_from_errno("asprintf");
2479 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2480 worktree->path_prefix);
2484 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2488 arg.worktree = worktree;
2489 arg.fileindex = fileindex;
2490 arg.progress_cb = progress_cb;
2491 arg.progress_arg = progress_arg;
2492 arg.cancel_cb = cancel_cb;
2493 arg.cancel_arg = cancel_arg;
2494 arg.label_orig = label_orig;
2495 arg.commit_id2 = commit_id2;
2496 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2497 sync_err = sync_fileindex(fileindex, fileindex_path);
2498 if (sync_err && err == NULL)
2502 got_object_tree_close(tree1);
2504 got_object_tree_close(tree2);
2509 const struct got_error *
2510 got_worktree_merge_files(struct got_worktree *worktree,
2511 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2512 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2513 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2515 const struct got_error *err, *unlockerr;
2516 char *fileindex_path = NULL;
2517 struct got_fileindex *fileindex = NULL;
2518 struct check_merge_ok_arg mok_arg;
2520 err = lock_worktree(worktree, LOCK_EX);
2524 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2528 mok_arg.worktree = worktree;
2529 mok_arg.repo = repo;
2530 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2535 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2536 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2539 got_fileindex_free(fileindex);
2540 free(fileindex_path);
2541 unlockerr = lock_worktree(worktree, LOCK_SH);
2542 if (unlockerr && err == NULL)
2547 struct diff_dir_cb_arg {
2548 struct got_fileindex *fileindex;
2549 struct got_worktree *worktree;
2550 const char *status_path;
2551 size_t status_path_len;
2552 struct got_repository *repo;
2553 got_worktree_status_cb status_cb;
2555 got_cancel_cb cancel_cb;
2557 /* A pathlist containing per-directory pathlists of ignore patterns. */
2558 struct got_pathlist_head ignores;
2559 int report_unchanged;
2563 static const struct got_error *
2564 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2565 int dirfd, const char *de_name,
2566 got_worktree_status_cb status_cb, void *status_arg,
2567 struct got_repository *repo, int report_unchanged)
2569 const struct got_error *err = NULL;
2570 unsigned char status = GOT_STATUS_NO_CHANGE;
2571 unsigned char staged_status = get_staged_status(ie);
2573 struct got_object_id blob_id, commit_id, staged_blob_id;
2574 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2575 struct got_object_id *staged_blob_idp = NULL;
2577 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2581 if (status == GOT_STATUS_NO_CHANGE &&
2582 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2585 if (got_fileindex_entry_has_blob(ie)) {
2586 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2587 blob_idp = &blob_id;
2589 if (got_fileindex_entry_has_commit(ie)) {
2590 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2591 commit_idp = &commit_id;
2593 if (staged_status == GOT_STATUS_ADD ||
2594 staged_status == GOT_STATUS_MODIFY) {
2595 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2596 SHA1_DIGEST_LENGTH);
2597 staged_blob_idp = &staged_blob_id;
2600 return (*status_cb)(status_arg, status, staged_status,
2601 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2604 static const struct got_error *
2605 status_old_new(void *arg, struct got_fileindex_entry *ie,
2606 struct dirent *de, const char *parent_path, int dirfd)
2608 const struct got_error *err = NULL;
2609 struct diff_dir_cb_arg *a = arg;
2612 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2613 return got_error(GOT_ERR_CANCELLED);
2615 if (got_path_cmp(parent_path, a->status_path,
2616 strlen(parent_path), a->status_path_len) != 0 &&
2617 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2620 if (parent_path[0]) {
2621 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2622 parent_path, de->d_name) == -1)
2623 return got_error_from_errno("asprintf");
2625 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2627 return got_error_from_errno("asprintf");
2630 err = report_file_status(ie, abspath, dirfd, de->d_name,
2631 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2636 static const struct got_error *
2637 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2639 struct diff_dir_cb_arg *a = arg;
2640 struct got_object_id blob_id, commit_id;
2641 unsigned char status;
2643 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2644 return got_error(GOT_ERR_CANCELLED);
2646 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2649 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2650 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2651 if (got_fileindex_entry_has_file_on_disk(ie))
2652 status = GOT_STATUS_MISSING;
2654 status = GOT_STATUS_DELETE;
2655 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2656 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2660 free_ignorelist(struct got_pathlist_head *ignorelist)
2662 struct got_pathlist_entry *pe;
2664 TAILQ_FOREACH(pe, ignorelist, entry)
2665 free((char *)pe->path);
2666 got_pathlist_free(ignorelist);
2670 free_ignores(struct got_pathlist_head *ignores)
2672 struct got_pathlist_entry *pe;
2674 TAILQ_FOREACH(pe, ignores, entry) {
2675 struct got_pathlist_head *ignorelist = pe->data;
2676 free_ignorelist(ignorelist);
2677 free((char *)pe->path);
2679 got_pathlist_free(ignores);
2682 static const struct got_error *
2683 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2685 const struct got_error *err = NULL;
2686 struct got_pathlist_entry *pe = NULL;
2687 struct got_pathlist_head *ignorelist;
2688 char *line = NULL, *pattern, *dirpath = NULL;
2689 size_t linesize = 0;
2692 ignorelist = calloc(1, sizeof(*ignorelist));
2693 if (ignorelist == NULL)
2694 return got_error_from_errno("calloc");
2695 TAILQ_INIT(ignorelist);
2697 while ((linelen = getline(&line, &linesize, f)) != -1) {
2698 if (linelen > 0 && line[linelen - 1] == '\n')
2699 line[linelen - 1] = '\0';
2701 /* Git's ignores may contain comments. */
2705 /* Git's negated patterns are not (yet?) supported. */
2709 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2711 err = got_error_from_errno("asprintf");
2714 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2719 err = got_error_from_errno("getline");
2723 dirpath = strdup(path);
2724 if (dirpath == NULL) {
2725 err = got_error_from_errno("strdup");
2728 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2731 if (err || pe == NULL) {
2733 free_ignorelist(ignorelist);
2739 match_ignores(struct got_pathlist_head *ignores, const char *path)
2741 struct got_pathlist_entry *pe;
2743 /* Handle patterns which match in all directories. */
2744 TAILQ_FOREACH(pe, ignores, entry) {
2745 struct got_pathlist_head *ignorelist = pe->data;
2746 struct got_pathlist_entry *pi;
2748 TAILQ_FOREACH(pi, ignorelist, entry) {
2749 const char *p, *pattern = pi->path;
2751 if (strncmp(pattern, "**/", 3) != 0)
2756 if (fnmatch(pattern, p,
2757 FNM_PATHNAME | FNM_LEADING_DIR)) {
2758 /* Retry in next directory. */
2759 while (*p && *p != '/')
2771 * The ignores pathlist contains ignore lists from children before
2772 * parents, so we can find the most specific ignorelist by walking
2773 * ignores backwards.
2775 pe = TAILQ_LAST(ignores, got_pathlist_head);
2777 if (got_path_is_child(path, pe->path, pe->path_len)) {
2778 struct got_pathlist_head *ignorelist = pe->data;
2779 struct got_pathlist_entry *pi;
2780 TAILQ_FOREACH(pi, ignorelist, entry) {
2781 const char *pattern = pi->path;
2782 int flags = FNM_LEADING_DIR;
2783 if (strstr(pattern, "/**/") == NULL)
2784 flags |= FNM_PATHNAME;
2785 if (fnmatch(pattern, path, flags))
2790 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2796 static const struct got_error *
2797 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2798 const char *path, int dirfd, const char *ignores_filename)
2800 const struct got_error *err = NULL;
2803 FILE *ignoresfile = NULL;
2805 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2806 path[0] ? "/" : "", ignores_filename) == -1)
2807 return got_error_from_errno("asprintf");
2810 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2812 if (errno != ENOENT && errno != EACCES)
2813 err = got_error_from_errno2("openat",
2816 ignoresfile = fdopen(fd, "r");
2817 if (ignoresfile == NULL)
2818 err = got_error_from_errno2("fdopen",
2822 err = read_ignores(ignores, path, ignoresfile);
2826 ignoresfile = fopen(ignorespath, "r");
2827 if (ignoresfile == NULL) {
2828 if (errno != ENOENT && errno != EACCES)
2829 err = got_error_from_errno2("fopen",
2832 err = read_ignores(ignores, path, ignoresfile);
2835 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2836 err = got_error_from_errno2("fclose", path);
2837 if (fd != -1 && close(fd) == -1 && err == NULL)
2838 err = got_error_from_errno2("close", path);
2843 static const struct got_error *
2844 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2846 const struct got_error *err = NULL;
2847 struct diff_dir_cb_arg *a = arg;
2850 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2851 return got_error(GOT_ERR_CANCELLED);
2853 /* XXX ignore symlinks for now */
2854 if (de->d_type == DT_LNK)
2857 if (parent_path[0]) {
2858 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2859 return got_error_from_errno("asprintf");
2864 if (de->d_type != DT_DIR &&
2865 got_path_is_child(path, a->status_path, a->status_path_len)
2866 && !match_ignores(&a->ignores, path))
2867 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2868 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2874 static const struct got_error *
2875 status_traverse(void *arg, const char *path, int dirfd)
2877 const struct got_error *err = NULL;
2878 struct diff_dir_cb_arg *a = arg;
2883 err = add_ignores(&a->ignores, a->worktree->root_path,
2884 path, dirfd, ".cvsignore");
2888 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2889 dirfd, ".gitignore");
2894 static const struct got_error *
2895 report_single_file_status(const char *path, const char *ondisk_path,
2896 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2897 void *status_arg, struct got_repository *repo, int report_unchanged)
2899 struct got_fileindex_entry *ie;
2902 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2904 return report_file_status(ie, ondisk_path, -1, NULL,
2905 status_cb, status_arg, repo, report_unchanged);
2907 if (lstat(ondisk_path, &sb) == -1) {
2908 if (errno != ENOENT)
2909 return got_error_from_errno2("lstat", ondisk_path);
2910 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2911 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2915 if (S_ISREG(sb.st_mode))
2916 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2917 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2922 static const struct got_error *
2923 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
2924 const char *root_path, const char *path)
2926 const struct got_error *err;
2927 char *parent_path, *next_parent_path = NULL;
2929 err = add_ignores(ignores, root_path, "", -1,
2934 err = add_ignores(ignores, root_path, "", -1,
2939 err = got_path_dirname(&parent_path, path);
2941 if (err->code == GOT_ERR_BAD_PATH)
2942 return NULL; /* cannot traverse parent */
2946 err = add_ignores(ignores, root_path, parent_path, -1,
2950 err = add_ignores(ignores, root_path, parent_path, -1,
2954 err = got_path_dirname(&next_parent_path, parent_path);
2956 if (err->code == GOT_ERR_BAD_PATH)
2957 err = NULL; /* traversed everything */
2961 parent_path = next_parent_path;
2962 next_parent_path = NULL;
2966 free(next_parent_path);
2970 static const struct got_error *
2971 worktree_status(struct got_worktree *worktree, const char *path,
2972 struct got_fileindex *fileindex, struct got_repository *repo,
2973 got_worktree_status_cb status_cb, void *status_arg,
2974 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2975 int report_unchanged)
2977 const struct got_error *err = NULL;
2979 struct got_fileindex_diff_dir_cb fdiff_cb;
2980 struct diff_dir_cb_arg arg;
2981 char *ondisk_path = NULL;
2983 TAILQ_INIT(&arg.ignores);
2985 if (asprintf(&ondisk_path, "%s%s%s",
2986 worktree->root_path, path[0] ? "/" : "", path) == -1)
2987 return got_error_from_errno("asprintf");
2989 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2991 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2992 err = got_error_from_errno2("open", ondisk_path);
2994 err = report_single_file_status(path, ondisk_path,
2995 fileindex, status_cb, status_arg, repo,
2998 fdiff_cb.diff_old_new = status_old_new;
2999 fdiff_cb.diff_old = status_old;
3000 fdiff_cb.diff_new = status_new;
3001 fdiff_cb.diff_traverse = status_traverse;
3002 arg.fileindex = fileindex;
3003 arg.worktree = worktree;
3004 arg.status_path = path;
3005 arg.status_path_len = strlen(path);
3007 arg.status_cb = status_cb;
3008 arg.status_arg = status_arg;
3009 arg.cancel_cb = cancel_cb;
3010 arg.cancel_arg = cancel_arg;
3011 arg.report_unchanged = report_unchanged;
3012 arg.no_ignores = no_ignores;
3014 err = add_ignores_from_parent_paths(&arg.ignores,
3015 worktree->root_path, path);
3019 err = got_fileindex_diff_dir(fileindex, fd,
3020 worktree->root_path, path, repo, &fdiff_cb, &arg);
3023 free_ignores(&arg.ignores);
3024 if (fd != -1 && close(fd) != 0 && err == NULL)
3025 err = got_error_from_errno("close");
3030 const struct got_error *
3031 got_worktree_status(struct got_worktree *worktree,
3032 struct got_pathlist_head *paths, struct got_repository *repo,
3033 got_worktree_status_cb status_cb, void *status_arg,
3034 got_cancel_cb cancel_cb, void *cancel_arg)
3036 const struct got_error *err = NULL;
3037 char *fileindex_path = NULL;
3038 struct got_fileindex *fileindex = NULL;
3039 struct got_pathlist_entry *pe;
3041 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3045 TAILQ_FOREACH(pe, paths, entry) {
3046 err = worktree_status(worktree, pe->path, fileindex, repo,
3047 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3051 free(fileindex_path);
3052 got_fileindex_free(fileindex);
3056 const struct got_error *
3057 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3060 const struct got_error *err = NULL;
3061 char *resolved, *cwd = NULL, *path = NULL;
3066 resolved = realpath(arg, NULL);
3067 if (resolved == NULL) {
3068 if (errno != ENOENT)
3069 return got_error_from_errno2("realpath", arg);
3070 cwd = getcwd(NULL, 0);
3072 return got_error_from_errno("getcwd");
3073 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3074 err = got_error_from_errno("asprintf");
3079 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3080 strlen(got_worktree_get_root_path(worktree)))) {
3081 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3085 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3086 err = got_path_skip_common_ancestor(&path,
3087 got_worktree_get_root_path(worktree), resolved);
3093 err = got_error_from_errno("strdup");
3098 /* XXX status walk can't deal with trailing slash! */
3100 while (len > 0 && path[len - 1] == '/') {
3101 path[len - 1] = '\0';
3114 struct schedule_addition_args {
3115 struct got_worktree *worktree;
3116 struct got_fileindex *fileindex;
3117 got_worktree_checkout_cb progress_cb;
3119 struct got_repository *repo;
3122 static const struct got_error *
3123 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3124 const char *relpath, struct got_object_id *blob_id,
3125 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3126 int dirfd, const char *de_name)
3128 struct schedule_addition_args *a = arg;
3129 const struct got_error *err = NULL;
3130 struct got_fileindex_entry *ie;
3134 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3136 return got_error_from_errno("asprintf");
3138 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3140 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3144 /* Re-adding an existing entry is a no-op. */
3145 if (status == GOT_STATUS_ADD)
3147 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3152 if (status != GOT_STATUS_UNVERSIONED) {
3153 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3157 err = got_fileindex_entry_alloc(&ie, relpath);
3160 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3162 got_fileindex_entry_free(ie);
3165 err = got_fileindex_entry_add(a->fileindex, ie);
3167 got_fileindex_entry_free(ie);
3174 if (status == GOT_STATUS_ADD)
3176 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3179 const struct got_error *
3180 got_worktree_schedule_add(struct got_worktree *worktree,
3181 struct got_pathlist_head *paths,
3182 got_worktree_checkout_cb progress_cb, void *progress_arg,
3183 struct got_repository *repo, int no_ignores)
3185 struct got_fileindex *fileindex = NULL;
3186 char *fileindex_path = NULL;
3187 const struct got_error *err = NULL, *sync_err, *unlockerr;
3188 struct got_pathlist_entry *pe;
3189 struct schedule_addition_args saa;
3191 err = lock_worktree(worktree, LOCK_EX);
3195 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3199 saa.worktree = worktree;
3200 saa.fileindex = fileindex;
3201 saa.progress_cb = progress_cb;
3202 saa.progress_arg = progress_arg;
3205 TAILQ_FOREACH(pe, paths, entry) {
3206 err = worktree_status(worktree, pe->path, fileindex, repo,
3207 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3211 sync_err = sync_fileindex(fileindex, fileindex_path);
3212 if (sync_err && err == NULL)
3215 free(fileindex_path);
3217 got_fileindex_free(fileindex);
3218 unlockerr = lock_worktree(worktree, LOCK_SH);
3219 if (unlockerr && err == NULL)
3224 struct schedule_deletion_args {
3225 struct got_worktree *worktree;
3226 struct got_fileindex *fileindex;
3227 got_worktree_delete_cb progress_cb;
3229 struct got_repository *repo;
3230 int delete_local_mods;
3234 static const struct got_error *
3235 schedule_for_deletion(void *arg, unsigned char status,
3236 unsigned char staged_status, const char *relpath,
3237 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3238 struct got_object_id *commit_id, int dirfd, const char *de_name)
3240 struct schedule_deletion_args *a = arg;
3241 const struct got_error *err = NULL;
3242 struct got_fileindex_entry *ie = NULL;
3244 char *ondisk_path, *parent = NULL;
3246 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3248 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3250 staged_status = get_staged_status(ie);
3251 if (staged_status != GOT_STATUS_NO_CHANGE) {
3252 if (staged_status == GOT_STATUS_DELETE)
3254 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3257 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3259 return got_error_from_errno("asprintf");
3261 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3266 if (status != GOT_STATUS_NO_CHANGE) {
3267 if (status == GOT_STATUS_DELETE)
3269 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3270 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3273 if (status != GOT_STATUS_MODIFY &&
3274 status != GOT_STATUS_MISSING) {
3275 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3280 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3282 if (unlinkat(dirfd, de_name, 0) != 0) {
3283 err = got_error_from_errno2("unlinkat",
3287 } else if (unlink(ondisk_path) != 0) {
3288 err = got_error_from_errno2("unlink", ondisk_path);
3292 parent = dirname(ondisk_path);
3294 if (parent == NULL) {
3295 err = got_error_from_errno2("dirname", ondisk_path);
3298 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3299 if (rmdir(parent) == -1) {
3300 if (errno != ENOTEMPTY)
3301 err = got_error_from_errno2("rmdir",
3305 parent = dirname(parent);
3306 if (parent == NULL) {
3307 err = got_error_from_errno2("dirname", parent);
3313 got_fileindex_entry_mark_deleted_from_disk(ie);
3318 if (status == GOT_STATUS_DELETE)
3320 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3321 staged_status, relpath);
3324 const struct got_error *
3325 got_worktree_schedule_delete(struct got_worktree *worktree,
3326 struct got_pathlist_head *paths, int delete_local_mods,
3327 got_worktree_delete_cb progress_cb, void *progress_arg,
3328 struct got_repository *repo, int keep_on_disk)
3330 struct got_fileindex *fileindex = NULL;
3331 char *fileindex_path = NULL;
3332 const struct got_error *err = NULL, *sync_err, *unlockerr;
3333 struct got_pathlist_entry *pe;
3334 struct schedule_deletion_args sda;
3336 err = lock_worktree(worktree, LOCK_EX);
3340 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3344 sda.worktree = worktree;
3345 sda.fileindex = fileindex;
3346 sda.progress_cb = progress_cb;
3347 sda.progress_arg = progress_arg;
3349 sda.delete_local_mods = delete_local_mods;
3350 sda.keep_on_disk = keep_on_disk;
3352 TAILQ_FOREACH(pe, paths, entry) {
3353 err = worktree_status(worktree, pe->path, fileindex, repo,
3354 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3358 sync_err = sync_fileindex(fileindex, fileindex_path);
3359 if (sync_err && err == NULL)
3362 free(fileindex_path);
3364 got_fileindex_free(fileindex);
3365 unlockerr = lock_worktree(worktree, LOCK_SH);
3366 if (unlockerr && err == NULL)
3371 static const struct got_error *
3372 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3374 const struct got_error *err = NULL;
3376 size_t linesize = 0, n;
3379 linelen = getline(&line, &linesize, infile);
3380 if (linelen == -1) {
3381 if (ferror(infile)) {
3382 err = got_error_from_errno("getline");
3388 n = fwrite(line, 1, linelen, outfile);
3390 err = got_ferror(outfile, GOT_ERR_IO);
3395 n = fwrite(line, 1, linelen, rejectfile);
3397 err = got_ferror(outfile, GOT_ERR_IO);
3404 static const struct got_error *
3405 skip_one_line(FILE *f)
3408 size_t linesize = 0;
3411 linelen = getline(&line, &linesize, f);
3412 if (linelen == -1) {
3414 return got_error_from_errno("getline");
3421 static const struct got_error *
3422 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3423 int start_old, int end_old, int start_new, int end_new,
3424 FILE *outfile, FILE *rejectfile)
3426 const struct got_error *err;
3428 /* Copy old file's lines leading up to patch. */
3429 while (!feof(f1) && *line_cur1 < start_old) {
3430 err = copy_one_line(f1, outfile, NULL);
3435 /* Skip new file's lines leading up to patch. */
3436 while (!feof(f2) && *line_cur2 < start_new) {
3438 err = copy_one_line(f2, NULL, rejectfile);
3440 err = skip_one_line(f2);
3445 /* Copy patched lines. */
3446 while (!feof(f2) && *line_cur2 <= end_new) {
3447 err = copy_one_line(f2, outfile, NULL);
3452 /* Skip over old file's replaced lines. */
3453 while (!feof(f1) && *line_cur1 <= end_old) {
3455 err = copy_one_line(f1, NULL, rejectfile);
3457 err = skip_one_line(f1);
3466 static const struct got_error *
3467 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3468 FILE *outfile, FILE *rejectfile)
3470 const struct got_error *err;
3473 /* Copy old file's lines until EOF. */
3475 err = copy_one_line(f1, outfile, NULL);
3482 /* Copy new file's lines until EOF. */
3484 err = copy_one_line(f2, NULL, rejectfile);
3494 static const struct got_error *
3495 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3496 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3497 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3498 int *line_cur2, FILE *outfile, FILE *rejectfile,
3499 got_worktree_patch_cb patch_cb, void *patch_arg)
3501 const struct got_error *err = NULL;
3502 int start_old = change->cv.a;
3503 int end_old = change->cv.b;
3504 int start_new = change->cv.c;
3505 int end_new = change->cv.d;
3509 *choice = GOT_PATCH_CHOICE_NONE;
3511 hunkfile = got_opentemp();
3512 if (hunkfile == NULL)
3513 return got_error_from_errno("got_opentemp");
3518 /* XXX TODO needs error checking */
3519 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3521 if (fseek(f1, pos1, SEEK_SET) == -1) {
3522 err = got_ferror(f1, GOT_ERR_IO);
3525 if (fseek(f2, pos2, SEEK_SET) == -1) {
3526 err = got_ferror(f1, GOT_ERR_IO);
3529 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3530 err = got_ferror(hunkfile, GOT_ERR_IO);
3534 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3535 hunkfile, n, nchanges);
3540 case GOT_PATCH_CHOICE_YES:
3541 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3542 end_old, start_new, end_new, outfile, rejectfile);
3544 case GOT_PATCH_CHOICE_NO:
3545 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3546 end_old, start_new, end_new, rejectfile, outfile);
3548 case GOT_PATCH_CHOICE_QUIT:
3551 err = got_error(GOT_ERR_PATCH_CHOICE);
3555 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3556 err = got_error_from_errno("fclose");
3560 struct revert_file_args {
3561 struct got_worktree *worktree;
3562 struct got_fileindex *fileindex;
3563 got_worktree_checkout_cb progress_cb;
3565 got_worktree_patch_cb patch_cb;
3567 struct got_repository *repo;
3570 static const struct got_error *
3571 create_patched_content(char **path_outfile, int reverse_patch,
3572 struct got_object_id *blob_id, const char *path2,
3573 int dirfd2, const char *de_name2,
3574 const char *relpath, struct got_repository *repo,
3575 got_worktree_patch_cb patch_cb, void *patch_arg)
3577 const struct got_error *err;
3578 struct got_blob_object *blob = NULL;
3579 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3581 char *path1 = NULL, *id_str = NULL;
3582 struct stat sb1, sb2;
3583 struct got_diff_changes *changes = NULL;
3584 struct got_diff_state *ds = NULL;
3585 struct got_diff_args *args = NULL;
3586 struct got_diff_change *change;
3587 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3590 *path_outfile = NULL;
3592 err = got_object_id_str(&id_str, blob_id);
3597 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3599 err = got_error_from_errno2("openat", path2);
3603 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3605 err = got_error_from_errno2("open", path2);
3609 if (fstat(fd2, &sb2) == -1) {
3610 err = got_error_from_errno2("fstat", path2);
3614 f2 = fdopen(fd2, "r");
3616 err = got_error_from_errno2("fdopen", path2);
3621 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3625 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3629 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3633 if (stat(path1, &sb1) == -1) {
3634 err = got_error_from_errno2("stat", path1);
3638 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3639 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3643 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3647 if (fseek(f1, 0L, SEEK_SET) == -1)
3648 return got_ferror(f1, GOT_ERR_IO);
3649 if (fseek(f2, 0L, SEEK_SET) == -1)
3650 return got_ferror(f2, GOT_ERR_IO);
3651 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3653 err = apply_or_reject_change(&choice, change, ++n,
3654 changes->nchanges, ds, args, diff_flags, relpath,
3655 f1, f2, &line_cur1, &line_cur2,
3656 reverse_patch ? NULL : outfile,
3657 reverse_patch ? outfile : NULL,
3658 patch_cb, patch_arg);
3661 if (choice == GOT_PATCH_CHOICE_YES)
3663 else if (choice == GOT_PATCH_CHOICE_QUIT)
3667 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3668 reverse_patch ? NULL : outfile,
3669 reverse_patch ? outfile : NULL);
3673 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3674 err = got_error_from_errno2("chmod", path2);
3681 got_object_blob_close(blob);
3682 if (f1 && fclose(f1) == EOF && err == NULL)
3683 err = got_error_from_errno2("fclose", path1);
3684 if (f2 && fclose(f2) == EOF && err == NULL)
3685 err = got_error_from_errno2("fclose", path2);
3686 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3687 err = got_error_from_errno2("close", path2);
3688 if (outfile && fclose(outfile) == EOF && err == NULL)
3689 err = got_error_from_errno2("fclose", *path_outfile);
3690 if (path1 && unlink(path1) == -1 && err == NULL)
3691 err = got_error_from_errno2("unlink", path1);
3692 if (err || !have_content) {
3693 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3694 err = got_error_from_errno2("unlink", *path_outfile);
3695 free(*path_outfile);
3696 *path_outfile = NULL;
3700 got_diff_state_free(ds);
3704 got_diff_free_changes(changes);
3709 static const struct got_error *
3710 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3711 const char *relpath, struct got_object_id *blob_id,
3712 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3713 int dirfd, const char *de_name)
3715 struct revert_file_args *a = arg;
3716 const struct got_error *err = NULL;
3717 char *parent_path = NULL;
3718 struct got_fileindex_entry *ie;
3719 struct got_tree_object *tree = NULL;
3720 struct got_object_id *tree_id = NULL;
3721 const struct got_tree_entry *te = NULL;
3722 char *tree_path = NULL, *te_name;
3723 char *ondisk_path = NULL, *path_content = NULL;
3724 struct got_blob_object *blob = NULL;
3726 /* Reverting a staged deletion is a no-op. */
3727 if (status == GOT_STATUS_DELETE &&
3728 staged_status != GOT_STATUS_NO_CHANGE)
3731 if (status == GOT_STATUS_UNVERSIONED)
3732 return (*a->progress_cb)(a->progress_arg,
3733 GOT_STATUS_UNVERSIONED, relpath);
3735 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3737 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3739 /* Construct in-repository path of tree which contains this blob. */
3740 err = got_path_dirname(&parent_path, ie->path);
3742 if (err->code != GOT_ERR_BAD_PATH)
3744 parent_path = strdup("/");
3745 if (parent_path == NULL) {
3746 err = got_error_from_errno("strdup");
3750 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3751 tree_path = strdup(parent_path);
3752 if (tree_path == NULL) {
3753 err = got_error_from_errno("strdup");
3757 if (got_path_is_root_dir(parent_path)) {
3758 tree_path = strdup(a->worktree->path_prefix);
3759 if (tree_path == NULL) {
3760 err = got_error_from_errno("strdup");
3764 if (asprintf(&tree_path, "%s/%s",
3765 a->worktree->path_prefix, parent_path) == -1) {
3766 err = got_error_from_errno("asprintf");
3772 err = got_object_id_by_path(&tree_id, a->repo,
3773 a->worktree->base_commit_id, tree_path);
3775 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3776 (status == GOT_STATUS_ADD ||
3777 staged_status == GOT_STATUS_ADD)))
3780 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3784 te_name = basename(ie->path);
3785 if (te_name == NULL) {
3786 err = got_error_from_errno2("basename", ie->path);
3790 te = got_object_tree_find_entry(tree, te_name);
3791 if (te == NULL && status != GOT_STATUS_ADD &&
3792 staged_status != GOT_STATUS_ADD) {
3793 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3799 case GOT_STATUS_ADD:
3801 int choice = GOT_PATCH_CHOICE_NONE;
3802 err = (*a->patch_cb)(&choice, a->patch_arg,
3803 status, ie->path, NULL, 1, 1);
3806 if (choice != GOT_PATCH_CHOICE_YES)
3809 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3813 got_fileindex_entry_remove(a->fileindex, ie);
3815 case GOT_STATUS_DELETE:
3817 int choice = GOT_PATCH_CHOICE_NONE;
3818 err = (*a->patch_cb)(&choice, a->patch_arg,
3819 status, ie->path, NULL, 1, 1);
3822 if (choice != GOT_PATCH_CHOICE_YES)
3826 case GOT_STATUS_MODIFY:
3827 case GOT_STATUS_MODE_CHANGE:
3828 case GOT_STATUS_CONFLICT:
3829 case GOT_STATUS_MISSING: {
3830 struct got_object_id id;
3831 if (staged_status == GOT_STATUS_ADD ||
3832 staged_status == GOT_STATUS_MODIFY) {
3833 memcpy(id.sha1, ie->staged_blob_sha1,
3834 SHA1_DIGEST_LENGTH);
3836 memcpy(id.sha1, ie->blob_sha1,
3837 SHA1_DIGEST_LENGTH);
3838 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3842 if (asprintf(&ondisk_path, "%s/%s",
3843 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3844 err = got_error_from_errno("asprintf");
3848 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3849 status == GOT_STATUS_CONFLICT)) {
3850 err = create_patched_content(&path_content, 1, &id,
3851 ondisk_path, dirfd, de_name, ie->path, a->repo,
3852 a->patch_cb, a->patch_arg);
3853 if (err || path_content == NULL)
3855 if (rename(path_content, ondisk_path) == -1) {
3856 err = got_error_from_errno3("rename",
3857 path_content, ondisk_path);
3861 err = install_blob(a->worktree, ondisk_path, ie->path,
3862 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3863 got_fileindex_perms_to_st(ie), blob, 0, 1,
3864 a->repo, a->progress_cb, a->progress_arg);
3867 if (status == GOT_STATUS_DELETE ||
3868 status == GOT_STATUS_MODE_CHANGE) {
3869 err = got_fileindex_entry_update(ie,
3870 ondisk_path, blob->id.sha1,
3871 a->worktree->base_commit_id->sha1, 1);
3887 got_object_blob_close(blob);
3889 got_object_tree_close(tree);
3894 const struct got_error *
3895 got_worktree_revert(struct got_worktree *worktree,
3896 struct got_pathlist_head *paths,
3897 got_worktree_checkout_cb progress_cb, void *progress_arg,
3898 got_worktree_patch_cb patch_cb, void *patch_arg,
3899 struct got_repository *repo)
3901 struct got_fileindex *fileindex = NULL;
3902 char *fileindex_path = NULL;
3903 const struct got_error *err = NULL, *unlockerr = NULL;
3904 const struct got_error *sync_err = NULL;
3905 struct got_pathlist_entry *pe;
3906 struct revert_file_args rfa;
3908 err = lock_worktree(worktree, LOCK_EX);
3912 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3916 rfa.worktree = worktree;
3917 rfa.fileindex = fileindex;
3918 rfa.progress_cb = progress_cb;
3919 rfa.progress_arg = progress_arg;
3920 rfa.patch_cb = patch_cb;
3921 rfa.patch_arg = patch_arg;
3923 TAILQ_FOREACH(pe, paths, entry) {
3924 err = worktree_status(worktree, pe->path, fileindex, repo,
3925 revert_file, &rfa, NULL, NULL, 0, 0);
3929 sync_err = sync_fileindex(fileindex, fileindex_path);
3930 if (sync_err && err == NULL)
3933 free(fileindex_path);
3935 got_fileindex_free(fileindex);
3936 unlockerr = lock_worktree(worktree, LOCK_SH);
3937 if (unlockerr && err == NULL)
3943 free_commitable(struct got_commitable *ct)
3946 free(ct->in_repo_path);
3947 free(ct->ondisk_path);
3949 free(ct->base_blob_id);
3950 free(ct->staged_blob_id);
3951 free(ct->base_commit_id);
3955 struct collect_commitables_arg {
3956 struct got_pathlist_head *commitable_paths;
3957 struct got_repository *repo;
3958 struct got_worktree *worktree;
3959 int have_staged_files;
3962 static const struct got_error *
3963 collect_commitables(void *arg, unsigned char status,
3964 unsigned char staged_status, const char *relpath,
3965 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3966 struct got_object_id *commit_id, int dirfd, const char *de_name)
3968 struct collect_commitables_arg *a = arg;
3969 const struct got_error *err = NULL;
3970 struct got_commitable *ct = NULL;
3971 struct got_pathlist_entry *new = NULL;
3972 char *parent_path = NULL, *path = NULL;
3975 if (a->have_staged_files) {
3976 if (staged_status != GOT_STATUS_MODIFY &&
3977 staged_status != GOT_STATUS_ADD &&
3978 staged_status != GOT_STATUS_DELETE)
3981 if (status == GOT_STATUS_CONFLICT)
3982 return got_error(GOT_ERR_COMMIT_CONFLICT);
3984 if (status != GOT_STATUS_MODIFY &&
3985 status != GOT_STATUS_MODE_CHANGE &&
3986 status != GOT_STATUS_ADD &&
3987 status != GOT_STATUS_DELETE)
3991 if (asprintf(&path, "/%s", relpath) == -1) {
3992 err = got_error_from_errno("asprintf");
3995 if (strcmp(path, "/") == 0) {
3996 parent_path = strdup("");
3997 if (parent_path == NULL)
3998 return got_error_from_errno("strdup");
4000 err = got_path_dirname(&parent_path, path);
4005 ct = calloc(1, sizeof(*ct));
4007 err = got_error_from_errno("calloc");
4011 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4013 err = got_error_from_errno("asprintf");
4016 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4017 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4020 if (fstatat(dirfd, de_name, &sb,
4021 AT_SYMLINK_NOFOLLOW) == -1) {
4022 err = got_error_from_errno2("fstatat",
4026 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4027 err = got_error_from_errno2("lstat", ct->ondisk_path);
4030 ct->mode = sb.st_mode;
4033 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4034 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4036 err = got_error_from_errno("asprintf");
4040 ct->status = status;
4041 ct->staged_status = staged_status;
4042 ct->blob_id = NULL; /* will be filled in when blob gets created */
4043 if (ct->status != GOT_STATUS_ADD &&
4044 ct->staged_status != GOT_STATUS_ADD) {
4045 ct->base_blob_id = got_object_id_dup(blob_id);
4046 if (ct->base_blob_id == NULL) {
4047 err = got_error_from_errno("got_object_id_dup");
4050 ct->base_commit_id = got_object_id_dup(commit_id);
4051 if (ct->base_commit_id == NULL) {
4052 err = got_error_from_errno("got_object_id_dup");
4056 if (ct->staged_status == GOT_STATUS_ADD ||
4057 ct->staged_status == GOT_STATUS_MODIFY) {
4058 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4059 if (ct->staged_blob_id == NULL) {
4060 err = got_error_from_errno("got_object_id_dup");
4064 ct->path = strdup(path);
4065 if (ct->path == NULL) {
4066 err = got_error_from_errno("strdup");
4069 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4071 if (ct && (err || new == NULL))
4072 free_commitable(ct);
4078 static const struct got_error *write_tree(struct got_object_id **, int *,
4079 struct got_tree_object *, const char *, struct got_pathlist_head *,
4080 got_worktree_status_cb status_cb, void *status_arg,
4081 struct got_repository *);
4083 static const struct got_error *
4084 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4085 struct got_tree_entry *te, const char *parent_path,
4086 struct got_pathlist_head *commitable_paths,
4087 got_worktree_status_cb status_cb, void *status_arg,
4088 struct got_repository *repo)
4090 const struct got_error *err = NULL;
4091 struct got_tree_object *subtree;
4094 if (asprintf(&subpath, "%s%s%s", parent_path,
4095 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4096 return got_error_from_errno("asprintf");
4098 err = got_object_open_as_tree(&subtree, repo, &te->id);
4102 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4103 commitable_paths, status_cb, status_arg, repo);
4104 got_object_tree_close(subtree);
4109 static const struct got_error *
4110 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4112 const struct got_error *err = NULL;
4113 char *ct_parent_path = NULL;
4117 if (strchr(ct->in_repo_path, '/') == NULL) {
4118 *match = got_path_is_root_dir(path);
4122 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4125 *match = (strcmp(path, ct_parent_path) == 0);
4126 free(ct_parent_path);
4131 get_ct_file_mode(struct got_commitable *ct)
4133 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4136 static const struct got_error *
4137 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4138 struct got_tree_entry *te, struct got_commitable *ct)
4140 const struct got_error *err = NULL;
4144 err = got_object_tree_entry_dup(new_te, te);
4148 (*new_te)->mode = get_ct_file_mode(ct);
4150 if (ct->staged_status == GOT_STATUS_MODIFY)
4151 memcpy(&(*new_te)->id, ct->staged_blob_id,
4152 sizeof((*new_te)->id));
4154 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4156 if (err && *new_te) {
4163 static const struct got_error *
4164 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4165 struct got_commitable *ct)
4167 const struct got_error *err = NULL;
4172 *new_te = calloc(1, sizeof(**new_te));
4173 if (*new_te == NULL)
4174 return got_error_from_errno("calloc");
4176 ct_name = basename(ct->path);
4177 if (ct_name == NULL) {
4178 err = got_error_from_errno2("basename", ct->path);
4181 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4182 sizeof((*new_te)->name)) {
4183 err = got_error(GOT_ERR_NO_SPACE);
4187 (*new_te)->mode = get_ct_file_mode(ct);
4189 if (ct->staged_status == GOT_STATUS_ADD)
4190 memcpy(&(*new_te)->id, ct->staged_blob_id,
4191 sizeof((*new_te)->id));
4193 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4195 if (err && *new_te) {
4202 static const struct got_error *
4203 insert_tree_entry(struct got_tree_entry *new_te,
4204 struct got_pathlist_head *paths)
4206 const struct got_error *err = NULL;
4207 struct got_pathlist_entry *new_pe;
4209 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4213 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4217 static const struct got_error *
4218 report_ct_status(struct got_commitable *ct,
4219 got_worktree_status_cb status_cb, void *status_arg)
4221 const char *ct_path = ct->path;
4222 unsigned char status;
4224 while (ct_path[0] == '/')
4227 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4228 status = ct->staged_status;
4230 status = ct->status;
4232 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4233 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4236 static const struct got_error *
4237 match_modified_subtree(int *modified, struct got_tree_entry *te,
4238 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4240 const struct got_error *err = NULL;
4241 struct got_pathlist_entry *pe;
4246 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4247 got_path_is_root_dir(base_tree_path) ? "" : "/",
4249 return got_error_from_errno("asprintf");
4251 TAILQ_FOREACH(pe, commitable_paths, entry) {
4252 struct got_commitable *ct = pe->data;
4253 *modified = got_path_is_child(ct->in_repo_path, te_path,
4263 static const struct got_error *
4264 match_deleted_or_modified_ct(struct got_commitable **ctp,
4265 struct got_tree_entry *te, const char *base_tree_path,
4266 struct got_pathlist_head *commitable_paths)
4268 const struct got_error *err = NULL;
4269 struct got_pathlist_entry *pe;
4273 TAILQ_FOREACH(pe, commitable_paths, entry) {
4274 struct got_commitable *ct = pe->data;
4275 char *ct_name = NULL;
4278 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4279 if (ct->status != GOT_STATUS_MODIFY &&
4280 ct->status != GOT_STATUS_MODE_CHANGE &&
4281 ct->status != GOT_STATUS_DELETE)
4284 if (ct->staged_status != GOT_STATUS_MODIFY &&
4285 ct->staged_status != GOT_STATUS_DELETE)
4289 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4292 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4298 ct_name = basename(pe->path);
4299 if (ct_name == NULL)
4300 return got_error_from_errno2("basename", pe->path);
4302 if (strcmp(te->name, ct_name) != 0)
4312 static const struct got_error *
4313 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4314 const char *child_path, const char *path_base_tree,
4315 struct got_pathlist_head *commitable_paths,
4316 got_worktree_status_cb status_cb, void *status_arg,
4317 struct got_repository *repo)
4319 const struct got_error *err = NULL;
4320 struct got_tree_entry *new_te;
4322 struct got_object_id *id = NULL;
4327 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4328 got_path_is_root_dir(path_base_tree) ? "" : "/",
4330 return got_error_from_errno("asprintf");
4332 new_te = calloc(1, sizeof(*new_te));
4334 return got_error_from_errno("calloc");
4335 new_te->mode = S_IFDIR;
4337 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4338 sizeof(new_te->name)) {
4339 err = got_error(GOT_ERR_NO_SPACE);
4342 err = write_tree(&id, &nentries, NULL, subtree_path,
4343 commitable_paths, status_cb, status_arg, repo);
4348 memcpy(&new_te->id, id, sizeof(new_te->id));
4357 static const struct got_error *
4358 write_tree(struct got_object_id **new_tree_id, int *nentries,
4359 struct got_tree_object *base_tree, const char *path_base_tree,
4360 struct got_pathlist_head *commitable_paths,
4361 got_worktree_status_cb status_cb, void *status_arg,
4362 struct got_repository *repo)
4364 const struct got_error *err = NULL;
4365 struct got_pathlist_head paths;
4366 struct got_tree_entry *te, *new_te = NULL;
4367 struct got_pathlist_entry *pe;
4372 /* Insert, and recurse into, newly added entries first. */
4373 TAILQ_FOREACH(pe, commitable_paths, entry) {
4374 struct got_commitable *ct = pe->data;
4375 char *child_path = NULL, *slash;
4377 if ((ct->status != GOT_STATUS_ADD &&
4378 ct->staged_status != GOT_STATUS_ADD) ||
4379 (ct->flags & GOT_COMMITABLE_ADDED))
4382 if (!got_path_is_child(pe->path, path_base_tree,
4383 strlen(path_base_tree)))
4386 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4391 slash = strchr(child_path, '/');
4392 if (slash == NULL) {
4393 err = alloc_added_blob_tree_entry(&new_te, ct);
4396 err = report_ct_status(ct, status_cb, status_arg);
4399 ct->flags |= GOT_COMMITABLE_ADDED;
4400 err = insert_tree_entry(new_te, &paths);
4405 *slash = '\0'; /* trim trailing path components */
4406 if (base_tree == NULL ||
4407 got_object_tree_find_entry(base_tree, child_path)
4409 err = make_subtree_for_added_blob(&new_te,
4410 child_path, path_base_tree,
4411 commitable_paths, status_cb, status_arg,
4415 err = insert_tree_entry(new_te, &paths);
4424 int i, nbase_entries;
4425 /* Handle modified and deleted entries. */
4426 nbase_entries = got_object_tree_get_nentries(base_tree);
4427 for (i = 0; i < nbase_entries; i++) {
4428 struct got_commitable *ct = NULL;
4430 te = got_object_tree_get_entry(base_tree, i);
4431 if (got_object_tree_entry_is_submodule(te)) {
4432 /* Entry is a submodule; just copy it. */
4433 err = got_object_tree_entry_dup(&new_te, te);
4436 err = insert_tree_entry(new_te, &paths);
4443 if (S_ISDIR(te->mode)) {
4445 err = got_object_tree_entry_dup(&new_te, te);
4448 err = match_modified_subtree(&modified, te,
4449 path_base_tree, commitable_paths);
4452 /* Avoid recursion into unmodified subtrees. */
4454 struct got_object_id *new_id;
4456 err = write_subtree(&new_id,
4458 path_base_tree, commitable_paths,
4459 status_cb, status_arg, repo);
4462 if (nsubentries == 0) {
4463 /* All entries were deleted. */
4467 memcpy(&new_te->id, new_id,
4468 sizeof(new_te->id));
4471 err = insert_tree_entry(new_te, &paths);
4478 err = match_deleted_or_modified_ct(&ct, te,
4479 path_base_tree, commitable_paths);
4483 /* NB: Deleted entries get dropped here. */
4484 if (ct->status == GOT_STATUS_MODIFY ||
4485 ct->status == GOT_STATUS_MODE_CHANGE ||
4486 ct->staged_status == GOT_STATUS_MODIFY) {
4487 err = alloc_modified_blob_tree_entry(
4491 err = insert_tree_entry(new_te, &paths);
4496 err = report_ct_status(ct, status_cb,
4501 /* Entry is unchanged; just copy it. */
4502 err = got_object_tree_entry_dup(&new_te, te);
4505 err = insert_tree_entry(new_te, &paths);
4513 /* Write new list of entries; deleted entries have been dropped. */
4514 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4516 got_pathlist_free(&paths);
4520 static const struct got_error *
4521 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4522 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4523 int have_staged_files)
4525 const struct got_error *err = NULL;
4526 struct got_pathlist_entry *pe;
4528 TAILQ_FOREACH(pe, commitable_paths, entry) {
4529 struct got_fileindex_entry *ie;
4530 struct got_commitable *ct = pe->data;
4532 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4534 if (ct->status == GOT_STATUS_DELETE ||
4535 ct->staged_status == GOT_STATUS_DELETE) {
4536 got_fileindex_entry_remove(fileindex, ie);
4537 } else if (ct->staged_status == GOT_STATUS_ADD ||
4538 ct->staged_status == GOT_STATUS_MODIFY) {
4539 got_fileindex_entry_stage_set(ie,
4540 GOT_FILEIDX_STAGE_NONE);
4541 err = got_fileindex_entry_update(ie,
4542 ct->ondisk_path, ct->staged_blob_id->sha1,
4543 new_base_commit_id->sha1,
4544 !have_staged_files);
4546 err = got_fileindex_entry_update(ie,
4547 ct->ondisk_path, ct->blob_id->sha1,
4548 new_base_commit_id->sha1,
4549 !have_staged_files);
4551 err = got_fileindex_entry_alloc(&ie, pe->path);
4554 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4555 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4557 got_fileindex_entry_free(ie);
4560 err = got_fileindex_entry_add(fileindex, ie);
4562 got_fileindex_entry_free(ie);
4571 static const struct got_error *
4572 check_out_of_date(const char *in_repo_path, unsigned char status,
4573 unsigned char staged_status, struct got_object_id *base_blob_id,
4574 struct got_object_id *base_commit_id,
4575 struct got_object_id *head_commit_id, struct got_repository *repo,
4578 const struct got_error *err = NULL;
4579 struct got_object_id *id = NULL;
4581 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4582 /* Trivial case: base commit == head commit */
4583 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4586 * Ensure file content which local changes were based
4587 * on matches file content in the branch head.
4589 err = got_object_id_by_path(&id, repo, head_commit_id,
4592 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4593 err = got_error(ood_errcode);
4595 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4596 err = got_error(ood_errcode);
4598 /* Require that added files don't exist in the branch head. */
4599 err = got_object_id_by_path(&id, repo, head_commit_id,
4601 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4603 err = id ? got_error(ood_errcode) : NULL;
4610 const struct got_error *
4611 commit_worktree(struct got_object_id **new_commit_id,
4612 struct got_pathlist_head *commitable_paths,
4613 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4614 const char *author, const char *committer,
4615 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4616 got_worktree_status_cb status_cb, void *status_arg,
4617 struct got_repository *repo)
4619 const struct got_error *err = NULL, *unlockerr = NULL;
4620 struct got_pathlist_entry *pe;
4621 const char *head_ref_name = NULL;
4622 struct got_commit_object *head_commit = NULL;
4623 struct got_reference *head_ref2 = NULL;
4624 struct got_object_id *head_commit_id2 = NULL;
4625 struct got_tree_object *head_tree = NULL;
4626 struct got_object_id *new_tree_id = NULL;
4628 struct got_object_id_queue parent_ids;
4629 struct got_object_qid *pid = NULL;
4630 char *logmsg = NULL;
4632 *new_commit_id = NULL;
4634 SIMPLEQ_INIT(&parent_ids);
4636 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4640 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4644 if (commit_msg_cb != NULL) {
4645 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4650 if (logmsg == NULL || strlen(logmsg) == 0) {
4651 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4655 /* Create blobs from added and modified files and record their IDs. */
4656 TAILQ_FOREACH(pe, commitable_paths, entry) {
4657 struct got_commitable *ct = pe->data;
4660 /* Blobs for staged files already exist. */
4661 if (ct->staged_status == GOT_STATUS_ADD ||
4662 ct->staged_status == GOT_STATUS_MODIFY)
4665 if (ct->status != GOT_STATUS_ADD &&
4666 ct->status != GOT_STATUS_MODIFY &&
4667 ct->status != GOT_STATUS_MODE_CHANGE)
4670 if (asprintf(&ondisk_path, "%s/%s",
4671 worktree->root_path, pe->path) == -1) {
4672 err = got_error_from_errno("asprintf");
4675 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4681 /* Recursively write new tree objects. */
4682 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4683 commitable_paths, status_cb, status_arg, repo);
4687 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4690 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4691 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4692 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4693 got_object_qid_free(pid);
4699 /* Check if a concurrent commit to our branch has occurred. */
4700 head_ref_name = got_worktree_get_head_ref_name(worktree);
4701 if (head_ref_name == NULL) {
4702 err = got_error_from_errno("got_worktree_get_head_ref_name");
4705 /* Lock the reference here to prevent concurrent modification. */
4706 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4709 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4712 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4713 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4716 /* Update branch head in repository. */
4717 err = got_ref_change_ref(head_ref2, *new_commit_id);
4720 err = got_ref_write(head_ref2, repo);
4724 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4728 err = ref_base_commit(worktree, repo);
4733 got_object_tree_close(head_tree);
4735 got_object_commit_close(head_commit);
4736 free(head_commit_id2);
4738 unlockerr = got_ref_unlock(head_ref2);
4739 if (unlockerr && err == NULL)
4741 got_ref_close(head_ref2);
4746 static const struct got_error *
4747 check_path_is_commitable(const char *path,
4748 struct got_pathlist_head *commitable_paths)
4750 struct got_pathlist_entry *cpe = NULL;
4751 size_t path_len = strlen(path);
4753 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4754 struct got_commitable *ct = cpe->data;
4755 const char *ct_path = ct->path;
4757 while (ct_path[0] == '/')
4760 if (strcmp(path, ct_path) == 0 ||
4761 got_path_is_child(ct_path, path, path_len))
4766 return got_error_path(path, GOT_ERR_BAD_PATH);
4771 static const struct got_error *
4772 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4774 int *have_staged_files = arg;
4776 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4777 *have_staged_files = 1;
4778 return got_error(GOT_ERR_CANCELLED);
4784 static const struct got_error *
4785 check_non_staged_files(struct got_fileindex *fileindex,
4786 struct got_pathlist_head *paths)
4788 struct got_pathlist_entry *pe;
4789 struct got_fileindex_entry *ie;
4791 TAILQ_FOREACH(pe, paths, entry) {
4792 if (pe->path[0] == '\0')
4794 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4796 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4797 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4798 return got_error_path(pe->path,
4799 GOT_ERR_FILE_NOT_STAGED);
4805 const struct got_error *
4806 got_worktree_commit(struct got_object_id **new_commit_id,
4807 struct got_worktree *worktree, struct got_pathlist_head *paths,
4808 const char *author, const char *committer,
4809 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4810 got_worktree_status_cb status_cb, void *status_arg,
4811 struct got_repository *repo)
4813 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4814 struct got_fileindex *fileindex = NULL;
4815 char *fileindex_path = NULL;
4816 struct got_pathlist_head commitable_paths;
4817 struct collect_commitables_arg cc_arg;
4818 struct got_pathlist_entry *pe;
4819 struct got_reference *head_ref = NULL;
4820 struct got_object_id *head_commit_id = NULL;
4821 int have_staged_files = 0;
4823 *new_commit_id = NULL;
4825 TAILQ_INIT(&commitable_paths);
4827 err = lock_worktree(worktree, LOCK_EX);
4831 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4835 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4839 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4843 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4844 &have_staged_files);
4845 if (err && err->code != GOT_ERR_CANCELLED)
4847 if (have_staged_files) {
4848 err = check_non_staged_files(fileindex, paths);
4853 cc_arg.commitable_paths = &commitable_paths;
4854 cc_arg.worktree = worktree;
4856 cc_arg.have_staged_files = have_staged_files;
4857 TAILQ_FOREACH(pe, paths, entry) {
4858 err = worktree_status(worktree, pe->path, fileindex, repo,
4859 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4864 if (TAILQ_EMPTY(&commitable_paths)) {
4865 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4869 TAILQ_FOREACH(pe, paths, entry) {
4870 err = check_path_is_commitable(pe->path, &commitable_paths);
4875 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4876 struct got_commitable *ct = pe->data;
4877 const char *ct_path = ct->in_repo_path;
4879 while (ct_path[0] == '/')
4881 err = check_out_of_date(ct_path, ct->status,
4882 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4883 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4889 err = commit_worktree(new_commit_id, &commitable_paths,
4890 head_commit_id, worktree, author, committer,
4891 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4895 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4896 fileindex, have_staged_files);
4897 sync_err = sync_fileindex(fileindex, fileindex_path);
4898 if (sync_err && err == NULL)
4902 got_fileindex_free(fileindex);
4903 free(fileindex_path);
4904 unlockerr = lock_worktree(worktree, LOCK_SH);
4905 if (unlockerr && err == NULL)
4907 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4908 struct got_commitable *ct = pe->data;
4909 free_commitable(ct);
4911 got_pathlist_free(&commitable_paths);
4916 got_commitable_get_path(struct got_commitable *ct)
4922 got_commitable_get_status(struct got_commitable *ct)
4927 struct check_rebase_ok_arg {
4928 struct got_worktree *worktree;
4929 struct got_repository *repo;
4932 static const struct got_error *
4933 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4935 const struct got_error *err = NULL;
4936 struct check_rebase_ok_arg *a = arg;
4937 unsigned char status;
4941 /* Reject rebase of a work tree with mixed base commits. */
4942 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4943 SHA1_DIGEST_LENGTH))
4944 return got_error(GOT_ERR_MIXED_COMMITS);
4946 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4948 return got_error_from_errno("asprintf");
4950 /* Reject rebase of a work tree with modified or staged files. */
4951 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4956 if (status != GOT_STATUS_NO_CHANGE)
4957 return got_error(GOT_ERR_MODIFIED);
4958 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4959 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4964 const struct got_error *
4965 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4966 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4967 struct got_worktree *worktree, struct got_reference *branch,
4968 struct got_repository *repo)
4970 const struct got_error *err = NULL;
4971 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4972 char *branch_ref_name = NULL;
4973 char *fileindex_path = NULL;
4974 struct check_rebase_ok_arg ok_arg;
4975 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4976 struct got_object_id *wt_branch_tip = NULL;
4978 *new_base_branch_ref = NULL;
4982 err = lock_worktree(worktree, LOCK_EX);
4986 err = open_fileindex(fileindex, &fileindex_path, worktree);
4990 ok_arg.worktree = worktree;
4992 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4997 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5001 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5005 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5009 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5014 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5017 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5018 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5022 err = got_ref_alloc_symref(new_base_branch_ref,
5023 new_base_branch_ref_name, wt_branch);
5026 err = got_ref_write(*new_base_branch_ref, repo);
5030 /* TODO Lock original branch's ref while rebasing? */
5032 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5036 err = got_ref_write(branch_ref, repo);
5040 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5041 worktree->base_commit_id);
5044 err = got_ref_write(*tmp_branch, repo);
5048 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5052 free(fileindex_path);
5053 free(tmp_branch_name);
5054 free(new_base_branch_ref_name);
5055 free(branch_ref_name);
5057 got_ref_close(branch_ref);
5059 got_ref_close(wt_branch);
5060 free(wt_branch_tip);
5062 if (*new_base_branch_ref) {
5063 got_ref_close(*new_base_branch_ref);
5064 *new_base_branch_ref = NULL;
5067 got_ref_close(*tmp_branch);
5071 got_fileindex_free(*fileindex);
5074 lock_worktree(worktree, LOCK_SH);
5079 const struct got_error *
5080 got_worktree_rebase_continue(struct got_object_id **commit_id,
5081 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5082 struct got_reference **branch, struct got_fileindex **fileindex,
5083 struct got_worktree *worktree, struct got_repository *repo)
5085 const struct got_error *err;
5086 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5087 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5088 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5089 char *fileindex_path = NULL;
5090 int have_staged_files = 0;
5093 *new_base_branch = NULL;
5098 err = lock_worktree(worktree, LOCK_EX);
5102 err = open_fileindex(fileindex, &fileindex_path, worktree);
5106 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5107 &have_staged_files);
5108 if (err && err->code != GOT_ERR_CANCELLED)
5110 if (have_staged_files) {
5111 err = got_error(GOT_ERR_STAGED_PATHS);
5115 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5119 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5123 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5127 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5131 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5135 err = got_ref_open(branch, repo,
5136 got_ref_get_symref_target(branch_ref), 0);
5140 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5144 err = got_ref_resolve(commit_id, repo, commit_ref);
5148 err = got_ref_open(new_base_branch, repo,
5149 new_base_branch_ref_name, 0);
5153 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5157 free(commit_ref_name);
5158 free(branch_ref_name);
5159 free(fileindex_path);
5161 got_ref_close(commit_ref);
5163 got_ref_close(branch_ref);
5168 got_ref_close(*tmp_branch);
5171 if (*new_base_branch) {
5172 got_ref_close(*new_base_branch);
5173 *new_base_branch = NULL;
5176 got_ref_close(*branch);
5180 got_fileindex_free(*fileindex);
5183 lock_worktree(worktree, LOCK_SH);
5188 const struct got_error *
5189 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5191 const struct got_error *err;
5192 char *tmp_branch_name = NULL;
5194 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5198 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5199 free(tmp_branch_name);
5203 static const struct got_error *
5204 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5205 char **logmsg, void *arg)
5211 static const struct got_error *
5212 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5213 const char *path, struct got_object_id *blob_id,
5214 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5215 int dirfd, const char *de_name)
5220 struct collect_merged_paths_arg {
5221 got_worktree_checkout_cb progress_cb;
5223 struct got_pathlist_head *merged_paths;
5226 static const struct got_error *
5227 collect_merged_paths(void *arg, unsigned char status, const char *path)
5229 const struct got_error *err;
5230 struct collect_merged_paths_arg *a = arg;
5232 struct got_pathlist_entry *new;
5234 err = (*a->progress_cb)(a->progress_arg, status, path);
5238 if (status != GOT_STATUS_MERGE &&
5239 status != GOT_STATUS_ADD &&
5240 status != GOT_STATUS_DELETE &&
5241 status != GOT_STATUS_CONFLICT)
5246 return got_error_from_errno("strdup");
5248 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5249 if (err || new == NULL)
5255 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5257 struct got_pathlist_entry *pe;
5259 TAILQ_FOREACH(pe, merged_paths, entry)
5260 free((char *)pe->path);
5262 got_pathlist_free(merged_paths);
5265 static const struct got_error *
5266 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5267 int is_rebase, struct got_repository *repo)
5269 const struct got_error *err;
5270 struct got_reference *commit_ref = NULL;
5272 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5274 if (err->code != GOT_ERR_NOT_REF)
5276 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5279 err = got_ref_write(commit_ref, repo);
5282 } else if (is_rebase) {
5283 struct got_object_id *stored_id;
5286 err = got_ref_resolve(&stored_id, repo, commit_ref);
5289 cmp = got_object_id_cmp(commit_id, stored_id);
5292 err = got_error(GOT_ERR_REBASE_COMMITID);
5298 got_ref_close(commit_ref);
5302 static const struct got_error *
5303 rebase_merge_files(struct got_pathlist_head *merged_paths,
5304 const char *commit_ref_name, struct got_worktree *worktree,
5305 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5306 struct got_object_id *commit_id, struct got_repository *repo,
5307 got_worktree_checkout_cb progress_cb, void *progress_arg,
5308 got_cancel_cb cancel_cb, void *cancel_arg)
5310 const struct got_error *err;
5311 struct got_reference *commit_ref = NULL;
5312 struct collect_merged_paths_arg cmp_arg;
5313 char *fileindex_path;
5315 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5317 err = get_fileindex_path(&fileindex_path, worktree);
5321 cmp_arg.progress_cb = progress_cb;
5322 cmp_arg.progress_arg = progress_arg;
5323 cmp_arg.merged_paths = merged_paths;
5324 err = merge_files(worktree, fileindex, fileindex_path,
5325 parent_commit_id, commit_id, repo, collect_merged_paths,
5326 &cmp_arg, cancel_cb, cancel_arg);
5328 got_ref_close(commit_ref);
5332 const struct got_error *
5333 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5334 struct got_worktree *worktree, struct got_fileindex *fileindex,
5335 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5336 struct got_repository *repo,
5337 got_worktree_checkout_cb progress_cb, void *progress_arg,
5338 got_cancel_cb cancel_cb, void *cancel_arg)
5340 const struct got_error *err;
5341 char *commit_ref_name;
5343 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5347 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5351 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5352 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5353 progress_arg, cancel_cb, cancel_arg);
5355 free(commit_ref_name);
5359 const struct got_error *
5360 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5361 struct got_worktree *worktree, struct got_fileindex *fileindex,
5362 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5363 struct got_repository *repo,
5364 got_worktree_checkout_cb progress_cb, void *progress_arg,
5365 got_cancel_cb cancel_cb, void *cancel_arg)
5367 const struct got_error *err;
5368 char *commit_ref_name;
5370 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5374 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5378 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5379 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5380 progress_arg, cancel_cb, cancel_arg);
5382 free(commit_ref_name);
5386 static const struct got_error *
5387 rebase_commit(struct got_object_id **new_commit_id,
5388 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5389 struct got_worktree *worktree, struct got_fileindex *fileindex,
5390 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5391 const char *new_logmsg, struct got_repository *repo)
5393 const struct got_error *err, *sync_err;
5394 struct got_pathlist_head commitable_paths;
5395 struct collect_commitables_arg cc_arg;
5396 char *fileindex_path = NULL;
5397 struct got_reference *head_ref = NULL;
5398 struct got_object_id *head_commit_id = NULL;
5399 char *logmsg = NULL;
5401 TAILQ_INIT(&commitable_paths);
5402 *new_commit_id = NULL;
5404 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5406 err = get_fileindex_path(&fileindex_path, worktree);
5410 cc_arg.commitable_paths = &commitable_paths;
5411 cc_arg.worktree = worktree;
5413 cc_arg.have_staged_files = 0;
5415 * If possible get the status of individual files directly to
5416 * avoid crawling the entire work tree once per rebased commit.
5417 * TODO: Ideally, merged_paths would contain a list of commitables
5418 * we could use so we could skip worktree_status() entirely.
5421 struct got_pathlist_entry *pe;
5422 TAILQ_FOREACH(pe, merged_paths, entry) {
5423 err = worktree_status(worktree, pe->path, fileindex,
5424 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5430 err = worktree_status(worktree, "", fileindex, repo,
5431 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5436 if (TAILQ_EMPTY(&commitable_paths)) {
5437 /* No-op change; commit will be elided. */
5438 err = got_ref_delete(commit_ref, repo);
5441 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5445 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5449 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5454 logmsg = strdup(new_logmsg);
5455 if (logmsg == NULL) {
5456 err = got_error_from_errno("strdup");
5460 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5465 /* NB: commit_worktree will call free(logmsg) */
5466 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5467 worktree, got_object_commit_get_author(orig_commit),
5468 got_object_commit_get_committer(orig_commit),
5469 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5473 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5477 err = got_ref_delete(commit_ref, repo);
5481 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5483 sync_err = sync_fileindex(fileindex, fileindex_path);
5484 if (sync_err && err == NULL)
5487 free(fileindex_path);
5488 free(head_commit_id);
5490 got_ref_close(head_ref);
5492 free(*new_commit_id);
5493 *new_commit_id = NULL;
5498 const struct got_error *
5499 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5500 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5501 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5502 struct got_commit_object *orig_commit,
5503 struct got_object_id *orig_commit_id, struct got_repository *repo)
5505 const struct got_error *err;
5506 char *commit_ref_name;
5507 struct got_reference *commit_ref = NULL;
5508 struct got_object_id *commit_id = NULL;
5510 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5514 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5517 err = got_ref_resolve(&commit_id, repo, commit_ref);
5520 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5521 err = got_error(GOT_ERR_REBASE_COMMITID);
5525 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5526 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5529 got_ref_close(commit_ref);
5530 free(commit_ref_name);
5535 const struct got_error *
5536 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5537 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5538 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5539 struct got_commit_object *orig_commit,
5540 struct got_object_id *orig_commit_id, const char *new_logmsg,
5541 struct got_repository *repo)
5543 const struct got_error *err;
5544 char *commit_ref_name;
5545 struct got_reference *commit_ref = NULL;
5547 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5551 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5555 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5556 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5559 got_ref_close(commit_ref);
5560 free(commit_ref_name);
5564 const struct got_error *
5565 got_worktree_rebase_postpone(struct got_worktree *worktree,
5566 struct got_fileindex *fileindex)
5569 got_fileindex_free(fileindex);
5570 return lock_worktree(worktree, LOCK_SH);
5573 static const struct got_error *
5574 delete_ref(const char *name, struct got_repository *repo)
5576 const struct got_error *err;
5577 struct got_reference *ref;
5579 err = got_ref_open(&ref, repo, name, 0);
5581 if (err->code == GOT_ERR_NOT_REF)
5586 err = got_ref_delete(ref, repo);
5591 static const struct got_error *
5592 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5594 const struct got_error *err;
5595 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5596 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5598 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5601 err = delete_ref(tmp_branch_name, repo);
5605 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5608 err = delete_ref(new_base_branch_ref_name, repo);
5612 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5615 err = delete_ref(branch_ref_name, repo);
5619 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5622 err = delete_ref(commit_ref_name, repo);
5627 free(tmp_branch_name);
5628 free(new_base_branch_ref_name);
5629 free(branch_ref_name);
5630 free(commit_ref_name);
5634 const struct got_error *
5635 got_worktree_rebase_complete(struct got_worktree *worktree,
5636 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5637 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5638 struct got_repository *repo)
5640 const struct got_error *err, *unlockerr;
5641 struct got_object_id *new_head_commit_id = NULL;
5643 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5647 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5651 err = got_ref_write(rebased_branch, repo);
5655 err = got_worktree_set_head_ref(worktree, rebased_branch);
5659 err = delete_rebase_refs(worktree, repo);
5662 got_fileindex_free(fileindex);
5663 free(new_head_commit_id);
5664 unlockerr = lock_worktree(worktree, LOCK_SH);
5665 if (unlockerr && err == NULL)
5670 const struct got_error *
5671 got_worktree_rebase_abort(struct got_worktree *worktree,
5672 struct got_fileindex *fileindex, struct got_repository *repo,
5673 struct got_reference *new_base_branch,
5674 got_worktree_checkout_cb progress_cb, void *progress_arg)
5676 const struct got_error *err, *unlockerr, *sync_err;
5677 struct got_reference *resolved = NULL;
5678 struct got_object_id *commit_id = NULL;
5679 char *fileindex_path = NULL;
5680 struct revert_file_args rfa;
5681 struct got_object_id *tree_id = NULL;
5683 err = lock_worktree(worktree, LOCK_EX);
5687 err = got_ref_open(&resolved, repo,
5688 got_ref_get_symref_target(new_base_branch), 0);
5692 err = got_worktree_set_head_ref(worktree, resolved);
5697 * XXX commits to the base branch could have happened while
5698 * we were busy rebasing; should we store the original commit ID
5699 * when rebase begins and read it back here?
5701 err = got_ref_resolve(&commit_id, repo, resolved);
5705 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5709 err = got_object_id_by_path(&tree_id, repo,
5710 worktree->base_commit_id, worktree->path_prefix);
5714 err = delete_rebase_refs(worktree, repo);
5718 err = get_fileindex_path(&fileindex_path, worktree);
5722 rfa.worktree = worktree;
5723 rfa.fileindex = fileindex;
5724 rfa.progress_cb = progress_cb;
5725 rfa.progress_arg = progress_arg;
5726 rfa.patch_cb = NULL;
5727 rfa.patch_arg = NULL;
5729 err = worktree_status(worktree, "", fileindex, repo,
5730 revert_file, &rfa, NULL, NULL, 0, 0);
5734 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5735 repo, progress_cb, progress_arg, NULL, NULL);
5737 sync_err = sync_fileindex(fileindex, fileindex_path);
5738 if (sync_err && err == NULL)
5741 got_ref_close(resolved);
5745 got_fileindex_free(fileindex);
5746 free(fileindex_path);
5748 unlockerr = lock_worktree(worktree, LOCK_SH);
5749 if (unlockerr && err == NULL)
5754 const struct got_error *
5755 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5756 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5757 struct got_fileindex **fileindex, struct got_worktree *worktree,
5758 struct got_repository *repo)
5760 const struct got_error *err = NULL;
5761 char *tmp_branch_name = NULL;
5762 char *branch_ref_name = NULL;
5763 char *base_commit_ref_name = NULL;
5764 char *fileindex_path = NULL;
5765 struct check_rebase_ok_arg ok_arg;
5766 struct got_reference *wt_branch = NULL;
5767 struct got_reference *base_commit_ref = NULL;
5771 *base_commit_id = NULL;
5774 err = lock_worktree(worktree, LOCK_EX);
5778 err = open_fileindex(fileindex, &fileindex_path, worktree);
5782 ok_arg.worktree = worktree;
5784 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5789 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5793 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5797 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5802 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5807 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5811 err = got_ref_write(*branch_ref, repo);
5815 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5816 worktree->base_commit_id);
5819 err = got_ref_write(base_commit_ref, repo);
5822 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5823 if (*base_commit_id == NULL) {
5824 err = got_error_from_errno("got_object_id_dup");
5828 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5829 worktree->base_commit_id);
5832 err = got_ref_write(*tmp_branch, repo);
5836 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5840 free(fileindex_path);
5841 free(tmp_branch_name);
5842 free(branch_ref_name);
5843 free(base_commit_ref_name);
5845 got_ref_close(wt_branch);
5848 got_ref_close(*branch_ref);
5852 got_ref_close(*tmp_branch);
5855 free(*base_commit_id);
5857 got_fileindex_free(*fileindex);
5860 lock_worktree(worktree, LOCK_SH);
5865 const struct got_error *
5866 got_worktree_histedit_postpone(struct got_worktree *worktree,
5867 struct got_fileindex *fileindex)
5870 got_fileindex_free(fileindex);
5871 return lock_worktree(worktree, LOCK_SH);
5874 const struct got_error *
5875 got_worktree_histedit_in_progress(int *in_progress,
5876 struct got_worktree *worktree)
5878 const struct got_error *err;
5879 char *tmp_branch_name = NULL;
5881 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5885 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5886 free(tmp_branch_name);
5890 const struct got_error *
5891 got_worktree_histedit_continue(struct got_object_id **commit_id,
5892 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5893 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5894 struct got_worktree *worktree, struct got_repository *repo)
5896 const struct got_error *err;
5897 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5898 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5899 struct got_reference *commit_ref = NULL;
5900 struct got_reference *base_commit_ref = NULL;
5901 char *fileindex_path = NULL;
5902 int have_staged_files = 0;
5906 *base_commit_id = NULL;
5909 err = lock_worktree(worktree, LOCK_EX);
5913 err = open_fileindex(fileindex, &fileindex_path, worktree);
5917 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5918 &have_staged_files);
5919 if (err && err->code != GOT_ERR_CANCELLED)
5921 if (have_staged_files) {
5922 err = got_error(GOT_ERR_STAGED_PATHS);
5926 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5930 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5934 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5938 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5943 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5947 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5950 err = got_ref_resolve(commit_id, repo, commit_ref);
5954 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5957 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5961 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5965 free(commit_ref_name);
5966 free(branch_ref_name);
5967 free(fileindex_path);
5969 got_ref_close(commit_ref);
5970 if (base_commit_ref)
5971 got_ref_close(base_commit_ref);
5975 free(*base_commit_id);
5976 *base_commit_id = NULL;
5978 got_ref_close(*tmp_branch);
5982 got_fileindex_free(*fileindex);
5985 lock_worktree(worktree, LOCK_EX);
5990 static const struct got_error *
5991 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5993 const struct got_error *err;
5994 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5995 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5997 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6000 err = delete_ref(tmp_branch_name, repo);
6004 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6008 err = delete_ref(base_commit_ref_name, repo);
6012 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6015 err = delete_ref(branch_ref_name, repo);
6019 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6022 err = delete_ref(commit_ref_name, repo);
6026 free(tmp_branch_name);
6027 free(base_commit_ref_name);
6028 free(branch_ref_name);
6029 free(commit_ref_name);
6033 const struct got_error *
6034 got_worktree_histedit_abort(struct got_worktree *worktree,
6035 struct got_fileindex *fileindex, struct got_repository *repo,
6036 struct got_reference *branch, struct got_object_id *base_commit_id,
6037 got_worktree_checkout_cb progress_cb, void *progress_arg)
6039 const struct got_error *err, *unlockerr, *sync_err;
6040 struct got_reference *resolved = NULL;
6041 char *fileindex_path = NULL;
6042 struct got_object_id *tree_id = NULL;
6043 struct revert_file_args rfa;
6045 err = lock_worktree(worktree, LOCK_EX);
6049 err = got_ref_open(&resolved, repo,
6050 got_ref_get_symref_target(branch), 0);
6054 err = got_worktree_set_head_ref(worktree, resolved);
6058 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6062 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6063 worktree->path_prefix);
6067 err = delete_histedit_refs(worktree, repo);
6071 err = get_fileindex_path(&fileindex_path, worktree);
6075 rfa.worktree = worktree;
6076 rfa.fileindex = fileindex;
6077 rfa.progress_cb = progress_cb;
6078 rfa.progress_arg = progress_arg;
6079 rfa.patch_cb = NULL;
6080 rfa.patch_arg = NULL;
6082 err = worktree_status(worktree, "", fileindex, repo,
6083 revert_file, &rfa, NULL, NULL, 0, 0);
6087 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6088 repo, progress_cb, progress_arg, NULL, NULL);
6090 sync_err = sync_fileindex(fileindex, fileindex_path);
6091 if (sync_err && err == NULL)
6094 got_ref_close(resolved);
6096 free(fileindex_path);
6098 unlockerr = lock_worktree(worktree, LOCK_SH);
6099 if (unlockerr && err == NULL)
6104 const struct got_error *
6105 got_worktree_histedit_complete(struct got_worktree *worktree,
6106 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6107 struct got_reference *edited_branch, struct got_repository *repo)
6109 const struct got_error *err, *unlockerr;
6110 struct got_object_id *new_head_commit_id = NULL;
6111 struct got_reference *resolved = NULL;
6113 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6117 err = got_ref_open(&resolved, repo,
6118 got_ref_get_symref_target(edited_branch), 0);
6122 err = got_ref_change_ref(resolved, new_head_commit_id);
6126 err = got_ref_write(resolved, repo);
6130 err = got_worktree_set_head_ref(worktree, resolved);
6134 err = delete_histedit_refs(worktree, repo);
6137 got_fileindex_free(fileindex);
6138 free(new_head_commit_id);
6139 unlockerr = lock_worktree(worktree, LOCK_SH);
6140 if (unlockerr && err == NULL)
6145 const struct got_error *
6146 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6147 struct got_object_id *commit_id, struct got_repository *repo)
6149 const struct got_error *err;
6150 char *commit_ref_name;
6152 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6156 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6160 err = delete_ref(commit_ref_name, repo);
6162 free(commit_ref_name);
6166 const struct got_error *
6167 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6168 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6169 struct got_worktree *worktree, const char *refname,
6170 struct got_repository *repo)
6172 const struct got_error *err = NULL;
6173 char *fileindex_path = NULL;
6174 struct check_rebase_ok_arg ok_arg;
6178 *base_branch_ref = NULL;
6180 err = lock_worktree(worktree, LOCK_EX);
6184 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6185 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6186 "cannot integrate a branch into itself; "
6187 "update -b or different branch name required");
6191 err = open_fileindex(fileindex, &fileindex_path, worktree);
6195 /* Preconditions are the same as for rebase. */
6196 ok_arg.worktree = worktree;
6198 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6203 err = got_ref_open(branch_ref, repo, refname, 1);
6207 err = got_ref_open(base_branch_ref, repo,
6208 got_worktree_get_head_ref_name(worktree), 1);
6212 got_ref_close(*branch_ref);
6215 if (*base_branch_ref) {
6216 got_ref_close(*base_branch_ref);
6217 *base_branch_ref = NULL;
6220 got_fileindex_free(*fileindex);
6223 lock_worktree(worktree, LOCK_SH);
6228 const struct got_error *
6229 got_worktree_integrate_continue(struct got_worktree *worktree,
6230 struct got_fileindex *fileindex, struct got_repository *repo,
6231 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6232 got_worktree_checkout_cb progress_cb, void *progress_arg,
6233 got_cancel_cb cancel_cb, void *cancel_arg)
6235 const struct got_error *err = NULL, *sync_err, *unlockerr;
6236 char *fileindex_path = NULL;
6237 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6239 err = get_fileindex_path(&fileindex_path, worktree);
6243 err = got_ref_resolve(&commit_id, repo, branch_ref);
6247 err = got_object_id_by_path(&tree_id, repo, commit_id,
6248 worktree->path_prefix);
6252 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6256 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6257 progress_cb, progress_arg, cancel_cb, cancel_arg);
6261 err = got_ref_change_ref(base_branch_ref, commit_id);
6265 err = got_ref_write(base_branch_ref, repo);
6267 sync_err = sync_fileindex(fileindex, fileindex_path);
6268 if (sync_err && err == NULL)
6272 unlockerr = got_ref_unlock(branch_ref);
6273 if (unlockerr && err == NULL)
6275 got_ref_close(branch_ref);
6277 unlockerr = got_ref_unlock(base_branch_ref);
6278 if (unlockerr && err == NULL)
6280 got_ref_close(base_branch_ref);
6282 got_fileindex_free(fileindex);
6283 free(fileindex_path);
6286 unlockerr = lock_worktree(worktree, LOCK_SH);
6287 if (unlockerr && err == NULL)
6292 const struct got_error *
6293 got_worktree_integrate_abort(struct got_worktree *worktree,
6294 struct got_fileindex *fileindex, struct got_repository *repo,
6295 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6297 const struct got_error *err = NULL, *unlockerr = NULL;
6299 got_fileindex_free(fileindex);
6301 err = lock_worktree(worktree, LOCK_SH);
6303 unlockerr = got_ref_unlock(branch_ref);
6304 if (unlockerr && err == NULL)
6306 got_ref_close(branch_ref);
6308 unlockerr = got_ref_unlock(base_branch_ref);
6309 if (unlockerr && err == NULL)
6311 got_ref_close(base_branch_ref);
6316 struct check_stage_ok_arg {
6317 struct got_object_id *head_commit_id;
6318 struct got_worktree *worktree;
6319 struct got_fileindex *fileindex;
6320 struct got_repository *repo;
6324 const struct got_error *
6325 check_stage_ok(void *arg, unsigned char status,
6326 unsigned char staged_status, const char *relpath,
6327 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6328 struct got_object_id *commit_id, int dirfd, const char *de_name)
6330 struct check_stage_ok_arg *a = arg;
6331 const struct got_error *err = NULL;
6332 struct got_fileindex_entry *ie;
6333 struct got_object_id base_commit_id;
6334 struct got_object_id *base_commit_idp = NULL;
6335 char *in_repo_path = NULL, *p;
6337 if (status == GOT_STATUS_UNVERSIONED ||
6338 status == GOT_STATUS_NO_CHANGE)
6340 if (status == GOT_STATUS_NONEXISTENT)
6341 return got_error_set_errno(ENOENT, relpath);
6343 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6345 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6347 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6348 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6350 return got_error_from_errno("asprintf");
6352 if (got_fileindex_entry_has_commit(ie)) {
6353 memcpy(base_commit_id.sha1, ie->commit_sha1,
6354 SHA1_DIGEST_LENGTH);
6355 base_commit_idp = &base_commit_id;
6358 if (status == GOT_STATUS_CONFLICT) {
6359 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6361 } else if (status != GOT_STATUS_ADD &&
6362 status != GOT_STATUS_MODIFY &&
6363 status != GOT_STATUS_DELETE) {
6364 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6368 a->have_changes = 1;
6373 err = check_out_of_date(p, status, staged_status,
6374 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6375 GOT_ERR_STAGE_OUT_OF_DATE);
6381 struct stage_path_arg {
6382 struct got_worktree *worktree;
6383 struct got_fileindex *fileindex;
6384 struct got_repository *repo;
6385 got_worktree_status_cb status_cb;
6387 got_worktree_patch_cb patch_cb;
6389 int staged_something;
6392 static const struct got_error *
6393 stage_path(void *arg, unsigned char status,
6394 unsigned char staged_status, const char *relpath,
6395 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6396 struct got_object_id *commit_id, int dirfd, const char *de_name)
6398 struct stage_path_arg *a = arg;
6399 const struct got_error *err = NULL;
6400 struct got_fileindex_entry *ie;
6401 char *ondisk_path = NULL, *path_content = NULL;
6403 struct got_object_id *new_staged_blob_id = NULL;
6405 if (status == GOT_STATUS_UNVERSIONED)
6408 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6410 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6412 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6414 return got_error_from_errno("asprintf");
6417 case GOT_STATUS_ADD:
6418 case GOT_STATUS_MODIFY:
6420 if (status == GOT_STATUS_ADD) {
6421 int choice = GOT_PATCH_CHOICE_NONE;
6422 err = (*a->patch_cb)(&choice, a->patch_arg,
6423 status, ie->path, NULL, 1, 1);
6426 if (choice != GOT_PATCH_CHOICE_YES)
6429 err = create_patched_content(&path_content, 0,
6430 staged_blob_id ? staged_blob_id : blob_id,
6431 ondisk_path, dirfd, de_name, ie->path,
6432 a->repo, a->patch_cb, a->patch_arg);
6433 if (err || path_content == NULL)
6437 err = got_object_blob_create(&new_staged_blob_id,
6438 path_content ? path_content : ondisk_path, a->repo);
6441 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6442 SHA1_DIGEST_LENGTH);
6443 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6444 stage = GOT_FILEIDX_STAGE_ADD;
6446 stage = GOT_FILEIDX_STAGE_MODIFY;
6447 got_fileindex_entry_stage_set(ie, stage);
6448 a->staged_something = 1;
6449 if (a->status_cb == NULL)
6451 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6452 get_staged_status(ie), relpath, blob_id,
6453 new_staged_blob_id, NULL, dirfd, de_name);
6455 case GOT_STATUS_DELETE:
6456 if (staged_status == GOT_STATUS_DELETE)
6459 int choice = GOT_PATCH_CHOICE_NONE;
6460 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6461 ie->path, NULL, 1, 1);
6464 if (choice == GOT_PATCH_CHOICE_NO)
6466 if (choice != GOT_PATCH_CHOICE_YES) {
6467 err = got_error(GOT_ERR_PATCH_CHOICE);
6471 stage = GOT_FILEIDX_STAGE_DELETE;
6472 got_fileindex_entry_stage_set(ie, stage);
6473 a->staged_something = 1;
6474 if (a->status_cb == NULL)
6476 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6477 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6480 case GOT_STATUS_NO_CHANGE:
6482 case GOT_STATUS_CONFLICT:
6483 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6485 case GOT_STATUS_NONEXISTENT:
6486 err = got_error_set_errno(ENOENT, relpath);
6489 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6493 if (path_content && unlink(path_content) == -1 && err == NULL)
6494 err = got_error_from_errno2("unlink", path_content);
6497 free(new_staged_blob_id);
6501 const struct got_error *
6502 got_worktree_stage(struct got_worktree *worktree,
6503 struct got_pathlist_head *paths,
6504 got_worktree_status_cb status_cb, void *status_arg,
6505 got_worktree_patch_cb patch_cb, void *patch_arg,
6506 struct got_repository *repo)
6508 const struct got_error *err = NULL, *sync_err, *unlockerr;
6509 struct got_pathlist_entry *pe;
6510 struct got_fileindex *fileindex = NULL;
6511 char *fileindex_path = NULL;
6512 struct got_reference *head_ref = NULL;
6513 struct got_object_id *head_commit_id = NULL;
6514 struct check_stage_ok_arg oka;
6515 struct stage_path_arg spa;
6517 err = lock_worktree(worktree, LOCK_EX);
6521 err = got_ref_open(&head_ref, repo,
6522 got_worktree_get_head_ref_name(worktree), 0);
6525 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6528 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6532 /* Check pre-conditions before staging anything. */
6533 oka.head_commit_id = head_commit_id;
6534 oka.worktree = worktree;
6535 oka.fileindex = fileindex;
6537 oka.have_changes = 0;
6538 TAILQ_FOREACH(pe, paths, entry) {
6539 err = worktree_status(worktree, pe->path, fileindex, repo,
6540 check_stage_ok, &oka, NULL, NULL, 0, 0);
6544 if (!oka.have_changes) {
6545 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6549 spa.worktree = worktree;
6550 spa.fileindex = fileindex;
6552 spa.patch_cb = patch_cb;
6553 spa.patch_arg = patch_arg;
6554 spa.status_cb = status_cb;
6555 spa.status_arg = status_arg;
6556 spa.staged_something = 0;
6557 TAILQ_FOREACH(pe, paths, entry) {
6558 err = worktree_status(worktree, pe->path, fileindex, repo,
6559 stage_path, &spa, NULL, NULL, 0, 0);
6563 if (!spa.staged_something) {
6564 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6568 sync_err = sync_fileindex(fileindex, fileindex_path);
6569 if (sync_err && err == NULL)
6573 got_ref_close(head_ref);
6574 free(head_commit_id);
6575 free(fileindex_path);
6577 got_fileindex_free(fileindex);
6578 unlockerr = lock_worktree(worktree, LOCK_SH);
6579 if (unlockerr && err == NULL)
6584 struct unstage_path_arg {
6585 struct got_worktree *worktree;
6586 struct got_fileindex *fileindex;
6587 struct got_repository *repo;
6588 got_worktree_checkout_cb progress_cb;
6590 got_worktree_patch_cb patch_cb;
6594 static const struct got_error *
6595 create_unstaged_content(char **path_unstaged_content,
6596 char **path_new_staged_content, struct got_object_id *blob_id,
6597 struct got_object_id *staged_blob_id, const char *relpath,
6598 struct got_repository *repo,
6599 got_worktree_patch_cb patch_cb, void *patch_arg)
6601 const struct got_error *err;
6602 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6603 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6604 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6605 struct stat sb1, sb2;
6606 struct got_diff_changes *changes = NULL;
6607 struct got_diff_state *ds = NULL;
6608 struct got_diff_args *args = NULL;
6609 struct got_diff_change *change;
6610 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6611 int have_content = 0, have_rejected_content = 0;
6613 *path_unstaged_content = NULL;
6614 *path_new_staged_content = NULL;
6616 err = got_object_id_str(&label1, blob_id);
6619 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6623 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6627 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6631 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6635 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6639 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6643 if (stat(path1, &sb1) == -1) {
6644 err = got_error_from_errno2("stat", path1);
6648 if (stat(path2, &sb2) == -1) {
6649 err = got_error_from_errno2("stat", path2);
6653 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6654 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6658 err = got_opentemp_named(path_unstaged_content, &outfile,
6659 "got-unstaged-content");
6662 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6663 "got-new-staged-content");
6667 if (fseek(f1, 0L, SEEK_SET) == -1) {
6668 err = got_ferror(f1, GOT_ERR_IO);
6671 if (fseek(f2, 0L, SEEK_SET) == -1) {
6672 err = got_ferror(f2, GOT_ERR_IO);
6675 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6677 err = apply_or_reject_change(&choice, change, ++n,
6678 changes->nchanges, ds, args, diff_flags, relpath,
6679 f1, f2, &line_cur1, &line_cur2,
6680 outfile, rejectfile, patch_cb, patch_arg);
6683 if (choice == GOT_PATCH_CHOICE_YES)
6686 have_rejected_content = 1;
6687 if (choice == GOT_PATCH_CHOICE_QUIT)
6690 if (have_content || have_rejected_content)
6691 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6692 outfile, rejectfile);
6696 got_object_blob_close(blob);
6698 got_object_blob_close(staged_blob);
6699 if (f1 && fclose(f1) == EOF && err == NULL)
6700 err = got_error_from_errno2("fclose", path1);
6701 if (f2 && fclose(f2) == EOF && err == NULL)
6702 err = got_error_from_errno2("fclose", path2);
6703 if (outfile && fclose(outfile) == EOF && err == NULL)
6704 err = got_error_from_errno2("fclose", *path_unstaged_content);
6705 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6706 err = got_error_from_errno2("fclose", *path_new_staged_content);
6707 if (path1 && unlink(path1) == -1 && err == NULL)
6708 err = got_error_from_errno2("unlink", path1);
6709 if (path2 && unlink(path2) == -1 && err == NULL)
6710 err = got_error_from_errno2("unlink", path2);
6711 if (err || !have_content) {
6712 if (*path_unstaged_content &&
6713 unlink(*path_unstaged_content) == -1 && err == NULL)
6714 err = got_error_from_errno2("unlink",
6715 *path_unstaged_content);
6716 free(*path_unstaged_content);
6717 *path_unstaged_content = NULL;
6719 if (err || !have_rejected_content) {
6720 if (*path_new_staged_content &&
6721 unlink(*path_new_staged_content) == -1 && err == NULL)
6722 err = got_error_from_errno2("unlink",
6723 *path_new_staged_content);
6724 free(*path_new_staged_content);
6725 *path_new_staged_content = NULL;
6729 got_diff_state_free(ds);
6733 got_diff_free_changes(changes);
6739 static const struct got_error *
6740 unstage_path(void *arg, unsigned char status,
6741 unsigned char staged_status, const char *relpath,
6742 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6743 struct got_object_id *commit_id, int dirfd, const char *de_name)
6745 const struct got_error *err = NULL;
6746 struct unstage_path_arg *a = arg;
6747 struct got_fileindex_entry *ie;
6748 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6749 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6750 char *path_new_staged_content = NULL;
6751 char *id_str = NULL, *label_orig = NULL;
6752 int local_changes_subsumed;
6755 if (staged_status != GOT_STATUS_ADD &&
6756 staged_status != GOT_STATUS_MODIFY &&
6757 staged_status != GOT_STATUS_DELETE)
6760 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6762 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6764 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6766 return got_error_from_errno("asprintf");
6768 err = got_object_id_str(&id_str,
6769 commit_id ? commit_id : a->worktree->base_commit_id);
6772 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6774 err = got_error_from_errno("asprintf");
6778 switch (staged_status) {
6779 case GOT_STATUS_MODIFY:
6780 err = got_object_open_as_blob(&blob_base, a->repo,
6785 case GOT_STATUS_ADD:
6787 if (staged_status == GOT_STATUS_ADD) {
6788 int choice = GOT_PATCH_CHOICE_NONE;
6789 err = (*a->patch_cb)(&choice, a->patch_arg,
6790 staged_status, ie->path, NULL, 1, 1);
6793 if (choice != GOT_PATCH_CHOICE_YES)
6796 err = create_unstaged_content(
6797 &path_unstaged_content,
6798 &path_new_staged_content, blob_id,
6799 staged_blob_id, ie->path, a->repo,
6800 a->patch_cb, a->patch_arg);
6801 if (err || path_unstaged_content == NULL)
6803 if (path_new_staged_content) {
6804 err = got_object_blob_create(
6806 path_new_staged_content,
6810 memcpy(ie->staged_blob_sha1,
6811 staged_blob_id->sha1,
6812 SHA1_DIGEST_LENGTH);
6814 err = merge_file(&local_changes_subsumed,
6815 a->worktree, blob_base, ondisk_path,
6816 relpath, got_fileindex_perms_to_st(ie),
6817 path_unstaged_content, label_orig,
6818 "unstaged", a->repo, a->progress_cb,
6821 path_new_staged_content == NULL)
6822 got_fileindex_entry_stage_set(ie,
6823 GOT_FILEIDX_STAGE_NONE);
6824 break; /* Done with this file. */
6827 err = got_object_open_as_blob(&blob_staged, a->repo,
6828 staged_blob_id, 8192);
6831 err = merge_blob(&local_changes_subsumed, a->worktree,
6832 blob_base, ondisk_path, relpath,
6833 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6834 commit_id ? commit_id : a->worktree->base_commit_id,
6835 a->repo, a->progress_cb, a->progress_arg);
6837 got_fileindex_entry_stage_set(ie,
6838 GOT_FILEIDX_STAGE_NONE);
6840 case GOT_STATUS_DELETE:
6842 int choice = GOT_PATCH_CHOICE_NONE;
6843 err = (*a->patch_cb)(&choice, a->patch_arg,
6844 staged_status, ie->path, NULL, 1, 1);
6847 if (choice == GOT_PATCH_CHOICE_NO)
6849 if (choice != GOT_PATCH_CHOICE_YES) {
6850 err = got_error(GOT_ERR_PATCH_CHOICE);
6854 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6855 err = get_file_status(&status, &sb, ie, ondisk_path,
6856 dirfd, de_name, a->repo);
6859 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6864 if (path_unstaged_content &&
6865 unlink(path_unstaged_content) == -1 && err == NULL)
6866 err = got_error_from_errno2("unlink", path_unstaged_content);
6867 if (path_new_staged_content &&
6868 unlink(path_new_staged_content) == -1 && err == NULL)
6869 err = got_error_from_errno2("unlink", path_new_staged_content);
6870 free(path_unstaged_content);
6871 free(path_new_staged_content);
6873 got_object_blob_close(blob_base);
6875 got_object_blob_close(blob_staged);
6881 const struct got_error *
6882 got_worktree_unstage(struct got_worktree *worktree,
6883 struct got_pathlist_head *paths,
6884 got_worktree_checkout_cb progress_cb, void *progress_arg,
6885 got_worktree_patch_cb patch_cb, void *patch_arg,
6886 struct got_repository *repo)
6888 const struct got_error *err = NULL, *sync_err, *unlockerr;
6889 struct got_pathlist_entry *pe;
6890 struct got_fileindex *fileindex = NULL;
6891 char *fileindex_path = NULL;
6892 struct unstage_path_arg upa;
6894 err = lock_worktree(worktree, LOCK_EX);
6898 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6902 upa.worktree = worktree;
6903 upa.fileindex = fileindex;
6905 upa.progress_cb = progress_cb;
6906 upa.progress_arg = progress_arg;
6907 upa.patch_cb = patch_cb;
6908 upa.patch_arg = patch_arg;
6909 TAILQ_FOREACH(pe, paths, entry) {
6910 err = worktree_status(worktree, pe->path, fileindex, repo,
6911 unstage_path, &upa, NULL, NULL, 0, 0);
6916 sync_err = sync_fileindex(fileindex, fileindex_path);
6917 if (sync_err && err == NULL)
6920 free(fileindex_path);
6922 got_fileindex_free(fileindex);
6923 unlockerr = lock_worktree(worktree, LOCK_SH);
6924 if (unlockerr && err == NULL)