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, struct got_repository *repo,
1374 got_worktree_checkout_cb progress_cb, void *progress_arg)
1376 const struct got_error *err = NULL;
1377 char target_path[PATH_MAX];
1378 size_t len, target_len = 0;
1379 char *path_got = NULL;
1380 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1381 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1383 *is_bad_symlink = 0;
1386 * Blob object content specifies the target path of the link.
1387 * If a symbolic link cannot be installed we instead create
1388 * a regular file which contains the link target path stored
1389 * in the blob object.
1392 err = got_object_blob_read_block(&len, blob);
1393 if (len + target_len >= sizeof(target_path)) {
1394 /* Path too long; install as a regular file. */
1395 *is_bad_symlink = 1;
1396 got_object_blob_rewind(blob);
1397 return install_blob(worktree, ondisk_path, path,
1398 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1399 restoring_missing_file, reverting_versioned_file,
1400 1, path_is_unversioned, repo, progress_cb,
1404 /* Skip blob object header first time around. */
1405 memcpy(target_path + target_len, buf + hdrlen,
1407 target_len += len - hdrlen;
1411 target_path[target_len] = '\0';
1413 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1414 ondisk_path, worktree->root_path);
1418 if (*is_bad_symlink) {
1419 /* install as a regular file */
1420 got_object_blob_rewind(blob);
1421 err = install_blob(worktree, ondisk_path, path,
1422 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1423 restoring_missing_file, reverting_versioned_file, 1,
1424 path_is_unversioned, repo, progress_cb, progress_arg);
1428 if (symlink(target_path, ondisk_path) == -1) {
1429 if (errno == EEXIST) {
1430 int symlink_replaced;
1431 if (path_is_unversioned) {
1432 err = (*progress_cb)(progress_arg,
1433 GOT_STATUS_UNVERSIONED, path);
1436 err = replace_existing_symlink(&symlink_replaced,
1437 ondisk_path, target_path, target_len);
1441 if (symlink_replaced) {
1442 err = (*progress_cb)(progress_arg,
1443 reverting_versioned_file ?
1445 GOT_STATUS_UPDATE, path);
1447 err = (*progress_cb)(progress_arg,
1448 GOT_STATUS_EXISTS, path);
1451 goto done; /* Nothing else to do. */
1454 if (errno == ENOENT) {
1456 err = got_path_dirname(&parent, ondisk_path);
1459 err = add_dir_on_disk(worktree, parent);
1464 * Retry, and fall through to error handling
1465 * below if this second attempt fails.
1467 if (symlink(target_path, ondisk_path) != -1) {
1468 err = NULL; /* success */
1473 /* Handle errors from first or second creation attempt. */
1474 if (errno == ENAMETOOLONG) {
1475 /* bad target path; install as a regular file */
1476 *is_bad_symlink = 1;
1477 got_object_blob_rewind(blob);
1478 err = install_blob(worktree, ondisk_path, path,
1479 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1480 restoring_missing_file, reverting_versioned_file, 1,
1481 path_is_unversioned, repo,
1482 progress_cb, progress_arg);
1483 } else if (errno == ENOTDIR) {
1484 err = got_error_path(ondisk_path,
1485 GOT_ERR_FILE_OBSTRUCTED);
1487 err = got_error_from_errno3("symlink",
1488 target_path, ondisk_path);
1490 } else if (progress_cb)
1491 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1492 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1498 static const struct got_error *
1499 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1500 const char *path, mode_t te_mode, mode_t st_mode,
1501 struct got_blob_object *blob, int restoring_missing_file,
1502 int reverting_versioned_file, int installing_bad_symlink,
1503 int path_is_unversioned, struct got_repository *repo,
1504 got_worktree_checkout_cb progress_cb, void *progress_arg)
1506 const struct got_error *err = NULL;
1510 char *tmppath = NULL;
1512 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1513 GOT_DEFAULT_FILE_MODE);
1515 if (errno == ENOENT) {
1517 err = got_path_dirname(&parent, path);
1520 err = add_dir_on_disk(worktree, parent);
1524 fd = open(ondisk_path,
1525 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1526 GOT_DEFAULT_FILE_MODE);
1528 return got_error_from_errno2("open",
1530 } else if (errno == EEXIST) {
1531 if (path_is_unversioned) {
1532 err = (*progress_cb)(progress_arg,
1533 GOT_STATUS_UNVERSIONED, path);
1536 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1537 !S_ISREG(st_mode) && !installing_bad_symlink) {
1538 /* TODO file is obstructed; do something */
1539 err = got_error_path(ondisk_path,
1540 GOT_ERR_FILE_OBSTRUCTED);
1543 err = got_opentemp_named_fd(&tmppath, &fd,
1550 return got_error_from_errno2("open", ondisk_path);
1553 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1554 err = got_error_from_errno2("fchmod",
1555 update ? tmppath : ondisk_path);
1560 if (restoring_missing_file)
1561 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1563 else if (reverting_versioned_file)
1564 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1567 err = (*progress_cb)(progress_arg,
1568 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1573 hdrlen = got_object_blob_get_hdrlen(blob);
1575 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1576 err = got_object_blob_read_block(&len, blob);
1580 /* Skip blob object header first time around. */
1581 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1583 err = got_error_from_errno("write");
1585 } else if (outlen != len - hdrlen) {
1586 err = got_error(GOT_ERR_IO);
1593 if (fsync(fd) != 0) {
1594 err = got_error_from_errno("fsync");
1599 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1600 err = got_error_from_errno2("unlink", ondisk_path);
1603 if (rename(tmppath, ondisk_path) != 0) {
1604 err = got_error_from_errno3("rename", tmppath,
1613 if (fd != -1 && close(fd) == -1 && err == NULL)
1614 err = got_error_from_errno("close");
1615 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1616 err = got_error_from_errno2("unlink", tmppath);
1621 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1622 static const struct got_error *
1623 get_modified_file_content_status(unsigned char *status, FILE *f)
1625 const struct got_error *err = NULL;
1626 const char *markers[3] = {
1627 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1628 GOT_DIFF_CONFLICT_MARKER_SEP,
1629 GOT_DIFF_CONFLICT_MARKER_END
1633 size_t linesize = 0;
1636 while (*status == GOT_STATUS_MODIFY) {
1637 linelen = getline(&line, &linesize, f);
1638 if (linelen == -1) {
1641 err = got_ferror(f, GOT_ERR_IO);
1645 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1646 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1648 *status = GOT_STATUS_CONFLICT;
1659 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1661 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1662 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1666 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1668 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1669 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1670 ie->mtime_sec == sb->st_mtim.tv_sec &&
1671 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1672 ie->size == (sb->st_size & 0xffffffff) &&
1673 !xbit_differs(ie, sb->st_mode));
1676 static unsigned char
1677 get_staged_status(struct got_fileindex_entry *ie)
1679 switch (got_fileindex_entry_stage_get(ie)) {
1680 case GOT_FILEIDX_STAGE_ADD:
1681 return GOT_STATUS_ADD;
1682 case GOT_FILEIDX_STAGE_DELETE:
1683 return GOT_STATUS_DELETE;
1684 case GOT_FILEIDX_STAGE_MODIFY:
1685 return GOT_STATUS_MODIFY;
1687 return GOT_STATUS_NO_CHANGE;
1691 static const struct got_error *
1692 get_symlink_modification_status(unsigned char *status,
1693 struct got_fileindex_entry *ie, const char *abspath,
1694 int dirfd, const char *de_name, struct got_blob_object *blob)
1696 const struct got_error *err = NULL;
1697 char target_path[PATH_MAX];
1698 char etarget[PATH_MAX];
1700 size_t len, target_len = 0;
1701 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1702 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1704 *status = GOT_STATUS_NO_CHANGE;
1706 /* Blob object content specifies the target path of the link. */
1708 err = got_object_blob_read_block(&len, blob);
1711 if (len + target_len >= sizeof(target_path)) {
1713 * Should not happen. The blob contents were OK
1714 * when this symlink was installed.
1716 return got_error(GOT_ERR_NO_SPACE);
1719 /* Skip blob object header first time around. */
1720 memcpy(target_path + target_len, buf + hdrlen,
1722 target_len += len - hdrlen;
1726 target_path[target_len] = '\0';
1729 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1731 return got_error_from_errno2("readlinkat", abspath);
1733 elen = readlink(abspath, etarget, sizeof(etarget));
1735 return got_error_from_errno2("readlink", abspath);
1738 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1739 *status = GOT_STATUS_MODIFY;
1744 static const struct got_error *
1745 get_file_status(unsigned char *status, struct stat *sb,
1746 struct got_fileindex_entry *ie, const char *abspath,
1747 int dirfd, const char *de_name, struct got_repository *repo)
1749 const struct got_error *err = NULL;
1750 struct got_object_id id;
1755 struct got_blob_object *blob = NULL;
1757 unsigned char staged_status = get_staged_status(ie);
1759 *status = GOT_STATUS_NO_CHANGE;
1760 memset(sb, 0, sizeof(*sb));
1763 * Whenever the caller provides a directory descriptor and a
1764 * directory entry name for the file, use them! This prevents
1765 * race conditions if filesystem paths change beneath our feet.
1768 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1769 if (errno == ENOENT) {
1770 if (got_fileindex_entry_has_file_on_disk(ie))
1771 *status = GOT_STATUS_MISSING;
1773 *status = GOT_STATUS_DELETE;
1776 err = got_error_from_errno2("fstatat", abspath);
1780 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1781 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1782 return got_error_from_errno2("open", abspath);
1783 else if (fd == -1 && errno == ELOOP) {
1784 if (lstat(abspath, sb) == -1)
1785 return got_error_from_errno2("lstat", abspath);
1786 } else if (fd == -1 || fstat(fd, sb) == -1) {
1787 if (errno == ENOENT) {
1788 if (got_fileindex_entry_has_file_on_disk(ie))
1789 *status = GOT_STATUS_MISSING;
1791 *status = GOT_STATUS_DELETE;
1794 err = got_error_from_errno2("fstat", abspath);
1799 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1800 *status = GOT_STATUS_OBSTRUCTED;
1804 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1805 *status = GOT_STATUS_DELETE;
1807 } else if (!got_fileindex_entry_has_blob(ie) &&
1808 staged_status != GOT_STATUS_ADD) {
1809 *status = GOT_STATUS_ADD;
1813 if (!stat_info_differs(ie, sb))
1816 if (S_ISLNK(sb->st_mode) &&
1817 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1818 *status = GOT_STATUS_MODIFY;
1822 if (staged_status == GOT_STATUS_MODIFY ||
1823 staged_status == GOT_STATUS_ADD)
1824 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1826 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1828 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1832 if (S_ISLNK(sb->st_mode)) {
1833 err = get_symlink_modification_status(status, ie,
1834 abspath, dirfd, de_name, blob);
1839 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1841 err = got_error_from_errno2("openat", abspath);
1846 f = fdopen(fd, "r");
1848 err = got_error_from_errno2("fdopen", abspath);
1852 hdrlen = got_object_blob_get_hdrlen(blob);
1854 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1855 err = got_object_blob_read_block(&blen, blob);
1858 /* Skip length of blob object header first time around. */
1859 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1860 if (flen == 0 && ferror(f)) {
1861 err = got_error_from_errno("fread");
1864 if (blen - hdrlen == 0) {
1866 *status = GOT_STATUS_MODIFY;
1868 } else if (flen == 0) {
1869 if (blen - hdrlen != 0)
1870 *status = GOT_STATUS_MODIFY;
1872 } else if (blen - hdrlen == flen) {
1873 /* Skip blob object header first time around. */
1874 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1875 *status = GOT_STATUS_MODIFY;
1879 *status = GOT_STATUS_MODIFY;
1885 if (*status == GOT_STATUS_MODIFY) {
1887 err = get_modified_file_content_status(status, f);
1888 } else if (xbit_differs(ie, sb->st_mode))
1889 *status = GOT_STATUS_MODE_CHANGE;
1892 got_object_blob_close(blob);
1893 if (f != NULL && fclose(f) == EOF && err == NULL)
1894 err = got_error_from_errno2("fclose", abspath);
1895 if (fd != -1 && close(fd) == -1 && err == NULL)
1896 err = got_error_from_errno2("close", abspath);
1901 * Update timestamps in the file index if a file is unmodified and
1902 * we had to run a full content comparison to find out.
1904 static const struct got_error *
1905 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1906 struct got_fileindex_entry *ie, struct stat *sb)
1908 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1909 return got_fileindex_entry_update(ie, wt_fd, path,
1910 ie->blob_sha1, ie->commit_sha1, 1);
1915 static const struct got_error *
1916 update_blob(struct got_worktree *worktree,
1917 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1918 struct got_tree_entry *te, const char *path,
1919 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1922 const struct got_error *err = NULL;
1923 struct got_blob_object *blob = NULL;
1925 unsigned char status = GOT_STATUS_NO_CHANGE;
1928 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1929 return got_error_from_errno("asprintf");
1932 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1933 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1936 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1940 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1941 sb.st_mode = got_fileindex_perms_to_st(ie);
1943 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1944 status = GOT_STATUS_UNVERSIONED;
1947 if (status == GOT_STATUS_OBSTRUCTED) {
1949 got_fileindex_entry_mark_skipped(ie);
1950 err = (*progress_cb)(progress_arg, status, path);
1953 if (status == GOT_STATUS_CONFLICT) {
1955 got_fileindex_entry_mark_skipped(ie);
1956 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1961 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1962 (S_ISLNK(te->mode) ||
1963 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1965 * This is a regular file or an installed bad symlink.
1966 * If the file index indicates that this file is already
1967 * up-to-date with respect to the repository we can skip
1968 * updating contents of this file.
1970 if (got_fileindex_entry_has_commit(ie) &&
1971 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1972 SHA1_DIGEST_LENGTH) == 0) {
1974 err = sync_timestamps(worktree->root_fd,
1975 path, status, ie, &sb);
1978 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1982 if (got_fileindex_entry_has_blob(ie) &&
1983 memcmp(ie->blob_sha1, te->id.sha1,
1984 SHA1_DIGEST_LENGTH) == 0) {
1985 /* Different commit but the same blob. */
1986 err = sync_timestamps(worktree->root_fd,
1987 path, status, ie, &sb);
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1996 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2000 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2001 int update_timestamps;
2002 struct got_blob_object *blob2 = NULL;
2003 char *label_orig = NULL;
2004 if (got_fileindex_entry_has_blob(ie)) {
2005 struct got_object_id id2;
2006 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2007 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2011 if (got_fileindex_entry_has_commit(ie)) {
2012 char id_str[SHA1_DIGEST_STRING_LENGTH];
2013 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2014 sizeof(id_str)) == NULL) {
2015 err = got_error_path(id_str,
2016 GOT_ERR_BAD_OBJ_ID_STR);
2019 if (asprintf(&label_orig, "%s: commit %s",
2020 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2021 err = got_error_from_errno("asprintf");
2025 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2027 err = got_object_blob_read_to_str(&link_target, blob);
2030 err = merge_symlink(worktree, blob2, ondisk_path, path,
2031 label_orig, link_target, worktree->base_commit_id,
2032 repo, progress_cb, progress_arg);
2035 err = merge_blob(&update_timestamps, worktree, blob2,
2036 ondisk_path, path, sb.st_mode, label_orig, blob,
2037 worktree->base_commit_id, repo,
2038 progress_cb, progress_arg);
2042 got_object_blob_close(blob2);
2046 * Do not update timestamps of files with local changes.
2047 * Otherwise, a future status walk would treat them as
2048 * unmodified files again.
2050 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 blob->id.sha1, worktree->base_commit_id->sha1,
2053 } else if (status == GOT_STATUS_MODE_CHANGE) {
2054 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2055 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2056 } else if (status == GOT_STATUS_DELETE) {
2057 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2060 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2061 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2065 int is_bad_symlink = 0;
2066 if (S_ISLNK(te->mode)) {
2067 err = install_symlink(&is_bad_symlink, worktree,
2068 ondisk_path, path, blob,
2069 status == GOT_STATUS_MISSING, 0,
2070 status == GOT_STATUS_UNVERSIONED, repo,
2071 progress_cb, progress_arg);
2073 err = install_blob(worktree, ondisk_path, path,
2074 te->mode, sb.st_mode, blob,
2075 status == GOT_STATUS_MISSING, 0, 0,
2076 status == GOT_STATUS_UNVERSIONED, repo,
2077 progress_cb, progress_arg);
2083 err = got_fileindex_entry_update(ie,
2084 worktree->root_fd, path, blob->id.sha1,
2085 worktree->base_commit_id->sha1, 1);
2087 err = create_fileindex_entry(&ie, fileindex,
2088 worktree->base_commit_id, worktree->root_fd, path,
2094 if (is_bad_symlink) {
2095 got_fileindex_entry_filetype_set(ie,
2096 GOT_FILEIDX_MODE_BAD_SYMLINK);
2099 got_object_blob_close(blob);
2105 static const struct got_error *
2106 remove_ondisk_file(const char *root_path, const char *path)
2108 const struct got_error *err = NULL;
2109 char *ondisk_path = NULL, *parent = NULL;
2111 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2112 return got_error_from_errno("asprintf");
2114 if (unlink(ondisk_path) == -1) {
2115 if (errno != ENOENT)
2116 err = got_error_from_errno2("unlink", ondisk_path);
2118 size_t root_len = strlen(root_path);
2119 err = got_path_dirname(&parent, ondisk_path);
2122 while (got_path_cmp(parent, root_path,
2123 strlen(parent), root_len) != 0) {
2125 ondisk_path = parent;
2127 if (rmdir(ondisk_path) == -1) {
2128 if (errno != ENOTEMPTY)
2129 err = got_error_from_errno2("rmdir",
2133 err = got_path_dirname(&parent, ondisk_path);
2144 static const struct got_error *
2145 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2146 struct got_fileindex_entry *ie, struct got_repository *repo,
2147 got_worktree_checkout_cb progress_cb, void *progress_arg)
2149 const struct got_error *err = NULL;
2150 unsigned char status;
2154 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2155 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2157 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2159 return got_error_from_errno("asprintf");
2161 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2165 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2166 char ondisk_target[PATH_MAX];
2167 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2168 sizeof(ondisk_target));
2169 if (ondisk_len == -1) {
2170 err = got_error_from_errno2("readlink", ondisk_path);
2173 ondisk_target[ondisk_len] = '\0';
2174 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2175 NULL, NULL, /* XXX pass common ancestor info? */
2176 ondisk_target, ondisk_path);
2179 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2184 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2185 status == GOT_STATUS_ADD) {
2186 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2190 * Preserve the working file and change the deleted blob's
2191 * entry into a schedule-add entry.
2193 err = got_fileindex_entry_update(ie, worktree->root_fd,
2194 ie->path, NULL, NULL, 0);
2196 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2199 if (status == GOT_STATUS_NO_CHANGE) {
2200 err = remove_ondisk_file(worktree->root_path, ie->path);
2204 got_fileindex_entry_remove(fileindex, ie);
2211 struct diff_cb_arg {
2212 struct got_fileindex *fileindex;
2213 struct got_worktree *worktree;
2214 struct got_repository *repo;
2215 got_worktree_checkout_cb progress_cb;
2217 got_cancel_cb cancel_cb;
2221 static const struct got_error *
2222 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2223 struct got_tree_entry *te, const char *parent_path)
2225 struct diff_cb_arg *a = arg;
2227 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2228 return got_error(GOT_ERR_CANCELLED);
2230 return update_blob(a->worktree, a->fileindex, ie, te,
2231 ie->path, a->repo, a->progress_cb, a->progress_arg);
2234 static const struct got_error *
2235 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2237 struct diff_cb_arg *a = arg;
2239 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2240 return got_error(GOT_ERR_CANCELLED);
2242 return delete_blob(a->worktree, a->fileindex, ie,
2243 a->repo, a->progress_cb, a->progress_arg);
2246 static const struct got_error *
2247 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2249 struct diff_cb_arg *a = arg;
2250 const struct got_error *err;
2253 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2254 return got_error(GOT_ERR_CANCELLED);
2256 if (got_object_tree_entry_is_submodule(te))
2259 if (asprintf(&path, "%s%s%s", parent_path,
2260 parent_path[0] ? "/" : "", te->name)
2262 return got_error_from_errno("asprintf");
2264 if (S_ISDIR(te->mode))
2265 err = add_dir_on_disk(a->worktree, path);
2267 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2268 a->repo, a->progress_cb, a->progress_arg);
2274 const struct got_error *
2275 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2277 uint32_t uuid_status;
2279 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2280 if (uuid_status != uuid_s_ok) {
2282 return got_error_uuid(uuid_status, "uuid_to_string");
2288 static const struct got_error *
2289 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2291 const struct got_error *err = NULL;
2292 char *uuidstr = NULL;
2296 err = got_worktree_get_uuid(&uuidstr, worktree);
2300 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2301 err = got_error_from_errno("asprintf");
2308 const struct got_error *
2309 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2311 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2314 static const struct got_error *
2315 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2317 return get_ref_name(refname, worktree,
2318 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2321 static const struct got_error *
2322 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2327 static const struct got_error *
2328 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2330 return get_ref_name(refname, worktree,
2331 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2334 static const struct got_error *
2335 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2337 return get_ref_name(refname, worktree,
2338 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2341 static const struct got_error *
2342 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2344 return get_ref_name(refname, worktree,
2345 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2348 static const struct got_error *
2349 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2351 return get_ref_name(refname, worktree,
2352 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2355 static const struct got_error *
2356 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2358 return get_ref_name(refname, worktree,
2359 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2362 static const struct got_error *
2363 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2365 return get_ref_name(refname, worktree,
2366 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2369 const struct got_error *
2370 got_worktree_get_histedit_script_path(char **path,
2371 struct got_worktree *worktree)
2373 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2374 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2376 return got_error_from_errno("asprintf");
2382 * Prevent Git's garbage collector from deleting our base commit by
2383 * setting a reference to our base commit's ID.
2385 static const struct got_error *
2386 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2388 const struct got_error *err = NULL;
2389 struct got_reference *ref = NULL;
2392 err = got_worktree_get_base_ref_name(&refname, worktree);
2396 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2400 err = got_ref_write(ref, repo);
2408 static const struct got_error *
2409 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2411 const struct got_error *err = NULL;
2413 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2414 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2415 err = got_error_from_errno("asprintf");
2416 *fileindex_path = NULL;
2422 static const struct got_error *
2423 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2424 struct got_worktree *worktree)
2426 const struct got_error *err = NULL;
2429 *fileindex_path = NULL;
2430 *fileindex = got_fileindex_alloc();
2431 if (*fileindex == NULL)
2432 return got_error_from_errno("got_fileindex_alloc");
2434 err = get_fileindex_path(fileindex_path, worktree);
2438 index = fopen(*fileindex_path, "rb");
2439 if (index == NULL) {
2440 if (errno != ENOENT)
2441 err = got_error_from_errno2("fopen", *fileindex_path);
2443 err = got_fileindex_read(*fileindex, index);
2444 if (fclose(index) == EOF && err == NULL)
2445 err = got_error_from_errno("fclose");
2449 free(*fileindex_path);
2450 *fileindex_path = NULL;
2451 got_fileindex_free(*fileindex);
2457 struct bump_base_commit_id_arg {
2458 struct got_object_id *base_commit_id;
2461 const char *entry_name;
2462 got_worktree_checkout_cb progress_cb;
2466 /* Bump base commit ID of all files within an updated part of the work tree. */
2467 static const struct got_error *
2468 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2470 const struct got_error *err;
2471 struct bump_base_commit_id_arg *a = arg;
2473 if (a->entry_name) {
2474 if (strcmp(ie->path, a->path) != 0)
2476 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2479 if (got_fileindex_entry_was_skipped(ie))
2482 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2483 SHA1_DIGEST_LENGTH) == 0)
2486 if (a->progress_cb) {
2487 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2492 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2496 static const struct got_error *
2497 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2498 struct got_fileindex *fileindex,
2499 got_worktree_checkout_cb progress_cb, void *progress_arg)
2501 struct bump_base_commit_id_arg bbc_arg;
2503 bbc_arg.base_commit_id = worktree->base_commit_id;
2504 bbc_arg.entry_name = NULL;
2506 bbc_arg.path_len = 0;
2507 bbc_arg.progress_cb = progress_cb;
2508 bbc_arg.progress_arg = progress_arg;
2510 return got_fileindex_for_each_entry_safe(fileindex,
2511 bump_base_commit_id, &bbc_arg);
2514 static const struct got_error *
2515 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2517 const struct got_error *err = NULL;
2518 char *new_fileindex_path = NULL;
2519 FILE *new_index = NULL;
2520 struct timespec timeout;
2522 err = got_opentemp_named(&new_fileindex_path, &new_index,
2527 err = got_fileindex_write(fileindex, new_index);
2531 if (rename(new_fileindex_path, fileindex_path) != 0) {
2532 err = got_error_from_errno3("rename", new_fileindex_path,
2534 unlink(new_fileindex_path);
2538 * Sleep for a short amount of time to ensure that files modified after
2539 * this program exits have a different time stamp from the one which
2540 * was recorded in the file index.
2543 timeout.tv_nsec = 1;
2544 nanosleep(&timeout, NULL);
2548 free(new_fileindex_path);
2552 static const struct got_error *
2553 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2554 struct got_object_id **tree_id, const char *wt_relpath,
2555 struct got_worktree *worktree, struct got_repository *repo)
2557 const struct got_error *err = NULL;
2558 struct got_object_id *id = NULL;
2559 char *in_repo_path = NULL;
2560 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2562 *entry_type = GOT_OBJ_TYPE_ANY;
2563 *tree_relpath = NULL;
2566 if (wt_relpath[0] == '\0') {
2567 /* Check out all files within the work tree. */
2568 *entry_type = GOT_OBJ_TYPE_TREE;
2569 *tree_relpath = strdup("");
2570 if (*tree_relpath == NULL) {
2571 err = got_error_from_errno("strdup");
2574 err = got_object_id_by_path(tree_id, repo,
2575 worktree->base_commit_id, worktree->path_prefix);
2581 /* Check out a subset of files in the work tree. */
2583 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2584 is_root_wt ? "" : "/", wt_relpath) == -1) {
2585 err = got_error_from_errno("asprintf");
2589 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2595 in_repo_path = NULL;
2597 err = got_object_get_type(entry_type, repo, id);
2601 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2602 /* Check out a single file. */
2603 if (strchr(wt_relpath, '/') == NULL) {
2604 /* Check out a single file in work tree's root dir. */
2605 in_repo_path = strdup(worktree->path_prefix);
2606 if (in_repo_path == NULL) {
2607 err = got_error_from_errno("strdup");
2610 *tree_relpath = strdup("");
2611 if (*tree_relpath == NULL) {
2612 err = got_error_from_errno("strdup");
2616 /* Check out a single file in a subdirectory. */
2617 err = got_path_dirname(tree_relpath, wt_relpath);
2620 if (asprintf(&in_repo_path, "%s%s%s",
2621 worktree->path_prefix, is_root_wt ? "" : "/",
2622 *tree_relpath) == -1) {
2623 err = got_error_from_errno("asprintf");
2627 err = got_object_id_by_path(tree_id, repo,
2628 worktree->base_commit_id, in_repo_path);
2630 /* Check out all files within a subdirectory. */
2631 *tree_id = got_object_id_dup(id);
2632 if (*tree_id == NULL) {
2633 err = got_error_from_errno("got_object_id_dup");
2636 *tree_relpath = strdup(wt_relpath);
2637 if (*tree_relpath == NULL) {
2638 err = got_error_from_errno("strdup");
2646 *entry_type = GOT_OBJ_TYPE_ANY;
2647 free(*tree_relpath);
2648 *tree_relpath = NULL;
2655 static const struct got_error *
2656 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2657 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2658 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2659 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2661 const struct got_error *err = NULL;
2662 struct got_commit_object *commit = NULL;
2663 struct got_tree_object *tree = NULL;
2664 struct got_fileindex_diff_tree_cb diff_cb;
2665 struct diff_cb_arg arg;
2667 err = ref_base_commit(worktree, repo);
2669 if (!(err->code == GOT_ERR_ERRNO &&
2670 (errno == EACCES || errno == EROFS)))
2672 err = (*progress_cb)(progress_arg,
2673 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2678 err = got_object_open_as_commit(&commit, repo,
2679 worktree->base_commit_id);
2683 err = got_object_open_as_tree(&tree, repo, tree_id);
2688 got_object_tree_find_entry(tree, entry_name) == NULL) {
2689 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2693 diff_cb.diff_old_new = diff_old_new;
2694 diff_cb.diff_old = diff_old;
2695 diff_cb.diff_new = diff_new;
2696 arg.fileindex = fileindex;
2697 arg.worktree = worktree;
2699 arg.progress_cb = progress_cb;
2700 arg.progress_arg = progress_arg;
2701 arg.cancel_cb = cancel_cb;
2702 arg.cancel_arg = cancel_arg;
2703 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2704 entry_name, repo, &diff_cb, &arg);
2707 got_object_tree_close(tree);
2709 got_object_commit_close(commit);
2713 const struct got_error *
2714 got_worktree_checkout_files(struct got_worktree *worktree,
2715 struct got_pathlist_head *paths, struct got_repository *repo,
2716 got_worktree_checkout_cb progress_cb, void *progress_arg,
2717 got_cancel_cb cancel_cb, void *cancel_arg)
2719 const struct got_error *err = NULL, *sync_err, *unlockerr;
2720 struct got_commit_object *commit = NULL;
2721 struct got_tree_object *tree = NULL;
2722 struct got_fileindex *fileindex = NULL;
2723 char *fileindex_path = NULL;
2724 struct got_pathlist_entry *pe;
2725 struct tree_path_data {
2726 STAILQ_ENTRY(tree_path_data) entry;
2727 struct got_object_id *tree_id;
2732 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2734 STAILQ_INIT(&tree_paths);
2736 err = lock_worktree(worktree, LOCK_EX);
2740 /* Map all specified paths to in-repository trees. */
2741 TAILQ_FOREACH(pe, paths, entry) {
2742 tpd = malloc(sizeof(*tpd));
2744 err = got_error_from_errno("malloc");
2748 err = find_tree_entry_for_checkout(&tpd->entry_type,
2749 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2755 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2756 err = got_path_basename(&tpd->entry_name, pe->path);
2764 tpd->entry_name = NULL;
2766 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2770 * Read the file index.
2771 * Checking out files is supposed to be an idempotent operation.
2772 * If the on-disk file index is incomplete we will try to complete it.
2774 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2778 tpd = STAILQ_FIRST(&tree_paths);
2779 TAILQ_FOREACH(pe, paths, entry) {
2780 struct bump_base_commit_id_arg bbc_arg;
2782 err = checkout_files(worktree, fileindex, tpd->relpath,
2783 tpd->tree_id, tpd->entry_name, repo,
2784 progress_cb, progress_arg, cancel_cb, cancel_arg);
2788 bbc_arg.base_commit_id = worktree->base_commit_id;
2789 bbc_arg.entry_name = tpd->entry_name;
2790 bbc_arg.path = pe->path;
2791 bbc_arg.path_len = pe->path_len;
2792 bbc_arg.progress_cb = progress_cb;
2793 bbc_arg.progress_arg = progress_arg;
2794 err = got_fileindex_for_each_entry_safe(fileindex,
2795 bump_base_commit_id, &bbc_arg);
2799 tpd = STAILQ_NEXT(tpd, entry);
2801 sync_err = sync_fileindex(fileindex, fileindex_path);
2802 if (sync_err && err == NULL)
2805 free(fileindex_path);
2807 got_object_tree_close(tree);
2809 got_object_commit_close(commit);
2811 got_fileindex_free(fileindex);
2812 while (!STAILQ_EMPTY(&tree_paths)) {
2813 tpd = STAILQ_FIRST(&tree_paths);
2814 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2819 unlockerr = lock_worktree(worktree, LOCK_SH);
2820 if (unlockerr && err == NULL)
2825 struct merge_file_cb_arg {
2826 struct got_worktree *worktree;
2827 struct got_fileindex *fileindex;
2828 got_worktree_checkout_cb progress_cb;
2830 got_cancel_cb cancel_cb;
2832 const char *label_orig;
2833 struct got_object_id *commit_id2;
2836 static const struct got_error *
2837 merge_file_cb(void *arg, struct got_blob_object *blob1,
2838 struct got_blob_object *blob2, struct got_object_id *id1,
2839 struct got_object_id *id2, const char *path1, const char *path2,
2840 mode_t mode1, mode_t mode2, struct got_repository *repo)
2842 static const struct got_error *err = NULL;
2843 struct merge_file_cb_arg *a = arg;
2844 struct got_fileindex_entry *ie;
2845 char *ondisk_path = NULL;
2847 unsigned char status;
2848 int local_changes_subsumed;
2849 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2850 char *id_str = NULL, *label_deriv2 = NULL;
2852 if (blob1 && blob2) {
2853 ie = got_fileindex_entry_get(a->fileindex, path2,
2856 return (*a->progress_cb)(a->progress_arg,
2857 GOT_STATUS_MISSING, path2);
2859 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2861 return got_error_from_errno("asprintf");
2863 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2868 if (status == GOT_STATUS_DELETE) {
2869 err = (*a->progress_cb)(a->progress_arg,
2870 GOT_STATUS_MERGE, path2);
2873 if (status != GOT_STATUS_NO_CHANGE &&
2874 status != GOT_STATUS_MODIFY &&
2875 status != GOT_STATUS_CONFLICT &&
2876 status != GOT_STATUS_ADD) {
2877 err = (*a->progress_cb)(a->progress_arg, status, path2);
2881 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2883 err = got_object_blob_read_to_str(&link_target2, blob2);
2886 err = merge_symlink(a->worktree, blob1, ondisk_path,
2887 path2, a->label_orig, link_target2, a->commit_id2,
2888 repo, a->progress_cb, a->progress_arg);
2893 f_orig = got_opentemp();
2894 if (f_orig == NULL) {
2895 err = got_error_from_errno("got_opentemp");
2898 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2903 f_deriv2 = got_opentemp();
2904 if (f_deriv2 == NULL)
2906 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2911 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2913 err = got_error_from_errno2("open",
2917 f_deriv = fdopen(fd, "r");
2918 if (f_deriv == NULL) {
2919 err = got_error_from_errno2("fdopen",
2924 err = got_object_id_str(&id_str, a->commit_id2);
2927 if (asprintf(&label_deriv2, "%s: commit %s",
2928 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2929 err = got_error_from_errno("asprintf");
2932 err = merge_file(&local_changes_subsumed, a->worktree,
2933 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2934 sb.st_mode, a->label_orig, NULL, label_deriv2,
2935 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2936 a->progress_cb, a->progress_arg);
2939 ie = got_fileindex_entry_get(a->fileindex, path1,
2942 return (*a->progress_cb)(a->progress_arg,
2943 GOT_STATUS_MISSING, path1);
2945 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2947 return got_error_from_errno("asprintf");
2949 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2955 case GOT_STATUS_NO_CHANGE:
2956 err = (*a->progress_cb)(a->progress_arg,
2957 GOT_STATUS_DELETE, path1);
2960 err = remove_ondisk_file(a->worktree->root_path, path1);
2964 got_fileindex_entry_mark_deleted_from_disk(ie);
2966 case GOT_STATUS_DELETE:
2967 case GOT_STATUS_MISSING:
2968 err = (*a->progress_cb)(a->progress_arg,
2969 GOT_STATUS_DELETE, path1);
2973 got_fileindex_entry_mark_deleted_from_disk(ie);
2975 case GOT_STATUS_ADD: {
2976 struct got_object_id *id;
2979 * Delete the added file only if its content already
2980 * exists in the repository.
2982 err = got_object_blob_file_create(&id, &blob1_f, path1);
2985 if (got_object_id_cmp(id, id1) == 0) {
2986 err = (*a->progress_cb)(a->progress_arg,
2987 GOT_STATUS_DELETE, path1);
2990 err = remove_ondisk_file(a->worktree->root_path,
2995 got_fileindex_entry_remove(a->fileindex,
2998 err = (*a->progress_cb)(a->progress_arg,
2999 GOT_STATUS_CANNOT_DELETE, path1);
3001 if (fclose(blob1_f) == EOF && err == NULL)
3002 err = got_error_from_errno("fclose");
3008 case GOT_STATUS_MODIFY:
3009 case GOT_STATUS_CONFLICT:
3010 err = (*a->progress_cb)(a->progress_arg,
3011 GOT_STATUS_CANNOT_DELETE, path1);
3015 case GOT_STATUS_OBSTRUCTED:
3016 err = (*a->progress_cb)(a->progress_arg, status, path1);
3024 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3026 return got_error_from_errno("asprintf");
3027 ie = got_fileindex_entry_get(a->fileindex, path2,
3030 err = get_file_status(&status, &sb, ie, ondisk_path,
3034 if (status != GOT_STATUS_NO_CHANGE &&
3035 status != GOT_STATUS_MODIFY &&
3036 status != GOT_STATUS_CONFLICT &&
3037 status != GOT_STATUS_ADD) {
3038 err = (*a->progress_cb)(a->progress_arg,
3042 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3044 err = got_object_blob_read_to_str(&link_target2,
3048 err = merge_symlink(a->worktree, NULL,
3049 ondisk_path, path2, a->label_orig,
3050 link_target2, a->commit_id2, repo,
3051 a->progress_cb, a->progress_arg);
3053 } else if (S_ISREG(sb.st_mode)) {
3054 err = merge_blob(&local_changes_subsumed,
3055 a->worktree, NULL, ondisk_path, path2,
3056 sb.st_mode, a->label_orig, blob2,
3057 a->commit_id2, repo, a->progress_cb,
3060 err = got_error_path(ondisk_path,
3061 GOT_ERR_FILE_OBSTRUCTED);
3065 if (status == GOT_STATUS_DELETE) {
3066 err = got_fileindex_entry_update(ie,
3067 a->worktree->root_fd, path2, blob2->id.sha1,
3068 a->worktree->base_commit_id->sha1, 0);
3073 int is_bad_symlink = 0;
3074 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3075 if (S_ISLNK(mode2)) {
3076 err = install_symlink(&is_bad_symlink,
3077 a->worktree, ondisk_path, path2, blob2, 0,
3078 0, 1, repo, a->progress_cb, a->progress_arg);
3080 err = install_blob(a->worktree, ondisk_path, path2,
3081 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3082 a->progress_cb, a->progress_arg);
3086 err = got_fileindex_entry_alloc(&ie, path2);
3089 err = got_fileindex_entry_update(ie,
3090 a->worktree->root_fd, path2, NULL, NULL, 1);
3092 got_fileindex_entry_free(ie);
3095 err = got_fileindex_entry_add(a->fileindex, ie);
3097 got_fileindex_entry_free(ie);
3100 if (is_bad_symlink) {
3101 got_fileindex_entry_filetype_set(ie,
3102 GOT_FILEIDX_MODE_BAD_SYMLINK);
3107 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3108 err = got_error_from_errno("fclose");
3109 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3110 err = got_error_from_errno("fclose");
3111 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3112 err = got_error_from_errno("fclose");
3119 static const struct got_error *
3120 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3122 struct got_worktree *worktree = arg;
3124 /* Reject merges into a work tree with mixed base commits. */
3125 if (got_fileindex_entry_has_commit(ie) &&
3126 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3127 SHA1_DIGEST_LENGTH) != 0)
3128 return got_error(GOT_ERR_MIXED_COMMITS);
3133 struct check_merge_conflicts_arg {
3134 struct got_worktree *worktree;
3135 struct got_fileindex *fileindex;
3136 struct got_repository *repo;
3139 static const struct got_error *
3140 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3141 struct got_blob_object *blob2, struct got_object_id *id1,
3142 struct got_object_id *id2, const char *path1, const char *path2,
3143 mode_t mode1, mode_t mode2, struct got_repository *repo)
3145 const struct got_error *err = NULL;
3146 struct check_merge_conflicts_arg *a = arg;
3147 unsigned char status;
3149 struct got_fileindex_entry *ie;
3150 const char *path = path2 ? path2 : path1;
3151 struct got_object_id *id = id2 ? id2 : id1;
3157 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3161 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3163 return got_error_from_errno("asprintf");
3165 /* Reject merges into a work tree with conflicted files. */
3166 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3170 if (status == GOT_STATUS_CONFLICT)
3171 return got_error(GOT_ERR_CONFLICTS);
3176 static const struct got_error *
3177 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3178 const char *fileindex_path, struct got_object_id *commit_id1,
3179 struct got_object_id *commit_id2, struct got_repository *repo,
3180 got_worktree_checkout_cb progress_cb, void *progress_arg,
3181 got_cancel_cb cancel_cb, void *cancel_arg)
3183 const struct got_error *err = NULL, *sync_err;
3184 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3185 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3186 struct check_merge_conflicts_arg cmc_arg;
3187 struct merge_file_cb_arg arg;
3188 char *label_orig = NULL;
3191 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3192 worktree->path_prefix);
3193 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3199 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3203 err = got_object_id_str(&id_str, commit_id1);
3207 if (asprintf(&label_orig, "%s: commit %s",
3208 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3209 err = got_error_from_errno("asprintf");
3216 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3217 worktree->path_prefix);
3221 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3225 cmc_arg.worktree = worktree;
3226 cmc_arg.fileindex = fileindex;
3227 cmc_arg.repo = repo;
3228 err = got_diff_tree(tree1, tree2, "", "", repo,
3229 check_merge_conflicts, &cmc_arg, 0);
3233 arg.worktree = worktree;
3234 arg.fileindex = fileindex;
3235 arg.progress_cb = progress_cb;
3236 arg.progress_arg = progress_arg;
3237 arg.cancel_cb = cancel_cb;
3238 arg.cancel_arg = cancel_arg;
3239 arg.label_orig = label_orig;
3240 arg.commit_id2 = commit_id2;
3241 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3242 sync_err = sync_fileindex(fileindex, fileindex_path);
3243 if (sync_err && err == NULL)
3247 got_object_tree_close(tree1);
3249 got_object_tree_close(tree2);
3254 const struct got_error *
3255 got_worktree_merge_files(struct got_worktree *worktree,
3256 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3257 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3258 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3260 const struct got_error *err, *unlockerr;
3261 char *fileindex_path = NULL;
3262 struct got_fileindex *fileindex = NULL;
3264 err = lock_worktree(worktree, LOCK_EX);
3268 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3272 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3277 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3278 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3281 got_fileindex_free(fileindex);
3282 free(fileindex_path);
3283 unlockerr = lock_worktree(worktree, LOCK_SH);
3284 if (unlockerr && err == NULL)
3289 struct diff_dir_cb_arg {
3290 struct got_fileindex *fileindex;
3291 struct got_worktree *worktree;
3292 const char *status_path;
3293 size_t status_path_len;
3294 struct got_repository *repo;
3295 got_worktree_status_cb status_cb;
3297 got_cancel_cb cancel_cb;
3299 /* A pathlist containing per-directory pathlists of ignore patterns. */
3300 struct got_pathlist_head *ignores;
3301 int report_unchanged;
3305 static const struct got_error *
3306 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3307 int dirfd, const char *de_name,
3308 got_worktree_status_cb status_cb, void *status_arg,
3309 struct got_repository *repo, int report_unchanged)
3311 const struct got_error *err = NULL;
3312 unsigned char status = GOT_STATUS_NO_CHANGE;
3313 unsigned char staged_status = get_staged_status(ie);
3315 struct got_object_id blob_id, commit_id, staged_blob_id;
3316 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3317 struct got_object_id *staged_blob_idp = NULL;
3319 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3323 if (status == GOT_STATUS_NO_CHANGE &&
3324 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3327 if (got_fileindex_entry_has_blob(ie)) {
3328 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3329 blob_idp = &blob_id;
3331 if (got_fileindex_entry_has_commit(ie)) {
3332 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3333 commit_idp = &commit_id;
3335 if (staged_status == GOT_STATUS_ADD ||
3336 staged_status == GOT_STATUS_MODIFY) {
3337 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3338 SHA1_DIGEST_LENGTH);
3339 staged_blob_idp = &staged_blob_id;
3342 return (*status_cb)(status_arg, status, staged_status,
3343 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3346 static const struct got_error *
3347 status_old_new(void *arg, struct got_fileindex_entry *ie,
3348 struct dirent *de, const char *parent_path, int dirfd)
3350 const struct got_error *err = NULL;
3351 struct diff_dir_cb_arg *a = arg;
3354 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3355 return got_error(GOT_ERR_CANCELLED);
3357 if (got_path_cmp(parent_path, a->status_path,
3358 strlen(parent_path), a->status_path_len) != 0 &&
3359 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3362 if (parent_path[0]) {
3363 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3364 parent_path, de->d_name) == -1)
3365 return got_error_from_errno("asprintf");
3367 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3369 return got_error_from_errno("asprintf");
3372 err = report_file_status(ie, abspath, dirfd, de->d_name,
3373 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3378 static const struct got_error *
3379 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3381 struct diff_dir_cb_arg *a = arg;
3382 struct got_object_id blob_id, commit_id;
3383 unsigned char status;
3385 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3386 return got_error(GOT_ERR_CANCELLED);
3388 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3391 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3392 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3393 if (got_fileindex_entry_has_file_on_disk(ie))
3394 status = GOT_STATUS_MISSING;
3396 status = GOT_STATUS_DELETE;
3397 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3398 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3402 free_ignorelist(struct got_pathlist_head *ignorelist)
3404 struct got_pathlist_entry *pe;
3406 TAILQ_FOREACH(pe, ignorelist, entry)
3407 free((char *)pe->path);
3408 got_pathlist_free(ignorelist);
3412 free_ignores(struct got_pathlist_head *ignores)
3414 struct got_pathlist_entry *pe;
3416 TAILQ_FOREACH(pe, ignores, entry) {
3417 struct got_pathlist_head *ignorelist = pe->data;
3418 free_ignorelist(ignorelist);
3419 free((char *)pe->path);
3421 got_pathlist_free(ignores);
3424 static const struct got_error *
3425 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3427 const struct got_error *err = NULL;
3428 struct got_pathlist_entry *pe = NULL;
3429 struct got_pathlist_head *ignorelist;
3430 char *line = NULL, *pattern, *dirpath = NULL;
3431 size_t linesize = 0;
3434 ignorelist = calloc(1, sizeof(*ignorelist));
3435 if (ignorelist == NULL)
3436 return got_error_from_errno("calloc");
3437 TAILQ_INIT(ignorelist);
3439 while ((linelen = getline(&line, &linesize, f)) != -1) {
3440 if (linelen > 0 && line[linelen - 1] == '\n')
3441 line[linelen - 1] = '\0';
3443 /* Git's ignores may contain comments. */
3447 /* Git's negated patterns are not (yet?) supported. */
3451 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3453 err = got_error_from_errno("asprintf");
3456 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3461 err = got_error_from_errno("getline");
3465 dirpath = strdup(path);
3466 if (dirpath == NULL) {
3467 err = got_error_from_errno("strdup");
3470 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3473 if (err || pe == NULL) {
3475 free_ignorelist(ignorelist);
3481 match_ignores(struct got_pathlist_head *ignores, const char *path)
3483 struct got_pathlist_entry *pe;
3485 /* Handle patterns which match in all directories. */
3486 TAILQ_FOREACH(pe, ignores, entry) {
3487 struct got_pathlist_head *ignorelist = pe->data;
3488 struct got_pathlist_entry *pi;
3490 TAILQ_FOREACH(pi, ignorelist, entry) {
3491 const char *p, *pattern = pi->path;
3493 if (strncmp(pattern, "**/", 3) != 0)
3498 if (fnmatch(pattern, p,
3499 FNM_PATHNAME | FNM_LEADING_DIR)) {
3500 /* Retry in next directory. */
3501 while (*p && *p != '/')
3513 * The ignores pathlist contains ignore lists from children before
3514 * parents, so we can find the most specific ignorelist by walking
3515 * ignores backwards.
3517 pe = TAILQ_LAST(ignores, got_pathlist_head);
3519 if (got_path_is_child(path, pe->path, pe->path_len)) {
3520 struct got_pathlist_head *ignorelist = pe->data;
3521 struct got_pathlist_entry *pi;
3522 TAILQ_FOREACH(pi, ignorelist, entry) {
3523 const char *pattern = pi->path;
3524 int flags = FNM_LEADING_DIR;
3525 if (strstr(pattern, "/**/") == NULL)
3526 flags |= FNM_PATHNAME;
3527 if (fnmatch(pattern, path, flags))
3532 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3538 static const struct got_error *
3539 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3540 const char *path, int dirfd, const char *ignores_filename)
3542 const struct got_error *err = NULL;
3545 FILE *ignoresfile = NULL;
3547 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3548 path[0] ? "/" : "", ignores_filename) == -1)
3549 return got_error_from_errno("asprintf");
3552 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3554 if (errno != ENOENT && errno != EACCES)
3555 err = got_error_from_errno2("openat",
3558 ignoresfile = fdopen(fd, "r");
3559 if (ignoresfile == NULL)
3560 err = got_error_from_errno2("fdopen",
3564 err = read_ignores(ignores, path, ignoresfile);
3568 ignoresfile = fopen(ignorespath, "r");
3569 if (ignoresfile == NULL) {
3570 if (errno != ENOENT && errno != EACCES)
3571 err = got_error_from_errno2("fopen",
3574 err = read_ignores(ignores, path, ignoresfile);
3577 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3578 err = got_error_from_errno2("fclose", path);
3579 if (fd != -1 && close(fd) == -1 && err == NULL)
3580 err = got_error_from_errno2("close", path);
3585 static const struct got_error *
3586 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3588 const struct got_error *err = NULL;
3589 struct diff_dir_cb_arg *a = arg;
3592 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3593 return got_error(GOT_ERR_CANCELLED);
3595 if (parent_path[0]) {
3596 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3597 return got_error_from_errno("asprintf");
3602 if (de->d_type != DT_DIR &&
3603 got_path_is_child(path, a->status_path, a->status_path_len)
3604 && !match_ignores(a->ignores, path))
3605 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3606 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3612 static const struct got_error *
3613 status_traverse(void *arg, const char *path, int dirfd)
3615 const struct got_error *err = NULL;
3616 struct diff_dir_cb_arg *a = arg;
3621 err = add_ignores(a->ignores, a->worktree->root_path,
3622 path, dirfd, ".cvsignore");
3626 err = add_ignores(a->ignores, a->worktree->root_path, path,
3627 dirfd, ".gitignore");
3632 static const struct got_error *
3633 report_single_file_status(const char *path, const char *ondisk_path,
3634 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3635 void *status_arg, struct got_repository *repo, int report_unchanged,
3636 struct got_pathlist_head *ignores, int no_ignores)
3638 struct got_fileindex_entry *ie;
3641 if (!no_ignores && match_ignores(ignores, path))
3644 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3646 return report_file_status(ie, ondisk_path, -1, NULL,
3647 status_cb, status_arg, repo, report_unchanged);
3649 if (lstat(ondisk_path, &sb) == -1) {
3650 if (errno != ENOENT)
3651 return got_error_from_errno2("lstat", ondisk_path);
3652 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3653 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3657 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3658 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3659 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3664 static const struct got_error *
3665 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3666 const char *root_path, const char *path)
3668 const struct got_error *err;
3669 char *parent_path, *next_parent_path = NULL;
3671 err = add_ignores(ignores, root_path, "", -1,
3676 err = add_ignores(ignores, root_path, "", -1,
3681 err = got_path_dirname(&parent_path, path);
3683 if (err->code == GOT_ERR_BAD_PATH)
3684 return NULL; /* cannot traverse parent */
3688 err = add_ignores(ignores, root_path, parent_path, -1,
3692 err = add_ignores(ignores, root_path, parent_path, -1,
3696 err = got_path_dirname(&next_parent_path, parent_path);
3698 if (err->code == GOT_ERR_BAD_PATH)
3699 err = NULL; /* traversed everything */
3702 if (got_path_is_root_dir(parent_path))
3705 parent_path = next_parent_path;
3706 next_parent_path = NULL;
3710 free(next_parent_path);
3714 static const struct got_error *
3715 worktree_status(struct got_worktree *worktree, const char *path,
3716 struct got_fileindex *fileindex, struct got_repository *repo,
3717 got_worktree_status_cb status_cb, void *status_arg,
3718 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3719 int report_unchanged)
3721 const struct got_error *err = NULL;
3723 struct got_fileindex_diff_dir_cb fdiff_cb;
3724 struct diff_dir_cb_arg arg;
3725 char *ondisk_path = NULL;
3726 struct got_pathlist_head ignores;
3728 TAILQ_INIT(&ignores);
3730 if (asprintf(&ondisk_path, "%s%s%s",
3731 worktree->root_path, path[0] ? "/" : "", path) == -1)
3732 return got_error_from_errno("asprintf");
3734 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3736 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3738 err = got_error_from_errno2("open", ondisk_path);
3741 err = add_ignores_from_parent_paths(&ignores,
3742 worktree->root_path, ondisk_path);
3746 err = report_single_file_status(path, ondisk_path,
3747 fileindex, status_cb, status_arg, repo,
3748 report_unchanged, &ignores, no_ignores);
3751 fdiff_cb.diff_old_new = status_old_new;
3752 fdiff_cb.diff_old = status_old;
3753 fdiff_cb.diff_new = status_new;
3754 fdiff_cb.diff_traverse = status_traverse;
3755 arg.fileindex = fileindex;
3756 arg.worktree = worktree;
3757 arg.status_path = path;
3758 arg.status_path_len = strlen(path);
3760 arg.status_cb = status_cb;
3761 arg.status_arg = status_arg;
3762 arg.cancel_cb = cancel_cb;
3763 arg.cancel_arg = cancel_arg;
3764 arg.report_unchanged = report_unchanged;
3765 arg.no_ignores = no_ignores;
3767 err = add_ignores_from_parent_paths(&ignores,
3768 worktree->root_path, path);
3772 arg.ignores = &ignores;
3773 err = got_fileindex_diff_dir(fileindex, fd,
3774 worktree->root_path, path, repo, &fdiff_cb, &arg);
3777 free_ignores(&ignores);
3778 if (fd != -1 && close(fd) == -1 && err == NULL)
3779 err = got_error_from_errno("close");
3784 const struct got_error *
3785 got_worktree_status(struct got_worktree *worktree,
3786 struct got_pathlist_head *paths, struct got_repository *repo,
3787 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3788 got_cancel_cb cancel_cb, void *cancel_arg)
3790 const struct got_error *err = NULL;
3791 char *fileindex_path = NULL;
3792 struct got_fileindex *fileindex = NULL;
3793 struct got_pathlist_entry *pe;
3795 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3799 TAILQ_FOREACH(pe, paths, entry) {
3800 err = worktree_status(worktree, pe->path, fileindex, repo,
3801 status_cb, status_arg, cancel_cb, cancel_arg,
3806 free(fileindex_path);
3807 got_fileindex_free(fileindex);
3811 const struct got_error *
3812 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3815 const struct got_error *err = NULL;
3816 char *resolved = NULL, *cwd = NULL, *path = NULL;
3819 char *abspath = NULL;
3820 char canonpath[PATH_MAX];
3824 cwd = getcwd(NULL, 0);
3826 return got_error_from_errno("getcwd");
3828 if (lstat(arg, &sb) == -1) {
3829 if (errno != ENOENT) {
3830 err = got_error_from_errno2("lstat", arg);
3835 if (S_ISLNK(sb.st_mode)) {
3837 * We cannot use realpath(3) with symlinks since we want to
3838 * operate on the symlink itself.
3839 * But we can make the path absolute, assuming it is relative
3840 * to the current working directory, and then canonicalize it.
3842 if (!got_path_is_absolute(arg)) {
3843 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3844 err = got_error_from_errno("asprintf");
3849 err = got_canonpath(abspath ? abspath : arg, canonpath,
3853 resolved = strdup(canonpath);
3854 if (resolved == NULL) {
3855 err = got_error_from_errno("strdup");
3859 resolved = realpath(arg, NULL);
3860 if (resolved == NULL) {
3861 if (errno != ENOENT) {
3862 err = got_error_from_errno2("realpath", arg);
3865 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3866 err = got_error_from_errno("asprintf");
3869 err = got_canonpath(abspath, canonpath,
3873 resolved = strdup(canonpath);
3874 if (resolved == NULL) {
3875 err = got_error_from_errno("strdup");
3881 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3882 strlen(got_worktree_get_root_path(worktree)))) {
3883 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3887 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3888 err = got_path_skip_common_ancestor(&path,
3889 got_worktree_get_root_path(worktree), resolved);
3895 err = got_error_from_errno("strdup");
3900 /* XXX status walk can't deal with trailing slash! */
3902 while (len > 0 && path[len - 1] == '/') {
3903 path[len - 1] = '\0';
3917 struct schedule_addition_args {
3918 struct got_worktree *worktree;
3919 struct got_fileindex *fileindex;
3920 got_worktree_checkout_cb progress_cb;
3922 struct got_repository *repo;
3925 static const struct got_error *
3926 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3927 const char *relpath, struct got_object_id *blob_id,
3928 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3929 int dirfd, const char *de_name)
3931 struct schedule_addition_args *a = arg;
3932 const struct got_error *err = NULL;
3933 struct got_fileindex_entry *ie;
3937 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3939 return got_error_from_errno("asprintf");
3941 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3943 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3947 /* Re-adding an existing entry is a no-op. */
3948 if (status == GOT_STATUS_ADD)
3950 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3955 if (status != GOT_STATUS_UNVERSIONED) {
3956 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3960 err = got_fileindex_entry_alloc(&ie, relpath);
3963 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3964 relpath, NULL, NULL, 1);
3966 got_fileindex_entry_free(ie);
3969 err = got_fileindex_entry_add(a->fileindex, ie);
3971 got_fileindex_entry_free(ie);
3978 if (status == GOT_STATUS_ADD)
3980 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3983 const struct got_error *
3984 got_worktree_schedule_add(struct got_worktree *worktree,
3985 struct got_pathlist_head *paths,
3986 got_worktree_checkout_cb progress_cb, void *progress_arg,
3987 struct got_repository *repo, int no_ignores)
3989 struct got_fileindex *fileindex = NULL;
3990 char *fileindex_path = NULL;
3991 const struct got_error *err = NULL, *sync_err, *unlockerr;
3992 struct got_pathlist_entry *pe;
3993 struct schedule_addition_args saa;
3995 err = lock_worktree(worktree, LOCK_EX);
3999 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4003 saa.worktree = worktree;
4004 saa.fileindex = fileindex;
4005 saa.progress_cb = progress_cb;
4006 saa.progress_arg = progress_arg;
4009 TAILQ_FOREACH(pe, paths, entry) {
4010 err = worktree_status(worktree, pe->path, fileindex, repo,
4011 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4015 sync_err = sync_fileindex(fileindex, fileindex_path);
4016 if (sync_err && err == NULL)
4019 free(fileindex_path);
4021 got_fileindex_free(fileindex);
4022 unlockerr = lock_worktree(worktree, LOCK_SH);
4023 if (unlockerr && err == NULL)
4028 struct schedule_deletion_args {
4029 struct got_worktree *worktree;
4030 struct got_fileindex *fileindex;
4031 got_worktree_delete_cb progress_cb;
4033 struct got_repository *repo;
4034 int delete_local_mods;
4036 const char *status_codes;
4039 static const struct got_error *
4040 schedule_for_deletion(void *arg, unsigned char status,
4041 unsigned char staged_status, const char *relpath,
4042 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4043 struct got_object_id *commit_id, int dirfd, const char *de_name)
4045 struct schedule_deletion_args *a = arg;
4046 const struct got_error *err = NULL;
4047 struct got_fileindex_entry *ie = NULL;
4051 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4053 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4055 staged_status = get_staged_status(ie);
4056 if (staged_status != GOT_STATUS_NO_CHANGE) {
4057 if (staged_status == GOT_STATUS_DELETE)
4059 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4062 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4064 return got_error_from_errno("asprintf");
4066 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4071 if (a->status_codes) {
4072 size_t ncodes = strlen(a->status_codes);
4074 for (i = 0; i < ncodes ; i++) {
4075 if (status == a->status_codes[i])
4079 /* Do not delete files in non-matching status. */
4083 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4084 a->status_codes[i] != GOT_STATUS_MISSING) {
4085 static char msg[64];
4086 snprintf(msg, sizeof(msg),
4087 "invalid status code '%c'", a->status_codes[i]);
4088 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4093 if (status != GOT_STATUS_NO_CHANGE) {
4094 if (status == GOT_STATUS_DELETE)
4096 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4097 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4100 if (status != GOT_STATUS_MODIFY &&
4101 status != GOT_STATUS_MISSING) {
4102 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4107 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4111 if (unlinkat(dirfd, de_name, 0) != 0) {
4112 err = got_error_from_errno2("unlinkat",
4116 } else if (unlink(ondisk_path) != 0) {
4117 err = got_error_from_errno2("unlink", ondisk_path);
4121 root_len = strlen(a->worktree->root_path);
4124 err = got_path_dirname(&parent, ondisk_path);
4128 ondisk_path = parent;
4129 if (rmdir(ondisk_path) == -1) {
4130 if (errno != ENOTEMPTY)
4131 err = got_error_from_errno2("rmdir",
4135 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4136 strlen(ondisk_path), root_len) != 0);
4139 got_fileindex_entry_mark_deleted_from_disk(ie);
4144 if (status == GOT_STATUS_DELETE)
4146 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4147 staged_status, relpath);
4150 const struct got_error *
4151 got_worktree_schedule_delete(struct got_worktree *worktree,
4152 struct got_pathlist_head *paths, int delete_local_mods,
4153 const char *status_codes,
4154 got_worktree_delete_cb progress_cb, void *progress_arg,
4155 struct got_repository *repo, int keep_on_disk)
4157 struct got_fileindex *fileindex = NULL;
4158 char *fileindex_path = NULL;
4159 const struct got_error *err = NULL, *sync_err, *unlockerr;
4160 struct got_pathlist_entry *pe;
4161 struct schedule_deletion_args sda;
4163 err = lock_worktree(worktree, LOCK_EX);
4167 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4171 sda.worktree = worktree;
4172 sda.fileindex = fileindex;
4173 sda.progress_cb = progress_cb;
4174 sda.progress_arg = progress_arg;
4176 sda.delete_local_mods = delete_local_mods;
4177 sda.keep_on_disk = keep_on_disk;
4178 sda.status_codes = status_codes;
4180 TAILQ_FOREACH(pe, paths, entry) {
4181 err = worktree_status(worktree, pe->path, fileindex, repo,
4182 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4186 sync_err = sync_fileindex(fileindex, fileindex_path);
4187 if (sync_err && err == NULL)
4190 free(fileindex_path);
4192 got_fileindex_free(fileindex);
4193 unlockerr = lock_worktree(worktree, LOCK_SH);
4194 if (unlockerr && err == NULL)
4199 static const struct got_error *
4200 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4202 const struct got_error *err = NULL;
4204 size_t linesize = 0, n;
4207 linelen = getline(&line, &linesize, infile);
4208 if (linelen == -1) {
4209 if (ferror(infile)) {
4210 err = got_error_from_errno("getline");
4216 n = fwrite(line, 1, linelen, outfile);
4218 err = got_ferror(outfile, GOT_ERR_IO);
4223 n = fwrite(line, 1, linelen, rejectfile);
4225 err = got_ferror(outfile, GOT_ERR_IO);
4232 static const struct got_error *
4233 skip_one_line(FILE *f)
4236 size_t linesize = 0;
4239 linelen = getline(&line, &linesize, f);
4240 if (linelen == -1) {
4242 return got_error_from_errno("getline");
4249 static const struct got_error *
4250 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4251 int start_old, int end_old, int start_new, int end_new,
4252 FILE *outfile, FILE *rejectfile)
4254 const struct got_error *err;
4256 /* Copy old file's lines leading up to patch. */
4257 while (!feof(f1) && *line_cur1 < start_old) {
4258 err = copy_one_line(f1, outfile, NULL);
4263 /* Skip new file's lines leading up to patch. */
4264 while (!feof(f2) && *line_cur2 < start_new) {
4266 err = copy_one_line(f2, NULL, rejectfile);
4268 err = skip_one_line(f2);
4273 /* Copy patched lines. */
4274 while (!feof(f2) && *line_cur2 <= end_new) {
4275 err = copy_one_line(f2, outfile, NULL);
4280 /* Skip over old file's replaced lines. */
4281 while (!feof(f1) && *line_cur1 <= end_old) {
4283 err = copy_one_line(f1, NULL, rejectfile);
4285 err = skip_one_line(f1);
4294 static const struct got_error *
4295 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4296 FILE *outfile, FILE *rejectfile)
4298 const struct got_error *err;
4301 /* Copy old file's lines until EOF. */
4303 err = copy_one_line(f1, outfile, NULL);
4310 /* Copy new file's lines until EOF. */
4312 err = copy_one_line(f2, NULL, rejectfile);
4322 static const struct got_error *
4323 apply_or_reject_change(int *choice, int *nchunks_used,
4324 struct diff_result *diff_result, int n,
4325 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4326 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4327 got_worktree_patch_cb patch_cb, void *patch_arg)
4329 const struct got_error *err = NULL;
4330 struct diff_chunk_context cc = {};
4331 int start_old, end_old, start_new, end_new;
4333 struct diff_output_unidiff_state *diff_state;
4334 struct diff_input_info diff_info;
4337 *choice = GOT_PATCH_CHOICE_NONE;
4339 /* Get changed line numbers without context lines for copy_change(). */
4340 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4341 start_old = cc.left.start;
4342 end_old = cc.left.end;
4343 start_new = cc.right.start;
4344 end_new = cc.right.end;
4346 /* Get the same change with context lines for display. */
4347 memset(&cc, 0, sizeof(cc));
4348 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4350 memset(&diff_info, 0, sizeof(diff_info));
4351 diff_info.left_path = relpath;
4352 diff_info.right_path = relpath;
4354 diff_state = diff_output_unidiff_state_alloc();
4355 if (diff_state == NULL)
4356 return got_error_set_errno(ENOMEM,
4357 "diff_output_unidiff_state_alloc");
4359 hunkfile = got_opentemp();
4360 if (hunkfile == NULL) {
4361 err = got_error_from_errno("got_opentemp");
4365 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4367 if (rc != DIFF_RC_OK) {
4368 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4372 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4373 err = got_ferror(hunkfile, GOT_ERR_IO);
4377 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4378 hunkfile, changeno, nchanges);
4383 case GOT_PATCH_CHOICE_YES:
4384 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4385 end_old, start_new, end_new, outfile, rejectfile);
4387 case GOT_PATCH_CHOICE_NO:
4388 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4389 end_old, start_new, end_new, rejectfile, outfile);
4391 case GOT_PATCH_CHOICE_QUIT:
4394 err = got_error(GOT_ERR_PATCH_CHOICE);
4398 diff_output_unidiff_state_free(diff_state);
4399 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4400 err = got_error_from_errno("fclose");
4404 struct revert_file_args {
4405 struct got_worktree *worktree;
4406 struct got_fileindex *fileindex;
4407 got_worktree_checkout_cb progress_cb;
4409 got_worktree_patch_cb patch_cb;
4411 struct got_repository *repo;
4414 static const struct got_error *
4415 create_patched_content(char **path_outfile, int reverse_patch,
4416 struct got_object_id *blob_id, const char *path2,
4417 int dirfd2, const char *de_name2,
4418 const char *relpath, struct got_repository *repo,
4419 got_worktree_patch_cb patch_cb, void *patch_arg)
4421 const struct got_error *err, *free_err;
4422 struct got_blob_object *blob = NULL;
4423 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4425 char link_target[PATH_MAX];
4426 ssize_t link_len = 0;
4427 char *path1 = NULL, *id_str = NULL;
4429 struct got_diffreg_result *diffreg_result = NULL;
4430 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4431 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4433 *path_outfile = NULL;
4435 err = got_object_id_str(&id_str, blob_id);
4440 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4442 if (errno != ELOOP) {
4443 err = got_error_from_errno2("openat", path2);
4446 link_len = readlinkat(dirfd2, de_name2,
4447 link_target, sizeof(link_target));
4449 return got_error_from_errno2("readlinkat", path2);
4450 sb2.st_mode = S_IFLNK;
4451 sb2.st_size = link_len;
4454 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4456 if (errno != ELOOP) {
4457 err = got_error_from_errno2("open", path2);
4460 link_len = readlink(path2, link_target,
4461 sizeof(link_target));
4463 return got_error_from_errno2("readlink", path2);
4464 sb2.st_mode = S_IFLNK;
4465 sb2.st_size = link_len;
4469 if (fstat(fd2, &sb2) == -1) {
4470 err = got_error_from_errno2("fstat", path2);
4474 f2 = fdopen(fd2, "r");
4476 err = got_error_from_errno2("fdopen", path2);
4482 f2 = got_opentemp();
4484 err = got_error_from_errno2("got_opentemp", path2);
4487 n = fwrite(link_target, 1, link_len, f2);
4488 if (n != link_len) {
4489 err = got_ferror(f2, GOT_ERR_IO);
4492 if (fflush(f2) == EOF) {
4493 err = got_error_from_errno("fflush");
4499 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4503 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4507 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4511 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4516 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4520 if (fseek(f1, 0L, SEEK_SET) == -1)
4521 return got_ferror(f1, GOT_ERR_IO);
4522 if (fseek(f2, 0L, SEEK_SET) == -1)
4523 return got_ferror(f2, GOT_ERR_IO);
4525 /* Count the number of actual changes in the diff result. */
4526 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4527 struct diff_chunk_context cc = {};
4528 diff_chunk_context_load_change(&cc, &nchunks_used,
4529 diffreg_result->result, n, 0);
4532 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4534 err = apply_or_reject_change(&choice, &nchunks_used,
4535 diffreg_result->result, n, relpath, f1, f2,
4536 &line_cur1, &line_cur2,
4537 reverse_patch ? NULL : outfile,
4538 reverse_patch ? outfile : NULL,
4539 ++i, nchanges, patch_cb, patch_arg);
4542 if (choice == GOT_PATCH_CHOICE_YES)
4544 else if (choice == GOT_PATCH_CHOICE_QUIT)
4548 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4549 reverse_patch ? NULL : outfile,
4550 reverse_patch ? outfile : NULL);
4554 if (!S_ISLNK(sb2.st_mode)) {
4555 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4556 err = got_error_from_errno2("fchmod", path2);
4564 got_object_blob_close(blob);
4565 free_err = got_diffreg_result_free(diffreg_result);
4568 if (f1 && fclose(f1) == EOF && err == NULL)
4569 err = got_error_from_errno2("fclose", path1);
4570 if (f2 && fclose(f2) == EOF && err == NULL)
4571 err = got_error_from_errno2("fclose", path2);
4572 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4573 err = got_error_from_errno2("close", path2);
4574 if (outfile && fclose(outfile) == EOF && err == NULL)
4575 err = got_error_from_errno2("fclose", *path_outfile);
4576 if (path1 && unlink(path1) == -1 && err == NULL)
4577 err = got_error_from_errno2("unlink", path1);
4578 if (err || !have_content) {
4579 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4580 err = got_error_from_errno2("unlink", *path_outfile);
4581 free(*path_outfile);
4582 *path_outfile = NULL;
4588 static const struct got_error *
4589 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4590 const char *relpath, struct got_object_id *blob_id,
4591 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4592 int dirfd, const char *de_name)
4594 struct revert_file_args *a = arg;
4595 const struct got_error *err = NULL;
4596 char *parent_path = NULL;
4597 struct got_fileindex_entry *ie;
4598 struct got_tree_object *tree = NULL;
4599 struct got_object_id *tree_id = NULL;
4600 const struct got_tree_entry *te = NULL;
4601 char *tree_path = NULL, *te_name;
4602 char *ondisk_path = NULL, *path_content = NULL;
4603 struct got_blob_object *blob = NULL;
4605 /* Reverting a staged deletion is a no-op. */
4606 if (status == GOT_STATUS_DELETE &&
4607 staged_status != GOT_STATUS_NO_CHANGE)
4610 if (status == GOT_STATUS_UNVERSIONED)
4611 return (*a->progress_cb)(a->progress_arg,
4612 GOT_STATUS_UNVERSIONED, relpath);
4614 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4616 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4618 /* Construct in-repository path of tree which contains this blob. */
4619 err = got_path_dirname(&parent_path, ie->path);
4621 if (err->code != GOT_ERR_BAD_PATH)
4623 parent_path = strdup("/");
4624 if (parent_path == NULL) {
4625 err = got_error_from_errno("strdup");
4629 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4630 tree_path = strdup(parent_path);
4631 if (tree_path == NULL) {
4632 err = got_error_from_errno("strdup");
4636 if (got_path_is_root_dir(parent_path)) {
4637 tree_path = strdup(a->worktree->path_prefix);
4638 if (tree_path == NULL) {
4639 err = got_error_from_errno("strdup");
4643 if (asprintf(&tree_path, "%s/%s",
4644 a->worktree->path_prefix, parent_path) == -1) {
4645 err = got_error_from_errno("asprintf");
4651 err = got_object_id_by_path(&tree_id, a->repo,
4652 a->worktree->base_commit_id, tree_path);
4654 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4655 (status == GOT_STATUS_ADD ||
4656 staged_status == GOT_STATUS_ADD)))
4659 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4663 err = got_path_basename(&te_name, ie->path);
4667 te = got_object_tree_find_entry(tree, te_name);
4669 if (te == NULL && status != GOT_STATUS_ADD &&
4670 staged_status != GOT_STATUS_ADD) {
4671 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4677 case GOT_STATUS_ADD:
4679 int choice = GOT_PATCH_CHOICE_NONE;
4680 err = (*a->patch_cb)(&choice, a->patch_arg,
4681 status, ie->path, NULL, 1, 1);
4684 if (choice != GOT_PATCH_CHOICE_YES)
4687 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4691 got_fileindex_entry_remove(a->fileindex, ie);
4693 case GOT_STATUS_DELETE:
4695 int choice = GOT_PATCH_CHOICE_NONE;
4696 err = (*a->patch_cb)(&choice, a->patch_arg,
4697 status, ie->path, NULL, 1, 1);
4700 if (choice != GOT_PATCH_CHOICE_YES)
4704 case GOT_STATUS_MODIFY:
4705 case GOT_STATUS_MODE_CHANGE:
4706 case GOT_STATUS_CONFLICT:
4707 case GOT_STATUS_MISSING: {
4708 struct got_object_id id;
4709 if (staged_status == GOT_STATUS_ADD ||
4710 staged_status == GOT_STATUS_MODIFY) {
4711 memcpy(id.sha1, ie->staged_blob_sha1,
4712 SHA1_DIGEST_LENGTH);
4714 memcpy(id.sha1, ie->blob_sha1,
4715 SHA1_DIGEST_LENGTH);
4716 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4720 if (asprintf(&ondisk_path, "%s/%s",
4721 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4722 err = got_error_from_errno("asprintf");
4726 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4727 status == GOT_STATUS_CONFLICT)) {
4728 int is_bad_symlink = 0;
4729 err = create_patched_content(&path_content, 1, &id,
4730 ondisk_path, dirfd, de_name, ie->path, a->repo,
4731 a->patch_cb, a->patch_arg);
4732 if (err || path_content == NULL)
4734 if (te && S_ISLNK(te->mode)) {
4735 if (unlink(path_content) == -1) {
4736 err = got_error_from_errno2("unlink",
4740 err = install_symlink(&is_bad_symlink,
4741 a->worktree, ondisk_path, ie->path,
4742 blob, 0, 1, 0, a->repo,
4743 a->progress_cb, a->progress_arg);
4745 if (rename(path_content, ondisk_path) == -1) {
4746 err = got_error_from_errno3("rename",
4747 path_content, ondisk_path);
4752 int is_bad_symlink = 0;
4753 if (te && S_ISLNK(te->mode)) {
4754 err = install_symlink(&is_bad_symlink,
4755 a->worktree, ondisk_path, ie->path,
4756 blob, 0, 1, 0, a->repo,
4757 a->progress_cb, a->progress_arg);
4759 err = install_blob(a->worktree, ondisk_path,
4761 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4762 got_fileindex_perms_to_st(ie), blob,
4763 0, 1, 0, 0, a->repo,
4764 a->progress_cb, a->progress_arg);
4768 if (status == GOT_STATUS_DELETE ||
4769 status == GOT_STATUS_MODE_CHANGE) {
4770 err = got_fileindex_entry_update(ie,
4771 a->worktree->root_fd, relpath,
4773 a->worktree->base_commit_id->sha1, 1);
4777 if (is_bad_symlink) {
4778 got_fileindex_entry_filetype_set(ie,
4779 GOT_FILEIDX_MODE_BAD_SYMLINK);
4793 got_object_blob_close(blob);
4795 got_object_tree_close(tree);
4800 const struct got_error *
4801 got_worktree_revert(struct got_worktree *worktree,
4802 struct got_pathlist_head *paths,
4803 got_worktree_checkout_cb progress_cb, void *progress_arg,
4804 got_worktree_patch_cb patch_cb, void *patch_arg,
4805 struct got_repository *repo)
4807 struct got_fileindex *fileindex = NULL;
4808 char *fileindex_path = NULL;
4809 const struct got_error *err = NULL, *unlockerr = NULL;
4810 const struct got_error *sync_err = NULL;
4811 struct got_pathlist_entry *pe;
4812 struct revert_file_args rfa;
4814 err = lock_worktree(worktree, LOCK_EX);
4818 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4822 rfa.worktree = worktree;
4823 rfa.fileindex = fileindex;
4824 rfa.progress_cb = progress_cb;
4825 rfa.progress_arg = progress_arg;
4826 rfa.patch_cb = patch_cb;
4827 rfa.patch_arg = patch_arg;
4829 TAILQ_FOREACH(pe, paths, entry) {
4830 err = worktree_status(worktree, pe->path, fileindex, repo,
4831 revert_file, &rfa, NULL, NULL, 0, 0);
4835 sync_err = sync_fileindex(fileindex, fileindex_path);
4836 if (sync_err && err == NULL)
4839 free(fileindex_path);
4841 got_fileindex_free(fileindex);
4842 unlockerr = lock_worktree(worktree, LOCK_SH);
4843 if (unlockerr && err == NULL)
4849 free_commitable(struct got_commitable *ct)
4852 free(ct->in_repo_path);
4853 free(ct->ondisk_path);
4855 free(ct->base_blob_id);
4856 free(ct->staged_blob_id);
4857 free(ct->base_commit_id);
4861 struct collect_commitables_arg {
4862 struct got_pathlist_head *commitable_paths;
4863 struct got_repository *repo;
4864 struct got_worktree *worktree;
4865 struct got_fileindex *fileindex;
4866 int have_staged_files;
4867 int allow_bad_symlinks;
4870 static const struct got_error *
4871 collect_commitables(void *arg, unsigned char status,
4872 unsigned char staged_status, const char *relpath,
4873 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4874 struct got_object_id *commit_id, int dirfd, const char *de_name)
4876 struct collect_commitables_arg *a = arg;
4877 const struct got_error *err = NULL;
4878 struct got_commitable *ct = NULL;
4879 struct got_pathlist_entry *new = NULL;
4880 char *parent_path = NULL, *path = NULL;
4883 if (a->have_staged_files) {
4884 if (staged_status != GOT_STATUS_MODIFY &&
4885 staged_status != GOT_STATUS_ADD &&
4886 staged_status != GOT_STATUS_DELETE)
4889 if (status == GOT_STATUS_CONFLICT)
4890 return got_error(GOT_ERR_COMMIT_CONFLICT);
4892 if (status != GOT_STATUS_MODIFY &&
4893 status != GOT_STATUS_MODE_CHANGE &&
4894 status != GOT_STATUS_ADD &&
4895 status != GOT_STATUS_DELETE)
4899 if (asprintf(&path, "/%s", relpath) == -1) {
4900 err = got_error_from_errno("asprintf");
4903 if (strcmp(path, "/") == 0) {
4904 parent_path = strdup("");
4905 if (parent_path == NULL)
4906 return got_error_from_errno("strdup");
4908 err = got_path_dirname(&parent_path, path);
4913 ct = calloc(1, sizeof(*ct));
4915 err = got_error_from_errno("calloc");
4919 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4921 err = got_error_from_errno("asprintf");
4925 if (staged_status == GOT_STATUS_ADD ||
4926 staged_status == GOT_STATUS_MODIFY) {
4927 struct got_fileindex_entry *ie;
4928 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4929 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4930 case GOT_FILEIDX_MODE_REGULAR_FILE:
4931 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4934 case GOT_FILEIDX_MODE_SYMLINK:
4938 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4941 ct->mode |= got_fileindex_entry_perms_get(ie);
4942 } else if (status != GOT_STATUS_DELETE &&
4943 staged_status != GOT_STATUS_DELETE) {
4945 if (fstatat(dirfd, de_name, &sb,
4946 AT_SYMLINK_NOFOLLOW) == -1) {
4947 err = got_error_from_errno2("fstatat",
4951 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4952 err = got_error_from_errno2("lstat", ct->ondisk_path);
4955 ct->mode = sb.st_mode;
4958 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4959 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4961 err = got_error_from_errno("asprintf");
4965 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4966 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4968 char target_path[PATH_MAX];
4970 target_len = readlink(ct->ondisk_path, target_path,
4971 sizeof(target_path));
4972 if (target_len == -1) {
4973 err = got_error_from_errno2("readlink",
4977 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4978 target_len, ct->ondisk_path, a->worktree->root_path);
4981 if (is_bad_symlink) {
4982 err = got_error_path(ct->ondisk_path,
4983 GOT_ERR_BAD_SYMLINK);
4989 ct->status = status;
4990 ct->staged_status = staged_status;
4991 ct->blob_id = NULL; /* will be filled in when blob gets created */
4992 if (ct->status != GOT_STATUS_ADD &&
4993 ct->staged_status != GOT_STATUS_ADD) {
4994 ct->base_blob_id = got_object_id_dup(blob_id);
4995 if (ct->base_blob_id == NULL) {
4996 err = got_error_from_errno("got_object_id_dup");
4999 ct->base_commit_id = got_object_id_dup(commit_id);
5000 if (ct->base_commit_id == NULL) {
5001 err = got_error_from_errno("got_object_id_dup");
5005 if (ct->staged_status == GOT_STATUS_ADD ||
5006 ct->staged_status == GOT_STATUS_MODIFY) {
5007 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5008 if (ct->staged_blob_id == NULL) {
5009 err = got_error_from_errno("got_object_id_dup");
5013 ct->path = strdup(path);
5014 if (ct->path == NULL) {
5015 err = got_error_from_errno("strdup");
5018 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5020 if (ct && (err || new == NULL))
5021 free_commitable(ct);
5027 static const struct got_error *write_tree(struct got_object_id **, int *,
5028 struct got_tree_object *, const char *, struct got_pathlist_head *,
5029 got_worktree_status_cb status_cb, void *status_arg,
5030 struct got_repository *);
5032 static const struct got_error *
5033 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5034 struct got_tree_entry *te, const char *parent_path,
5035 struct got_pathlist_head *commitable_paths,
5036 got_worktree_status_cb status_cb, void *status_arg,
5037 struct got_repository *repo)
5039 const struct got_error *err = NULL;
5040 struct got_tree_object *subtree;
5043 if (asprintf(&subpath, "%s%s%s", parent_path,
5044 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5045 return got_error_from_errno("asprintf");
5047 err = got_object_open_as_tree(&subtree, repo, &te->id);
5051 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5052 commitable_paths, status_cb, status_arg, repo);
5053 got_object_tree_close(subtree);
5058 static const struct got_error *
5059 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5061 const struct got_error *err = NULL;
5062 char *ct_parent_path = NULL;
5066 if (strchr(ct->in_repo_path, '/') == NULL) {
5067 *match = got_path_is_root_dir(path);
5071 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5074 *match = (strcmp(path, ct_parent_path) == 0);
5075 free(ct_parent_path);
5080 get_ct_file_mode(struct got_commitable *ct)
5082 if (S_ISLNK(ct->mode))
5085 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5088 static const struct got_error *
5089 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5090 struct got_tree_entry *te, struct got_commitable *ct)
5092 const struct got_error *err = NULL;
5096 err = got_object_tree_entry_dup(new_te, te);
5100 (*new_te)->mode = get_ct_file_mode(ct);
5102 if (ct->staged_status == GOT_STATUS_MODIFY)
5103 memcpy(&(*new_te)->id, ct->staged_blob_id,
5104 sizeof((*new_te)->id));
5106 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5108 if (err && *new_te) {
5115 static const struct got_error *
5116 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5117 struct got_commitable *ct)
5119 const struct got_error *err = NULL;
5120 char *ct_name = NULL;
5124 *new_te = calloc(1, sizeof(**new_te));
5125 if (*new_te == NULL)
5126 return got_error_from_errno("calloc");
5128 err = got_path_basename(&ct_name, ct->path);
5131 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5132 sizeof((*new_te)->name)) {
5133 err = got_error(GOT_ERR_NO_SPACE);
5137 (*new_te)->mode = get_ct_file_mode(ct);
5139 if (ct->staged_status == GOT_STATUS_ADD)
5140 memcpy(&(*new_te)->id, ct->staged_blob_id,
5141 sizeof((*new_te)->id));
5143 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5146 if (err && *new_te) {
5153 static const struct got_error *
5154 insert_tree_entry(struct got_tree_entry *new_te,
5155 struct got_pathlist_head *paths)
5157 const struct got_error *err = NULL;
5158 struct got_pathlist_entry *new_pe;
5160 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5164 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5168 static const struct got_error *
5169 report_ct_status(struct got_commitable *ct,
5170 got_worktree_status_cb status_cb, void *status_arg)
5172 const char *ct_path = ct->path;
5173 unsigned char status;
5175 while (ct_path[0] == '/')
5178 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5179 status = ct->staged_status;
5181 status = ct->status;
5183 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5184 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5187 static const struct got_error *
5188 match_modified_subtree(int *modified, struct got_tree_entry *te,
5189 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5191 const struct got_error *err = NULL;
5192 struct got_pathlist_entry *pe;
5197 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5198 got_path_is_root_dir(base_tree_path) ? "" : "/",
5200 return got_error_from_errno("asprintf");
5202 TAILQ_FOREACH(pe, commitable_paths, entry) {
5203 struct got_commitable *ct = pe->data;
5204 *modified = got_path_is_child(ct->in_repo_path, te_path,
5214 static const struct got_error *
5215 match_deleted_or_modified_ct(struct got_commitable **ctp,
5216 struct got_tree_entry *te, const char *base_tree_path,
5217 struct got_pathlist_head *commitable_paths)
5219 const struct got_error *err = NULL;
5220 struct got_pathlist_entry *pe;
5224 TAILQ_FOREACH(pe, commitable_paths, entry) {
5225 struct got_commitable *ct = pe->data;
5226 char *ct_name = NULL;
5229 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5230 if (ct->status != GOT_STATUS_MODIFY &&
5231 ct->status != GOT_STATUS_MODE_CHANGE &&
5232 ct->status != GOT_STATUS_DELETE)
5235 if (ct->staged_status != GOT_STATUS_MODIFY &&
5236 ct->staged_status != GOT_STATUS_DELETE)
5240 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5243 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5249 err = got_path_basename(&ct_name, pe->path);
5253 if (strcmp(te->name, ct_name) != 0) {
5266 static const struct got_error *
5267 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5268 const char *child_path, const char *path_base_tree,
5269 struct got_pathlist_head *commitable_paths,
5270 got_worktree_status_cb status_cb, void *status_arg,
5271 struct got_repository *repo)
5273 const struct got_error *err = NULL;
5274 struct got_tree_entry *new_te;
5276 struct got_object_id *id = NULL;
5281 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5282 got_path_is_root_dir(path_base_tree) ? "" : "/",
5284 return got_error_from_errno("asprintf");
5286 new_te = calloc(1, sizeof(*new_te));
5288 return got_error_from_errno("calloc");
5289 new_te->mode = S_IFDIR;
5291 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5292 sizeof(new_te->name)) {
5293 err = got_error(GOT_ERR_NO_SPACE);
5296 err = write_tree(&id, &nentries, NULL, subtree_path,
5297 commitable_paths, status_cb, status_arg, repo);
5302 memcpy(&new_te->id, id, sizeof(new_te->id));
5311 static const struct got_error *
5312 write_tree(struct got_object_id **new_tree_id, int *nentries,
5313 struct got_tree_object *base_tree, const char *path_base_tree,
5314 struct got_pathlist_head *commitable_paths,
5315 got_worktree_status_cb status_cb, void *status_arg,
5316 struct got_repository *repo)
5318 const struct got_error *err = NULL;
5319 struct got_pathlist_head paths;
5320 struct got_tree_entry *te, *new_te = NULL;
5321 struct got_pathlist_entry *pe;
5326 /* Insert, and recurse into, newly added entries first. */
5327 TAILQ_FOREACH(pe, commitable_paths, entry) {
5328 struct got_commitable *ct = pe->data;
5329 char *child_path = NULL, *slash;
5331 if ((ct->status != GOT_STATUS_ADD &&
5332 ct->staged_status != GOT_STATUS_ADD) ||
5333 (ct->flags & GOT_COMMITABLE_ADDED))
5336 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5337 strlen(path_base_tree)))
5340 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5345 slash = strchr(child_path, '/');
5346 if (slash == NULL) {
5347 err = alloc_added_blob_tree_entry(&new_te, ct);
5350 err = report_ct_status(ct, status_cb, status_arg);
5353 ct->flags |= GOT_COMMITABLE_ADDED;
5354 err = insert_tree_entry(new_te, &paths);
5359 *slash = '\0'; /* trim trailing path components */
5360 if (base_tree == NULL ||
5361 got_object_tree_find_entry(base_tree, child_path)
5363 err = make_subtree_for_added_blob(&new_te,
5364 child_path, path_base_tree,
5365 commitable_paths, status_cb, status_arg,
5369 err = insert_tree_entry(new_te, &paths);
5378 int i, nbase_entries;
5379 /* Handle modified and deleted entries. */
5380 nbase_entries = got_object_tree_get_nentries(base_tree);
5381 for (i = 0; i < nbase_entries; i++) {
5382 struct got_commitable *ct = NULL;
5384 te = got_object_tree_get_entry(base_tree, i);
5385 if (got_object_tree_entry_is_submodule(te)) {
5386 /* Entry is a submodule; just copy it. */
5387 err = got_object_tree_entry_dup(&new_te, te);
5390 err = insert_tree_entry(new_te, &paths);
5397 if (S_ISDIR(te->mode)) {
5399 err = got_object_tree_entry_dup(&new_te, te);
5402 err = match_modified_subtree(&modified, te,
5403 path_base_tree, commitable_paths);
5406 /* Avoid recursion into unmodified subtrees. */
5408 struct got_object_id *new_id;
5410 err = write_subtree(&new_id,
5412 path_base_tree, commitable_paths,
5413 status_cb, status_arg, repo);
5416 if (nsubentries == 0) {
5417 /* All entries were deleted. */
5421 memcpy(&new_te->id, new_id,
5422 sizeof(new_te->id));
5425 err = insert_tree_entry(new_te, &paths);
5432 err = match_deleted_or_modified_ct(&ct, te,
5433 path_base_tree, commitable_paths);
5437 /* NB: Deleted entries get dropped here. */
5438 if (ct->status == GOT_STATUS_MODIFY ||
5439 ct->status == GOT_STATUS_MODE_CHANGE ||
5440 ct->staged_status == GOT_STATUS_MODIFY) {
5441 err = alloc_modified_blob_tree_entry(
5445 err = insert_tree_entry(new_te, &paths);
5450 err = report_ct_status(ct, status_cb,
5455 /* Entry is unchanged; just copy it. */
5456 err = got_object_tree_entry_dup(&new_te, te);
5459 err = insert_tree_entry(new_te, &paths);
5467 /* Write new list of entries; deleted entries have been dropped. */
5468 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5470 got_pathlist_free(&paths);
5474 static const struct got_error *
5475 update_fileindex_after_commit(struct got_worktree *worktree,
5476 struct got_pathlist_head *commitable_paths,
5477 struct got_object_id *new_base_commit_id,
5478 struct got_fileindex *fileindex, int have_staged_files)
5480 const struct got_error *err = NULL;
5481 struct got_pathlist_entry *pe;
5482 char *relpath = NULL;
5484 TAILQ_FOREACH(pe, commitable_paths, entry) {
5485 struct got_fileindex_entry *ie;
5486 struct got_commitable *ct = pe->data;
5488 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5490 err = got_path_skip_common_ancestor(&relpath,
5491 worktree->root_path, ct->ondisk_path);
5496 if (ct->status == GOT_STATUS_DELETE ||
5497 ct->staged_status == GOT_STATUS_DELETE) {
5498 got_fileindex_entry_remove(fileindex, ie);
5499 } else if (ct->staged_status == GOT_STATUS_ADD ||
5500 ct->staged_status == GOT_STATUS_MODIFY) {
5501 got_fileindex_entry_stage_set(ie,
5502 GOT_FILEIDX_STAGE_NONE);
5503 got_fileindex_entry_staged_filetype_set(ie, 0);
5505 err = got_fileindex_entry_update(ie,
5506 worktree->root_fd, relpath,
5507 ct->staged_blob_id->sha1,
5508 new_base_commit_id->sha1,
5509 !have_staged_files);
5511 err = got_fileindex_entry_update(ie,
5512 worktree->root_fd, relpath,
5514 new_base_commit_id->sha1,
5515 !have_staged_files);
5517 err = got_fileindex_entry_alloc(&ie, pe->path);
5520 err = got_fileindex_entry_update(ie,
5521 worktree->root_fd, relpath, ct->blob_id->sha1,
5522 new_base_commit_id->sha1, 1);
5524 got_fileindex_entry_free(ie);
5527 err = got_fileindex_entry_add(fileindex, ie);
5529 got_fileindex_entry_free(ie);
5542 static const struct got_error *
5543 check_out_of_date(const char *in_repo_path, unsigned char status,
5544 unsigned char staged_status, struct got_object_id *base_blob_id,
5545 struct got_object_id *base_commit_id,
5546 struct got_object_id *head_commit_id, struct got_repository *repo,
5549 const struct got_error *err = NULL;
5550 struct got_object_id *id = NULL;
5552 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5553 /* Trivial case: base commit == head commit */
5554 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5557 * Ensure file content which local changes were based
5558 * on matches file content in the branch head.
5560 err = got_object_id_by_path(&id, repo, head_commit_id,
5563 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5564 err = got_error(ood_errcode);
5566 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5567 err = got_error(ood_errcode);
5569 /* Require that added files don't exist in the branch head. */
5570 err = got_object_id_by_path(&id, repo, head_commit_id,
5572 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5574 err = id ? got_error(ood_errcode) : NULL;
5581 const struct got_error *
5582 commit_worktree(struct got_object_id **new_commit_id,
5583 struct got_pathlist_head *commitable_paths,
5584 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5585 const char *author, const char *committer,
5586 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5587 got_worktree_status_cb status_cb, void *status_arg,
5588 struct got_repository *repo)
5590 const struct got_error *err = NULL, *unlockerr = NULL;
5591 struct got_pathlist_entry *pe;
5592 const char *head_ref_name = NULL;
5593 struct got_commit_object *head_commit = NULL;
5594 struct got_reference *head_ref2 = NULL;
5595 struct got_object_id *head_commit_id2 = NULL;
5596 struct got_tree_object *head_tree = NULL;
5597 struct got_object_id *new_tree_id = NULL;
5599 struct got_object_id_queue parent_ids;
5600 struct got_object_qid *pid = NULL;
5601 char *logmsg = NULL;
5603 *new_commit_id = NULL;
5605 STAILQ_INIT(&parent_ids);
5607 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5611 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5615 if (commit_msg_cb != NULL) {
5616 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5621 if (logmsg == NULL || strlen(logmsg) == 0) {
5622 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5626 /* Create blobs from added and modified files and record their IDs. */
5627 TAILQ_FOREACH(pe, commitable_paths, entry) {
5628 struct got_commitable *ct = pe->data;
5631 /* Blobs for staged files already exist. */
5632 if (ct->staged_status == GOT_STATUS_ADD ||
5633 ct->staged_status == GOT_STATUS_MODIFY)
5636 if (ct->status != GOT_STATUS_ADD &&
5637 ct->status != GOT_STATUS_MODIFY &&
5638 ct->status != GOT_STATUS_MODE_CHANGE)
5641 if (asprintf(&ondisk_path, "%s/%s",
5642 worktree->root_path, pe->path) == -1) {
5643 err = got_error_from_errno("asprintf");
5646 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5652 /* Recursively write new tree objects. */
5653 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5654 commitable_paths, status_cb, status_arg, repo);
5658 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5661 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5662 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5663 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5664 got_object_qid_free(pid);
5670 /* Check if a concurrent commit to our branch has occurred. */
5671 head_ref_name = got_worktree_get_head_ref_name(worktree);
5672 if (head_ref_name == NULL) {
5673 err = got_error_from_errno("got_worktree_get_head_ref_name");
5676 /* Lock the reference here to prevent concurrent modification. */
5677 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5680 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5683 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5684 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5687 /* Update branch head in repository. */
5688 err = got_ref_change_ref(head_ref2, *new_commit_id);
5691 err = got_ref_write(head_ref2, repo);
5695 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5699 err = ref_base_commit(worktree, repo);
5704 got_object_tree_close(head_tree);
5706 got_object_commit_close(head_commit);
5707 free(head_commit_id2);
5709 unlockerr = got_ref_unlock(head_ref2);
5710 if (unlockerr && err == NULL)
5712 got_ref_close(head_ref2);
5717 static const struct got_error *
5718 check_path_is_commitable(const char *path,
5719 struct got_pathlist_head *commitable_paths)
5721 struct got_pathlist_entry *cpe = NULL;
5722 size_t path_len = strlen(path);
5724 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5725 struct got_commitable *ct = cpe->data;
5726 const char *ct_path = ct->path;
5728 while (ct_path[0] == '/')
5731 if (strcmp(path, ct_path) == 0 ||
5732 got_path_is_child(ct_path, path, path_len))
5737 return got_error_path(path, GOT_ERR_BAD_PATH);
5742 static const struct got_error *
5743 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5745 int *have_staged_files = arg;
5747 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5748 *have_staged_files = 1;
5749 return got_error(GOT_ERR_CANCELLED);
5755 static const struct got_error *
5756 check_non_staged_files(struct got_fileindex *fileindex,
5757 struct got_pathlist_head *paths)
5759 struct got_pathlist_entry *pe;
5760 struct got_fileindex_entry *ie;
5762 TAILQ_FOREACH(pe, paths, entry) {
5763 if (pe->path[0] == '\0')
5765 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5767 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5768 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5769 return got_error_path(pe->path,
5770 GOT_ERR_FILE_NOT_STAGED);
5776 const struct got_error *
5777 got_worktree_commit(struct got_object_id **new_commit_id,
5778 struct got_worktree *worktree, struct got_pathlist_head *paths,
5779 const char *author, const char *committer, int allow_bad_symlinks,
5780 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5781 got_worktree_status_cb status_cb, void *status_arg,
5782 struct got_repository *repo)
5784 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5785 struct got_fileindex *fileindex = NULL;
5786 char *fileindex_path = NULL;
5787 struct got_pathlist_head commitable_paths;
5788 struct collect_commitables_arg cc_arg;
5789 struct got_pathlist_entry *pe;
5790 struct got_reference *head_ref = NULL;
5791 struct got_object_id *head_commit_id = NULL;
5792 int have_staged_files = 0;
5794 *new_commit_id = NULL;
5796 TAILQ_INIT(&commitable_paths);
5798 err = lock_worktree(worktree, LOCK_EX);
5802 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5806 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5810 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5814 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5815 &have_staged_files);
5816 if (err && err->code != GOT_ERR_CANCELLED)
5818 if (have_staged_files) {
5819 err = check_non_staged_files(fileindex, paths);
5824 cc_arg.commitable_paths = &commitable_paths;
5825 cc_arg.worktree = worktree;
5826 cc_arg.fileindex = fileindex;
5828 cc_arg.have_staged_files = have_staged_files;
5829 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5830 TAILQ_FOREACH(pe, paths, entry) {
5831 err = worktree_status(worktree, pe->path, fileindex, repo,
5832 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5837 if (TAILQ_EMPTY(&commitable_paths)) {
5838 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5842 TAILQ_FOREACH(pe, paths, entry) {
5843 err = check_path_is_commitable(pe->path, &commitable_paths);
5848 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5849 struct got_commitable *ct = pe->data;
5850 const char *ct_path = ct->in_repo_path;
5852 while (ct_path[0] == '/')
5854 err = check_out_of_date(ct_path, ct->status,
5855 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5856 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5862 err = commit_worktree(new_commit_id, &commitable_paths,
5863 head_commit_id, worktree, author, committer,
5864 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5868 err = update_fileindex_after_commit(worktree, &commitable_paths,
5869 *new_commit_id, fileindex, have_staged_files);
5870 sync_err = sync_fileindex(fileindex, fileindex_path);
5871 if (sync_err && err == NULL)
5875 got_fileindex_free(fileindex);
5876 free(fileindex_path);
5877 unlockerr = lock_worktree(worktree, LOCK_SH);
5878 if (unlockerr && err == NULL)
5880 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5881 struct got_commitable *ct = pe->data;
5882 free_commitable(ct);
5884 got_pathlist_free(&commitable_paths);
5889 got_commitable_get_path(struct got_commitable *ct)
5895 got_commitable_get_status(struct got_commitable *ct)
5900 struct check_rebase_ok_arg {
5901 struct got_worktree *worktree;
5902 struct got_repository *repo;
5905 static const struct got_error *
5906 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5908 const struct got_error *err = NULL;
5909 struct check_rebase_ok_arg *a = arg;
5910 unsigned char status;
5914 /* Reject rebase of a work tree with mixed base commits. */
5915 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5916 SHA1_DIGEST_LENGTH))
5917 return got_error(GOT_ERR_MIXED_COMMITS);
5919 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5921 return got_error_from_errno("asprintf");
5923 /* Reject rebase of a work tree with modified or staged files. */
5924 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5929 if (status != GOT_STATUS_NO_CHANGE)
5930 return got_error(GOT_ERR_MODIFIED);
5931 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5932 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5937 const struct got_error *
5938 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5939 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5940 struct got_worktree *worktree, struct got_reference *branch,
5941 struct got_repository *repo)
5943 const struct got_error *err = NULL;
5944 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5945 char *branch_ref_name = NULL;
5946 char *fileindex_path = NULL;
5947 struct check_rebase_ok_arg ok_arg;
5948 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5949 struct got_object_id *wt_branch_tip = NULL;
5951 *new_base_branch_ref = NULL;
5955 err = lock_worktree(worktree, LOCK_EX);
5959 err = open_fileindex(fileindex, &fileindex_path, worktree);
5963 ok_arg.worktree = worktree;
5965 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5970 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5974 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5978 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5982 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5987 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5990 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5991 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5995 err = got_ref_alloc_symref(new_base_branch_ref,
5996 new_base_branch_ref_name, wt_branch);
5999 err = got_ref_write(*new_base_branch_ref, repo);
6003 /* TODO Lock original branch's ref while rebasing? */
6005 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6009 err = got_ref_write(branch_ref, repo);
6013 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6014 worktree->base_commit_id);
6017 err = got_ref_write(*tmp_branch, repo);
6021 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6025 free(fileindex_path);
6026 free(tmp_branch_name);
6027 free(new_base_branch_ref_name);
6028 free(branch_ref_name);
6030 got_ref_close(branch_ref);
6032 got_ref_close(wt_branch);
6033 free(wt_branch_tip);
6035 if (*new_base_branch_ref) {
6036 got_ref_close(*new_base_branch_ref);
6037 *new_base_branch_ref = NULL;
6040 got_ref_close(*tmp_branch);
6044 got_fileindex_free(*fileindex);
6047 lock_worktree(worktree, LOCK_SH);
6052 const struct got_error *
6053 got_worktree_rebase_continue(struct got_object_id **commit_id,
6054 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6055 struct got_reference **branch, struct got_fileindex **fileindex,
6056 struct got_worktree *worktree, struct got_repository *repo)
6058 const struct got_error *err;
6059 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6060 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6061 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6062 char *fileindex_path = NULL;
6063 int have_staged_files = 0;
6066 *new_base_branch = NULL;
6071 err = lock_worktree(worktree, LOCK_EX);
6075 err = open_fileindex(fileindex, &fileindex_path, worktree);
6079 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6080 &have_staged_files);
6081 if (err && err->code != GOT_ERR_CANCELLED)
6083 if (have_staged_files) {
6084 err = got_error(GOT_ERR_STAGED_PATHS);
6088 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6092 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6096 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6100 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6104 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6108 err = got_ref_open(branch, repo,
6109 got_ref_get_symref_target(branch_ref), 0);
6113 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6117 err = got_ref_resolve(commit_id, repo, commit_ref);
6121 err = got_ref_open(new_base_branch, repo,
6122 new_base_branch_ref_name, 0);
6126 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6130 free(commit_ref_name);
6131 free(branch_ref_name);
6132 free(fileindex_path);
6134 got_ref_close(commit_ref);
6136 got_ref_close(branch_ref);
6141 got_ref_close(*tmp_branch);
6144 if (*new_base_branch) {
6145 got_ref_close(*new_base_branch);
6146 *new_base_branch = NULL;
6149 got_ref_close(*branch);
6153 got_fileindex_free(*fileindex);
6156 lock_worktree(worktree, LOCK_SH);
6161 const struct got_error *
6162 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6164 const struct got_error *err;
6165 char *tmp_branch_name = NULL;
6167 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6171 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6172 free(tmp_branch_name);
6176 static const struct got_error *
6177 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6178 char **logmsg, void *arg)
6184 static const struct got_error *
6185 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6186 const char *path, struct got_object_id *blob_id,
6187 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6188 int dirfd, const char *de_name)
6193 struct collect_merged_paths_arg {
6194 got_worktree_checkout_cb progress_cb;
6196 struct got_pathlist_head *merged_paths;
6199 static const struct got_error *
6200 collect_merged_paths(void *arg, unsigned char status, const char *path)
6202 const struct got_error *err;
6203 struct collect_merged_paths_arg *a = arg;
6205 struct got_pathlist_entry *new;
6207 err = (*a->progress_cb)(a->progress_arg, status, path);
6211 if (status != GOT_STATUS_MERGE &&
6212 status != GOT_STATUS_ADD &&
6213 status != GOT_STATUS_DELETE &&
6214 status != GOT_STATUS_CONFLICT)
6219 return got_error_from_errno("strdup");
6221 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6222 if (err || new == NULL)
6228 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6230 struct got_pathlist_entry *pe;
6232 TAILQ_FOREACH(pe, merged_paths, entry)
6233 free((char *)pe->path);
6235 got_pathlist_free(merged_paths);
6238 static const struct got_error *
6239 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6240 int is_rebase, struct got_repository *repo)
6242 const struct got_error *err;
6243 struct got_reference *commit_ref = NULL;
6245 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6247 if (err->code != GOT_ERR_NOT_REF)
6249 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6252 err = got_ref_write(commit_ref, repo);
6255 } else if (is_rebase) {
6256 struct got_object_id *stored_id;
6259 err = got_ref_resolve(&stored_id, repo, commit_ref);
6262 cmp = got_object_id_cmp(commit_id, stored_id);
6265 err = got_error(GOT_ERR_REBASE_COMMITID);
6271 got_ref_close(commit_ref);
6275 static const struct got_error *
6276 rebase_merge_files(struct got_pathlist_head *merged_paths,
6277 const char *commit_ref_name, struct got_worktree *worktree,
6278 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6279 struct got_object_id *commit_id, struct got_repository *repo,
6280 got_worktree_checkout_cb progress_cb, void *progress_arg,
6281 got_cancel_cb cancel_cb, void *cancel_arg)
6283 const struct got_error *err;
6284 struct got_reference *commit_ref = NULL;
6285 struct collect_merged_paths_arg cmp_arg;
6286 char *fileindex_path;
6288 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6290 err = get_fileindex_path(&fileindex_path, worktree);
6294 cmp_arg.progress_cb = progress_cb;
6295 cmp_arg.progress_arg = progress_arg;
6296 cmp_arg.merged_paths = merged_paths;
6297 err = merge_files(worktree, fileindex, fileindex_path,
6298 parent_commit_id, commit_id, repo, collect_merged_paths,
6299 &cmp_arg, cancel_cb, cancel_arg);
6301 got_ref_close(commit_ref);
6305 const struct got_error *
6306 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6307 struct got_worktree *worktree, struct got_fileindex *fileindex,
6308 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6309 struct got_repository *repo,
6310 got_worktree_checkout_cb progress_cb, void *progress_arg,
6311 got_cancel_cb cancel_cb, void *cancel_arg)
6313 const struct got_error *err;
6314 char *commit_ref_name;
6316 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6320 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6324 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6325 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6326 progress_arg, cancel_cb, cancel_arg);
6328 free(commit_ref_name);
6332 const struct got_error *
6333 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6334 struct got_worktree *worktree, struct got_fileindex *fileindex,
6335 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6336 struct got_repository *repo,
6337 got_worktree_checkout_cb progress_cb, void *progress_arg,
6338 got_cancel_cb cancel_cb, void *cancel_arg)
6340 const struct got_error *err;
6341 char *commit_ref_name;
6343 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6347 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6351 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6352 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6353 progress_arg, cancel_cb, cancel_arg);
6355 free(commit_ref_name);
6359 static const struct got_error *
6360 rebase_commit(struct got_object_id **new_commit_id,
6361 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6362 struct got_worktree *worktree, struct got_fileindex *fileindex,
6363 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6364 const char *new_logmsg, struct got_repository *repo)
6366 const struct got_error *err, *sync_err;
6367 struct got_pathlist_head commitable_paths;
6368 struct collect_commitables_arg cc_arg;
6369 char *fileindex_path = NULL;
6370 struct got_reference *head_ref = NULL;
6371 struct got_object_id *head_commit_id = NULL;
6372 char *logmsg = NULL;
6374 TAILQ_INIT(&commitable_paths);
6375 *new_commit_id = NULL;
6377 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6379 err = get_fileindex_path(&fileindex_path, worktree);
6383 cc_arg.commitable_paths = &commitable_paths;
6384 cc_arg.worktree = worktree;
6386 cc_arg.have_staged_files = 0;
6388 * If possible get the status of individual files directly to
6389 * avoid crawling the entire work tree once per rebased commit.
6391 * Ideally, merged_paths would contain a list of commitables
6392 * we could use so we could skip worktree_status() entirely.
6393 * However, we would then need carefully keep track of cumulative
6394 * effects of operations such as file additions and deletions
6395 * in 'got histedit -f' (folding multiple commits into one),
6396 * and this extra complexity is not really worth it.
6399 struct got_pathlist_entry *pe;
6400 TAILQ_FOREACH(pe, merged_paths, entry) {
6401 err = worktree_status(worktree, pe->path, fileindex,
6402 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6408 err = worktree_status(worktree, "", fileindex, repo,
6409 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6414 if (TAILQ_EMPTY(&commitable_paths)) {
6415 /* No-op change; commit will be elided. */
6416 err = got_ref_delete(commit_ref, repo);
6419 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6423 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6427 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6432 logmsg = strdup(new_logmsg);
6433 if (logmsg == NULL) {
6434 err = got_error_from_errno("strdup");
6438 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6443 /* NB: commit_worktree will call free(logmsg) */
6444 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6445 worktree, got_object_commit_get_author(orig_commit),
6446 got_object_commit_get_committer(orig_commit),
6447 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6451 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6455 err = got_ref_delete(commit_ref, repo);
6459 err = update_fileindex_after_commit(worktree, &commitable_paths,
6460 *new_commit_id, fileindex, 0);
6461 sync_err = sync_fileindex(fileindex, fileindex_path);
6462 if (sync_err && err == NULL)
6465 free(fileindex_path);
6466 free(head_commit_id);
6468 got_ref_close(head_ref);
6470 free(*new_commit_id);
6471 *new_commit_id = NULL;
6476 const struct got_error *
6477 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6478 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6479 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6480 struct got_commit_object *orig_commit,
6481 struct got_object_id *orig_commit_id, struct got_repository *repo)
6483 const struct got_error *err;
6484 char *commit_ref_name;
6485 struct got_reference *commit_ref = NULL;
6486 struct got_object_id *commit_id = NULL;
6488 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6492 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6495 err = got_ref_resolve(&commit_id, repo, commit_ref);
6498 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6499 err = got_error(GOT_ERR_REBASE_COMMITID);
6503 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6504 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6507 got_ref_close(commit_ref);
6508 free(commit_ref_name);
6513 const struct got_error *
6514 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6515 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6516 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6517 struct got_commit_object *orig_commit,
6518 struct got_object_id *orig_commit_id, const char *new_logmsg,
6519 struct got_repository *repo)
6521 const struct got_error *err;
6522 char *commit_ref_name;
6523 struct got_reference *commit_ref = NULL;
6525 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6529 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6533 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6534 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6537 got_ref_close(commit_ref);
6538 free(commit_ref_name);
6542 const struct got_error *
6543 got_worktree_rebase_postpone(struct got_worktree *worktree,
6544 struct got_fileindex *fileindex)
6547 got_fileindex_free(fileindex);
6548 return lock_worktree(worktree, LOCK_SH);
6551 static const struct got_error *
6552 delete_ref(const char *name, struct got_repository *repo)
6554 const struct got_error *err;
6555 struct got_reference *ref;
6557 err = got_ref_open(&ref, repo, name, 0);
6559 if (err->code == GOT_ERR_NOT_REF)
6564 err = got_ref_delete(ref, repo);
6569 static const struct got_error *
6570 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6572 const struct got_error *err;
6573 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6574 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6576 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6579 err = delete_ref(tmp_branch_name, repo);
6583 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6586 err = delete_ref(new_base_branch_ref_name, repo);
6590 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6593 err = delete_ref(branch_ref_name, repo);
6597 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6600 err = delete_ref(commit_ref_name, repo);
6605 free(tmp_branch_name);
6606 free(new_base_branch_ref_name);
6607 free(branch_ref_name);
6608 free(commit_ref_name);
6612 const struct got_error *
6613 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6614 struct got_object_id *new_commit_id, struct got_repository *repo)
6616 const struct got_error *err;
6617 struct got_reference *ref = NULL;
6618 struct got_object_id *old_commit_id = NULL;
6619 const char *branch_name = NULL;
6620 char *new_id_str = NULL;
6621 char *refname = NULL;
6623 branch_name = got_ref_get_name(branch);
6624 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6625 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6628 err = got_object_id_str(&new_id_str, new_commit_id);
6632 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6633 new_id_str) == -1) {
6634 err = got_error_from_errno("asprintf");
6638 err = got_ref_resolve(&old_commit_id, repo, branch);
6642 err = got_ref_alloc(&ref, refname, old_commit_id);
6646 err = got_ref_write(ref, repo);
6650 free(old_commit_id);
6656 const struct got_error *
6657 got_worktree_rebase_complete(struct got_worktree *worktree,
6658 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6659 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6660 struct got_repository *repo, int create_backup)
6662 const struct got_error *err, *unlockerr, *sync_err;
6663 struct got_object_id *new_head_commit_id = NULL;
6664 char *fileindex_path = NULL;
6666 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6670 if (create_backup) {
6671 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6672 rebased_branch, new_head_commit_id, repo);
6677 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6681 err = got_ref_write(rebased_branch, repo);
6685 err = got_worktree_set_head_ref(worktree, rebased_branch);
6689 err = delete_rebase_refs(worktree, repo);
6693 err = get_fileindex_path(&fileindex_path, worktree);
6696 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6697 sync_err = sync_fileindex(fileindex, fileindex_path);
6698 if (sync_err && err == NULL)
6701 got_fileindex_free(fileindex);
6702 free(fileindex_path);
6703 free(new_head_commit_id);
6704 unlockerr = lock_worktree(worktree, LOCK_SH);
6705 if (unlockerr && err == NULL)
6710 const struct got_error *
6711 got_worktree_rebase_abort(struct got_worktree *worktree,
6712 struct got_fileindex *fileindex, struct got_repository *repo,
6713 struct got_reference *new_base_branch,
6714 got_worktree_checkout_cb progress_cb, void *progress_arg)
6716 const struct got_error *err, *unlockerr, *sync_err;
6717 struct got_reference *resolved = NULL;
6718 struct got_object_id *commit_id = NULL;
6719 char *fileindex_path = NULL;
6720 struct revert_file_args rfa;
6721 struct got_object_id *tree_id = NULL;
6723 err = lock_worktree(worktree, LOCK_EX);
6727 err = got_ref_open(&resolved, repo,
6728 got_ref_get_symref_target(new_base_branch), 0);
6732 err = got_worktree_set_head_ref(worktree, resolved);
6737 * XXX commits to the base branch could have happened while
6738 * we were busy rebasing; should we store the original commit ID
6739 * when rebase begins and read it back here?
6741 err = got_ref_resolve(&commit_id, repo, resolved);
6745 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6749 err = got_object_id_by_path(&tree_id, repo,
6750 worktree->base_commit_id, worktree->path_prefix);
6754 err = delete_rebase_refs(worktree, repo);
6758 err = get_fileindex_path(&fileindex_path, worktree);
6762 rfa.worktree = worktree;
6763 rfa.fileindex = fileindex;
6764 rfa.progress_cb = progress_cb;
6765 rfa.progress_arg = progress_arg;
6766 rfa.patch_cb = NULL;
6767 rfa.patch_arg = NULL;
6769 err = worktree_status(worktree, "", fileindex, repo,
6770 revert_file, &rfa, NULL, NULL, 0, 0);
6774 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6775 repo, progress_cb, progress_arg, NULL, NULL);
6777 sync_err = sync_fileindex(fileindex, fileindex_path);
6778 if (sync_err && err == NULL)
6781 got_ref_close(resolved);
6785 got_fileindex_free(fileindex);
6786 free(fileindex_path);
6788 unlockerr = lock_worktree(worktree, LOCK_SH);
6789 if (unlockerr && err == NULL)
6794 const struct got_error *
6795 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6796 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6797 struct got_fileindex **fileindex, struct got_worktree *worktree,
6798 struct got_repository *repo)
6800 const struct got_error *err = NULL;
6801 char *tmp_branch_name = NULL;
6802 char *branch_ref_name = NULL;
6803 char *base_commit_ref_name = NULL;
6804 char *fileindex_path = NULL;
6805 struct check_rebase_ok_arg ok_arg;
6806 struct got_reference *wt_branch = NULL;
6807 struct got_reference *base_commit_ref = NULL;
6811 *base_commit_id = NULL;
6814 err = lock_worktree(worktree, LOCK_EX);
6818 err = open_fileindex(fileindex, &fileindex_path, worktree);
6822 ok_arg.worktree = worktree;
6824 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6829 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6833 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6837 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6842 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6847 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6851 err = got_ref_write(*branch_ref, repo);
6855 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6856 worktree->base_commit_id);
6859 err = got_ref_write(base_commit_ref, repo);
6862 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6863 if (*base_commit_id == NULL) {
6864 err = got_error_from_errno("got_object_id_dup");
6868 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6869 worktree->base_commit_id);
6872 err = got_ref_write(*tmp_branch, repo);
6876 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6880 free(fileindex_path);
6881 free(tmp_branch_name);
6882 free(branch_ref_name);
6883 free(base_commit_ref_name);
6885 got_ref_close(wt_branch);
6888 got_ref_close(*branch_ref);
6892 got_ref_close(*tmp_branch);
6895 free(*base_commit_id);
6897 got_fileindex_free(*fileindex);
6900 lock_worktree(worktree, LOCK_SH);
6905 const struct got_error *
6906 got_worktree_histedit_postpone(struct got_worktree *worktree,
6907 struct got_fileindex *fileindex)
6910 got_fileindex_free(fileindex);
6911 return lock_worktree(worktree, LOCK_SH);
6914 const struct got_error *
6915 got_worktree_histedit_in_progress(int *in_progress,
6916 struct got_worktree *worktree)
6918 const struct got_error *err;
6919 char *tmp_branch_name = NULL;
6921 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6925 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6926 free(tmp_branch_name);
6930 const struct got_error *
6931 got_worktree_histedit_continue(struct got_object_id **commit_id,
6932 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6933 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6934 struct got_worktree *worktree, struct got_repository *repo)
6936 const struct got_error *err;
6937 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6938 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6939 struct got_reference *commit_ref = NULL;
6940 struct got_reference *base_commit_ref = NULL;
6941 char *fileindex_path = NULL;
6942 int have_staged_files = 0;
6946 *base_commit_id = NULL;
6949 err = lock_worktree(worktree, LOCK_EX);
6953 err = open_fileindex(fileindex, &fileindex_path, worktree);
6957 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6958 &have_staged_files);
6959 if (err && err->code != GOT_ERR_CANCELLED)
6961 if (have_staged_files) {
6962 err = got_error(GOT_ERR_STAGED_PATHS);
6966 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6970 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6974 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6978 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6983 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6987 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6990 err = got_ref_resolve(commit_id, repo, commit_ref);
6994 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6997 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7001 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7005 free(commit_ref_name);
7006 free(branch_ref_name);
7007 free(fileindex_path);
7009 got_ref_close(commit_ref);
7010 if (base_commit_ref)
7011 got_ref_close(base_commit_ref);
7015 free(*base_commit_id);
7016 *base_commit_id = NULL;
7018 got_ref_close(*tmp_branch);
7022 got_fileindex_free(*fileindex);
7025 lock_worktree(worktree, LOCK_EX);
7030 static const struct got_error *
7031 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7033 const struct got_error *err;
7034 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7035 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7037 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7040 err = delete_ref(tmp_branch_name, repo);
7044 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7048 err = delete_ref(base_commit_ref_name, repo);
7052 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7055 err = delete_ref(branch_ref_name, repo);
7059 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7062 err = delete_ref(commit_ref_name, repo);
7066 free(tmp_branch_name);
7067 free(base_commit_ref_name);
7068 free(branch_ref_name);
7069 free(commit_ref_name);
7073 const struct got_error *
7074 got_worktree_histedit_abort(struct got_worktree *worktree,
7075 struct got_fileindex *fileindex, struct got_repository *repo,
7076 struct got_reference *branch, struct got_object_id *base_commit_id,
7077 got_worktree_checkout_cb progress_cb, void *progress_arg)
7079 const struct got_error *err, *unlockerr, *sync_err;
7080 struct got_reference *resolved = NULL;
7081 char *fileindex_path = NULL;
7082 struct got_object_id *tree_id = NULL;
7083 struct revert_file_args rfa;
7085 err = lock_worktree(worktree, LOCK_EX);
7089 err = got_ref_open(&resolved, repo,
7090 got_ref_get_symref_target(branch), 0);
7094 err = got_worktree_set_head_ref(worktree, resolved);
7098 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7102 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7103 worktree->path_prefix);
7107 err = delete_histedit_refs(worktree, repo);
7111 err = get_fileindex_path(&fileindex_path, worktree);
7115 rfa.worktree = worktree;
7116 rfa.fileindex = fileindex;
7117 rfa.progress_cb = progress_cb;
7118 rfa.progress_arg = progress_arg;
7119 rfa.patch_cb = NULL;
7120 rfa.patch_arg = NULL;
7122 err = worktree_status(worktree, "", fileindex, repo,
7123 revert_file, &rfa, NULL, NULL, 0, 0);
7127 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7128 repo, progress_cb, progress_arg, NULL, NULL);
7130 sync_err = sync_fileindex(fileindex, fileindex_path);
7131 if (sync_err && err == NULL)
7134 got_ref_close(resolved);
7136 free(fileindex_path);
7138 unlockerr = lock_worktree(worktree, LOCK_SH);
7139 if (unlockerr && err == NULL)
7144 const struct got_error *
7145 got_worktree_histedit_complete(struct got_worktree *worktree,
7146 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7147 struct got_reference *edited_branch, struct got_repository *repo)
7149 const struct got_error *err, *unlockerr, *sync_err;
7150 struct got_object_id *new_head_commit_id = NULL;
7151 struct got_reference *resolved = NULL;
7152 char *fileindex_path = NULL;
7154 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7158 err = got_ref_open(&resolved, repo,
7159 got_ref_get_symref_target(edited_branch), 0);
7163 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7164 resolved, new_head_commit_id, repo);
7168 err = got_ref_change_ref(resolved, new_head_commit_id);
7172 err = got_ref_write(resolved, repo);
7176 err = got_worktree_set_head_ref(worktree, resolved);
7180 err = delete_histedit_refs(worktree, repo);
7184 err = get_fileindex_path(&fileindex_path, worktree);
7187 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7188 sync_err = sync_fileindex(fileindex, fileindex_path);
7189 if (sync_err && err == NULL)
7192 got_fileindex_free(fileindex);
7193 free(fileindex_path);
7194 free(new_head_commit_id);
7195 unlockerr = lock_worktree(worktree, LOCK_SH);
7196 if (unlockerr && err == NULL)
7201 const struct got_error *
7202 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7203 struct got_object_id *commit_id, struct got_repository *repo)
7205 const struct got_error *err;
7206 char *commit_ref_name;
7208 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7212 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7216 err = delete_ref(commit_ref_name, repo);
7218 free(commit_ref_name);
7222 const struct got_error *
7223 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7224 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7225 struct got_worktree *worktree, const char *refname,
7226 struct got_repository *repo)
7228 const struct got_error *err = NULL;
7229 char *fileindex_path = NULL;
7230 struct check_rebase_ok_arg ok_arg;
7234 *base_branch_ref = NULL;
7236 err = lock_worktree(worktree, LOCK_EX);
7240 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7241 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7242 "cannot integrate a branch into itself; "
7243 "update -b or different branch name required");
7247 err = open_fileindex(fileindex, &fileindex_path, worktree);
7251 /* Preconditions are the same as for rebase. */
7252 ok_arg.worktree = worktree;
7254 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7259 err = got_ref_open(branch_ref, repo, refname, 1);
7263 err = got_ref_open(base_branch_ref, repo,
7264 got_worktree_get_head_ref_name(worktree), 1);
7268 got_ref_close(*branch_ref);
7271 if (*base_branch_ref) {
7272 got_ref_close(*base_branch_ref);
7273 *base_branch_ref = NULL;
7276 got_fileindex_free(*fileindex);
7279 lock_worktree(worktree, LOCK_SH);
7284 const struct got_error *
7285 got_worktree_integrate_continue(struct got_worktree *worktree,
7286 struct got_fileindex *fileindex, struct got_repository *repo,
7287 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7288 got_worktree_checkout_cb progress_cb, void *progress_arg,
7289 got_cancel_cb cancel_cb, void *cancel_arg)
7291 const struct got_error *err = NULL, *sync_err, *unlockerr;
7292 char *fileindex_path = NULL;
7293 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7295 err = get_fileindex_path(&fileindex_path, worktree);
7299 err = got_ref_resolve(&commit_id, repo, branch_ref);
7303 err = got_object_id_by_path(&tree_id, repo, commit_id,
7304 worktree->path_prefix);
7308 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7312 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7313 progress_cb, progress_arg, cancel_cb, cancel_arg);
7317 err = got_ref_change_ref(base_branch_ref, commit_id);
7321 err = got_ref_write(base_branch_ref, repo);
7325 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7327 sync_err = sync_fileindex(fileindex, fileindex_path);
7328 if (sync_err && err == NULL)
7332 unlockerr = got_ref_unlock(branch_ref);
7333 if (unlockerr && err == NULL)
7335 got_ref_close(branch_ref);
7337 unlockerr = got_ref_unlock(base_branch_ref);
7338 if (unlockerr && err == NULL)
7340 got_ref_close(base_branch_ref);
7342 got_fileindex_free(fileindex);
7343 free(fileindex_path);
7346 unlockerr = lock_worktree(worktree, LOCK_SH);
7347 if (unlockerr && err == NULL)
7352 const struct got_error *
7353 got_worktree_integrate_abort(struct got_worktree *worktree,
7354 struct got_fileindex *fileindex, struct got_repository *repo,
7355 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7357 const struct got_error *err = NULL, *unlockerr = NULL;
7359 got_fileindex_free(fileindex);
7361 err = lock_worktree(worktree, LOCK_SH);
7363 unlockerr = got_ref_unlock(branch_ref);
7364 if (unlockerr && err == NULL)
7366 got_ref_close(branch_ref);
7368 unlockerr = got_ref_unlock(base_branch_ref);
7369 if (unlockerr && err == NULL)
7371 got_ref_close(base_branch_ref);
7376 struct check_stage_ok_arg {
7377 struct got_object_id *head_commit_id;
7378 struct got_worktree *worktree;
7379 struct got_fileindex *fileindex;
7380 struct got_repository *repo;
7384 const struct got_error *
7385 check_stage_ok(void *arg, unsigned char status,
7386 unsigned char staged_status, const char *relpath,
7387 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7388 struct got_object_id *commit_id, int dirfd, const char *de_name)
7390 struct check_stage_ok_arg *a = arg;
7391 const struct got_error *err = NULL;
7392 struct got_fileindex_entry *ie;
7393 struct got_object_id base_commit_id;
7394 struct got_object_id *base_commit_idp = NULL;
7395 char *in_repo_path = NULL, *p;
7397 if (status == GOT_STATUS_UNVERSIONED ||
7398 status == GOT_STATUS_NO_CHANGE)
7400 if (status == GOT_STATUS_NONEXISTENT)
7401 return got_error_set_errno(ENOENT, relpath);
7403 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7405 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7407 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7408 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7410 return got_error_from_errno("asprintf");
7412 if (got_fileindex_entry_has_commit(ie)) {
7413 memcpy(base_commit_id.sha1, ie->commit_sha1,
7414 SHA1_DIGEST_LENGTH);
7415 base_commit_idp = &base_commit_id;
7418 if (status == GOT_STATUS_CONFLICT) {
7419 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7421 } else if (status != GOT_STATUS_ADD &&
7422 status != GOT_STATUS_MODIFY &&
7423 status != GOT_STATUS_DELETE) {
7424 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7428 a->have_changes = 1;
7433 err = check_out_of_date(p, status, staged_status,
7434 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7435 GOT_ERR_STAGE_OUT_OF_DATE);
7441 struct stage_path_arg {
7442 struct got_worktree *worktree;
7443 struct got_fileindex *fileindex;
7444 struct got_repository *repo;
7445 got_worktree_status_cb status_cb;
7447 got_worktree_patch_cb patch_cb;
7449 int staged_something;
7450 int allow_bad_symlinks;
7453 static const struct got_error *
7454 stage_path(void *arg, unsigned char status,
7455 unsigned char staged_status, const char *relpath,
7456 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7457 struct got_object_id *commit_id, int dirfd, const char *de_name)
7459 struct stage_path_arg *a = arg;
7460 const struct got_error *err = NULL;
7461 struct got_fileindex_entry *ie;
7462 char *ondisk_path = NULL, *path_content = NULL;
7464 struct got_object_id *new_staged_blob_id = NULL;
7467 if (status == GOT_STATUS_UNVERSIONED)
7470 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7472 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7474 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7476 return got_error_from_errno("asprintf");
7479 case GOT_STATUS_ADD:
7480 case GOT_STATUS_MODIFY:
7481 /* XXX could sb.st_mode be passed in by our caller? */
7482 if (lstat(ondisk_path, &sb) == -1) {
7483 err = got_error_from_errno2("lstat", ondisk_path);
7487 if (status == GOT_STATUS_ADD) {
7488 int choice = GOT_PATCH_CHOICE_NONE;
7489 err = (*a->patch_cb)(&choice, a->patch_arg,
7490 status, ie->path, NULL, 1, 1);
7493 if (choice != GOT_PATCH_CHOICE_YES)
7496 err = create_patched_content(&path_content, 0,
7497 staged_blob_id ? staged_blob_id : blob_id,
7498 ondisk_path, dirfd, de_name, ie->path,
7499 a->repo, a->patch_cb, a->patch_arg);
7500 if (err || path_content == NULL)
7504 err = got_object_blob_create(&new_staged_blob_id,
7505 path_content ? path_content : ondisk_path, a->repo);
7508 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7509 SHA1_DIGEST_LENGTH);
7510 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7511 stage = GOT_FILEIDX_STAGE_ADD;
7513 stage = GOT_FILEIDX_STAGE_MODIFY;
7514 got_fileindex_entry_stage_set(ie, stage);
7515 if (S_ISLNK(sb.st_mode)) {
7516 int is_bad_symlink = 0;
7517 if (!a->allow_bad_symlinks) {
7518 char target_path[PATH_MAX];
7520 target_len = readlink(ondisk_path, target_path,
7521 sizeof(target_path));
7522 if (target_len == -1) {
7523 err = got_error_from_errno2("readlink",
7527 err = is_bad_symlink_target(&is_bad_symlink,
7528 target_path, target_len, ondisk_path,
7529 a->worktree->root_path);
7532 if (is_bad_symlink) {
7533 err = got_error_path(ondisk_path,
7534 GOT_ERR_BAD_SYMLINK);
7539 got_fileindex_entry_staged_filetype_set(ie,
7540 GOT_FILEIDX_MODE_BAD_SYMLINK);
7542 got_fileindex_entry_staged_filetype_set(ie,
7543 GOT_FILEIDX_MODE_SYMLINK);
7545 got_fileindex_entry_staged_filetype_set(ie,
7546 GOT_FILEIDX_MODE_REGULAR_FILE);
7548 a->staged_something = 1;
7549 if (a->status_cb == NULL)
7551 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7552 get_staged_status(ie), relpath, blob_id,
7553 new_staged_blob_id, NULL, dirfd, de_name);
7555 case GOT_STATUS_DELETE:
7556 if (staged_status == GOT_STATUS_DELETE)
7559 int choice = GOT_PATCH_CHOICE_NONE;
7560 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7561 ie->path, NULL, 1, 1);
7564 if (choice == GOT_PATCH_CHOICE_NO)
7566 if (choice != GOT_PATCH_CHOICE_YES) {
7567 err = got_error(GOT_ERR_PATCH_CHOICE);
7571 stage = GOT_FILEIDX_STAGE_DELETE;
7572 got_fileindex_entry_stage_set(ie, stage);
7573 a->staged_something = 1;
7574 if (a->status_cb == NULL)
7576 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7577 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7580 case GOT_STATUS_NO_CHANGE:
7582 case GOT_STATUS_CONFLICT:
7583 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7585 case GOT_STATUS_NONEXISTENT:
7586 err = got_error_set_errno(ENOENT, relpath);
7589 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7593 if (path_content && unlink(path_content) == -1 && err == NULL)
7594 err = got_error_from_errno2("unlink", path_content);
7597 free(new_staged_blob_id);
7601 const struct got_error *
7602 got_worktree_stage(struct got_worktree *worktree,
7603 struct got_pathlist_head *paths,
7604 got_worktree_status_cb status_cb, void *status_arg,
7605 got_worktree_patch_cb patch_cb, void *patch_arg,
7606 int allow_bad_symlinks, struct got_repository *repo)
7608 const struct got_error *err = NULL, *sync_err, *unlockerr;
7609 struct got_pathlist_entry *pe;
7610 struct got_fileindex *fileindex = NULL;
7611 char *fileindex_path = NULL;
7612 struct got_reference *head_ref = NULL;
7613 struct got_object_id *head_commit_id = NULL;
7614 struct check_stage_ok_arg oka;
7615 struct stage_path_arg spa;
7617 err = lock_worktree(worktree, LOCK_EX);
7621 err = got_ref_open(&head_ref, repo,
7622 got_worktree_get_head_ref_name(worktree), 0);
7625 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7628 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7632 /* Check pre-conditions before staging anything. */
7633 oka.head_commit_id = head_commit_id;
7634 oka.worktree = worktree;
7635 oka.fileindex = fileindex;
7637 oka.have_changes = 0;
7638 TAILQ_FOREACH(pe, paths, entry) {
7639 err = worktree_status(worktree, pe->path, fileindex, repo,
7640 check_stage_ok, &oka, NULL, NULL, 0, 0);
7644 if (!oka.have_changes) {
7645 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7649 spa.worktree = worktree;
7650 spa.fileindex = fileindex;
7652 spa.patch_cb = patch_cb;
7653 spa.patch_arg = patch_arg;
7654 spa.status_cb = status_cb;
7655 spa.status_arg = status_arg;
7656 spa.staged_something = 0;
7657 spa.allow_bad_symlinks = allow_bad_symlinks;
7658 TAILQ_FOREACH(pe, paths, entry) {
7659 err = worktree_status(worktree, pe->path, fileindex, repo,
7660 stage_path, &spa, NULL, NULL, 0, 0);
7664 if (!spa.staged_something) {
7665 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7669 sync_err = sync_fileindex(fileindex, fileindex_path);
7670 if (sync_err && err == NULL)
7674 got_ref_close(head_ref);
7675 free(head_commit_id);
7676 free(fileindex_path);
7678 got_fileindex_free(fileindex);
7679 unlockerr = lock_worktree(worktree, LOCK_SH);
7680 if (unlockerr && err == NULL)
7685 struct unstage_path_arg {
7686 struct got_worktree *worktree;
7687 struct got_fileindex *fileindex;
7688 struct got_repository *repo;
7689 got_worktree_checkout_cb progress_cb;
7691 got_worktree_patch_cb patch_cb;
7695 static const struct got_error *
7696 create_unstaged_content(char **path_unstaged_content,
7697 char **path_new_staged_content, struct got_object_id *blob_id,
7698 struct got_object_id *staged_blob_id, const char *relpath,
7699 struct got_repository *repo,
7700 got_worktree_patch_cb patch_cb, void *patch_arg)
7702 const struct got_error *err, *free_err;
7703 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7704 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7705 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7706 struct got_diffreg_result *diffreg_result = NULL;
7707 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7708 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7710 *path_unstaged_content = NULL;
7711 *path_new_staged_content = NULL;
7713 err = got_object_id_str(&label1, blob_id);
7716 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7720 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7724 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7728 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7732 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7736 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7740 err = got_diff_files(&diffreg_result, f1, label1, f2,
7741 path2, 3, 0, 1, NULL);
7745 err = got_opentemp_named(path_unstaged_content, &outfile,
7746 "got-unstaged-content");
7749 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7750 "got-new-staged-content");
7754 if (fseek(f1, 0L, SEEK_SET) == -1) {
7755 err = got_ferror(f1, GOT_ERR_IO);
7758 if (fseek(f2, 0L, SEEK_SET) == -1) {
7759 err = got_ferror(f2, GOT_ERR_IO);
7762 /* Count the number of actual changes in the diff result. */
7763 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7764 struct diff_chunk_context cc = {};
7765 diff_chunk_context_load_change(&cc, &nchunks_used,
7766 diffreg_result->result, n, 0);
7769 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7771 err = apply_or_reject_change(&choice, &nchunks_used,
7772 diffreg_result->result, n, relpath, f1, f2,
7773 &line_cur1, &line_cur2,
7774 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7777 if (choice == GOT_PATCH_CHOICE_YES)
7780 have_rejected_content = 1;
7781 if (choice == GOT_PATCH_CHOICE_QUIT)
7784 if (have_content || have_rejected_content)
7785 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7786 outfile, rejectfile);
7790 got_object_blob_close(blob);
7792 got_object_blob_close(staged_blob);
7793 free_err = got_diffreg_result_free(diffreg_result);
7794 if (free_err && err == NULL)
7796 if (f1 && fclose(f1) == EOF && err == NULL)
7797 err = got_error_from_errno2("fclose", path1);
7798 if (f2 && fclose(f2) == EOF && err == NULL)
7799 err = got_error_from_errno2("fclose", path2);
7800 if (outfile && fclose(outfile) == EOF && err == NULL)
7801 err = got_error_from_errno2("fclose", *path_unstaged_content);
7802 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7803 err = got_error_from_errno2("fclose", *path_new_staged_content);
7804 if (path1 && unlink(path1) == -1 && err == NULL)
7805 err = got_error_from_errno2("unlink", path1);
7806 if (path2 && unlink(path2) == -1 && err == NULL)
7807 err = got_error_from_errno2("unlink", path2);
7808 if (err || !have_content) {
7809 if (*path_unstaged_content &&
7810 unlink(*path_unstaged_content) == -1 && err == NULL)
7811 err = got_error_from_errno2("unlink",
7812 *path_unstaged_content);
7813 free(*path_unstaged_content);
7814 *path_unstaged_content = NULL;
7816 if (err || !have_content || !have_rejected_content) {
7817 if (*path_new_staged_content &&
7818 unlink(*path_new_staged_content) == -1 && err == NULL)
7819 err = got_error_from_errno2("unlink",
7820 *path_new_staged_content);
7821 free(*path_new_staged_content);
7822 *path_new_staged_content = NULL;
7829 static const struct got_error *
7830 unstage_hunks(struct got_object_id *staged_blob_id,
7831 struct got_blob_object *blob_base,
7832 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7833 const char *ondisk_path, const char *label_orig,
7834 struct got_worktree *worktree, struct got_repository *repo,
7835 got_worktree_patch_cb patch_cb, void *patch_arg,
7836 got_worktree_checkout_cb progress_cb, void *progress_arg)
7838 const struct got_error *err = NULL;
7839 char *path_unstaged_content = NULL;
7840 char *path_new_staged_content = NULL;
7841 char *parent = NULL, *base_path = NULL;
7842 char *blob_base_path = NULL;
7843 struct got_object_id *new_staged_blob_id = NULL;
7844 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7847 err = create_unstaged_content(&path_unstaged_content,
7848 &path_new_staged_content, blob_id, staged_blob_id,
7849 ie->path, repo, patch_cb, patch_arg);
7853 if (path_unstaged_content == NULL)
7856 if (path_new_staged_content) {
7857 err = got_object_blob_create(&new_staged_blob_id,
7858 path_new_staged_content, repo);
7863 f = fopen(path_unstaged_content, "r");
7865 err = got_error_from_errno2("fopen",
7866 path_unstaged_content);
7869 if (fstat(fileno(f), &sb) == -1) {
7870 err = got_error_from_errno2("fstat", path_unstaged_content);
7873 if (got_fileindex_entry_staged_filetype_get(ie) ==
7874 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7875 char link_target[PATH_MAX];
7877 r = fread(link_target, 1, sizeof(link_target), f);
7878 if (r == 0 && ferror(f)) {
7879 err = got_error_from_errno("fread");
7882 if (r >= sizeof(link_target)) { /* should not happen */
7883 err = got_error(GOT_ERR_NO_SPACE);
7886 link_target[r] = '\0';
7887 err = merge_symlink(worktree, blob_base,
7888 ondisk_path, ie->path, label_orig, link_target,
7889 worktree->base_commit_id, repo, progress_cb,
7892 int local_changes_subsumed;
7894 err = got_path_dirname(&parent, ondisk_path);
7898 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7900 err = got_error_from_errno("asprintf");
7905 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7908 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7914 * In order the run a 3-way merge with a symlink we copy the symlink's
7915 * target path into a temporary file and use that file with diff3.
7917 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7918 err = dump_symlink_target_path_to_file(&f_deriv2,
7924 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7926 err = got_error_from_errno2("open", ondisk_path);
7929 f_deriv2 = fdopen(fd, "r");
7930 if (f_deriv2 == NULL) {
7931 err = got_error_from_errno2("fdopen", ondisk_path);
7937 err = merge_file(&local_changes_subsumed, worktree,
7938 f_base, f, f_deriv2, ondisk_path, ie->path,
7939 got_fileindex_perms_to_st(ie),
7940 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
7941 repo, progress_cb, progress_arg);
7946 if (new_staged_blob_id) {
7947 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7948 SHA1_DIGEST_LENGTH);
7950 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7951 got_fileindex_entry_staged_filetype_set(ie, 0);
7954 free(new_staged_blob_id);
7955 if (path_unstaged_content &&
7956 unlink(path_unstaged_content) == -1 && err == NULL)
7957 err = got_error_from_errno2("unlink", path_unstaged_content);
7958 if (path_new_staged_content &&
7959 unlink(path_new_staged_content) == -1 && err == NULL)
7960 err = got_error_from_errno2("unlink", path_new_staged_content);
7961 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7962 err = got_error_from_errno2("unlink", blob_base_path);
7963 if (f_base && fclose(f_base) == EOF && err == NULL)
7964 err = got_error_from_errno2("fclose", path_unstaged_content);
7965 if (f && fclose(f) == EOF && err == NULL)
7966 err = got_error_from_errno2("fclose", path_unstaged_content);
7967 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7968 err = got_error_from_errno2("fclose", ondisk_path);
7969 free(path_unstaged_content);
7970 free(path_new_staged_content);
7971 free(blob_base_path);
7977 static const struct got_error *
7978 unstage_path(void *arg, unsigned char status,
7979 unsigned char staged_status, const char *relpath,
7980 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7981 struct got_object_id *commit_id, int dirfd, const char *de_name)
7983 const struct got_error *err = NULL;
7984 struct unstage_path_arg *a = arg;
7985 struct got_fileindex_entry *ie;
7986 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7987 char *ondisk_path = NULL;
7988 char *id_str = NULL, *label_orig = NULL;
7989 int local_changes_subsumed;
7992 if (staged_status != GOT_STATUS_ADD &&
7993 staged_status != GOT_STATUS_MODIFY &&
7994 staged_status != GOT_STATUS_DELETE)
7997 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7999 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8001 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8003 return got_error_from_errno("asprintf");
8005 err = got_object_id_str(&id_str,
8006 commit_id ? commit_id : a->worktree->base_commit_id);
8009 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8011 err = got_error_from_errno("asprintf");
8015 switch (staged_status) {
8016 case GOT_STATUS_MODIFY:
8017 err = got_object_open_as_blob(&blob_base, a->repo,
8022 case GOT_STATUS_ADD:
8024 if (staged_status == GOT_STATUS_ADD) {
8025 int choice = GOT_PATCH_CHOICE_NONE;
8026 err = (*a->patch_cb)(&choice, a->patch_arg,
8027 staged_status, ie->path, NULL, 1, 1);
8030 if (choice != GOT_PATCH_CHOICE_YES)
8033 err = unstage_hunks(staged_blob_id,
8034 blob_base, blob_id, ie, ondisk_path,
8035 label_orig, a->worktree, a->repo,
8036 a->patch_cb, a->patch_arg,
8037 a->progress_cb, a->progress_arg);
8038 break; /* Done with this file. */
8041 err = got_object_open_as_blob(&blob_staged, a->repo,
8042 staged_blob_id, 8192);
8045 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8046 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8047 case GOT_FILEIDX_MODE_REGULAR_FILE:
8048 err = merge_blob(&local_changes_subsumed, a->worktree,
8049 blob_base, ondisk_path, relpath,
8050 got_fileindex_perms_to_st(ie), label_orig,
8051 blob_staged, commit_id ? commit_id :
8052 a->worktree->base_commit_id, a->repo,
8053 a->progress_cb, a->progress_arg);
8055 case GOT_FILEIDX_MODE_SYMLINK:
8056 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8057 char *staged_target;
8058 err = got_object_blob_read_to_str(
8059 &staged_target, blob_staged);
8062 err = merge_symlink(a->worktree, blob_base,
8063 ondisk_path, relpath, label_orig,
8064 staged_target, commit_id ? commit_id :
8065 a->worktree->base_commit_id,
8066 a->repo, a->progress_cb, a->progress_arg);
8067 free(staged_target);
8069 err = merge_blob(&local_changes_subsumed,
8070 a->worktree, blob_base, ondisk_path,
8071 relpath, got_fileindex_perms_to_st(ie),
8072 label_orig, blob_staged,
8073 commit_id ? commit_id :
8074 a->worktree->base_commit_id, a->repo,
8075 a->progress_cb, a->progress_arg);
8079 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8083 got_fileindex_entry_stage_set(ie,
8084 GOT_FILEIDX_STAGE_NONE);
8085 got_fileindex_entry_staged_filetype_set(ie, 0);
8088 case GOT_STATUS_DELETE:
8090 int choice = GOT_PATCH_CHOICE_NONE;
8091 err = (*a->patch_cb)(&choice, a->patch_arg,
8092 staged_status, ie->path, NULL, 1, 1);
8095 if (choice == GOT_PATCH_CHOICE_NO)
8097 if (choice != GOT_PATCH_CHOICE_YES) {
8098 err = got_error(GOT_ERR_PATCH_CHOICE);
8102 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8103 got_fileindex_entry_staged_filetype_set(ie, 0);
8104 err = get_file_status(&status, &sb, ie, ondisk_path,
8105 dirfd, de_name, a->repo);
8108 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8114 got_object_blob_close(blob_base);
8116 got_object_blob_close(blob_staged);
8122 const struct got_error *
8123 got_worktree_unstage(struct got_worktree *worktree,
8124 struct got_pathlist_head *paths,
8125 got_worktree_checkout_cb progress_cb, void *progress_arg,
8126 got_worktree_patch_cb patch_cb, void *patch_arg,
8127 struct got_repository *repo)
8129 const struct got_error *err = NULL, *sync_err, *unlockerr;
8130 struct got_pathlist_entry *pe;
8131 struct got_fileindex *fileindex = NULL;
8132 char *fileindex_path = NULL;
8133 struct unstage_path_arg upa;
8135 err = lock_worktree(worktree, LOCK_EX);
8139 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8143 upa.worktree = worktree;
8144 upa.fileindex = fileindex;
8146 upa.progress_cb = progress_cb;
8147 upa.progress_arg = progress_arg;
8148 upa.patch_cb = patch_cb;
8149 upa.patch_arg = patch_arg;
8150 TAILQ_FOREACH(pe, paths, entry) {
8151 err = worktree_status(worktree, pe->path, fileindex, repo,
8152 unstage_path, &upa, NULL, NULL, 0, 0);
8157 sync_err = sync_fileindex(fileindex, fileindex_path);
8158 if (sync_err && err == NULL)
8161 free(fileindex_path);
8163 got_fileindex_free(fileindex);
8164 unlockerr = lock_worktree(worktree, LOCK_SH);
8165 if (unlockerr && err == NULL)
8170 struct report_file_info_arg {
8171 struct got_worktree *worktree;
8172 got_worktree_path_info_cb info_cb;
8174 struct got_pathlist_head *paths;
8175 got_cancel_cb cancel_cb;
8179 static const struct got_error *
8180 report_file_info(void *arg, struct got_fileindex_entry *ie)
8182 struct report_file_info_arg *a = arg;
8183 struct got_pathlist_entry *pe;
8184 struct got_object_id blob_id, staged_blob_id, commit_id;
8185 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8186 struct got_object_id *commit_idp = NULL;
8189 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8190 return got_error(GOT_ERR_CANCELLED);
8192 TAILQ_FOREACH(pe, a->paths, entry) {
8193 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8194 got_path_is_child(ie->path, pe->path, pe->path_len))
8197 if (pe == NULL) /* not found */
8200 if (got_fileindex_entry_has_blob(ie)) {
8201 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8202 blob_idp = &blob_id;
8204 stage = got_fileindex_entry_stage_get(ie);
8205 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8206 stage == GOT_FILEIDX_STAGE_ADD) {
8207 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8208 SHA1_DIGEST_LENGTH);
8209 staged_blob_idp = &staged_blob_id;
8212 if (got_fileindex_entry_has_commit(ie)) {
8213 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8214 commit_idp = &commit_id;
8217 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8218 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8221 const struct got_error *
8222 got_worktree_path_info(struct got_worktree *worktree,
8223 struct got_pathlist_head *paths,
8224 got_worktree_path_info_cb info_cb, void *info_arg,
8225 got_cancel_cb cancel_cb, void *cancel_arg)
8228 const struct got_error *err = NULL, *unlockerr;
8229 struct got_fileindex *fileindex = NULL;
8230 char *fileindex_path = NULL;
8231 struct report_file_info_arg arg;
8233 err = lock_worktree(worktree, LOCK_SH);
8237 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8241 arg.worktree = worktree;
8242 arg.info_cb = info_cb;
8243 arg.info_arg = info_arg;
8245 arg.cancel_cb = cancel_cb;
8246 arg.cancel_arg = cancel_arg;
8247 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8250 free(fileindex_path);
8252 got_fileindex_free(fileindex);
8253 unlockerr = lock_worktree(worktree, LOCK_UN);
8254 if (unlockerr && err == NULL)