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.
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static const struct got_error *
66 create_meta_file(const char *path_got, const char *name, const char *content)
68 const struct got_error *err = NULL;
71 if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 return got_error_from_errno("asprintf");
74 err = got_path_create_file(path, content);
79 static const struct got_error *
80 update_meta_file(const char *path_got, const char *name, const char *content)
82 const struct got_error *err = NULL;
87 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 err = got_error_from_errno("asprintf");
93 err = got_opentemp_named(&tmppath, &tmpfile, path);
98 int len = fprintf(tmpfile, "%s\n", content);
99 if (len != strlen(content) + 1) {
100 err = got_error_from_errno2("fprintf", tmppath);
105 if (rename(tmppath, path) != 0) {
106 err = got_error_from_errno3("rename", tmppath, path);
112 if (fclose(tmpfile) == EOF && err == NULL)
113 err = got_error_from_errno2("fclose", tmppath);
118 static const struct got_error *
119 read_meta_file(char **content, const char *path_got, const char *name)
121 const struct got_error *err = NULL;
129 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
130 err = got_error_from_errno("asprintf");
135 fd = open(path, O_RDONLY | O_NOFOLLOW);
138 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 err = got_error_from_errno2("open", path);
143 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
144 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
145 : got_error_from_errno2("flock", path));
149 if (fstat(fd, &sb) != 0) {
150 err = got_error_from_errno2("fstat", path);
153 *content = calloc(1, sb.st_size);
154 if (*content == NULL) {
155 err = got_error_from_errno("calloc");
159 n = read(fd, *content, sb.st_size);
160 if (n != sb.st_size) {
161 err = (n == -1 ? got_error_from_errno2("read", path) :
162 got_error_path(path, GOT_ERR_WORKTREE_META));
165 if ((*content)[sb.st_size - 1] != '\n') {
166 err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 (*content)[sb.st_size - 1] = '\0';
172 if (fd != -1 && close(fd) == -1 && err == NULL)
173 err = got_error_from_errno2("close", path_got);
182 static const struct got_error *
183 write_head_ref(const char *path_got, struct got_reference *head_ref)
185 const struct got_error *err = NULL;
188 if (got_ref_is_symbolic(head_ref)) {
189 refstr = got_ref_to_str(head_ref);
191 return got_error_from_errno("got_ref_to_str");
193 refstr = strdup(got_ref_get_name(head_ref));
195 return got_error_from_errno("strdup");
197 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
202 const struct got_error *
203 got_worktree_init(const char *path, struct got_reference *head_ref,
204 const char *prefix, struct got_repository *repo)
206 const struct got_error *err = NULL;
207 struct got_object_id *commit_id = NULL;
209 uint32_t uuid_status;
211 char *path_got = NULL;
212 char *formatstr = NULL;
213 char *absprefix = NULL;
214 char *basestr = NULL;
215 char *uuidstr = NULL;
217 if (strcmp(path, got_repo_get_path(repo)) == 0) {
218 err = got_error(GOT_ERR_WORKTREE_REPO);
222 err = got_ref_resolve(&commit_id, repo, head_ref);
225 err = got_object_get_type(&obj_type, repo, commit_id);
228 if (obj_type != GOT_OBJ_TYPE_COMMIT)
229 return got_error(GOT_ERR_OBJ_TYPE);
231 if (!got_path_is_absolute(prefix)) {
232 if (asprintf(&absprefix, "/%s", prefix) == -1)
233 return got_error_from_errno("asprintf");
236 /* Create top-level directory (may already exist). */
237 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
238 err = got_error_from_errno2("mkdir", path);
242 /* Create .got directory (may already exist). */
243 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
244 err = got_error_from_errno("asprintf");
247 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
248 err = got_error_from_errno2("mkdir", path_got);
252 /* Create an empty lock file. */
253 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
257 /* Create an empty file index. */
258 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
262 /* Write the HEAD reference. */
263 err = write_head_ref(path_got, head_ref);
267 /* Record our base commit. */
268 err = got_object_id_str(&basestr, commit_id);
271 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
275 /* Store path to repository. */
276 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
277 got_repo_get_path(repo));
281 /* Store in-repository path prefix. */
282 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
283 absprefix ? absprefix : prefix);
288 uuid_create(&uuid, &uuid_status);
289 if (uuid_status != uuid_s_ok) {
290 err = got_error_uuid(uuid_status, "uuid_create");
293 uuid_to_string(&uuid, &uuidstr, &uuid_status);
294 if (uuid_status != uuid_s_ok) {
295 err = got_error_uuid(uuid_status, "uuid_to_string");
298 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
302 /* Stamp work tree with format file. */
303 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
304 err = got_error_from_errno("asprintf");
307 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
321 static const struct got_error *
322 open_worktree(struct got_worktree **worktree, const char *path)
324 const struct got_error *err = NULL;
326 char *formatstr = NULL;
327 char *uuidstr = NULL;
328 char *path_lock = NULL;
329 char *base_commit_id_str = NULL;
330 int version, fd = -1;
332 struct got_repository *repo = NULL;
333 uint32_t uuid_status;
337 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
338 err = got_error_from_errno("asprintf");
343 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
344 err = got_error_from_errno("asprintf");
349 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
352 : got_error_from_errno2("open", path_lock));
356 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
360 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 err = got_error_msg(GOT_ERR_WORKTREE_META,
363 "could not parse work tree format version number");
366 if (version != GOT_WORKTREE_FORMAT_VERSION) {
367 err = got_error(GOT_ERR_WORKTREE_VERS);
371 *worktree = calloc(1, sizeof(**worktree));
372 if (*worktree == NULL) {
373 err = got_error_from_errno("calloc");
376 (*worktree)->lockfd = -1;
378 (*worktree)->root_path = realpath(path, NULL);
379 if ((*worktree)->root_path == NULL) {
380 err = got_error_from_errno2("realpath", path);
383 err = read_meta_file(&(*worktree)->repo_path, path_got,
384 GOT_WORKTREE_REPOSITORY);
388 err = read_meta_file(&(*worktree)->path_prefix, path_got,
389 GOT_WORKTREE_PATH_PREFIX);
393 err = read_meta_file(&base_commit_id_str, path_got,
394 GOT_WORKTREE_BASE_COMMIT);
398 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
402 if (uuid_status != uuid_s_ok) {
403 err = got_error_uuid(uuid_status, "uuid_from_string");
407 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
411 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
416 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
417 GOT_WORKTREE_HEAD_REF);
421 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
422 (*worktree)->root_path,
423 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
424 err = got_error_from_errno("asprintf");
428 err = got_gotconfig_read(&(*worktree)->gotconfig,
429 (*worktree)->gotconfig_path);
431 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
432 if ((*worktree)->root_fd == -1) {
433 err = got_error_from_errno2("open", (*worktree)->root_path);
438 const struct got_error *close_err = got_repo_close(repo);
444 free(base_commit_id_str);
450 if (*worktree != NULL)
451 got_worktree_close(*worktree);
454 (*worktree)->lockfd = fd;
459 const struct got_error *
460 got_worktree_open(struct got_worktree **worktree, const char *path)
462 const struct got_error *err = NULL;
465 worktree_path = strdup(path);
466 if (worktree_path == NULL)
467 return got_error_from_errno("strdup");
472 err = open_worktree(worktree, worktree_path);
473 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
481 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
483 err = got_path_dirname(&parent_path, worktree_path);
485 if (err->code != GOT_ERR_BAD_PATH) {
492 worktree_path = parent_path;
496 return got_error(GOT_ERR_NOT_WORKTREE);
499 const struct got_error *
500 got_worktree_close(struct got_worktree *worktree)
502 const struct got_error *err = NULL;
504 if (worktree->lockfd != -1) {
505 if (close(worktree->lockfd) == -1)
506 err = got_error_from_errno2("close",
507 got_worktree_get_root_path(worktree));
509 if (close(worktree->root_fd) == -1 && err == NULL)
510 err = got_error_from_errno2("close",
511 got_worktree_get_root_path(worktree));
512 free(worktree->repo_path);
513 free(worktree->path_prefix);
514 free(worktree->base_commit_id);
515 free(worktree->head_ref_name);
516 free(worktree->root_path);
517 free(worktree->gotconfig_path);
518 got_gotconfig_free(worktree->gotconfig);
524 got_worktree_get_root_path(struct got_worktree *worktree)
526 return worktree->root_path;
530 got_worktree_get_repo_path(struct got_worktree *worktree)
532 return worktree->repo_path;
535 got_worktree_get_path_prefix(struct got_worktree *worktree)
537 return worktree->path_prefix;
540 const struct got_error *
541 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
542 const char *path_prefix)
544 char *absprefix = NULL;
546 if (!got_path_is_absolute(path_prefix)) {
547 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
548 return got_error_from_errno("asprintf");
550 *match = (strcmp(absprefix ? absprefix : path_prefix,
551 worktree->path_prefix) == 0);
557 got_worktree_get_head_ref_name(struct got_worktree *worktree)
559 return worktree->head_ref_name;
562 const struct got_error *
563 got_worktree_set_head_ref(struct got_worktree *worktree,
564 struct got_reference *head_ref)
566 const struct got_error *err = NULL;
567 char *path_got = NULL, *head_ref_name = NULL;
569 if (asprintf(&path_got, "%s/%s", worktree->root_path,
570 GOT_WORKTREE_GOT_DIR) == -1) {
571 err = got_error_from_errno("asprintf");
576 head_ref_name = strdup(got_ref_get_name(head_ref));
577 if (head_ref_name == NULL) {
578 err = got_error_from_errno("strdup");
582 err = write_head_ref(path_got, head_ref);
586 free(worktree->head_ref_name);
587 worktree->head_ref_name = head_ref_name;
595 struct got_object_id *
596 got_worktree_get_base_commit_id(struct got_worktree *worktree)
598 return worktree->base_commit_id;
601 const struct got_error *
602 got_worktree_set_base_commit_id(struct got_worktree *worktree,
603 struct got_repository *repo, struct got_object_id *commit_id)
605 const struct got_error *err;
606 struct got_object *obj = NULL;
608 char *path_got = NULL;
610 if (asprintf(&path_got, "%s/%s", worktree->root_path,
611 GOT_WORKTREE_GOT_DIR) == -1) {
612 err = got_error_from_errno("asprintf");
617 err = got_object_open(&obj, repo, commit_id);
621 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
622 err = got_error(GOT_ERR_OBJ_TYPE);
626 /* Record our base commit. */
627 err = got_object_id_str(&id_str, commit_id);
630 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
634 free(worktree->base_commit_id);
635 worktree->base_commit_id = got_object_id_dup(commit_id);
636 if (worktree->base_commit_id == NULL) {
637 err = got_error_from_errno("got_object_id_dup");
642 got_object_close(obj);
648 const struct got_gotconfig *
649 got_worktree_get_gotconfig(struct got_worktree *worktree)
651 return worktree->gotconfig;
654 static const struct got_error *
655 lock_worktree(struct got_worktree *worktree, int operation)
657 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
658 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
659 : got_error_from_errno2("flock",
660 got_worktree_get_root_path(worktree)));
664 static const struct got_error *
665 add_dir_on_disk(struct got_worktree *worktree, const char *path)
667 const struct got_error *err = NULL;
670 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
671 return got_error_from_errno("asprintf");
673 err = got_path_mkdir(abspath);
674 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
677 if (lstat(abspath, &sb) == -1) {
678 err = got_error_from_errno2("lstat", abspath);
679 } else if (!S_ISDIR(sb.st_mode)) {
680 /* TODO directory is obstructed; do something */
681 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
688 static const struct got_error *
689 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
691 const struct got_error *err = NULL;
694 size_t flen1 = 0, flen2 = 0;
699 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
700 if (flen1 == 0 && ferror(f1)) {
701 err = got_error_from_errno("fread");
704 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
705 if (flen2 == 0 && ferror(f2)) {
706 err = got_error_from_errno("fread");
713 } else if (flen2 == 0) {
717 } else if (flen1 == flen2) {
718 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
731 static const struct got_error *
732 check_files_equal(int *same, FILE *f1, FILE *f2)
739 if (fstat(fileno(f1), &sb) != 0)
740 return got_error_from_errno("fstat");
743 if (fstat(fileno(f2), &sb) != 0)
744 return got_error_from_errno("fstat");
747 if (size1 != size2) {
752 if (fseek(f1, 0L, SEEK_SET) == -1)
753 return got_ferror(f1, GOT_ERR_IO);
754 if (fseek(f2, 0L, SEEK_SET) == -1)
755 return got_ferror(f2, GOT_ERR_IO);
757 return check_file_contents_equal(same, f1, f2);
761 * Perform a 3-way merge where the file f_orig acts as the common
762 * ancestor, the file f_deriv acts as the first derived version,
763 * and the file f_deriv2 acts as the second derived version.
764 * The merge result will be written to a new file at ondisk_path; any
765 * existing file at this path will be replaced.
767 static const struct got_error *
768 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
769 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
770 const char *path, uint16_t st_mode,
771 const char *label_orig, const char *label_deriv, const char *label_deriv2,
772 enum got_diff_algorithm diff_algo, struct got_repository *repo,
773 got_worktree_checkout_cb progress_cb, void *progress_arg)
775 const struct got_error *err = NULL;
777 FILE *f_merged = NULL;
778 char *merged_path = NULL, *base_path = NULL;
782 *local_changes_subsumed = 0;
784 err = got_path_dirname(&parent, ondisk_path);
788 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
789 err = got_error_from_errno("asprintf");
793 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
797 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
798 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
802 err = (*progress_cb)(progress_arg,
803 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
807 if (fsync(merged_fd) != 0) {
808 err = got_error_from_errno("fsync");
812 f_merged = fdopen(merged_fd, "r");
813 if (f_merged == NULL) {
814 err = got_error_from_errno("fdopen");
819 /* Check if a clean merge has subsumed all local changes. */
820 if (overlapcnt == 0) {
821 err = check_files_equal(local_changes_subsumed, f_deriv,
827 if (fchmod(fileno(f_merged), st_mode) != 0) {
828 err = got_error_from_errno2("fchmod", merged_path);
832 if (rename(merged_path, ondisk_path) != 0) {
833 err = got_error_from_errno3("rename", merged_path,
842 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
843 err = got_error_from_errno("close");
844 if (f_merged && fclose(f_merged) == EOF && err == NULL)
845 err = got_error_from_errno("fclose");
852 static const struct got_error *
853 update_symlink(const char *ondisk_path, const char *target_path,
856 /* This is not atomic but matches what 'ln -sf' does. */
857 if (unlink(ondisk_path) == -1)
858 return got_error_from_errno2("unlink", ondisk_path);
859 if (symlink(target_path, ondisk_path) == -1)
860 return got_error_from_errno3("symlink", target_path,
866 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
867 * in the work tree with a file that contains conflict markers and the
868 * conflicting target paths of the original version, a "derived version"
869 * of a symlink from an incoming change, and a local version of the symlink.
871 * The original versions's target path can be NULL if it is not available,
872 * such as if both derived versions added a new symlink at the same path.
874 * The incoming derived symlink target is NULL in case the incoming change
875 * has deleted this symlink.
877 static const struct got_error *
878 install_symlink_conflict(const char *deriv_target,
879 struct got_object_id *deriv_base_commit_id, const char *orig_target,
880 const char *label_orig, const char *local_target, const char *ondisk_path)
882 const struct got_error *err;
883 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
886 err = got_object_id_str(&id_str, deriv_base_commit_id);
888 return got_error_from_errno("asprintf");
890 if (asprintf(&label_deriv, "%s: commit %s",
891 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
892 err = got_error_from_errno("asprintf");
896 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
900 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
901 err = got_error_from_errno2("fchmod", path);
905 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
906 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
907 deriv_target ? deriv_target : "(symlink was deleted)",
908 orig_target ? label_orig : "",
909 orig_target ? "\n" : "",
910 orig_target ? orig_target : "",
911 orig_target ? "\n" : "",
912 GOT_DIFF_CONFLICT_MARKER_SEP,
913 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
914 err = got_error_from_errno2("fprintf", path);
918 if (unlink(ondisk_path) == -1) {
919 err = got_error_from_errno2("unlink", ondisk_path);
922 if (rename(path, ondisk_path) == -1) {
923 err = got_error_from_errno3("rename", path, ondisk_path);
927 if (f != NULL && fclose(f) == EOF && err == NULL)
928 err = got_error_from_errno2("fclose", path);
935 /* forward declaration */
936 static const struct got_error *
937 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
938 const char *, const char *, uint16_t, const char *,
939 struct got_blob_object *, struct got_object_id *,
940 struct got_repository *, got_worktree_checkout_cb, void *);
943 * Merge a symlink into the work tree, where blob_orig acts as the common
944 * ancestor, deriv_target is the link target of the first derived version,
945 * and the symlink on disk acts as the second derived version.
946 * Assume that contents of both blobs represent symlinks.
948 static const struct got_error *
949 merge_symlink(struct got_worktree *worktree,
950 struct got_blob_object *blob_orig, const char *ondisk_path,
951 const char *path, const char *label_orig, const char *deriv_target,
952 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
953 got_worktree_checkout_cb progress_cb, void *progress_arg)
955 const struct got_error *err = NULL;
956 char *ancestor_target = NULL;
958 ssize_t ondisk_len, deriv_len;
959 char ondisk_target[PATH_MAX];
960 int have_local_change = 0;
961 int have_incoming_change = 0;
963 if (lstat(ondisk_path, &sb) == -1)
964 return got_error_from_errno2("lstat", ondisk_path);
966 ondisk_len = readlink(ondisk_path, ondisk_target,
967 sizeof(ondisk_target));
968 if (ondisk_len == -1) {
969 err = got_error_from_errno2("readlink",
973 ondisk_target[ondisk_len] = '\0';
976 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
981 if (ancestor_target == NULL ||
982 (ondisk_len != strlen(ancestor_target) ||
983 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
984 have_local_change = 1;
986 deriv_len = strlen(deriv_target);
987 if (ancestor_target == NULL ||
988 (deriv_len != strlen(ancestor_target) ||
989 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
990 have_incoming_change = 1;
992 if (!have_local_change && !have_incoming_change) {
993 if (ancestor_target) {
994 /* Both sides made the same change. */
995 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
997 } else if (deriv_len == ondisk_len &&
998 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
999 /* Both sides added the same symlink. */
1000 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1003 /* Both sides added symlinks which don't match. */
1004 err = install_symlink_conflict(deriv_target,
1005 deriv_base_commit_id, ancestor_target,
1006 label_orig, ondisk_target, ondisk_path);
1009 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1012 } else if (!have_local_change && have_incoming_change) {
1013 /* Apply the incoming change. */
1014 err = update_symlink(ondisk_path, deriv_target,
1015 strlen(deriv_target));
1018 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1019 } else if (have_local_change && have_incoming_change) {
1020 if (deriv_len == ondisk_len &&
1021 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1022 /* Both sides made the same change. */
1023 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1026 err = install_symlink_conflict(deriv_target,
1027 deriv_base_commit_id, ancestor_target, label_orig,
1028 ondisk_target, ondisk_path);
1031 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1037 free(ancestor_target);
1041 static const struct got_error *
1042 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1044 const struct got_error *err = NULL;
1045 char target_path[PATH_MAX];
1054 return got_error_from_errno("got_opentemp");
1055 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1056 if (target_len == -1) {
1057 err = got_error_from_errno2("readlink", ondisk_path);
1060 n = fwrite(target_path, 1, target_len, f);
1061 if (n != target_len) {
1062 err = got_ferror(f, GOT_ERR_IO);
1065 if (fflush(f) == EOF) {
1066 err = got_error_from_errno("fflush");
1069 if (fseek(f, 0L, SEEK_SET) == -1) {
1070 err = got_ferror(f, GOT_ERR_IO);
1082 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1083 * blob_deriv acts as the first derived version, and the file on disk
1084 * acts as the second derived version.
1086 static const struct got_error *
1087 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1088 struct got_blob_object *blob_orig, const char *ondisk_path,
1089 const char *path, uint16_t st_mode, const char *label_orig,
1090 struct got_blob_object *blob_deriv,
1091 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1092 got_worktree_checkout_cb progress_cb, void *progress_arg)
1094 const struct got_error *err = NULL;
1095 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1096 char *blob_orig_path = NULL;
1097 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1098 char *label_deriv = NULL, *parent = NULL;
1100 *local_changes_subsumed = 0;
1102 err = got_path_dirname(&parent, ondisk_path);
1107 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1109 err = got_error_from_errno("asprintf");
1114 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1117 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1124 * No common ancestor exists. This is an "add vs add" conflict
1125 * and we simply use an empty ancestor file to make both files
1126 * appear in the merged result in their entirety.
1128 f_orig = got_opentemp();
1129 if (f_orig == NULL) {
1130 err = got_error_from_errno("got_opentemp");
1135 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1136 err = got_error_from_errno("asprintf");
1141 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1149 err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 if (asprintf(&label_deriv, "%s: commit %s",
1153 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1154 err = got_error_from_errno("asprintf");
1159 * In order the run a 3-way merge with a symlink we copy the symlink's
1160 * target path into a temporary file and use that file with diff3.
1162 if (S_ISLNK(st_mode)) {
1163 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1168 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1170 err = got_error_from_errno2("open", ondisk_path);
1173 f_deriv2 = fdopen(fd, "r");
1174 if (f_deriv2 == NULL) {
1175 err = got_error_from_errno2("fdopen", ondisk_path);
1181 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1182 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1183 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1185 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1186 err = got_error_from_errno("fclose");
1187 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1188 err = got_error_from_errno("fclose");
1189 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1190 err = got_error_from_errno("fclose");
1192 if (blob_orig_path) {
1193 unlink(blob_orig_path);
1194 free(blob_orig_path);
1196 if (blob_deriv_path) {
1197 unlink(blob_deriv_path);
1198 free(blob_deriv_path);
1206 static const struct got_error *
1207 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1208 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1209 int wt_fd, const char *path, struct got_object_id *blob_id)
1211 const struct got_error *err = NULL;
1212 struct got_fileindex_entry *new_ie;
1216 err = got_fileindex_entry_alloc(&new_ie, path);
1220 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1221 blob_id->sha1, base_commit_id->sha1, 1);
1225 err = got_fileindex_entry_add(fileindex, new_ie);
1228 got_fileindex_entry_free(new_ie);
1235 get_ondisk_perms(int executable, mode_t st_mode)
1237 mode_t xbits = S_IXUSR;
1240 /* Map read bits to execute bits. */
1241 if (st_mode & S_IRGRP)
1243 if (st_mode & S_IROTH)
1245 return st_mode | xbits;
1248 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1251 /* forward declaration */
1252 static const struct got_error *
1253 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1254 const char *path, mode_t te_mode, mode_t st_mode,
1255 struct got_blob_object *blob, int restoring_missing_file,
1256 int reverting_versioned_file, int installing_bad_symlink,
1257 int path_is_unversioned, struct got_repository *repo,
1258 got_worktree_checkout_cb progress_cb, void *progress_arg);
1261 * This function assumes that the provided symlink target points at a
1262 * safe location in the work tree!
1264 static const struct got_error *
1265 replace_existing_symlink(int *did_something, const char *ondisk_path,
1266 const char *target_path, size_t target_len)
1268 const struct got_error *err = NULL;
1270 char etarget[PATH_MAX];
1276 * "Bad" symlinks (those pointing outside the work tree or into the
1277 * .got directory) are installed in the work tree as a regular file
1278 * which contains the bad symlink target path.
1279 * The new symlink target has already been checked for safety by our
1280 * caller. If we can successfully open a regular file then we simply
1281 * replace this file with a symlink below.
1283 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1286 return got_error_from_errno2("open", ondisk_path);
1288 /* We are updating an existing on-disk symlink. */
1289 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1291 return got_error_from_errno2("readlink", ondisk_path);
1293 if (elen == target_len &&
1294 memcmp(etarget, target_path, target_len) == 0)
1295 return NULL; /* nothing to do */
1299 err = update_symlink(ondisk_path, target_path, target_len);
1300 if (fd != -1 && close(fd) == -1 && err == NULL)
1301 err = got_error_from_errno2("close", ondisk_path);
1305 static const struct got_error *
1306 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1307 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1309 const struct got_error *err = NULL;
1310 char canonpath[PATH_MAX];
1311 char *path_got = NULL;
1313 *is_bad_symlink = 0;
1315 if (target_len >= sizeof(canonpath)) {
1316 *is_bad_symlink = 1;
1321 * We do not use realpath(3) to resolve the symlink's target
1322 * path because we don't want to resolve symlinks recursively.
1323 * Instead we make the path absolute and then canonicalize it.
1324 * Relative symlink target lookup should begin at the directory
1325 * in which the blob object is being installed.
1327 if (!got_path_is_absolute(target_path)) {
1328 char *abspath, *parent;
1329 err = got_path_dirname(&parent, ondisk_path);
1332 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1334 return got_error_from_errno("asprintf");
1337 if (strlen(abspath) >= sizeof(canonpath)) {
1338 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1342 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1347 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1352 /* Only allow symlinks pointing at paths within the work tree. */
1353 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1354 *is_bad_symlink = 1;
1358 /* Do not allow symlinks pointing into the .got directory. */
1359 if (asprintf(&path_got, "%s/%s", wtroot_path,
1360 GOT_WORKTREE_GOT_DIR) == -1)
1361 return got_error_from_errno("asprintf");
1362 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1363 *is_bad_symlink = 1;
1369 static const struct got_error *
1370 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1371 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1372 int restoring_missing_file, int reverting_versioned_file,
1373 int path_is_unversioned, int allow_bad_symlinks,
1374 struct got_repository *repo,
1375 got_worktree_checkout_cb progress_cb, void *progress_arg)
1377 const struct got_error *err = NULL;
1378 char target_path[PATH_MAX];
1379 size_t len, target_len = 0;
1380 char *path_got = NULL;
1381 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1382 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1384 *is_bad_symlink = 0;
1387 * Blob object content specifies the target path of the link.
1388 * If a symbolic link cannot be installed we instead create
1389 * a regular file which contains the link target path stored
1390 * in the blob object.
1393 err = got_object_blob_read_block(&len, blob);
1394 if (len + target_len >= sizeof(target_path)) {
1395 /* Path too long; install as a regular file. */
1396 *is_bad_symlink = 1;
1397 got_object_blob_rewind(blob);
1398 return install_blob(worktree, ondisk_path, path,
1399 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1400 restoring_missing_file, reverting_versioned_file,
1401 1, path_is_unversioned, repo, progress_cb,
1405 /* Skip blob object header first time around. */
1406 memcpy(target_path + target_len, buf + hdrlen,
1408 target_len += len - hdrlen;
1412 target_path[target_len] = '\0';
1414 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1415 ondisk_path, worktree->root_path);
1419 if (*is_bad_symlink && !allow_bad_symlinks) {
1420 /* install as a regular file */
1421 got_object_blob_rewind(blob);
1422 err = install_blob(worktree, ondisk_path, path,
1423 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1424 restoring_missing_file, reverting_versioned_file, 1,
1425 path_is_unversioned, repo, progress_cb, progress_arg);
1429 if (symlink(target_path, ondisk_path) == -1) {
1430 if (errno == EEXIST) {
1431 int symlink_replaced;
1432 if (path_is_unversioned) {
1433 err = (*progress_cb)(progress_arg,
1434 GOT_STATUS_UNVERSIONED, path);
1437 err = replace_existing_symlink(&symlink_replaced,
1438 ondisk_path, target_path, target_len);
1442 if (symlink_replaced) {
1443 err = (*progress_cb)(progress_arg,
1444 reverting_versioned_file ?
1446 GOT_STATUS_UPDATE, path);
1448 err = (*progress_cb)(progress_arg,
1449 GOT_STATUS_EXISTS, path);
1452 goto done; /* Nothing else to do. */
1455 if (errno == ENOENT) {
1457 err = got_path_dirname(&parent, ondisk_path);
1460 err = add_dir_on_disk(worktree, parent);
1465 * Retry, and fall through to error handling
1466 * below if this second attempt fails.
1468 if (symlink(target_path, ondisk_path) != -1) {
1469 err = NULL; /* success */
1474 /* Handle errors from first or second creation attempt. */
1475 if (errno == ENAMETOOLONG) {
1476 /* bad target path; install as a regular file */
1477 *is_bad_symlink = 1;
1478 got_object_blob_rewind(blob);
1479 err = install_blob(worktree, ondisk_path, path,
1480 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1481 restoring_missing_file, reverting_versioned_file, 1,
1482 path_is_unversioned, repo,
1483 progress_cb, progress_arg);
1484 } else if (errno == ENOTDIR) {
1485 err = got_error_path(ondisk_path,
1486 GOT_ERR_FILE_OBSTRUCTED);
1488 err = got_error_from_errno3("symlink",
1489 target_path, ondisk_path);
1491 } else if (progress_cb)
1492 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1493 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1499 static const struct got_error *
1500 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1501 const char *path, mode_t te_mode, mode_t st_mode,
1502 struct got_blob_object *blob, int restoring_missing_file,
1503 int reverting_versioned_file, int installing_bad_symlink,
1504 int path_is_unversioned, struct got_repository *repo,
1505 got_worktree_checkout_cb progress_cb, void *progress_arg)
1507 const struct got_error *err = NULL;
1511 char *tmppath = NULL;
1513 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1514 GOT_DEFAULT_FILE_MODE);
1516 if (errno == ENOENT) {
1518 err = got_path_dirname(&parent, path);
1521 err = add_dir_on_disk(worktree, parent);
1525 fd = open(ondisk_path,
1526 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1527 GOT_DEFAULT_FILE_MODE);
1529 return got_error_from_errno2("open",
1531 } else if (errno == EEXIST) {
1532 if (path_is_unversioned) {
1533 err = (*progress_cb)(progress_arg,
1534 GOT_STATUS_UNVERSIONED, path);
1537 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1538 !S_ISREG(st_mode) && !installing_bad_symlink) {
1539 /* TODO file is obstructed; do something */
1540 err = got_error_path(ondisk_path,
1541 GOT_ERR_FILE_OBSTRUCTED);
1544 err = got_opentemp_named_fd(&tmppath, &fd,
1551 return got_error_from_errno2("open", ondisk_path);
1554 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1555 err = got_error_from_errno2("fchmod",
1556 update ? tmppath : ondisk_path);
1561 if (restoring_missing_file)
1562 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1564 else if (reverting_versioned_file)
1565 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1568 err = (*progress_cb)(progress_arg,
1569 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1574 hdrlen = got_object_blob_get_hdrlen(blob);
1576 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1577 err = got_object_blob_read_block(&len, blob);
1581 /* Skip blob object header first time around. */
1582 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1584 err = got_error_from_errno("write");
1586 } else if (outlen != len - hdrlen) {
1587 err = got_error(GOT_ERR_IO);
1594 if (fsync(fd) != 0) {
1595 err = got_error_from_errno("fsync");
1600 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1601 err = got_error_from_errno2("unlink", ondisk_path);
1604 if (rename(tmppath, ondisk_path) != 0) {
1605 err = got_error_from_errno3("rename", tmppath,
1614 if (fd != -1 && close(fd) == -1 && err == NULL)
1615 err = got_error_from_errno("close");
1616 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1617 err = got_error_from_errno2("unlink", tmppath);
1622 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1623 static const struct got_error *
1624 get_modified_file_content_status(unsigned char *status, FILE *f)
1626 const struct got_error *err = NULL;
1627 const char *markers[3] = {
1628 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1629 GOT_DIFF_CONFLICT_MARKER_SEP,
1630 GOT_DIFF_CONFLICT_MARKER_END
1634 size_t linesize = 0;
1637 while (*status == GOT_STATUS_MODIFY) {
1638 linelen = getline(&line, &linesize, f);
1639 if (linelen == -1) {
1642 err = got_ferror(f, GOT_ERR_IO);
1646 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1647 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1649 *status = GOT_STATUS_CONFLICT;
1660 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1662 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1663 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1667 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1669 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1670 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1671 ie->mtime_sec == sb->st_mtim.tv_sec &&
1672 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1673 ie->size == (sb->st_size & 0xffffffff) &&
1674 !xbit_differs(ie, sb->st_mode));
1677 static unsigned char
1678 get_staged_status(struct got_fileindex_entry *ie)
1680 switch (got_fileindex_entry_stage_get(ie)) {
1681 case GOT_FILEIDX_STAGE_ADD:
1682 return GOT_STATUS_ADD;
1683 case GOT_FILEIDX_STAGE_DELETE:
1684 return GOT_STATUS_DELETE;
1685 case GOT_FILEIDX_STAGE_MODIFY:
1686 return GOT_STATUS_MODIFY;
1688 return GOT_STATUS_NO_CHANGE;
1692 static const struct got_error *
1693 get_symlink_modification_status(unsigned char *status,
1694 struct got_fileindex_entry *ie, const char *abspath,
1695 int dirfd, const char *de_name, struct got_blob_object *blob)
1697 const struct got_error *err = NULL;
1698 char target_path[PATH_MAX];
1699 char etarget[PATH_MAX];
1701 size_t len, target_len = 0;
1702 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1703 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1705 *status = GOT_STATUS_NO_CHANGE;
1707 /* Blob object content specifies the target path of the link. */
1709 err = got_object_blob_read_block(&len, blob);
1712 if (len + target_len >= sizeof(target_path)) {
1714 * Should not happen. The blob contents were OK
1715 * when this symlink was installed.
1717 return got_error(GOT_ERR_NO_SPACE);
1720 /* Skip blob object header first time around. */
1721 memcpy(target_path + target_len, buf + hdrlen,
1723 target_len += len - hdrlen;
1727 target_path[target_len] = '\0';
1730 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1732 return got_error_from_errno2("readlinkat", abspath);
1734 elen = readlink(abspath, etarget, sizeof(etarget));
1736 return got_error_from_errno2("readlink", abspath);
1739 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1740 *status = GOT_STATUS_MODIFY;
1745 static const struct got_error *
1746 get_file_status(unsigned char *status, struct stat *sb,
1747 struct got_fileindex_entry *ie, const char *abspath,
1748 int dirfd, const char *de_name, struct got_repository *repo)
1750 const struct got_error *err = NULL;
1751 struct got_object_id id;
1756 struct got_blob_object *blob = NULL;
1758 unsigned char staged_status = get_staged_status(ie);
1760 *status = GOT_STATUS_NO_CHANGE;
1761 memset(sb, 0, sizeof(*sb));
1764 * Whenever the caller provides a directory descriptor and a
1765 * directory entry name for the file, use them! This prevents
1766 * race conditions if filesystem paths change beneath our feet.
1769 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1770 if (errno == ENOENT) {
1771 if (got_fileindex_entry_has_file_on_disk(ie))
1772 *status = GOT_STATUS_MISSING;
1774 *status = GOT_STATUS_DELETE;
1777 err = got_error_from_errno2("fstatat", abspath);
1781 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1782 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1783 return got_error_from_errno2("open", abspath);
1784 else if (fd == -1 && errno == ELOOP) {
1785 if (lstat(abspath, sb) == -1)
1786 return got_error_from_errno2("lstat", abspath);
1787 } else if (fd == -1 || fstat(fd, sb) == -1) {
1788 if (errno == ENOENT) {
1789 if (got_fileindex_entry_has_file_on_disk(ie))
1790 *status = GOT_STATUS_MISSING;
1792 *status = GOT_STATUS_DELETE;
1795 err = got_error_from_errno2("fstat", abspath);
1800 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1801 *status = GOT_STATUS_OBSTRUCTED;
1805 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1806 *status = GOT_STATUS_DELETE;
1808 } else if (!got_fileindex_entry_has_blob(ie) &&
1809 staged_status != GOT_STATUS_ADD) {
1810 *status = GOT_STATUS_ADD;
1814 if (!stat_info_differs(ie, sb))
1817 if (S_ISLNK(sb->st_mode) &&
1818 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1819 *status = GOT_STATUS_MODIFY;
1823 if (staged_status == GOT_STATUS_MODIFY ||
1824 staged_status == GOT_STATUS_ADD)
1825 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1827 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1829 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1833 if (S_ISLNK(sb->st_mode)) {
1834 err = get_symlink_modification_status(status, ie,
1835 abspath, dirfd, de_name, blob);
1840 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1842 err = got_error_from_errno2("openat", abspath);
1847 f = fdopen(fd, "r");
1849 err = got_error_from_errno2("fdopen", abspath);
1853 hdrlen = got_object_blob_get_hdrlen(blob);
1855 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1856 err = got_object_blob_read_block(&blen, blob);
1859 /* Skip length of blob object header first time around. */
1860 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1861 if (flen == 0 && ferror(f)) {
1862 err = got_error_from_errno("fread");
1865 if (blen - hdrlen == 0) {
1867 *status = GOT_STATUS_MODIFY;
1869 } else if (flen == 0) {
1870 if (blen - hdrlen != 0)
1871 *status = GOT_STATUS_MODIFY;
1873 } else if (blen - hdrlen == flen) {
1874 /* Skip blob object header first time around. */
1875 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1876 *status = GOT_STATUS_MODIFY;
1880 *status = GOT_STATUS_MODIFY;
1886 if (*status == GOT_STATUS_MODIFY) {
1888 err = get_modified_file_content_status(status, f);
1889 } else if (xbit_differs(ie, sb->st_mode))
1890 *status = GOT_STATUS_MODE_CHANGE;
1893 got_object_blob_close(blob);
1894 if (f != NULL && fclose(f) == EOF && err == NULL)
1895 err = got_error_from_errno2("fclose", abspath);
1896 if (fd != -1 && close(fd) == -1 && err == NULL)
1897 err = got_error_from_errno2("close", abspath);
1902 * Update timestamps in the file index if a file is unmodified and
1903 * we had to run a full content comparison to find out.
1905 static const struct got_error *
1906 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1907 struct got_fileindex_entry *ie, struct stat *sb)
1909 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1910 return got_fileindex_entry_update(ie, wt_fd, path,
1911 ie->blob_sha1, ie->commit_sha1, 1);
1916 static const struct got_error *
1917 update_blob(struct got_worktree *worktree,
1918 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1919 struct got_tree_entry *te, const char *path,
1920 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1923 const struct got_error *err = NULL;
1924 struct got_blob_object *blob = NULL;
1926 unsigned char status = GOT_STATUS_NO_CHANGE;
1929 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1930 return got_error_from_errno("asprintf");
1933 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1934 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1937 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1941 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1942 sb.st_mode = got_fileindex_perms_to_st(ie);
1944 if (stat(ondisk_path, &sb) == -1) {
1945 if (errno != ENOENT) {
1946 err = got_error_from_errno2("stat",
1950 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1951 status = GOT_STATUS_UNVERSIONED;
1953 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1954 status = GOT_STATUS_UNVERSIONED;
1956 status = GOT_STATUS_OBSTRUCTED;
1960 if (status == GOT_STATUS_OBSTRUCTED) {
1962 got_fileindex_entry_mark_skipped(ie);
1963 err = (*progress_cb)(progress_arg, status, path);
1966 if (status == GOT_STATUS_CONFLICT) {
1968 got_fileindex_entry_mark_skipped(ie);
1969 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1974 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1975 (S_ISLNK(te->mode) ||
1976 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1978 * This is a regular file or an installed bad symlink.
1979 * If the file index indicates that this file is already
1980 * up-to-date with respect to the repository we can skip
1981 * updating contents of this file.
1983 if (got_fileindex_entry_has_commit(ie) &&
1984 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1985 SHA1_DIGEST_LENGTH) == 0) {
1987 err = sync_timestamps(worktree->root_fd,
1988 path, status, ie, &sb);
1991 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1995 if (got_fileindex_entry_has_blob(ie) &&
1996 memcmp(ie->blob_sha1, te->id.sha1,
1997 SHA1_DIGEST_LENGTH) == 0) {
1998 /* Different commit but the same blob. */
1999 err = sync_timestamps(worktree->root_fd,
2000 path, status, ie, &sb);
2003 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2009 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2013 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2014 int update_timestamps;
2015 struct got_blob_object *blob2 = NULL;
2016 char *label_orig = NULL;
2017 if (got_fileindex_entry_has_blob(ie)) {
2018 struct got_object_id id2;
2019 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2020 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2024 if (got_fileindex_entry_has_commit(ie)) {
2025 char id_str[SHA1_DIGEST_STRING_LENGTH];
2026 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2027 sizeof(id_str)) == NULL) {
2028 err = got_error_path(id_str,
2029 GOT_ERR_BAD_OBJ_ID_STR);
2032 if (asprintf(&label_orig, "%s: commit %s",
2033 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2034 err = got_error_from_errno("asprintf");
2038 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2040 err = got_object_blob_read_to_str(&link_target, blob);
2043 err = merge_symlink(worktree, blob2, ondisk_path, path,
2044 label_orig, link_target, worktree->base_commit_id,
2045 repo, progress_cb, progress_arg);
2048 err = merge_blob(&update_timestamps, worktree, blob2,
2049 ondisk_path, path, sb.st_mode, label_orig, blob,
2050 worktree->base_commit_id, repo,
2051 progress_cb, progress_arg);
2055 got_object_blob_close(blob2);
2059 * Do not update timestamps of files with local changes.
2060 * Otherwise, a future status walk would treat them as
2061 * unmodified files again.
2063 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2064 blob->id.sha1, worktree->base_commit_id->sha1,
2066 } else if (status == GOT_STATUS_MODE_CHANGE) {
2067 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2068 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2069 } else if (status == GOT_STATUS_DELETE) {
2070 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2073 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2074 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2078 int is_bad_symlink = 0;
2079 if (S_ISLNK(te->mode)) {
2080 err = install_symlink(&is_bad_symlink, worktree,
2081 ondisk_path, path, blob,
2082 status == GOT_STATUS_MISSING, 0,
2083 status == GOT_STATUS_UNVERSIONED, 0,
2084 repo, progress_cb, progress_arg);
2086 err = install_blob(worktree, ondisk_path, path,
2087 te->mode, sb.st_mode, blob,
2088 status == GOT_STATUS_MISSING, 0, 0,
2089 status == GOT_STATUS_UNVERSIONED, repo,
2090 progress_cb, progress_arg);
2096 err = got_fileindex_entry_update(ie,
2097 worktree->root_fd, path, blob->id.sha1,
2098 worktree->base_commit_id->sha1, 1);
2100 err = create_fileindex_entry(&ie, fileindex,
2101 worktree->base_commit_id, worktree->root_fd, path,
2107 if (is_bad_symlink) {
2108 got_fileindex_entry_filetype_set(ie,
2109 GOT_FILEIDX_MODE_BAD_SYMLINK);
2112 got_object_blob_close(blob);
2118 static const struct got_error *
2119 remove_ondisk_file(const char *root_path, const char *path)
2121 const struct got_error *err = NULL;
2122 char *ondisk_path = NULL, *parent = NULL;
2124 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2125 return got_error_from_errno("asprintf");
2127 if (unlink(ondisk_path) == -1) {
2128 if (errno != ENOENT)
2129 err = got_error_from_errno2("unlink", ondisk_path);
2131 size_t root_len = strlen(root_path);
2132 err = got_path_dirname(&parent, ondisk_path);
2135 while (got_path_cmp(parent, root_path,
2136 strlen(parent), root_len) != 0) {
2138 ondisk_path = parent;
2140 if (rmdir(ondisk_path) == -1) {
2141 if (errno != ENOTEMPTY)
2142 err = got_error_from_errno2("rmdir",
2146 err = got_path_dirname(&parent, ondisk_path);
2157 static const struct got_error *
2158 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2159 struct got_fileindex_entry *ie, struct got_repository *repo,
2160 got_worktree_checkout_cb progress_cb, void *progress_arg)
2162 const struct got_error *err = NULL;
2163 unsigned char status;
2167 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2168 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2170 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2172 return got_error_from_errno("asprintf");
2174 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2178 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2179 char ondisk_target[PATH_MAX];
2180 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2181 sizeof(ondisk_target));
2182 if (ondisk_len == -1) {
2183 err = got_error_from_errno2("readlink", ondisk_path);
2186 ondisk_target[ondisk_len] = '\0';
2187 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2188 NULL, NULL, /* XXX pass common ancestor info? */
2189 ondisk_target, ondisk_path);
2192 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2197 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2198 status == GOT_STATUS_ADD) {
2199 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2203 * Preserve the working file and change the deleted blob's
2204 * entry into a schedule-add entry.
2206 err = got_fileindex_entry_update(ie, worktree->root_fd,
2207 ie->path, NULL, NULL, 0);
2209 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2212 if (status == GOT_STATUS_NO_CHANGE) {
2213 err = remove_ondisk_file(worktree->root_path, ie->path);
2217 got_fileindex_entry_remove(fileindex, ie);
2224 struct diff_cb_arg {
2225 struct got_fileindex *fileindex;
2226 struct got_worktree *worktree;
2227 struct got_repository *repo;
2228 got_worktree_checkout_cb progress_cb;
2230 got_cancel_cb cancel_cb;
2234 static const struct got_error *
2235 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2236 struct got_tree_entry *te, const char *parent_path)
2238 struct diff_cb_arg *a = arg;
2240 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2241 return got_error(GOT_ERR_CANCELLED);
2243 return update_blob(a->worktree, a->fileindex, ie, te,
2244 ie->path, a->repo, a->progress_cb, a->progress_arg);
2247 static const struct got_error *
2248 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2250 struct diff_cb_arg *a = arg;
2252 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2253 return got_error(GOT_ERR_CANCELLED);
2255 return delete_blob(a->worktree, a->fileindex, ie,
2256 a->repo, a->progress_cb, a->progress_arg);
2259 static const struct got_error *
2260 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2262 struct diff_cb_arg *a = arg;
2263 const struct got_error *err;
2266 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2267 return got_error(GOT_ERR_CANCELLED);
2269 if (got_object_tree_entry_is_submodule(te))
2272 if (asprintf(&path, "%s%s%s", parent_path,
2273 parent_path[0] ? "/" : "", te->name)
2275 return got_error_from_errno("asprintf");
2277 if (S_ISDIR(te->mode))
2278 err = add_dir_on_disk(a->worktree, path);
2280 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2281 a->repo, a->progress_cb, a->progress_arg);
2287 const struct got_error *
2288 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2290 uint32_t uuid_status;
2292 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2293 if (uuid_status != uuid_s_ok) {
2295 return got_error_uuid(uuid_status, "uuid_to_string");
2301 static const struct got_error *
2302 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2304 const struct got_error *err = NULL;
2305 char *uuidstr = NULL;
2309 err = got_worktree_get_uuid(&uuidstr, worktree);
2313 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2314 err = got_error_from_errno("asprintf");
2321 const struct got_error *
2322 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2327 static const struct got_error *
2328 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2330 return get_ref_name(refname, worktree,
2331 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2334 static const struct got_error *
2335 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2337 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2340 static const struct got_error *
2341 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2343 return get_ref_name(refname, worktree,
2344 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2347 static const struct got_error *
2348 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2350 return get_ref_name(refname, worktree,
2351 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2354 static const struct got_error *
2355 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2357 return get_ref_name(refname, worktree,
2358 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2361 static const struct got_error *
2362 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2364 return get_ref_name(refname, worktree,
2365 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2368 static const struct got_error *
2369 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2371 return get_ref_name(refname, worktree,
2372 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2375 static const struct got_error *
2376 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 return get_ref_name(refname, worktree,
2379 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2382 const struct got_error *
2383 got_worktree_get_histedit_script_path(char **path,
2384 struct got_worktree *worktree)
2386 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2387 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2389 return got_error_from_errno("asprintf");
2394 static const struct got_error *
2395 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2397 return get_ref_name(refname, worktree,
2398 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2401 static const struct got_error *
2402 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2404 return get_ref_name(refname, worktree,
2405 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2409 * Prevent Git's garbage collector from deleting our base commit by
2410 * setting a reference to our base commit's ID.
2412 static const struct got_error *
2413 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2415 const struct got_error *err = NULL;
2416 struct got_reference *ref = NULL;
2419 err = got_worktree_get_base_ref_name(&refname, worktree);
2423 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2427 err = got_ref_write(ref, repo);
2435 static const struct got_error *
2436 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2438 const struct got_error *err = NULL;
2440 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2441 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2442 err = got_error_from_errno("asprintf");
2443 *fileindex_path = NULL;
2449 static const struct got_error *
2450 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2451 struct got_worktree *worktree)
2453 const struct got_error *err = NULL;
2456 *fileindex_path = NULL;
2457 *fileindex = got_fileindex_alloc();
2458 if (*fileindex == NULL)
2459 return got_error_from_errno("got_fileindex_alloc");
2461 err = get_fileindex_path(fileindex_path, worktree);
2465 index = fopen(*fileindex_path, "rb");
2466 if (index == NULL) {
2467 if (errno != ENOENT)
2468 err = got_error_from_errno2("fopen", *fileindex_path);
2470 err = got_fileindex_read(*fileindex, index);
2471 if (fclose(index) == EOF && err == NULL)
2472 err = got_error_from_errno("fclose");
2476 free(*fileindex_path);
2477 *fileindex_path = NULL;
2478 got_fileindex_free(*fileindex);
2484 struct bump_base_commit_id_arg {
2485 struct got_object_id *base_commit_id;
2488 const char *entry_name;
2489 got_worktree_checkout_cb progress_cb;
2493 /* Bump base commit ID of all files within an updated part of the work tree. */
2494 static const struct got_error *
2495 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2497 const struct got_error *err;
2498 struct bump_base_commit_id_arg *a = arg;
2500 if (a->entry_name) {
2501 if (strcmp(ie->path, a->path) != 0)
2503 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2506 if (got_fileindex_entry_was_skipped(ie))
2509 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2510 SHA1_DIGEST_LENGTH) == 0)
2513 if (a->progress_cb) {
2514 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2519 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2523 static const struct got_error *
2524 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2525 struct got_fileindex *fileindex,
2526 got_worktree_checkout_cb progress_cb, void *progress_arg)
2528 struct bump_base_commit_id_arg bbc_arg;
2530 bbc_arg.base_commit_id = worktree->base_commit_id;
2531 bbc_arg.entry_name = NULL;
2533 bbc_arg.path_len = 0;
2534 bbc_arg.progress_cb = progress_cb;
2535 bbc_arg.progress_arg = progress_arg;
2537 return got_fileindex_for_each_entry_safe(fileindex,
2538 bump_base_commit_id, &bbc_arg);
2541 static const struct got_error *
2542 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2544 const struct got_error *err = NULL;
2545 char *new_fileindex_path = NULL;
2546 FILE *new_index = NULL;
2547 struct timespec timeout;
2549 err = got_opentemp_named(&new_fileindex_path, &new_index,
2554 err = got_fileindex_write(fileindex, new_index);
2558 if (rename(new_fileindex_path, fileindex_path) != 0) {
2559 err = got_error_from_errno3("rename", new_fileindex_path,
2561 unlink(new_fileindex_path);
2565 * Sleep for a short amount of time to ensure that files modified after
2566 * this program exits have a different time stamp from the one which
2567 * was recorded in the file index.
2570 timeout.tv_nsec = 1;
2571 nanosleep(&timeout, NULL);
2575 free(new_fileindex_path);
2579 static const struct got_error *
2580 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2581 struct got_object_id **tree_id, const char *wt_relpath,
2582 struct got_worktree *worktree, struct got_repository *repo)
2584 const struct got_error *err = NULL;
2585 struct got_object_id *id = NULL;
2586 char *in_repo_path = NULL;
2587 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2589 *entry_type = GOT_OBJ_TYPE_ANY;
2590 *tree_relpath = NULL;
2593 if (wt_relpath[0] == '\0') {
2594 /* Check out all files within the work tree. */
2595 *entry_type = GOT_OBJ_TYPE_TREE;
2596 *tree_relpath = strdup("");
2597 if (*tree_relpath == NULL) {
2598 err = got_error_from_errno("strdup");
2601 err = got_object_id_by_path(tree_id, repo,
2602 worktree->base_commit_id, worktree->path_prefix);
2608 /* Check out a subset of files in the work tree. */
2610 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2611 is_root_wt ? "" : "/", wt_relpath) == -1) {
2612 err = got_error_from_errno("asprintf");
2616 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2622 in_repo_path = NULL;
2624 err = got_object_get_type(entry_type, repo, id);
2628 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2629 /* Check out a single file. */
2630 if (strchr(wt_relpath, '/') == NULL) {
2631 /* Check out a single file in work tree's root dir. */
2632 in_repo_path = strdup(worktree->path_prefix);
2633 if (in_repo_path == NULL) {
2634 err = got_error_from_errno("strdup");
2637 *tree_relpath = strdup("");
2638 if (*tree_relpath == NULL) {
2639 err = got_error_from_errno("strdup");
2643 /* Check out a single file in a subdirectory. */
2644 err = got_path_dirname(tree_relpath, wt_relpath);
2647 if (asprintf(&in_repo_path, "%s%s%s",
2648 worktree->path_prefix, is_root_wt ? "" : "/",
2649 *tree_relpath) == -1) {
2650 err = got_error_from_errno("asprintf");
2654 err = got_object_id_by_path(tree_id, repo,
2655 worktree->base_commit_id, in_repo_path);
2657 /* Check out all files within a subdirectory. */
2658 *tree_id = got_object_id_dup(id);
2659 if (*tree_id == NULL) {
2660 err = got_error_from_errno("got_object_id_dup");
2663 *tree_relpath = strdup(wt_relpath);
2664 if (*tree_relpath == NULL) {
2665 err = got_error_from_errno("strdup");
2673 *entry_type = GOT_OBJ_TYPE_ANY;
2674 free(*tree_relpath);
2675 *tree_relpath = NULL;
2682 static const struct got_error *
2683 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2684 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2685 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2686 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2688 const struct got_error *err = NULL;
2689 struct got_commit_object *commit = NULL;
2690 struct got_tree_object *tree = NULL;
2691 struct got_fileindex_diff_tree_cb diff_cb;
2692 struct diff_cb_arg arg;
2694 err = ref_base_commit(worktree, repo);
2696 if (!(err->code == GOT_ERR_ERRNO &&
2697 (errno == EACCES || errno == EROFS)))
2699 err = (*progress_cb)(progress_arg,
2700 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2705 err = got_object_open_as_commit(&commit, repo,
2706 worktree->base_commit_id);
2710 err = got_object_open_as_tree(&tree, repo, tree_id);
2715 got_object_tree_find_entry(tree, entry_name) == NULL) {
2716 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2720 diff_cb.diff_old_new = diff_old_new;
2721 diff_cb.diff_old = diff_old;
2722 diff_cb.diff_new = diff_new;
2723 arg.fileindex = fileindex;
2724 arg.worktree = worktree;
2726 arg.progress_cb = progress_cb;
2727 arg.progress_arg = progress_arg;
2728 arg.cancel_cb = cancel_cb;
2729 arg.cancel_arg = cancel_arg;
2730 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2731 entry_name, repo, &diff_cb, &arg);
2734 got_object_tree_close(tree);
2736 got_object_commit_close(commit);
2740 const struct got_error *
2741 got_worktree_checkout_files(struct got_worktree *worktree,
2742 struct got_pathlist_head *paths, struct got_repository *repo,
2743 got_worktree_checkout_cb progress_cb, void *progress_arg,
2744 got_cancel_cb cancel_cb, void *cancel_arg)
2746 const struct got_error *err = NULL, *sync_err, *unlockerr;
2747 struct got_commit_object *commit = NULL;
2748 struct got_tree_object *tree = NULL;
2749 struct got_fileindex *fileindex = NULL;
2750 char *fileindex_path = NULL;
2751 struct got_pathlist_entry *pe;
2752 struct tree_path_data {
2753 STAILQ_ENTRY(tree_path_data) entry;
2754 struct got_object_id *tree_id;
2759 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2761 STAILQ_INIT(&tree_paths);
2763 err = lock_worktree(worktree, LOCK_EX);
2767 /* Map all specified paths to in-repository trees. */
2768 TAILQ_FOREACH(pe, paths, entry) {
2769 tpd = malloc(sizeof(*tpd));
2771 err = got_error_from_errno("malloc");
2775 err = find_tree_entry_for_checkout(&tpd->entry_type,
2776 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2782 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2783 err = got_path_basename(&tpd->entry_name, pe->path);
2791 tpd->entry_name = NULL;
2793 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2797 * Read the file index.
2798 * Checking out files is supposed to be an idempotent operation.
2799 * If the on-disk file index is incomplete we will try to complete it.
2801 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2805 tpd = STAILQ_FIRST(&tree_paths);
2806 TAILQ_FOREACH(pe, paths, entry) {
2807 struct bump_base_commit_id_arg bbc_arg;
2809 err = checkout_files(worktree, fileindex, tpd->relpath,
2810 tpd->tree_id, tpd->entry_name, repo,
2811 progress_cb, progress_arg, cancel_cb, cancel_arg);
2815 bbc_arg.base_commit_id = worktree->base_commit_id;
2816 bbc_arg.entry_name = tpd->entry_name;
2817 bbc_arg.path = pe->path;
2818 bbc_arg.path_len = pe->path_len;
2819 bbc_arg.progress_cb = progress_cb;
2820 bbc_arg.progress_arg = progress_arg;
2821 err = got_fileindex_for_each_entry_safe(fileindex,
2822 bump_base_commit_id, &bbc_arg);
2826 tpd = STAILQ_NEXT(tpd, entry);
2828 sync_err = sync_fileindex(fileindex, fileindex_path);
2829 if (sync_err && err == NULL)
2832 free(fileindex_path);
2834 got_object_tree_close(tree);
2836 got_object_commit_close(commit);
2838 got_fileindex_free(fileindex);
2839 while (!STAILQ_EMPTY(&tree_paths)) {
2840 tpd = STAILQ_FIRST(&tree_paths);
2841 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2846 unlockerr = lock_worktree(worktree, LOCK_SH);
2847 if (unlockerr && err == NULL)
2852 struct merge_file_cb_arg {
2853 struct got_worktree *worktree;
2854 struct got_fileindex *fileindex;
2855 got_worktree_checkout_cb progress_cb;
2857 got_cancel_cb cancel_cb;
2859 const char *label_orig;
2860 struct got_object_id *commit_id2;
2861 int allow_bad_symlinks;
2864 static const struct got_error *
2865 merge_file_cb(void *arg, struct got_blob_object *blob1,
2866 struct got_blob_object *blob2, struct got_object_id *id1,
2867 struct got_object_id *id2, const char *path1, const char *path2,
2868 mode_t mode1, mode_t mode2, struct got_repository *repo)
2870 static const struct got_error *err = NULL;
2871 struct merge_file_cb_arg *a = arg;
2872 struct got_fileindex_entry *ie;
2873 char *ondisk_path = NULL;
2875 unsigned char status;
2876 int local_changes_subsumed;
2877 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2878 char *id_str = NULL, *label_deriv2 = NULL;
2880 if (blob1 && blob2) {
2881 ie = got_fileindex_entry_get(a->fileindex, path2,
2884 return (*a->progress_cb)(a->progress_arg,
2885 GOT_STATUS_MISSING, path2);
2887 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2889 return got_error_from_errno("asprintf");
2891 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2896 if (status == GOT_STATUS_DELETE) {
2897 err = (*a->progress_cb)(a->progress_arg,
2898 GOT_STATUS_MERGE, path2);
2901 if (status != GOT_STATUS_NO_CHANGE &&
2902 status != GOT_STATUS_MODIFY &&
2903 status != GOT_STATUS_CONFLICT &&
2904 status != GOT_STATUS_ADD) {
2905 err = (*a->progress_cb)(a->progress_arg, status, path2);
2909 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2911 err = got_object_blob_read_to_str(&link_target2, blob2);
2914 err = merge_symlink(a->worktree, blob1, ondisk_path,
2915 path2, a->label_orig, link_target2, a->commit_id2,
2916 repo, a->progress_cb, a->progress_arg);
2921 f_orig = got_opentemp();
2922 if (f_orig == NULL) {
2923 err = got_error_from_errno("got_opentemp");
2926 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2931 f_deriv2 = got_opentemp();
2932 if (f_deriv2 == NULL)
2934 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2939 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2941 err = got_error_from_errno2("open",
2945 f_deriv = fdopen(fd, "r");
2946 if (f_deriv == NULL) {
2947 err = got_error_from_errno2("fdopen",
2952 err = got_object_id_str(&id_str, a->commit_id2);
2955 if (asprintf(&label_deriv2, "%s: commit %s",
2956 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2957 err = got_error_from_errno("asprintf");
2960 err = merge_file(&local_changes_subsumed, a->worktree,
2961 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2962 sb.st_mode, a->label_orig, NULL, label_deriv2,
2963 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2964 a->progress_cb, a->progress_arg);
2967 ie = got_fileindex_entry_get(a->fileindex, path1,
2970 return (*a->progress_cb)(a->progress_arg,
2971 GOT_STATUS_MISSING, path1);
2973 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2975 return got_error_from_errno("asprintf");
2977 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2983 case GOT_STATUS_NO_CHANGE:
2984 err = (*a->progress_cb)(a->progress_arg,
2985 GOT_STATUS_DELETE, path1);
2988 err = remove_ondisk_file(a->worktree->root_path, path1);
2992 got_fileindex_entry_mark_deleted_from_disk(ie);
2994 case GOT_STATUS_DELETE:
2995 case GOT_STATUS_MISSING:
2996 err = (*a->progress_cb)(a->progress_arg,
2997 GOT_STATUS_DELETE, path1);
3001 got_fileindex_entry_mark_deleted_from_disk(ie);
3003 case GOT_STATUS_ADD: {
3004 struct got_object_id *id;
3007 * Delete the added file only if its content already
3008 * exists in the repository.
3010 err = got_object_blob_file_create(&id, &blob1_f, path1);
3013 if (got_object_id_cmp(id, id1) == 0) {
3014 err = (*a->progress_cb)(a->progress_arg,
3015 GOT_STATUS_DELETE, path1);
3018 err = remove_ondisk_file(a->worktree->root_path,
3023 got_fileindex_entry_remove(a->fileindex,
3026 err = (*a->progress_cb)(a->progress_arg,
3027 GOT_STATUS_CANNOT_DELETE, path1);
3029 if (fclose(blob1_f) == EOF && err == NULL)
3030 err = got_error_from_errno("fclose");
3036 case GOT_STATUS_MODIFY:
3037 case GOT_STATUS_CONFLICT:
3038 err = (*a->progress_cb)(a->progress_arg,
3039 GOT_STATUS_CANNOT_DELETE, path1);
3043 case GOT_STATUS_OBSTRUCTED:
3044 err = (*a->progress_cb)(a->progress_arg, status, path1);
3052 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3054 return got_error_from_errno("asprintf");
3055 ie = got_fileindex_entry_get(a->fileindex, path2,
3058 err = get_file_status(&status, &sb, ie, ondisk_path,
3062 if (status != GOT_STATUS_NO_CHANGE &&
3063 status != GOT_STATUS_MODIFY &&
3064 status != GOT_STATUS_CONFLICT &&
3065 status != GOT_STATUS_ADD) {
3066 err = (*a->progress_cb)(a->progress_arg,
3070 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3072 err = got_object_blob_read_to_str(&link_target2,
3076 err = merge_symlink(a->worktree, NULL,
3077 ondisk_path, path2, a->label_orig,
3078 link_target2, a->commit_id2, repo,
3079 a->progress_cb, a->progress_arg);
3081 } else if (S_ISREG(sb.st_mode)) {
3082 err = merge_blob(&local_changes_subsumed,
3083 a->worktree, NULL, ondisk_path, path2,
3084 sb.st_mode, a->label_orig, blob2,
3085 a->commit_id2, repo, a->progress_cb,
3088 err = got_error_path(ondisk_path,
3089 GOT_ERR_FILE_OBSTRUCTED);
3093 if (status == GOT_STATUS_DELETE) {
3094 err = got_fileindex_entry_update(ie,
3095 a->worktree->root_fd, path2, blob2->id.sha1,
3096 a->worktree->base_commit_id->sha1, 0);
3101 int is_bad_symlink = 0;
3102 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3103 if (S_ISLNK(mode2)) {
3104 err = install_symlink(&is_bad_symlink,
3105 a->worktree, ondisk_path, path2, blob2, 0,
3106 0, 1, a->allow_bad_symlinks, repo,
3107 a->progress_cb, a->progress_arg);
3109 err = install_blob(a->worktree, ondisk_path, path2,
3110 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3111 a->progress_cb, a->progress_arg);
3115 err = got_fileindex_entry_alloc(&ie, path2);
3118 err = got_fileindex_entry_update(ie,
3119 a->worktree->root_fd, path2, NULL, NULL, 1);
3121 got_fileindex_entry_free(ie);
3124 err = got_fileindex_entry_add(a->fileindex, ie);
3126 got_fileindex_entry_free(ie);
3129 if (is_bad_symlink) {
3130 got_fileindex_entry_filetype_set(ie,
3131 GOT_FILEIDX_MODE_BAD_SYMLINK);
3136 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3137 err = got_error_from_errno("fclose");
3138 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3139 err = got_error_from_errno("fclose");
3140 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3141 err = got_error_from_errno("fclose");
3148 static const struct got_error *
3149 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3151 struct got_worktree *worktree = arg;
3153 /* Reject merges into a work tree with mixed base commits. */
3154 if (got_fileindex_entry_has_commit(ie) &&
3155 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3156 SHA1_DIGEST_LENGTH) != 0)
3157 return got_error(GOT_ERR_MIXED_COMMITS);
3162 struct check_merge_conflicts_arg {
3163 struct got_worktree *worktree;
3164 struct got_fileindex *fileindex;
3165 struct got_repository *repo;
3168 static const struct got_error *
3169 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3170 struct got_blob_object *blob2, struct got_object_id *id1,
3171 struct got_object_id *id2, const char *path1, const char *path2,
3172 mode_t mode1, mode_t mode2, struct got_repository *repo)
3174 const struct got_error *err = NULL;
3175 struct check_merge_conflicts_arg *a = arg;
3176 unsigned char status;
3178 struct got_fileindex_entry *ie;
3179 const char *path = path2 ? path2 : path1;
3180 struct got_object_id *id = id2 ? id2 : id1;
3186 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3190 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3192 return got_error_from_errno("asprintf");
3194 /* Reject merges into a work tree with conflicted files. */
3195 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3199 if (status == GOT_STATUS_CONFLICT)
3200 return got_error(GOT_ERR_CONFLICTS);
3205 static const struct got_error *
3206 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3207 const char *fileindex_path, struct got_object_id *commit_id1,
3208 struct got_object_id *commit_id2, struct got_repository *repo,
3209 got_worktree_checkout_cb progress_cb, void *progress_arg,
3210 got_cancel_cb cancel_cb, void *cancel_arg)
3212 const struct got_error *err = NULL, *sync_err;
3213 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3214 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3215 struct check_merge_conflicts_arg cmc_arg;
3216 struct merge_file_cb_arg arg;
3217 char *label_orig = NULL;
3220 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3221 worktree->path_prefix);
3222 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3228 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3232 err = got_object_id_str(&id_str, commit_id1);
3236 if (asprintf(&label_orig, "%s: commit %s",
3237 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3238 err = got_error_from_errno("asprintf");
3245 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3246 worktree->path_prefix);
3250 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3254 cmc_arg.worktree = worktree;
3255 cmc_arg.fileindex = fileindex;
3256 cmc_arg.repo = repo;
3257 err = got_diff_tree(tree1, tree2, "", "", repo,
3258 check_merge_conflicts, &cmc_arg, 0);
3262 arg.worktree = worktree;
3263 arg.fileindex = fileindex;
3264 arg.progress_cb = progress_cb;
3265 arg.progress_arg = progress_arg;
3266 arg.cancel_cb = cancel_cb;
3267 arg.cancel_arg = cancel_arg;
3268 arg.label_orig = label_orig;
3269 arg.commit_id2 = commit_id2;
3270 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3271 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3272 sync_err = sync_fileindex(fileindex, fileindex_path);
3273 if (sync_err && err == NULL)
3277 got_object_tree_close(tree1);
3279 got_object_tree_close(tree2);
3284 const struct got_error *
3285 got_worktree_merge_files(struct got_worktree *worktree,
3286 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3287 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3288 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3290 const struct got_error *err, *unlockerr;
3291 char *fileindex_path = NULL;
3292 struct got_fileindex *fileindex = NULL;
3294 err = lock_worktree(worktree, LOCK_EX);
3298 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3302 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3307 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3308 commit_id2, repo, progress_cb, progress_arg,
3309 cancel_cb, cancel_arg);
3312 got_fileindex_free(fileindex);
3313 free(fileindex_path);
3314 unlockerr = lock_worktree(worktree, LOCK_SH);
3315 if (unlockerr && err == NULL)
3320 struct diff_dir_cb_arg {
3321 struct got_fileindex *fileindex;
3322 struct got_worktree *worktree;
3323 const char *status_path;
3324 size_t status_path_len;
3325 struct got_repository *repo;
3326 got_worktree_status_cb status_cb;
3328 got_cancel_cb cancel_cb;
3330 /* A pathlist containing per-directory pathlists of ignore patterns. */
3331 struct got_pathlist_head *ignores;
3332 int report_unchanged;
3336 static const struct got_error *
3337 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3338 int dirfd, const char *de_name,
3339 got_worktree_status_cb status_cb, void *status_arg,
3340 struct got_repository *repo, int report_unchanged)
3342 const struct got_error *err = NULL;
3343 unsigned char status = GOT_STATUS_NO_CHANGE;
3344 unsigned char staged_status = get_staged_status(ie);
3346 struct got_object_id blob_id, commit_id, staged_blob_id;
3347 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3348 struct got_object_id *staged_blob_idp = NULL;
3350 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3354 if (status == GOT_STATUS_NO_CHANGE &&
3355 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3358 if (got_fileindex_entry_has_blob(ie)) {
3359 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3360 blob_idp = &blob_id;
3362 if (got_fileindex_entry_has_commit(ie)) {
3363 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3364 commit_idp = &commit_id;
3366 if (staged_status == GOT_STATUS_ADD ||
3367 staged_status == GOT_STATUS_MODIFY) {
3368 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3369 SHA1_DIGEST_LENGTH);
3370 staged_blob_idp = &staged_blob_id;
3373 return (*status_cb)(status_arg, status, staged_status,
3374 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3377 static const struct got_error *
3378 status_old_new(void *arg, struct got_fileindex_entry *ie,
3379 struct dirent *de, const char *parent_path, int dirfd)
3381 const struct got_error *err = NULL;
3382 struct diff_dir_cb_arg *a = arg;
3385 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3386 return got_error(GOT_ERR_CANCELLED);
3388 if (got_path_cmp(parent_path, a->status_path,
3389 strlen(parent_path), a->status_path_len) != 0 &&
3390 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3393 if (parent_path[0]) {
3394 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3395 parent_path, de->d_name) == -1)
3396 return got_error_from_errno("asprintf");
3398 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3400 return got_error_from_errno("asprintf");
3403 err = report_file_status(ie, abspath, dirfd, de->d_name,
3404 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3409 static const struct got_error *
3410 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3412 struct diff_dir_cb_arg *a = arg;
3413 struct got_object_id blob_id, commit_id;
3414 unsigned char status;
3416 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3417 return got_error(GOT_ERR_CANCELLED);
3419 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3422 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3423 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3424 if (got_fileindex_entry_has_file_on_disk(ie))
3425 status = GOT_STATUS_MISSING;
3427 status = GOT_STATUS_DELETE;
3428 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3429 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3433 free_ignorelist(struct got_pathlist_head *ignorelist)
3435 struct got_pathlist_entry *pe;
3437 TAILQ_FOREACH(pe, ignorelist, entry)
3438 free((char *)pe->path);
3439 got_pathlist_free(ignorelist);
3443 free_ignores(struct got_pathlist_head *ignores)
3445 struct got_pathlist_entry *pe;
3447 TAILQ_FOREACH(pe, ignores, entry) {
3448 struct got_pathlist_head *ignorelist = pe->data;
3449 free_ignorelist(ignorelist);
3450 free((char *)pe->path);
3452 got_pathlist_free(ignores);
3455 static const struct got_error *
3456 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3458 const struct got_error *err = NULL;
3459 struct got_pathlist_entry *pe = NULL;
3460 struct got_pathlist_head *ignorelist;
3461 char *line = NULL, *pattern, *dirpath = NULL;
3462 size_t linesize = 0;
3465 ignorelist = calloc(1, sizeof(*ignorelist));
3466 if (ignorelist == NULL)
3467 return got_error_from_errno("calloc");
3468 TAILQ_INIT(ignorelist);
3470 while ((linelen = getline(&line, &linesize, f)) != -1) {
3471 if (linelen > 0 && line[linelen - 1] == '\n')
3472 line[linelen - 1] = '\0';
3474 /* Git's ignores may contain comments. */
3478 /* Git's negated patterns are not (yet?) supported. */
3482 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3484 err = got_error_from_errno("asprintf");
3487 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3492 err = got_error_from_errno("getline");
3496 dirpath = strdup(path);
3497 if (dirpath == NULL) {
3498 err = got_error_from_errno("strdup");
3501 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3504 if (err || pe == NULL) {
3506 free_ignorelist(ignorelist);
3512 match_ignores(struct got_pathlist_head *ignores, const char *path)
3514 struct got_pathlist_entry *pe;
3516 /* Handle patterns which match in all directories. */
3517 TAILQ_FOREACH(pe, ignores, entry) {
3518 struct got_pathlist_head *ignorelist = pe->data;
3519 struct got_pathlist_entry *pi;
3521 TAILQ_FOREACH(pi, ignorelist, entry) {
3522 const char *p, *pattern = pi->path;
3524 if (strncmp(pattern, "**/", 3) != 0)
3529 if (fnmatch(pattern, p,
3530 FNM_PATHNAME | FNM_LEADING_DIR)) {
3531 /* Retry in next directory. */
3532 while (*p && *p != '/')
3544 * The ignores pathlist contains ignore lists from children before
3545 * parents, so we can find the most specific ignorelist by walking
3546 * ignores backwards.
3548 pe = TAILQ_LAST(ignores, got_pathlist_head);
3550 if (got_path_is_child(path, pe->path, pe->path_len)) {
3551 struct got_pathlist_head *ignorelist = pe->data;
3552 struct got_pathlist_entry *pi;
3553 TAILQ_FOREACH(pi, ignorelist, entry) {
3554 const char *pattern = pi->path;
3555 int flags = FNM_LEADING_DIR;
3556 if (strstr(pattern, "/**/") == NULL)
3557 flags |= FNM_PATHNAME;
3558 if (fnmatch(pattern, path, flags))
3563 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3569 static const struct got_error *
3570 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3571 const char *path, int dirfd, const char *ignores_filename)
3573 const struct got_error *err = NULL;
3576 FILE *ignoresfile = NULL;
3578 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3579 path[0] ? "/" : "", ignores_filename) == -1)
3580 return got_error_from_errno("asprintf");
3583 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3585 if (errno != ENOENT && errno != EACCES)
3586 err = got_error_from_errno2("openat",
3589 ignoresfile = fdopen(fd, "r");
3590 if (ignoresfile == NULL)
3591 err = got_error_from_errno2("fdopen",
3595 err = read_ignores(ignores, path, ignoresfile);
3599 ignoresfile = fopen(ignorespath, "r");
3600 if (ignoresfile == NULL) {
3601 if (errno != ENOENT && errno != EACCES)
3602 err = got_error_from_errno2("fopen",
3605 err = read_ignores(ignores, path, ignoresfile);
3608 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3609 err = got_error_from_errno2("fclose", path);
3610 if (fd != -1 && close(fd) == -1 && err == NULL)
3611 err = got_error_from_errno2("close", path);
3616 static const struct got_error *
3617 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3619 const struct got_error *err = NULL;
3620 struct diff_dir_cb_arg *a = arg;
3623 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3624 return got_error(GOT_ERR_CANCELLED);
3626 if (parent_path[0]) {
3627 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3628 return got_error_from_errno("asprintf");
3633 if (de->d_type != DT_DIR &&
3634 got_path_is_child(path, a->status_path, a->status_path_len)
3635 && !match_ignores(a->ignores, path))
3636 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3637 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3643 static const struct got_error *
3644 status_traverse(void *arg, const char *path, int dirfd)
3646 const struct got_error *err = NULL;
3647 struct diff_dir_cb_arg *a = arg;
3652 err = add_ignores(a->ignores, a->worktree->root_path,
3653 path, dirfd, ".cvsignore");
3657 err = add_ignores(a->ignores, a->worktree->root_path, path,
3658 dirfd, ".gitignore");
3663 static const struct got_error *
3664 report_single_file_status(const char *path, const char *ondisk_path,
3665 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3666 void *status_arg, struct got_repository *repo, int report_unchanged,
3667 struct got_pathlist_head *ignores, int no_ignores)
3669 struct got_fileindex_entry *ie;
3672 if (!no_ignores && match_ignores(ignores, path))
3675 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3677 return report_file_status(ie, ondisk_path, -1, NULL,
3678 status_cb, status_arg, repo, report_unchanged);
3680 if (lstat(ondisk_path, &sb) == -1) {
3681 if (errno != ENOENT)
3682 return got_error_from_errno2("lstat", ondisk_path);
3683 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3684 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3688 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3689 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3690 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3695 static const struct got_error *
3696 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3697 const char *root_path, const char *path)
3699 const struct got_error *err;
3700 char *parent_path, *next_parent_path = NULL;
3702 err = add_ignores(ignores, root_path, "", -1,
3707 err = add_ignores(ignores, root_path, "", -1,
3712 err = got_path_dirname(&parent_path, path);
3714 if (err->code == GOT_ERR_BAD_PATH)
3715 return NULL; /* cannot traverse parent */
3719 err = add_ignores(ignores, root_path, parent_path, -1,
3723 err = add_ignores(ignores, root_path, parent_path, -1,
3727 err = got_path_dirname(&next_parent_path, parent_path);
3729 if (err->code == GOT_ERR_BAD_PATH)
3730 err = NULL; /* traversed everything */
3733 if (got_path_is_root_dir(parent_path))
3736 parent_path = next_parent_path;
3737 next_parent_path = NULL;
3741 free(next_parent_path);
3745 static const struct got_error *
3746 worktree_status(struct got_worktree *worktree, const char *path,
3747 struct got_fileindex *fileindex, struct got_repository *repo,
3748 got_worktree_status_cb status_cb, void *status_arg,
3749 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3750 int report_unchanged)
3752 const struct got_error *err = NULL;
3754 struct got_fileindex_diff_dir_cb fdiff_cb;
3755 struct diff_dir_cb_arg arg;
3756 char *ondisk_path = NULL;
3757 struct got_pathlist_head ignores;
3759 TAILQ_INIT(&ignores);
3761 if (asprintf(&ondisk_path, "%s%s%s",
3762 worktree->root_path, path[0] ? "/" : "", path) == -1)
3763 return got_error_from_errno("asprintf");
3765 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3767 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3769 err = got_error_from_errno2("open", ondisk_path);
3772 err = add_ignores_from_parent_paths(&ignores,
3773 worktree->root_path, ondisk_path);
3777 err = report_single_file_status(path, ondisk_path,
3778 fileindex, status_cb, status_arg, repo,
3779 report_unchanged, &ignores, no_ignores);
3782 fdiff_cb.diff_old_new = status_old_new;
3783 fdiff_cb.diff_old = status_old;
3784 fdiff_cb.diff_new = status_new;
3785 fdiff_cb.diff_traverse = status_traverse;
3786 arg.fileindex = fileindex;
3787 arg.worktree = worktree;
3788 arg.status_path = path;
3789 arg.status_path_len = strlen(path);
3791 arg.status_cb = status_cb;
3792 arg.status_arg = status_arg;
3793 arg.cancel_cb = cancel_cb;
3794 arg.cancel_arg = cancel_arg;
3795 arg.report_unchanged = report_unchanged;
3796 arg.no_ignores = no_ignores;
3798 err = add_ignores_from_parent_paths(&ignores,
3799 worktree->root_path, path);
3803 arg.ignores = &ignores;
3804 err = got_fileindex_diff_dir(fileindex, fd,
3805 worktree->root_path, path, repo, &fdiff_cb, &arg);
3808 free_ignores(&ignores);
3809 if (fd != -1 && close(fd) == -1 && err == NULL)
3810 err = got_error_from_errno("close");
3815 const struct got_error *
3816 got_worktree_status(struct got_worktree *worktree,
3817 struct got_pathlist_head *paths, struct got_repository *repo,
3818 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3819 got_cancel_cb cancel_cb, void *cancel_arg)
3821 const struct got_error *err = NULL;
3822 char *fileindex_path = NULL;
3823 struct got_fileindex *fileindex = NULL;
3824 struct got_pathlist_entry *pe;
3826 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3830 TAILQ_FOREACH(pe, paths, entry) {
3831 err = worktree_status(worktree, pe->path, fileindex, repo,
3832 status_cb, status_arg, cancel_cb, cancel_arg,
3837 free(fileindex_path);
3838 got_fileindex_free(fileindex);
3842 const struct got_error *
3843 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3846 const struct got_error *err = NULL;
3847 char *resolved = NULL, *cwd = NULL, *path = NULL;
3850 char *abspath = NULL;
3851 char canonpath[PATH_MAX];
3855 cwd = getcwd(NULL, 0);
3857 return got_error_from_errno("getcwd");
3859 if (lstat(arg, &sb) == -1) {
3860 if (errno != ENOENT) {
3861 err = got_error_from_errno2("lstat", arg);
3866 if (S_ISLNK(sb.st_mode)) {
3868 * We cannot use realpath(3) with symlinks since we want to
3869 * operate on the symlink itself.
3870 * But we can make the path absolute, assuming it is relative
3871 * to the current working directory, and then canonicalize it.
3873 if (!got_path_is_absolute(arg)) {
3874 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3875 err = got_error_from_errno("asprintf");
3880 err = got_canonpath(abspath ? abspath : arg, canonpath,
3884 resolved = strdup(canonpath);
3885 if (resolved == NULL) {
3886 err = got_error_from_errno("strdup");
3890 resolved = realpath(arg, NULL);
3891 if (resolved == NULL) {
3892 if (errno != ENOENT) {
3893 err = got_error_from_errno2("realpath", arg);
3896 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3897 err = got_error_from_errno("asprintf");
3900 err = got_canonpath(abspath, canonpath,
3904 resolved = strdup(canonpath);
3905 if (resolved == NULL) {
3906 err = got_error_from_errno("strdup");
3912 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3913 strlen(got_worktree_get_root_path(worktree)))) {
3914 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3918 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3919 err = got_path_skip_common_ancestor(&path,
3920 got_worktree_get_root_path(worktree), resolved);
3926 err = got_error_from_errno("strdup");
3931 /* XXX status walk can't deal with trailing slash! */
3933 while (len > 0 && path[len - 1] == '/') {
3934 path[len - 1] = '\0';
3948 struct schedule_addition_args {
3949 struct got_worktree *worktree;
3950 struct got_fileindex *fileindex;
3951 got_worktree_checkout_cb progress_cb;
3953 struct got_repository *repo;
3956 static const struct got_error *
3957 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3958 const char *relpath, struct got_object_id *blob_id,
3959 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3960 int dirfd, const char *de_name)
3962 struct schedule_addition_args *a = arg;
3963 const struct got_error *err = NULL;
3964 struct got_fileindex_entry *ie;
3968 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3970 return got_error_from_errno("asprintf");
3972 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3974 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3978 /* Re-adding an existing entry is a no-op. */
3979 if (status == GOT_STATUS_ADD)
3981 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3986 if (status != GOT_STATUS_UNVERSIONED) {
3987 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3991 err = got_fileindex_entry_alloc(&ie, relpath);
3994 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3995 relpath, NULL, NULL, 1);
3997 got_fileindex_entry_free(ie);
4000 err = got_fileindex_entry_add(a->fileindex, ie);
4002 got_fileindex_entry_free(ie);
4009 if (status == GOT_STATUS_ADD)
4011 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4014 const struct got_error *
4015 got_worktree_schedule_add(struct got_worktree *worktree,
4016 struct got_pathlist_head *paths,
4017 got_worktree_checkout_cb progress_cb, void *progress_arg,
4018 struct got_repository *repo, int no_ignores)
4020 struct got_fileindex *fileindex = NULL;
4021 char *fileindex_path = NULL;
4022 const struct got_error *err = NULL, *sync_err, *unlockerr;
4023 struct got_pathlist_entry *pe;
4024 struct schedule_addition_args saa;
4026 err = lock_worktree(worktree, LOCK_EX);
4030 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4034 saa.worktree = worktree;
4035 saa.fileindex = fileindex;
4036 saa.progress_cb = progress_cb;
4037 saa.progress_arg = progress_arg;
4040 TAILQ_FOREACH(pe, paths, entry) {
4041 err = worktree_status(worktree, pe->path, fileindex, repo,
4042 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4046 sync_err = sync_fileindex(fileindex, fileindex_path);
4047 if (sync_err && err == NULL)
4050 free(fileindex_path);
4052 got_fileindex_free(fileindex);
4053 unlockerr = lock_worktree(worktree, LOCK_SH);
4054 if (unlockerr && err == NULL)
4059 struct schedule_deletion_args {
4060 struct got_worktree *worktree;
4061 struct got_fileindex *fileindex;
4062 got_worktree_delete_cb progress_cb;
4064 struct got_repository *repo;
4065 int delete_local_mods;
4067 const char *status_codes;
4070 static const struct got_error *
4071 schedule_for_deletion(void *arg, unsigned char status,
4072 unsigned char staged_status, const char *relpath,
4073 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 struct got_object_id *commit_id, int dirfd, const char *de_name)
4076 struct schedule_deletion_args *a = arg;
4077 const struct got_error *err = NULL;
4078 struct got_fileindex_entry *ie = NULL;
4082 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4084 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4086 staged_status = get_staged_status(ie);
4087 if (staged_status != GOT_STATUS_NO_CHANGE) {
4088 if (staged_status == GOT_STATUS_DELETE)
4090 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4093 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4095 return got_error_from_errno("asprintf");
4097 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4102 if (a->status_codes) {
4103 size_t ncodes = strlen(a->status_codes);
4105 for (i = 0; i < ncodes ; i++) {
4106 if (status == a->status_codes[i])
4110 /* Do not delete files in non-matching status. */
4114 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4115 a->status_codes[i] != GOT_STATUS_MISSING) {
4116 static char msg[64];
4117 snprintf(msg, sizeof(msg),
4118 "invalid status code '%c'", a->status_codes[i]);
4119 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4124 if (status != GOT_STATUS_NO_CHANGE) {
4125 if (status == GOT_STATUS_DELETE)
4127 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4128 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4131 if (status != GOT_STATUS_MODIFY &&
4132 status != GOT_STATUS_MISSING) {
4133 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4138 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4142 if (unlinkat(dirfd, de_name, 0) != 0) {
4143 err = got_error_from_errno2("unlinkat",
4147 } else if (unlink(ondisk_path) != 0) {
4148 err = got_error_from_errno2("unlink", ondisk_path);
4152 root_len = strlen(a->worktree->root_path);
4155 err = got_path_dirname(&parent, ondisk_path);
4159 ondisk_path = parent;
4160 if (rmdir(ondisk_path) == -1) {
4161 if (errno != ENOTEMPTY)
4162 err = got_error_from_errno2("rmdir",
4166 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4167 strlen(ondisk_path), root_len) != 0);
4170 got_fileindex_entry_mark_deleted_from_disk(ie);
4175 if (status == GOT_STATUS_DELETE)
4177 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4178 staged_status, relpath);
4181 const struct got_error *
4182 got_worktree_schedule_delete(struct got_worktree *worktree,
4183 struct got_pathlist_head *paths, int delete_local_mods,
4184 const char *status_codes,
4185 got_worktree_delete_cb progress_cb, void *progress_arg,
4186 struct got_repository *repo, int keep_on_disk)
4188 struct got_fileindex *fileindex = NULL;
4189 char *fileindex_path = NULL;
4190 const struct got_error *err = NULL, *sync_err, *unlockerr;
4191 struct got_pathlist_entry *pe;
4192 struct schedule_deletion_args sda;
4194 err = lock_worktree(worktree, LOCK_EX);
4198 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4202 sda.worktree = worktree;
4203 sda.fileindex = fileindex;
4204 sda.progress_cb = progress_cb;
4205 sda.progress_arg = progress_arg;
4207 sda.delete_local_mods = delete_local_mods;
4208 sda.keep_on_disk = keep_on_disk;
4209 sda.status_codes = status_codes;
4211 TAILQ_FOREACH(pe, paths, entry) {
4212 err = worktree_status(worktree, pe->path, fileindex, repo,
4213 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4217 sync_err = sync_fileindex(fileindex, fileindex_path);
4218 if (sync_err && err == NULL)
4221 free(fileindex_path);
4223 got_fileindex_free(fileindex);
4224 unlockerr = lock_worktree(worktree, LOCK_SH);
4225 if (unlockerr && err == NULL)
4230 static const struct got_error *
4231 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4233 const struct got_error *err = NULL;
4235 size_t linesize = 0, n;
4238 linelen = getline(&line, &linesize, infile);
4239 if (linelen == -1) {
4240 if (ferror(infile)) {
4241 err = got_error_from_errno("getline");
4247 n = fwrite(line, 1, linelen, outfile);
4249 err = got_ferror(outfile, GOT_ERR_IO);
4254 n = fwrite(line, 1, linelen, rejectfile);
4256 err = got_ferror(outfile, GOT_ERR_IO);
4263 static const struct got_error *
4264 skip_one_line(FILE *f)
4267 size_t linesize = 0;
4270 linelen = getline(&line, &linesize, f);
4271 if (linelen == -1) {
4273 return got_error_from_errno("getline");
4280 static const struct got_error *
4281 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4282 int start_old, int end_old, int start_new, int end_new,
4283 FILE *outfile, FILE *rejectfile)
4285 const struct got_error *err;
4287 /* Copy old file's lines leading up to patch. */
4288 while (!feof(f1) && *line_cur1 < start_old) {
4289 err = copy_one_line(f1, outfile, NULL);
4294 /* Skip new file's lines leading up to patch. */
4295 while (!feof(f2) && *line_cur2 < start_new) {
4297 err = copy_one_line(f2, NULL, rejectfile);
4299 err = skip_one_line(f2);
4304 /* Copy patched lines. */
4305 while (!feof(f2) && *line_cur2 <= end_new) {
4306 err = copy_one_line(f2, outfile, NULL);
4311 /* Skip over old file's replaced lines. */
4312 while (!feof(f1) && *line_cur1 <= end_old) {
4314 err = copy_one_line(f1, NULL, rejectfile);
4316 err = skip_one_line(f1);
4325 static const struct got_error *
4326 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4327 FILE *outfile, FILE *rejectfile)
4329 const struct got_error *err;
4332 /* Copy old file's lines until EOF. */
4334 err = copy_one_line(f1, outfile, NULL);
4341 /* Copy new file's lines until EOF. */
4343 err = copy_one_line(f2, NULL, rejectfile);
4353 static const struct got_error *
4354 apply_or_reject_change(int *choice, int *nchunks_used,
4355 struct diff_result *diff_result, int n,
4356 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4357 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4358 got_worktree_patch_cb patch_cb, void *patch_arg)
4360 const struct got_error *err = NULL;
4361 struct diff_chunk_context cc = {};
4362 int start_old, end_old, start_new, end_new;
4364 struct diff_output_unidiff_state *diff_state;
4365 struct diff_input_info diff_info;
4368 *choice = GOT_PATCH_CHOICE_NONE;
4370 /* Get changed line numbers without context lines for copy_change(). */
4371 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4372 start_old = cc.left.start;
4373 end_old = cc.left.end;
4374 start_new = cc.right.start;
4375 end_new = cc.right.end;
4377 /* Get the same change with context lines for display. */
4378 memset(&cc, 0, sizeof(cc));
4379 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4381 memset(&diff_info, 0, sizeof(diff_info));
4382 diff_info.left_path = relpath;
4383 diff_info.right_path = relpath;
4385 diff_state = diff_output_unidiff_state_alloc();
4386 if (diff_state == NULL)
4387 return got_error_set_errno(ENOMEM,
4388 "diff_output_unidiff_state_alloc");
4390 hunkfile = got_opentemp();
4391 if (hunkfile == NULL) {
4392 err = got_error_from_errno("got_opentemp");
4396 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4398 if (rc != DIFF_RC_OK) {
4399 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4403 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4404 err = got_ferror(hunkfile, GOT_ERR_IO);
4408 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4409 hunkfile, changeno, nchanges);
4414 case GOT_PATCH_CHOICE_YES:
4415 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4416 end_old, start_new, end_new, outfile, rejectfile);
4418 case GOT_PATCH_CHOICE_NO:
4419 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4420 end_old, start_new, end_new, rejectfile, outfile);
4422 case GOT_PATCH_CHOICE_QUIT:
4425 err = got_error(GOT_ERR_PATCH_CHOICE);
4429 diff_output_unidiff_state_free(diff_state);
4430 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4431 err = got_error_from_errno("fclose");
4435 struct revert_file_args {
4436 struct got_worktree *worktree;
4437 struct got_fileindex *fileindex;
4438 got_worktree_checkout_cb progress_cb;
4440 got_worktree_patch_cb patch_cb;
4442 struct got_repository *repo;
4443 int unlink_added_files;
4446 static const struct got_error *
4447 create_patched_content(char **path_outfile, int reverse_patch,
4448 struct got_object_id *blob_id, const char *path2,
4449 int dirfd2, const char *de_name2,
4450 const char *relpath, struct got_repository *repo,
4451 got_worktree_patch_cb patch_cb, void *patch_arg)
4453 const struct got_error *err, *free_err;
4454 struct got_blob_object *blob = NULL;
4455 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4457 char link_target[PATH_MAX];
4458 ssize_t link_len = 0;
4459 char *path1 = NULL, *id_str = NULL;
4461 struct got_diffreg_result *diffreg_result = NULL;
4462 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4463 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4465 *path_outfile = NULL;
4467 err = got_object_id_str(&id_str, blob_id);
4472 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4474 if (errno != ELOOP) {
4475 err = got_error_from_errno2("openat", path2);
4478 link_len = readlinkat(dirfd2, de_name2,
4479 link_target, sizeof(link_target));
4481 return got_error_from_errno2("readlinkat", path2);
4482 sb2.st_mode = S_IFLNK;
4483 sb2.st_size = link_len;
4486 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4488 if (errno != ELOOP) {
4489 err = got_error_from_errno2("open", path2);
4492 link_len = readlink(path2, link_target,
4493 sizeof(link_target));
4495 return got_error_from_errno2("readlink", path2);
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4501 if (fstat(fd2, &sb2) == -1) {
4502 err = got_error_from_errno2("fstat", path2);
4506 f2 = fdopen(fd2, "r");
4508 err = got_error_from_errno2("fdopen", path2);
4514 f2 = got_opentemp();
4516 err = got_error_from_errno2("got_opentemp", path2);
4519 n = fwrite(link_target, 1, link_len, f2);
4520 if (n != link_len) {
4521 err = got_ferror(f2, GOT_ERR_IO);
4524 if (fflush(f2) == EOF) {
4525 err = got_error_from_errno("fflush");
4531 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4535 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4539 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4543 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4548 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4552 if (fseek(f1, 0L, SEEK_SET) == -1)
4553 return got_ferror(f1, GOT_ERR_IO);
4554 if (fseek(f2, 0L, SEEK_SET) == -1)
4555 return got_ferror(f2, GOT_ERR_IO);
4557 /* Count the number of actual changes in the diff result. */
4558 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4559 struct diff_chunk_context cc = {};
4560 diff_chunk_context_load_change(&cc, &nchunks_used,
4561 diffreg_result->result, n, 0);
4564 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4566 err = apply_or_reject_change(&choice, &nchunks_used,
4567 diffreg_result->result, n, relpath, f1, f2,
4568 &line_cur1, &line_cur2,
4569 reverse_patch ? NULL : outfile,
4570 reverse_patch ? outfile : NULL,
4571 ++i, nchanges, patch_cb, patch_arg);
4574 if (choice == GOT_PATCH_CHOICE_YES)
4576 else if (choice == GOT_PATCH_CHOICE_QUIT)
4580 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4581 reverse_patch ? NULL : outfile,
4582 reverse_patch ? outfile : NULL);
4586 if (!S_ISLNK(sb2.st_mode)) {
4587 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4588 err = got_error_from_errno2("fchmod", path2);
4596 got_object_blob_close(blob);
4597 free_err = got_diffreg_result_free(diffreg_result);
4600 if (f1 && fclose(f1) == EOF && err == NULL)
4601 err = got_error_from_errno2("fclose", path1);
4602 if (f2 && fclose(f2) == EOF && err == NULL)
4603 err = got_error_from_errno2("fclose", path2);
4604 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4605 err = got_error_from_errno2("close", path2);
4606 if (outfile && fclose(outfile) == EOF && err == NULL)
4607 err = got_error_from_errno2("fclose", *path_outfile);
4608 if (path1 && unlink(path1) == -1 && err == NULL)
4609 err = got_error_from_errno2("unlink", path1);
4610 if (err || !have_content) {
4611 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4612 err = got_error_from_errno2("unlink", *path_outfile);
4613 free(*path_outfile);
4614 *path_outfile = NULL;
4620 static const struct got_error *
4621 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4622 const char *relpath, struct got_object_id *blob_id,
4623 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4624 int dirfd, const char *de_name)
4626 struct revert_file_args *a = arg;
4627 const struct got_error *err = NULL;
4628 char *parent_path = NULL;
4629 struct got_fileindex_entry *ie;
4630 struct got_tree_object *tree = NULL;
4631 struct got_object_id *tree_id = NULL;
4632 const struct got_tree_entry *te = NULL;
4633 char *tree_path = NULL, *te_name;
4634 char *ondisk_path = NULL, *path_content = NULL;
4635 struct got_blob_object *blob = NULL;
4637 /* Reverting a staged deletion is a no-op. */
4638 if (status == GOT_STATUS_DELETE &&
4639 staged_status != GOT_STATUS_NO_CHANGE)
4642 if (status == GOT_STATUS_UNVERSIONED)
4643 return (*a->progress_cb)(a->progress_arg,
4644 GOT_STATUS_UNVERSIONED, relpath);
4646 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4648 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4650 /* Construct in-repository path of tree which contains this blob. */
4651 err = got_path_dirname(&parent_path, ie->path);
4653 if (err->code != GOT_ERR_BAD_PATH)
4655 parent_path = strdup("/");
4656 if (parent_path == NULL) {
4657 err = got_error_from_errno("strdup");
4661 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4662 tree_path = strdup(parent_path);
4663 if (tree_path == NULL) {
4664 err = got_error_from_errno("strdup");
4668 if (got_path_is_root_dir(parent_path)) {
4669 tree_path = strdup(a->worktree->path_prefix);
4670 if (tree_path == NULL) {
4671 err = got_error_from_errno("strdup");
4675 if (asprintf(&tree_path, "%s/%s",
4676 a->worktree->path_prefix, parent_path) == -1) {
4677 err = got_error_from_errno("asprintf");
4683 err = got_object_id_by_path(&tree_id, a->repo,
4684 a->worktree->base_commit_id, tree_path);
4686 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4687 (status == GOT_STATUS_ADD ||
4688 staged_status == GOT_STATUS_ADD)))
4691 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4695 err = got_path_basename(&te_name, ie->path);
4699 te = got_object_tree_find_entry(tree, te_name);
4701 if (te == NULL && status != GOT_STATUS_ADD &&
4702 staged_status != GOT_STATUS_ADD) {
4703 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4709 case GOT_STATUS_ADD:
4711 int choice = GOT_PATCH_CHOICE_NONE;
4712 err = (*a->patch_cb)(&choice, a->patch_arg,
4713 status, ie->path, NULL, 1, 1);
4716 if (choice != GOT_PATCH_CHOICE_YES)
4719 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4723 got_fileindex_entry_remove(a->fileindex, ie);
4724 if (a->unlink_added_files) {
4725 if (asprintf(&ondisk_path, "%s/%s",
4726 got_worktree_get_root_path(a->worktree),
4728 err = got_error_from_errno("asprintf");
4731 if (unlink(ondisk_path) == -1) {
4732 err = got_error_from_errno2("unlink",
4738 case GOT_STATUS_DELETE:
4740 int choice = GOT_PATCH_CHOICE_NONE;
4741 err = (*a->patch_cb)(&choice, a->patch_arg,
4742 status, ie->path, NULL, 1, 1);
4745 if (choice != GOT_PATCH_CHOICE_YES)
4749 case GOT_STATUS_MODIFY:
4750 case GOT_STATUS_MODE_CHANGE:
4751 case GOT_STATUS_CONFLICT:
4752 case GOT_STATUS_MISSING: {
4753 struct got_object_id id;
4754 if (staged_status == GOT_STATUS_ADD ||
4755 staged_status == GOT_STATUS_MODIFY) {
4756 memcpy(id.sha1, ie->staged_blob_sha1,
4757 SHA1_DIGEST_LENGTH);
4759 memcpy(id.sha1, ie->blob_sha1,
4760 SHA1_DIGEST_LENGTH);
4761 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4765 if (asprintf(&ondisk_path, "%s/%s",
4766 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4767 err = got_error_from_errno("asprintf");
4771 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4772 status == GOT_STATUS_CONFLICT)) {
4773 int is_bad_symlink = 0;
4774 err = create_patched_content(&path_content, 1, &id,
4775 ondisk_path, dirfd, de_name, ie->path, a->repo,
4776 a->patch_cb, a->patch_arg);
4777 if (err || path_content == NULL)
4779 if (te && S_ISLNK(te->mode)) {
4780 if (unlink(path_content) == -1) {
4781 err = got_error_from_errno2("unlink",
4785 err = install_symlink(&is_bad_symlink,
4786 a->worktree, ondisk_path, ie->path,
4787 blob, 0, 1, 0, 0, a->repo,
4788 a->progress_cb, a->progress_arg);
4790 if (rename(path_content, ondisk_path) == -1) {
4791 err = got_error_from_errno3("rename",
4792 path_content, ondisk_path);
4797 int is_bad_symlink = 0;
4798 if (te && S_ISLNK(te->mode)) {
4799 err = install_symlink(&is_bad_symlink,
4800 a->worktree, ondisk_path, ie->path,
4801 blob, 0, 1, 0, 0, a->repo,
4802 a->progress_cb, a->progress_arg);
4804 err = install_blob(a->worktree, ondisk_path,
4806 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4807 got_fileindex_perms_to_st(ie), blob,
4808 0, 1, 0, 0, a->repo,
4809 a->progress_cb, a->progress_arg);
4813 if (status == GOT_STATUS_DELETE ||
4814 status == GOT_STATUS_MODE_CHANGE) {
4815 err = got_fileindex_entry_update(ie,
4816 a->worktree->root_fd, relpath,
4818 a->worktree->base_commit_id->sha1, 1);
4822 if (is_bad_symlink) {
4823 got_fileindex_entry_filetype_set(ie,
4824 GOT_FILEIDX_MODE_BAD_SYMLINK);
4838 got_object_blob_close(blob);
4840 got_object_tree_close(tree);
4845 const struct got_error *
4846 got_worktree_revert(struct got_worktree *worktree,
4847 struct got_pathlist_head *paths,
4848 got_worktree_checkout_cb progress_cb, void *progress_arg,
4849 got_worktree_patch_cb patch_cb, void *patch_arg,
4850 struct got_repository *repo)
4852 struct got_fileindex *fileindex = NULL;
4853 char *fileindex_path = NULL;
4854 const struct got_error *err = NULL, *unlockerr = NULL;
4855 const struct got_error *sync_err = NULL;
4856 struct got_pathlist_entry *pe;
4857 struct revert_file_args rfa;
4859 err = lock_worktree(worktree, LOCK_EX);
4863 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4867 rfa.worktree = worktree;
4868 rfa.fileindex = fileindex;
4869 rfa.progress_cb = progress_cb;
4870 rfa.progress_arg = progress_arg;
4871 rfa.patch_cb = patch_cb;
4872 rfa.patch_arg = patch_arg;
4874 rfa.unlink_added_files = 0;
4875 TAILQ_FOREACH(pe, paths, entry) {
4876 err = worktree_status(worktree, pe->path, fileindex, repo,
4877 revert_file, &rfa, NULL, NULL, 0, 0);
4881 sync_err = sync_fileindex(fileindex, fileindex_path);
4882 if (sync_err && err == NULL)
4885 free(fileindex_path);
4887 got_fileindex_free(fileindex);
4888 unlockerr = lock_worktree(worktree, LOCK_SH);
4889 if (unlockerr && err == NULL)
4895 free_commitable(struct got_commitable *ct)
4898 free(ct->in_repo_path);
4899 free(ct->ondisk_path);
4901 free(ct->base_blob_id);
4902 free(ct->staged_blob_id);
4903 free(ct->base_commit_id);
4907 struct collect_commitables_arg {
4908 struct got_pathlist_head *commitable_paths;
4909 struct got_repository *repo;
4910 struct got_worktree *worktree;
4911 struct got_fileindex *fileindex;
4912 int have_staged_files;
4913 int allow_bad_symlinks;
4916 static const struct got_error *
4917 collect_commitables(void *arg, unsigned char status,
4918 unsigned char staged_status, const char *relpath,
4919 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4920 struct got_object_id *commit_id, int dirfd, const char *de_name)
4922 struct collect_commitables_arg *a = arg;
4923 const struct got_error *err = NULL;
4924 struct got_commitable *ct = NULL;
4925 struct got_pathlist_entry *new = NULL;
4926 char *parent_path = NULL, *path = NULL;
4929 if (a->have_staged_files) {
4930 if (staged_status != GOT_STATUS_MODIFY &&
4931 staged_status != GOT_STATUS_ADD &&
4932 staged_status != GOT_STATUS_DELETE)
4935 if (status == GOT_STATUS_CONFLICT)
4936 return got_error(GOT_ERR_COMMIT_CONFLICT);
4938 if (status != GOT_STATUS_MODIFY &&
4939 status != GOT_STATUS_MODE_CHANGE &&
4940 status != GOT_STATUS_ADD &&
4941 status != GOT_STATUS_DELETE)
4945 if (asprintf(&path, "/%s", relpath) == -1) {
4946 err = got_error_from_errno("asprintf");
4949 if (strcmp(path, "/") == 0) {
4950 parent_path = strdup("");
4951 if (parent_path == NULL)
4952 return got_error_from_errno("strdup");
4954 err = got_path_dirname(&parent_path, path);
4959 ct = calloc(1, sizeof(*ct));
4961 err = got_error_from_errno("calloc");
4965 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4967 err = got_error_from_errno("asprintf");
4971 if (staged_status == GOT_STATUS_ADD ||
4972 staged_status == GOT_STATUS_MODIFY) {
4973 struct got_fileindex_entry *ie;
4974 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4975 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4976 case GOT_FILEIDX_MODE_REGULAR_FILE:
4977 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4980 case GOT_FILEIDX_MODE_SYMLINK:
4984 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4987 ct->mode |= got_fileindex_entry_perms_get(ie);
4988 } else if (status != GOT_STATUS_DELETE &&
4989 staged_status != GOT_STATUS_DELETE) {
4991 if (fstatat(dirfd, de_name, &sb,
4992 AT_SYMLINK_NOFOLLOW) == -1) {
4993 err = got_error_from_errno2("fstatat",
4997 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4998 err = got_error_from_errno2("lstat", ct->ondisk_path);
5001 ct->mode = sb.st_mode;
5004 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5005 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5007 err = got_error_from_errno("asprintf");
5011 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5012 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5014 char target_path[PATH_MAX];
5016 target_len = readlink(ct->ondisk_path, target_path,
5017 sizeof(target_path));
5018 if (target_len == -1) {
5019 err = got_error_from_errno2("readlink",
5023 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5024 target_len, ct->ondisk_path, a->worktree->root_path);
5027 if (is_bad_symlink) {
5028 err = got_error_path(ct->ondisk_path,
5029 GOT_ERR_BAD_SYMLINK);
5035 ct->status = status;
5036 ct->staged_status = staged_status;
5037 ct->blob_id = NULL; /* will be filled in when blob gets created */
5038 if (ct->status != GOT_STATUS_ADD &&
5039 ct->staged_status != GOT_STATUS_ADD) {
5040 ct->base_blob_id = got_object_id_dup(blob_id);
5041 if (ct->base_blob_id == NULL) {
5042 err = got_error_from_errno("got_object_id_dup");
5045 ct->base_commit_id = got_object_id_dup(commit_id);
5046 if (ct->base_commit_id == NULL) {
5047 err = got_error_from_errno("got_object_id_dup");
5051 if (ct->staged_status == GOT_STATUS_ADD ||
5052 ct->staged_status == GOT_STATUS_MODIFY) {
5053 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5054 if (ct->staged_blob_id == NULL) {
5055 err = got_error_from_errno("got_object_id_dup");
5059 ct->path = strdup(path);
5060 if (ct->path == NULL) {
5061 err = got_error_from_errno("strdup");
5064 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5066 if (ct && (err || new == NULL))
5067 free_commitable(ct);
5073 static const struct got_error *write_tree(struct got_object_id **, int *,
5074 struct got_tree_object *, const char *, struct got_pathlist_head *,
5075 got_worktree_status_cb status_cb, void *status_arg,
5076 struct got_repository *);
5078 static const struct got_error *
5079 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5080 struct got_tree_entry *te, const char *parent_path,
5081 struct got_pathlist_head *commitable_paths,
5082 got_worktree_status_cb status_cb, void *status_arg,
5083 struct got_repository *repo)
5085 const struct got_error *err = NULL;
5086 struct got_tree_object *subtree;
5089 if (asprintf(&subpath, "%s%s%s", parent_path,
5090 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5091 return got_error_from_errno("asprintf");
5093 err = got_object_open_as_tree(&subtree, repo, &te->id);
5097 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5098 commitable_paths, status_cb, status_arg, repo);
5099 got_object_tree_close(subtree);
5104 static const struct got_error *
5105 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5107 const struct got_error *err = NULL;
5108 char *ct_parent_path = NULL;
5112 if (strchr(ct->in_repo_path, '/') == NULL) {
5113 *match = got_path_is_root_dir(path);
5117 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5120 *match = (strcmp(path, ct_parent_path) == 0);
5121 free(ct_parent_path);
5126 get_ct_file_mode(struct got_commitable *ct)
5128 if (S_ISLNK(ct->mode))
5131 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5134 static const struct got_error *
5135 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5136 struct got_tree_entry *te, struct got_commitable *ct)
5138 const struct got_error *err = NULL;
5142 err = got_object_tree_entry_dup(new_te, te);
5146 (*new_te)->mode = get_ct_file_mode(ct);
5148 if (ct->staged_status == GOT_STATUS_MODIFY)
5149 memcpy(&(*new_te)->id, ct->staged_blob_id,
5150 sizeof((*new_te)->id));
5152 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5154 if (err && *new_te) {
5161 static const struct got_error *
5162 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5163 struct got_commitable *ct)
5165 const struct got_error *err = NULL;
5166 char *ct_name = NULL;
5170 *new_te = calloc(1, sizeof(**new_te));
5171 if (*new_te == NULL)
5172 return got_error_from_errno("calloc");
5174 err = got_path_basename(&ct_name, ct->path);
5177 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5178 sizeof((*new_te)->name)) {
5179 err = got_error(GOT_ERR_NO_SPACE);
5183 (*new_te)->mode = get_ct_file_mode(ct);
5185 if (ct->staged_status == GOT_STATUS_ADD)
5186 memcpy(&(*new_te)->id, ct->staged_blob_id,
5187 sizeof((*new_te)->id));
5189 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5192 if (err && *new_te) {
5199 static const struct got_error *
5200 insert_tree_entry(struct got_tree_entry *new_te,
5201 struct got_pathlist_head *paths)
5203 const struct got_error *err = NULL;
5204 struct got_pathlist_entry *new_pe;
5206 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5210 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5214 static const struct got_error *
5215 report_ct_status(struct got_commitable *ct,
5216 got_worktree_status_cb status_cb, void *status_arg)
5218 const char *ct_path = ct->path;
5219 unsigned char status;
5221 while (ct_path[0] == '/')
5224 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5225 status = ct->staged_status;
5227 status = ct->status;
5229 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5230 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5233 static const struct got_error *
5234 match_modified_subtree(int *modified, struct got_tree_entry *te,
5235 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5237 const struct got_error *err = NULL;
5238 struct got_pathlist_entry *pe;
5243 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5244 got_path_is_root_dir(base_tree_path) ? "" : "/",
5246 return got_error_from_errno("asprintf");
5248 TAILQ_FOREACH(pe, commitable_paths, entry) {
5249 struct got_commitable *ct = pe->data;
5250 *modified = got_path_is_child(ct->in_repo_path, te_path,
5260 static const struct got_error *
5261 match_deleted_or_modified_ct(struct got_commitable **ctp,
5262 struct got_tree_entry *te, const char *base_tree_path,
5263 struct got_pathlist_head *commitable_paths)
5265 const struct got_error *err = NULL;
5266 struct got_pathlist_entry *pe;
5270 TAILQ_FOREACH(pe, commitable_paths, entry) {
5271 struct got_commitable *ct = pe->data;
5272 char *ct_name = NULL;
5275 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5276 if (ct->status != GOT_STATUS_MODIFY &&
5277 ct->status != GOT_STATUS_MODE_CHANGE &&
5278 ct->status != GOT_STATUS_DELETE)
5281 if (ct->staged_status != GOT_STATUS_MODIFY &&
5282 ct->staged_status != GOT_STATUS_DELETE)
5286 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5289 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5295 err = got_path_basename(&ct_name, pe->path);
5299 if (strcmp(te->name, ct_name) != 0) {
5312 static const struct got_error *
5313 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5314 const char *child_path, const char *path_base_tree,
5315 struct got_pathlist_head *commitable_paths,
5316 got_worktree_status_cb status_cb, void *status_arg,
5317 struct got_repository *repo)
5319 const struct got_error *err = NULL;
5320 struct got_tree_entry *new_te;
5322 struct got_object_id *id = NULL;
5327 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5328 got_path_is_root_dir(path_base_tree) ? "" : "/",
5330 return got_error_from_errno("asprintf");
5332 new_te = calloc(1, sizeof(*new_te));
5334 return got_error_from_errno("calloc");
5335 new_te->mode = S_IFDIR;
5337 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5338 sizeof(new_te->name)) {
5339 err = got_error(GOT_ERR_NO_SPACE);
5342 err = write_tree(&id, &nentries, NULL, subtree_path,
5343 commitable_paths, status_cb, status_arg, repo);
5348 memcpy(&new_te->id, id, sizeof(new_te->id));
5357 static const struct got_error *
5358 write_tree(struct got_object_id **new_tree_id, int *nentries,
5359 struct got_tree_object *base_tree, const char *path_base_tree,
5360 struct got_pathlist_head *commitable_paths,
5361 got_worktree_status_cb status_cb, void *status_arg,
5362 struct got_repository *repo)
5364 const struct got_error *err = NULL;
5365 struct got_pathlist_head paths;
5366 struct got_tree_entry *te, *new_te = NULL;
5367 struct got_pathlist_entry *pe;
5372 /* Insert, and recurse into, newly added entries first. */
5373 TAILQ_FOREACH(pe, commitable_paths, entry) {
5374 struct got_commitable *ct = pe->data;
5375 char *child_path = NULL, *slash;
5377 if ((ct->status != GOT_STATUS_ADD &&
5378 ct->staged_status != GOT_STATUS_ADD) ||
5379 (ct->flags & GOT_COMMITABLE_ADDED))
5382 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5383 strlen(path_base_tree)))
5386 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5391 slash = strchr(child_path, '/');
5392 if (slash == NULL) {
5393 err = alloc_added_blob_tree_entry(&new_te, ct);
5396 err = report_ct_status(ct, status_cb, status_arg);
5399 ct->flags |= GOT_COMMITABLE_ADDED;
5400 err = insert_tree_entry(new_te, &paths);
5405 *slash = '\0'; /* trim trailing path components */
5406 if (base_tree == NULL ||
5407 got_object_tree_find_entry(base_tree, child_path)
5409 err = make_subtree_for_added_blob(&new_te,
5410 child_path, path_base_tree,
5411 commitable_paths, status_cb, status_arg,
5415 err = insert_tree_entry(new_te, &paths);
5424 int i, nbase_entries;
5425 /* Handle modified and deleted entries. */
5426 nbase_entries = got_object_tree_get_nentries(base_tree);
5427 for (i = 0; i < nbase_entries; i++) {
5428 struct got_commitable *ct = NULL;
5430 te = got_object_tree_get_entry(base_tree, i);
5431 if (got_object_tree_entry_is_submodule(te)) {
5432 /* Entry is a submodule; just copy it. */
5433 err = got_object_tree_entry_dup(&new_te, te);
5436 err = insert_tree_entry(new_te, &paths);
5443 if (S_ISDIR(te->mode)) {
5445 err = got_object_tree_entry_dup(&new_te, te);
5448 err = match_modified_subtree(&modified, te,
5449 path_base_tree, commitable_paths);
5452 /* Avoid recursion into unmodified subtrees. */
5454 struct got_object_id *new_id;
5456 err = write_subtree(&new_id,
5458 path_base_tree, commitable_paths,
5459 status_cb, status_arg, repo);
5462 if (nsubentries == 0) {
5463 /* All entries were deleted. */
5467 memcpy(&new_te->id, new_id,
5468 sizeof(new_te->id));
5471 err = insert_tree_entry(new_te, &paths);
5478 err = match_deleted_or_modified_ct(&ct, te,
5479 path_base_tree, commitable_paths);
5483 /* NB: Deleted entries get dropped here. */
5484 if (ct->status == GOT_STATUS_MODIFY ||
5485 ct->status == GOT_STATUS_MODE_CHANGE ||
5486 ct->staged_status == GOT_STATUS_MODIFY) {
5487 err = alloc_modified_blob_tree_entry(
5491 err = insert_tree_entry(new_te, &paths);
5496 err = report_ct_status(ct, status_cb,
5501 /* Entry is unchanged; just copy it. */
5502 err = got_object_tree_entry_dup(&new_te, te);
5505 err = insert_tree_entry(new_te, &paths);
5513 /* Write new list of entries; deleted entries have been dropped. */
5514 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5516 got_pathlist_free(&paths);
5520 static const struct got_error *
5521 update_fileindex_after_commit(struct got_worktree *worktree,
5522 struct got_pathlist_head *commitable_paths,
5523 struct got_object_id *new_base_commit_id,
5524 struct got_fileindex *fileindex, int have_staged_files)
5526 const struct got_error *err = NULL;
5527 struct got_pathlist_entry *pe;
5528 char *relpath = NULL;
5530 TAILQ_FOREACH(pe, commitable_paths, entry) {
5531 struct got_fileindex_entry *ie;
5532 struct got_commitable *ct = pe->data;
5534 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5536 err = got_path_skip_common_ancestor(&relpath,
5537 worktree->root_path, ct->ondisk_path);
5542 if (ct->status == GOT_STATUS_DELETE ||
5543 ct->staged_status == GOT_STATUS_DELETE) {
5544 got_fileindex_entry_remove(fileindex, ie);
5545 } else if (ct->staged_status == GOT_STATUS_ADD ||
5546 ct->staged_status == GOT_STATUS_MODIFY) {
5547 got_fileindex_entry_stage_set(ie,
5548 GOT_FILEIDX_STAGE_NONE);
5549 got_fileindex_entry_staged_filetype_set(ie, 0);
5551 err = got_fileindex_entry_update(ie,
5552 worktree->root_fd, relpath,
5553 ct->staged_blob_id->sha1,
5554 new_base_commit_id->sha1,
5555 !have_staged_files);
5557 err = got_fileindex_entry_update(ie,
5558 worktree->root_fd, relpath,
5560 new_base_commit_id->sha1,
5561 !have_staged_files);
5563 err = got_fileindex_entry_alloc(&ie, pe->path);
5566 err = got_fileindex_entry_update(ie,
5567 worktree->root_fd, relpath, ct->blob_id->sha1,
5568 new_base_commit_id->sha1, 1);
5570 got_fileindex_entry_free(ie);
5573 err = got_fileindex_entry_add(fileindex, ie);
5575 got_fileindex_entry_free(ie);
5588 static const struct got_error *
5589 check_out_of_date(const char *in_repo_path, unsigned char status,
5590 unsigned char staged_status, struct got_object_id *base_blob_id,
5591 struct got_object_id *base_commit_id,
5592 struct got_object_id *head_commit_id, struct got_repository *repo,
5595 const struct got_error *err = NULL;
5596 struct got_object_id *id = NULL;
5598 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5599 /* Trivial case: base commit == head commit */
5600 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5603 * Ensure file content which local changes were based
5604 * on matches file content in the branch head.
5606 err = got_object_id_by_path(&id, repo, head_commit_id,
5609 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5610 err = got_error(ood_errcode);
5612 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5613 err = got_error(ood_errcode);
5615 /* Require that added files don't exist in the branch head. */
5616 err = got_object_id_by_path(&id, repo, head_commit_id,
5618 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5620 err = id ? got_error(ood_errcode) : NULL;
5627 const struct got_error *
5628 commit_worktree(struct got_object_id **new_commit_id,
5629 struct got_pathlist_head *commitable_paths,
5630 struct got_object_id *head_commit_id,
5631 struct got_object_id *parent_id2,
5632 struct got_worktree *worktree,
5633 const char *author, const char *committer,
5634 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5635 got_worktree_status_cb status_cb, void *status_arg,
5636 struct got_repository *repo)
5638 const struct got_error *err = NULL, *unlockerr = NULL;
5639 struct got_pathlist_entry *pe;
5640 const char *head_ref_name = NULL;
5641 struct got_commit_object *head_commit = NULL;
5642 struct got_reference *head_ref2 = NULL;
5643 struct got_object_id *head_commit_id2 = NULL;
5644 struct got_tree_object *head_tree = NULL;
5645 struct got_object_id *new_tree_id = NULL;
5646 int nentries, nparents = 0;
5647 struct got_object_id_queue parent_ids;
5648 struct got_object_qid *pid = NULL;
5649 char *logmsg = NULL;
5651 *new_commit_id = NULL;
5653 STAILQ_INIT(&parent_ids);
5655 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5659 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5663 if (commit_msg_cb != NULL) {
5664 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5669 if (logmsg == NULL || strlen(logmsg) == 0) {
5670 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5674 /* Create blobs from added and modified files and record their IDs. */
5675 TAILQ_FOREACH(pe, commitable_paths, entry) {
5676 struct got_commitable *ct = pe->data;
5679 /* Blobs for staged files already exist. */
5680 if (ct->staged_status == GOT_STATUS_ADD ||
5681 ct->staged_status == GOT_STATUS_MODIFY)
5684 if (ct->status != GOT_STATUS_ADD &&
5685 ct->status != GOT_STATUS_MODIFY &&
5686 ct->status != GOT_STATUS_MODE_CHANGE)
5689 if (asprintf(&ondisk_path, "%s/%s",
5690 worktree->root_path, pe->path) == -1) {
5691 err = got_error_from_errno("asprintf");
5694 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5700 /* Recursively write new tree objects. */
5701 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5702 commitable_paths, status_cb, status_arg, repo);
5706 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5709 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5712 err = got_object_qid_alloc(&pid, parent_id2);
5715 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5718 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5719 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5725 /* Check if a concurrent commit to our branch has occurred. */
5726 head_ref_name = got_worktree_get_head_ref_name(worktree);
5727 if (head_ref_name == NULL) {
5728 err = got_error_from_errno("got_worktree_get_head_ref_name");
5731 /* Lock the reference here to prevent concurrent modification. */
5732 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5735 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5738 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5739 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5742 /* Update branch head in repository. */
5743 err = got_ref_change_ref(head_ref2, *new_commit_id);
5746 err = got_ref_write(head_ref2, repo);
5750 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5754 err = ref_base_commit(worktree, repo);
5758 got_object_id_queue_free(&parent_ids);
5760 got_object_tree_close(head_tree);
5762 got_object_commit_close(head_commit);
5763 free(head_commit_id2);
5765 unlockerr = got_ref_unlock(head_ref2);
5766 if (unlockerr && err == NULL)
5768 got_ref_close(head_ref2);
5773 static const struct got_error *
5774 check_path_is_commitable(const char *path,
5775 struct got_pathlist_head *commitable_paths)
5777 struct got_pathlist_entry *cpe = NULL;
5778 size_t path_len = strlen(path);
5780 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5781 struct got_commitable *ct = cpe->data;
5782 const char *ct_path = ct->path;
5784 while (ct_path[0] == '/')
5787 if (strcmp(path, ct_path) == 0 ||
5788 got_path_is_child(ct_path, path, path_len))
5793 return got_error_path(path, GOT_ERR_BAD_PATH);
5798 static const struct got_error *
5799 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5801 int *have_staged_files = arg;
5803 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5804 *have_staged_files = 1;
5805 return got_error(GOT_ERR_CANCELLED);
5811 static const struct got_error *
5812 check_non_staged_files(struct got_fileindex *fileindex,
5813 struct got_pathlist_head *paths)
5815 struct got_pathlist_entry *pe;
5816 struct got_fileindex_entry *ie;
5818 TAILQ_FOREACH(pe, paths, entry) {
5819 if (pe->path[0] == '\0')
5821 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5823 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5824 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5825 return got_error_path(pe->path,
5826 GOT_ERR_FILE_NOT_STAGED);
5832 const struct got_error *
5833 got_worktree_commit(struct got_object_id **new_commit_id,
5834 struct got_worktree *worktree, struct got_pathlist_head *paths,
5835 const char *author, const char *committer, int allow_bad_symlinks,
5836 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5837 got_worktree_status_cb status_cb, void *status_arg,
5838 struct got_repository *repo)
5840 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5841 struct got_fileindex *fileindex = NULL;
5842 char *fileindex_path = NULL;
5843 struct got_pathlist_head commitable_paths;
5844 struct collect_commitables_arg cc_arg;
5845 struct got_pathlist_entry *pe;
5846 struct got_reference *head_ref = NULL;
5847 struct got_object_id *head_commit_id = NULL;
5848 int have_staged_files = 0;
5850 *new_commit_id = NULL;
5852 TAILQ_INIT(&commitable_paths);
5854 err = lock_worktree(worktree, LOCK_EX);
5858 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5862 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5866 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5870 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5871 &have_staged_files);
5872 if (err && err->code != GOT_ERR_CANCELLED)
5874 if (have_staged_files) {
5875 err = check_non_staged_files(fileindex, paths);
5880 cc_arg.commitable_paths = &commitable_paths;
5881 cc_arg.worktree = worktree;
5882 cc_arg.fileindex = fileindex;
5884 cc_arg.have_staged_files = have_staged_files;
5885 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5886 TAILQ_FOREACH(pe, paths, entry) {
5887 err = worktree_status(worktree, pe->path, fileindex, repo,
5888 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5893 if (TAILQ_EMPTY(&commitable_paths)) {
5894 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5898 TAILQ_FOREACH(pe, paths, entry) {
5899 err = check_path_is_commitable(pe->path, &commitable_paths);
5904 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5905 struct got_commitable *ct = pe->data;
5906 const char *ct_path = ct->in_repo_path;
5908 while (ct_path[0] == '/')
5910 err = check_out_of_date(ct_path, ct->status,
5911 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5912 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5918 err = commit_worktree(new_commit_id, &commitable_paths,
5919 head_commit_id, NULL, worktree, author, committer,
5920 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5924 err = update_fileindex_after_commit(worktree, &commitable_paths,
5925 *new_commit_id, fileindex, have_staged_files);
5926 sync_err = sync_fileindex(fileindex, fileindex_path);
5927 if (sync_err && err == NULL)
5931 got_fileindex_free(fileindex);
5932 free(fileindex_path);
5933 unlockerr = lock_worktree(worktree, LOCK_SH);
5934 if (unlockerr && err == NULL)
5936 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5937 struct got_commitable *ct = pe->data;
5938 free_commitable(ct);
5940 got_pathlist_free(&commitable_paths);
5945 got_commitable_get_path(struct got_commitable *ct)
5951 got_commitable_get_status(struct got_commitable *ct)
5956 struct check_rebase_ok_arg {
5957 struct got_worktree *worktree;
5958 struct got_repository *repo;
5961 static const struct got_error *
5962 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5964 const struct got_error *err = NULL;
5965 struct check_rebase_ok_arg *a = arg;
5966 unsigned char status;
5970 /* Reject rebase of a work tree with mixed base commits. */
5971 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5972 SHA1_DIGEST_LENGTH))
5973 return got_error(GOT_ERR_MIXED_COMMITS);
5975 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5977 return got_error_from_errno("asprintf");
5979 /* Reject rebase of a work tree with modified or staged files. */
5980 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5985 if (status != GOT_STATUS_NO_CHANGE)
5986 return got_error(GOT_ERR_MODIFIED);
5987 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5988 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5993 const struct got_error *
5994 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5995 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5996 struct got_worktree *worktree, struct got_reference *branch,
5997 struct got_repository *repo)
5999 const struct got_error *err = NULL;
6000 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6001 char *branch_ref_name = NULL;
6002 char *fileindex_path = NULL;
6003 struct check_rebase_ok_arg ok_arg;
6004 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6005 struct got_object_id *wt_branch_tip = NULL;
6007 *new_base_branch_ref = NULL;
6011 err = lock_worktree(worktree, LOCK_EX);
6015 err = open_fileindex(fileindex, &fileindex_path, worktree);
6019 ok_arg.worktree = worktree;
6021 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6026 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6030 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6034 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6038 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6043 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6046 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6047 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6051 err = got_ref_alloc_symref(new_base_branch_ref,
6052 new_base_branch_ref_name, wt_branch);
6055 err = got_ref_write(*new_base_branch_ref, repo);
6059 /* TODO Lock original branch's ref while rebasing? */
6061 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6065 err = got_ref_write(branch_ref, repo);
6069 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6070 worktree->base_commit_id);
6073 err = got_ref_write(*tmp_branch, repo);
6077 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6081 free(fileindex_path);
6082 free(tmp_branch_name);
6083 free(new_base_branch_ref_name);
6084 free(branch_ref_name);
6086 got_ref_close(branch_ref);
6088 got_ref_close(wt_branch);
6089 free(wt_branch_tip);
6091 if (*new_base_branch_ref) {
6092 got_ref_close(*new_base_branch_ref);
6093 *new_base_branch_ref = NULL;
6096 got_ref_close(*tmp_branch);
6100 got_fileindex_free(*fileindex);
6103 lock_worktree(worktree, LOCK_SH);
6108 const struct got_error *
6109 got_worktree_rebase_continue(struct got_object_id **commit_id,
6110 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6111 struct got_reference **branch, struct got_fileindex **fileindex,
6112 struct got_worktree *worktree, struct got_repository *repo)
6114 const struct got_error *err;
6115 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6116 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6117 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6118 char *fileindex_path = NULL;
6119 int have_staged_files = 0;
6122 *new_base_branch = NULL;
6127 err = lock_worktree(worktree, LOCK_EX);
6131 err = open_fileindex(fileindex, &fileindex_path, worktree);
6135 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6136 &have_staged_files);
6137 if (err && err->code != GOT_ERR_CANCELLED)
6139 if (have_staged_files) {
6140 err = got_error(GOT_ERR_STAGED_PATHS);
6144 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6148 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6152 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6156 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6160 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6164 err = got_ref_open(branch, repo,
6165 got_ref_get_symref_target(branch_ref), 0);
6169 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6173 err = got_ref_resolve(commit_id, repo, commit_ref);
6177 err = got_ref_open(new_base_branch, repo,
6178 new_base_branch_ref_name, 0);
6182 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6186 free(commit_ref_name);
6187 free(branch_ref_name);
6188 free(fileindex_path);
6190 got_ref_close(commit_ref);
6192 got_ref_close(branch_ref);
6197 got_ref_close(*tmp_branch);
6200 if (*new_base_branch) {
6201 got_ref_close(*new_base_branch);
6202 *new_base_branch = NULL;
6205 got_ref_close(*branch);
6209 got_fileindex_free(*fileindex);
6212 lock_worktree(worktree, LOCK_SH);
6217 const struct got_error *
6218 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6220 const struct got_error *err;
6221 char *tmp_branch_name = NULL;
6223 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6227 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6228 free(tmp_branch_name);
6232 static const struct got_error *
6233 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6234 char **logmsg, void *arg)
6240 static const struct got_error *
6241 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6242 const char *path, struct got_object_id *blob_id,
6243 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6244 int dirfd, const char *de_name)
6249 struct collect_merged_paths_arg {
6250 got_worktree_checkout_cb progress_cb;
6252 struct got_pathlist_head *merged_paths;
6255 static const struct got_error *
6256 collect_merged_paths(void *arg, unsigned char status, const char *path)
6258 const struct got_error *err;
6259 struct collect_merged_paths_arg *a = arg;
6261 struct got_pathlist_entry *new;
6263 err = (*a->progress_cb)(a->progress_arg, status, path);
6267 if (status != GOT_STATUS_MERGE &&
6268 status != GOT_STATUS_ADD &&
6269 status != GOT_STATUS_DELETE &&
6270 status != GOT_STATUS_CONFLICT)
6275 return got_error_from_errno("strdup");
6277 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6278 if (err || new == NULL)
6284 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6286 struct got_pathlist_entry *pe;
6288 TAILQ_FOREACH(pe, merged_paths, entry)
6289 free((char *)pe->path);
6291 got_pathlist_free(merged_paths);
6294 static const struct got_error *
6295 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6296 int is_rebase, struct got_repository *repo)
6298 const struct got_error *err;
6299 struct got_reference *commit_ref = NULL;
6301 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6303 if (err->code != GOT_ERR_NOT_REF)
6305 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6308 err = got_ref_write(commit_ref, repo);
6311 } else if (is_rebase) {
6312 struct got_object_id *stored_id;
6315 err = got_ref_resolve(&stored_id, repo, commit_ref);
6318 cmp = got_object_id_cmp(commit_id, stored_id);
6321 err = got_error(GOT_ERR_REBASE_COMMITID);
6327 got_ref_close(commit_ref);
6331 static const struct got_error *
6332 rebase_merge_files(struct got_pathlist_head *merged_paths,
6333 const char *commit_ref_name, struct got_worktree *worktree,
6334 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6335 struct got_object_id *commit_id, struct got_repository *repo,
6336 got_worktree_checkout_cb progress_cb, void *progress_arg,
6337 got_cancel_cb cancel_cb, void *cancel_arg)
6339 const struct got_error *err;
6340 struct got_reference *commit_ref = NULL;
6341 struct collect_merged_paths_arg cmp_arg;
6342 char *fileindex_path;
6344 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6346 err = get_fileindex_path(&fileindex_path, worktree);
6350 cmp_arg.progress_cb = progress_cb;
6351 cmp_arg.progress_arg = progress_arg;
6352 cmp_arg.merged_paths = merged_paths;
6353 err = merge_files(worktree, fileindex, fileindex_path,
6354 parent_commit_id, commit_id, repo, collect_merged_paths,
6355 &cmp_arg, cancel_cb, cancel_arg);
6357 got_ref_close(commit_ref);
6361 const struct got_error *
6362 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6363 struct got_worktree *worktree, struct got_fileindex *fileindex,
6364 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6365 struct got_repository *repo,
6366 got_worktree_checkout_cb progress_cb, void *progress_arg,
6367 got_cancel_cb cancel_cb, void *cancel_arg)
6369 const struct got_error *err;
6370 char *commit_ref_name;
6372 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6376 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6380 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6381 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6382 progress_arg, cancel_cb, cancel_arg);
6384 free(commit_ref_name);
6388 const struct got_error *
6389 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6390 struct got_worktree *worktree, struct got_fileindex *fileindex,
6391 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6392 struct got_repository *repo,
6393 got_worktree_checkout_cb progress_cb, void *progress_arg,
6394 got_cancel_cb cancel_cb, void *cancel_arg)
6396 const struct got_error *err;
6397 char *commit_ref_name;
6399 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6403 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6407 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6408 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6409 progress_arg, cancel_cb, cancel_arg);
6411 free(commit_ref_name);
6415 static const struct got_error *
6416 rebase_commit(struct got_object_id **new_commit_id,
6417 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6418 struct got_worktree *worktree, struct got_fileindex *fileindex,
6419 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6420 const char *new_logmsg, struct got_repository *repo)
6422 const struct got_error *err, *sync_err;
6423 struct got_pathlist_head commitable_paths;
6424 struct collect_commitables_arg cc_arg;
6425 char *fileindex_path = NULL;
6426 struct got_reference *head_ref = NULL;
6427 struct got_object_id *head_commit_id = NULL;
6428 char *logmsg = NULL;
6430 TAILQ_INIT(&commitable_paths);
6431 *new_commit_id = NULL;
6433 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6435 err = get_fileindex_path(&fileindex_path, worktree);
6439 cc_arg.commitable_paths = &commitable_paths;
6440 cc_arg.worktree = worktree;
6442 cc_arg.have_staged_files = 0;
6444 * If possible get the status of individual files directly to
6445 * avoid crawling the entire work tree once per rebased commit.
6447 * Ideally, merged_paths would contain a list of commitables
6448 * we could use so we could skip worktree_status() entirely.
6449 * However, we would then need carefully keep track of cumulative
6450 * effects of operations such as file additions and deletions
6451 * in 'got histedit -f' (folding multiple commits into one),
6452 * and this extra complexity is not really worth it.
6455 struct got_pathlist_entry *pe;
6456 TAILQ_FOREACH(pe, merged_paths, entry) {
6457 err = worktree_status(worktree, pe->path, fileindex,
6458 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6464 err = worktree_status(worktree, "", fileindex, repo,
6465 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6470 if (TAILQ_EMPTY(&commitable_paths)) {
6471 /* No-op change; commit will be elided. */
6472 err = got_ref_delete(commit_ref, repo);
6475 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6479 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6483 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6488 logmsg = strdup(new_logmsg);
6489 if (logmsg == NULL) {
6490 err = got_error_from_errno("strdup");
6494 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6499 /* NB: commit_worktree will call free(logmsg) */
6500 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6501 NULL, worktree, got_object_commit_get_author(orig_commit),
6502 got_object_commit_get_committer(orig_commit),
6503 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6507 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6511 err = got_ref_delete(commit_ref, repo);
6515 err = update_fileindex_after_commit(worktree, &commitable_paths,
6516 *new_commit_id, fileindex, 0);
6517 sync_err = sync_fileindex(fileindex, fileindex_path);
6518 if (sync_err && err == NULL)
6521 free(fileindex_path);
6522 free(head_commit_id);
6524 got_ref_close(head_ref);
6526 free(*new_commit_id);
6527 *new_commit_id = NULL;
6532 const struct got_error *
6533 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6534 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6535 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6536 struct got_commit_object *orig_commit,
6537 struct got_object_id *orig_commit_id, struct got_repository *repo)
6539 const struct got_error *err;
6540 char *commit_ref_name;
6541 struct got_reference *commit_ref = NULL;
6542 struct got_object_id *commit_id = NULL;
6544 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6548 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6551 err = got_ref_resolve(&commit_id, repo, commit_ref);
6554 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6555 err = got_error(GOT_ERR_REBASE_COMMITID);
6559 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6560 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6563 got_ref_close(commit_ref);
6564 free(commit_ref_name);
6569 const struct got_error *
6570 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6571 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6572 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6573 struct got_commit_object *orig_commit,
6574 struct got_object_id *orig_commit_id, const char *new_logmsg,
6575 struct got_repository *repo)
6577 const struct got_error *err;
6578 char *commit_ref_name;
6579 struct got_reference *commit_ref = NULL;
6581 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6585 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6589 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6590 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6593 got_ref_close(commit_ref);
6594 free(commit_ref_name);
6598 const struct got_error *
6599 got_worktree_rebase_postpone(struct got_worktree *worktree,
6600 struct got_fileindex *fileindex)
6603 got_fileindex_free(fileindex);
6604 return lock_worktree(worktree, LOCK_SH);
6607 static const struct got_error *
6608 delete_ref(const char *name, struct got_repository *repo)
6610 const struct got_error *err;
6611 struct got_reference *ref;
6613 err = got_ref_open(&ref, repo, name, 0);
6615 if (err->code == GOT_ERR_NOT_REF)
6620 err = got_ref_delete(ref, repo);
6625 static const struct got_error *
6626 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6628 const struct got_error *err;
6629 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6630 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6632 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6635 err = delete_ref(tmp_branch_name, repo);
6639 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6642 err = delete_ref(new_base_branch_ref_name, repo);
6646 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6649 err = delete_ref(branch_ref_name, repo);
6653 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6656 err = delete_ref(commit_ref_name, repo);
6661 free(tmp_branch_name);
6662 free(new_base_branch_ref_name);
6663 free(branch_ref_name);
6664 free(commit_ref_name);
6668 const struct got_error *
6669 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6670 struct got_object_id *new_commit_id, struct got_repository *repo)
6672 const struct got_error *err;
6673 struct got_reference *ref = NULL;
6674 struct got_object_id *old_commit_id = NULL;
6675 const char *branch_name = NULL;
6676 char *new_id_str = NULL;
6677 char *refname = NULL;
6679 branch_name = got_ref_get_name(branch);
6680 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6681 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6684 err = got_object_id_str(&new_id_str, new_commit_id);
6688 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6689 new_id_str) == -1) {
6690 err = got_error_from_errno("asprintf");
6694 err = got_ref_resolve(&old_commit_id, repo, branch);
6698 err = got_ref_alloc(&ref, refname, old_commit_id);
6702 err = got_ref_write(ref, repo);
6706 free(old_commit_id);
6712 const struct got_error *
6713 got_worktree_rebase_complete(struct got_worktree *worktree,
6714 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6715 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6716 struct got_repository *repo, int create_backup)
6718 const struct got_error *err, *unlockerr, *sync_err;
6719 struct got_object_id *new_head_commit_id = NULL;
6720 char *fileindex_path = NULL;
6722 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6726 if (create_backup) {
6727 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6728 rebased_branch, new_head_commit_id, repo);
6733 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6737 err = got_ref_write(rebased_branch, repo);
6741 err = got_worktree_set_head_ref(worktree, rebased_branch);
6745 err = delete_rebase_refs(worktree, repo);
6749 err = get_fileindex_path(&fileindex_path, worktree);
6752 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6753 sync_err = sync_fileindex(fileindex, fileindex_path);
6754 if (sync_err && err == NULL)
6757 got_fileindex_free(fileindex);
6758 free(fileindex_path);
6759 free(new_head_commit_id);
6760 unlockerr = lock_worktree(worktree, LOCK_SH);
6761 if (unlockerr && err == NULL)
6766 const struct got_error *
6767 got_worktree_rebase_abort(struct got_worktree *worktree,
6768 struct got_fileindex *fileindex, struct got_repository *repo,
6769 struct got_reference *new_base_branch,
6770 got_worktree_checkout_cb progress_cb, void *progress_arg)
6772 const struct got_error *err, *unlockerr, *sync_err;
6773 struct got_reference *resolved = NULL;
6774 struct got_object_id *commit_id = NULL;
6775 char *fileindex_path = NULL;
6776 struct revert_file_args rfa;
6777 struct got_object_id *tree_id = NULL;
6779 err = lock_worktree(worktree, LOCK_EX);
6783 err = got_ref_open(&resolved, repo,
6784 got_ref_get_symref_target(new_base_branch), 0);
6788 err = got_worktree_set_head_ref(worktree, resolved);
6793 * XXX commits to the base branch could have happened while
6794 * we were busy rebasing; should we store the original commit ID
6795 * when rebase begins and read it back here?
6797 err = got_ref_resolve(&commit_id, repo, resolved);
6801 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6805 err = got_object_id_by_path(&tree_id, repo,
6806 worktree->base_commit_id, worktree->path_prefix);
6810 err = delete_rebase_refs(worktree, repo);
6814 err = get_fileindex_path(&fileindex_path, worktree);
6818 rfa.worktree = worktree;
6819 rfa.fileindex = fileindex;
6820 rfa.progress_cb = progress_cb;
6821 rfa.progress_arg = progress_arg;
6822 rfa.patch_cb = NULL;
6823 rfa.patch_arg = NULL;
6825 rfa.unlink_added_files = 0;
6826 err = worktree_status(worktree, "", fileindex, repo,
6827 revert_file, &rfa, NULL, NULL, 0, 0);
6831 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6832 repo, progress_cb, progress_arg, NULL, NULL);
6834 sync_err = sync_fileindex(fileindex, fileindex_path);
6835 if (sync_err && err == NULL)
6838 got_ref_close(resolved);
6842 got_fileindex_free(fileindex);
6843 free(fileindex_path);
6845 unlockerr = lock_worktree(worktree, LOCK_SH);
6846 if (unlockerr && err == NULL)
6851 const struct got_error *
6852 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6853 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6854 struct got_fileindex **fileindex, struct got_worktree *worktree,
6855 struct got_repository *repo)
6857 const struct got_error *err = NULL;
6858 char *tmp_branch_name = NULL;
6859 char *branch_ref_name = NULL;
6860 char *base_commit_ref_name = NULL;
6861 char *fileindex_path = NULL;
6862 struct check_rebase_ok_arg ok_arg;
6863 struct got_reference *wt_branch = NULL;
6864 struct got_reference *base_commit_ref = NULL;
6868 *base_commit_id = NULL;
6871 err = lock_worktree(worktree, LOCK_EX);
6875 err = open_fileindex(fileindex, &fileindex_path, worktree);
6879 ok_arg.worktree = worktree;
6881 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6886 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6890 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6894 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6899 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6904 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6908 err = got_ref_write(*branch_ref, repo);
6912 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6913 worktree->base_commit_id);
6916 err = got_ref_write(base_commit_ref, repo);
6919 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6920 if (*base_commit_id == NULL) {
6921 err = got_error_from_errno("got_object_id_dup");
6925 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6926 worktree->base_commit_id);
6929 err = got_ref_write(*tmp_branch, repo);
6933 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6937 free(fileindex_path);
6938 free(tmp_branch_name);
6939 free(branch_ref_name);
6940 free(base_commit_ref_name);
6942 got_ref_close(wt_branch);
6945 got_ref_close(*branch_ref);
6949 got_ref_close(*tmp_branch);
6952 free(*base_commit_id);
6954 got_fileindex_free(*fileindex);
6957 lock_worktree(worktree, LOCK_SH);
6962 const struct got_error *
6963 got_worktree_histedit_postpone(struct got_worktree *worktree,
6964 struct got_fileindex *fileindex)
6967 got_fileindex_free(fileindex);
6968 return lock_worktree(worktree, LOCK_SH);
6971 const struct got_error *
6972 got_worktree_histedit_in_progress(int *in_progress,
6973 struct got_worktree *worktree)
6975 const struct got_error *err;
6976 char *tmp_branch_name = NULL;
6978 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6982 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6983 free(tmp_branch_name);
6987 const struct got_error *
6988 got_worktree_histedit_continue(struct got_object_id **commit_id,
6989 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6990 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6991 struct got_worktree *worktree, struct got_repository *repo)
6993 const struct got_error *err;
6994 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6995 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6996 struct got_reference *commit_ref = NULL;
6997 struct got_reference *base_commit_ref = NULL;
6998 char *fileindex_path = NULL;
6999 int have_staged_files = 0;
7003 *base_commit_id = NULL;
7006 err = lock_worktree(worktree, LOCK_EX);
7010 err = open_fileindex(fileindex, &fileindex_path, worktree);
7014 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7015 &have_staged_files);
7016 if (err && err->code != GOT_ERR_CANCELLED)
7018 if (have_staged_files) {
7019 err = got_error(GOT_ERR_STAGED_PATHS);
7023 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7027 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7031 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7035 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7040 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7044 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7047 err = got_ref_resolve(commit_id, repo, commit_ref);
7051 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7054 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7058 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7062 free(commit_ref_name);
7063 free(branch_ref_name);
7064 free(fileindex_path);
7066 got_ref_close(commit_ref);
7067 if (base_commit_ref)
7068 got_ref_close(base_commit_ref);
7072 free(*base_commit_id);
7073 *base_commit_id = NULL;
7075 got_ref_close(*tmp_branch);
7079 got_fileindex_free(*fileindex);
7082 lock_worktree(worktree, LOCK_EX);
7087 static const struct got_error *
7088 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7090 const struct got_error *err;
7091 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7092 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7094 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7097 err = delete_ref(tmp_branch_name, repo);
7101 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7105 err = delete_ref(base_commit_ref_name, repo);
7109 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7112 err = delete_ref(branch_ref_name, repo);
7116 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7119 err = delete_ref(commit_ref_name, repo);
7123 free(tmp_branch_name);
7124 free(base_commit_ref_name);
7125 free(branch_ref_name);
7126 free(commit_ref_name);
7130 const struct got_error *
7131 got_worktree_histedit_abort(struct got_worktree *worktree,
7132 struct got_fileindex *fileindex, struct got_repository *repo,
7133 struct got_reference *branch, struct got_object_id *base_commit_id,
7134 got_worktree_checkout_cb progress_cb, void *progress_arg)
7136 const struct got_error *err, *unlockerr, *sync_err;
7137 struct got_reference *resolved = NULL;
7138 char *fileindex_path = NULL;
7139 struct got_object_id *tree_id = NULL;
7140 struct revert_file_args rfa;
7142 err = lock_worktree(worktree, LOCK_EX);
7146 err = got_ref_open(&resolved, repo,
7147 got_ref_get_symref_target(branch), 0);
7151 err = got_worktree_set_head_ref(worktree, resolved);
7155 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7159 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7160 worktree->path_prefix);
7164 err = delete_histedit_refs(worktree, repo);
7168 err = get_fileindex_path(&fileindex_path, worktree);
7172 rfa.worktree = worktree;
7173 rfa.fileindex = fileindex;
7174 rfa.progress_cb = progress_cb;
7175 rfa.progress_arg = progress_arg;
7176 rfa.patch_cb = NULL;
7177 rfa.patch_arg = NULL;
7179 rfa.unlink_added_files = 0;
7180 err = worktree_status(worktree, "", fileindex, repo,
7181 revert_file, &rfa, NULL, NULL, 0, 0);
7185 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7186 repo, progress_cb, progress_arg, NULL, NULL);
7188 sync_err = sync_fileindex(fileindex, fileindex_path);
7189 if (sync_err && err == NULL)
7192 got_ref_close(resolved);
7194 free(fileindex_path);
7196 unlockerr = lock_worktree(worktree, LOCK_SH);
7197 if (unlockerr && err == NULL)
7202 const struct got_error *
7203 got_worktree_histedit_complete(struct got_worktree *worktree,
7204 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7205 struct got_reference *edited_branch, struct got_repository *repo)
7207 const struct got_error *err, *unlockerr, *sync_err;
7208 struct got_object_id *new_head_commit_id = NULL;
7209 struct got_reference *resolved = NULL;
7210 char *fileindex_path = NULL;
7212 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7216 err = got_ref_open(&resolved, repo,
7217 got_ref_get_symref_target(edited_branch), 0);
7221 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7222 resolved, new_head_commit_id, repo);
7226 err = got_ref_change_ref(resolved, new_head_commit_id);
7230 err = got_ref_write(resolved, repo);
7234 err = got_worktree_set_head_ref(worktree, resolved);
7238 err = delete_histedit_refs(worktree, repo);
7242 err = get_fileindex_path(&fileindex_path, worktree);
7245 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7246 sync_err = sync_fileindex(fileindex, fileindex_path);
7247 if (sync_err && err == NULL)
7250 got_fileindex_free(fileindex);
7251 free(fileindex_path);
7252 free(new_head_commit_id);
7253 unlockerr = lock_worktree(worktree, LOCK_SH);
7254 if (unlockerr && err == NULL)
7259 const struct got_error *
7260 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7261 struct got_object_id *commit_id, struct got_repository *repo)
7263 const struct got_error *err;
7264 char *commit_ref_name;
7266 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7270 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7274 err = delete_ref(commit_ref_name, repo);
7276 free(commit_ref_name);
7280 const struct got_error *
7281 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7282 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7283 struct got_worktree *worktree, const char *refname,
7284 struct got_repository *repo)
7286 const struct got_error *err = NULL;
7287 char *fileindex_path = NULL;
7288 struct check_rebase_ok_arg ok_arg;
7292 *base_branch_ref = NULL;
7294 err = lock_worktree(worktree, LOCK_EX);
7298 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7299 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7300 "cannot integrate a branch into itself; "
7301 "update -b or different branch name required");
7305 err = open_fileindex(fileindex, &fileindex_path, worktree);
7309 /* Preconditions are the same as for rebase. */
7310 ok_arg.worktree = worktree;
7312 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7317 err = got_ref_open(branch_ref, repo, refname, 1);
7321 err = got_ref_open(base_branch_ref, repo,
7322 got_worktree_get_head_ref_name(worktree), 1);
7326 got_ref_close(*branch_ref);
7329 if (*base_branch_ref) {
7330 got_ref_close(*base_branch_ref);
7331 *base_branch_ref = NULL;
7334 got_fileindex_free(*fileindex);
7337 lock_worktree(worktree, LOCK_SH);
7342 const struct got_error *
7343 got_worktree_integrate_continue(struct got_worktree *worktree,
7344 struct got_fileindex *fileindex, struct got_repository *repo,
7345 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7346 got_worktree_checkout_cb progress_cb, void *progress_arg,
7347 got_cancel_cb cancel_cb, void *cancel_arg)
7349 const struct got_error *err = NULL, *sync_err, *unlockerr;
7350 char *fileindex_path = NULL;
7351 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7353 err = get_fileindex_path(&fileindex_path, worktree);
7357 err = got_ref_resolve(&commit_id, repo, branch_ref);
7361 err = got_object_id_by_path(&tree_id, repo, commit_id,
7362 worktree->path_prefix);
7366 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7370 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7371 progress_cb, progress_arg, cancel_cb, cancel_arg);
7375 err = got_ref_change_ref(base_branch_ref, commit_id);
7379 err = got_ref_write(base_branch_ref, repo);
7383 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7385 sync_err = sync_fileindex(fileindex, fileindex_path);
7386 if (sync_err && err == NULL)
7390 unlockerr = got_ref_unlock(branch_ref);
7391 if (unlockerr && err == NULL)
7393 got_ref_close(branch_ref);
7395 unlockerr = got_ref_unlock(base_branch_ref);
7396 if (unlockerr && err == NULL)
7398 got_ref_close(base_branch_ref);
7400 got_fileindex_free(fileindex);
7401 free(fileindex_path);
7404 unlockerr = lock_worktree(worktree, LOCK_SH);
7405 if (unlockerr && err == NULL)
7410 const struct got_error *
7411 got_worktree_integrate_abort(struct got_worktree *worktree,
7412 struct got_fileindex *fileindex, struct got_repository *repo,
7413 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7415 const struct got_error *err = NULL, *unlockerr = NULL;
7417 got_fileindex_free(fileindex);
7419 err = lock_worktree(worktree, LOCK_SH);
7421 unlockerr = got_ref_unlock(branch_ref);
7422 if (unlockerr && err == NULL)
7424 got_ref_close(branch_ref);
7426 unlockerr = got_ref_unlock(base_branch_ref);
7427 if (unlockerr && err == NULL)
7429 got_ref_close(base_branch_ref);
7434 const struct got_error *
7435 got_worktree_merge_postpone(struct got_worktree *worktree,
7436 struct got_fileindex *fileindex)
7438 const struct got_error *err, *sync_err;
7439 char *fileindex_path = NULL;
7441 err = get_fileindex_path(&fileindex_path, worktree);
7445 sync_err = sync_fileindex(fileindex, fileindex_path);
7447 err = lock_worktree(worktree, LOCK_SH);
7448 if (sync_err && err == NULL)
7451 got_fileindex_free(fileindex);
7452 free(fileindex_path);
7456 static const struct got_error *
7457 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7459 const struct got_error *err;
7460 char *branch_refname = NULL, *commit_refname = NULL;
7462 err = get_merge_branch_ref_name(&branch_refname, worktree);
7465 err = delete_ref(branch_refname, repo);
7469 err = get_merge_commit_ref_name(&commit_refname, worktree);
7472 err = delete_ref(commit_refname, repo);
7477 free(branch_refname);
7478 free(commit_refname);
7482 struct merge_commit_msg_arg {
7483 struct got_worktree *worktree;
7484 const char *branch_name;
7487 static const struct got_error *
7488 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7491 struct merge_commit_msg_arg *a = arg;
7493 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7494 got_worktree_get_head_ref_name(a->worktree)) == -1)
7495 return got_error_from_errno("asprintf");
7500 static const struct got_error *
7501 merge_status_cb(void *arg, unsigned char status, unsigned char staged_status,
7502 const char *path, struct got_object_id *blob_id,
7503 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7504 int dirfd, const char *de_name)
7509 const struct got_error *
7510 got_worktree_merge_branch(struct got_worktree *worktree,
7511 struct got_fileindex *fileindex,
7512 struct got_object_id *yca_commit_id,
7513 struct got_object_id *branch_tip,
7514 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7515 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7517 const struct got_error *err;
7518 char *fileindex_path = NULL;
7520 err = get_fileindex_path(&fileindex_path, worktree);
7524 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7529 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7530 branch_tip, repo, progress_cb, progress_arg,
7531 cancel_cb, cancel_arg);
7533 free(fileindex_path);
7537 const struct got_error *
7538 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7539 struct got_worktree *worktree, struct got_fileindex *fileindex,
7540 const char *author, const char *committer, int allow_bad_symlinks,
7541 struct got_object_id *branch_tip, const char *branch_name,
7542 struct got_repository *repo)
7544 const struct got_error *err = NULL, *sync_err;
7545 struct got_pathlist_head commitable_paths;
7546 struct collect_commitables_arg cc_arg;
7547 struct got_pathlist_entry *pe;
7548 struct got_reference *head_ref = NULL;
7549 struct got_object_id *head_commit_id = NULL;
7550 int have_staged_files = 0;
7551 struct merge_commit_msg_arg mcm_arg;
7552 char *fileindex_path = NULL;
7554 *new_commit_id = NULL;
7556 TAILQ_INIT(&commitable_paths);
7558 err = get_fileindex_path(&fileindex_path, worktree);
7562 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7566 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7570 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7571 &have_staged_files);
7572 if (err && err->code != GOT_ERR_CANCELLED)
7574 if (have_staged_files) {
7575 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7579 cc_arg.commitable_paths = &commitable_paths;
7580 cc_arg.worktree = worktree;
7581 cc_arg.fileindex = fileindex;
7583 cc_arg.have_staged_files = have_staged_files;
7584 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7585 err = worktree_status(worktree, "", fileindex, repo,
7586 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
7590 if (TAILQ_EMPTY(&commitable_paths)) {
7591 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7592 "merge of %s cannot proceed", branch_name);
7596 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7597 struct got_commitable *ct = pe->data;
7598 const char *ct_path = ct->in_repo_path;
7600 while (ct_path[0] == '/')
7602 err = check_out_of_date(ct_path, ct->status,
7603 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7604 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7610 mcm_arg.worktree = worktree;
7611 mcm_arg.branch_name = branch_name;
7612 err = commit_worktree(new_commit_id, &commitable_paths,
7613 head_commit_id, branch_tip, worktree, author, committer,
7614 merge_commit_msg_cb, &mcm_arg, merge_status_cb, NULL, repo);
7618 err = update_fileindex_after_commit(worktree, &commitable_paths,
7619 *new_commit_id, fileindex, have_staged_files);
7620 sync_err = sync_fileindex(fileindex, fileindex_path);
7621 if (sync_err && err == NULL)
7624 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7625 struct got_commitable *ct = pe->data;
7626 free_commitable(ct);
7628 got_pathlist_free(&commitable_paths);
7629 free(fileindex_path);
7633 const struct got_error *
7634 got_worktree_merge_complete(struct got_worktree *worktree,
7635 struct got_fileindex *fileindex, struct got_repository *repo)
7637 const struct got_error *err, *unlockerr, *sync_err;
7638 char *fileindex_path = NULL;
7640 err = delete_merge_refs(worktree, repo);
7644 err = get_fileindex_path(&fileindex_path, worktree);
7647 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7648 sync_err = sync_fileindex(fileindex, fileindex_path);
7649 if (sync_err && err == NULL)
7652 got_fileindex_free(fileindex);
7653 free(fileindex_path);
7654 unlockerr = lock_worktree(worktree, LOCK_SH);
7655 if (unlockerr && err == NULL)
7660 const struct got_error *
7661 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7662 struct got_repository *repo)
7664 const struct got_error *err;
7665 char *branch_refname = NULL;
7666 struct got_reference *branch_ref = NULL;
7670 err = get_merge_branch_ref_name(&branch_refname, worktree);
7673 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7674 free(branch_refname);
7676 if (err->code != GOT_ERR_NOT_REF)
7684 const struct got_error *got_worktree_merge_prepare(
7685 struct got_fileindex **fileindex, struct got_worktree *worktree,
7686 struct got_reference *branch, struct got_repository *repo)
7688 const struct got_error *err = NULL;
7689 char *fileindex_path = NULL;
7690 char *branch_refname = NULL, *commit_refname = NULL;
7691 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7692 struct got_reference *commit_ref = NULL;
7693 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7694 struct check_rebase_ok_arg ok_arg;
7698 err = lock_worktree(worktree, LOCK_EX);
7702 err = open_fileindex(fileindex, &fileindex_path, worktree);
7706 /* Preconditions are the same as for rebase. */
7707 ok_arg.worktree = worktree;
7709 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7714 err = get_merge_branch_ref_name(&branch_refname, worktree);
7718 err = get_merge_commit_ref_name(&commit_refname, worktree);
7722 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7727 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7731 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7732 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7736 err = got_ref_resolve(&branch_tip, repo, branch);
7740 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7743 err = got_ref_write(branch_ref, repo);
7747 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7750 err = got_ref_write(commit_ref, repo);
7755 free(branch_refname);
7756 free(commit_refname);
7757 free(fileindex_path);
7759 got_ref_close(branch_ref);
7761 got_ref_close(commit_ref);
7763 got_ref_close(wt_branch);
7764 free(wt_branch_tip);
7767 got_fileindex_free(*fileindex);
7770 lock_worktree(worktree, LOCK_SH);
7775 const struct got_error *
7776 got_worktree_merge_continue(char **branch_name,
7777 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7778 struct got_worktree *worktree, struct got_repository *repo)
7780 const struct got_error *err;
7781 char *commit_refname = NULL, *branch_refname = NULL;
7782 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7783 char *fileindex_path = NULL;
7784 int have_staged_files = 0;
7786 *branch_name = NULL;
7790 err = lock_worktree(worktree, LOCK_EX);
7794 err = open_fileindex(fileindex, &fileindex_path, worktree);
7798 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7799 &have_staged_files);
7800 if (err && err->code != GOT_ERR_CANCELLED)
7802 if (have_staged_files) {
7803 err = got_error(GOT_ERR_STAGED_PATHS);
7807 err = get_merge_branch_ref_name(&branch_refname, worktree);
7811 err = get_merge_commit_ref_name(&commit_refname, worktree);
7815 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7819 if (!got_ref_is_symbolic(branch_ref)) {
7820 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7821 "%s is not a symbolic reference",
7822 got_ref_get_name(branch_ref));
7825 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7826 if (*branch_name == NULL) {
7827 err = got_error_from_errno("strdup");
7831 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7835 err = got_ref_resolve(branch_tip, repo, commit_ref);
7839 free(commit_refname);
7840 free(branch_refname);
7841 free(fileindex_path);
7843 got_ref_close(commit_ref);
7845 got_ref_close(branch_ref);
7849 *branch_name = NULL;
7854 got_fileindex_free(*fileindex);
7857 lock_worktree(worktree, LOCK_SH);
7862 const struct got_error *
7863 got_worktree_merge_abort(struct got_worktree *worktree,
7864 struct got_fileindex *fileindex, struct got_repository *repo,
7865 got_worktree_checkout_cb progress_cb, void *progress_arg)
7867 const struct got_error *err, *unlockerr, *sync_err;
7868 struct got_object_id *commit_id = NULL;
7869 char *fileindex_path = NULL;
7870 struct revert_file_args rfa;
7871 struct got_object_id *tree_id = NULL;
7873 err = got_object_id_by_path(&tree_id, repo,
7874 worktree->base_commit_id, worktree->path_prefix);
7878 err = delete_merge_refs(worktree, repo);
7882 err = get_fileindex_path(&fileindex_path, worktree);
7886 rfa.worktree = worktree;
7887 rfa.fileindex = fileindex;
7888 rfa.progress_cb = progress_cb;
7889 rfa.progress_arg = progress_arg;
7890 rfa.patch_cb = NULL;
7891 rfa.patch_arg = NULL;
7893 rfa.unlink_added_files = 1;
7894 err = worktree_status(worktree, "", fileindex, repo,
7895 revert_file, &rfa, NULL, NULL, 0, 0);
7899 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7900 repo, progress_cb, progress_arg, NULL, NULL);
7902 sync_err = sync_fileindex(fileindex, fileindex_path);
7903 if (sync_err && err == NULL)
7909 got_fileindex_free(fileindex);
7910 free(fileindex_path);
7912 unlockerr = lock_worktree(worktree, LOCK_SH);
7913 if (unlockerr && err == NULL)
7918 struct check_stage_ok_arg {
7919 struct got_object_id *head_commit_id;
7920 struct got_worktree *worktree;
7921 struct got_fileindex *fileindex;
7922 struct got_repository *repo;
7926 const struct got_error *
7927 check_stage_ok(void *arg, unsigned char status,
7928 unsigned char staged_status, const char *relpath,
7929 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7930 struct got_object_id *commit_id, int dirfd, const char *de_name)
7932 struct check_stage_ok_arg *a = arg;
7933 const struct got_error *err = NULL;
7934 struct got_fileindex_entry *ie;
7935 struct got_object_id base_commit_id;
7936 struct got_object_id *base_commit_idp = NULL;
7937 char *in_repo_path = NULL, *p;
7939 if (status == GOT_STATUS_UNVERSIONED ||
7940 status == GOT_STATUS_NO_CHANGE)
7942 if (status == GOT_STATUS_NONEXISTENT)
7943 return got_error_set_errno(ENOENT, relpath);
7945 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7947 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7949 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7950 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7952 return got_error_from_errno("asprintf");
7954 if (got_fileindex_entry_has_commit(ie)) {
7955 memcpy(base_commit_id.sha1, ie->commit_sha1,
7956 SHA1_DIGEST_LENGTH);
7957 base_commit_idp = &base_commit_id;
7960 if (status == GOT_STATUS_CONFLICT) {
7961 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7963 } else if (status != GOT_STATUS_ADD &&
7964 status != GOT_STATUS_MODIFY &&
7965 status != GOT_STATUS_DELETE) {
7966 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7970 a->have_changes = 1;
7975 err = check_out_of_date(p, status, staged_status,
7976 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7977 GOT_ERR_STAGE_OUT_OF_DATE);
7983 struct stage_path_arg {
7984 struct got_worktree *worktree;
7985 struct got_fileindex *fileindex;
7986 struct got_repository *repo;
7987 got_worktree_status_cb status_cb;
7989 got_worktree_patch_cb patch_cb;
7991 int staged_something;
7992 int allow_bad_symlinks;
7995 static const struct got_error *
7996 stage_path(void *arg, unsigned char status,
7997 unsigned char staged_status, const char *relpath,
7998 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7999 struct got_object_id *commit_id, int dirfd, const char *de_name)
8001 struct stage_path_arg *a = arg;
8002 const struct got_error *err = NULL;
8003 struct got_fileindex_entry *ie;
8004 char *ondisk_path = NULL, *path_content = NULL;
8006 struct got_object_id *new_staged_blob_id = NULL;
8009 if (status == GOT_STATUS_UNVERSIONED)
8012 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8014 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8016 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8018 return got_error_from_errno("asprintf");
8021 case GOT_STATUS_ADD:
8022 case GOT_STATUS_MODIFY:
8023 /* XXX could sb.st_mode be passed in by our caller? */
8024 if (lstat(ondisk_path, &sb) == -1) {
8025 err = got_error_from_errno2("lstat", ondisk_path);
8029 if (status == GOT_STATUS_ADD) {
8030 int choice = GOT_PATCH_CHOICE_NONE;
8031 err = (*a->patch_cb)(&choice, a->patch_arg,
8032 status, ie->path, NULL, 1, 1);
8035 if (choice != GOT_PATCH_CHOICE_YES)
8038 err = create_patched_content(&path_content, 0,
8039 staged_blob_id ? staged_blob_id : blob_id,
8040 ondisk_path, dirfd, de_name, ie->path,
8041 a->repo, a->patch_cb, a->patch_arg);
8042 if (err || path_content == NULL)
8046 err = got_object_blob_create(&new_staged_blob_id,
8047 path_content ? path_content : ondisk_path, a->repo);
8050 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8051 SHA1_DIGEST_LENGTH);
8052 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8053 stage = GOT_FILEIDX_STAGE_ADD;
8055 stage = GOT_FILEIDX_STAGE_MODIFY;
8056 got_fileindex_entry_stage_set(ie, stage);
8057 if (S_ISLNK(sb.st_mode)) {
8058 int is_bad_symlink = 0;
8059 if (!a->allow_bad_symlinks) {
8060 char target_path[PATH_MAX];
8062 target_len = readlink(ondisk_path, target_path,
8063 sizeof(target_path));
8064 if (target_len == -1) {
8065 err = got_error_from_errno2("readlink",
8069 err = is_bad_symlink_target(&is_bad_symlink,
8070 target_path, target_len, ondisk_path,
8071 a->worktree->root_path);
8074 if (is_bad_symlink) {
8075 err = got_error_path(ondisk_path,
8076 GOT_ERR_BAD_SYMLINK);
8081 got_fileindex_entry_staged_filetype_set(ie,
8082 GOT_FILEIDX_MODE_BAD_SYMLINK);
8084 got_fileindex_entry_staged_filetype_set(ie,
8085 GOT_FILEIDX_MODE_SYMLINK);
8087 got_fileindex_entry_staged_filetype_set(ie,
8088 GOT_FILEIDX_MODE_REGULAR_FILE);
8090 a->staged_something = 1;
8091 if (a->status_cb == NULL)
8093 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8094 get_staged_status(ie), relpath, blob_id,
8095 new_staged_blob_id, NULL, dirfd, de_name);
8097 case GOT_STATUS_DELETE:
8098 if (staged_status == GOT_STATUS_DELETE)
8101 int choice = GOT_PATCH_CHOICE_NONE;
8102 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8103 ie->path, NULL, 1, 1);
8106 if (choice == GOT_PATCH_CHOICE_NO)
8108 if (choice != GOT_PATCH_CHOICE_YES) {
8109 err = got_error(GOT_ERR_PATCH_CHOICE);
8113 stage = GOT_FILEIDX_STAGE_DELETE;
8114 got_fileindex_entry_stage_set(ie, stage);
8115 a->staged_something = 1;
8116 if (a->status_cb == NULL)
8118 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8119 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8122 case GOT_STATUS_NO_CHANGE:
8124 case GOT_STATUS_CONFLICT:
8125 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8127 case GOT_STATUS_NONEXISTENT:
8128 err = got_error_set_errno(ENOENT, relpath);
8131 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8135 if (path_content && unlink(path_content) == -1 && err == NULL)
8136 err = got_error_from_errno2("unlink", path_content);
8139 free(new_staged_blob_id);
8143 const struct got_error *
8144 got_worktree_stage(struct got_worktree *worktree,
8145 struct got_pathlist_head *paths,
8146 got_worktree_status_cb status_cb, void *status_arg,
8147 got_worktree_patch_cb patch_cb, void *patch_arg,
8148 int allow_bad_symlinks, struct got_repository *repo)
8150 const struct got_error *err = NULL, *sync_err, *unlockerr;
8151 struct got_pathlist_entry *pe;
8152 struct got_fileindex *fileindex = NULL;
8153 char *fileindex_path = NULL;
8154 struct got_reference *head_ref = NULL;
8155 struct got_object_id *head_commit_id = NULL;
8156 struct check_stage_ok_arg oka;
8157 struct stage_path_arg spa;
8159 err = lock_worktree(worktree, LOCK_EX);
8163 err = got_ref_open(&head_ref, repo,
8164 got_worktree_get_head_ref_name(worktree), 0);
8167 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8170 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8174 /* Check pre-conditions before staging anything. */
8175 oka.head_commit_id = head_commit_id;
8176 oka.worktree = worktree;
8177 oka.fileindex = fileindex;
8179 oka.have_changes = 0;
8180 TAILQ_FOREACH(pe, paths, entry) {
8181 err = worktree_status(worktree, pe->path, fileindex, repo,
8182 check_stage_ok, &oka, NULL, NULL, 0, 0);
8186 if (!oka.have_changes) {
8187 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8191 spa.worktree = worktree;
8192 spa.fileindex = fileindex;
8194 spa.patch_cb = patch_cb;
8195 spa.patch_arg = patch_arg;
8196 spa.status_cb = status_cb;
8197 spa.status_arg = status_arg;
8198 spa.staged_something = 0;
8199 spa.allow_bad_symlinks = allow_bad_symlinks;
8200 TAILQ_FOREACH(pe, paths, entry) {
8201 err = worktree_status(worktree, pe->path, fileindex, repo,
8202 stage_path, &spa, NULL, NULL, 0, 0);
8206 if (!spa.staged_something) {
8207 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8211 sync_err = sync_fileindex(fileindex, fileindex_path);
8212 if (sync_err && err == NULL)
8216 got_ref_close(head_ref);
8217 free(head_commit_id);
8218 free(fileindex_path);
8220 got_fileindex_free(fileindex);
8221 unlockerr = lock_worktree(worktree, LOCK_SH);
8222 if (unlockerr && err == NULL)
8227 struct unstage_path_arg {
8228 struct got_worktree *worktree;
8229 struct got_fileindex *fileindex;
8230 struct got_repository *repo;
8231 got_worktree_checkout_cb progress_cb;
8233 got_worktree_patch_cb patch_cb;
8237 static const struct got_error *
8238 create_unstaged_content(char **path_unstaged_content,
8239 char **path_new_staged_content, struct got_object_id *blob_id,
8240 struct got_object_id *staged_blob_id, const char *relpath,
8241 struct got_repository *repo,
8242 got_worktree_patch_cb patch_cb, void *patch_arg)
8244 const struct got_error *err, *free_err;
8245 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8246 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8247 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8248 struct got_diffreg_result *diffreg_result = NULL;
8249 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8250 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8252 *path_unstaged_content = NULL;
8253 *path_new_staged_content = NULL;
8255 err = got_object_id_str(&label1, blob_id);
8258 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8262 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8266 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8270 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8274 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8278 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8282 err = got_diff_files(&diffreg_result, f1, label1, f2,
8283 path2, 3, 0, 1, NULL);
8287 err = got_opentemp_named(path_unstaged_content, &outfile,
8288 "got-unstaged-content");
8291 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8292 "got-new-staged-content");
8296 if (fseek(f1, 0L, SEEK_SET) == -1) {
8297 err = got_ferror(f1, GOT_ERR_IO);
8300 if (fseek(f2, 0L, SEEK_SET) == -1) {
8301 err = got_ferror(f2, GOT_ERR_IO);
8304 /* Count the number of actual changes in the diff result. */
8305 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8306 struct diff_chunk_context cc = {};
8307 diff_chunk_context_load_change(&cc, &nchunks_used,
8308 diffreg_result->result, n, 0);
8311 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8313 err = apply_or_reject_change(&choice, &nchunks_used,
8314 diffreg_result->result, n, relpath, f1, f2,
8315 &line_cur1, &line_cur2,
8316 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8319 if (choice == GOT_PATCH_CHOICE_YES)
8322 have_rejected_content = 1;
8323 if (choice == GOT_PATCH_CHOICE_QUIT)
8326 if (have_content || have_rejected_content)
8327 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8328 outfile, rejectfile);
8332 got_object_blob_close(blob);
8334 got_object_blob_close(staged_blob);
8335 free_err = got_diffreg_result_free(diffreg_result);
8336 if (free_err && err == NULL)
8338 if (f1 && fclose(f1) == EOF && err == NULL)
8339 err = got_error_from_errno2("fclose", path1);
8340 if (f2 && fclose(f2) == EOF && err == NULL)
8341 err = got_error_from_errno2("fclose", path2);
8342 if (outfile && fclose(outfile) == EOF && err == NULL)
8343 err = got_error_from_errno2("fclose", *path_unstaged_content);
8344 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8345 err = got_error_from_errno2("fclose", *path_new_staged_content);
8346 if (path1 && unlink(path1) == -1 && err == NULL)
8347 err = got_error_from_errno2("unlink", path1);
8348 if (path2 && unlink(path2) == -1 && err == NULL)
8349 err = got_error_from_errno2("unlink", path2);
8350 if (err || !have_content) {
8351 if (*path_unstaged_content &&
8352 unlink(*path_unstaged_content) == -1 && err == NULL)
8353 err = got_error_from_errno2("unlink",
8354 *path_unstaged_content);
8355 free(*path_unstaged_content);
8356 *path_unstaged_content = NULL;
8358 if (err || !have_content || !have_rejected_content) {
8359 if (*path_new_staged_content &&
8360 unlink(*path_new_staged_content) == -1 && err == NULL)
8361 err = got_error_from_errno2("unlink",
8362 *path_new_staged_content);
8363 free(*path_new_staged_content);
8364 *path_new_staged_content = NULL;
8371 static const struct got_error *
8372 unstage_hunks(struct got_object_id *staged_blob_id,
8373 struct got_blob_object *blob_base,
8374 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8375 const char *ondisk_path, const char *label_orig,
8376 struct got_worktree *worktree, struct got_repository *repo,
8377 got_worktree_patch_cb patch_cb, void *patch_arg,
8378 got_worktree_checkout_cb progress_cb, void *progress_arg)
8380 const struct got_error *err = NULL;
8381 char *path_unstaged_content = NULL;
8382 char *path_new_staged_content = NULL;
8383 char *parent = NULL, *base_path = NULL;
8384 char *blob_base_path = NULL;
8385 struct got_object_id *new_staged_blob_id = NULL;
8386 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8389 err = create_unstaged_content(&path_unstaged_content,
8390 &path_new_staged_content, blob_id, staged_blob_id,
8391 ie->path, repo, patch_cb, patch_arg);
8395 if (path_unstaged_content == NULL)
8398 if (path_new_staged_content) {
8399 err = got_object_blob_create(&new_staged_blob_id,
8400 path_new_staged_content, repo);
8405 f = fopen(path_unstaged_content, "r");
8407 err = got_error_from_errno2("fopen",
8408 path_unstaged_content);
8411 if (fstat(fileno(f), &sb) == -1) {
8412 err = got_error_from_errno2("fstat", path_unstaged_content);
8415 if (got_fileindex_entry_staged_filetype_get(ie) ==
8416 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8417 char link_target[PATH_MAX];
8419 r = fread(link_target, 1, sizeof(link_target), f);
8420 if (r == 0 && ferror(f)) {
8421 err = got_error_from_errno("fread");
8424 if (r >= sizeof(link_target)) { /* should not happen */
8425 err = got_error(GOT_ERR_NO_SPACE);
8428 link_target[r] = '\0';
8429 err = merge_symlink(worktree, blob_base,
8430 ondisk_path, ie->path, label_orig, link_target,
8431 worktree->base_commit_id, repo, progress_cb,
8434 int local_changes_subsumed;
8436 err = got_path_dirname(&parent, ondisk_path);
8440 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8442 err = got_error_from_errno("asprintf");
8447 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8450 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8456 * In order the run a 3-way merge with a symlink we copy the symlink's
8457 * target path into a temporary file and use that file with diff3.
8459 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8460 err = dump_symlink_target_path_to_file(&f_deriv2,
8466 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8468 err = got_error_from_errno2("open", ondisk_path);
8471 f_deriv2 = fdopen(fd, "r");
8472 if (f_deriv2 == NULL) {
8473 err = got_error_from_errno2("fdopen", ondisk_path);
8479 err = merge_file(&local_changes_subsumed, worktree,
8480 f_base, f, f_deriv2, ondisk_path, ie->path,
8481 got_fileindex_perms_to_st(ie),
8482 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8483 repo, progress_cb, progress_arg);
8488 if (new_staged_blob_id) {
8489 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8490 SHA1_DIGEST_LENGTH);
8492 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8493 got_fileindex_entry_staged_filetype_set(ie, 0);
8496 free(new_staged_blob_id);
8497 if (path_unstaged_content &&
8498 unlink(path_unstaged_content) == -1 && err == NULL)
8499 err = got_error_from_errno2("unlink", path_unstaged_content);
8500 if (path_new_staged_content &&
8501 unlink(path_new_staged_content) == -1 && err == NULL)
8502 err = got_error_from_errno2("unlink", path_new_staged_content);
8503 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8504 err = got_error_from_errno2("unlink", blob_base_path);
8505 if (f_base && fclose(f_base) == EOF && err == NULL)
8506 err = got_error_from_errno2("fclose", path_unstaged_content);
8507 if (f && fclose(f) == EOF && err == NULL)
8508 err = got_error_from_errno2("fclose", path_unstaged_content);
8509 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8510 err = got_error_from_errno2("fclose", ondisk_path);
8511 free(path_unstaged_content);
8512 free(path_new_staged_content);
8513 free(blob_base_path);
8519 static const struct got_error *
8520 unstage_path(void *arg, unsigned char status,
8521 unsigned char staged_status, const char *relpath,
8522 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8523 struct got_object_id *commit_id, int dirfd, const char *de_name)
8525 const struct got_error *err = NULL;
8526 struct unstage_path_arg *a = arg;
8527 struct got_fileindex_entry *ie;
8528 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8529 char *ondisk_path = NULL;
8530 char *id_str = NULL, *label_orig = NULL;
8531 int local_changes_subsumed;
8534 if (staged_status != GOT_STATUS_ADD &&
8535 staged_status != GOT_STATUS_MODIFY &&
8536 staged_status != GOT_STATUS_DELETE)
8539 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8541 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8543 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8545 return got_error_from_errno("asprintf");
8547 err = got_object_id_str(&id_str,
8548 commit_id ? commit_id : a->worktree->base_commit_id);
8551 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8553 err = got_error_from_errno("asprintf");
8557 switch (staged_status) {
8558 case GOT_STATUS_MODIFY:
8559 err = got_object_open_as_blob(&blob_base, a->repo,
8564 case GOT_STATUS_ADD:
8566 if (staged_status == GOT_STATUS_ADD) {
8567 int choice = GOT_PATCH_CHOICE_NONE;
8568 err = (*a->patch_cb)(&choice, a->patch_arg,
8569 staged_status, ie->path, NULL, 1, 1);
8572 if (choice != GOT_PATCH_CHOICE_YES)
8575 err = unstage_hunks(staged_blob_id,
8576 blob_base, blob_id, ie, ondisk_path,
8577 label_orig, a->worktree, a->repo,
8578 a->patch_cb, a->patch_arg,
8579 a->progress_cb, a->progress_arg);
8580 break; /* Done with this file. */
8583 err = got_object_open_as_blob(&blob_staged, a->repo,
8584 staged_blob_id, 8192);
8587 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8588 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8589 case GOT_FILEIDX_MODE_REGULAR_FILE:
8590 err = merge_blob(&local_changes_subsumed, a->worktree,
8591 blob_base, ondisk_path, relpath,
8592 got_fileindex_perms_to_st(ie), label_orig,
8593 blob_staged, commit_id ? commit_id :
8594 a->worktree->base_commit_id, a->repo,
8595 a->progress_cb, a->progress_arg);
8597 case GOT_FILEIDX_MODE_SYMLINK:
8598 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8599 char *staged_target;
8600 err = got_object_blob_read_to_str(
8601 &staged_target, blob_staged);
8604 err = merge_symlink(a->worktree, blob_base,
8605 ondisk_path, relpath, label_orig,
8606 staged_target, commit_id ? commit_id :
8607 a->worktree->base_commit_id,
8608 a->repo, a->progress_cb, a->progress_arg);
8609 free(staged_target);
8611 err = merge_blob(&local_changes_subsumed,
8612 a->worktree, blob_base, ondisk_path,
8613 relpath, got_fileindex_perms_to_st(ie),
8614 label_orig, blob_staged,
8615 commit_id ? commit_id :
8616 a->worktree->base_commit_id, a->repo,
8617 a->progress_cb, a->progress_arg);
8621 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8625 got_fileindex_entry_stage_set(ie,
8626 GOT_FILEIDX_STAGE_NONE);
8627 got_fileindex_entry_staged_filetype_set(ie, 0);
8630 case GOT_STATUS_DELETE:
8632 int choice = GOT_PATCH_CHOICE_NONE;
8633 err = (*a->patch_cb)(&choice, a->patch_arg,
8634 staged_status, ie->path, NULL, 1, 1);
8637 if (choice == GOT_PATCH_CHOICE_NO)
8639 if (choice != GOT_PATCH_CHOICE_YES) {
8640 err = got_error(GOT_ERR_PATCH_CHOICE);
8644 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8645 got_fileindex_entry_staged_filetype_set(ie, 0);
8646 err = get_file_status(&status, &sb, ie, ondisk_path,
8647 dirfd, de_name, a->repo);
8650 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8656 got_object_blob_close(blob_base);
8658 got_object_blob_close(blob_staged);
8664 const struct got_error *
8665 got_worktree_unstage(struct got_worktree *worktree,
8666 struct got_pathlist_head *paths,
8667 got_worktree_checkout_cb progress_cb, void *progress_arg,
8668 got_worktree_patch_cb patch_cb, void *patch_arg,
8669 struct got_repository *repo)
8671 const struct got_error *err = NULL, *sync_err, *unlockerr;
8672 struct got_pathlist_entry *pe;
8673 struct got_fileindex *fileindex = NULL;
8674 char *fileindex_path = NULL;
8675 struct unstage_path_arg upa;
8677 err = lock_worktree(worktree, LOCK_EX);
8681 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8685 upa.worktree = worktree;
8686 upa.fileindex = fileindex;
8688 upa.progress_cb = progress_cb;
8689 upa.progress_arg = progress_arg;
8690 upa.patch_cb = patch_cb;
8691 upa.patch_arg = patch_arg;
8692 TAILQ_FOREACH(pe, paths, entry) {
8693 err = worktree_status(worktree, pe->path, fileindex, repo,
8694 unstage_path, &upa, NULL, NULL, 0, 0);
8699 sync_err = sync_fileindex(fileindex, fileindex_path);
8700 if (sync_err && err == NULL)
8703 free(fileindex_path);
8705 got_fileindex_free(fileindex);
8706 unlockerr = lock_worktree(worktree, LOCK_SH);
8707 if (unlockerr && err == NULL)
8712 struct report_file_info_arg {
8713 struct got_worktree *worktree;
8714 got_worktree_path_info_cb info_cb;
8716 struct got_pathlist_head *paths;
8717 got_cancel_cb cancel_cb;
8721 static const struct got_error *
8722 report_file_info(void *arg, struct got_fileindex_entry *ie)
8724 struct report_file_info_arg *a = arg;
8725 struct got_pathlist_entry *pe;
8726 struct got_object_id blob_id, staged_blob_id, commit_id;
8727 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8728 struct got_object_id *commit_idp = NULL;
8731 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8732 return got_error(GOT_ERR_CANCELLED);
8734 TAILQ_FOREACH(pe, a->paths, entry) {
8735 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8736 got_path_is_child(ie->path, pe->path, pe->path_len))
8739 if (pe == NULL) /* not found */
8742 if (got_fileindex_entry_has_blob(ie)) {
8743 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8744 blob_idp = &blob_id;
8746 stage = got_fileindex_entry_stage_get(ie);
8747 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8748 stage == GOT_FILEIDX_STAGE_ADD) {
8749 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8750 SHA1_DIGEST_LENGTH);
8751 staged_blob_idp = &staged_blob_id;
8754 if (got_fileindex_entry_has_commit(ie)) {
8755 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8756 commit_idp = &commit_id;
8759 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8760 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8763 const struct got_error *
8764 got_worktree_path_info(struct got_worktree *worktree,
8765 struct got_pathlist_head *paths,
8766 got_worktree_path_info_cb info_cb, void *info_arg,
8767 got_cancel_cb cancel_cb, void *cancel_arg)
8770 const struct got_error *err = NULL, *unlockerr;
8771 struct got_fileindex *fileindex = NULL;
8772 char *fileindex_path = NULL;
8773 struct report_file_info_arg arg;
8775 err = lock_worktree(worktree, LOCK_SH);
8779 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8783 arg.worktree = worktree;
8784 arg.info_cb = info_cb;
8785 arg.info_arg = info_arg;
8787 arg.cancel_cb = cancel_cb;
8788 arg.cancel_arg = cancel_arg;
8789 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8792 free(fileindex_path);
8794 got_fileindex_free(fileindex);
8795 unlockerr = lock_worktree(worktree, LOCK_UN);
8796 if (unlockerr && err == NULL)