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) && !S_ISLNK(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 if (parent_path[0]) {
2854 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2855 return got_error_from_errno("asprintf");
2860 if (de->d_type != DT_DIR &&
2861 got_path_is_child(path, a->status_path, a->status_path_len)
2862 && !match_ignores(&a->ignores, path))
2863 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2864 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2870 static const struct got_error *
2871 status_traverse(void *arg, const char *path, int dirfd)
2873 const struct got_error *err = NULL;
2874 struct diff_dir_cb_arg *a = arg;
2879 err = add_ignores(&a->ignores, a->worktree->root_path,
2880 path, dirfd, ".cvsignore");
2884 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2885 dirfd, ".gitignore");
2890 static const struct got_error *
2891 report_single_file_status(const char *path, const char *ondisk_path,
2892 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2893 void *status_arg, struct got_repository *repo, int report_unchanged)
2895 struct got_fileindex_entry *ie;
2898 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2900 return report_file_status(ie, ondisk_path, -1, NULL,
2901 status_cb, status_arg, repo, report_unchanged);
2903 if (lstat(ondisk_path, &sb) == -1) {
2904 if (errno != ENOENT)
2905 return got_error_from_errno2("lstat", ondisk_path);
2906 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2907 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2911 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
2912 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2913 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2918 static const struct got_error *
2919 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
2920 const char *root_path, const char *path)
2922 const struct got_error *err;
2923 char *parent_path, *next_parent_path = NULL;
2925 err = add_ignores(ignores, root_path, "", -1,
2930 err = add_ignores(ignores, root_path, "", -1,
2935 err = got_path_dirname(&parent_path, path);
2937 if (err->code == GOT_ERR_BAD_PATH)
2938 return NULL; /* cannot traverse parent */
2942 err = add_ignores(ignores, root_path, parent_path, -1,
2946 err = add_ignores(ignores, root_path, parent_path, -1,
2950 err = got_path_dirname(&next_parent_path, parent_path);
2952 if (err->code == GOT_ERR_BAD_PATH)
2953 err = NULL; /* traversed everything */
2957 parent_path = next_parent_path;
2958 next_parent_path = NULL;
2962 free(next_parent_path);
2966 static const struct got_error *
2967 worktree_status(struct got_worktree *worktree, const char *path,
2968 struct got_fileindex *fileindex, struct got_repository *repo,
2969 got_worktree_status_cb status_cb, void *status_arg,
2970 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2971 int report_unchanged)
2973 const struct got_error *err = NULL;
2975 struct got_fileindex_diff_dir_cb fdiff_cb;
2976 struct diff_dir_cb_arg arg;
2977 char *ondisk_path = NULL;
2979 TAILQ_INIT(&arg.ignores);
2981 if (asprintf(&ondisk_path, "%s%s%s",
2982 worktree->root_path, path[0] ? "/" : "", path) == -1)
2983 return got_error_from_errno("asprintf");
2985 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2987 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
2989 err = got_error_from_errno2("open", ondisk_path);
2991 err = report_single_file_status(path, ondisk_path,
2992 fileindex, status_cb, status_arg, repo,
2995 fdiff_cb.diff_old_new = status_old_new;
2996 fdiff_cb.diff_old = status_old;
2997 fdiff_cb.diff_new = status_new;
2998 fdiff_cb.diff_traverse = status_traverse;
2999 arg.fileindex = fileindex;
3000 arg.worktree = worktree;
3001 arg.status_path = path;
3002 arg.status_path_len = strlen(path);
3004 arg.status_cb = status_cb;
3005 arg.status_arg = status_arg;
3006 arg.cancel_cb = cancel_cb;
3007 arg.cancel_arg = cancel_arg;
3008 arg.report_unchanged = report_unchanged;
3009 arg.no_ignores = no_ignores;
3011 err = add_ignores_from_parent_paths(&arg.ignores,
3012 worktree->root_path, path);
3016 err = got_fileindex_diff_dir(fileindex, fd,
3017 worktree->root_path, path, repo, &fdiff_cb, &arg);
3020 free_ignores(&arg.ignores);
3021 if (fd != -1 && close(fd) != 0 && err == NULL)
3022 err = got_error_from_errno("close");
3027 const struct got_error *
3028 got_worktree_status(struct got_worktree *worktree,
3029 struct got_pathlist_head *paths, struct got_repository *repo,
3030 got_worktree_status_cb status_cb, void *status_arg,
3031 got_cancel_cb cancel_cb, void *cancel_arg)
3033 const struct got_error *err = NULL;
3034 char *fileindex_path = NULL;
3035 struct got_fileindex *fileindex = NULL;
3036 struct got_pathlist_entry *pe;
3038 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3042 TAILQ_FOREACH(pe, paths, entry) {
3043 err = worktree_status(worktree, pe->path, fileindex, repo,
3044 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3048 free(fileindex_path);
3049 got_fileindex_free(fileindex);
3053 const struct got_error *
3054 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3057 const struct got_error *err = NULL;
3058 char *resolved = NULL, *cwd = NULL, *path = NULL;
3064 cwd = getcwd(NULL, 0);
3066 return got_error_from_errno("getcwd");
3068 if (lstat(arg, &sb) == -1) {
3069 if (errno != ENOENT) {
3070 err = got_error_from_errno2("lstat", arg);
3074 if (S_ISLNK(sb.st_mode)) {
3076 * We cannot use realpath(3) with symlinks since we want to
3077 * operate on the symlink itself.
3078 * But we can make the path absolute, assuming it is relative
3079 * to the current working directory, and then canonicalize it.
3081 char *abspath = NULL;
3082 char canonpath[PATH_MAX];
3083 if (!got_path_is_absolute(arg)) {
3084 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3085 err = got_error_from_errno("asprintf");
3090 err = got_canonpath(abspath ? abspath : arg, canonpath,
3094 resolved = strdup(canonpath);
3095 if (resolved == NULL) {
3096 err = got_error_from_errno("strdup");
3100 resolved = realpath(arg, NULL);
3101 if (resolved == NULL) {
3102 if (errno != ENOENT) {
3103 err = got_error_from_errno2("realpath", arg);
3106 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3107 err = got_error_from_errno("asprintf");
3113 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3114 strlen(got_worktree_get_root_path(worktree)))) {
3115 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3119 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3120 err = got_path_skip_common_ancestor(&path,
3121 got_worktree_get_root_path(worktree), resolved);
3127 err = got_error_from_errno("strdup");
3132 /* XXX status walk can't deal with trailing slash! */
3134 while (len > 0 && path[len - 1] == '/') {
3135 path[len - 1] = '\0';
3148 struct schedule_addition_args {
3149 struct got_worktree *worktree;
3150 struct got_fileindex *fileindex;
3151 got_worktree_checkout_cb progress_cb;
3153 struct got_repository *repo;
3156 static const struct got_error *
3157 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3158 const char *relpath, struct got_object_id *blob_id,
3159 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3160 int dirfd, const char *de_name)
3162 struct schedule_addition_args *a = arg;
3163 const struct got_error *err = NULL;
3164 struct got_fileindex_entry *ie;
3168 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3170 return got_error_from_errno("asprintf");
3172 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3174 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3178 /* Re-adding an existing entry is a no-op. */
3179 if (status == GOT_STATUS_ADD)
3181 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3186 if (status != GOT_STATUS_UNVERSIONED) {
3187 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3191 err = got_fileindex_entry_alloc(&ie, relpath);
3194 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3196 got_fileindex_entry_free(ie);
3199 err = got_fileindex_entry_add(a->fileindex, ie);
3201 got_fileindex_entry_free(ie);
3208 if (status == GOT_STATUS_ADD)
3210 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3213 const struct got_error *
3214 got_worktree_schedule_add(struct got_worktree *worktree,
3215 struct got_pathlist_head *paths,
3216 got_worktree_checkout_cb progress_cb, void *progress_arg,
3217 struct got_repository *repo, int no_ignores)
3219 struct got_fileindex *fileindex = NULL;
3220 char *fileindex_path = NULL;
3221 const struct got_error *err = NULL, *sync_err, *unlockerr;
3222 struct got_pathlist_entry *pe;
3223 struct schedule_addition_args saa;
3225 err = lock_worktree(worktree, LOCK_EX);
3229 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3233 saa.worktree = worktree;
3234 saa.fileindex = fileindex;
3235 saa.progress_cb = progress_cb;
3236 saa.progress_arg = progress_arg;
3239 TAILQ_FOREACH(pe, paths, entry) {
3240 err = worktree_status(worktree, pe->path, fileindex, repo,
3241 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3245 sync_err = sync_fileindex(fileindex, fileindex_path);
3246 if (sync_err && err == NULL)
3249 free(fileindex_path);
3251 got_fileindex_free(fileindex);
3252 unlockerr = lock_worktree(worktree, LOCK_SH);
3253 if (unlockerr && err == NULL)
3258 struct schedule_deletion_args {
3259 struct got_worktree *worktree;
3260 struct got_fileindex *fileindex;
3261 got_worktree_delete_cb progress_cb;
3263 struct got_repository *repo;
3264 int delete_local_mods;
3268 static const struct got_error *
3269 schedule_for_deletion(void *arg, unsigned char status,
3270 unsigned char staged_status, const char *relpath,
3271 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3272 struct got_object_id *commit_id, int dirfd, const char *de_name)
3274 struct schedule_deletion_args *a = arg;
3275 const struct got_error *err = NULL;
3276 struct got_fileindex_entry *ie = NULL;
3278 char *ondisk_path, *parent = NULL;
3280 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3282 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3284 staged_status = get_staged_status(ie);
3285 if (staged_status != GOT_STATUS_NO_CHANGE) {
3286 if (staged_status == GOT_STATUS_DELETE)
3288 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3291 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3293 return got_error_from_errno("asprintf");
3295 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3300 if (status != GOT_STATUS_NO_CHANGE) {
3301 if (status == GOT_STATUS_DELETE)
3303 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3304 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3307 if (status != GOT_STATUS_MODIFY &&
3308 status != GOT_STATUS_MISSING) {
3309 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3314 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3316 if (unlinkat(dirfd, de_name, 0) != 0) {
3317 err = got_error_from_errno2("unlinkat",
3321 } else if (unlink(ondisk_path) != 0) {
3322 err = got_error_from_errno2("unlink", ondisk_path);
3326 parent = dirname(ondisk_path);
3328 if (parent == NULL) {
3329 err = got_error_from_errno2("dirname", ondisk_path);
3332 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3333 if (rmdir(parent) == -1) {
3334 if (errno != ENOTEMPTY)
3335 err = got_error_from_errno2("rmdir",
3339 parent = dirname(parent);
3340 if (parent == NULL) {
3341 err = got_error_from_errno2("dirname", parent);
3347 got_fileindex_entry_mark_deleted_from_disk(ie);
3352 if (status == GOT_STATUS_DELETE)
3354 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3355 staged_status, relpath);
3358 const struct got_error *
3359 got_worktree_schedule_delete(struct got_worktree *worktree,
3360 struct got_pathlist_head *paths, int delete_local_mods,
3361 got_worktree_delete_cb progress_cb, void *progress_arg,
3362 struct got_repository *repo, int keep_on_disk)
3364 struct got_fileindex *fileindex = NULL;
3365 char *fileindex_path = NULL;
3366 const struct got_error *err = NULL, *sync_err, *unlockerr;
3367 struct got_pathlist_entry *pe;
3368 struct schedule_deletion_args sda;
3370 err = lock_worktree(worktree, LOCK_EX);
3374 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3378 sda.worktree = worktree;
3379 sda.fileindex = fileindex;
3380 sda.progress_cb = progress_cb;
3381 sda.progress_arg = progress_arg;
3383 sda.delete_local_mods = delete_local_mods;
3384 sda.keep_on_disk = keep_on_disk;
3386 TAILQ_FOREACH(pe, paths, entry) {
3387 err = worktree_status(worktree, pe->path, fileindex, repo,
3388 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3392 sync_err = sync_fileindex(fileindex, fileindex_path);
3393 if (sync_err && err == NULL)
3396 free(fileindex_path);
3398 got_fileindex_free(fileindex);
3399 unlockerr = lock_worktree(worktree, LOCK_SH);
3400 if (unlockerr && err == NULL)
3405 static const struct got_error *
3406 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3408 const struct got_error *err = NULL;
3410 size_t linesize = 0, n;
3413 linelen = getline(&line, &linesize, infile);
3414 if (linelen == -1) {
3415 if (ferror(infile)) {
3416 err = got_error_from_errno("getline");
3422 n = fwrite(line, 1, linelen, outfile);
3424 err = got_ferror(outfile, GOT_ERR_IO);
3429 n = fwrite(line, 1, linelen, rejectfile);
3431 err = got_ferror(outfile, GOT_ERR_IO);
3438 static const struct got_error *
3439 skip_one_line(FILE *f)
3442 size_t linesize = 0;
3445 linelen = getline(&line, &linesize, f);
3446 if (linelen == -1) {
3448 return got_error_from_errno("getline");
3455 static const struct got_error *
3456 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3457 int start_old, int end_old, int start_new, int end_new,
3458 FILE *outfile, FILE *rejectfile)
3460 const struct got_error *err;
3462 /* Copy old file's lines leading up to patch. */
3463 while (!feof(f1) && *line_cur1 < start_old) {
3464 err = copy_one_line(f1, outfile, NULL);
3469 /* Skip new file's lines leading up to patch. */
3470 while (!feof(f2) && *line_cur2 < start_new) {
3472 err = copy_one_line(f2, NULL, rejectfile);
3474 err = skip_one_line(f2);
3479 /* Copy patched lines. */
3480 while (!feof(f2) && *line_cur2 <= end_new) {
3481 err = copy_one_line(f2, outfile, NULL);
3486 /* Skip over old file's replaced lines. */
3487 while (!feof(f1) && *line_cur1 <= end_old) {
3489 err = copy_one_line(f1, NULL, rejectfile);
3491 err = skip_one_line(f1);
3500 static const struct got_error *
3501 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3502 FILE *outfile, FILE *rejectfile)
3504 const struct got_error *err;
3507 /* Copy old file's lines until EOF. */
3509 err = copy_one_line(f1, outfile, NULL);
3516 /* Copy new file's lines until EOF. */
3518 err = copy_one_line(f2, NULL, rejectfile);
3528 static const struct got_error *
3529 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3530 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3531 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3532 int *line_cur2, FILE *outfile, FILE *rejectfile,
3533 got_worktree_patch_cb patch_cb, void *patch_arg)
3535 const struct got_error *err = NULL;
3536 int start_old = change->cv.a;
3537 int end_old = change->cv.b;
3538 int start_new = change->cv.c;
3539 int end_new = change->cv.d;
3543 *choice = GOT_PATCH_CHOICE_NONE;
3545 hunkfile = got_opentemp();
3546 if (hunkfile == NULL)
3547 return got_error_from_errno("got_opentemp");
3552 /* XXX TODO needs error checking */
3553 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3555 if (fseek(f1, pos1, SEEK_SET) == -1) {
3556 err = got_ferror(f1, GOT_ERR_IO);
3559 if (fseek(f2, pos2, SEEK_SET) == -1) {
3560 err = got_ferror(f1, GOT_ERR_IO);
3563 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3564 err = got_ferror(hunkfile, GOT_ERR_IO);
3568 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3569 hunkfile, n, nchanges);
3574 case GOT_PATCH_CHOICE_YES:
3575 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3576 end_old, start_new, end_new, outfile, rejectfile);
3578 case GOT_PATCH_CHOICE_NO:
3579 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3580 end_old, start_new, end_new, rejectfile, outfile);
3582 case GOT_PATCH_CHOICE_QUIT:
3585 err = got_error(GOT_ERR_PATCH_CHOICE);
3589 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3590 err = got_error_from_errno("fclose");
3594 struct revert_file_args {
3595 struct got_worktree *worktree;
3596 struct got_fileindex *fileindex;
3597 got_worktree_checkout_cb progress_cb;
3599 got_worktree_patch_cb patch_cb;
3601 struct got_repository *repo;
3604 static const struct got_error *
3605 create_patched_content(char **path_outfile, int reverse_patch,
3606 struct got_object_id *blob_id, const char *path2,
3607 int dirfd2, const char *de_name2,
3608 const char *relpath, struct got_repository *repo,
3609 got_worktree_patch_cb patch_cb, void *patch_arg)
3611 const struct got_error *err;
3612 struct got_blob_object *blob = NULL;
3613 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3615 char *path1 = NULL, *id_str = NULL;
3616 struct stat sb1, sb2;
3617 struct got_diff_changes *changes = NULL;
3618 struct got_diff_state *ds = NULL;
3619 struct got_diff_args *args = NULL;
3620 struct got_diff_change *change;
3621 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3624 *path_outfile = NULL;
3626 err = got_object_id_str(&id_str, blob_id);
3631 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3633 err = got_error_from_errno2("openat", path2);
3637 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3639 err = got_error_from_errno2("open", path2);
3643 if (fstat(fd2, &sb2) == -1) {
3644 err = got_error_from_errno2("fstat", path2);
3648 f2 = fdopen(fd2, "r");
3650 err = got_error_from_errno2("fdopen", path2);
3655 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3659 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3663 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3667 if (stat(path1, &sb1) == -1) {
3668 err = got_error_from_errno2("stat", path1);
3672 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3673 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3677 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3681 if (fseek(f1, 0L, SEEK_SET) == -1)
3682 return got_ferror(f1, GOT_ERR_IO);
3683 if (fseek(f2, 0L, SEEK_SET) == -1)
3684 return got_ferror(f2, GOT_ERR_IO);
3685 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3687 err = apply_or_reject_change(&choice, change, ++n,
3688 changes->nchanges, ds, args, diff_flags, relpath,
3689 f1, f2, &line_cur1, &line_cur2,
3690 reverse_patch ? NULL : outfile,
3691 reverse_patch ? outfile : NULL,
3692 patch_cb, patch_arg);
3695 if (choice == GOT_PATCH_CHOICE_YES)
3697 else if (choice == GOT_PATCH_CHOICE_QUIT)
3701 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3702 reverse_patch ? NULL : outfile,
3703 reverse_patch ? outfile : NULL);
3707 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3708 err = got_error_from_errno2("chmod", path2);
3715 got_object_blob_close(blob);
3716 if (f1 && fclose(f1) == EOF && err == NULL)
3717 err = got_error_from_errno2("fclose", path1);
3718 if (f2 && fclose(f2) == EOF && err == NULL)
3719 err = got_error_from_errno2("fclose", path2);
3720 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3721 err = got_error_from_errno2("close", path2);
3722 if (outfile && fclose(outfile) == EOF && err == NULL)
3723 err = got_error_from_errno2("fclose", *path_outfile);
3724 if (path1 && unlink(path1) == -1 && err == NULL)
3725 err = got_error_from_errno2("unlink", path1);
3726 if (err || !have_content) {
3727 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3728 err = got_error_from_errno2("unlink", *path_outfile);
3729 free(*path_outfile);
3730 *path_outfile = NULL;
3734 got_diff_state_free(ds);
3738 got_diff_free_changes(changes);
3743 static const struct got_error *
3744 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3745 const char *relpath, struct got_object_id *blob_id,
3746 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3747 int dirfd, const char *de_name)
3749 struct revert_file_args *a = arg;
3750 const struct got_error *err = NULL;
3751 char *parent_path = NULL;
3752 struct got_fileindex_entry *ie;
3753 struct got_tree_object *tree = NULL;
3754 struct got_object_id *tree_id = NULL;
3755 const struct got_tree_entry *te = NULL;
3756 char *tree_path = NULL, *te_name;
3757 char *ondisk_path = NULL, *path_content = NULL;
3758 struct got_blob_object *blob = NULL;
3760 /* Reverting a staged deletion is a no-op. */
3761 if (status == GOT_STATUS_DELETE &&
3762 staged_status != GOT_STATUS_NO_CHANGE)
3765 if (status == GOT_STATUS_UNVERSIONED)
3766 return (*a->progress_cb)(a->progress_arg,
3767 GOT_STATUS_UNVERSIONED, relpath);
3769 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3771 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3773 /* Construct in-repository path of tree which contains this blob. */
3774 err = got_path_dirname(&parent_path, ie->path);
3776 if (err->code != GOT_ERR_BAD_PATH)
3778 parent_path = strdup("/");
3779 if (parent_path == NULL) {
3780 err = got_error_from_errno("strdup");
3784 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3785 tree_path = strdup(parent_path);
3786 if (tree_path == NULL) {
3787 err = got_error_from_errno("strdup");
3791 if (got_path_is_root_dir(parent_path)) {
3792 tree_path = strdup(a->worktree->path_prefix);
3793 if (tree_path == NULL) {
3794 err = got_error_from_errno("strdup");
3798 if (asprintf(&tree_path, "%s/%s",
3799 a->worktree->path_prefix, parent_path) == -1) {
3800 err = got_error_from_errno("asprintf");
3806 err = got_object_id_by_path(&tree_id, a->repo,
3807 a->worktree->base_commit_id, tree_path);
3809 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3810 (status == GOT_STATUS_ADD ||
3811 staged_status == GOT_STATUS_ADD)))
3814 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3818 te_name = basename(ie->path);
3819 if (te_name == NULL) {
3820 err = got_error_from_errno2("basename", ie->path);
3824 te = got_object_tree_find_entry(tree, te_name);
3825 if (te == NULL && status != GOT_STATUS_ADD &&
3826 staged_status != GOT_STATUS_ADD) {
3827 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3833 case GOT_STATUS_ADD:
3835 int choice = GOT_PATCH_CHOICE_NONE;
3836 err = (*a->patch_cb)(&choice, a->patch_arg,
3837 status, ie->path, NULL, 1, 1);
3840 if (choice != GOT_PATCH_CHOICE_YES)
3843 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3847 got_fileindex_entry_remove(a->fileindex, ie);
3849 case GOT_STATUS_DELETE:
3851 int choice = GOT_PATCH_CHOICE_NONE;
3852 err = (*a->patch_cb)(&choice, a->patch_arg,
3853 status, ie->path, NULL, 1, 1);
3856 if (choice != GOT_PATCH_CHOICE_YES)
3860 case GOT_STATUS_MODIFY:
3861 case GOT_STATUS_MODE_CHANGE:
3862 case GOT_STATUS_CONFLICT:
3863 case GOT_STATUS_MISSING: {
3864 struct got_object_id id;
3865 if (staged_status == GOT_STATUS_ADD ||
3866 staged_status == GOT_STATUS_MODIFY) {
3867 memcpy(id.sha1, ie->staged_blob_sha1,
3868 SHA1_DIGEST_LENGTH);
3870 memcpy(id.sha1, ie->blob_sha1,
3871 SHA1_DIGEST_LENGTH);
3872 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3876 if (asprintf(&ondisk_path, "%s/%s",
3877 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3878 err = got_error_from_errno("asprintf");
3882 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3883 status == GOT_STATUS_CONFLICT)) {
3884 err = create_patched_content(&path_content, 1, &id,
3885 ondisk_path, dirfd, de_name, ie->path, a->repo,
3886 a->patch_cb, a->patch_arg);
3887 if (err || path_content == NULL)
3889 if (rename(path_content, ondisk_path) == -1) {
3890 err = got_error_from_errno3("rename",
3891 path_content, ondisk_path);
3895 err = install_blob(a->worktree, ondisk_path, ie->path,
3896 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3897 got_fileindex_perms_to_st(ie), blob, 0, 1,
3898 a->repo, a->progress_cb, a->progress_arg);
3901 if (status == GOT_STATUS_DELETE ||
3902 status == GOT_STATUS_MODE_CHANGE) {
3903 err = got_fileindex_entry_update(ie,
3904 ondisk_path, blob->id.sha1,
3905 a->worktree->base_commit_id->sha1, 1);
3921 got_object_blob_close(blob);
3923 got_object_tree_close(tree);
3928 const struct got_error *
3929 got_worktree_revert(struct got_worktree *worktree,
3930 struct got_pathlist_head *paths,
3931 got_worktree_checkout_cb progress_cb, void *progress_arg,
3932 got_worktree_patch_cb patch_cb, void *patch_arg,
3933 struct got_repository *repo)
3935 struct got_fileindex *fileindex = NULL;
3936 char *fileindex_path = NULL;
3937 const struct got_error *err = NULL, *unlockerr = NULL;
3938 const struct got_error *sync_err = NULL;
3939 struct got_pathlist_entry *pe;
3940 struct revert_file_args rfa;
3942 err = lock_worktree(worktree, LOCK_EX);
3946 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3950 rfa.worktree = worktree;
3951 rfa.fileindex = fileindex;
3952 rfa.progress_cb = progress_cb;
3953 rfa.progress_arg = progress_arg;
3954 rfa.patch_cb = patch_cb;
3955 rfa.patch_arg = patch_arg;
3957 TAILQ_FOREACH(pe, paths, entry) {
3958 err = worktree_status(worktree, pe->path, fileindex, repo,
3959 revert_file, &rfa, NULL, NULL, 0, 0);
3963 sync_err = sync_fileindex(fileindex, fileindex_path);
3964 if (sync_err && err == NULL)
3967 free(fileindex_path);
3969 got_fileindex_free(fileindex);
3970 unlockerr = lock_worktree(worktree, LOCK_SH);
3971 if (unlockerr && err == NULL)
3977 free_commitable(struct got_commitable *ct)
3980 free(ct->in_repo_path);
3981 free(ct->ondisk_path);
3983 free(ct->base_blob_id);
3984 free(ct->staged_blob_id);
3985 free(ct->base_commit_id);
3989 struct collect_commitables_arg {
3990 struct got_pathlist_head *commitable_paths;
3991 struct got_repository *repo;
3992 struct got_worktree *worktree;
3993 int have_staged_files;
3996 static const struct got_error *
3997 collect_commitables(void *arg, unsigned char status,
3998 unsigned char staged_status, const char *relpath,
3999 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4000 struct got_object_id *commit_id, int dirfd, const char *de_name)
4002 struct collect_commitables_arg *a = arg;
4003 const struct got_error *err = NULL;
4004 struct got_commitable *ct = NULL;
4005 struct got_pathlist_entry *new = NULL;
4006 char *parent_path = NULL, *path = NULL;
4009 if (a->have_staged_files) {
4010 if (staged_status != GOT_STATUS_MODIFY &&
4011 staged_status != GOT_STATUS_ADD &&
4012 staged_status != GOT_STATUS_DELETE)
4015 if (status == GOT_STATUS_CONFLICT)
4016 return got_error(GOT_ERR_COMMIT_CONFLICT);
4018 if (status != GOT_STATUS_MODIFY &&
4019 status != GOT_STATUS_MODE_CHANGE &&
4020 status != GOT_STATUS_ADD &&
4021 status != GOT_STATUS_DELETE)
4025 if (asprintf(&path, "/%s", relpath) == -1) {
4026 err = got_error_from_errno("asprintf");
4029 if (strcmp(path, "/") == 0) {
4030 parent_path = strdup("");
4031 if (parent_path == NULL)
4032 return got_error_from_errno("strdup");
4034 err = got_path_dirname(&parent_path, path);
4039 ct = calloc(1, sizeof(*ct));
4041 err = got_error_from_errno("calloc");
4045 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4047 err = got_error_from_errno("asprintf");
4050 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4051 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4054 if (fstatat(dirfd, de_name, &sb,
4055 AT_SYMLINK_NOFOLLOW) == -1) {
4056 err = got_error_from_errno2("fstatat",
4060 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4061 err = got_error_from_errno2("lstat", ct->ondisk_path);
4064 ct->mode = sb.st_mode;
4067 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4068 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4070 err = got_error_from_errno("asprintf");
4074 ct->status = status;
4075 ct->staged_status = staged_status;
4076 ct->blob_id = NULL; /* will be filled in when blob gets created */
4077 if (ct->status != GOT_STATUS_ADD &&
4078 ct->staged_status != GOT_STATUS_ADD) {
4079 ct->base_blob_id = got_object_id_dup(blob_id);
4080 if (ct->base_blob_id == NULL) {
4081 err = got_error_from_errno("got_object_id_dup");
4084 ct->base_commit_id = got_object_id_dup(commit_id);
4085 if (ct->base_commit_id == NULL) {
4086 err = got_error_from_errno("got_object_id_dup");
4090 if (ct->staged_status == GOT_STATUS_ADD ||
4091 ct->staged_status == GOT_STATUS_MODIFY) {
4092 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4093 if (ct->staged_blob_id == NULL) {
4094 err = got_error_from_errno("got_object_id_dup");
4098 ct->path = strdup(path);
4099 if (ct->path == NULL) {
4100 err = got_error_from_errno("strdup");
4103 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4105 if (ct && (err || new == NULL))
4106 free_commitable(ct);
4112 static const struct got_error *write_tree(struct got_object_id **, int *,
4113 struct got_tree_object *, const char *, struct got_pathlist_head *,
4114 got_worktree_status_cb status_cb, void *status_arg,
4115 struct got_repository *);
4117 static const struct got_error *
4118 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4119 struct got_tree_entry *te, const char *parent_path,
4120 struct got_pathlist_head *commitable_paths,
4121 got_worktree_status_cb status_cb, void *status_arg,
4122 struct got_repository *repo)
4124 const struct got_error *err = NULL;
4125 struct got_tree_object *subtree;
4128 if (asprintf(&subpath, "%s%s%s", parent_path,
4129 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4130 return got_error_from_errno("asprintf");
4132 err = got_object_open_as_tree(&subtree, repo, &te->id);
4136 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4137 commitable_paths, status_cb, status_arg, repo);
4138 got_object_tree_close(subtree);
4143 static const struct got_error *
4144 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4146 const struct got_error *err = NULL;
4147 char *ct_parent_path = NULL;
4151 if (strchr(ct->in_repo_path, '/') == NULL) {
4152 *match = got_path_is_root_dir(path);
4156 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4159 *match = (strcmp(path, ct_parent_path) == 0);
4160 free(ct_parent_path);
4165 get_ct_file_mode(struct got_commitable *ct)
4167 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4170 static const struct got_error *
4171 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4172 struct got_tree_entry *te, struct got_commitable *ct)
4174 const struct got_error *err = NULL;
4178 err = got_object_tree_entry_dup(new_te, te);
4182 (*new_te)->mode = get_ct_file_mode(ct);
4184 if (ct->staged_status == GOT_STATUS_MODIFY)
4185 memcpy(&(*new_te)->id, ct->staged_blob_id,
4186 sizeof((*new_te)->id));
4188 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4190 if (err && *new_te) {
4197 static const struct got_error *
4198 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4199 struct got_commitable *ct)
4201 const struct got_error *err = NULL;
4206 *new_te = calloc(1, sizeof(**new_te));
4207 if (*new_te == NULL)
4208 return got_error_from_errno("calloc");
4210 ct_name = basename(ct->path);
4211 if (ct_name == NULL) {
4212 err = got_error_from_errno2("basename", ct->path);
4215 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4216 sizeof((*new_te)->name)) {
4217 err = got_error(GOT_ERR_NO_SPACE);
4221 (*new_te)->mode = get_ct_file_mode(ct);
4223 if (ct->staged_status == GOT_STATUS_ADD)
4224 memcpy(&(*new_te)->id, ct->staged_blob_id,
4225 sizeof((*new_te)->id));
4227 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4229 if (err && *new_te) {
4236 static const struct got_error *
4237 insert_tree_entry(struct got_tree_entry *new_te,
4238 struct got_pathlist_head *paths)
4240 const struct got_error *err = NULL;
4241 struct got_pathlist_entry *new_pe;
4243 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4247 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4251 static const struct got_error *
4252 report_ct_status(struct got_commitable *ct,
4253 got_worktree_status_cb status_cb, void *status_arg)
4255 const char *ct_path = ct->path;
4256 unsigned char status;
4258 while (ct_path[0] == '/')
4261 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4262 status = ct->staged_status;
4264 status = ct->status;
4266 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4267 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4270 static const struct got_error *
4271 match_modified_subtree(int *modified, struct got_tree_entry *te,
4272 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4274 const struct got_error *err = NULL;
4275 struct got_pathlist_entry *pe;
4280 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4281 got_path_is_root_dir(base_tree_path) ? "" : "/",
4283 return got_error_from_errno("asprintf");
4285 TAILQ_FOREACH(pe, commitable_paths, entry) {
4286 struct got_commitable *ct = pe->data;
4287 *modified = got_path_is_child(ct->in_repo_path, te_path,
4297 static const struct got_error *
4298 match_deleted_or_modified_ct(struct got_commitable **ctp,
4299 struct got_tree_entry *te, const char *base_tree_path,
4300 struct got_pathlist_head *commitable_paths)
4302 const struct got_error *err = NULL;
4303 struct got_pathlist_entry *pe;
4307 TAILQ_FOREACH(pe, commitable_paths, entry) {
4308 struct got_commitable *ct = pe->data;
4309 char *ct_name = NULL;
4312 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4313 if (ct->status != GOT_STATUS_MODIFY &&
4314 ct->status != GOT_STATUS_MODE_CHANGE &&
4315 ct->status != GOT_STATUS_DELETE)
4318 if (ct->staged_status != GOT_STATUS_MODIFY &&
4319 ct->staged_status != GOT_STATUS_DELETE)
4323 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4326 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4332 ct_name = basename(pe->path);
4333 if (ct_name == NULL)
4334 return got_error_from_errno2("basename", pe->path);
4336 if (strcmp(te->name, ct_name) != 0)
4346 static const struct got_error *
4347 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4348 const char *child_path, const char *path_base_tree,
4349 struct got_pathlist_head *commitable_paths,
4350 got_worktree_status_cb status_cb, void *status_arg,
4351 struct got_repository *repo)
4353 const struct got_error *err = NULL;
4354 struct got_tree_entry *new_te;
4356 struct got_object_id *id = NULL;
4361 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4362 got_path_is_root_dir(path_base_tree) ? "" : "/",
4364 return got_error_from_errno("asprintf");
4366 new_te = calloc(1, sizeof(*new_te));
4368 return got_error_from_errno("calloc");
4369 new_te->mode = S_IFDIR;
4371 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4372 sizeof(new_te->name)) {
4373 err = got_error(GOT_ERR_NO_SPACE);
4376 err = write_tree(&id, &nentries, NULL, subtree_path,
4377 commitable_paths, status_cb, status_arg, repo);
4382 memcpy(&new_te->id, id, sizeof(new_te->id));
4391 static const struct got_error *
4392 write_tree(struct got_object_id **new_tree_id, int *nentries,
4393 struct got_tree_object *base_tree, const char *path_base_tree,
4394 struct got_pathlist_head *commitable_paths,
4395 got_worktree_status_cb status_cb, void *status_arg,
4396 struct got_repository *repo)
4398 const struct got_error *err = NULL;
4399 struct got_pathlist_head paths;
4400 struct got_tree_entry *te, *new_te = NULL;
4401 struct got_pathlist_entry *pe;
4406 /* Insert, and recurse into, newly added entries first. */
4407 TAILQ_FOREACH(pe, commitable_paths, entry) {
4408 struct got_commitable *ct = pe->data;
4409 char *child_path = NULL, *slash;
4411 if ((ct->status != GOT_STATUS_ADD &&
4412 ct->staged_status != GOT_STATUS_ADD) ||
4413 (ct->flags & GOT_COMMITABLE_ADDED))
4416 if (!got_path_is_child(pe->path, path_base_tree,
4417 strlen(path_base_tree)))
4420 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4425 slash = strchr(child_path, '/');
4426 if (slash == NULL) {
4427 err = alloc_added_blob_tree_entry(&new_te, ct);
4430 err = report_ct_status(ct, status_cb, status_arg);
4433 ct->flags |= GOT_COMMITABLE_ADDED;
4434 err = insert_tree_entry(new_te, &paths);
4439 *slash = '\0'; /* trim trailing path components */
4440 if (base_tree == NULL ||
4441 got_object_tree_find_entry(base_tree, child_path)
4443 err = make_subtree_for_added_blob(&new_te,
4444 child_path, path_base_tree,
4445 commitable_paths, status_cb, status_arg,
4449 err = insert_tree_entry(new_te, &paths);
4458 int i, nbase_entries;
4459 /* Handle modified and deleted entries. */
4460 nbase_entries = got_object_tree_get_nentries(base_tree);
4461 for (i = 0; i < nbase_entries; i++) {
4462 struct got_commitable *ct = NULL;
4464 te = got_object_tree_get_entry(base_tree, i);
4465 if (got_object_tree_entry_is_submodule(te)) {
4466 /* Entry is a submodule; just copy it. */
4467 err = got_object_tree_entry_dup(&new_te, te);
4470 err = insert_tree_entry(new_te, &paths);
4477 if (S_ISDIR(te->mode)) {
4479 err = got_object_tree_entry_dup(&new_te, te);
4482 err = match_modified_subtree(&modified, te,
4483 path_base_tree, commitable_paths);
4486 /* Avoid recursion into unmodified subtrees. */
4488 struct got_object_id *new_id;
4490 err = write_subtree(&new_id,
4492 path_base_tree, commitable_paths,
4493 status_cb, status_arg, repo);
4496 if (nsubentries == 0) {
4497 /* All entries were deleted. */
4501 memcpy(&new_te->id, new_id,
4502 sizeof(new_te->id));
4505 err = insert_tree_entry(new_te, &paths);
4512 err = match_deleted_or_modified_ct(&ct, te,
4513 path_base_tree, commitable_paths);
4517 /* NB: Deleted entries get dropped here. */
4518 if (ct->status == GOT_STATUS_MODIFY ||
4519 ct->status == GOT_STATUS_MODE_CHANGE ||
4520 ct->staged_status == GOT_STATUS_MODIFY) {
4521 err = alloc_modified_blob_tree_entry(
4525 err = insert_tree_entry(new_te, &paths);
4530 err = report_ct_status(ct, status_cb,
4535 /* Entry is unchanged; just copy it. */
4536 err = got_object_tree_entry_dup(&new_te, te);
4539 err = insert_tree_entry(new_te, &paths);
4547 /* Write new list of entries; deleted entries have been dropped. */
4548 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4550 got_pathlist_free(&paths);
4554 static const struct got_error *
4555 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4556 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4557 int have_staged_files)
4559 const struct got_error *err = NULL;
4560 struct got_pathlist_entry *pe;
4562 TAILQ_FOREACH(pe, commitable_paths, entry) {
4563 struct got_fileindex_entry *ie;
4564 struct got_commitable *ct = pe->data;
4566 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4568 if (ct->status == GOT_STATUS_DELETE ||
4569 ct->staged_status == GOT_STATUS_DELETE) {
4570 got_fileindex_entry_remove(fileindex, ie);
4571 } else if (ct->staged_status == GOT_STATUS_ADD ||
4572 ct->staged_status == GOT_STATUS_MODIFY) {
4573 got_fileindex_entry_stage_set(ie,
4574 GOT_FILEIDX_STAGE_NONE);
4575 err = got_fileindex_entry_update(ie,
4576 ct->ondisk_path, ct->staged_blob_id->sha1,
4577 new_base_commit_id->sha1,
4578 !have_staged_files);
4580 err = got_fileindex_entry_update(ie,
4581 ct->ondisk_path, ct->blob_id->sha1,
4582 new_base_commit_id->sha1,
4583 !have_staged_files);
4585 err = got_fileindex_entry_alloc(&ie, pe->path);
4588 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4589 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4591 got_fileindex_entry_free(ie);
4594 err = got_fileindex_entry_add(fileindex, ie);
4596 got_fileindex_entry_free(ie);
4605 static const struct got_error *
4606 check_out_of_date(const char *in_repo_path, unsigned char status,
4607 unsigned char staged_status, struct got_object_id *base_blob_id,
4608 struct got_object_id *base_commit_id,
4609 struct got_object_id *head_commit_id, struct got_repository *repo,
4612 const struct got_error *err = NULL;
4613 struct got_object_id *id = NULL;
4615 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4616 /* Trivial case: base commit == head commit */
4617 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4620 * Ensure file content which local changes were based
4621 * on matches file content in the branch head.
4623 err = got_object_id_by_path(&id, repo, head_commit_id,
4626 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4627 err = got_error(ood_errcode);
4629 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4630 err = got_error(ood_errcode);
4632 /* Require that added files don't exist in the branch head. */
4633 err = got_object_id_by_path(&id, repo, head_commit_id,
4635 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4637 err = id ? got_error(ood_errcode) : NULL;
4644 const struct got_error *
4645 commit_worktree(struct got_object_id **new_commit_id,
4646 struct got_pathlist_head *commitable_paths,
4647 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4648 const char *author, const char *committer,
4649 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4650 got_worktree_status_cb status_cb, void *status_arg,
4651 struct got_repository *repo)
4653 const struct got_error *err = NULL, *unlockerr = NULL;
4654 struct got_pathlist_entry *pe;
4655 const char *head_ref_name = NULL;
4656 struct got_commit_object *head_commit = NULL;
4657 struct got_reference *head_ref2 = NULL;
4658 struct got_object_id *head_commit_id2 = NULL;
4659 struct got_tree_object *head_tree = NULL;
4660 struct got_object_id *new_tree_id = NULL;
4662 struct got_object_id_queue parent_ids;
4663 struct got_object_qid *pid = NULL;
4664 char *logmsg = NULL;
4666 *new_commit_id = NULL;
4668 SIMPLEQ_INIT(&parent_ids);
4670 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4674 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4678 if (commit_msg_cb != NULL) {
4679 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4684 if (logmsg == NULL || strlen(logmsg) == 0) {
4685 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4689 /* Create blobs from added and modified files and record their IDs. */
4690 TAILQ_FOREACH(pe, commitable_paths, entry) {
4691 struct got_commitable *ct = pe->data;
4694 /* Blobs for staged files already exist. */
4695 if (ct->staged_status == GOT_STATUS_ADD ||
4696 ct->staged_status == GOT_STATUS_MODIFY)
4699 if (ct->status != GOT_STATUS_ADD &&
4700 ct->status != GOT_STATUS_MODIFY &&
4701 ct->status != GOT_STATUS_MODE_CHANGE)
4704 if (asprintf(&ondisk_path, "%s/%s",
4705 worktree->root_path, pe->path) == -1) {
4706 err = got_error_from_errno("asprintf");
4709 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4715 /* Recursively write new tree objects. */
4716 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4717 commitable_paths, status_cb, status_arg, repo);
4721 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4724 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4725 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4726 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4727 got_object_qid_free(pid);
4733 /* Check if a concurrent commit to our branch has occurred. */
4734 head_ref_name = got_worktree_get_head_ref_name(worktree);
4735 if (head_ref_name == NULL) {
4736 err = got_error_from_errno("got_worktree_get_head_ref_name");
4739 /* Lock the reference here to prevent concurrent modification. */
4740 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4743 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4746 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4747 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4750 /* Update branch head in repository. */
4751 err = got_ref_change_ref(head_ref2, *new_commit_id);
4754 err = got_ref_write(head_ref2, repo);
4758 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4762 err = ref_base_commit(worktree, repo);
4767 got_object_tree_close(head_tree);
4769 got_object_commit_close(head_commit);
4770 free(head_commit_id2);
4772 unlockerr = got_ref_unlock(head_ref2);
4773 if (unlockerr && err == NULL)
4775 got_ref_close(head_ref2);
4780 static const struct got_error *
4781 check_path_is_commitable(const char *path,
4782 struct got_pathlist_head *commitable_paths)
4784 struct got_pathlist_entry *cpe = NULL;
4785 size_t path_len = strlen(path);
4787 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4788 struct got_commitable *ct = cpe->data;
4789 const char *ct_path = ct->path;
4791 while (ct_path[0] == '/')
4794 if (strcmp(path, ct_path) == 0 ||
4795 got_path_is_child(ct_path, path, path_len))
4800 return got_error_path(path, GOT_ERR_BAD_PATH);
4805 static const struct got_error *
4806 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4808 int *have_staged_files = arg;
4810 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4811 *have_staged_files = 1;
4812 return got_error(GOT_ERR_CANCELLED);
4818 static const struct got_error *
4819 check_non_staged_files(struct got_fileindex *fileindex,
4820 struct got_pathlist_head *paths)
4822 struct got_pathlist_entry *pe;
4823 struct got_fileindex_entry *ie;
4825 TAILQ_FOREACH(pe, paths, entry) {
4826 if (pe->path[0] == '\0')
4828 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4830 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4831 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4832 return got_error_path(pe->path,
4833 GOT_ERR_FILE_NOT_STAGED);
4839 const struct got_error *
4840 got_worktree_commit(struct got_object_id **new_commit_id,
4841 struct got_worktree *worktree, struct got_pathlist_head *paths,
4842 const char *author, const char *committer,
4843 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4844 got_worktree_status_cb status_cb, void *status_arg,
4845 struct got_repository *repo)
4847 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4848 struct got_fileindex *fileindex = NULL;
4849 char *fileindex_path = NULL;
4850 struct got_pathlist_head commitable_paths;
4851 struct collect_commitables_arg cc_arg;
4852 struct got_pathlist_entry *pe;
4853 struct got_reference *head_ref = NULL;
4854 struct got_object_id *head_commit_id = NULL;
4855 int have_staged_files = 0;
4857 *new_commit_id = NULL;
4859 TAILQ_INIT(&commitable_paths);
4861 err = lock_worktree(worktree, LOCK_EX);
4865 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4869 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4873 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4877 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4878 &have_staged_files);
4879 if (err && err->code != GOT_ERR_CANCELLED)
4881 if (have_staged_files) {
4882 err = check_non_staged_files(fileindex, paths);
4887 cc_arg.commitable_paths = &commitable_paths;
4888 cc_arg.worktree = worktree;
4890 cc_arg.have_staged_files = have_staged_files;
4891 TAILQ_FOREACH(pe, paths, entry) {
4892 err = worktree_status(worktree, pe->path, fileindex, repo,
4893 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4898 if (TAILQ_EMPTY(&commitable_paths)) {
4899 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4903 TAILQ_FOREACH(pe, paths, entry) {
4904 err = check_path_is_commitable(pe->path, &commitable_paths);
4909 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4910 struct got_commitable *ct = pe->data;
4911 const char *ct_path = ct->in_repo_path;
4913 while (ct_path[0] == '/')
4915 err = check_out_of_date(ct_path, ct->status,
4916 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4917 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4923 err = commit_worktree(new_commit_id, &commitable_paths,
4924 head_commit_id, worktree, author, committer,
4925 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4929 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4930 fileindex, have_staged_files);
4931 sync_err = sync_fileindex(fileindex, fileindex_path);
4932 if (sync_err && err == NULL)
4936 got_fileindex_free(fileindex);
4937 free(fileindex_path);
4938 unlockerr = lock_worktree(worktree, LOCK_SH);
4939 if (unlockerr && err == NULL)
4941 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4942 struct got_commitable *ct = pe->data;
4943 free_commitable(ct);
4945 got_pathlist_free(&commitable_paths);
4950 got_commitable_get_path(struct got_commitable *ct)
4956 got_commitable_get_status(struct got_commitable *ct)
4961 struct check_rebase_ok_arg {
4962 struct got_worktree *worktree;
4963 struct got_repository *repo;
4966 static const struct got_error *
4967 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4969 const struct got_error *err = NULL;
4970 struct check_rebase_ok_arg *a = arg;
4971 unsigned char status;
4975 /* Reject rebase of a work tree with mixed base commits. */
4976 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4977 SHA1_DIGEST_LENGTH))
4978 return got_error(GOT_ERR_MIXED_COMMITS);
4980 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4982 return got_error_from_errno("asprintf");
4984 /* Reject rebase of a work tree with modified or staged files. */
4985 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4990 if (status != GOT_STATUS_NO_CHANGE)
4991 return got_error(GOT_ERR_MODIFIED);
4992 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4993 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4998 const struct got_error *
4999 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5000 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5001 struct got_worktree *worktree, struct got_reference *branch,
5002 struct got_repository *repo)
5004 const struct got_error *err = NULL;
5005 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5006 char *branch_ref_name = NULL;
5007 char *fileindex_path = NULL;
5008 struct check_rebase_ok_arg ok_arg;
5009 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5010 struct got_object_id *wt_branch_tip = NULL;
5012 *new_base_branch_ref = NULL;
5016 err = lock_worktree(worktree, LOCK_EX);
5020 err = open_fileindex(fileindex, &fileindex_path, worktree);
5024 ok_arg.worktree = worktree;
5026 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5031 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5035 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5039 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5043 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5048 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5051 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5052 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5056 err = got_ref_alloc_symref(new_base_branch_ref,
5057 new_base_branch_ref_name, wt_branch);
5060 err = got_ref_write(*new_base_branch_ref, repo);
5064 /* TODO Lock original branch's ref while rebasing? */
5066 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5070 err = got_ref_write(branch_ref, repo);
5074 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5075 worktree->base_commit_id);
5078 err = got_ref_write(*tmp_branch, repo);
5082 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5086 free(fileindex_path);
5087 free(tmp_branch_name);
5088 free(new_base_branch_ref_name);
5089 free(branch_ref_name);
5091 got_ref_close(branch_ref);
5093 got_ref_close(wt_branch);
5094 free(wt_branch_tip);
5096 if (*new_base_branch_ref) {
5097 got_ref_close(*new_base_branch_ref);
5098 *new_base_branch_ref = NULL;
5101 got_ref_close(*tmp_branch);
5105 got_fileindex_free(*fileindex);
5108 lock_worktree(worktree, LOCK_SH);
5113 const struct got_error *
5114 got_worktree_rebase_continue(struct got_object_id **commit_id,
5115 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5116 struct got_reference **branch, struct got_fileindex **fileindex,
5117 struct got_worktree *worktree, struct got_repository *repo)
5119 const struct got_error *err;
5120 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5121 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5122 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5123 char *fileindex_path = NULL;
5124 int have_staged_files = 0;
5127 *new_base_branch = NULL;
5132 err = lock_worktree(worktree, LOCK_EX);
5136 err = open_fileindex(fileindex, &fileindex_path, worktree);
5140 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5141 &have_staged_files);
5142 if (err && err->code != GOT_ERR_CANCELLED)
5144 if (have_staged_files) {
5145 err = got_error(GOT_ERR_STAGED_PATHS);
5149 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5153 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5157 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5161 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5165 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5169 err = got_ref_open(branch, repo,
5170 got_ref_get_symref_target(branch_ref), 0);
5174 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5178 err = got_ref_resolve(commit_id, repo, commit_ref);
5182 err = got_ref_open(new_base_branch, repo,
5183 new_base_branch_ref_name, 0);
5187 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5191 free(commit_ref_name);
5192 free(branch_ref_name);
5193 free(fileindex_path);
5195 got_ref_close(commit_ref);
5197 got_ref_close(branch_ref);
5202 got_ref_close(*tmp_branch);
5205 if (*new_base_branch) {
5206 got_ref_close(*new_base_branch);
5207 *new_base_branch = NULL;
5210 got_ref_close(*branch);
5214 got_fileindex_free(*fileindex);
5217 lock_worktree(worktree, LOCK_SH);
5222 const struct got_error *
5223 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5225 const struct got_error *err;
5226 char *tmp_branch_name = NULL;
5228 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5232 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5233 free(tmp_branch_name);
5237 static const struct got_error *
5238 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5239 char **logmsg, void *arg)
5245 static const struct got_error *
5246 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5247 const char *path, struct got_object_id *blob_id,
5248 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5249 int dirfd, const char *de_name)
5254 struct collect_merged_paths_arg {
5255 got_worktree_checkout_cb progress_cb;
5257 struct got_pathlist_head *merged_paths;
5260 static const struct got_error *
5261 collect_merged_paths(void *arg, unsigned char status, const char *path)
5263 const struct got_error *err;
5264 struct collect_merged_paths_arg *a = arg;
5266 struct got_pathlist_entry *new;
5268 err = (*a->progress_cb)(a->progress_arg, status, path);
5272 if (status != GOT_STATUS_MERGE &&
5273 status != GOT_STATUS_ADD &&
5274 status != GOT_STATUS_DELETE &&
5275 status != GOT_STATUS_CONFLICT)
5280 return got_error_from_errno("strdup");
5282 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5283 if (err || new == NULL)
5289 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5291 struct got_pathlist_entry *pe;
5293 TAILQ_FOREACH(pe, merged_paths, entry)
5294 free((char *)pe->path);
5296 got_pathlist_free(merged_paths);
5299 static const struct got_error *
5300 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5301 int is_rebase, struct got_repository *repo)
5303 const struct got_error *err;
5304 struct got_reference *commit_ref = NULL;
5306 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5308 if (err->code != GOT_ERR_NOT_REF)
5310 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5313 err = got_ref_write(commit_ref, repo);
5316 } else if (is_rebase) {
5317 struct got_object_id *stored_id;
5320 err = got_ref_resolve(&stored_id, repo, commit_ref);
5323 cmp = got_object_id_cmp(commit_id, stored_id);
5326 err = got_error(GOT_ERR_REBASE_COMMITID);
5332 got_ref_close(commit_ref);
5336 static const struct got_error *
5337 rebase_merge_files(struct got_pathlist_head *merged_paths,
5338 const char *commit_ref_name, struct got_worktree *worktree,
5339 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5340 struct got_object_id *commit_id, struct got_repository *repo,
5341 got_worktree_checkout_cb progress_cb, void *progress_arg,
5342 got_cancel_cb cancel_cb, void *cancel_arg)
5344 const struct got_error *err;
5345 struct got_reference *commit_ref = NULL;
5346 struct collect_merged_paths_arg cmp_arg;
5347 char *fileindex_path;
5349 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5351 err = get_fileindex_path(&fileindex_path, worktree);
5355 cmp_arg.progress_cb = progress_cb;
5356 cmp_arg.progress_arg = progress_arg;
5357 cmp_arg.merged_paths = merged_paths;
5358 err = merge_files(worktree, fileindex, fileindex_path,
5359 parent_commit_id, commit_id, repo, collect_merged_paths,
5360 &cmp_arg, cancel_cb, cancel_arg);
5362 got_ref_close(commit_ref);
5366 const struct got_error *
5367 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5368 struct got_worktree *worktree, struct got_fileindex *fileindex,
5369 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5370 struct got_repository *repo,
5371 got_worktree_checkout_cb progress_cb, void *progress_arg,
5372 got_cancel_cb cancel_cb, void *cancel_arg)
5374 const struct got_error *err;
5375 char *commit_ref_name;
5377 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5381 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5385 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5386 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5387 progress_arg, cancel_cb, cancel_arg);
5389 free(commit_ref_name);
5393 const struct got_error *
5394 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5395 struct got_worktree *worktree, struct got_fileindex *fileindex,
5396 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5397 struct got_repository *repo,
5398 got_worktree_checkout_cb progress_cb, void *progress_arg,
5399 got_cancel_cb cancel_cb, void *cancel_arg)
5401 const struct got_error *err;
5402 char *commit_ref_name;
5404 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5408 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5412 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5413 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5414 progress_arg, cancel_cb, cancel_arg);
5416 free(commit_ref_name);
5420 static const struct got_error *
5421 rebase_commit(struct got_object_id **new_commit_id,
5422 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5423 struct got_worktree *worktree, struct got_fileindex *fileindex,
5424 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5425 const char *new_logmsg, struct got_repository *repo)
5427 const struct got_error *err, *sync_err;
5428 struct got_pathlist_head commitable_paths;
5429 struct collect_commitables_arg cc_arg;
5430 char *fileindex_path = NULL;
5431 struct got_reference *head_ref = NULL;
5432 struct got_object_id *head_commit_id = NULL;
5433 char *logmsg = NULL;
5435 TAILQ_INIT(&commitable_paths);
5436 *new_commit_id = NULL;
5438 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5440 err = get_fileindex_path(&fileindex_path, worktree);
5444 cc_arg.commitable_paths = &commitable_paths;
5445 cc_arg.worktree = worktree;
5447 cc_arg.have_staged_files = 0;
5449 * If possible get the status of individual files directly to
5450 * avoid crawling the entire work tree once per rebased commit.
5451 * TODO: Ideally, merged_paths would contain a list of commitables
5452 * we could use so we could skip worktree_status() entirely.
5455 struct got_pathlist_entry *pe;
5456 TAILQ_FOREACH(pe, merged_paths, entry) {
5457 err = worktree_status(worktree, pe->path, fileindex,
5458 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5464 err = worktree_status(worktree, "", fileindex, repo,
5465 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5470 if (TAILQ_EMPTY(&commitable_paths)) {
5471 /* No-op change; commit will be elided. */
5472 err = got_ref_delete(commit_ref, repo);
5475 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5479 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5483 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5488 logmsg = strdup(new_logmsg);
5489 if (logmsg == NULL) {
5490 err = got_error_from_errno("strdup");
5494 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5499 /* NB: commit_worktree will call free(logmsg) */
5500 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5501 worktree, got_object_commit_get_author(orig_commit),
5502 got_object_commit_get_committer(orig_commit),
5503 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5507 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5511 err = got_ref_delete(commit_ref, repo);
5515 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5517 sync_err = sync_fileindex(fileindex, fileindex_path);
5518 if (sync_err && err == NULL)
5521 free(fileindex_path);
5522 free(head_commit_id);
5524 got_ref_close(head_ref);
5526 free(*new_commit_id);
5527 *new_commit_id = NULL;
5532 const struct got_error *
5533 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5534 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5535 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5536 struct got_commit_object *orig_commit,
5537 struct got_object_id *orig_commit_id, struct got_repository *repo)
5539 const struct got_error *err;
5540 char *commit_ref_name;
5541 struct got_reference *commit_ref = NULL;
5542 struct got_object_id *commit_id = NULL;
5544 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5548 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5551 err = got_ref_resolve(&commit_id, repo, commit_ref);
5554 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5555 err = got_error(GOT_ERR_REBASE_COMMITID);
5559 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5560 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5563 got_ref_close(commit_ref);
5564 free(commit_ref_name);
5569 const struct got_error *
5570 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5571 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5572 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5573 struct got_commit_object *orig_commit,
5574 struct got_object_id *orig_commit_id, const char *new_logmsg,
5575 struct got_repository *repo)
5577 const struct got_error *err;
5578 char *commit_ref_name;
5579 struct got_reference *commit_ref = NULL;
5581 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5585 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5589 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5590 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5593 got_ref_close(commit_ref);
5594 free(commit_ref_name);
5598 const struct got_error *
5599 got_worktree_rebase_postpone(struct got_worktree *worktree,
5600 struct got_fileindex *fileindex)
5603 got_fileindex_free(fileindex);
5604 return lock_worktree(worktree, LOCK_SH);
5607 static const struct got_error *
5608 delete_ref(const char *name, struct got_repository *repo)
5610 const struct got_error *err;
5611 struct got_reference *ref;
5613 err = got_ref_open(&ref, repo, name, 0);
5615 if (err->code == GOT_ERR_NOT_REF)
5620 err = got_ref_delete(ref, repo);
5625 static const struct got_error *
5626 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5628 const struct got_error *err;
5629 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5630 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5632 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5635 err = delete_ref(tmp_branch_name, repo);
5639 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5642 err = delete_ref(new_base_branch_ref_name, repo);
5646 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5649 err = delete_ref(branch_ref_name, repo);
5653 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5656 err = delete_ref(commit_ref_name, repo);
5661 free(tmp_branch_name);
5662 free(new_base_branch_ref_name);
5663 free(branch_ref_name);
5664 free(commit_ref_name);
5668 const struct got_error *
5669 got_worktree_rebase_complete(struct got_worktree *worktree,
5670 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5671 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5672 struct got_repository *repo)
5674 const struct got_error *err, *unlockerr;
5675 struct got_object_id *new_head_commit_id = NULL;
5677 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5681 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5685 err = got_ref_write(rebased_branch, repo);
5689 err = got_worktree_set_head_ref(worktree, rebased_branch);
5693 err = delete_rebase_refs(worktree, repo);
5696 got_fileindex_free(fileindex);
5697 free(new_head_commit_id);
5698 unlockerr = lock_worktree(worktree, LOCK_SH);
5699 if (unlockerr && err == NULL)
5704 const struct got_error *
5705 got_worktree_rebase_abort(struct got_worktree *worktree,
5706 struct got_fileindex *fileindex, struct got_repository *repo,
5707 struct got_reference *new_base_branch,
5708 got_worktree_checkout_cb progress_cb, void *progress_arg)
5710 const struct got_error *err, *unlockerr, *sync_err;
5711 struct got_reference *resolved = NULL;
5712 struct got_object_id *commit_id = NULL;
5713 char *fileindex_path = NULL;
5714 struct revert_file_args rfa;
5715 struct got_object_id *tree_id = NULL;
5717 err = lock_worktree(worktree, LOCK_EX);
5721 err = got_ref_open(&resolved, repo,
5722 got_ref_get_symref_target(new_base_branch), 0);
5726 err = got_worktree_set_head_ref(worktree, resolved);
5731 * XXX commits to the base branch could have happened while
5732 * we were busy rebasing; should we store the original commit ID
5733 * when rebase begins and read it back here?
5735 err = got_ref_resolve(&commit_id, repo, resolved);
5739 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5743 err = got_object_id_by_path(&tree_id, repo,
5744 worktree->base_commit_id, worktree->path_prefix);
5748 err = delete_rebase_refs(worktree, repo);
5752 err = get_fileindex_path(&fileindex_path, worktree);
5756 rfa.worktree = worktree;
5757 rfa.fileindex = fileindex;
5758 rfa.progress_cb = progress_cb;
5759 rfa.progress_arg = progress_arg;
5760 rfa.patch_cb = NULL;
5761 rfa.patch_arg = NULL;
5763 err = worktree_status(worktree, "", fileindex, repo,
5764 revert_file, &rfa, NULL, NULL, 0, 0);
5768 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5769 repo, progress_cb, progress_arg, NULL, NULL);
5771 sync_err = sync_fileindex(fileindex, fileindex_path);
5772 if (sync_err && err == NULL)
5775 got_ref_close(resolved);
5779 got_fileindex_free(fileindex);
5780 free(fileindex_path);
5782 unlockerr = lock_worktree(worktree, LOCK_SH);
5783 if (unlockerr && err == NULL)
5788 const struct got_error *
5789 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5790 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5791 struct got_fileindex **fileindex, struct got_worktree *worktree,
5792 struct got_repository *repo)
5794 const struct got_error *err = NULL;
5795 char *tmp_branch_name = NULL;
5796 char *branch_ref_name = NULL;
5797 char *base_commit_ref_name = NULL;
5798 char *fileindex_path = NULL;
5799 struct check_rebase_ok_arg ok_arg;
5800 struct got_reference *wt_branch = NULL;
5801 struct got_reference *base_commit_ref = NULL;
5805 *base_commit_id = NULL;
5808 err = lock_worktree(worktree, LOCK_EX);
5812 err = open_fileindex(fileindex, &fileindex_path, worktree);
5816 ok_arg.worktree = worktree;
5818 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5823 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5827 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5831 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5836 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5841 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5845 err = got_ref_write(*branch_ref, repo);
5849 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5850 worktree->base_commit_id);
5853 err = got_ref_write(base_commit_ref, repo);
5856 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5857 if (*base_commit_id == NULL) {
5858 err = got_error_from_errno("got_object_id_dup");
5862 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5863 worktree->base_commit_id);
5866 err = got_ref_write(*tmp_branch, repo);
5870 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5874 free(fileindex_path);
5875 free(tmp_branch_name);
5876 free(branch_ref_name);
5877 free(base_commit_ref_name);
5879 got_ref_close(wt_branch);
5882 got_ref_close(*branch_ref);
5886 got_ref_close(*tmp_branch);
5889 free(*base_commit_id);
5891 got_fileindex_free(*fileindex);
5894 lock_worktree(worktree, LOCK_SH);
5899 const struct got_error *
5900 got_worktree_histedit_postpone(struct got_worktree *worktree,
5901 struct got_fileindex *fileindex)
5904 got_fileindex_free(fileindex);
5905 return lock_worktree(worktree, LOCK_SH);
5908 const struct got_error *
5909 got_worktree_histedit_in_progress(int *in_progress,
5910 struct got_worktree *worktree)
5912 const struct got_error *err;
5913 char *tmp_branch_name = NULL;
5915 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5919 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5920 free(tmp_branch_name);
5924 const struct got_error *
5925 got_worktree_histedit_continue(struct got_object_id **commit_id,
5926 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5927 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5928 struct got_worktree *worktree, struct got_repository *repo)
5930 const struct got_error *err;
5931 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5932 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5933 struct got_reference *commit_ref = NULL;
5934 struct got_reference *base_commit_ref = NULL;
5935 char *fileindex_path = NULL;
5936 int have_staged_files = 0;
5940 *base_commit_id = NULL;
5943 err = lock_worktree(worktree, LOCK_EX);
5947 err = open_fileindex(fileindex, &fileindex_path, worktree);
5951 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5952 &have_staged_files);
5953 if (err && err->code != GOT_ERR_CANCELLED)
5955 if (have_staged_files) {
5956 err = got_error(GOT_ERR_STAGED_PATHS);
5960 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5964 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5968 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5972 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5977 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5981 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5984 err = got_ref_resolve(commit_id, repo, commit_ref);
5988 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5991 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5995 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5999 free(commit_ref_name);
6000 free(branch_ref_name);
6001 free(fileindex_path);
6003 got_ref_close(commit_ref);
6004 if (base_commit_ref)
6005 got_ref_close(base_commit_ref);
6009 free(*base_commit_id);
6010 *base_commit_id = NULL;
6012 got_ref_close(*tmp_branch);
6016 got_fileindex_free(*fileindex);
6019 lock_worktree(worktree, LOCK_EX);
6024 static const struct got_error *
6025 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6027 const struct got_error *err;
6028 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6029 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6031 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6034 err = delete_ref(tmp_branch_name, repo);
6038 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6042 err = delete_ref(base_commit_ref_name, repo);
6046 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6049 err = delete_ref(branch_ref_name, repo);
6053 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6056 err = delete_ref(commit_ref_name, repo);
6060 free(tmp_branch_name);
6061 free(base_commit_ref_name);
6062 free(branch_ref_name);
6063 free(commit_ref_name);
6067 const struct got_error *
6068 got_worktree_histedit_abort(struct got_worktree *worktree,
6069 struct got_fileindex *fileindex, struct got_repository *repo,
6070 struct got_reference *branch, struct got_object_id *base_commit_id,
6071 got_worktree_checkout_cb progress_cb, void *progress_arg)
6073 const struct got_error *err, *unlockerr, *sync_err;
6074 struct got_reference *resolved = NULL;
6075 char *fileindex_path = NULL;
6076 struct got_object_id *tree_id = NULL;
6077 struct revert_file_args rfa;
6079 err = lock_worktree(worktree, LOCK_EX);
6083 err = got_ref_open(&resolved, repo,
6084 got_ref_get_symref_target(branch), 0);
6088 err = got_worktree_set_head_ref(worktree, resolved);
6092 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6096 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6097 worktree->path_prefix);
6101 err = delete_histedit_refs(worktree, repo);
6105 err = get_fileindex_path(&fileindex_path, worktree);
6109 rfa.worktree = worktree;
6110 rfa.fileindex = fileindex;
6111 rfa.progress_cb = progress_cb;
6112 rfa.progress_arg = progress_arg;
6113 rfa.patch_cb = NULL;
6114 rfa.patch_arg = NULL;
6116 err = worktree_status(worktree, "", fileindex, repo,
6117 revert_file, &rfa, NULL, NULL, 0, 0);
6121 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6122 repo, progress_cb, progress_arg, NULL, NULL);
6124 sync_err = sync_fileindex(fileindex, fileindex_path);
6125 if (sync_err && err == NULL)
6128 got_ref_close(resolved);
6130 free(fileindex_path);
6132 unlockerr = lock_worktree(worktree, LOCK_SH);
6133 if (unlockerr && err == NULL)
6138 const struct got_error *
6139 got_worktree_histedit_complete(struct got_worktree *worktree,
6140 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6141 struct got_reference *edited_branch, struct got_repository *repo)
6143 const struct got_error *err, *unlockerr;
6144 struct got_object_id *new_head_commit_id = NULL;
6145 struct got_reference *resolved = NULL;
6147 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6151 err = got_ref_open(&resolved, repo,
6152 got_ref_get_symref_target(edited_branch), 0);
6156 err = got_ref_change_ref(resolved, new_head_commit_id);
6160 err = got_ref_write(resolved, repo);
6164 err = got_worktree_set_head_ref(worktree, resolved);
6168 err = delete_histedit_refs(worktree, repo);
6171 got_fileindex_free(fileindex);
6172 free(new_head_commit_id);
6173 unlockerr = lock_worktree(worktree, LOCK_SH);
6174 if (unlockerr && err == NULL)
6179 const struct got_error *
6180 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6181 struct got_object_id *commit_id, struct got_repository *repo)
6183 const struct got_error *err;
6184 char *commit_ref_name;
6186 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6190 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6194 err = delete_ref(commit_ref_name, repo);
6196 free(commit_ref_name);
6200 const struct got_error *
6201 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6202 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6203 struct got_worktree *worktree, const char *refname,
6204 struct got_repository *repo)
6206 const struct got_error *err = NULL;
6207 char *fileindex_path = NULL;
6208 struct check_rebase_ok_arg ok_arg;
6212 *base_branch_ref = NULL;
6214 err = lock_worktree(worktree, LOCK_EX);
6218 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6219 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6220 "cannot integrate a branch into itself; "
6221 "update -b or different branch name required");
6225 err = open_fileindex(fileindex, &fileindex_path, worktree);
6229 /* Preconditions are the same as for rebase. */
6230 ok_arg.worktree = worktree;
6232 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6237 err = got_ref_open(branch_ref, repo, refname, 1);
6241 err = got_ref_open(base_branch_ref, repo,
6242 got_worktree_get_head_ref_name(worktree), 1);
6246 got_ref_close(*branch_ref);
6249 if (*base_branch_ref) {
6250 got_ref_close(*base_branch_ref);
6251 *base_branch_ref = NULL;
6254 got_fileindex_free(*fileindex);
6257 lock_worktree(worktree, LOCK_SH);
6262 const struct got_error *
6263 got_worktree_integrate_continue(struct got_worktree *worktree,
6264 struct got_fileindex *fileindex, struct got_repository *repo,
6265 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6266 got_worktree_checkout_cb progress_cb, void *progress_arg,
6267 got_cancel_cb cancel_cb, void *cancel_arg)
6269 const struct got_error *err = NULL, *sync_err, *unlockerr;
6270 char *fileindex_path = NULL;
6271 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6273 err = get_fileindex_path(&fileindex_path, worktree);
6277 err = got_ref_resolve(&commit_id, repo, branch_ref);
6281 err = got_object_id_by_path(&tree_id, repo, commit_id,
6282 worktree->path_prefix);
6286 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6290 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6291 progress_cb, progress_arg, cancel_cb, cancel_arg);
6295 err = got_ref_change_ref(base_branch_ref, commit_id);
6299 err = got_ref_write(base_branch_ref, repo);
6301 sync_err = sync_fileindex(fileindex, fileindex_path);
6302 if (sync_err && err == NULL)
6306 unlockerr = got_ref_unlock(branch_ref);
6307 if (unlockerr && err == NULL)
6309 got_ref_close(branch_ref);
6311 unlockerr = got_ref_unlock(base_branch_ref);
6312 if (unlockerr && err == NULL)
6314 got_ref_close(base_branch_ref);
6316 got_fileindex_free(fileindex);
6317 free(fileindex_path);
6320 unlockerr = lock_worktree(worktree, LOCK_SH);
6321 if (unlockerr && err == NULL)
6326 const struct got_error *
6327 got_worktree_integrate_abort(struct got_worktree *worktree,
6328 struct got_fileindex *fileindex, struct got_repository *repo,
6329 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6331 const struct got_error *err = NULL, *unlockerr = NULL;
6333 got_fileindex_free(fileindex);
6335 err = lock_worktree(worktree, LOCK_SH);
6337 unlockerr = got_ref_unlock(branch_ref);
6338 if (unlockerr && err == NULL)
6340 got_ref_close(branch_ref);
6342 unlockerr = got_ref_unlock(base_branch_ref);
6343 if (unlockerr && err == NULL)
6345 got_ref_close(base_branch_ref);
6350 struct check_stage_ok_arg {
6351 struct got_object_id *head_commit_id;
6352 struct got_worktree *worktree;
6353 struct got_fileindex *fileindex;
6354 struct got_repository *repo;
6358 const struct got_error *
6359 check_stage_ok(void *arg, unsigned char status,
6360 unsigned char staged_status, const char *relpath,
6361 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6362 struct got_object_id *commit_id, int dirfd, const char *de_name)
6364 struct check_stage_ok_arg *a = arg;
6365 const struct got_error *err = NULL;
6366 struct got_fileindex_entry *ie;
6367 struct got_object_id base_commit_id;
6368 struct got_object_id *base_commit_idp = NULL;
6369 char *in_repo_path = NULL, *p;
6371 if (status == GOT_STATUS_UNVERSIONED ||
6372 status == GOT_STATUS_NO_CHANGE)
6374 if (status == GOT_STATUS_NONEXISTENT)
6375 return got_error_set_errno(ENOENT, relpath);
6377 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6379 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6381 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6382 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6384 return got_error_from_errno("asprintf");
6386 if (got_fileindex_entry_has_commit(ie)) {
6387 memcpy(base_commit_id.sha1, ie->commit_sha1,
6388 SHA1_DIGEST_LENGTH);
6389 base_commit_idp = &base_commit_id;
6392 if (status == GOT_STATUS_CONFLICT) {
6393 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6395 } else if (status != GOT_STATUS_ADD &&
6396 status != GOT_STATUS_MODIFY &&
6397 status != GOT_STATUS_DELETE) {
6398 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6402 a->have_changes = 1;
6407 err = check_out_of_date(p, status, staged_status,
6408 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6409 GOT_ERR_STAGE_OUT_OF_DATE);
6415 struct stage_path_arg {
6416 struct got_worktree *worktree;
6417 struct got_fileindex *fileindex;
6418 struct got_repository *repo;
6419 got_worktree_status_cb status_cb;
6421 got_worktree_patch_cb patch_cb;
6423 int staged_something;
6426 static const struct got_error *
6427 stage_path(void *arg, unsigned char status,
6428 unsigned char staged_status, const char *relpath,
6429 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6430 struct got_object_id *commit_id, int dirfd, const char *de_name)
6432 struct stage_path_arg *a = arg;
6433 const struct got_error *err = NULL;
6434 struct got_fileindex_entry *ie;
6435 char *ondisk_path = NULL, *path_content = NULL;
6437 struct got_object_id *new_staged_blob_id = NULL;
6439 if (status == GOT_STATUS_UNVERSIONED)
6442 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6444 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6446 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6448 return got_error_from_errno("asprintf");
6451 case GOT_STATUS_ADD:
6452 case GOT_STATUS_MODIFY:
6454 if (status == GOT_STATUS_ADD) {
6455 int choice = GOT_PATCH_CHOICE_NONE;
6456 err = (*a->patch_cb)(&choice, a->patch_arg,
6457 status, ie->path, NULL, 1, 1);
6460 if (choice != GOT_PATCH_CHOICE_YES)
6463 err = create_patched_content(&path_content, 0,
6464 staged_blob_id ? staged_blob_id : blob_id,
6465 ondisk_path, dirfd, de_name, ie->path,
6466 a->repo, a->patch_cb, a->patch_arg);
6467 if (err || path_content == NULL)
6471 err = got_object_blob_create(&new_staged_blob_id,
6472 path_content ? path_content : ondisk_path, a->repo);
6475 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6476 SHA1_DIGEST_LENGTH);
6477 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6478 stage = GOT_FILEIDX_STAGE_ADD;
6480 stage = GOT_FILEIDX_STAGE_MODIFY;
6481 got_fileindex_entry_stage_set(ie, stage);
6482 a->staged_something = 1;
6483 if (a->status_cb == NULL)
6485 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6486 get_staged_status(ie), relpath, blob_id,
6487 new_staged_blob_id, NULL, dirfd, de_name);
6489 case GOT_STATUS_DELETE:
6490 if (staged_status == GOT_STATUS_DELETE)
6493 int choice = GOT_PATCH_CHOICE_NONE;
6494 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6495 ie->path, NULL, 1, 1);
6498 if (choice == GOT_PATCH_CHOICE_NO)
6500 if (choice != GOT_PATCH_CHOICE_YES) {
6501 err = got_error(GOT_ERR_PATCH_CHOICE);
6505 stage = GOT_FILEIDX_STAGE_DELETE;
6506 got_fileindex_entry_stage_set(ie, stage);
6507 a->staged_something = 1;
6508 if (a->status_cb == NULL)
6510 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6511 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6514 case GOT_STATUS_NO_CHANGE:
6516 case GOT_STATUS_CONFLICT:
6517 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6519 case GOT_STATUS_NONEXISTENT:
6520 err = got_error_set_errno(ENOENT, relpath);
6523 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6527 if (path_content && unlink(path_content) == -1 && err == NULL)
6528 err = got_error_from_errno2("unlink", path_content);
6531 free(new_staged_blob_id);
6535 const struct got_error *
6536 got_worktree_stage(struct got_worktree *worktree,
6537 struct got_pathlist_head *paths,
6538 got_worktree_status_cb status_cb, void *status_arg,
6539 got_worktree_patch_cb patch_cb, void *patch_arg,
6540 struct got_repository *repo)
6542 const struct got_error *err = NULL, *sync_err, *unlockerr;
6543 struct got_pathlist_entry *pe;
6544 struct got_fileindex *fileindex = NULL;
6545 char *fileindex_path = NULL;
6546 struct got_reference *head_ref = NULL;
6547 struct got_object_id *head_commit_id = NULL;
6548 struct check_stage_ok_arg oka;
6549 struct stage_path_arg spa;
6551 err = lock_worktree(worktree, LOCK_EX);
6555 err = got_ref_open(&head_ref, repo,
6556 got_worktree_get_head_ref_name(worktree), 0);
6559 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6562 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6566 /* Check pre-conditions before staging anything. */
6567 oka.head_commit_id = head_commit_id;
6568 oka.worktree = worktree;
6569 oka.fileindex = fileindex;
6571 oka.have_changes = 0;
6572 TAILQ_FOREACH(pe, paths, entry) {
6573 err = worktree_status(worktree, pe->path, fileindex, repo,
6574 check_stage_ok, &oka, NULL, NULL, 0, 0);
6578 if (!oka.have_changes) {
6579 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6583 spa.worktree = worktree;
6584 spa.fileindex = fileindex;
6586 spa.patch_cb = patch_cb;
6587 spa.patch_arg = patch_arg;
6588 spa.status_cb = status_cb;
6589 spa.status_arg = status_arg;
6590 spa.staged_something = 0;
6591 TAILQ_FOREACH(pe, paths, entry) {
6592 err = worktree_status(worktree, pe->path, fileindex, repo,
6593 stage_path, &spa, NULL, NULL, 0, 0);
6597 if (!spa.staged_something) {
6598 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6602 sync_err = sync_fileindex(fileindex, fileindex_path);
6603 if (sync_err && err == NULL)
6607 got_ref_close(head_ref);
6608 free(head_commit_id);
6609 free(fileindex_path);
6611 got_fileindex_free(fileindex);
6612 unlockerr = lock_worktree(worktree, LOCK_SH);
6613 if (unlockerr && err == NULL)
6618 struct unstage_path_arg {
6619 struct got_worktree *worktree;
6620 struct got_fileindex *fileindex;
6621 struct got_repository *repo;
6622 got_worktree_checkout_cb progress_cb;
6624 got_worktree_patch_cb patch_cb;
6628 static const struct got_error *
6629 create_unstaged_content(char **path_unstaged_content,
6630 char **path_new_staged_content, struct got_object_id *blob_id,
6631 struct got_object_id *staged_blob_id, const char *relpath,
6632 struct got_repository *repo,
6633 got_worktree_patch_cb patch_cb, void *patch_arg)
6635 const struct got_error *err;
6636 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6637 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6638 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6639 struct stat sb1, sb2;
6640 struct got_diff_changes *changes = NULL;
6641 struct got_diff_state *ds = NULL;
6642 struct got_diff_args *args = NULL;
6643 struct got_diff_change *change;
6644 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6645 int have_content = 0, have_rejected_content = 0;
6647 *path_unstaged_content = NULL;
6648 *path_new_staged_content = NULL;
6650 err = got_object_id_str(&label1, blob_id);
6653 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6657 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6661 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6665 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6669 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6673 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6677 if (stat(path1, &sb1) == -1) {
6678 err = got_error_from_errno2("stat", path1);
6682 if (stat(path2, &sb2) == -1) {
6683 err = got_error_from_errno2("stat", path2);
6687 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6688 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6692 err = got_opentemp_named(path_unstaged_content, &outfile,
6693 "got-unstaged-content");
6696 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6697 "got-new-staged-content");
6701 if (fseek(f1, 0L, SEEK_SET) == -1) {
6702 err = got_ferror(f1, GOT_ERR_IO);
6705 if (fseek(f2, 0L, SEEK_SET) == -1) {
6706 err = got_ferror(f2, GOT_ERR_IO);
6709 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6711 err = apply_or_reject_change(&choice, change, ++n,
6712 changes->nchanges, ds, args, diff_flags, relpath,
6713 f1, f2, &line_cur1, &line_cur2,
6714 outfile, rejectfile, patch_cb, patch_arg);
6717 if (choice == GOT_PATCH_CHOICE_YES)
6720 have_rejected_content = 1;
6721 if (choice == GOT_PATCH_CHOICE_QUIT)
6724 if (have_content || have_rejected_content)
6725 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6726 outfile, rejectfile);
6730 got_object_blob_close(blob);
6732 got_object_blob_close(staged_blob);
6733 if (f1 && fclose(f1) == EOF && err == NULL)
6734 err = got_error_from_errno2("fclose", path1);
6735 if (f2 && fclose(f2) == EOF && err == NULL)
6736 err = got_error_from_errno2("fclose", path2);
6737 if (outfile && fclose(outfile) == EOF && err == NULL)
6738 err = got_error_from_errno2("fclose", *path_unstaged_content);
6739 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6740 err = got_error_from_errno2("fclose", *path_new_staged_content);
6741 if (path1 && unlink(path1) == -1 && err == NULL)
6742 err = got_error_from_errno2("unlink", path1);
6743 if (path2 && unlink(path2) == -1 && err == NULL)
6744 err = got_error_from_errno2("unlink", path2);
6745 if (err || !have_content) {
6746 if (*path_unstaged_content &&
6747 unlink(*path_unstaged_content) == -1 && err == NULL)
6748 err = got_error_from_errno2("unlink",
6749 *path_unstaged_content);
6750 free(*path_unstaged_content);
6751 *path_unstaged_content = NULL;
6753 if (err || !have_rejected_content) {
6754 if (*path_new_staged_content &&
6755 unlink(*path_new_staged_content) == -1 && err == NULL)
6756 err = got_error_from_errno2("unlink",
6757 *path_new_staged_content);
6758 free(*path_new_staged_content);
6759 *path_new_staged_content = NULL;
6763 got_diff_state_free(ds);
6767 got_diff_free_changes(changes);
6773 static const struct got_error *
6774 unstage_path(void *arg, unsigned char status,
6775 unsigned char staged_status, const char *relpath,
6776 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6777 struct got_object_id *commit_id, int dirfd, const char *de_name)
6779 const struct got_error *err = NULL;
6780 struct unstage_path_arg *a = arg;
6781 struct got_fileindex_entry *ie;
6782 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6783 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6784 char *path_new_staged_content = NULL;
6785 char *id_str = NULL, *label_orig = NULL;
6786 int local_changes_subsumed;
6789 if (staged_status != GOT_STATUS_ADD &&
6790 staged_status != GOT_STATUS_MODIFY &&
6791 staged_status != GOT_STATUS_DELETE)
6794 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6796 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6798 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6800 return got_error_from_errno("asprintf");
6802 err = got_object_id_str(&id_str,
6803 commit_id ? commit_id : a->worktree->base_commit_id);
6806 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6808 err = got_error_from_errno("asprintf");
6812 switch (staged_status) {
6813 case GOT_STATUS_MODIFY:
6814 err = got_object_open_as_blob(&blob_base, a->repo,
6819 case GOT_STATUS_ADD:
6821 if (staged_status == GOT_STATUS_ADD) {
6822 int choice = GOT_PATCH_CHOICE_NONE;
6823 err = (*a->patch_cb)(&choice, a->patch_arg,
6824 staged_status, ie->path, NULL, 1, 1);
6827 if (choice != GOT_PATCH_CHOICE_YES)
6830 err = create_unstaged_content(
6831 &path_unstaged_content,
6832 &path_new_staged_content, blob_id,
6833 staged_blob_id, ie->path, a->repo,
6834 a->patch_cb, a->patch_arg);
6835 if (err || path_unstaged_content == NULL)
6837 if (path_new_staged_content) {
6838 err = got_object_blob_create(
6840 path_new_staged_content,
6844 memcpy(ie->staged_blob_sha1,
6845 staged_blob_id->sha1,
6846 SHA1_DIGEST_LENGTH);
6848 err = merge_file(&local_changes_subsumed,
6849 a->worktree, blob_base, ondisk_path,
6850 relpath, got_fileindex_perms_to_st(ie),
6851 path_unstaged_content, label_orig,
6852 "unstaged", a->repo, a->progress_cb,
6855 path_new_staged_content == NULL)
6856 got_fileindex_entry_stage_set(ie,
6857 GOT_FILEIDX_STAGE_NONE);
6858 break; /* Done with this file. */
6861 err = got_object_open_as_blob(&blob_staged, a->repo,
6862 staged_blob_id, 8192);
6865 err = merge_blob(&local_changes_subsumed, a->worktree,
6866 blob_base, ondisk_path, relpath,
6867 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6868 commit_id ? commit_id : a->worktree->base_commit_id,
6869 a->repo, a->progress_cb, a->progress_arg);
6871 got_fileindex_entry_stage_set(ie,
6872 GOT_FILEIDX_STAGE_NONE);
6874 case GOT_STATUS_DELETE:
6876 int choice = GOT_PATCH_CHOICE_NONE;
6877 err = (*a->patch_cb)(&choice, a->patch_arg,
6878 staged_status, ie->path, NULL, 1, 1);
6881 if (choice == GOT_PATCH_CHOICE_NO)
6883 if (choice != GOT_PATCH_CHOICE_YES) {
6884 err = got_error(GOT_ERR_PATCH_CHOICE);
6888 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6889 err = get_file_status(&status, &sb, ie, ondisk_path,
6890 dirfd, de_name, a->repo);
6893 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6898 if (path_unstaged_content &&
6899 unlink(path_unstaged_content) == -1 && err == NULL)
6900 err = got_error_from_errno2("unlink", path_unstaged_content);
6901 if (path_new_staged_content &&
6902 unlink(path_new_staged_content) == -1 && err == NULL)
6903 err = got_error_from_errno2("unlink", path_new_staged_content);
6904 free(path_unstaged_content);
6905 free(path_new_staged_content);
6907 got_object_blob_close(blob_base);
6909 got_object_blob_close(blob_staged);
6915 const struct got_error *
6916 got_worktree_unstage(struct got_worktree *worktree,
6917 struct got_pathlist_head *paths,
6918 got_worktree_checkout_cb progress_cb, void *progress_arg,
6919 got_worktree_patch_cb patch_cb, void *patch_arg,
6920 struct got_repository *repo)
6922 const struct got_error *err = NULL, *sync_err, *unlockerr;
6923 struct got_pathlist_entry *pe;
6924 struct got_fileindex *fileindex = NULL;
6925 char *fileindex_path = NULL;
6926 struct unstage_path_arg upa;
6928 err = lock_worktree(worktree, LOCK_EX);
6932 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6936 upa.worktree = worktree;
6937 upa.fileindex = fileindex;
6939 upa.progress_cb = progress_cb;
6940 upa.progress_arg = progress_arg;
6941 upa.patch_cb = patch_cb;
6942 upa.patch_arg = patch_arg;
6943 TAILQ_FOREACH(pe, paths, entry) {
6944 err = worktree_status(worktree, pe->path, fileindex, repo,
6945 unstage_path, &upa, NULL, NULL, 0, 0);
6950 sync_err = sync_fileindex(fileindex, fileindex_path);
6951 if (sync_err && err == NULL)
6954 free(fileindex_path);
6956 got_fileindex_free(fileindex);
6957 unlockerr = lock_worktree(worktree, LOCK_SH);
6958 if (unlockerr && err == NULL)