2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
142 err = got_error_from_errno2("open", path);
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
171 (*content)[sb.st_size - 1] = '\0';
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
193 return got_error_from_errno("got_ref_to_str");
195 refstr = strdup(got_ref_get_name(head_ref));
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
211 uint32_t uuid_status;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
224 err = got_ref_resolve(&commit_id, repo, head_ref);
227 err = got_object_get_type(&obj_type, repo, commit_id);
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
433 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 if ((*worktree)->root_fd == -1) {
435 err = got_error_from_errno2("open", (*worktree)->root_path);
440 const struct got_error *close_err = got_repo_close(repo);
446 free(base_commit_id_str);
452 if (*worktree != NULL)
453 got_worktree_close(*worktree);
456 (*worktree)->lockfd = fd;
461 const struct got_error *
462 got_worktree_open(struct got_worktree **worktree, const char *path)
464 const struct got_error *err = NULL;
467 worktree_path = strdup(path);
468 if (worktree_path == NULL)
469 return got_error_from_errno("strdup");
474 err = open_worktree(worktree, worktree_path);
475 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
483 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
485 err = got_path_dirname(&parent_path, worktree_path);
487 if (err->code != GOT_ERR_BAD_PATH) {
494 worktree_path = parent_path;
498 return got_error(GOT_ERR_NOT_WORKTREE);
501 const struct got_error *
502 got_worktree_close(struct got_worktree *worktree)
504 const struct got_error *err = NULL;
506 if (worktree->lockfd != -1) {
507 if (close(worktree->lockfd) == -1)
508 err = got_error_from_errno2("close",
509 got_worktree_get_root_path(worktree));
511 if (close(worktree->root_fd) == -1 && err == NULL)
512 err = got_error_from_errno2("close",
513 got_worktree_get_root_path(worktree));
514 free(worktree->repo_path);
515 free(worktree->path_prefix);
516 free(worktree->base_commit_id);
517 free(worktree->head_ref_name);
518 free(worktree->root_path);
519 free(worktree->gotconfig_path);
520 got_gotconfig_free(worktree->gotconfig);
526 got_worktree_get_root_path(struct got_worktree *worktree)
528 return worktree->root_path;
532 got_worktree_get_repo_path(struct got_worktree *worktree)
534 return worktree->repo_path;
537 got_worktree_get_path_prefix(struct got_worktree *worktree)
539 return worktree->path_prefix;
542 const struct got_error *
543 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
544 const char *path_prefix)
546 char *absprefix = NULL;
548 if (!got_path_is_absolute(path_prefix)) {
549 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
550 return got_error_from_errno("asprintf");
552 *match = (strcmp(absprefix ? absprefix : path_prefix,
553 worktree->path_prefix) == 0);
559 got_worktree_get_head_ref_name(struct got_worktree *worktree)
561 return worktree->head_ref_name;
564 const struct got_error *
565 got_worktree_set_head_ref(struct got_worktree *worktree,
566 struct got_reference *head_ref)
568 const struct got_error *err = NULL;
569 char *path_got = NULL, *head_ref_name = NULL;
571 if (asprintf(&path_got, "%s/%s", worktree->root_path,
572 GOT_WORKTREE_GOT_DIR) == -1) {
573 err = got_error_from_errno("asprintf");
578 head_ref_name = strdup(got_ref_get_name(head_ref));
579 if (head_ref_name == NULL) {
580 err = got_error_from_errno("strdup");
584 err = write_head_ref(path_got, head_ref);
588 free(worktree->head_ref_name);
589 worktree->head_ref_name = head_ref_name;
597 struct got_object_id *
598 got_worktree_get_base_commit_id(struct got_worktree *worktree)
600 return worktree->base_commit_id;
603 const struct got_error *
604 got_worktree_set_base_commit_id(struct got_worktree *worktree,
605 struct got_repository *repo, struct got_object_id *commit_id)
607 const struct got_error *err;
608 struct got_object *obj = NULL;
610 char *path_got = NULL;
612 if (asprintf(&path_got, "%s/%s", worktree->root_path,
613 GOT_WORKTREE_GOT_DIR) == -1) {
614 err = got_error_from_errno("asprintf");
619 err = got_object_open(&obj, repo, commit_id);
623 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
624 err = got_error(GOT_ERR_OBJ_TYPE);
628 /* Record our base commit. */
629 err = got_object_id_str(&id_str, commit_id);
632 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
636 free(worktree->base_commit_id);
637 worktree->base_commit_id = got_object_id_dup(commit_id);
638 if (worktree->base_commit_id == NULL) {
639 err = got_error_from_errno("got_object_id_dup");
644 got_object_close(obj);
650 const struct got_gotconfig *
651 got_worktree_get_gotconfig(struct got_worktree *worktree)
653 return worktree->gotconfig;
656 static const struct got_error *
657 lock_worktree(struct got_worktree *worktree, int operation)
659 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
660 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
661 : got_error_from_errno2("flock",
662 got_worktree_get_root_path(worktree)));
666 static const struct got_error *
667 add_dir_on_disk(struct got_worktree *worktree, const char *path)
669 const struct got_error *err = NULL;
672 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
673 return got_error_from_errno("asprintf");
675 err = got_path_mkdir(abspath);
676 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
679 if (lstat(abspath, &sb) == -1) {
680 err = got_error_from_errno2("lstat", abspath);
681 } else if (!S_ISDIR(sb.st_mode)) {
682 /* TODO directory is obstructed; do something */
683 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
690 static const struct got_error *
691 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
693 const struct got_error *err = NULL;
696 size_t flen1 = 0, flen2 = 0;
701 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
702 if (flen1 == 0 && ferror(f1)) {
703 err = got_error_from_errno("fread");
706 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
707 if (flen2 == 0 && ferror(f2)) {
708 err = got_error_from_errno("fread");
715 } else if (flen2 == 0) {
719 } else if (flen1 == flen2) {
720 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
733 static const struct got_error *
734 check_files_equal(int *same, FILE *f1, FILE *f2)
741 if (fstat(fileno(f1), &sb) != 0)
742 return got_error_from_errno("fstat");
745 if (fstat(fileno(f2), &sb) != 0)
746 return got_error_from_errno("fstat");
749 if (size1 != size2) {
754 if (fseek(f1, 0L, SEEK_SET) == -1)
755 return got_ferror(f1, GOT_ERR_IO);
756 if (fseek(f2, 0L, SEEK_SET) == -1)
757 return got_ferror(f2, GOT_ERR_IO);
759 return check_file_contents_equal(same, f1, f2);
763 * Perform a 3-way merge where the file f_orig acts as the common
764 * ancestor, the file f_deriv acts as the first derived version,
765 * and the file f_deriv2 acts as the second derived version.
766 * The merge result will be written to a new file at ondisk_path; any
767 * existing file at this path will be replaced.
769 static const struct got_error *
770 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
771 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
772 const char *path, uint16_t st_mode,
773 const char *label_orig, const char *label_deriv, const char *label_deriv2,
774 enum got_diff_algorithm diff_algo, struct got_repository *repo,
775 got_worktree_checkout_cb progress_cb, void *progress_arg)
777 const struct got_error *err = NULL;
779 FILE *f_merged = NULL;
780 char *merged_path = NULL, *base_path = NULL;
784 *local_changes_subsumed = 0;
786 err = got_path_dirname(&parent, ondisk_path);
790 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
791 err = got_error_from_errno("asprintf");
795 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
799 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
800 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
804 err = (*progress_cb)(progress_arg,
805 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 if (fsync(merged_fd) != 0) {
810 err = got_error_from_errno("fsync");
814 f_merged = fdopen(merged_fd, "r");
815 if (f_merged == NULL) {
816 err = got_error_from_errno("fdopen");
821 /* Check if a clean merge has subsumed all local changes. */
822 if (overlapcnt == 0) {
823 err = check_files_equal(local_changes_subsumed, f_deriv,
829 if (fchmod(fileno(f_merged), st_mode) != 0) {
830 err = got_error_from_errno2("fchmod", merged_path);
834 if (rename(merged_path, ondisk_path) != 0) {
835 err = got_error_from_errno3("rename", merged_path,
844 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
845 err = got_error_from_errno("close");
846 if (f_merged && fclose(f_merged) == EOF && err == NULL)
847 err = got_error_from_errno("fclose");
854 static const struct got_error *
855 update_symlink(const char *ondisk_path, const char *target_path,
858 /* This is not atomic but matches what 'ln -sf' does. */
859 if (unlink(ondisk_path) == -1)
860 return got_error_from_errno2("unlink", ondisk_path);
861 if (symlink(target_path, ondisk_path) == -1)
862 return got_error_from_errno3("symlink", target_path,
868 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
869 * in the work tree with a file that contains conflict markers and the
870 * conflicting target paths of the original version, a "derived version"
871 * of a symlink from an incoming change, and a local version of the symlink.
873 * The original versions's target path can be NULL if it is not available,
874 * such as if both derived versions added a new symlink at the same path.
876 * The incoming derived symlink target is NULL in case the incoming change
877 * has deleted this symlink.
879 static const struct got_error *
880 install_symlink_conflict(const char *deriv_target,
881 struct got_object_id *deriv_base_commit_id, const char *orig_target,
882 const char *label_orig, const char *local_target, const char *ondisk_path)
884 const struct got_error *err;
885 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
888 err = got_object_id_str(&id_str, deriv_base_commit_id);
890 return got_error_from_errno("asprintf");
892 if (asprintf(&label_deriv, "%s: commit %s",
893 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
894 err = got_error_from_errno("asprintf");
898 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
902 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
903 err = got_error_from_errno2("fchmod", path);
907 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
908 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
909 deriv_target ? deriv_target : "(symlink was deleted)",
910 orig_target ? label_orig : "",
911 orig_target ? "\n" : "",
912 orig_target ? orig_target : "",
913 orig_target ? "\n" : "",
914 GOT_DIFF_CONFLICT_MARKER_SEP,
915 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
916 err = got_error_from_errno2("fprintf", path);
920 if (unlink(ondisk_path) == -1) {
921 err = got_error_from_errno2("unlink", ondisk_path);
924 if (rename(path, ondisk_path) == -1) {
925 err = got_error_from_errno3("rename", path, ondisk_path);
929 if (f != NULL && fclose(f) == EOF && err == NULL)
930 err = got_error_from_errno2("fclose", path);
937 /* forward declaration */
938 static const struct got_error *
939 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
940 const char *, const char *, uint16_t, const char *,
941 struct got_blob_object *, struct got_object_id *,
942 struct got_repository *, got_worktree_checkout_cb, void *);
945 * Merge a symlink into the work tree, where blob_orig acts as the common
946 * ancestor, deriv_target is the link target of the first derived version,
947 * and the symlink on disk acts as the second derived version.
948 * Assume that contents of both blobs represent symlinks.
950 static const struct got_error *
951 merge_symlink(struct got_worktree *worktree,
952 struct got_blob_object *blob_orig, const char *ondisk_path,
953 const char *path, const char *label_orig, const char *deriv_target,
954 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
955 got_worktree_checkout_cb progress_cb, void *progress_arg)
957 const struct got_error *err = NULL;
958 char *ancestor_target = NULL;
960 ssize_t ondisk_len, deriv_len;
961 char ondisk_target[PATH_MAX];
962 int have_local_change = 0;
963 int have_incoming_change = 0;
965 if (lstat(ondisk_path, &sb) == -1)
966 return got_error_from_errno2("lstat", ondisk_path);
968 ondisk_len = readlink(ondisk_path, ondisk_target,
969 sizeof(ondisk_target));
970 if (ondisk_len == -1) {
971 err = got_error_from_errno2("readlink",
975 ondisk_target[ondisk_len] = '\0';
978 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
983 if (ancestor_target == NULL ||
984 (ondisk_len != strlen(ancestor_target) ||
985 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
986 have_local_change = 1;
988 deriv_len = strlen(deriv_target);
989 if (ancestor_target == NULL ||
990 (deriv_len != strlen(ancestor_target) ||
991 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
992 have_incoming_change = 1;
994 if (!have_local_change && !have_incoming_change) {
995 if (ancestor_target) {
996 /* Both sides made the same change. */
997 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
999 } else if (deriv_len == ondisk_len &&
1000 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1001 /* Both sides added the same symlink. */
1002 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1005 /* Both sides added symlinks which don't match. */
1006 err = install_symlink_conflict(deriv_target,
1007 deriv_base_commit_id, ancestor_target,
1008 label_orig, ondisk_target, ondisk_path);
1011 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1014 } else if (!have_local_change && have_incoming_change) {
1015 /* Apply the incoming change. */
1016 err = update_symlink(ondisk_path, deriv_target,
1017 strlen(deriv_target));
1020 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1021 } else if (have_local_change && have_incoming_change) {
1022 if (deriv_len == ondisk_len &&
1023 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1024 /* Both sides made the same change. */
1025 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1028 err = install_symlink_conflict(deriv_target,
1029 deriv_base_commit_id, ancestor_target, label_orig,
1030 ondisk_target, ondisk_path);
1033 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1039 free(ancestor_target);
1043 static const struct got_error *
1044 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1046 const struct got_error *err = NULL;
1047 char target_path[PATH_MAX];
1056 return got_error_from_errno("got_opentemp");
1057 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1058 if (target_len == -1) {
1059 err = got_error_from_errno2("readlink", ondisk_path);
1062 n = fwrite(target_path, 1, target_len, f);
1063 if (n != target_len) {
1064 err = got_ferror(f, GOT_ERR_IO);
1067 if (fflush(f) == EOF) {
1068 err = got_error_from_errno("fflush");
1071 if (fseek(f, 0L, SEEK_SET) == -1) {
1072 err = got_ferror(f, GOT_ERR_IO);
1084 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1085 * blob_deriv acts as the first derived version, and the file on disk
1086 * acts as the second derived version.
1088 static const struct got_error *
1089 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1090 struct got_blob_object *blob_orig, const char *ondisk_path,
1091 const char *path, uint16_t st_mode, const char *label_orig,
1092 struct got_blob_object *blob_deriv,
1093 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1094 got_worktree_checkout_cb progress_cb, void *progress_arg)
1096 const struct got_error *err = NULL;
1097 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1098 char *blob_orig_path = NULL;
1099 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1100 char *label_deriv = NULL, *parent = NULL;
1102 *local_changes_subsumed = 0;
1104 err = got_path_dirname(&parent, ondisk_path);
1109 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1111 err = got_error_from_errno("asprintf");
1116 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1119 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1126 * No common ancestor exists. This is an "add vs add" conflict
1127 * and we simply use an empty ancestor file to make both files
1128 * appear in the merged result in their entirety.
1130 f_orig = got_opentemp();
1131 if (f_orig == NULL) {
1132 err = got_error_from_errno("got_opentemp");
1137 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 err = got_error_from_errno("asprintf");
1143 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1151 err = got_object_id_str(&id_str, deriv_base_commit_id);
1154 if (asprintf(&label_deriv, "%s: commit %s",
1155 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 err = got_error_from_errno("asprintf");
1161 * In order the run a 3-way merge with a symlink we copy the symlink's
1162 * target path into a temporary file and use that file with diff3.
1164 if (S_ISLNK(st_mode)) {
1165 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1170 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1172 err = got_error_from_errno2("open", ondisk_path);
1175 f_deriv2 = fdopen(fd, "r");
1176 if (f_deriv2 == NULL) {
1177 err = got_error_from_errno2("fdopen", ondisk_path);
1183 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1184 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1185 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1187 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1188 err = got_error_from_errno("fclose");
1189 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1190 err = got_error_from_errno("fclose");
1191 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1192 err = got_error_from_errno("fclose");
1194 if (blob_orig_path) {
1195 unlink(blob_orig_path);
1196 free(blob_orig_path);
1198 if (blob_deriv_path) {
1199 unlink(blob_deriv_path);
1200 free(blob_deriv_path);
1208 static const struct got_error *
1209 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1210 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1211 int wt_fd, const char *path, struct got_object_id *blob_id)
1213 const struct got_error *err = NULL;
1214 struct got_fileindex_entry *new_ie;
1218 err = got_fileindex_entry_alloc(&new_ie, path);
1222 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1223 blob_id->sha1, base_commit_id->sha1, 1);
1227 err = got_fileindex_entry_add(fileindex, new_ie);
1230 got_fileindex_entry_free(new_ie);
1237 get_ondisk_perms(int executable, mode_t st_mode)
1239 mode_t xbits = S_IXUSR;
1242 /* Map read bits to execute bits. */
1243 if (st_mode & S_IRGRP)
1245 if (st_mode & S_IROTH)
1247 return st_mode | xbits;
1250 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1253 /* forward declaration */
1254 static const struct got_error *
1255 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1256 const char *path, mode_t te_mode, mode_t st_mode,
1257 struct got_blob_object *blob, int restoring_missing_file,
1258 int reverting_versioned_file, int installing_bad_symlink,
1259 int path_is_unversioned, struct got_repository *repo,
1260 got_worktree_checkout_cb progress_cb, void *progress_arg);
1263 * This function assumes that the provided symlink target points at a
1264 * safe location in the work tree!
1266 static const struct got_error *
1267 replace_existing_symlink(int *did_something, const char *ondisk_path,
1268 const char *target_path, size_t target_len)
1270 const struct got_error *err = NULL;
1272 char etarget[PATH_MAX];
1278 * "Bad" symlinks (those pointing outside the work tree or into the
1279 * .got directory) are installed in the work tree as a regular file
1280 * which contains the bad symlink target path.
1281 * The new symlink target has already been checked for safety by our
1282 * caller. If we can successfully open a regular file then we simply
1283 * replace this file with a symlink below.
1285 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1287 if (!got_err_open_nofollow_on_symlink())
1288 return got_error_from_errno2("open", ondisk_path);
1290 /* We are updating an existing on-disk symlink. */
1291 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1293 return got_error_from_errno2("readlink", ondisk_path);
1295 if (elen == target_len &&
1296 memcmp(etarget, target_path, target_len) == 0)
1297 return NULL; /* nothing to do */
1301 err = update_symlink(ondisk_path, target_path, target_len);
1302 if (fd != -1 && close(fd) == -1 && err == NULL)
1303 err = got_error_from_errno2("close", ondisk_path);
1307 static const struct got_error *
1308 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1309 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1311 const struct got_error *err = NULL;
1312 char canonpath[PATH_MAX];
1313 char *path_got = NULL;
1315 *is_bad_symlink = 0;
1317 if (target_len >= sizeof(canonpath)) {
1318 *is_bad_symlink = 1;
1323 * We do not use realpath(3) to resolve the symlink's target
1324 * path because we don't want to resolve symlinks recursively.
1325 * Instead we make the path absolute and then canonicalize it.
1326 * Relative symlink target lookup should begin at the directory
1327 * in which the blob object is being installed.
1329 if (!got_path_is_absolute(target_path)) {
1330 char *abspath, *parent;
1331 err = got_path_dirname(&parent, ondisk_path);
1334 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1336 return got_error_from_errno("asprintf");
1339 if (strlen(abspath) >= sizeof(canonpath)) {
1340 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1344 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1349 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1354 /* Only allow symlinks pointing at paths within the work tree. */
1355 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1356 *is_bad_symlink = 1;
1360 /* Do not allow symlinks pointing into the .got directory. */
1361 if (asprintf(&path_got, "%s/%s", wtroot_path,
1362 GOT_WORKTREE_GOT_DIR) == -1)
1363 return got_error_from_errno("asprintf");
1364 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1365 *is_bad_symlink = 1;
1371 static const struct got_error *
1372 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1373 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1374 int restoring_missing_file, int reverting_versioned_file,
1375 int path_is_unversioned, int allow_bad_symlinks,
1376 struct got_repository *repo,
1377 got_worktree_checkout_cb progress_cb, void *progress_arg)
1379 const struct got_error *err = NULL;
1380 char target_path[PATH_MAX];
1381 size_t len, target_len = 0;
1382 char *path_got = NULL;
1383 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1384 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1386 *is_bad_symlink = 0;
1389 * Blob object content specifies the target path of the link.
1390 * If a symbolic link cannot be installed we instead create
1391 * a regular file which contains the link target path stored
1392 * in the blob object.
1395 err = got_object_blob_read_block(&len, blob);
1396 if (len + target_len >= sizeof(target_path)) {
1397 /* Path too long; install as a regular file. */
1398 *is_bad_symlink = 1;
1399 got_object_blob_rewind(blob);
1400 return install_blob(worktree, ondisk_path, path,
1401 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1402 restoring_missing_file, reverting_versioned_file,
1403 1, path_is_unversioned, repo, progress_cb,
1407 /* Skip blob object header first time around. */
1408 memcpy(target_path + target_len, buf + hdrlen,
1410 target_len += len - hdrlen;
1414 target_path[target_len] = '\0';
1416 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1417 ondisk_path, worktree->root_path);
1421 if (*is_bad_symlink && !allow_bad_symlinks) {
1422 /* install as a regular file */
1423 got_object_blob_rewind(blob);
1424 err = install_blob(worktree, ondisk_path, path,
1425 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1426 restoring_missing_file, reverting_versioned_file, 1,
1427 path_is_unversioned, repo, progress_cb, progress_arg);
1431 if (symlink(target_path, ondisk_path) == -1) {
1432 if (errno == EEXIST) {
1433 int symlink_replaced;
1434 if (path_is_unversioned) {
1435 err = (*progress_cb)(progress_arg,
1436 GOT_STATUS_UNVERSIONED, path);
1439 err = replace_existing_symlink(&symlink_replaced,
1440 ondisk_path, target_path, target_len);
1444 if (symlink_replaced) {
1445 err = (*progress_cb)(progress_arg,
1446 reverting_versioned_file ?
1448 GOT_STATUS_UPDATE, path);
1450 err = (*progress_cb)(progress_arg,
1451 GOT_STATUS_EXISTS, path);
1454 goto done; /* Nothing else to do. */
1457 if (errno == ENOENT) {
1459 err = got_path_dirname(&parent, ondisk_path);
1462 err = add_dir_on_disk(worktree, parent);
1467 * Retry, and fall through to error handling
1468 * below if this second attempt fails.
1470 if (symlink(target_path, ondisk_path) != -1) {
1471 err = NULL; /* success */
1476 /* Handle errors from first or second creation attempt. */
1477 if (errno == ENAMETOOLONG) {
1478 /* bad target path; install as a regular file */
1479 *is_bad_symlink = 1;
1480 got_object_blob_rewind(blob);
1481 err = install_blob(worktree, ondisk_path, path,
1482 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1483 restoring_missing_file, reverting_versioned_file, 1,
1484 path_is_unversioned, repo,
1485 progress_cb, progress_arg);
1486 } else if (errno == ENOTDIR) {
1487 err = got_error_path(ondisk_path,
1488 GOT_ERR_FILE_OBSTRUCTED);
1490 err = got_error_from_errno3("symlink",
1491 target_path, ondisk_path);
1493 } else if (progress_cb)
1494 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1495 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1501 static const struct got_error *
1502 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1503 const char *path, mode_t te_mode, mode_t st_mode,
1504 struct got_blob_object *blob, int restoring_missing_file,
1505 int reverting_versioned_file, int installing_bad_symlink,
1506 int path_is_unversioned, struct got_repository *repo,
1507 got_worktree_checkout_cb progress_cb, void *progress_arg)
1509 const struct got_error *err = NULL;
1513 char *tmppath = NULL;
1515 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1516 GOT_DEFAULT_FILE_MODE);
1518 if (errno == ENOENT) {
1520 err = got_path_dirname(&parent, path);
1523 err = add_dir_on_disk(worktree, parent);
1527 fd = open(ondisk_path,
1528 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1529 GOT_DEFAULT_FILE_MODE);
1531 return got_error_from_errno2("open",
1533 } else if (errno == EEXIST) {
1534 if (path_is_unversioned) {
1535 err = (*progress_cb)(progress_arg,
1536 GOT_STATUS_UNVERSIONED, path);
1539 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1540 !S_ISREG(st_mode) && !installing_bad_symlink) {
1541 /* TODO file is obstructed; do something */
1542 err = got_error_path(ondisk_path,
1543 GOT_ERR_FILE_OBSTRUCTED);
1546 err = got_opentemp_named_fd(&tmppath, &fd,
1553 return got_error_from_errno2("open", ondisk_path);
1556 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1557 err = got_error_from_errno2("fchmod",
1558 update ? tmppath : ondisk_path);
1563 if (restoring_missing_file)
1564 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1566 else if (reverting_versioned_file)
1567 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1570 err = (*progress_cb)(progress_arg,
1571 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1576 hdrlen = got_object_blob_get_hdrlen(blob);
1578 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 err = got_object_blob_read_block(&len, blob);
1583 /* Skip blob object header first time around. */
1584 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1586 err = got_error_from_errno("write");
1588 } else if (outlen != len - hdrlen) {
1589 err = got_error(GOT_ERR_IO);
1596 if (fsync(fd) != 0) {
1597 err = got_error_from_errno("fsync");
1602 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1603 err = got_error_from_errno2("unlink", ondisk_path);
1606 if (rename(tmppath, ondisk_path) != 0) {
1607 err = got_error_from_errno3("rename", tmppath,
1616 if (fd != -1 && close(fd) == -1 && err == NULL)
1617 err = got_error_from_errno("close");
1618 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1619 err = got_error_from_errno2("unlink", tmppath);
1624 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1625 static const struct got_error *
1626 get_modified_file_content_status(unsigned char *status, FILE *f)
1628 const struct got_error *err = NULL;
1629 const char *markers[3] = {
1630 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1631 GOT_DIFF_CONFLICT_MARKER_SEP,
1632 GOT_DIFF_CONFLICT_MARKER_END
1636 size_t linesize = 0;
1639 while (*status == GOT_STATUS_MODIFY) {
1640 linelen = getline(&line, &linesize, f);
1641 if (linelen == -1) {
1644 err = got_ferror(f, GOT_ERR_IO);
1648 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1649 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1651 *status = GOT_STATUS_CONFLICT;
1662 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1664 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1665 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1669 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1671 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1672 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1673 ie->mtime_sec == sb->st_mtim.tv_sec &&
1674 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1675 ie->size == (sb->st_size & 0xffffffff) &&
1676 !xbit_differs(ie, sb->st_mode));
1679 static unsigned char
1680 get_staged_status(struct got_fileindex_entry *ie)
1682 switch (got_fileindex_entry_stage_get(ie)) {
1683 case GOT_FILEIDX_STAGE_ADD:
1684 return GOT_STATUS_ADD;
1685 case GOT_FILEIDX_STAGE_DELETE:
1686 return GOT_STATUS_DELETE;
1687 case GOT_FILEIDX_STAGE_MODIFY:
1688 return GOT_STATUS_MODIFY;
1690 return GOT_STATUS_NO_CHANGE;
1694 static const struct got_error *
1695 get_symlink_modification_status(unsigned char *status,
1696 struct got_fileindex_entry *ie, const char *abspath,
1697 int dirfd, const char *de_name, struct got_blob_object *blob)
1699 const struct got_error *err = NULL;
1700 char target_path[PATH_MAX];
1701 char etarget[PATH_MAX];
1703 size_t len, target_len = 0;
1704 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1705 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1707 *status = GOT_STATUS_NO_CHANGE;
1709 /* Blob object content specifies the target path of the link. */
1711 err = got_object_blob_read_block(&len, blob);
1714 if (len + target_len >= sizeof(target_path)) {
1716 * Should not happen. The blob contents were OK
1717 * when this symlink was installed.
1719 return got_error(GOT_ERR_NO_SPACE);
1722 /* Skip blob object header first time around. */
1723 memcpy(target_path + target_len, buf + hdrlen,
1725 target_len += len - hdrlen;
1729 target_path[target_len] = '\0';
1732 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1734 return got_error_from_errno2("readlinkat", abspath);
1736 elen = readlink(abspath, etarget, sizeof(etarget));
1738 return got_error_from_errno2("readlink", abspath);
1741 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1742 *status = GOT_STATUS_MODIFY;
1747 static const struct got_error *
1748 get_file_status(unsigned char *status, struct stat *sb,
1749 struct got_fileindex_entry *ie, const char *abspath,
1750 int dirfd, const char *de_name, struct got_repository *repo)
1752 const struct got_error *err = NULL;
1753 struct got_object_id id;
1758 struct got_blob_object *blob = NULL;
1760 unsigned char staged_status = get_staged_status(ie);
1762 *status = GOT_STATUS_NO_CHANGE;
1763 memset(sb, 0, sizeof(*sb));
1766 * Whenever the caller provides a directory descriptor and a
1767 * directory entry name for the file, use them! This prevents
1768 * race conditions if filesystem paths change beneath our feet.
1771 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1772 if (errno == ENOENT) {
1773 if (got_fileindex_entry_has_file_on_disk(ie))
1774 *status = GOT_STATUS_MISSING;
1776 *status = GOT_STATUS_DELETE;
1779 err = got_error_from_errno2("fstatat", abspath);
1783 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1784 if (fd == -1 && errno != ENOENT &&
1785 !got_err_open_nofollow_on_symlink())
1786 return got_error_from_errno2("open", abspath);
1787 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1788 if (lstat(abspath, sb) == -1)
1789 return got_error_from_errno2("lstat", abspath);
1790 } else if (fd == -1 || fstat(fd, sb) == -1) {
1791 if (errno == ENOENT) {
1792 if (got_fileindex_entry_has_file_on_disk(ie))
1793 *status = GOT_STATUS_MISSING;
1795 *status = GOT_STATUS_DELETE;
1798 err = got_error_from_errno2("fstat", abspath);
1803 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1804 *status = GOT_STATUS_OBSTRUCTED;
1808 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1809 *status = GOT_STATUS_DELETE;
1811 } else if (!got_fileindex_entry_has_blob(ie) &&
1812 staged_status != GOT_STATUS_ADD) {
1813 *status = GOT_STATUS_ADD;
1817 if (!stat_info_differs(ie, sb))
1820 if (S_ISLNK(sb->st_mode) &&
1821 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1822 *status = GOT_STATUS_MODIFY;
1826 if (staged_status == GOT_STATUS_MODIFY ||
1827 staged_status == GOT_STATUS_ADD)
1828 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1830 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1832 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1836 if (S_ISLNK(sb->st_mode)) {
1837 err = get_symlink_modification_status(status, ie,
1838 abspath, dirfd, de_name, blob);
1843 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1845 err = got_error_from_errno2("openat", abspath);
1850 f = fdopen(fd, "r");
1852 err = got_error_from_errno2("fdopen", abspath);
1856 hdrlen = got_object_blob_get_hdrlen(blob);
1858 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1859 err = got_object_blob_read_block(&blen, blob);
1862 /* Skip length of blob object header first time around. */
1863 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1864 if (flen == 0 && ferror(f)) {
1865 err = got_error_from_errno("fread");
1868 if (blen - hdrlen == 0) {
1870 *status = GOT_STATUS_MODIFY;
1872 } else if (flen == 0) {
1873 if (blen - hdrlen != 0)
1874 *status = GOT_STATUS_MODIFY;
1876 } else if (blen - hdrlen == flen) {
1877 /* Skip blob object header first time around. */
1878 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1879 *status = GOT_STATUS_MODIFY;
1883 *status = GOT_STATUS_MODIFY;
1889 if (*status == GOT_STATUS_MODIFY) {
1891 err = get_modified_file_content_status(status, f);
1892 } else if (xbit_differs(ie, sb->st_mode))
1893 *status = GOT_STATUS_MODE_CHANGE;
1896 got_object_blob_close(blob);
1897 if (f != NULL && fclose(f) == EOF && err == NULL)
1898 err = got_error_from_errno2("fclose", abspath);
1899 if (fd != -1 && close(fd) == -1 && err == NULL)
1900 err = got_error_from_errno2("close", abspath);
1905 * Update timestamps in the file index if a file is unmodified and
1906 * we had to run a full content comparison to find out.
1908 static const struct got_error *
1909 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1910 struct got_fileindex_entry *ie, struct stat *sb)
1912 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1913 return got_fileindex_entry_update(ie, wt_fd, path,
1914 ie->blob_sha1, ie->commit_sha1, 1);
1919 static const struct got_error *
1920 update_blob(struct got_worktree *worktree,
1921 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1922 struct got_tree_entry *te, const char *path,
1923 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1926 const struct got_error *err = NULL;
1927 struct got_blob_object *blob = NULL;
1929 unsigned char status = GOT_STATUS_NO_CHANGE;
1932 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1933 return got_error_from_errno("asprintf");
1936 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1937 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1940 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1944 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1945 sb.st_mode = got_fileindex_perms_to_st(ie);
1947 if (stat(ondisk_path, &sb) == -1) {
1948 if (errno != ENOENT) {
1949 err = got_error_from_errno2("stat",
1953 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1954 status = GOT_STATUS_UNVERSIONED;
1956 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1957 status = GOT_STATUS_UNVERSIONED;
1959 status = GOT_STATUS_OBSTRUCTED;
1963 if (status == GOT_STATUS_OBSTRUCTED) {
1965 got_fileindex_entry_mark_skipped(ie);
1966 err = (*progress_cb)(progress_arg, status, path);
1969 if (status == GOT_STATUS_CONFLICT) {
1971 got_fileindex_entry_mark_skipped(ie);
1972 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1977 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1978 (S_ISLNK(te->mode) ||
1979 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1981 * This is a regular file or an installed bad symlink.
1982 * If the file index indicates that this file is already
1983 * up-to-date with respect to the repository we can skip
1984 * updating contents of this file.
1986 if (got_fileindex_entry_has_commit(ie) &&
1987 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1988 SHA1_DIGEST_LENGTH) == 0) {
1990 err = sync_timestamps(worktree->root_fd,
1991 path, status, ie, &sb);
1994 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1998 if (got_fileindex_entry_has_blob(ie) &&
1999 memcmp(ie->blob_sha1, te->id.sha1,
2000 SHA1_DIGEST_LENGTH) == 0) {
2001 /* Different commit but the same blob. */
2002 err = sync_timestamps(worktree->root_fd,
2003 path, status, ie, &sb);
2006 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2012 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2016 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2017 int update_timestamps;
2018 struct got_blob_object *blob2 = NULL;
2019 char *label_orig = NULL;
2020 if (got_fileindex_entry_has_blob(ie)) {
2021 struct got_object_id id2;
2022 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2023 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2027 if (got_fileindex_entry_has_commit(ie)) {
2028 char id_str[SHA1_DIGEST_STRING_LENGTH];
2029 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2030 sizeof(id_str)) == NULL) {
2031 err = got_error_path(id_str,
2032 GOT_ERR_BAD_OBJ_ID_STR);
2035 if (asprintf(&label_orig, "%s: commit %s",
2036 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2037 err = got_error_from_errno("asprintf");
2041 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2043 err = got_object_blob_read_to_str(&link_target, blob);
2046 err = merge_symlink(worktree, blob2, ondisk_path, path,
2047 label_orig, link_target, worktree->base_commit_id,
2048 repo, progress_cb, progress_arg);
2051 err = merge_blob(&update_timestamps, worktree, blob2,
2052 ondisk_path, path, sb.st_mode, label_orig, blob,
2053 worktree->base_commit_id, repo,
2054 progress_cb, progress_arg);
2058 got_object_blob_close(blob2);
2062 * Do not update timestamps of files with local changes.
2063 * Otherwise, a future status walk would treat them as
2064 * unmodified files again.
2066 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2067 blob->id.sha1, worktree->base_commit_id->sha1,
2069 } else if (status == GOT_STATUS_MODE_CHANGE) {
2070 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2071 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2072 } else if (status == GOT_STATUS_DELETE) {
2073 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2076 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2077 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2081 int is_bad_symlink = 0;
2082 if (S_ISLNK(te->mode)) {
2083 err = install_symlink(&is_bad_symlink, worktree,
2084 ondisk_path, path, blob,
2085 status == GOT_STATUS_MISSING, 0,
2086 status == GOT_STATUS_UNVERSIONED, 0,
2087 repo, progress_cb, progress_arg);
2089 err = install_blob(worktree, ondisk_path, path,
2090 te->mode, sb.st_mode, blob,
2091 status == GOT_STATUS_MISSING, 0, 0,
2092 status == GOT_STATUS_UNVERSIONED, repo,
2093 progress_cb, progress_arg);
2099 err = got_fileindex_entry_update(ie,
2100 worktree->root_fd, path, blob->id.sha1,
2101 worktree->base_commit_id->sha1, 1);
2103 err = create_fileindex_entry(&ie, fileindex,
2104 worktree->base_commit_id, worktree->root_fd, path,
2110 if (is_bad_symlink) {
2111 got_fileindex_entry_filetype_set(ie,
2112 GOT_FILEIDX_MODE_BAD_SYMLINK);
2115 got_object_blob_close(blob);
2121 static const struct got_error *
2122 remove_ondisk_file(const char *root_path, const char *path)
2124 const struct got_error *err = NULL;
2125 char *ondisk_path = NULL, *parent = NULL;
2127 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2128 return got_error_from_errno("asprintf");
2130 if (unlink(ondisk_path) == -1) {
2131 if (errno != ENOENT)
2132 err = got_error_from_errno2("unlink", ondisk_path);
2134 size_t root_len = strlen(root_path);
2135 err = got_path_dirname(&parent, ondisk_path);
2138 while (got_path_cmp(parent, root_path,
2139 strlen(parent), root_len) != 0) {
2141 ondisk_path = parent;
2143 if (rmdir(ondisk_path) == -1) {
2144 if (errno != ENOTEMPTY)
2145 err = got_error_from_errno2("rmdir",
2149 err = got_path_dirname(&parent, ondisk_path);
2160 static const struct got_error *
2161 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2162 struct got_fileindex_entry *ie, struct got_repository *repo,
2163 got_worktree_checkout_cb progress_cb, void *progress_arg)
2165 const struct got_error *err = NULL;
2166 unsigned char status;
2170 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2171 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2173 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2175 return got_error_from_errno("asprintf");
2177 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2181 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2182 char ondisk_target[PATH_MAX];
2183 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2184 sizeof(ondisk_target));
2185 if (ondisk_len == -1) {
2186 err = got_error_from_errno2("readlink", ondisk_path);
2189 ondisk_target[ondisk_len] = '\0';
2190 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2191 NULL, NULL, /* XXX pass common ancestor info? */
2192 ondisk_target, ondisk_path);
2195 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2200 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2201 status == GOT_STATUS_ADD) {
2202 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2206 * Preserve the working file and change the deleted blob's
2207 * entry into a schedule-add entry.
2209 err = got_fileindex_entry_update(ie, worktree->root_fd,
2210 ie->path, NULL, NULL, 0);
2212 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2215 if (status == GOT_STATUS_NO_CHANGE) {
2216 err = remove_ondisk_file(worktree->root_path, ie->path);
2220 got_fileindex_entry_remove(fileindex, ie);
2227 struct diff_cb_arg {
2228 struct got_fileindex *fileindex;
2229 struct got_worktree *worktree;
2230 struct got_repository *repo;
2231 got_worktree_checkout_cb progress_cb;
2233 got_cancel_cb cancel_cb;
2237 static const struct got_error *
2238 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2239 struct got_tree_entry *te, const char *parent_path)
2241 struct diff_cb_arg *a = arg;
2243 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2244 return got_error(GOT_ERR_CANCELLED);
2246 return update_blob(a->worktree, a->fileindex, ie, te,
2247 ie->path, a->repo, a->progress_cb, a->progress_arg);
2250 static const struct got_error *
2251 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2253 struct diff_cb_arg *a = arg;
2255 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 return got_error(GOT_ERR_CANCELLED);
2258 return delete_blob(a->worktree, a->fileindex, ie,
2259 a->repo, a->progress_cb, a->progress_arg);
2262 static const struct got_error *
2263 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2265 struct diff_cb_arg *a = arg;
2266 const struct got_error *err;
2269 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2270 return got_error(GOT_ERR_CANCELLED);
2272 if (got_object_tree_entry_is_submodule(te))
2275 if (asprintf(&path, "%s%s%s", parent_path,
2276 parent_path[0] ? "/" : "", te->name)
2278 return got_error_from_errno("asprintf");
2280 if (S_ISDIR(te->mode))
2281 err = add_dir_on_disk(a->worktree, path);
2283 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2284 a->repo, a->progress_cb, a->progress_arg);
2290 const struct got_error *
2291 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2293 uint32_t uuid_status;
2295 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2296 if (uuid_status != uuid_s_ok) {
2298 return got_error_uuid(uuid_status, "uuid_to_string");
2304 static const struct got_error *
2305 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2307 const struct got_error *err = NULL;
2308 char *uuidstr = NULL;
2312 err = got_worktree_get_uuid(&uuidstr, worktree);
2316 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2317 err = got_error_from_errno("asprintf");
2324 const struct got_error *
2325 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2327 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2330 static const struct got_error *
2331 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2333 return get_ref_name(refname, worktree,
2334 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2337 static const struct got_error *
2338 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2340 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2343 static const struct got_error *
2344 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2346 return get_ref_name(refname, worktree,
2347 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2350 static const struct got_error *
2351 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree,
2354 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2357 static const struct got_error *
2358 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2360 return get_ref_name(refname, worktree,
2361 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2364 static const struct got_error *
2365 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2367 return get_ref_name(refname, worktree,
2368 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2371 static const struct got_error *
2372 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2374 return get_ref_name(refname, worktree,
2375 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2378 static const struct got_error *
2379 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2381 return get_ref_name(refname, worktree,
2382 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2385 const struct got_error *
2386 got_worktree_get_histedit_script_path(char **path,
2387 struct got_worktree *worktree)
2389 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2390 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2392 return got_error_from_errno("asprintf");
2397 static const struct got_error *
2398 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2400 return get_ref_name(refname, worktree,
2401 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2404 static const struct got_error *
2405 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2407 return get_ref_name(refname, worktree,
2408 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2412 * Prevent Git's garbage collector from deleting our base commit by
2413 * setting a reference to our base commit's ID.
2415 static const struct got_error *
2416 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2418 const struct got_error *err = NULL;
2419 struct got_reference *ref = NULL;
2422 err = got_worktree_get_base_ref_name(&refname, worktree);
2426 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2430 err = got_ref_write(ref, repo);
2438 static const struct got_error *
2439 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2441 const struct got_error *err = NULL;
2443 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2444 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2445 err = got_error_from_errno("asprintf");
2446 *fileindex_path = NULL;
2452 static const struct got_error *
2453 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2454 struct got_worktree *worktree)
2456 const struct got_error *err = NULL;
2459 *fileindex_path = NULL;
2460 *fileindex = got_fileindex_alloc();
2461 if (*fileindex == NULL)
2462 return got_error_from_errno("got_fileindex_alloc");
2464 err = get_fileindex_path(fileindex_path, worktree);
2468 index = fopen(*fileindex_path, "rb");
2469 if (index == NULL) {
2470 if (errno != ENOENT)
2471 err = got_error_from_errno2("fopen", *fileindex_path);
2473 err = got_fileindex_read(*fileindex, index);
2474 if (fclose(index) == EOF && err == NULL)
2475 err = got_error_from_errno("fclose");
2479 free(*fileindex_path);
2480 *fileindex_path = NULL;
2481 got_fileindex_free(*fileindex);
2487 struct bump_base_commit_id_arg {
2488 struct got_object_id *base_commit_id;
2491 const char *entry_name;
2492 got_worktree_checkout_cb progress_cb;
2496 /* Bump base commit ID of all files within an updated part of the work tree. */
2497 static const struct got_error *
2498 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2500 const struct got_error *err;
2501 struct bump_base_commit_id_arg *a = arg;
2503 if (a->entry_name) {
2504 if (strcmp(ie->path, a->path) != 0)
2506 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2509 if (got_fileindex_entry_was_skipped(ie))
2512 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2513 SHA1_DIGEST_LENGTH) == 0)
2516 if (a->progress_cb) {
2517 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2522 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2526 static const struct got_error *
2527 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2528 struct got_fileindex *fileindex,
2529 got_worktree_checkout_cb progress_cb, void *progress_arg)
2531 struct bump_base_commit_id_arg bbc_arg;
2533 bbc_arg.base_commit_id = worktree->base_commit_id;
2534 bbc_arg.entry_name = NULL;
2536 bbc_arg.path_len = 0;
2537 bbc_arg.progress_cb = progress_cb;
2538 bbc_arg.progress_arg = progress_arg;
2540 return got_fileindex_for_each_entry_safe(fileindex,
2541 bump_base_commit_id, &bbc_arg);
2544 static const struct got_error *
2545 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2547 const struct got_error *err = NULL;
2548 char *new_fileindex_path = NULL;
2549 FILE *new_index = NULL;
2550 struct timespec timeout;
2552 err = got_opentemp_named(&new_fileindex_path, &new_index,
2557 err = got_fileindex_write(fileindex, new_index);
2561 if (rename(new_fileindex_path, fileindex_path) != 0) {
2562 err = got_error_from_errno3("rename", new_fileindex_path,
2564 unlink(new_fileindex_path);
2568 * Sleep for a short amount of time to ensure that files modified after
2569 * this program exits have a different time stamp from the one which
2570 * was recorded in the file index.
2573 timeout.tv_nsec = 1;
2574 nanosleep(&timeout, NULL);
2578 free(new_fileindex_path);
2582 static const struct got_error *
2583 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2584 struct got_object_id **tree_id, const char *wt_relpath,
2585 struct got_worktree *worktree, struct got_repository *repo)
2587 const struct got_error *err = NULL;
2588 struct got_object_id *id = NULL;
2589 char *in_repo_path = NULL;
2590 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2592 *entry_type = GOT_OBJ_TYPE_ANY;
2593 *tree_relpath = NULL;
2596 if (wt_relpath[0] == '\0') {
2597 /* Check out all files within the work tree. */
2598 *entry_type = GOT_OBJ_TYPE_TREE;
2599 *tree_relpath = strdup("");
2600 if (*tree_relpath == NULL) {
2601 err = got_error_from_errno("strdup");
2604 err = got_object_id_by_path(tree_id, repo,
2605 worktree->base_commit_id, worktree->path_prefix);
2611 /* Check out a subset of files in the work tree. */
2613 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2614 is_root_wt ? "" : "/", wt_relpath) == -1) {
2615 err = got_error_from_errno("asprintf");
2619 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2625 in_repo_path = NULL;
2627 err = got_object_get_type(entry_type, repo, id);
2631 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2632 /* Check out a single file. */
2633 if (strchr(wt_relpath, '/') == NULL) {
2634 /* Check out a single file in work tree's root dir. */
2635 in_repo_path = strdup(worktree->path_prefix);
2636 if (in_repo_path == NULL) {
2637 err = got_error_from_errno("strdup");
2640 *tree_relpath = strdup("");
2641 if (*tree_relpath == NULL) {
2642 err = got_error_from_errno("strdup");
2646 /* Check out a single file in a subdirectory. */
2647 err = got_path_dirname(tree_relpath, wt_relpath);
2650 if (asprintf(&in_repo_path, "%s%s%s",
2651 worktree->path_prefix, is_root_wt ? "" : "/",
2652 *tree_relpath) == -1) {
2653 err = got_error_from_errno("asprintf");
2657 err = got_object_id_by_path(tree_id, repo,
2658 worktree->base_commit_id, in_repo_path);
2660 /* Check out all files within a subdirectory. */
2661 *tree_id = got_object_id_dup(id);
2662 if (*tree_id == NULL) {
2663 err = got_error_from_errno("got_object_id_dup");
2666 *tree_relpath = strdup(wt_relpath);
2667 if (*tree_relpath == NULL) {
2668 err = got_error_from_errno("strdup");
2676 *entry_type = GOT_OBJ_TYPE_ANY;
2677 free(*tree_relpath);
2678 *tree_relpath = NULL;
2685 static const struct got_error *
2686 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2687 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2688 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2689 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2691 const struct got_error *err = NULL;
2692 struct got_commit_object *commit = NULL;
2693 struct got_tree_object *tree = NULL;
2694 struct got_fileindex_diff_tree_cb diff_cb;
2695 struct diff_cb_arg arg;
2697 err = ref_base_commit(worktree, repo);
2699 if (!(err->code == GOT_ERR_ERRNO &&
2700 (errno == EACCES || errno == EROFS)))
2702 err = (*progress_cb)(progress_arg,
2703 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2708 err = got_object_open_as_commit(&commit, repo,
2709 worktree->base_commit_id);
2713 err = got_object_open_as_tree(&tree, repo, tree_id);
2718 got_object_tree_find_entry(tree, entry_name) == NULL) {
2719 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2723 diff_cb.diff_old_new = diff_old_new;
2724 diff_cb.diff_old = diff_old;
2725 diff_cb.diff_new = diff_new;
2726 arg.fileindex = fileindex;
2727 arg.worktree = worktree;
2729 arg.progress_cb = progress_cb;
2730 arg.progress_arg = progress_arg;
2731 arg.cancel_cb = cancel_cb;
2732 arg.cancel_arg = cancel_arg;
2733 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2734 entry_name, repo, &diff_cb, &arg);
2737 got_object_tree_close(tree);
2739 got_object_commit_close(commit);
2743 const struct got_error *
2744 got_worktree_checkout_files(struct got_worktree *worktree,
2745 struct got_pathlist_head *paths, struct got_repository *repo,
2746 got_worktree_checkout_cb progress_cb, void *progress_arg,
2747 got_cancel_cb cancel_cb, void *cancel_arg)
2749 const struct got_error *err = NULL, *sync_err, *unlockerr;
2750 struct got_commit_object *commit = NULL;
2751 struct got_tree_object *tree = NULL;
2752 struct got_fileindex *fileindex = NULL;
2753 char *fileindex_path = NULL;
2754 struct got_pathlist_entry *pe;
2755 struct tree_path_data {
2756 STAILQ_ENTRY(tree_path_data) entry;
2757 struct got_object_id *tree_id;
2762 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2764 STAILQ_INIT(&tree_paths);
2766 err = lock_worktree(worktree, LOCK_EX);
2770 /* Map all specified paths to in-repository trees. */
2771 TAILQ_FOREACH(pe, paths, entry) {
2772 tpd = malloc(sizeof(*tpd));
2774 err = got_error_from_errno("malloc");
2778 err = find_tree_entry_for_checkout(&tpd->entry_type,
2779 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2785 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2786 err = got_path_basename(&tpd->entry_name, pe->path);
2794 tpd->entry_name = NULL;
2796 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2800 * Read the file index.
2801 * Checking out files is supposed to be an idempotent operation.
2802 * If the on-disk file index is incomplete we will try to complete it.
2804 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2808 tpd = STAILQ_FIRST(&tree_paths);
2809 TAILQ_FOREACH(pe, paths, entry) {
2810 struct bump_base_commit_id_arg bbc_arg;
2812 err = checkout_files(worktree, fileindex, tpd->relpath,
2813 tpd->tree_id, tpd->entry_name, repo,
2814 progress_cb, progress_arg, cancel_cb, cancel_arg);
2818 bbc_arg.base_commit_id = worktree->base_commit_id;
2819 bbc_arg.entry_name = tpd->entry_name;
2820 bbc_arg.path = pe->path;
2821 bbc_arg.path_len = pe->path_len;
2822 bbc_arg.progress_cb = progress_cb;
2823 bbc_arg.progress_arg = progress_arg;
2824 err = got_fileindex_for_each_entry_safe(fileindex,
2825 bump_base_commit_id, &bbc_arg);
2829 tpd = STAILQ_NEXT(tpd, entry);
2831 sync_err = sync_fileindex(fileindex, fileindex_path);
2832 if (sync_err && err == NULL)
2835 free(fileindex_path);
2837 got_object_tree_close(tree);
2839 got_object_commit_close(commit);
2841 got_fileindex_free(fileindex);
2842 while (!STAILQ_EMPTY(&tree_paths)) {
2843 tpd = STAILQ_FIRST(&tree_paths);
2844 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2849 unlockerr = lock_worktree(worktree, LOCK_SH);
2850 if (unlockerr && err == NULL)
2855 struct merge_file_cb_arg {
2856 struct got_worktree *worktree;
2857 struct got_fileindex *fileindex;
2858 got_worktree_checkout_cb progress_cb;
2860 got_cancel_cb cancel_cb;
2862 const char *label_orig;
2863 struct got_object_id *commit_id2;
2864 int allow_bad_symlinks;
2867 static const struct got_error *
2868 merge_file_cb(void *arg, struct got_blob_object *blob1,
2869 struct got_blob_object *blob2, struct got_object_id *id1,
2870 struct got_object_id *id2, const char *path1, const char *path2,
2871 mode_t mode1, mode_t mode2, struct got_repository *repo)
2873 static const struct got_error *err = NULL;
2874 struct merge_file_cb_arg *a = arg;
2875 struct got_fileindex_entry *ie;
2876 char *ondisk_path = NULL;
2878 unsigned char status;
2879 int local_changes_subsumed;
2880 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2881 char *id_str = NULL, *label_deriv2 = NULL;
2883 if (blob1 && blob2) {
2884 ie = got_fileindex_entry_get(a->fileindex, path2,
2887 return (*a->progress_cb)(a->progress_arg,
2888 GOT_STATUS_MISSING, path2);
2890 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2892 return got_error_from_errno("asprintf");
2894 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2899 if (status == GOT_STATUS_DELETE) {
2900 err = (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_MERGE, path2);
2904 if (status != GOT_STATUS_NO_CHANGE &&
2905 status != GOT_STATUS_MODIFY &&
2906 status != GOT_STATUS_CONFLICT &&
2907 status != GOT_STATUS_ADD) {
2908 err = (*a->progress_cb)(a->progress_arg, status, path2);
2912 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2914 err = got_object_blob_read_to_str(&link_target2, blob2);
2917 err = merge_symlink(a->worktree, blob1, ondisk_path,
2918 path2, a->label_orig, link_target2, a->commit_id2,
2919 repo, a->progress_cb, a->progress_arg);
2924 f_orig = got_opentemp();
2925 if (f_orig == NULL) {
2926 err = got_error_from_errno("got_opentemp");
2929 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2934 f_deriv2 = got_opentemp();
2935 if (f_deriv2 == NULL)
2937 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2942 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2944 err = got_error_from_errno2("open",
2948 f_deriv = fdopen(fd, "r");
2949 if (f_deriv == NULL) {
2950 err = got_error_from_errno2("fdopen",
2955 err = got_object_id_str(&id_str, a->commit_id2);
2958 if (asprintf(&label_deriv2, "%s: commit %s",
2959 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2960 err = got_error_from_errno("asprintf");
2963 err = merge_file(&local_changes_subsumed, a->worktree,
2964 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2965 sb.st_mode, a->label_orig, NULL, label_deriv2,
2966 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2967 a->progress_cb, a->progress_arg);
2970 ie = got_fileindex_entry_get(a->fileindex, path1,
2973 return (*a->progress_cb)(a->progress_arg,
2974 GOT_STATUS_MISSING, path1);
2976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2978 return got_error_from_errno("asprintf");
2980 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2986 case GOT_STATUS_NO_CHANGE:
2987 err = (*a->progress_cb)(a->progress_arg,
2988 GOT_STATUS_DELETE, path1);
2991 err = remove_ondisk_file(a->worktree->root_path, path1);
2995 got_fileindex_entry_mark_deleted_from_disk(ie);
2997 case GOT_STATUS_DELETE:
2998 case GOT_STATUS_MISSING:
2999 err = (*a->progress_cb)(a->progress_arg,
3000 GOT_STATUS_DELETE, path1);
3004 got_fileindex_entry_mark_deleted_from_disk(ie);
3006 case GOT_STATUS_ADD: {
3007 struct got_object_id *id;
3010 * Delete the added file only if its content already
3011 * exists in the repository.
3013 err = got_object_blob_file_create(&id, &blob1_f, path1);
3016 if (got_object_id_cmp(id, id1) == 0) {
3017 err = (*a->progress_cb)(a->progress_arg,
3018 GOT_STATUS_DELETE, path1);
3021 err = remove_ondisk_file(a->worktree->root_path,
3026 got_fileindex_entry_remove(a->fileindex,
3029 err = (*a->progress_cb)(a->progress_arg,
3030 GOT_STATUS_CANNOT_DELETE, path1);
3032 if (fclose(blob1_f) == EOF && err == NULL)
3033 err = got_error_from_errno("fclose");
3039 case GOT_STATUS_MODIFY:
3040 case GOT_STATUS_CONFLICT:
3041 err = (*a->progress_cb)(a->progress_arg,
3042 GOT_STATUS_CANNOT_DELETE, path1);
3046 case GOT_STATUS_OBSTRUCTED:
3047 err = (*a->progress_cb)(a->progress_arg, status, path1);
3055 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3057 return got_error_from_errno("asprintf");
3058 ie = got_fileindex_entry_get(a->fileindex, path2,
3061 err = get_file_status(&status, &sb, ie, ondisk_path,
3065 if (status != GOT_STATUS_NO_CHANGE &&
3066 status != GOT_STATUS_MODIFY &&
3067 status != GOT_STATUS_CONFLICT &&
3068 status != GOT_STATUS_ADD) {
3069 err = (*a->progress_cb)(a->progress_arg,
3073 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3075 err = got_object_blob_read_to_str(&link_target2,
3079 err = merge_symlink(a->worktree, NULL,
3080 ondisk_path, path2, a->label_orig,
3081 link_target2, a->commit_id2, repo,
3082 a->progress_cb, a->progress_arg);
3084 } else if (S_ISREG(sb.st_mode)) {
3085 err = merge_blob(&local_changes_subsumed,
3086 a->worktree, NULL, ondisk_path, path2,
3087 sb.st_mode, a->label_orig, blob2,
3088 a->commit_id2, repo, a->progress_cb,
3091 err = got_error_path(ondisk_path,
3092 GOT_ERR_FILE_OBSTRUCTED);
3096 if (status == GOT_STATUS_DELETE) {
3097 err = got_fileindex_entry_update(ie,
3098 a->worktree->root_fd, path2, blob2->id.sha1,
3099 a->worktree->base_commit_id->sha1, 0);
3104 int is_bad_symlink = 0;
3105 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3106 if (S_ISLNK(mode2)) {
3107 err = install_symlink(&is_bad_symlink,
3108 a->worktree, ondisk_path, path2, blob2, 0,
3109 0, 1, a->allow_bad_symlinks, repo,
3110 a->progress_cb, a->progress_arg);
3112 err = install_blob(a->worktree, ondisk_path, path2,
3113 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3114 a->progress_cb, a->progress_arg);
3118 err = got_fileindex_entry_alloc(&ie, path2);
3121 err = got_fileindex_entry_update(ie,
3122 a->worktree->root_fd, path2, NULL, NULL, 1);
3124 got_fileindex_entry_free(ie);
3127 err = got_fileindex_entry_add(a->fileindex, ie);
3129 got_fileindex_entry_free(ie);
3132 if (is_bad_symlink) {
3133 got_fileindex_entry_filetype_set(ie,
3134 GOT_FILEIDX_MODE_BAD_SYMLINK);
3139 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3140 err = got_error_from_errno("fclose");
3141 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3142 err = got_error_from_errno("fclose");
3143 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3144 err = got_error_from_errno("fclose");
3151 static const struct got_error *
3152 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3154 struct got_worktree *worktree = arg;
3156 /* Reject merges into a work tree with mixed base commits. */
3157 if (got_fileindex_entry_has_commit(ie) &&
3158 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3159 SHA1_DIGEST_LENGTH) != 0)
3160 return got_error(GOT_ERR_MIXED_COMMITS);
3165 struct check_merge_conflicts_arg {
3166 struct got_worktree *worktree;
3167 struct got_fileindex *fileindex;
3168 struct got_repository *repo;
3171 static const struct got_error *
3172 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3173 struct got_blob_object *blob2, struct got_object_id *id1,
3174 struct got_object_id *id2, const char *path1, const char *path2,
3175 mode_t mode1, mode_t mode2, struct got_repository *repo)
3177 const struct got_error *err = NULL;
3178 struct check_merge_conflicts_arg *a = arg;
3179 unsigned char status;
3181 struct got_fileindex_entry *ie;
3182 const char *path = path2 ? path2 : path1;
3183 struct got_object_id *id = id2 ? id2 : id1;
3189 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3193 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3195 return got_error_from_errno("asprintf");
3197 /* Reject merges into a work tree with conflicted files. */
3198 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3202 if (status == GOT_STATUS_CONFLICT)
3203 return got_error(GOT_ERR_CONFLICTS);
3208 static const struct got_error *
3209 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3210 const char *fileindex_path, struct got_object_id *commit_id1,
3211 struct got_object_id *commit_id2, struct got_repository *repo,
3212 got_worktree_checkout_cb progress_cb, void *progress_arg,
3213 got_cancel_cb cancel_cb, void *cancel_arg)
3215 const struct got_error *err = NULL, *sync_err;
3216 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3217 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3218 struct check_merge_conflicts_arg cmc_arg;
3219 struct merge_file_cb_arg arg;
3220 char *label_orig = NULL;
3223 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3224 worktree->path_prefix);
3225 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3231 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3235 err = got_object_id_str(&id_str, commit_id1);
3239 if (asprintf(&label_orig, "%s: commit %s",
3240 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3241 err = got_error_from_errno("asprintf");
3248 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3249 worktree->path_prefix);
3253 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3257 cmc_arg.worktree = worktree;
3258 cmc_arg.fileindex = fileindex;
3259 cmc_arg.repo = repo;
3260 err = got_diff_tree(tree1, tree2, "", "", repo,
3261 check_merge_conflicts, &cmc_arg, 0);
3265 arg.worktree = worktree;
3266 arg.fileindex = fileindex;
3267 arg.progress_cb = progress_cb;
3268 arg.progress_arg = progress_arg;
3269 arg.cancel_cb = cancel_cb;
3270 arg.cancel_arg = cancel_arg;
3271 arg.label_orig = label_orig;
3272 arg.commit_id2 = commit_id2;
3273 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3274 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3275 sync_err = sync_fileindex(fileindex, fileindex_path);
3276 if (sync_err && err == NULL)
3280 got_object_tree_close(tree1);
3282 got_object_tree_close(tree2);
3287 const struct got_error *
3288 got_worktree_merge_files(struct got_worktree *worktree,
3289 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3290 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3291 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3293 const struct got_error *err, *unlockerr;
3294 char *fileindex_path = NULL;
3295 struct got_fileindex *fileindex = NULL;
3297 err = lock_worktree(worktree, LOCK_EX);
3301 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3305 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3310 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3311 commit_id2, repo, progress_cb, progress_arg,
3312 cancel_cb, cancel_arg);
3315 got_fileindex_free(fileindex);
3316 free(fileindex_path);
3317 unlockerr = lock_worktree(worktree, LOCK_SH);
3318 if (unlockerr && err == NULL)
3323 struct diff_dir_cb_arg {
3324 struct got_fileindex *fileindex;
3325 struct got_worktree *worktree;
3326 const char *status_path;
3327 size_t status_path_len;
3328 struct got_repository *repo;
3329 got_worktree_status_cb status_cb;
3331 got_cancel_cb cancel_cb;
3333 /* A pathlist containing per-directory pathlists of ignore patterns. */
3334 struct got_pathlist_head *ignores;
3335 int report_unchanged;
3339 static const struct got_error *
3340 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3341 int dirfd, const char *de_name,
3342 got_worktree_status_cb status_cb, void *status_arg,
3343 struct got_repository *repo, int report_unchanged)
3345 const struct got_error *err = NULL;
3346 unsigned char status = GOT_STATUS_NO_CHANGE;
3347 unsigned char staged_status = get_staged_status(ie);
3349 struct got_object_id blob_id, commit_id, staged_blob_id;
3350 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3351 struct got_object_id *staged_blob_idp = NULL;
3353 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3357 if (status == GOT_STATUS_NO_CHANGE &&
3358 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3361 if (got_fileindex_entry_has_blob(ie)) {
3362 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3363 blob_idp = &blob_id;
3365 if (got_fileindex_entry_has_commit(ie)) {
3366 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3367 commit_idp = &commit_id;
3369 if (staged_status == GOT_STATUS_ADD ||
3370 staged_status == GOT_STATUS_MODIFY) {
3371 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3372 SHA1_DIGEST_LENGTH);
3373 staged_blob_idp = &staged_blob_id;
3376 return (*status_cb)(status_arg, status, staged_status,
3377 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3380 static const struct got_error *
3381 status_old_new(void *arg, struct got_fileindex_entry *ie,
3382 struct dirent *de, const char *parent_path, int dirfd)
3384 const struct got_error *err = NULL;
3385 struct diff_dir_cb_arg *a = arg;
3388 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3389 return got_error(GOT_ERR_CANCELLED);
3391 if (got_path_cmp(parent_path, a->status_path,
3392 strlen(parent_path), a->status_path_len) != 0 &&
3393 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3396 if (parent_path[0]) {
3397 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3398 parent_path, de->d_name) == -1)
3399 return got_error_from_errno("asprintf");
3401 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3403 return got_error_from_errno("asprintf");
3406 err = report_file_status(ie, abspath, dirfd, de->d_name,
3407 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3412 static const struct got_error *
3413 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3415 struct diff_dir_cb_arg *a = arg;
3416 struct got_object_id blob_id, commit_id;
3417 unsigned char status;
3419 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3420 return got_error(GOT_ERR_CANCELLED);
3422 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3425 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3426 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3427 if (got_fileindex_entry_has_file_on_disk(ie))
3428 status = GOT_STATUS_MISSING;
3430 status = GOT_STATUS_DELETE;
3431 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3432 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3436 free_ignorelist(struct got_pathlist_head *ignorelist)
3438 struct got_pathlist_entry *pe;
3440 TAILQ_FOREACH(pe, ignorelist, entry)
3441 free((char *)pe->path);
3442 got_pathlist_free(ignorelist);
3446 free_ignores(struct got_pathlist_head *ignores)
3448 struct got_pathlist_entry *pe;
3450 TAILQ_FOREACH(pe, ignores, entry) {
3451 struct got_pathlist_head *ignorelist = pe->data;
3452 free_ignorelist(ignorelist);
3453 free((char *)pe->path);
3455 got_pathlist_free(ignores);
3458 static const struct got_error *
3459 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3461 const struct got_error *err = NULL;
3462 struct got_pathlist_entry *pe = NULL;
3463 struct got_pathlist_head *ignorelist;
3464 char *line = NULL, *pattern, *dirpath = NULL;
3465 size_t linesize = 0;
3468 ignorelist = calloc(1, sizeof(*ignorelist));
3469 if (ignorelist == NULL)
3470 return got_error_from_errno("calloc");
3471 TAILQ_INIT(ignorelist);
3473 while ((linelen = getline(&line, &linesize, f)) != -1) {
3474 if (linelen > 0 && line[linelen - 1] == '\n')
3475 line[linelen - 1] = '\0';
3477 /* Git's ignores may contain comments. */
3481 /* Git's negated patterns are not (yet?) supported. */
3485 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3487 err = got_error_from_errno("asprintf");
3490 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3495 err = got_error_from_errno("getline");
3499 dirpath = strdup(path);
3500 if (dirpath == NULL) {
3501 err = got_error_from_errno("strdup");
3504 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3507 if (err || pe == NULL) {
3509 free_ignorelist(ignorelist);
3515 match_ignores(struct got_pathlist_head *ignores, const char *path)
3517 struct got_pathlist_entry *pe;
3519 /* Handle patterns which match in all directories. */
3520 TAILQ_FOREACH(pe, ignores, entry) {
3521 struct got_pathlist_head *ignorelist = pe->data;
3522 struct got_pathlist_entry *pi;
3524 TAILQ_FOREACH(pi, ignorelist, entry) {
3525 const char *p, *pattern = pi->path;
3527 if (strncmp(pattern, "**/", 3) != 0)
3532 if (fnmatch(pattern, p,
3533 FNM_PATHNAME | FNM_LEADING_DIR)) {
3534 /* Retry in next directory. */
3535 while (*p && *p != '/')
3547 * The ignores pathlist contains ignore lists from children before
3548 * parents, so we can find the most specific ignorelist by walking
3549 * ignores backwards.
3551 pe = TAILQ_LAST(ignores, got_pathlist_head);
3553 if (got_path_is_child(path, pe->path, pe->path_len)) {
3554 struct got_pathlist_head *ignorelist = pe->data;
3555 struct got_pathlist_entry *pi;
3556 TAILQ_FOREACH(pi, ignorelist, entry) {
3557 const char *pattern = pi->path;
3558 int flags = FNM_LEADING_DIR;
3559 if (strstr(pattern, "/**/") == NULL)
3560 flags |= FNM_PATHNAME;
3561 if (fnmatch(pattern, path, flags))
3566 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3572 static const struct got_error *
3573 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3574 const char *path, int dirfd, const char *ignores_filename)
3576 const struct got_error *err = NULL;
3579 FILE *ignoresfile = NULL;
3581 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3582 path[0] ? "/" : "", ignores_filename) == -1)
3583 return got_error_from_errno("asprintf");
3586 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3588 if (errno != ENOENT && errno != EACCES)
3589 err = got_error_from_errno2("openat",
3592 ignoresfile = fdopen(fd, "r");
3593 if (ignoresfile == NULL)
3594 err = got_error_from_errno2("fdopen",
3598 err = read_ignores(ignores, path, ignoresfile);
3602 ignoresfile = fopen(ignorespath, "r");
3603 if (ignoresfile == NULL) {
3604 if (errno != ENOENT && errno != EACCES)
3605 err = got_error_from_errno2("fopen",
3608 err = read_ignores(ignores, path, ignoresfile);
3611 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3612 err = got_error_from_errno2("fclose", path);
3613 if (fd != -1 && close(fd) == -1 && err == NULL)
3614 err = got_error_from_errno2("close", path);
3619 static const struct got_error *
3620 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3623 const struct got_error *err = NULL;
3624 struct diff_dir_cb_arg *a = arg;
3630 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3631 return got_error(GOT_ERR_CANCELLED);
3633 if (parent_path[0]) {
3634 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3635 return got_error_from_errno("asprintf");
3640 if (de->d_type == DT_DIR) {
3641 if (!a->no_ignores && ignore != NULL &&
3642 match_ignores(a->ignores, path))
3644 } else if (!match_ignores(a->ignores, path) &&
3645 got_path_is_child(path, a->status_path, a->status_path_len))
3646 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3647 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3653 static const struct got_error *
3654 status_traverse(void *arg, const char *path, int dirfd)
3656 const struct got_error *err = NULL;
3657 struct diff_dir_cb_arg *a = arg;
3662 err = add_ignores(a->ignores, a->worktree->root_path,
3663 path, dirfd, ".cvsignore");
3667 err = add_ignores(a->ignores, a->worktree->root_path, path,
3668 dirfd, ".gitignore");
3673 static const struct got_error *
3674 report_single_file_status(const char *path, const char *ondisk_path,
3675 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3676 void *status_arg, struct got_repository *repo, int report_unchanged,
3677 struct got_pathlist_head *ignores, int no_ignores)
3679 struct got_fileindex_entry *ie;
3682 if (!no_ignores && match_ignores(ignores, path))
3685 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3687 return report_file_status(ie, ondisk_path, -1, NULL,
3688 status_cb, status_arg, repo, report_unchanged);
3690 if (lstat(ondisk_path, &sb) == -1) {
3691 if (errno != ENOENT)
3692 return got_error_from_errno2("lstat", ondisk_path);
3693 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3694 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3698 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3699 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3700 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3705 static const struct got_error *
3706 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3707 const char *root_path, const char *path)
3709 const struct got_error *err;
3710 char *parent_path, *next_parent_path = NULL;
3712 err = add_ignores(ignores, root_path, "", -1,
3717 err = add_ignores(ignores, root_path, "", -1,
3722 err = got_path_dirname(&parent_path, path);
3724 if (err->code == GOT_ERR_BAD_PATH)
3725 return NULL; /* cannot traverse parent */
3729 err = add_ignores(ignores, root_path, parent_path, -1,
3733 err = add_ignores(ignores, root_path, parent_path, -1,
3737 err = got_path_dirname(&next_parent_path, parent_path);
3739 if (err->code == GOT_ERR_BAD_PATH)
3740 err = NULL; /* traversed everything */
3743 if (got_path_is_root_dir(parent_path))
3746 parent_path = next_parent_path;
3747 next_parent_path = NULL;
3751 free(next_parent_path);
3755 static const struct got_error *
3756 worktree_status(struct got_worktree *worktree, const char *path,
3757 struct got_fileindex *fileindex, struct got_repository *repo,
3758 got_worktree_status_cb status_cb, void *status_arg,
3759 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3760 int report_unchanged)
3762 const struct got_error *err = NULL;
3764 struct got_fileindex_diff_dir_cb fdiff_cb;
3765 struct diff_dir_cb_arg arg;
3766 char *ondisk_path = NULL;
3767 struct got_pathlist_head ignores;
3769 TAILQ_INIT(&ignores);
3771 if (asprintf(&ondisk_path, "%s%s%s",
3772 worktree->root_path, path[0] ? "/" : "", path) == -1)
3773 return got_error_from_errno("asprintf");
3775 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3777 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3778 !got_err_open_nofollow_on_symlink())
3779 err = got_error_from_errno2("open", ondisk_path);
3782 err = add_ignores_from_parent_paths(&ignores,
3783 worktree->root_path, ondisk_path);
3787 err = report_single_file_status(path, ondisk_path,
3788 fileindex, status_cb, status_arg, repo,
3789 report_unchanged, &ignores, no_ignores);
3792 fdiff_cb.diff_old_new = status_old_new;
3793 fdiff_cb.diff_old = status_old;
3794 fdiff_cb.diff_new = status_new;
3795 fdiff_cb.diff_traverse = status_traverse;
3796 arg.fileindex = fileindex;
3797 arg.worktree = worktree;
3798 arg.status_path = path;
3799 arg.status_path_len = strlen(path);
3801 arg.status_cb = status_cb;
3802 arg.status_arg = status_arg;
3803 arg.cancel_cb = cancel_cb;
3804 arg.cancel_arg = cancel_arg;
3805 arg.report_unchanged = report_unchanged;
3806 arg.no_ignores = no_ignores;
3808 err = add_ignores_from_parent_paths(&ignores,
3809 worktree->root_path, path);
3813 arg.ignores = &ignores;
3814 err = got_fileindex_diff_dir(fileindex, fd,
3815 worktree->root_path, path, repo, &fdiff_cb, &arg);
3818 free_ignores(&ignores);
3819 if (fd != -1 && close(fd) == -1 && err == NULL)
3820 err = got_error_from_errno("close");
3825 const struct got_error *
3826 got_worktree_status(struct got_worktree *worktree,
3827 struct got_pathlist_head *paths, struct got_repository *repo,
3828 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3829 got_cancel_cb cancel_cb, void *cancel_arg)
3831 const struct got_error *err = NULL;
3832 char *fileindex_path = NULL;
3833 struct got_fileindex *fileindex = NULL;
3834 struct got_pathlist_entry *pe;
3836 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3840 TAILQ_FOREACH(pe, paths, entry) {
3841 err = worktree_status(worktree, pe->path, fileindex, repo,
3842 status_cb, status_arg, cancel_cb, cancel_arg,
3847 free(fileindex_path);
3848 got_fileindex_free(fileindex);
3852 const struct got_error *
3853 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3856 const struct got_error *err = NULL;
3857 char *resolved = NULL, *cwd = NULL, *path = NULL;
3860 char *abspath = NULL;
3861 char canonpath[PATH_MAX];
3865 cwd = getcwd(NULL, 0);
3867 return got_error_from_errno("getcwd");
3869 if (lstat(arg, &sb) == -1) {
3870 if (errno != ENOENT) {
3871 err = got_error_from_errno2("lstat", arg);
3876 if (S_ISLNK(sb.st_mode)) {
3878 * We cannot use realpath(3) with symlinks since we want to
3879 * operate on the symlink itself.
3880 * But we can make the path absolute, assuming it is relative
3881 * to the current working directory, and then canonicalize it.
3883 if (!got_path_is_absolute(arg)) {
3884 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3885 err = got_error_from_errno("asprintf");
3890 err = got_canonpath(abspath ? abspath : arg, canonpath,
3894 resolved = strdup(canonpath);
3895 if (resolved == NULL) {
3896 err = got_error_from_errno("strdup");
3900 resolved = realpath(arg, NULL);
3901 if (resolved == NULL) {
3902 if (errno != ENOENT) {
3903 err = got_error_from_errno2("realpath", arg);
3906 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3907 err = got_error_from_errno("asprintf");
3910 err = got_canonpath(abspath, canonpath,
3914 resolved = strdup(canonpath);
3915 if (resolved == NULL) {
3916 err = got_error_from_errno("strdup");
3922 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3923 strlen(got_worktree_get_root_path(worktree)))) {
3924 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3928 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3929 err = got_path_skip_common_ancestor(&path,
3930 got_worktree_get_root_path(worktree), resolved);
3936 err = got_error_from_errno("strdup");
3941 /* XXX status walk can't deal with trailing slash! */
3943 while (len > 0 && path[len - 1] == '/') {
3944 path[len - 1] = '\0';
3958 struct schedule_addition_args {
3959 struct got_worktree *worktree;
3960 struct got_fileindex *fileindex;
3961 got_worktree_checkout_cb progress_cb;
3963 struct got_repository *repo;
3966 static const struct got_error *
3967 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3968 const char *relpath, struct got_object_id *blob_id,
3969 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3970 int dirfd, const char *de_name)
3972 struct schedule_addition_args *a = arg;
3973 const struct got_error *err = NULL;
3974 struct got_fileindex_entry *ie;
3978 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3980 return got_error_from_errno("asprintf");
3982 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3984 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3988 /* Re-adding an existing entry is a no-op. */
3989 if (status == GOT_STATUS_ADD)
3991 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3996 if (status != GOT_STATUS_UNVERSIONED) {
3997 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4001 err = got_fileindex_entry_alloc(&ie, relpath);
4004 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4005 relpath, NULL, NULL, 1);
4007 got_fileindex_entry_free(ie);
4010 err = got_fileindex_entry_add(a->fileindex, ie);
4012 got_fileindex_entry_free(ie);
4019 if (status == GOT_STATUS_ADD)
4021 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4024 const struct got_error *
4025 got_worktree_schedule_add(struct got_worktree *worktree,
4026 struct got_pathlist_head *paths,
4027 got_worktree_checkout_cb progress_cb, void *progress_arg,
4028 struct got_repository *repo, int no_ignores)
4030 struct got_fileindex *fileindex = NULL;
4031 char *fileindex_path = NULL;
4032 const struct got_error *err = NULL, *sync_err, *unlockerr;
4033 struct got_pathlist_entry *pe;
4034 struct schedule_addition_args saa;
4036 err = lock_worktree(worktree, LOCK_EX);
4040 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4044 saa.worktree = worktree;
4045 saa.fileindex = fileindex;
4046 saa.progress_cb = progress_cb;
4047 saa.progress_arg = progress_arg;
4050 TAILQ_FOREACH(pe, paths, entry) {
4051 err = worktree_status(worktree, pe->path, fileindex, repo,
4052 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4056 sync_err = sync_fileindex(fileindex, fileindex_path);
4057 if (sync_err && err == NULL)
4060 free(fileindex_path);
4062 got_fileindex_free(fileindex);
4063 unlockerr = lock_worktree(worktree, LOCK_SH);
4064 if (unlockerr && err == NULL)
4069 struct schedule_deletion_args {
4070 struct got_worktree *worktree;
4071 struct got_fileindex *fileindex;
4072 got_worktree_delete_cb progress_cb;
4074 struct got_repository *repo;
4075 int delete_local_mods;
4077 const char *status_codes;
4080 static const struct got_error *
4081 schedule_for_deletion(void *arg, unsigned char status,
4082 unsigned char staged_status, const char *relpath,
4083 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4084 struct got_object_id *commit_id, int dirfd, const char *de_name)
4086 struct schedule_deletion_args *a = arg;
4087 const struct got_error *err = NULL;
4088 struct got_fileindex_entry *ie = NULL;
4092 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4094 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4096 staged_status = get_staged_status(ie);
4097 if (staged_status != GOT_STATUS_NO_CHANGE) {
4098 if (staged_status == GOT_STATUS_DELETE)
4100 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4103 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4105 return got_error_from_errno("asprintf");
4107 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4112 if (a->status_codes) {
4113 size_t ncodes = strlen(a->status_codes);
4115 for (i = 0; i < ncodes ; i++) {
4116 if (status == a->status_codes[i])
4120 /* Do not delete files in non-matching status. */
4124 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4125 a->status_codes[i] != GOT_STATUS_MISSING) {
4126 static char msg[64];
4127 snprintf(msg, sizeof(msg),
4128 "invalid status code '%c'", a->status_codes[i]);
4129 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4134 if (status != GOT_STATUS_NO_CHANGE) {
4135 if (status == GOT_STATUS_DELETE)
4137 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4138 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4141 if (status != GOT_STATUS_MODIFY &&
4142 status != GOT_STATUS_MISSING) {
4143 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4148 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4152 if (unlinkat(dirfd, de_name, 0) != 0) {
4153 err = got_error_from_errno2("unlinkat",
4157 } else if (unlink(ondisk_path) != 0) {
4158 err = got_error_from_errno2("unlink", ondisk_path);
4162 root_len = strlen(a->worktree->root_path);
4165 err = got_path_dirname(&parent, ondisk_path);
4169 ondisk_path = parent;
4170 if (rmdir(ondisk_path) == -1) {
4171 if (errno != ENOTEMPTY)
4172 err = got_error_from_errno2("rmdir",
4176 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4177 strlen(ondisk_path), root_len) != 0);
4180 got_fileindex_entry_mark_deleted_from_disk(ie);
4185 if (status == GOT_STATUS_DELETE)
4187 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4188 staged_status, relpath);
4191 const struct got_error *
4192 got_worktree_schedule_delete(struct got_worktree *worktree,
4193 struct got_pathlist_head *paths, int delete_local_mods,
4194 const char *status_codes,
4195 got_worktree_delete_cb progress_cb, void *progress_arg,
4196 struct got_repository *repo, int keep_on_disk)
4198 struct got_fileindex *fileindex = NULL;
4199 char *fileindex_path = NULL;
4200 const struct got_error *err = NULL, *sync_err, *unlockerr;
4201 struct got_pathlist_entry *pe;
4202 struct schedule_deletion_args sda;
4204 err = lock_worktree(worktree, LOCK_EX);
4208 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4212 sda.worktree = worktree;
4213 sda.fileindex = fileindex;
4214 sda.progress_cb = progress_cb;
4215 sda.progress_arg = progress_arg;
4217 sda.delete_local_mods = delete_local_mods;
4218 sda.keep_on_disk = keep_on_disk;
4219 sda.status_codes = status_codes;
4221 TAILQ_FOREACH(pe, paths, entry) {
4222 err = worktree_status(worktree, pe->path, fileindex, repo,
4223 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4227 sync_err = sync_fileindex(fileindex, fileindex_path);
4228 if (sync_err && err == NULL)
4231 free(fileindex_path);
4233 got_fileindex_free(fileindex);
4234 unlockerr = lock_worktree(worktree, LOCK_SH);
4235 if (unlockerr && err == NULL)
4240 static const struct got_error *
4241 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4243 const struct got_error *err = NULL;
4245 size_t linesize = 0, n;
4248 linelen = getline(&line, &linesize, infile);
4249 if (linelen == -1) {
4250 if (ferror(infile)) {
4251 err = got_error_from_errno("getline");
4257 n = fwrite(line, 1, linelen, outfile);
4259 err = got_ferror(outfile, GOT_ERR_IO);
4264 n = fwrite(line, 1, linelen, rejectfile);
4266 err = got_ferror(outfile, GOT_ERR_IO);
4273 static const struct got_error *
4274 skip_one_line(FILE *f)
4277 size_t linesize = 0;
4280 linelen = getline(&line, &linesize, f);
4281 if (linelen == -1) {
4283 return got_error_from_errno("getline");
4290 static const struct got_error *
4291 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4292 int start_old, int end_old, int start_new, int end_new,
4293 FILE *outfile, FILE *rejectfile)
4295 const struct got_error *err;
4297 /* Copy old file's lines leading up to patch. */
4298 while (!feof(f1) && *line_cur1 < start_old) {
4299 err = copy_one_line(f1, outfile, NULL);
4304 /* Skip new file's lines leading up to patch. */
4305 while (!feof(f2) && *line_cur2 < start_new) {
4307 err = copy_one_line(f2, NULL, rejectfile);
4309 err = skip_one_line(f2);
4314 /* Copy patched lines. */
4315 while (!feof(f2) && *line_cur2 <= end_new) {
4316 err = copy_one_line(f2, outfile, NULL);
4321 /* Skip over old file's replaced lines. */
4322 while (!feof(f1) && *line_cur1 <= end_old) {
4324 err = copy_one_line(f1, NULL, rejectfile);
4326 err = skip_one_line(f1);
4335 static const struct got_error *
4336 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4337 FILE *outfile, FILE *rejectfile)
4339 const struct got_error *err;
4342 /* Copy old file's lines until EOF. */
4344 err = copy_one_line(f1, outfile, NULL);
4351 /* Copy new file's lines until EOF. */
4353 err = copy_one_line(f2, NULL, rejectfile);
4363 static const struct got_error *
4364 apply_or_reject_change(int *choice, int *nchunks_used,
4365 struct diff_result *diff_result, int n,
4366 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4367 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4368 got_worktree_patch_cb patch_cb, void *patch_arg)
4370 const struct got_error *err = NULL;
4371 struct diff_chunk_context cc = {};
4372 int start_old, end_old, start_new, end_new;
4374 struct diff_output_unidiff_state *diff_state;
4375 struct diff_input_info diff_info;
4378 *choice = GOT_PATCH_CHOICE_NONE;
4380 /* Get changed line numbers without context lines for copy_change(). */
4381 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4382 start_old = cc.left.start;
4383 end_old = cc.left.end;
4384 start_new = cc.right.start;
4385 end_new = cc.right.end;
4387 /* Get the same change with context lines for display. */
4388 memset(&cc, 0, sizeof(cc));
4389 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4391 memset(&diff_info, 0, sizeof(diff_info));
4392 diff_info.left_path = relpath;
4393 diff_info.right_path = relpath;
4395 diff_state = diff_output_unidiff_state_alloc();
4396 if (diff_state == NULL)
4397 return got_error_set_errno(ENOMEM,
4398 "diff_output_unidiff_state_alloc");
4400 hunkfile = got_opentemp();
4401 if (hunkfile == NULL) {
4402 err = got_error_from_errno("got_opentemp");
4406 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4408 if (rc != DIFF_RC_OK) {
4409 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4413 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4414 err = got_ferror(hunkfile, GOT_ERR_IO);
4418 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4419 hunkfile, changeno, nchanges);
4424 case GOT_PATCH_CHOICE_YES:
4425 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4426 end_old, start_new, end_new, outfile, rejectfile);
4428 case GOT_PATCH_CHOICE_NO:
4429 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4430 end_old, start_new, end_new, rejectfile, outfile);
4432 case GOT_PATCH_CHOICE_QUIT:
4435 err = got_error(GOT_ERR_PATCH_CHOICE);
4439 diff_output_unidiff_state_free(diff_state);
4440 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4441 err = got_error_from_errno("fclose");
4445 struct revert_file_args {
4446 struct got_worktree *worktree;
4447 struct got_fileindex *fileindex;
4448 got_worktree_checkout_cb progress_cb;
4450 got_worktree_patch_cb patch_cb;
4452 struct got_repository *repo;
4453 int unlink_added_files;
4456 static const struct got_error *
4457 create_patched_content(char **path_outfile, int reverse_patch,
4458 struct got_object_id *blob_id, const char *path2,
4459 int dirfd2, const char *de_name2,
4460 const char *relpath, struct got_repository *repo,
4461 got_worktree_patch_cb patch_cb, void *patch_arg)
4463 const struct got_error *err, *free_err;
4464 struct got_blob_object *blob = NULL;
4465 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4467 char link_target[PATH_MAX];
4468 ssize_t link_len = 0;
4469 char *path1 = NULL, *id_str = NULL;
4471 struct got_diffreg_result *diffreg_result = NULL;
4472 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4473 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4475 *path_outfile = NULL;
4477 err = got_object_id_str(&id_str, blob_id);
4482 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4484 if (!got_err_open_nofollow_on_symlink()) {
4485 err = got_error_from_errno2("openat", path2);
4488 link_len = readlinkat(dirfd2, de_name2,
4489 link_target, sizeof(link_target));
4491 return got_error_from_errno2("readlinkat", path2);
4492 sb2.st_mode = S_IFLNK;
4493 sb2.st_size = link_len;
4496 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4498 if (!got_err_open_nofollow_on_symlink()) {
4499 err = got_error_from_errno2("open", path2);
4502 link_len = readlink(path2, link_target,
4503 sizeof(link_target));
4505 return got_error_from_errno2("readlink", path2);
4506 sb2.st_mode = S_IFLNK;
4507 sb2.st_size = link_len;
4511 if (fstat(fd2, &sb2) == -1) {
4512 err = got_error_from_errno2("fstat", path2);
4516 f2 = fdopen(fd2, "r");
4518 err = got_error_from_errno2("fdopen", path2);
4524 f2 = got_opentemp();
4526 err = got_error_from_errno2("got_opentemp", path2);
4529 n = fwrite(link_target, 1, link_len, f2);
4530 if (n != link_len) {
4531 err = got_ferror(f2, GOT_ERR_IO);
4534 if (fflush(f2) == EOF) {
4535 err = got_error_from_errno("fflush");
4541 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4545 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4549 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4553 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4558 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4562 if (fseek(f1, 0L, SEEK_SET) == -1)
4563 return got_ferror(f1, GOT_ERR_IO);
4564 if (fseek(f2, 0L, SEEK_SET) == -1)
4565 return got_ferror(f2, GOT_ERR_IO);
4567 /* Count the number of actual changes in the diff result. */
4568 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4569 struct diff_chunk_context cc = {};
4570 diff_chunk_context_load_change(&cc, &nchunks_used,
4571 diffreg_result->result, n, 0);
4574 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4576 err = apply_or_reject_change(&choice, &nchunks_used,
4577 diffreg_result->result, n, relpath, f1, f2,
4578 &line_cur1, &line_cur2,
4579 reverse_patch ? NULL : outfile,
4580 reverse_patch ? outfile : NULL,
4581 ++i, nchanges, patch_cb, patch_arg);
4584 if (choice == GOT_PATCH_CHOICE_YES)
4586 else if (choice == GOT_PATCH_CHOICE_QUIT)
4590 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4591 reverse_patch ? NULL : outfile,
4592 reverse_patch ? outfile : NULL);
4596 if (!S_ISLNK(sb2.st_mode)) {
4597 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4598 err = got_error_from_errno2("fchmod", path2);
4606 got_object_blob_close(blob);
4607 free_err = got_diffreg_result_free(diffreg_result);
4610 if (f1 && fclose(f1) == EOF && err == NULL)
4611 err = got_error_from_errno2("fclose", path1);
4612 if (f2 && fclose(f2) == EOF && err == NULL)
4613 err = got_error_from_errno2("fclose", path2);
4614 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4615 err = got_error_from_errno2("close", path2);
4616 if (outfile && fclose(outfile) == EOF && err == NULL)
4617 err = got_error_from_errno2("fclose", *path_outfile);
4618 if (path1 && unlink(path1) == -1 && err == NULL)
4619 err = got_error_from_errno2("unlink", path1);
4620 if (err || !have_content) {
4621 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4622 err = got_error_from_errno2("unlink", *path_outfile);
4623 free(*path_outfile);
4624 *path_outfile = NULL;
4630 static const struct got_error *
4631 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4632 const char *relpath, struct got_object_id *blob_id,
4633 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4634 int dirfd, const char *de_name)
4636 struct revert_file_args *a = arg;
4637 const struct got_error *err = NULL;
4638 char *parent_path = NULL;
4639 struct got_fileindex_entry *ie;
4640 struct got_tree_object *tree = NULL;
4641 struct got_object_id *tree_id = NULL;
4642 const struct got_tree_entry *te = NULL;
4643 char *tree_path = NULL, *te_name;
4644 char *ondisk_path = NULL, *path_content = NULL;
4645 struct got_blob_object *blob = NULL;
4647 /* Reverting a staged deletion is a no-op. */
4648 if (status == GOT_STATUS_DELETE &&
4649 staged_status != GOT_STATUS_NO_CHANGE)
4652 if (status == GOT_STATUS_UNVERSIONED)
4653 return (*a->progress_cb)(a->progress_arg,
4654 GOT_STATUS_UNVERSIONED, relpath);
4656 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4658 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4660 /* Construct in-repository path of tree which contains this blob. */
4661 err = got_path_dirname(&parent_path, ie->path);
4663 if (err->code != GOT_ERR_BAD_PATH)
4665 parent_path = strdup("/");
4666 if (parent_path == NULL) {
4667 err = got_error_from_errno("strdup");
4671 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4672 tree_path = strdup(parent_path);
4673 if (tree_path == NULL) {
4674 err = got_error_from_errno("strdup");
4678 if (got_path_is_root_dir(parent_path)) {
4679 tree_path = strdup(a->worktree->path_prefix);
4680 if (tree_path == NULL) {
4681 err = got_error_from_errno("strdup");
4685 if (asprintf(&tree_path, "%s/%s",
4686 a->worktree->path_prefix, parent_path) == -1) {
4687 err = got_error_from_errno("asprintf");
4693 err = got_object_id_by_path(&tree_id, a->repo,
4694 a->worktree->base_commit_id, tree_path);
4696 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4697 (status == GOT_STATUS_ADD ||
4698 staged_status == GOT_STATUS_ADD)))
4701 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4705 err = got_path_basename(&te_name, ie->path);
4709 te = got_object_tree_find_entry(tree, te_name);
4711 if (te == NULL && status != GOT_STATUS_ADD &&
4712 staged_status != GOT_STATUS_ADD) {
4713 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4719 case GOT_STATUS_ADD:
4721 int choice = GOT_PATCH_CHOICE_NONE;
4722 err = (*a->patch_cb)(&choice, a->patch_arg,
4723 status, ie->path, NULL, 1, 1);
4726 if (choice != GOT_PATCH_CHOICE_YES)
4729 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4733 got_fileindex_entry_remove(a->fileindex, ie);
4734 if (a->unlink_added_files) {
4735 if (asprintf(&ondisk_path, "%s/%s",
4736 got_worktree_get_root_path(a->worktree),
4738 err = got_error_from_errno("asprintf");
4741 if (unlink(ondisk_path) == -1) {
4742 err = got_error_from_errno2("unlink",
4748 case GOT_STATUS_DELETE:
4750 int choice = GOT_PATCH_CHOICE_NONE;
4751 err = (*a->patch_cb)(&choice, a->patch_arg,
4752 status, ie->path, NULL, 1, 1);
4755 if (choice != GOT_PATCH_CHOICE_YES)
4759 case GOT_STATUS_MODIFY:
4760 case GOT_STATUS_MODE_CHANGE:
4761 case GOT_STATUS_CONFLICT:
4762 case GOT_STATUS_MISSING: {
4763 struct got_object_id id;
4764 if (staged_status == GOT_STATUS_ADD ||
4765 staged_status == GOT_STATUS_MODIFY) {
4766 memcpy(id.sha1, ie->staged_blob_sha1,
4767 SHA1_DIGEST_LENGTH);
4769 memcpy(id.sha1, ie->blob_sha1,
4770 SHA1_DIGEST_LENGTH);
4771 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4775 if (asprintf(&ondisk_path, "%s/%s",
4776 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4777 err = got_error_from_errno("asprintf");
4781 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4782 status == GOT_STATUS_CONFLICT)) {
4783 int is_bad_symlink = 0;
4784 err = create_patched_content(&path_content, 1, &id,
4785 ondisk_path, dirfd, de_name, ie->path, a->repo,
4786 a->patch_cb, a->patch_arg);
4787 if (err || path_content == NULL)
4789 if (te && S_ISLNK(te->mode)) {
4790 if (unlink(path_content) == -1) {
4791 err = got_error_from_errno2("unlink",
4795 err = install_symlink(&is_bad_symlink,
4796 a->worktree, ondisk_path, ie->path,
4797 blob, 0, 1, 0, 0, a->repo,
4798 a->progress_cb, a->progress_arg);
4800 if (rename(path_content, ondisk_path) == -1) {
4801 err = got_error_from_errno3("rename",
4802 path_content, ondisk_path);
4807 int is_bad_symlink = 0;
4808 if (te && S_ISLNK(te->mode)) {
4809 err = install_symlink(&is_bad_symlink,
4810 a->worktree, ondisk_path, ie->path,
4811 blob, 0, 1, 0, 0, a->repo,
4812 a->progress_cb, a->progress_arg);
4814 err = install_blob(a->worktree, ondisk_path,
4816 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4817 got_fileindex_perms_to_st(ie), blob,
4818 0, 1, 0, 0, a->repo,
4819 a->progress_cb, a->progress_arg);
4823 if (status == GOT_STATUS_DELETE ||
4824 status == GOT_STATUS_MODE_CHANGE) {
4825 err = got_fileindex_entry_update(ie,
4826 a->worktree->root_fd, relpath,
4828 a->worktree->base_commit_id->sha1, 1);
4832 if (is_bad_symlink) {
4833 got_fileindex_entry_filetype_set(ie,
4834 GOT_FILEIDX_MODE_BAD_SYMLINK);
4848 got_object_blob_close(blob);
4850 got_object_tree_close(tree);
4855 const struct got_error *
4856 got_worktree_revert(struct got_worktree *worktree,
4857 struct got_pathlist_head *paths,
4858 got_worktree_checkout_cb progress_cb, void *progress_arg,
4859 got_worktree_patch_cb patch_cb, void *patch_arg,
4860 struct got_repository *repo)
4862 struct got_fileindex *fileindex = NULL;
4863 char *fileindex_path = NULL;
4864 const struct got_error *err = NULL, *unlockerr = NULL;
4865 const struct got_error *sync_err = NULL;
4866 struct got_pathlist_entry *pe;
4867 struct revert_file_args rfa;
4869 err = lock_worktree(worktree, LOCK_EX);
4873 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4877 rfa.worktree = worktree;
4878 rfa.fileindex = fileindex;
4879 rfa.progress_cb = progress_cb;
4880 rfa.progress_arg = progress_arg;
4881 rfa.patch_cb = patch_cb;
4882 rfa.patch_arg = patch_arg;
4884 rfa.unlink_added_files = 0;
4885 TAILQ_FOREACH(pe, paths, entry) {
4886 err = worktree_status(worktree, pe->path, fileindex, repo,
4887 revert_file, &rfa, NULL, NULL, 1, 0);
4891 sync_err = sync_fileindex(fileindex, fileindex_path);
4892 if (sync_err && err == NULL)
4895 free(fileindex_path);
4897 got_fileindex_free(fileindex);
4898 unlockerr = lock_worktree(worktree, LOCK_SH);
4899 if (unlockerr && err == NULL)
4905 free_commitable(struct got_commitable *ct)
4908 free(ct->in_repo_path);
4909 free(ct->ondisk_path);
4911 free(ct->base_blob_id);
4912 free(ct->staged_blob_id);
4913 free(ct->base_commit_id);
4917 struct collect_commitables_arg {
4918 struct got_pathlist_head *commitable_paths;
4919 struct got_repository *repo;
4920 struct got_worktree *worktree;
4921 struct got_fileindex *fileindex;
4922 int have_staged_files;
4923 int allow_bad_symlinks;
4926 static const struct got_error *
4927 collect_commitables(void *arg, unsigned char status,
4928 unsigned char staged_status, const char *relpath,
4929 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4930 struct got_object_id *commit_id, int dirfd, const char *de_name)
4932 struct collect_commitables_arg *a = arg;
4933 const struct got_error *err = NULL;
4934 struct got_commitable *ct = NULL;
4935 struct got_pathlist_entry *new = NULL;
4936 char *parent_path = NULL, *path = NULL;
4939 if (a->have_staged_files) {
4940 if (staged_status != GOT_STATUS_MODIFY &&
4941 staged_status != GOT_STATUS_ADD &&
4942 staged_status != GOT_STATUS_DELETE)
4945 if (status == GOT_STATUS_CONFLICT)
4946 return got_error(GOT_ERR_COMMIT_CONFLICT);
4948 if (status != GOT_STATUS_MODIFY &&
4949 status != GOT_STATUS_MODE_CHANGE &&
4950 status != GOT_STATUS_ADD &&
4951 status != GOT_STATUS_DELETE)
4955 if (asprintf(&path, "/%s", relpath) == -1) {
4956 err = got_error_from_errno("asprintf");
4959 if (strcmp(path, "/") == 0) {
4960 parent_path = strdup("");
4961 if (parent_path == NULL)
4962 return got_error_from_errno("strdup");
4964 err = got_path_dirname(&parent_path, path);
4969 ct = calloc(1, sizeof(*ct));
4971 err = got_error_from_errno("calloc");
4975 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4977 err = got_error_from_errno("asprintf");
4981 if (staged_status == GOT_STATUS_ADD ||
4982 staged_status == GOT_STATUS_MODIFY) {
4983 struct got_fileindex_entry *ie;
4984 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4985 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4986 case GOT_FILEIDX_MODE_REGULAR_FILE:
4987 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4990 case GOT_FILEIDX_MODE_SYMLINK:
4994 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4997 ct->mode |= got_fileindex_entry_perms_get(ie);
4998 } else if (status != GOT_STATUS_DELETE &&
4999 staged_status != GOT_STATUS_DELETE) {
5001 if (fstatat(dirfd, de_name, &sb,
5002 AT_SYMLINK_NOFOLLOW) == -1) {
5003 err = got_error_from_errno2("fstatat",
5007 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5008 err = got_error_from_errno2("lstat", ct->ondisk_path);
5011 ct->mode = sb.st_mode;
5014 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5015 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5017 err = got_error_from_errno("asprintf");
5021 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5022 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5024 char target_path[PATH_MAX];
5026 target_len = readlink(ct->ondisk_path, target_path,
5027 sizeof(target_path));
5028 if (target_len == -1) {
5029 err = got_error_from_errno2("readlink",
5033 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5034 target_len, ct->ondisk_path, a->worktree->root_path);
5037 if (is_bad_symlink) {
5038 err = got_error_path(ct->ondisk_path,
5039 GOT_ERR_BAD_SYMLINK);
5045 ct->status = status;
5046 ct->staged_status = staged_status;
5047 ct->blob_id = NULL; /* will be filled in when blob gets created */
5048 if (ct->status != GOT_STATUS_ADD &&
5049 ct->staged_status != GOT_STATUS_ADD) {
5050 ct->base_blob_id = got_object_id_dup(blob_id);
5051 if (ct->base_blob_id == NULL) {
5052 err = got_error_from_errno("got_object_id_dup");
5055 ct->base_commit_id = got_object_id_dup(commit_id);
5056 if (ct->base_commit_id == NULL) {
5057 err = got_error_from_errno("got_object_id_dup");
5061 if (ct->staged_status == GOT_STATUS_ADD ||
5062 ct->staged_status == GOT_STATUS_MODIFY) {
5063 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5064 if (ct->staged_blob_id == NULL) {
5065 err = got_error_from_errno("got_object_id_dup");
5069 ct->path = strdup(path);
5070 if (ct->path == NULL) {
5071 err = got_error_from_errno("strdup");
5074 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5076 if (ct && (err || new == NULL))
5077 free_commitable(ct);
5083 static const struct got_error *write_tree(struct got_object_id **, int *,
5084 struct got_tree_object *, const char *, struct got_pathlist_head *,
5085 got_worktree_status_cb status_cb, void *status_arg,
5086 struct got_repository *);
5088 static const struct got_error *
5089 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5090 struct got_tree_entry *te, const char *parent_path,
5091 struct got_pathlist_head *commitable_paths,
5092 got_worktree_status_cb status_cb, void *status_arg,
5093 struct got_repository *repo)
5095 const struct got_error *err = NULL;
5096 struct got_tree_object *subtree;
5099 if (asprintf(&subpath, "%s%s%s", parent_path,
5100 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5101 return got_error_from_errno("asprintf");
5103 err = got_object_open_as_tree(&subtree, repo, &te->id);
5107 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5108 commitable_paths, status_cb, status_arg, repo);
5109 got_object_tree_close(subtree);
5114 static const struct got_error *
5115 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5117 const struct got_error *err = NULL;
5118 char *ct_parent_path = NULL;
5122 if (strchr(ct->in_repo_path, '/') == NULL) {
5123 *match = got_path_is_root_dir(path);
5127 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5130 *match = (strcmp(path, ct_parent_path) == 0);
5131 free(ct_parent_path);
5136 get_ct_file_mode(struct got_commitable *ct)
5138 if (S_ISLNK(ct->mode))
5141 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5144 static const struct got_error *
5145 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5146 struct got_tree_entry *te, struct got_commitable *ct)
5148 const struct got_error *err = NULL;
5152 err = got_object_tree_entry_dup(new_te, te);
5156 (*new_te)->mode = get_ct_file_mode(ct);
5158 if (ct->staged_status == GOT_STATUS_MODIFY)
5159 memcpy(&(*new_te)->id, ct->staged_blob_id,
5160 sizeof((*new_te)->id));
5162 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5164 if (err && *new_te) {
5171 static const struct got_error *
5172 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5173 struct got_commitable *ct)
5175 const struct got_error *err = NULL;
5176 char *ct_name = NULL;
5180 *new_te = calloc(1, sizeof(**new_te));
5181 if (*new_te == NULL)
5182 return got_error_from_errno("calloc");
5184 err = got_path_basename(&ct_name, ct->path);
5187 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5188 sizeof((*new_te)->name)) {
5189 err = got_error(GOT_ERR_NO_SPACE);
5193 (*new_te)->mode = get_ct_file_mode(ct);
5195 if (ct->staged_status == GOT_STATUS_ADD)
5196 memcpy(&(*new_te)->id, ct->staged_blob_id,
5197 sizeof((*new_te)->id));
5199 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5202 if (err && *new_te) {
5209 static const struct got_error *
5210 insert_tree_entry(struct got_tree_entry *new_te,
5211 struct got_pathlist_head *paths)
5213 const struct got_error *err = NULL;
5214 struct got_pathlist_entry *new_pe;
5216 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5220 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5224 static const struct got_error *
5225 report_ct_status(struct got_commitable *ct,
5226 got_worktree_status_cb status_cb, void *status_arg)
5228 const char *ct_path = ct->path;
5229 unsigned char status;
5231 if (status_cb == NULL) /* no commit progress output desired */
5234 while (ct_path[0] == '/')
5237 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5238 status = ct->staged_status;
5240 status = ct->status;
5242 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5243 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5246 static const struct got_error *
5247 match_modified_subtree(int *modified, struct got_tree_entry *te,
5248 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5250 const struct got_error *err = NULL;
5251 struct got_pathlist_entry *pe;
5256 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5257 got_path_is_root_dir(base_tree_path) ? "" : "/",
5259 return got_error_from_errno("asprintf");
5261 TAILQ_FOREACH(pe, commitable_paths, entry) {
5262 struct got_commitable *ct = pe->data;
5263 *modified = got_path_is_child(ct->in_repo_path, te_path,
5273 static const struct got_error *
5274 match_deleted_or_modified_ct(struct got_commitable **ctp,
5275 struct got_tree_entry *te, const char *base_tree_path,
5276 struct got_pathlist_head *commitable_paths)
5278 const struct got_error *err = NULL;
5279 struct got_pathlist_entry *pe;
5283 TAILQ_FOREACH(pe, commitable_paths, entry) {
5284 struct got_commitable *ct = pe->data;
5285 char *ct_name = NULL;
5288 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5289 if (ct->status != GOT_STATUS_MODIFY &&
5290 ct->status != GOT_STATUS_MODE_CHANGE &&
5291 ct->status != GOT_STATUS_DELETE)
5294 if (ct->staged_status != GOT_STATUS_MODIFY &&
5295 ct->staged_status != GOT_STATUS_DELETE)
5299 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5302 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5308 err = got_path_basename(&ct_name, pe->path);
5312 if (strcmp(te->name, ct_name) != 0) {
5325 static const struct got_error *
5326 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5327 const char *child_path, const char *path_base_tree,
5328 struct got_pathlist_head *commitable_paths,
5329 got_worktree_status_cb status_cb, void *status_arg,
5330 struct got_repository *repo)
5332 const struct got_error *err = NULL;
5333 struct got_tree_entry *new_te;
5335 struct got_object_id *id = NULL;
5340 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5341 got_path_is_root_dir(path_base_tree) ? "" : "/",
5343 return got_error_from_errno("asprintf");
5345 new_te = calloc(1, sizeof(*new_te));
5347 return got_error_from_errno("calloc");
5348 new_te->mode = S_IFDIR;
5350 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5351 sizeof(new_te->name)) {
5352 err = got_error(GOT_ERR_NO_SPACE);
5355 err = write_tree(&id, &nentries, NULL, subtree_path,
5356 commitable_paths, status_cb, status_arg, repo);
5361 memcpy(&new_te->id, id, sizeof(new_te->id));
5370 static const struct got_error *
5371 write_tree(struct got_object_id **new_tree_id, int *nentries,
5372 struct got_tree_object *base_tree, const char *path_base_tree,
5373 struct got_pathlist_head *commitable_paths,
5374 got_worktree_status_cb status_cb, void *status_arg,
5375 struct got_repository *repo)
5377 const struct got_error *err = NULL;
5378 struct got_pathlist_head paths;
5379 struct got_tree_entry *te, *new_te = NULL;
5380 struct got_pathlist_entry *pe;
5385 /* Insert, and recurse into, newly added entries first. */
5386 TAILQ_FOREACH(pe, commitable_paths, entry) {
5387 struct got_commitable *ct = pe->data;
5388 char *child_path = NULL, *slash;
5390 if ((ct->status != GOT_STATUS_ADD &&
5391 ct->staged_status != GOT_STATUS_ADD) ||
5392 (ct->flags & GOT_COMMITABLE_ADDED))
5395 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5396 strlen(path_base_tree)))
5399 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5404 slash = strchr(child_path, '/');
5405 if (slash == NULL) {
5406 err = alloc_added_blob_tree_entry(&new_te, ct);
5409 err = report_ct_status(ct, status_cb, status_arg);
5412 ct->flags |= GOT_COMMITABLE_ADDED;
5413 err = insert_tree_entry(new_te, &paths);
5418 *slash = '\0'; /* trim trailing path components */
5419 if (base_tree == NULL ||
5420 got_object_tree_find_entry(base_tree, child_path)
5422 err = make_subtree_for_added_blob(&new_te,
5423 child_path, path_base_tree,
5424 commitable_paths, status_cb, status_arg,
5428 err = insert_tree_entry(new_te, &paths);
5437 int i, nbase_entries;
5438 /* Handle modified and deleted entries. */
5439 nbase_entries = got_object_tree_get_nentries(base_tree);
5440 for (i = 0; i < nbase_entries; i++) {
5441 struct got_commitable *ct = NULL;
5443 te = got_object_tree_get_entry(base_tree, i);
5444 if (got_object_tree_entry_is_submodule(te)) {
5445 /* Entry is a submodule; just copy it. */
5446 err = got_object_tree_entry_dup(&new_te, te);
5449 err = insert_tree_entry(new_te, &paths);
5456 if (S_ISDIR(te->mode)) {
5458 err = got_object_tree_entry_dup(&new_te, te);
5461 err = match_modified_subtree(&modified, te,
5462 path_base_tree, commitable_paths);
5465 /* Avoid recursion into unmodified subtrees. */
5467 struct got_object_id *new_id;
5469 err = write_subtree(&new_id,
5471 path_base_tree, commitable_paths,
5472 status_cb, status_arg, repo);
5475 if (nsubentries == 0) {
5476 /* All entries were deleted. */
5480 memcpy(&new_te->id, new_id,
5481 sizeof(new_te->id));
5484 err = insert_tree_entry(new_te, &paths);
5491 err = match_deleted_or_modified_ct(&ct, te,
5492 path_base_tree, commitable_paths);
5496 /* NB: Deleted entries get dropped here. */
5497 if (ct->status == GOT_STATUS_MODIFY ||
5498 ct->status == GOT_STATUS_MODE_CHANGE ||
5499 ct->staged_status == GOT_STATUS_MODIFY) {
5500 err = alloc_modified_blob_tree_entry(
5504 err = insert_tree_entry(new_te, &paths);
5509 err = report_ct_status(ct, status_cb,
5514 /* Entry is unchanged; just copy it. */
5515 err = got_object_tree_entry_dup(&new_te, te);
5518 err = insert_tree_entry(new_te, &paths);
5526 /* Write new list of entries; deleted entries have been dropped. */
5527 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5529 got_pathlist_free(&paths);
5533 static const struct got_error *
5534 update_fileindex_after_commit(struct got_worktree *worktree,
5535 struct got_pathlist_head *commitable_paths,
5536 struct got_object_id *new_base_commit_id,
5537 struct got_fileindex *fileindex, int have_staged_files)
5539 const struct got_error *err = NULL;
5540 struct got_pathlist_entry *pe;
5541 char *relpath = NULL;
5543 TAILQ_FOREACH(pe, commitable_paths, entry) {
5544 struct got_fileindex_entry *ie;
5545 struct got_commitable *ct = pe->data;
5547 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5549 err = got_path_skip_common_ancestor(&relpath,
5550 worktree->root_path, ct->ondisk_path);
5555 if (ct->status == GOT_STATUS_DELETE ||
5556 ct->staged_status == GOT_STATUS_DELETE) {
5557 got_fileindex_entry_remove(fileindex, ie);
5558 } else if (ct->staged_status == GOT_STATUS_ADD ||
5559 ct->staged_status == GOT_STATUS_MODIFY) {
5560 got_fileindex_entry_stage_set(ie,
5561 GOT_FILEIDX_STAGE_NONE);
5562 got_fileindex_entry_staged_filetype_set(ie, 0);
5564 err = got_fileindex_entry_update(ie,
5565 worktree->root_fd, relpath,
5566 ct->staged_blob_id->sha1,
5567 new_base_commit_id->sha1,
5568 !have_staged_files);
5570 err = got_fileindex_entry_update(ie,
5571 worktree->root_fd, relpath,
5573 new_base_commit_id->sha1,
5574 !have_staged_files);
5576 err = got_fileindex_entry_alloc(&ie, pe->path);
5579 err = got_fileindex_entry_update(ie,
5580 worktree->root_fd, relpath, ct->blob_id->sha1,
5581 new_base_commit_id->sha1, 1);
5583 got_fileindex_entry_free(ie);
5586 err = got_fileindex_entry_add(fileindex, ie);
5588 got_fileindex_entry_free(ie);
5601 static const struct got_error *
5602 check_out_of_date(const char *in_repo_path, unsigned char status,
5603 unsigned char staged_status, struct got_object_id *base_blob_id,
5604 struct got_object_id *base_commit_id,
5605 struct got_object_id *head_commit_id, struct got_repository *repo,
5608 const struct got_error *err = NULL;
5609 struct got_object_id *id = NULL;
5611 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5612 /* Trivial case: base commit == head commit */
5613 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5616 * Ensure file content which local changes were based
5617 * on matches file content in the branch head.
5619 err = got_object_id_by_path(&id, repo, head_commit_id,
5622 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5623 err = got_error(ood_errcode);
5625 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5626 err = got_error(ood_errcode);
5628 /* Require that added files don't exist in the branch head. */
5629 err = got_object_id_by_path(&id, repo, head_commit_id,
5631 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5633 err = id ? got_error(ood_errcode) : NULL;
5640 const struct got_error *
5641 commit_worktree(struct got_object_id **new_commit_id,
5642 struct got_pathlist_head *commitable_paths,
5643 struct got_object_id *head_commit_id,
5644 struct got_object_id *parent_id2,
5645 struct got_worktree *worktree,
5646 const char *author, const char *committer,
5647 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5648 got_worktree_status_cb status_cb, void *status_arg,
5649 struct got_repository *repo)
5651 const struct got_error *err = NULL, *unlockerr = NULL;
5652 struct got_pathlist_entry *pe;
5653 const char *head_ref_name = NULL;
5654 struct got_commit_object *head_commit = NULL;
5655 struct got_reference *head_ref2 = NULL;
5656 struct got_object_id *head_commit_id2 = NULL;
5657 struct got_tree_object *head_tree = NULL;
5658 struct got_object_id *new_tree_id = NULL;
5659 int nentries, nparents = 0;
5660 struct got_object_id_queue parent_ids;
5661 struct got_object_qid *pid = NULL;
5662 char *logmsg = NULL;
5664 *new_commit_id = NULL;
5666 STAILQ_INIT(&parent_ids);
5668 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5672 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5676 if (commit_msg_cb != NULL) {
5677 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5682 if (logmsg == NULL || strlen(logmsg) == 0) {
5683 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5687 /* Create blobs from added and modified files and record their IDs. */
5688 TAILQ_FOREACH(pe, commitable_paths, entry) {
5689 struct got_commitable *ct = pe->data;
5692 /* Blobs for staged files already exist. */
5693 if (ct->staged_status == GOT_STATUS_ADD ||
5694 ct->staged_status == GOT_STATUS_MODIFY)
5697 if (ct->status != GOT_STATUS_ADD &&
5698 ct->status != GOT_STATUS_MODIFY &&
5699 ct->status != GOT_STATUS_MODE_CHANGE)
5702 if (asprintf(&ondisk_path, "%s/%s",
5703 worktree->root_path, pe->path) == -1) {
5704 err = got_error_from_errno("asprintf");
5707 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5713 /* Recursively write new tree objects. */
5714 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5715 commitable_paths, status_cb, status_arg, repo);
5719 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5722 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5725 err = got_object_qid_alloc(&pid, parent_id2);
5728 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5731 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5732 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5738 /* Check if a concurrent commit to our branch has occurred. */
5739 head_ref_name = got_worktree_get_head_ref_name(worktree);
5740 if (head_ref_name == NULL) {
5741 err = got_error_from_errno("got_worktree_get_head_ref_name");
5744 /* Lock the reference here to prevent concurrent modification. */
5745 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5748 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5751 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5752 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5755 /* Update branch head in repository. */
5756 err = got_ref_change_ref(head_ref2, *new_commit_id);
5759 err = got_ref_write(head_ref2, repo);
5763 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5767 err = ref_base_commit(worktree, repo);
5771 got_object_id_queue_free(&parent_ids);
5773 got_object_tree_close(head_tree);
5775 got_object_commit_close(head_commit);
5776 free(head_commit_id2);
5778 unlockerr = got_ref_unlock(head_ref2);
5779 if (unlockerr && err == NULL)
5781 got_ref_close(head_ref2);
5786 static const struct got_error *
5787 check_path_is_commitable(const char *path,
5788 struct got_pathlist_head *commitable_paths)
5790 struct got_pathlist_entry *cpe = NULL;
5791 size_t path_len = strlen(path);
5793 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5794 struct got_commitable *ct = cpe->data;
5795 const char *ct_path = ct->path;
5797 while (ct_path[0] == '/')
5800 if (strcmp(path, ct_path) == 0 ||
5801 got_path_is_child(ct_path, path, path_len))
5806 return got_error_path(path, GOT_ERR_BAD_PATH);
5811 static const struct got_error *
5812 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5814 int *have_staged_files = arg;
5816 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5817 *have_staged_files = 1;
5818 return got_error(GOT_ERR_CANCELLED);
5824 static const struct got_error *
5825 check_non_staged_files(struct got_fileindex *fileindex,
5826 struct got_pathlist_head *paths)
5828 struct got_pathlist_entry *pe;
5829 struct got_fileindex_entry *ie;
5831 TAILQ_FOREACH(pe, paths, entry) {
5832 if (pe->path[0] == '\0')
5834 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5836 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5837 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5838 return got_error_path(pe->path,
5839 GOT_ERR_FILE_NOT_STAGED);
5845 const struct got_error *
5846 got_worktree_commit(struct got_object_id **new_commit_id,
5847 struct got_worktree *worktree, struct got_pathlist_head *paths,
5848 const char *author, const char *committer, int allow_bad_symlinks,
5849 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5850 got_worktree_status_cb status_cb, void *status_arg,
5851 struct got_repository *repo)
5853 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5854 struct got_fileindex *fileindex = NULL;
5855 char *fileindex_path = NULL;
5856 struct got_pathlist_head commitable_paths;
5857 struct collect_commitables_arg cc_arg;
5858 struct got_pathlist_entry *pe;
5859 struct got_reference *head_ref = NULL;
5860 struct got_object_id *head_commit_id = NULL;
5861 int have_staged_files = 0;
5863 *new_commit_id = NULL;
5865 TAILQ_INIT(&commitable_paths);
5867 err = lock_worktree(worktree, LOCK_EX);
5871 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5875 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5879 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5883 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5884 &have_staged_files);
5885 if (err && err->code != GOT_ERR_CANCELLED)
5887 if (have_staged_files) {
5888 err = check_non_staged_files(fileindex, paths);
5893 cc_arg.commitable_paths = &commitable_paths;
5894 cc_arg.worktree = worktree;
5895 cc_arg.fileindex = fileindex;
5897 cc_arg.have_staged_files = have_staged_files;
5898 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5899 TAILQ_FOREACH(pe, paths, entry) {
5900 err = worktree_status(worktree, pe->path, fileindex, repo,
5901 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5906 if (TAILQ_EMPTY(&commitable_paths)) {
5907 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5911 TAILQ_FOREACH(pe, paths, entry) {
5912 err = check_path_is_commitable(pe->path, &commitable_paths);
5917 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5918 struct got_commitable *ct = pe->data;
5919 const char *ct_path = ct->in_repo_path;
5921 while (ct_path[0] == '/')
5923 err = check_out_of_date(ct_path, ct->status,
5924 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5925 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5931 err = commit_worktree(new_commit_id, &commitable_paths,
5932 head_commit_id, NULL, worktree, author, committer,
5933 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5937 err = update_fileindex_after_commit(worktree, &commitable_paths,
5938 *new_commit_id, fileindex, have_staged_files);
5939 sync_err = sync_fileindex(fileindex, fileindex_path);
5940 if (sync_err && err == NULL)
5944 got_fileindex_free(fileindex);
5945 free(fileindex_path);
5946 unlockerr = lock_worktree(worktree, LOCK_SH);
5947 if (unlockerr && err == NULL)
5949 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5950 struct got_commitable *ct = pe->data;
5951 free_commitable(ct);
5953 got_pathlist_free(&commitable_paths);
5958 got_commitable_get_path(struct got_commitable *ct)
5964 got_commitable_get_status(struct got_commitable *ct)
5969 struct check_rebase_ok_arg {
5970 struct got_worktree *worktree;
5971 struct got_repository *repo;
5974 static const struct got_error *
5975 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5977 const struct got_error *err = NULL;
5978 struct check_rebase_ok_arg *a = arg;
5979 unsigned char status;
5983 /* Reject rebase of a work tree with mixed base commits. */
5984 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5985 SHA1_DIGEST_LENGTH))
5986 return got_error(GOT_ERR_MIXED_COMMITS);
5988 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5990 return got_error_from_errno("asprintf");
5992 /* Reject rebase of a work tree with modified or staged files. */
5993 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5998 if (status != GOT_STATUS_NO_CHANGE)
5999 return got_error(GOT_ERR_MODIFIED);
6000 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6001 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6006 const struct got_error *
6007 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6008 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6009 struct got_worktree *worktree, struct got_reference *branch,
6010 struct got_repository *repo)
6012 const struct got_error *err = NULL;
6013 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6014 char *branch_ref_name = NULL;
6015 char *fileindex_path = NULL;
6016 struct check_rebase_ok_arg ok_arg;
6017 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6018 struct got_object_id *wt_branch_tip = NULL;
6020 *new_base_branch_ref = NULL;
6024 err = lock_worktree(worktree, LOCK_EX);
6028 err = open_fileindex(fileindex, &fileindex_path, worktree);
6032 ok_arg.worktree = worktree;
6034 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6039 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6043 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6047 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6051 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6056 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6059 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6060 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6064 err = got_ref_alloc_symref(new_base_branch_ref,
6065 new_base_branch_ref_name, wt_branch);
6068 err = got_ref_write(*new_base_branch_ref, repo);
6072 /* TODO Lock original branch's ref while rebasing? */
6074 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6078 err = got_ref_write(branch_ref, repo);
6082 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6083 worktree->base_commit_id);
6086 err = got_ref_write(*tmp_branch, repo);
6090 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6094 free(fileindex_path);
6095 free(tmp_branch_name);
6096 free(new_base_branch_ref_name);
6097 free(branch_ref_name);
6099 got_ref_close(branch_ref);
6101 got_ref_close(wt_branch);
6102 free(wt_branch_tip);
6104 if (*new_base_branch_ref) {
6105 got_ref_close(*new_base_branch_ref);
6106 *new_base_branch_ref = NULL;
6109 got_ref_close(*tmp_branch);
6113 got_fileindex_free(*fileindex);
6116 lock_worktree(worktree, LOCK_SH);
6121 const struct got_error *
6122 got_worktree_rebase_continue(struct got_object_id **commit_id,
6123 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6124 struct got_reference **branch, struct got_fileindex **fileindex,
6125 struct got_worktree *worktree, struct got_repository *repo)
6127 const struct got_error *err;
6128 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6129 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6130 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6131 char *fileindex_path = NULL;
6132 int have_staged_files = 0;
6135 *new_base_branch = NULL;
6140 err = lock_worktree(worktree, LOCK_EX);
6144 err = open_fileindex(fileindex, &fileindex_path, worktree);
6148 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6149 &have_staged_files);
6150 if (err && err->code != GOT_ERR_CANCELLED)
6152 if (have_staged_files) {
6153 err = got_error(GOT_ERR_STAGED_PATHS);
6157 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6161 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6165 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6169 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6173 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6177 err = got_ref_open(branch, repo,
6178 got_ref_get_symref_target(branch_ref), 0);
6182 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6186 err = got_ref_resolve(commit_id, repo, commit_ref);
6190 err = got_ref_open(new_base_branch, repo,
6191 new_base_branch_ref_name, 0);
6195 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6199 free(commit_ref_name);
6200 free(branch_ref_name);
6201 free(fileindex_path);
6203 got_ref_close(commit_ref);
6205 got_ref_close(branch_ref);
6210 got_ref_close(*tmp_branch);
6213 if (*new_base_branch) {
6214 got_ref_close(*new_base_branch);
6215 *new_base_branch = NULL;
6218 got_ref_close(*branch);
6222 got_fileindex_free(*fileindex);
6225 lock_worktree(worktree, LOCK_SH);
6230 const struct got_error *
6231 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6233 const struct got_error *err;
6234 char *tmp_branch_name = NULL;
6236 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6240 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6241 free(tmp_branch_name);
6245 static const struct got_error *
6246 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6247 char **logmsg, void *arg)
6253 static const struct got_error *
6254 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6255 const char *path, struct got_object_id *blob_id,
6256 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6257 int dirfd, const char *de_name)
6262 struct collect_merged_paths_arg {
6263 got_worktree_checkout_cb progress_cb;
6265 struct got_pathlist_head *merged_paths;
6268 static const struct got_error *
6269 collect_merged_paths(void *arg, unsigned char status, const char *path)
6271 const struct got_error *err;
6272 struct collect_merged_paths_arg *a = arg;
6274 struct got_pathlist_entry *new;
6276 err = (*a->progress_cb)(a->progress_arg, status, path);
6280 if (status != GOT_STATUS_MERGE &&
6281 status != GOT_STATUS_ADD &&
6282 status != GOT_STATUS_DELETE &&
6283 status != GOT_STATUS_CONFLICT)
6288 return got_error_from_errno("strdup");
6290 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6291 if (err || new == NULL)
6297 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6299 struct got_pathlist_entry *pe;
6301 TAILQ_FOREACH(pe, merged_paths, entry)
6302 free((char *)pe->path);
6304 got_pathlist_free(merged_paths);
6307 static const struct got_error *
6308 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6309 int is_rebase, struct got_repository *repo)
6311 const struct got_error *err;
6312 struct got_reference *commit_ref = NULL;
6314 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6316 if (err->code != GOT_ERR_NOT_REF)
6318 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6321 err = got_ref_write(commit_ref, repo);
6324 } else if (is_rebase) {
6325 struct got_object_id *stored_id;
6328 err = got_ref_resolve(&stored_id, repo, commit_ref);
6331 cmp = got_object_id_cmp(commit_id, stored_id);
6334 err = got_error(GOT_ERR_REBASE_COMMITID);
6340 got_ref_close(commit_ref);
6344 static const struct got_error *
6345 rebase_merge_files(struct got_pathlist_head *merged_paths,
6346 const char *commit_ref_name, struct got_worktree *worktree,
6347 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6348 struct got_object_id *commit_id, struct got_repository *repo,
6349 got_worktree_checkout_cb progress_cb, void *progress_arg,
6350 got_cancel_cb cancel_cb, void *cancel_arg)
6352 const struct got_error *err;
6353 struct got_reference *commit_ref = NULL;
6354 struct collect_merged_paths_arg cmp_arg;
6355 char *fileindex_path;
6357 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6359 err = get_fileindex_path(&fileindex_path, worktree);
6363 cmp_arg.progress_cb = progress_cb;
6364 cmp_arg.progress_arg = progress_arg;
6365 cmp_arg.merged_paths = merged_paths;
6366 err = merge_files(worktree, fileindex, fileindex_path,
6367 parent_commit_id, commit_id, repo, collect_merged_paths,
6368 &cmp_arg, cancel_cb, cancel_arg);
6370 got_ref_close(commit_ref);
6374 const struct got_error *
6375 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6376 struct got_worktree *worktree, struct got_fileindex *fileindex,
6377 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6378 struct got_repository *repo,
6379 got_worktree_checkout_cb progress_cb, void *progress_arg,
6380 got_cancel_cb cancel_cb, void *cancel_arg)
6382 const struct got_error *err;
6383 char *commit_ref_name;
6385 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6389 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6393 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6394 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6395 progress_arg, cancel_cb, cancel_arg);
6397 free(commit_ref_name);
6401 const struct got_error *
6402 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6403 struct got_worktree *worktree, struct got_fileindex *fileindex,
6404 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6405 struct got_repository *repo,
6406 got_worktree_checkout_cb progress_cb, void *progress_arg,
6407 got_cancel_cb cancel_cb, void *cancel_arg)
6409 const struct got_error *err;
6410 char *commit_ref_name;
6412 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6416 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6420 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6421 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6422 progress_arg, cancel_cb, cancel_arg);
6424 free(commit_ref_name);
6428 static const struct got_error *
6429 rebase_commit(struct got_object_id **new_commit_id,
6430 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6431 struct got_worktree *worktree, struct got_fileindex *fileindex,
6432 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6433 const char *new_logmsg, struct got_repository *repo)
6435 const struct got_error *err, *sync_err;
6436 struct got_pathlist_head commitable_paths;
6437 struct collect_commitables_arg cc_arg;
6438 char *fileindex_path = NULL;
6439 struct got_reference *head_ref = NULL;
6440 struct got_object_id *head_commit_id = NULL;
6441 char *logmsg = NULL;
6443 TAILQ_INIT(&commitable_paths);
6444 *new_commit_id = NULL;
6446 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6448 err = get_fileindex_path(&fileindex_path, worktree);
6452 cc_arg.commitable_paths = &commitable_paths;
6453 cc_arg.worktree = worktree;
6455 cc_arg.have_staged_files = 0;
6457 * If possible get the status of individual files directly to
6458 * avoid crawling the entire work tree once per rebased commit.
6460 * Ideally, merged_paths would contain a list of commitables
6461 * we could use so we could skip worktree_status() entirely.
6462 * However, we would then need carefully keep track of cumulative
6463 * effects of operations such as file additions and deletions
6464 * in 'got histedit -f' (folding multiple commits into one),
6465 * and this extra complexity is not really worth it.
6468 struct got_pathlist_entry *pe;
6469 TAILQ_FOREACH(pe, merged_paths, entry) {
6470 err = worktree_status(worktree, pe->path, fileindex,
6471 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6477 err = worktree_status(worktree, "", fileindex, repo,
6478 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6483 if (TAILQ_EMPTY(&commitable_paths)) {
6484 /* No-op change; commit will be elided. */
6485 err = got_ref_delete(commit_ref, repo);
6488 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6492 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6496 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6501 logmsg = strdup(new_logmsg);
6502 if (logmsg == NULL) {
6503 err = got_error_from_errno("strdup");
6507 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6512 /* NB: commit_worktree will call free(logmsg) */
6513 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6514 NULL, worktree, got_object_commit_get_author(orig_commit),
6515 got_object_commit_get_committer(orig_commit),
6516 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6520 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6524 err = got_ref_delete(commit_ref, repo);
6528 err = update_fileindex_after_commit(worktree, &commitable_paths,
6529 *new_commit_id, fileindex, 0);
6530 sync_err = sync_fileindex(fileindex, fileindex_path);
6531 if (sync_err && err == NULL)
6534 free(fileindex_path);
6535 free(head_commit_id);
6537 got_ref_close(head_ref);
6539 free(*new_commit_id);
6540 *new_commit_id = NULL;
6545 const struct got_error *
6546 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6547 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6548 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6549 struct got_commit_object *orig_commit,
6550 struct got_object_id *orig_commit_id, struct got_repository *repo)
6552 const struct got_error *err;
6553 char *commit_ref_name;
6554 struct got_reference *commit_ref = NULL;
6555 struct got_object_id *commit_id = NULL;
6557 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6561 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6564 err = got_ref_resolve(&commit_id, repo, commit_ref);
6567 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6568 err = got_error(GOT_ERR_REBASE_COMMITID);
6572 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6573 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6576 got_ref_close(commit_ref);
6577 free(commit_ref_name);
6582 const struct got_error *
6583 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6584 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6585 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6586 struct got_commit_object *orig_commit,
6587 struct got_object_id *orig_commit_id, const char *new_logmsg,
6588 struct got_repository *repo)
6590 const struct got_error *err;
6591 char *commit_ref_name;
6592 struct got_reference *commit_ref = NULL;
6594 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6598 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6602 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6603 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6606 got_ref_close(commit_ref);
6607 free(commit_ref_name);
6611 const struct got_error *
6612 got_worktree_rebase_postpone(struct got_worktree *worktree,
6613 struct got_fileindex *fileindex)
6616 got_fileindex_free(fileindex);
6617 return lock_worktree(worktree, LOCK_SH);
6620 static const struct got_error *
6621 delete_ref(const char *name, struct got_repository *repo)
6623 const struct got_error *err;
6624 struct got_reference *ref;
6626 err = got_ref_open(&ref, repo, name, 0);
6628 if (err->code == GOT_ERR_NOT_REF)
6633 err = got_ref_delete(ref, repo);
6638 static const struct got_error *
6639 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6641 const struct got_error *err;
6642 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6643 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6645 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6648 err = delete_ref(tmp_branch_name, repo);
6652 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6655 err = delete_ref(new_base_branch_ref_name, repo);
6659 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6662 err = delete_ref(branch_ref_name, repo);
6666 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6669 err = delete_ref(commit_ref_name, repo);
6674 free(tmp_branch_name);
6675 free(new_base_branch_ref_name);
6676 free(branch_ref_name);
6677 free(commit_ref_name);
6681 const struct got_error *
6682 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6683 struct got_object_id *new_commit_id, struct got_repository *repo)
6685 const struct got_error *err;
6686 struct got_reference *ref = NULL;
6687 struct got_object_id *old_commit_id = NULL;
6688 const char *branch_name = NULL;
6689 char *new_id_str = NULL;
6690 char *refname = NULL;
6692 branch_name = got_ref_get_name(branch);
6693 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6694 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6697 err = got_object_id_str(&new_id_str, new_commit_id);
6701 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6702 new_id_str) == -1) {
6703 err = got_error_from_errno("asprintf");
6707 err = got_ref_resolve(&old_commit_id, repo, branch);
6711 err = got_ref_alloc(&ref, refname, old_commit_id);
6715 err = got_ref_write(ref, repo);
6719 free(old_commit_id);
6725 const struct got_error *
6726 got_worktree_rebase_complete(struct got_worktree *worktree,
6727 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6728 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6729 struct got_repository *repo, int create_backup)
6731 const struct got_error *err, *unlockerr, *sync_err;
6732 struct got_object_id *new_head_commit_id = NULL;
6733 char *fileindex_path = NULL;
6735 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6739 if (create_backup) {
6740 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6741 rebased_branch, new_head_commit_id, repo);
6746 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6750 err = got_ref_write(rebased_branch, repo);
6754 err = got_worktree_set_head_ref(worktree, rebased_branch);
6758 err = delete_rebase_refs(worktree, repo);
6762 err = get_fileindex_path(&fileindex_path, worktree);
6765 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6766 sync_err = sync_fileindex(fileindex, fileindex_path);
6767 if (sync_err && err == NULL)
6770 got_fileindex_free(fileindex);
6771 free(fileindex_path);
6772 free(new_head_commit_id);
6773 unlockerr = lock_worktree(worktree, LOCK_SH);
6774 if (unlockerr && err == NULL)
6779 const struct got_error *
6780 got_worktree_rebase_abort(struct got_worktree *worktree,
6781 struct got_fileindex *fileindex, struct got_repository *repo,
6782 struct got_reference *new_base_branch,
6783 got_worktree_checkout_cb progress_cb, void *progress_arg)
6785 const struct got_error *err, *unlockerr, *sync_err;
6786 struct got_reference *resolved = NULL;
6787 struct got_object_id *commit_id = NULL;
6788 char *fileindex_path = NULL;
6789 struct revert_file_args rfa;
6790 struct got_object_id *tree_id = NULL;
6792 err = lock_worktree(worktree, LOCK_EX);
6796 err = got_ref_open(&resolved, repo,
6797 got_ref_get_symref_target(new_base_branch), 0);
6801 err = got_worktree_set_head_ref(worktree, resolved);
6806 * XXX commits to the base branch could have happened while
6807 * we were busy rebasing; should we store the original commit ID
6808 * when rebase begins and read it back here?
6810 err = got_ref_resolve(&commit_id, repo, resolved);
6814 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6818 err = got_object_id_by_path(&tree_id, repo,
6819 worktree->base_commit_id, worktree->path_prefix);
6823 err = delete_rebase_refs(worktree, repo);
6827 err = get_fileindex_path(&fileindex_path, worktree);
6831 rfa.worktree = worktree;
6832 rfa.fileindex = fileindex;
6833 rfa.progress_cb = progress_cb;
6834 rfa.progress_arg = progress_arg;
6835 rfa.patch_cb = NULL;
6836 rfa.patch_arg = NULL;
6838 rfa.unlink_added_files = 0;
6839 err = worktree_status(worktree, "", fileindex, repo,
6840 revert_file, &rfa, NULL, NULL, 1, 0);
6844 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6845 repo, progress_cb, progress_arg, NULL, NULL);
6847 sync_err = sync_fileindex(fileindex, fileindex_path);
6848 if (sync_err && err == NULL)
6851 got_ref_close(resolved);
6855 got_fileindex_free(fileindex);
6856 free(fileindex_path);
6858 unlockerr = lock_worktree(worktree, LOCK_SH);
6859 if (unlockerr && err == NULL)
6864 const struct got_error *
6865 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6866 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6867 struct got_fileindex **fileindex, struct got_worktree *worktree,
6868 struct got_repository *repo)
6870 const struct got_error *err = NULL;
6871 char *tmp_branch_name = NULL;
6872 char *branch_ref_name = NULL;
6873 char *base_commit_ref_name = NULL;
6874 char *fileindex_path = NULL;
6875 struct check_rebase_ok_arg ok_arg;
6876 struct got_reference *wt_branch = NULL;
6877 struct got_reference *base_commit_ref = NULL;
6881 *base_commit_id = NULL;
6884 err = lock_worktree(worktree, LOCK_EX);
6888 err = open_fileindex(fileindex, &fileindex_path, worktree);
6892 ok_arg.worktree = worktree;
6894 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6899 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6903 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6907 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6912 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6917 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6921 err = got_ref_write(*branch_ref, repo);
6925 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6926 worktree->base_commit_id);
6929 err = got_ref_write(base_commit_ref, repo);
6932 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6933 if (*base_commit_id == NULL) {
6934 err = got_error_from_errno("got_object_id_dup");
6938 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6939 worktree->base_commit_id);
6942 err = got_ref_write(*tmp_branch, repo);
6946 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6950 free(fileindex_path);
6951 free(tmp_branch_name);
6952 free(branch_ref_name);
6953 free(base_commit_ref_name);
6955 got_ref_close(wt_branch);
6958 got_ref_close(*branch_ref);
6962 got_ref_close(*tmp_branch);
6965 free(*base_commit_id);
6967 got_fileindex_free(*fileindex);
6970 lock_worktree(worktree, LOCK_SH);
6975 const struct got_error *
6976 got_worktree_histedit_postpone(struct got_worktree *worktree,
6977 struct got_fileindex *fileindex)
6980 got_fileindex_free(fileindex);
6981 return lock_worktree(worktree, LOCK_SH);
6984 const struct got_error *
6985 got_worktree_histedit_in_progress(int *in_progress,
6986 struct got_worktree *worktree)
6988 const struct got_error *err;
6989 char *tmp_branch_name = NULL;
6991 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6995 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6996 free(tmp_branch_name);
7000 const struct got_error *
7001 got_worktree_histedit_continue(struct got_object_id **commit_id,
7002 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7003 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7004 struct got_worktree *worktree, struct got_repository *repo)
7006 const struct got_error *err;
7007 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7008 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7009 struct got_reference *commit_ref = NULL;
7010 struct got_reference *base_commit_ref = NULL;
7011 char *fileindex_path = NULL;
7012 int have_staged_files = 0;
7016 *base_commit_id = NULL;
7019 err = lock_worktree(worktree, LOCK_EX);
7023 err = open_fileindex(fileindex, &fileindex_path, worktree);
7027 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7028 &have_staged_files);
7029 if (err && err->code != GOT_ERR_CANCELLED)
7031 if (have_staged_files) {
7032 err = got_error(GOT_ERR_STAGED_PATHS);
7036 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7040 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7044 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7048 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7053 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7057 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7060 err = got_ref_resolve(commit_id, repo, commit_ref);
7064 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7067 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7071 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7075 free(commit_ref_name);
7076 free(branch_ref_name);
7077 free(fileindex_path);
7079 got_ref_close(commit_ref);
7080 if (base_commit_ref)
7081 got_ref_close(base_commit_ref);
7085 free(*base_commit_id);
7086 *base_commit_id = NULL;
7088 got_ref_close(*tmp_branch);
7092 got_fileindex_free(*fileindex);
7095 lock_worktree(worktree, LOCK_EX);
7100 static const struct got_error *
7101 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7103 const struct got_error *err;
7104 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7105 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7107 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7110 err = delete_ref(tmp_branch_name, repo);
7114 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7118 err = delete_ref(base_commit_ref_name, repo);
7122 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7125 err = delete_ref(branch_ref_name, repo);
7129 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7132 err = delete_ref(commit_ref_name, repo);
7136 free(tmp_branch_name);
7137 free(base_commit_ref_name);
7138 free(branch_ref_name);
7139 free(commit_ref_name);
7143 const struct got_error *
7144 got_worktree_histedit_abort(struct got_worktree *worktree,
7145 struct got_fileindex *fileindex, struct got_repository *repo,
7146 struct got_reference *branch, struct got_object_id *base_commit_id,
7147 got_worktree_checkout_cb progress_cb, void *progress_arg)
7149 const struct got_error *err, *unlockerr, *sync_err;
7150 struct got_reference *resolved = NULL;
7151 char *fileindex_path = NULL;
7152 struct got_object_id *tree_id = NULL;
7153 struct revert_file_args rfa;
7155 err = lock_worktree(worktree, LOCK_EX);
7159 err = got_ref_open(&resolved, repo,
7160 got_ref_get_symref_target(branch), 0);
7164 err = got_worktree_set_head_ref(worktree, resolved);
7168 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7172 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7173 worktree->path_prefix);
7177 err = delete_histedit_refs(worktree, repo);
7181 err = get_fileindex_path(&fileindex_path, worktree);
7185 rfa.worktree = worktree;
7186 rfa.fileindex = fileindex;
7187 rfa.progress_cb = progress_cb;
7188 rfa.progress_arg = progress_arg;
7189 rfa.patch_cb = NULL;
7190 rfa.patch_arg = NULL;
7192 rfa.unlink_added_files = 0;
7193 err = worktree_status(worktree, "", fileindex, repo,
7194 revert_file, &rfa, NULL, NULL, 1, 0);
7198 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7199 repo, progress_cb, progress_arg, NULL, NULL);
7201 sync_err = sync_fileindex(fileindex, fileindex_path);
7202 if (sync_err && err == NULL)
7205 got_ref_close(resolved);
7207 free(fileindex_path);
7209 unlockerr = lock_worktree(worktree, LOCK_SH);
7210 if (unlockerr && err == NULL)
7215 const struct got_error *
7216 got_worktree_histedit_complete(struct got_worktree *worktree,
7217 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7218 struct got_reference *edited_branch, struct got_repository *repo)
7220 const struct got_error *err, *unlockerr, *sync_err;
7221 struct got_object_id *new_head_commit_id = NULL;
7222 struct got_reference *resolved = NULL;
7223 char *fileindex_path = NULL;
7225 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7229 err = got_ref_open(&resolved, repo,
7230 got_ref_get_symref_target(edited_branch), 0);
7234 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7235 resolved, new_head_commit_id, repo);
7239 err = got_ref_change_ref(resolved, new_head_commit_id);
7243 err = got_ref_write(resolved, repo);
7247 err = got_worktree_set_head_ref(worktree, resolved);
7251 err = delete_histedit_refs(worktree, repo);
7255 err = get_fileindex_path(&fileindex_path, worktree);
7258 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7259 sync_err = sync_fileindex(fileindex, fileindex_path);
7260 if (sync_err && err == NULL)
7263 got_fileindex_free(fileindex);
7264 free(fileindex_path);
7265 free(new_head_commit_id);
7266 unlockerr = lock_worktree(worktree, LOCK_SH);
7267 if (unlockerr && err == NULL)
7272 const struct got_error *
7273 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7274 struct got_object_id *commit_id, struct got_repository *repo)
7276 const struct got_error *err;
7277 char *commit_ref_name;
7279 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7283 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7287 err = delete_ref(commit_ref_name, repo);
7289 free(commit_ref_name);
7293 const struct got_error *
7294 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7295 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7296 struct got_worktree *worktree, const char *refname,
7297 struct got_repository *repo)
7299 const struct got_error *err = NULL;
7300 char *fileindex_path = NULL;
7301 struct check_rebase_ok_arg ok_arg;
7305 *base_branch_ref = NULL;
7307 err = lock_worktree(worktree, LOCK_EX);
7311 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7312 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7313 "cannot integrate a branch into itself; "
7314 "update -b or different branch name required");
7318 err = open_fileindex(fileindex, &fileindex_path, worktree);
7322 /* Preconditions are the same as for rebase. */
7323 ok_arg.worktree = worktree;
7325 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7330 err = got_ref_open(branch_ref, repo, refname, 1);
7334 err = got_ref_open(base_branch_ref, repo,
7335 got_worktree_get_head_ref_name(worktree), 1);
7339 got_ref_close(*branch_ref);
7342 if (*base_branch_ref) {
7343 got_ref_close(*base_branch_ref);
7344 *base_branch_ref = NULL;
7347 got_fileindex_free(*fileindex);
7350 lock_worktree(worktree, LOCK_SH);
7355 const struct got_error *
7356 got_worktree_integrate_continue(struct got_worktree *worktree,
7357 struct got_fileindex *fileindex, struct got_repository *repo,
7358 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7359 got_worktree_checkout_cb progress_cb, void *progress_arg,
7360 got_cancel_cb cancel_cb, void *cancel_arg)
7362 const struct got_error *err = NULL, *sync_err, *unlockerr;
7363 char *fileindex_path = NULL;
7364 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7366 err = get_fileindex_path(&fileindex_path, worktree);
7370 err = got_ref_resolve(&commit_id, repo, branch_ref);
7374 err = got_object_id_by_path(&tree_id, repo, commit_id,
7375 worktree->path_prefix);
7379 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7383 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7384 progress_cb, progress_arg, cancel_cb, cancel_arg);
7388 err = got_ref_change_ref(base_branch_ref, commit_id);
7392 err = got_ref_write(base_branch_ref, repo);
7396 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7398 sync_err = sync_fileindex(fileindex, fileindex_path);
7399 if (sync_err && err == NULL)
7403 unlockerr = got_ref_unlock(branch_ref);
7404 if (unlockerr && err == NULL)
7406 got_ref_close(branch_ref);
7408 unlockerr = got_ref_unlock(base_branch_ref);
7409 if (unlockerr && err == NULL)
7411 got_ref_close(base_branch_ref);
7413 got_fileindex_free(fileindex);
7414 free(fileindex_path);
7417 unlockerr = lock_worktree(worktree, LOCK_SH);
7418 if (unlockerr && err == NULL)
7423 const struct got_error *
7424 got_worktree_integrate_abort(struct got_worktree *worktree,
7425 struct got_fileindex *fileindex, struct got_repository *repo,
7426 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7428 const struct got_error *err = NULL, *unlockerr = NULL;
7430 got_fileindex_free(fileindex);
7432 err = lock_worktree(worktree, LOCK_SH);
7434 unlockerr = got_ref_unlock(branch_ref);
7435 if (unlockerr && err == NULL)
7437 got_ref_close(branch_ref);
7439 unlockerr = got_ref_unlock(base_branch_ref);
7440 if (unlockerr && err == NULL)
7442 got_ref_close(base_branch_ref);
7447 const struct got_error *
7448 got_worktree_merge_postpone(struct got_worktree *worktree,
7449 struct got_fileindex *fileindex)
7451 const struct got_error *err, *sync_err;
7452 char *fileindex_path = NULL;
7454 err = get_fileindex_path(&fileindex_path, worktree);
7458 sync_err = sync_fileindex(fileindex, fileindex_path);
7460 err = lock_worktree(worktree, LOCK_SH);
7461 if (sync_err && err == NULL)
7464 got_fileindex_free(fileindex);
7465 free(fileindex_path);
7469 static const struct got_error *
7470 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7472 const struct got_error *err;
7473 char *branch_refname = NULL, *commit_refname = NULL;
7475 err = get_merge_branch_ref_name(&branch_refname, worktree);
7478 err = delete_ref(branch_refname, repo);
7482 err = get_merge_commit_ref_name(&commit_refname, worktree);
7485 err = delete_ref(commit_refname, repo);
7490 free(branch_refname);
7491 free(commit_refname);
7495 struct merge_commit_msg_arg {
7496 struct got_worktree *worktree;
7497 const char *branch_name;
7500 static const struct got_error *
7501 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7504 struct merge_commit_msg_arg *a = arg;
7506 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7507 got_worktree_get_head_ref_name(a->worktree)) == -1)
7508 return got_error_from_errno("asprintf");
7514 const struct got_error *
7515 got_worktree_merge_branch(struct got_worktree *worktree,
7516 struct got_fileindex *fileindex,
7517 struct got_object_id *yca_commit_id,
7518 struct got_object_id *branch_tip,
7519 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7520 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7522 const struct got_error *err;
7523 char *fileindex_path = NULL;
7525 err = get_fileindex_path(&fileindex_path, worktree);
7529 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7534 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7535 branch_tip, repo, progress_cb, progress_arg,
7536 cancel_cb, cancel_arg);
7538 free(fileindex_path);
7542 const struct got_error *
7543 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7544 struct got_worktree *worktree, struct got_fileindex *fileindex,
7545 const char *author, const char *committer, int allow_bad_symlinks,
7546 struct got_object_id *branch_tip, const char *branch_name,
7547 struct got_repository *repo,
7548 got_worktree_status_cb status_cb, void *status_arg)
7551 const struct got_error *err = NULL, *sync_err;
7552 struct got_pathlist_head commitable_paths;
7553 struct collect_commitables_arg cc_arg;
7554 struct got_pathlist_entry *pe;
7555 struct got_reference *head_ref = NULL;
7556 struct got_object_id *head_commit_id = NULL;
7557 int have_staged_files = 0;
7558 struct merge_commit_msg_arg mcm_arg;
7559 char *fileindex_path = NULL;
7561 *new_commit_id = NULL;
7563 TAILQ_INIT(&commitable_paths);
7565 err = get_fileindex_path(&fileindex_path, worktree);
7569 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7573 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7577 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7578 &have_staged_files);
7579 if (err && err->code != GOT_ERR_CANCELLED)
7581 if (have_staged_files) {
7582 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7586 cc_arg.commitable_paths = &commitable_paths;
7587 cc_arg.worktree = worktree;
7588 cc_arg.fileindex = fileindex;
7590 cc_arg.have_staged_files = have_staged_files;
7591 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7592 err = worktree_status(worktree, "", fileindex, repo,
7593 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7597 if (TAILQ_EMPTY(&commitable_paths)) {
7598 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7599 "merge of %s cannot proceed", branch_name);
7603 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7604 struct got_commitable *ct = pe->data;
7605 const char *ct_path = ct->in_repo_path;
7607 while (ct_path[0] == '/')
7609 err = check_out_of_date(ct_path, ct->status,
7610 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7611 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7617 mcm_arg.worktree = worktree;
7618 mcm_arg.branch_name = branch_name;
7619 err = commit_worktree(new_commit_id, &commitable_paths,
7620 head_commit_id, branch_tip, worktree, author, committer,
7621 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7625 err = update_fileindex_after_commit(worktree, &commitable_paths,
7626 *new_commit_id, fileindex, have_staged_files);
7627 sync_err = sync_fileindex(fileindex, fileindex_path);
7628 if (sync_err && err == NULL)
7631 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7632 struct got_commitable *ct = pe->data;
7633 free_commitable(ct);
7635 got_pathlist_free(&commitable_paths);
7636 free(fileindex_path);
7640 const struct got_error *
7641 got_worktree_merge_complete(struct got_worktree *worktree,
7642 struct got_fileindex *fileindex, struct got_repository *repo)
7644 const struct got_error *err, *unlockerr, *sync_err;
7645 char *fileindex_path = NULL;
7647 err = delete_merge_refs(worktree, repo);
7651 err = get_fileindex_path(&fileindex_path, worktree);
7654 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7655 sync_err = sync_fileindex(fileindex, fileindex_path);
7656 if (sync_err && err == NULL)
7659 got_fileindex_free(fileindex);
7660 free(fileindex_path);
7661 unlockerr = lock_worktree(worktree, LOCK_SH);
7662 if (unlockerr && err == NULL)
7667 const struct got_error *
7668 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7669 struct got_repository *repo)
7671 const struct got_error *err;
7672 char *branch_refname = NULL;
7673 struct got_reference *branch_ref = NULL;
7677 err = get_merge_branch_ref_name(&branch_refname, worktree);
7680 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7681 free(branch_refname);
7683 if (err->code != GOT_ERR_NOT_REF)
7691 const struct got_error *got_worktree_merge_prepare(
7692 struct got_fileindex **fileindex, struct got_worktree *worktree,
7693 struct got_reference *branch, struct got_repository *repo)
7695 const struct got_error *err = NULL;
7696 char *fileindex_path = NULL;
7697 char *branch_refname = NULL, *commit_refname = NULL;
7698 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7699 struct got_reference *commit_ref = NULL;
7700 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7701 struct check_rebase_ok_arg ok_arg;
7705 err = lock_worktree(worktree, LOCK_EX);
7709 err = open_fileindex(fileindex, &fileindex_path, worktree);
7713 /* Preconditions are the same as for rebase. */
7714 ok_arg.worktree = worktree;
7716 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7721 err = get_merge_branch_ref_name(&branch_refname, worktree);
7725 err = get_merge_commit_ref_name(&commit_refname, worktree);
7729 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7734 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7738 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7739 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7743 err = got_ref_resolve(&branch_tip, repo, branch);
7747 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7750 err = got_ref_write(branch_ref, repo);
7754 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7757 err = got_ref_write(commit_ref, repo);
7762 free(branch_refname);
7763 free(commit_refname);
7764 free(fileindex_path);
7766 got_ref_close(branch_ref);
7768 got_ref_close(commit_ref);
7770 got_ref_close(wt_branch);
7771 free(wt_branch_tip);
7774 got_fileindex_free(*fileindex);
7777 lock_worktree(worktree, LOCK_SH);
7782 const struct got_error *
7783 got_worktree_merge_continue(char **branch_name,
7784 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7785 struct got_worktree *worktree, struct got_repository *repo)
7787 const struct got_error *err;
7788 char *commit_refname = NULL, *branch_refname = NULL;
7789 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7790 char *fileindex_path = NULL;
7791 int have_staged_files = 0;
7793 *branch_name = NULL;
7797 err = lock_worktree(worktree, LOCK_EX);
7801 err = open_fileindex(fileindex, &fileindex_path, worktree);
7805 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7806 &have_staged_files);
7807 if (err && err->code != GOT_ERR_CANCELLED)
7809 if (have_staged_files) {
7810 err = got_error(GOT_ERR_STAGED_PATHS);
7814 err = get_merge_branch_ref_name(&branch_refname, worktree);
7818 err = get_merge_commit_ref_name(&commit_refname, worktree);
7822 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7826 if (!got_ref_is_symbolic(branch_ref)) {
7827 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7828 "%s is not a symbolic reference",
7829 got_ref_get_name(branch_ref));
7832 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7833 if (*branch_name == NULL) {
7834 err = got_error_from_errno("strdup");
7838 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7842 err = got_ref_resolve(branch_tip, repo, commit_ref);
7846 free(commit_refname);
7847 free(branch_refname);
7848 free(fileindex_path);
7850 got_ref_close(commit_ref);
7852 got_ref_close(branch_ref);
7856 *branch_name = NULL;
7861 got_fileindex_free(*fileindex);
7864 lock_worktree(worktree, LOCK_SH);
7869 const struct got_error *
7870 got_worktree_merge_abort(struct got_worktree *worktree,
7871 struct got_fileindex *fileindex, struct got_repository *repo,
7872 got_worktree_checkout_cb progress_cb, void *progress_arg)
7874 const struct got_error *err, *unlockerr, *sync_err;
7875 struct got_object_id *commit_id = NULL;
7876 char *fileindex_path = NULL;
7877 struct revert_file_args rfa;
7878 struct got_object_id *tree_id = NULL;
7880 err = got_object_id_by_path(&tree_id, repo,
7881 worktree->base_commit_id, worktree->path_prefix);
7885 err = delete_merge_refs(worktree, repo);
7889 err = get_fileindex_path(&fileindex_path, worktree);
7893 rfa.worktree = worktree;
7894 rfa.fileindex = fileindex;
7895 rfa.progress_cb = progress_cb;
7896 rfa.progress_arg = progress_arg;
7897 rfa.patch_cb = NULL;
7898 rfa.patch_arg = NULL;
7900 rfa.unlink_added_files = 1;
7901 err = worktree_status(worktree, "", fileindex, repo,
7902 revert_file, &rfa, NULL, NULL, 1, 0);
7906 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7907 repo, progress_cb, progress_arg, NULL, NULL);
7909 sync_err = sync_fileindex(fileindex, fileindex_path);
7910 if (sync_err && err == NULL)
7916 got_fileindex_free(fileindex);
7917 free(fileindex_path);
7919 unlockerr = lock_worktree(worktree, LOCK_SH);
7920 if (unlockerr && err == NULL)
7925 struct check_stage_ok_arg {
7926 struct got_object_id *head_commit_id;
7927 struct got_worktree *worktree;
7928 struct got_fileindex *fileindex;
7929 struct got_repository *repo;
7933 const struct got_error *
7934 check_stage_ok(void *arg, unsigned char status,
7935 unsigned char staged_status, const char *relpath,
7936 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7937 struct got_object_id *commit_id, int dirfd, const char *de_name)
7939 struct check_stage_ok_arg *a = arg;
7940 const struct got_error *err = NULL;
7941 struct got_fileindex_entry *ie;
7942 struct got_object_id base_commit_id;
7943 struct got_object_id *base_commit_idp = NULL;
7944 char *in_repo_path = NULL, *p;
7946 if (status == GOT_STATUS_UNVERSIONED ||
7947 status == GOT_STATUS_NO_CHANGE)
7949 if (status == GOT_STATUS_NONEXISTENT)
7950 return got_error_set_errno(ENOENT, relpath);
7952 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7954 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7956 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7957 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7959 return got_error_from_errno("asprintf");
7961 if (got_fileindex_entry_has_commit(ie)) {
7962 memcpy(base_commit_id.sha1, ie->commit_sha1,
7963 SHA1_DIGEST_LENGTH);
7964 base_commit_idp = &base_commit_id;
7967 if (status == GOT_STATUS_CONFLICT) {
7968 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7970 } else if (status != GOT_STATUS_ADD &&
7971 status != GOT_STATUS_MODIFY &&
7972 status != GOT_STATUS_DELETE) {
7973 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7977 a->have_changes = 1;
7982 err = check_out_of_date(p, status, staged_status,
7983 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7984 GOT_ERR_STAGE_OUT_OF_DATE);
7990 struct stage_path_arg {
7991 struct got_worktree *worktree;
7992 struct got_fileindex *fileindex;
7993 struct got_repository *repo;
7994 got_worktree_status_cb status_cb;
7996 got_worktree_patch_cb patch_cb;
7998 int staged_something;
7999 int allow_bad_symlinks;
8002 static const struct got_error *
8003 stage_path(void *arg, unsigned char status,
8004 unsigned char staged_status, const char *relpath,
8005 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8006 struct got_object_id *commit_id, int dirfd, const char *de_name)
8008 struct stage_path_arg *a = arg;
8009 const struct got_error *err = NULL;
8010 struct got_fileindex_entry *ie;
8011 char *ondisk_path = NULL, *path_content = NULL;
8013 struct got_object_id *new_staged_blob_id = NULL;
8016 if (status == GOT_STATUS_UNVERSIONED)
8019 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8021 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8023 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8025 return got_error_from_errno("asprintf");
8028 case GOT_STATUS_ADD:
8029 case GOT_STATUS_MODIFY:
8030 /* XXX could sb.st_mode be passed in by our caller? */
8031 if (lstat(ondisk_path, &sb) == -1) {
8032 err = got_error_from_errno2("lstat", ondisk_path);
8036 if (status == GOT_STATUS_ADD) {
8037 int choice = GOT_PATCH_CHOICE_NONE;
8038 err = (*a->patch_cb)(&choice, a->patch_arg,
8039 status, ie->path, NULL, 1, 1);
8042 if (choice != GOT_PATCH_CHOICE_YES)
8045 err = create_patched_content(&path_content, 0,
8046 staged_blob_id ? staged_blob_id : blob_id,
8047 ondisk_path, dirfd, de_name, ie->path,
8048 a->repo, a->patch_cb, a->patch_arg);
8049 if (err || path_content == NULL)
8053 err = got_object_blob_create(&new_staged_blob_id,
8054 path_content ? path_content : ondisk_path, a->repo);
8057 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8058 SHA1_DIGEST_LENGTH);
8059 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8060 stage = GOT_FILEIDX_STAGE_ADD;
8062 stage = GOT_FILEIDX_STAGE_MODIFY;
8063 got_fileindex_entry_stage_set(ie, stage);
8064 if (S_ISLNK(sb.st_mode)) {
8065 int is_bad_symlink = 0;
8066 if (!a->allow_bad_symlinks) {
8067 char target_path[PATH_MAX];
8069 target_len = readlink(ondisk_path, target_path,
8070 sizeof(target_path));
8071 if (target_len == -1) {
8072 err = got_error_from_errno2("readlink",
8076 err = is_bad_symlink_target(&is_bad_symlink,
8077 target_path, target_len, ondisk_path,
8078 a->worktree->root_path);
8081 if (is_bad_symlink) {
8082 err = got_error_path(ondisk_path,
8083 GOT_ERR_BAD_SYMLINK);
8088 got_fileindex_entry_staged_filetype_set(ie,
8089 GOT_FILEIDX_MODE_BAD_SYMLINK);
8091 got_fileindex_entry_staged_filetype_set(ie,
8092 GOT_FILEIDX_MODE_SYMLINK);
8094 got_fileindex_entry_staged_filetype_set(ie,
8095 GOT_FILEIDX_MODE_REGULAR_FILE);
8097 a->staged_something = 1;
8098 if (a->status_cb == NULL)
8100 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8101 get_staged_status(ie), relpath, blob_id,
8102 new_staged_blob_id, NULL, dirfd, de_name);
8104 case GOT_STATUS_DELETE:
8105 if (staged_status == GOT_STATUS_DELETE)
8108 int choice = GOT_PATCH_CHOICE_NONE;
8109 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8110 ie->path, NULL, 1, 1);
8113 if (choice == GOT_PATCH_CHOICE_NO)
8115 if (choice != GOT_PATCH_CHOICE_YES) {
8116 err = got_error(GOT_ERR_PATCH_CHOICE);
8120 stage = GOT_FILEIDX_STAGE_DELETE;
8121 got_fileindex_entry_stage_set(ie, stage);
8122 a->staged_something = 1;
8123 if (a->status_cb == NULL)
8125 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8126 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8129 case GOT_STATUS_NO_CHANGE:
8131 case GOT_STATUS_CONFLICT:
8132 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8134 case GOT_STATUS_NONEXISTENT:
8135 err = got_error_set_errno(ENOENT, relpath);
8138 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8142 if (path_content && unlink(path_content) == -1 && err == NULL)
8143 err = got_error_from_errno2("unlink", path_content);
8146 free(new_staged_blob_id);
8150 const struct got_error *
8151 got_worktree_stage(struct got_worktree *worktree,
8152 struct got_pathlist_head *paths,
8153 got_worktree_status_cb status_cb, void *status_arg,
8154 got_worktree_patch_cb patch_cb, void *patch_arg,
8155 int allow_bad_symlinks, struct got_repository *repo)
8157 const struct got_error *err = NULL, *sync_err, *unlockerr;
8158 struct got_pathlist_entry *pe;
8159 struct got_fileindex *fileindex = NULL;
8160 char *fileindex_path = NULL;
8161 struct got_reference *head_ref = NULL;
8162 struct got_object_id *head_commit_id = NULL;
8163 struct check_stage_ok_arg oka;
8164 struct stage_path_arg spa;
8166 err = lock_worktree(worktree, LOCK_EX);
8170 err = got_ref_open(&head_ref, repo,
8171 got_worktree_get_head_ref_name(worktree), 0);
8174 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8177 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8181 /* Check pre-conditions before staging anything. */
8182 oka.head_commit_id = head_commit_id;
8183 oka.worktree = worktree;
8184 oka.fileindex = fileindex;
8186 oka.have_changes = 0;
8187 TAILQ_FOREACH(pe, paths, entry) {
8188 err = worktree_status(worktree, pe->path, fileindex, repo,
8189 check_stage_ok, &oka, NULL, NULL, 1, 0);
8193 if (!oka.have_changes) {
8194 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8198 spa.worktree = worktree;
8199 spa.fileindex = fileindex;
8201 spa.patch_cb = patch_cb;
8202 spa.patch_arg = patch_arg;
8203 spa.status_cb = status_cb;
8204 spa.status_arg = status_arg;
8205 spa.staged_something = 0;
8206 spa.allow_bad_symlinks = allow_bad_symlinks;
8207 TAILQ_FOREACH(pe, paths, entry) {
8208 err = worktree_status(worktree, pe->path, fileindex, repo,
8209 stage_path, &spa, NULL, NULL, 1, 0);
8213 if (!spa.staged_something) {
8214 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8218 sync_err = sync_fileindex(fileindex, fileindex_path);
8219 if (sync_err && err == NULL)
8223 got_ref_close(head_ref);
8224 free(head_commit_id);
8225 free(fileindex_path);
8227 got_fileindex_free(fileindex);
8228 unlockerr = lock_worktree(worktree, LOCK_SH);
8229 if (unlockerr && err == NULL)
8234 struct unstage_path_arg {
8235 struct got_worktree *worktree;
8236 struct got_fileindex *fileindex;
8237 struct got_repository *repo;
8238 got_worktree_checkout_cb progress_cb;
8240 got_worktree_patch_cb patch_cb;
8244 static const struct got_error *
8245 create_unstaged_content(char **path_unstaged_content,
8246 char **path_new_staged_content, struct got_object_id *blob_id,
8247 struct got_object_id *staged_blob_id, const char *relpath,
8248 struct got_repository *repo,
8249 got_worktree_patch_cb patch_cb, void *patch_arg)
8251 const struct got_error *err, *free_err;
8252 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8253 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8254 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8255 struct got_diffreg_result *diffreg_result = NULL;
8256 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8257 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8259 *path_unstaged_content = NULL;
8260 *path_new_staged_content = NULL;
8262 err = got_object_id_str(&label1, blob_id);
8265 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8269 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8273 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8277 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8281 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8285 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8289 err = got_diff_files(&diffreg_result, f1, label1, f2,
8290 path2, 3, 0, 1, NULL);
8294 err = got_opentemp_named(path_unstaged_content, &outfile,
8295 "got-unstaged-content");
8298 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8299 "got-new-staged-content");
8303 if (fseek(f1, 0L, SEEK_SET) == -1) {
8304 err = got_ferror(f1, GOT_ERR_IO);
8307 if (fseek(f2, 0L, SEEK_SET) == -1) {
8308 err = got_ferror(f2, GOT_ERR_IO);
8311 /* Count the number of actual changes in the diff result. */
8312 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8313 struct diff_chunk_context cc = {};
8314 diff_chunk_context_load_change(&cc, &nchunks_used,
8315 diffreg_result->result, n, 0);
8318 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8320 err = apply_or_reject_change(&choice, &nchunks_used,
8321 diffreg_result->result, n, relpath, f1, f2,
8322 &line_cur1, &line_cur2,
8323 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8326 if (choice == GOT_PATCH_CHOICE_YES)
8329 have_rejected_content = 1;
8330 if (choice == GOT_PATCH_CHOICE_QUIT)
8333 if (have_content || have_rejected_content)
8334 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8335 outfile, rejectfile);
8339 got_object_blob_close(blob);
8341 got_object_blob_close(staged_blob);
8342 free_err = got_diffreg_result_free(diffreg_result);
8343 if (free_err && err == NULL)
8345 if (f1 && fclose(f1) == EOF && err == NULL)
8346 err = got_error_from_errno2("fclose", path1);
8347 if (f2 && fclose(f2) == EOF && err == NULL)
8348 err = got_error_from_errno2("fclose", path2);
8349 if (outfile && fclose(outfile) == EOF && err == NULL)
8350 err = got_error_from_errno2("fclose", *path_unstaged_content);
8351 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8352 err = got_error_from_errno2("fclose", *path_new_staged_content);
8353 if (path1 && unlink(path1) == -1 && err == NULL)
8354 err = got_error_from_errno2("unlink", path1);
8355 if (path2 && unlink(path2) == -1 && err == NULL)
8356 err = got_error_from_errno2("unlink", path2);
8357 if (err || !have_content) {
8358 if (*path_unstaged_content &&
8359 unlink(*path_unstaged_content) == -1 && err == NULL)
8360 err = got_error_from_errno2("unlink",
8361 *path_unstaged_content);
8362 free(*path_unstaged_content);
8363 *path_unstaged_content = NULL;
8365 if (err || !have_content || !have_rejected_content) {
8366 if (*path_new_staged_content &&
8367 unlink(*path_new_staged_content) == -1 && err == NULL)
8368 err = got_error_from_errno2("unlink",
8369 *path_new_staged_content);
8370 free(*path_new_staged_content);
8371 *path_new_staged_content = NULL;
8378 static const struct got_error *
8379 unstage_hunks(struct got_object_id *staged_blob_id,
8380 struct got_blob_object *blob_base,
8381 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8382 const char *ondisk_path, const char *label_orig,
8383 struct got_worktree *worktree, struct got_repository *repo,
8384 got_worktree_patch_cb patch_cb, void *patch_arg,
8385 got_worktree_checkout_cb progress_cb, void *progress_arg)
8387 const struct got_error *err = NULL;
8388 char *path_unstaged_content = NULL;
8389 char *path_new_staged_content = NULL;
8390 char *parent = NULL, *base_path = NULL;
8391 char *blob_base_path = NULL;
8392 struct got_object_id *new_staged_blob_id = NULL;
8393 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8396 err = create_unstaged_content(&path_unstaged_content,
8397 &path_new_staged_content, blob_id, staged_blob_id,
8398 ie->path, repo, patch_cb, patch_arg);
8402 if (path_unstaged_content == NULL)
8405 if (path_new_staged_content) {
8406 err = got_object_blob_create(&new_staged_blob_id,
8407 path_new_staged_content, repo);
8412 f = fopen(path_unstaged_content, "r");
8414 err = got_error_from_errno2("fopen",
8415 path_unstaged_content);
8418 if (fstat(fileno(f), &sb) == -1) {
8419 err = got_error_from_errno2("fstat", path_unstaged_content);
8422 if (got_fileindex_entry_staged_filetype_get(ie) ==
8423 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8424 char link_target[PATH_MAX];
8426 r = fread(link_target, 1, sizeof(link_target), f);
8427 if (r == 0 && ferror(f)) {
8428 err = got_error_from_errno("fread");
8431 if (r >= sizeof(link_target)) { /* should not happen */
8432 err = got_error(GOT_ERR_NO_SPACE);
8435 link_target[r] = '\0';
8436 err = merge_symlink(worktree, blob_base,
8437 ondisk_path, ie->path, label_orig, link_target,
8438 worktree->base_commit_id, repo, progress_cb,
8441 int local_changes_subsumed;
8443 err = got_path_dirname(&parent, ondisk_path);
8447 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8449 err = got_error_from_errno("asprintf");
8454 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8457 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8463 * In order the run a 3-way merge with a symlink we copy the symlink's
8464 * target path into a temporary file and use that file with diff3.
8466 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8467 err = dump_symlink_target_path_to_file(&f_deriv2,
8473 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8475 err = got_error_from_errno2("open", ondisk_path);
8478 f_deriv2 = fdopen(fd, "r");
8479 if (f_deriv2 == NULL) {
8480 err = got_error_from_errno2("fdopen", ondisk_path);
8486 err = merge_file(&local_changes_subsumed, worktree,
8487 f_base, f, f_deriv2, ondisk_path, ie->path,
8488 got_fileindex_perms_to_st(ie),
8489 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8490 repo, progress_cb, progress_arg);
8495 if (new_staged_blob_id) {
8496 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8497 SHA1_DIGEST_LENGTH);
8499 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8500 got_fileindex_entry_staged_filetype_set(ie, 0);
8503 free(new_staged_blob_id);
8504 if (path_unstaged_content &&
8505 unlink(path_unstaged_content) == -1 && err == NULL)
8506 err = got_error_from_errno2("unlink", path_unstaged_content);
8507 if (path_new_staged_content &&
8508 unlink(path_new_staged_content) == -1 && err == NULL)
8509 err = got_error_from_errno2("unlink", path_new_staged_content);
8510 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8511 err = got_error_from_errno2("unlink", blob_base_path);
8512 if (f_base && fclose(f_base) == EOF && err == NULL)
8513 err = got_error_from_errno2("fclose", path_unstaged_content);
8514 if (f && fclose(f) == EOF && err == NULL)
8515 err = got_error_from_errno2("fclose", path_unstaged_content);
8516 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8517 err = got_error_from_errno2("fclose", ondisk_path);
8518 free(path_unstaged_content);
8519 free(path_new_staged_content);
8520 free(blob_base_path);
8526 static const struct got_error *
8527 unstage_path(void *arg, unsigned char status,
8528 unsigned char staged_status, const char *relpath,
8529 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8530 struct got_object_id *commit_id, int dirfd, const char *de_name)
8532 const struct got_error *err = NULL;
8533 struct unstage_path_arg *a = arg;
8534 struct got_fileindex_entry *ie;
8535 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8536 char *ondisk_path = NULL;
8537 char *id_str = NULL, *label_orig = NULL;
8538 int local_changes_subsumed;
8541 if (staged_status != GOT_STATUS_ADD &&
8542 staged_status != GOT_STATUS_MODIFY &&
8543 staged_status != GOT_STATUS_DELETE)
8546 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8548 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8550 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8552 return got_error_from_errno("asprintf");
8554 err = got_object_id_str(&id_str,
8555 commit_id ? commit_id : a->worktree->base_commit_id);
8558 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8560 err = got_error_from_errno("asprintf");
8564 switch (staged_status) {
8565 case GOT_STATUS_MODIFY:
8566 err = got_object_open_as_blob(&blob_base, a->repo,
8571 case GOT_STATUS_ADD:
8573 if (staged_status == GOT_STATUS_ADD) {
8574 int choice = GOT_PATCH_CHOICE_NONE;
8575 err = (*a->patch_cb)(&choice, a->patch_arg,
8576 staged_status, ie->path, NULL, 1, 1);
8579 if (choice != GOT_PATCH_CHOICE_YES)
8582 err = unstage_hunks(staged_blob_id,
8583 blob_base, blob_id, ie, ondisk_path,
8584 label_orig, a->worktree, a->repo,
8585 a->patch_cb, a->patch_arg,
8586 a->progress_cb, a->progress_arg);
8587 break; /* Done with this file. */
8590 err = got_object_open_as_blob(&blob_staged, a->repo,
8591 staged_blob_id, 8192);
8594 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8595 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8596 case GOT_FILEIDX_MODE_REGULAR_FILE:
8597 err = merge_blob(&local_changes_subsumed, a->worktree,
8598 blob_base, ondisk_path, relpath,
8599 got_fileindex_perms_to_st(ie), label_orig,
8600 blob_staged, commit_id ? commit_id :
8601 a->worktree->base_commit_id, a->repo,
8602 a->progress_cb, a->progress_arg);
8604 case GOT_FILEIDX_MODE_SYMLINK:
8605 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8606 char *staged_target;
8607 err = got_object_blob_read_to_str(
8608 &staged_target, blob_staged);
8611 err = merge_symlink(a->worktree, blob_base,
8612 ondisk_path, relpath, label_orig,
8613 staged_target, commit_id ? commit_id :
8614 a->worktree->base_commit_id,
8615 a->repo, a->progress_cb, a->progress_arg);
8616 free(staged_target);
8618 err = merge_blob(&local_changes_subsumed,
8619 a->worktree, blob_base, ondisk_path,
8620 relpath, got_fileindex_perms_to_st(ie),
8621 label_orig, blob_staged,
8622 commit_id ? commit_id :
8623 a->worktree->base_commit_id, a->repo,
8624 a->progress_cb, a->progress_arg);
8628 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8632 got_fileindex_entry_stage_set(ie,
8633 GOT_FILEIDX_STAGE_NONE);
8634 got_fileindex_entry_staged_filetype_set(ie, 0);
8637 case GOT_STATUS_DELETE:
8639 int choice = GOT_PATCH_CHOICE_NONE;
8640 err = (*a->patch_cb)(&choice, a->patch_arg,
8641 staged_status, ie->path, NULL, 1, 1);
8644 if (choice == GOT_PATCH_CHOICE_NO)
8646 if (choice != GOT_PATCH_CHOICE_YES) {
8647 err = got_error(GOT_ERR_PATCH_CHOICE);
8651 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8652 got_fileindex_entry_staged_filetype_set(ie, 0);
8653 err = get_file_status(&status, &sb, ie, ondisk_path,
8654 dirfd, de_name, a->repo);
8657 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8663 got_object_blob_close(blob_base);
8665 got_object_blob_close(blob_staged);
8671 const struct got_error *
8672 got_worktree_unstage(struct got_worktree *worktree,
8673 struct got_pathlist_head *paths,
8674 got_worktree_checkout_cb progress_cb, void *progress_arg,
8675 got_worktree_patch_cb patch_cb, void *patch_arg,
8676 struct got_repository *repo)
8678 const struct got_error *err = NULL, *sync_err, *unlockerr;
8679 struct got_pathlist_entry *pe;
8680 struct got_fileindex *fileindex = NULL;
8681 char *fileindex_path = NULL;
8682 struct unstage_path_arg upa;
8684 err = lock_worktree(worktree, LOCK_EX);
8688 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8692 upa.worktree = worktree;
8693 upa.fileindex = fileindex;
8695 upa.progress_cb = progress_cb;
8696 upa.progress_arg = progress_arg;
8697 upa.patch_cb = patch_cb;
8698 upa.patch_arg = patch_arg;
8699 TAILQ_FOREACH(pe, paths, entry) {
8700 err = worktree_status(worktree, pe->path, fileindex, repo,
8701 unstage_path, &upa, NULL, NULL, 1, 0);
8706 sync_err = sync_fileindex(fileindex, fileindex_path);
8707 if (sync_err && err == NULL)
8710 free(fileindex_path);
8712 got_fileindex_free(fileindex);
8713 unlockerr = lock_worktree(worktree, LOCK_SH);
8714 if (unlockerr && err == NULL)
8719 struct report_file_info_arg {
8720 struct got_worktree *worktree;
8721 got_worktree_path_info_cb info_cb;
8723 struct got_pathlist_head *paths;
8724 got_cancel_cb cancel_cb;
8728 static const struct got_error *
8729 report_file_info(void *arg, struct got_fileindex_entry *ie)
8731 struct report_file_info_arg *a = arg;
8732 struct got_pathlist_entry *pe;
8733 struct got_object_id blob_id, staged_blob_id, commit_id;
8734 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8735 struct got_object_id *commit_idp = NULL;
8738 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8739 return got_error(GOT_ERR_CANCELLED);
8741 TAILQ_FOREACH(pe, a->paths, entry) {
8742 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8743 got_path_is_child(ie->path, pe->path, pe->path_len))
8746 if (pe == NULL) /* not found */
8749 if (got_fileindex_entry_has_blob(ie)) {
8750 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8751 blob_idp = &blob_id;
8753 stage = got_fileindex_entry_stage_get(ie);
8754 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8755 stage == GOT_FILEIDX_STAGE_ADD) {
8756 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8757 SHA1_DIGEST_LENGTH);
8758 staged_blob_idp = &staged_blob_id;
8761 if (got_fileindex_entry_has_commit(ie)) {
8762 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8763 commit_idp = &commit_id;
8766 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8767 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8770 const struct got_error *
8771 got_worktree_path_info(struct got_worktree *worktree,
8772 struct got_pathlist_head *paths,
8773 got_worktree_path_info_cb info_cb, void *info_arg,
8774 got_cancel_cb cancel_cb, void *cancel_arg)
8777 const struct got_error *err = NULL, *unlockerr;
8778 struct got_fileindex *fileindex = NULL;
8779 char *fileindex_path = NULL;
8780 struct report_file_info_arg arg;
8782 err = lock_worktree(worktree, LOCK_SH);
8786 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8790 arg.worktree = worktree;
8791 arg.info_cb = info_cb;
8792 arg.info_arg = info_arg;
8794 arg.cancel_cb = cancel_cb;
8795 arg.cancel_arg = cancel_arg;
8796 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8799 free(fileindex_path);
8801 got_fileindex_free(fileindex);
8802 unlockerr = lock_worktree(worktree, LOCK_UN);
8803 if (unlockerr && err == NULL)