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.
33 #include "got_compat.h"
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_object.h"
40 #include "got_cancel.h"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
45 #include "got_lib_worktree.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
55 #include "got_lib_gotconfig.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #define GOT_MERGE_LABEL_MERGED "merged change"
62 #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 static const struct got_error *
65 create_meta_file(const char *path_got, const char *name, const char *content)
67 const struct got_error *err = NULL;
70 if (asprintf(&path, "%s/%s", path_got, name) == -1)
71 return got_error_from_errno("asprintf");
73 err = got_path_create_file(path, content);
78 static const struct got_error *
79 update_meta_file(const char *path_got, const char *name, const char *content)
81 const struct got_error *err = NULL;
86 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
87 err = got_error_from_errno("asprintf");
92 err = got_opentemp_named(&tmppath, &tmpfile, path);
97 int len = fprintf(tmpfile, "%s\n", content);
98 if (len != strlen(content) + 1) {
99 err = got_error_from_errno2("fprintf", tmppath);
104 if (rename(tmppath, path) != 0) {
105 err = got_error_from_errno3("rename", tmppath, path);
111 if (fclose(tmpfile) == EOF && err == NULL)
112 err = got_error_from_errno2("fclose", tmppath);
117 static const struct got_error *
118 write_head_ref(const char *path_got, struct got_reference *head_ref)
120 const struct got_error *err = NULL;
123 if (got_ref_is_symbolic(head_ref)) {
124 refstr = got_ref_to_str(head_ref);
126 return got_error_from_errno("got_ref_to_str");
128 refstr = strdup(got_ref_get_name(head_ref));
130 return got_error_from_errno("strdup");
132 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
137 const struct got_error *
138 got_worktree_init(const char *path, struct got_reference *head_ref,
139 const char *prefix, struct got_repository *repo)
141 const struct got_error *err = NULL;
142 struct got_object_id *commit_id = NULL;
144 uint32_t uuid_status;
146 char *path_got = NULL;
147 char *formatstr = NULL;
148 char *absprefix = NULL;
149 char *basestr = NULL;
150 char *uuidstr = NULL;
152 if (strcmp(path, got_repo_get_path(repo)) == 0) {
153 err = got_error(GOT_ERR_WORKTREE_REPO);
157 err = got_ref_resolve(&commit_id, repo, head_ref);
160 err = got_object_get_type(&obj_type, repo, commit_id);
163 if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 return got_error(GOT_ERR_OBJ_TYPE);
166 if (!got_path_is_absolute(prefix)) {
167 if (asprintf(&absprefix, "/%s", prefix) == -1)
168 return got_error_from_errno("asprintf");
171 /* Create top-level directory (may already exist). */
172 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 err = got_error_from_errno2("mkdir", path);
177 /* Create .got directory (may already exist). */
178 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 err = got_error_from_errno("asprintf");
182 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 err = got_error_from_errno2("mkdir", path_got);
187 /* Create an empty lock file. */
188 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 /* Create an empty file index. */
193 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 /* Write the HEAD reference. */
198 err = write_head_ref(path_got, head_ref);
202 /* Record our base commit. */
203 err = got_object_id_str(&basestr, commit_id);
206 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 /* Store path to repository. */
211 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
212 got_repo_get_path(repo));
216 /* Store in-repository path prefix. */
217 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
218 absprefix ? absprefix : prefix);
223 uuid_create(&uuid, &uuid_status);
224 if (uuid_status != uuid_s_ok) {
225 err = got_error_uuid(uuid_status, "uuid_create");
228 uuid_to_string(&uuid, &uuidstr, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_to_string");
233 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 /* Stamp work tree with format file. */
238 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
239 err = got_error_from_errno("asprintf");
242 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
256 const struct got_error *
257 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
258 const char *path_prefix)
260 char *absprefix = NULL;
262 if (!got_path_is_absolute(path_prefix)) {
263 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
264 return got_error_from_errno("asprintf");
266 *match = (strcmp(absprefix ? absprefix : path_prefix,
267 worktree->path_prefix) == 0);
273 got_worktree_get_head_ref_name(struct got_worktree *worktree)
275 return worktree->head_ref_name;
278 const struct got_error *
279 got_worktree_set_head_ref(struct got_worktree *worktree,
280 struct got_reference *head_ref)
282 const struct got_error *err = NULL;
283 char *path_got = NULL, *head_ref_name = NULL;
285 if (asprintf(&path_got, "%s/%s", worktree->root_path,
286 GOT_WORKTREE_GOT_DIR) == -1) {
287 err = got_error_from_errno("asprintf");
292 head_ref_name = strdup(got_ref_get_name(head_ref));
293 if (head_ref_name == NULL) {
294 err = got_error_from_errno("strdup");
298 err = write_head_ref(path_got, head_ref);
302 free(worktree->head_ref_name);
303 worktree->head_ref_name = head_ref_name;
311 struct got_object_id *
312 got_worktree_get_base_commit_id(struct got_worktree *worktree)
314 return worktree->base_commit_id;
317 const struct got_error *
318 got_worktree_set_base_commit_id(struct got_worktree *worktree,
319 struct got_repository *repo, struct got_object_id *commit_id)
321 const struct got_error *err;
322 struct got_object *obj = NULL;
324 char *path_got = NULL;
326 if (asprintf(&path_got, "%s/%s", worktree->root_path,
327 GOT_WORKTREE_GOT_DIR) == -1) {
328 err = got_error_from_errno("asprintf");
333 err = got_object_open(&obj, repo, commit_id);
337 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
338 err = got_error(GOT_ERR_OBJ_TYPE);
342 /* Record our base commit. */
343 err = got_object_id_str(&id_str, commit_id);
346 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 free(worktree->base_commit_id);
351 worktree->base_commit_id = got_object_id_dup(commit_id);
352 if (worktree->base_commit_id == NULL) {
353 err = got_error_from_errno("got_object_id_dup");
358 got_object_close(obj);
364 const struct got_gotconfig *
365 got_worktree_get_gotconfig(struct got_worktree *worktree)
367 return worktree->gotconfig;
370 static const struct got_error *
371 lock_worktree(struct got_worktree *worktree, int operation)
373 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
374 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
375 : got_error_from_errno2("flock",
376 got_worktree_get_root_path(worktree)));
380 static const struct got_error *
381 add_dir_on_disk(struct got_worktree *worktree, const char *path)
383 const struct got_error *err = NULL;
386 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
387 return got_error_from_errno("asprintf");
389 err = got_path_mkdir(abspath);
390 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
393 if (lstat(abspath, &sb) == -1) {
394 err = got_error_from_errno2("lstat", abspath);
395 } else if (!S_ISDIR(sb.st_mode)) {
396 /* TODO directory is obstructed; do something */
397 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
404 static const struct got_error *
405 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
407 const struct got_error *err = NULL;
410 size_t flen1 = 0, flen2 = 0;
415 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
416 if (flen1 == 0 && ferror(f1)) {
417 err = got_error_from_errno("fread");
420 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
421 if (flen2 == 0 && ferror(f2)) {
422 err = got_error_from_errno("fread");
429 } else if (flen2 == 0) {
433 } else if (flen1 == flen2) {
434 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
447 static const struct got_error *
448 check_files_equal(int *same, FILE *f1, FILE *f2)
455 if (fstat(fileno(f1), &sb) != 0)
456 return got_error_from_errno("fstat");
459 if (fstat(fileno(f2), &sb) != 0)
460 return got_error_from_errno("fstat");
463 if (size1 != size2) {
468 if (fseek(f1, 0L, SEEK_SET) == -1)
469 return got_ferror(f1, GOT_ERR_IO);
470 if (fseek(f2, 0L, SEEK_SET) == -1)
471 return got_ferror(f2, GOT_ERR_IO);
473 return check_file_contents_equal(same, f1, f2);
476 static const struct got_error *
477 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
485 if (fseek(f, 0L, SEEK_SET) == -1)
486 return got_ferror(f, GOT_ERR_IO);
489 flen = fread(fbuf, 1, sizeof(fbuf), f);
492 return got_error_from_errno("fread");
496 outlen = write(outfd, fbuf, flen);
498 return got_error_from_errno("write");
500 return got_error(GOT_ERR_IO);
507 static const struct got_error *
508 merge_binary_file(int *overlapcnt, int merged_fd,
509 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
510 const char *label_deriv, const char *label_orig, const char *label_deriv2,
511 const char *ondisk_path)
513 const struct got_error *err = NULL;
514 int same_content, changed_deriv, changed_deriv2;
515 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
516 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
517 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
518 char *base_path_orig = NULL, *base_path_deriv = NULL;
519 char *base_path_deriv2 = NULL;
523 err = check_files_equal(&same_content, f_deriv, f_deriv2);
528 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
530 err = check_files_equal(&same_content, f_deriv, f_orig);
533 changed_deriv = !same_content;
534 err = check_files_equal(&same_content, f_deriv2, f_orig);
537 changed_deriv2 = !same_content;
539 if (changed_deriv && changed_deriv2) {
541 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
542 err = got_error_from_errno("asprintf");
545 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
546 err = got_error_from_errno("asprintf");
549 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
550 err = got_error_from_errno("asprintf");
553 err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
568 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
571 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
574 if (dprintf(merged_fd, "Binary files differ and cannot be "
575 "merged automatically:\n") < 0) {
576 err = got_error_from_errno("dprintf");
579 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
580 GOT_DIFF_CONFLICT_MARKER_BEGIN,
581 label_deriv ? " " : "",
582 label_deriv ? label_deriv : "",
584 err = got_error_from_errno("dprintf");
588 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
589 GOT_DIFF_CONFLICT_MARKER_ORIG,
590 label_orig ? " " : "",
591 label_orig ? label_orig : "",
593 err = got_error_from_errno("dprintf");
597 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
598 GOT_DIFF_CONFLICT_MARKER_SEP,
600 GOT_DIFF_CONFLICT_MARKER_END,
601 label_deriv2 ? " " : "",
602 label_deriv2 ? label_deriv2 : "") < 0) {
603 err = got_error_from_errno("dprintf");
606 } else if (changed_deriv)
607 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
608 else if (changed_deriv2)
609 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
611 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
613 err = got_error_from_errno2("unlink", path_orig);
614 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
615 err = got_error_from_errno2("close", path_orig);
616 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
617 err = got_error_from_errno2("close", path_deriv);
618 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
619 err = got_error_from_errno2("close", path_deriv2);
623 free(base_path_orig);
624 free(base_path_deriv);
625 free(base_path_deriv2);
630 * Perform a 3-way merge where the file f_orig acts as the common
631 * ancestor, the file f_deriv acts as the first derived version,
632 * and the file f_deriv2 acts as the second derived version.
633 * The merge result will be written to a new file at ondisk_path; any
634 * existing file at this path will be replaced.
636 static const struct got_error *
637 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
638 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
639 const char *path, uint16_t st_mode,
640 const char *label_orig, const char *label_deriv, const char *label_deriv2,
641 enum got_diff_algorithm diff_algo, struct got_repository *repo,
642 got_worktree_checkout_cb progress_cb, void *progress_arg)
644 const struct got_error *err = NULL;
646 FILE *f_merged = NULL;
647 char *merged_path = NULL, *base_path = NULL;
651 *local_changes_subsumed = 0;
653 err = got_path_dirname(&parent, ondisk_path);
657 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
658 err = got_error_from_errno("asprintf");
662 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
667 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
669 if (err->code != GOT_ERR_FILE_BINARY)
671 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
672 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 err = (*progress_cb)(progress_arg,
679 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 if (fsync(merged_fd) != 0) {
684 err = got_error_from_errno("fsync");
688 f_merged = fdopen(merged_fd, "r");
689 if (f_merged == NULL) {
690 err = got_error_from_errno("fdopen");
695 /* Check if a clean merge has subsumed all local changes. */
696 if (overlapcnt == 0) {
697 err = check_files_equal(local_changes_subsumed, f_deriv,
703 if (fchmod(fileno(f_merged), st_mode) != 0) {
704 err = got_error_from_errno2("fchmod", merged_path);
708 if (rename(merged_path, ondisk_path) != 0) {
709 err = got_error_from_errno3("rename", merged_path,
718 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
719 err = got_error_from_errno("close");
720 if (f_merged && fclose(f_merged) == EOF && err == NULL)
721 err = got_error_from_errno("fclose");
728 static const struct got_error *
729 update_symlink(const char *ondisk_path, const char *target_path,
732 /* This is not atomic but matches what 'ln -sf' does. */
733 if (unlink(ondisk_path) == -1)
734 return got_error_from_errno2("unlink", ondisk_path);
735 if (symlink(target_path, ondisk_path) == -1)
736 return got_error_from_errno3("symlink", target_path,
742 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
743 * in the work tree with a file that contains conflict markers and the
744 * conflicting target paths of the original version, a "derived version"
745 * of a symlink from an incoming change, and a local version of the symlink.
747 * The original versions's target path can be NULL if it is not available,
748 * such as if both derived versions added a new symlink at the same path.
750 * The incoming derived symlink target is NULL in case the incoming change
751 * has deleted this symlink.
753 static const struct got_error *
754 install_symlink_conflict(const char *deriv_target,
755 struct got_object_id *deriv_base_commit_id, const char *orig_target,
756 const char *label_orig, const char *local_target, const char *ondisk_path)
758 const struct got_error *err;
759 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
762 err = got_object_id_str(&id_str, deriv_base_commit_id);
764 return got_error_from_errno("asprintf");
766 if (asprintf(&label_deriv, "%s: commit %s",
767 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
768 err = got_error_from_errno("asprintf");
772 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
777 err = got_error_from_errno2("fchmod", path);
781 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
782 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
783 deriv_target ? deriv_target : "(symlink was deleted)",
784 orig_target ? label_orig : "",
785 orig_target ? "\n" : "",
786 orig_target ? orig_target : "",
787 orig_target ? "\n" : "",
788 GOT_DIFF_CONFLICT_MARKER_SEP,
789 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
790 err = got_error_from_errno2("fprintf", path);
794 if (unlink(ondisk_path) == -1) {
795 err = got_error_from_errno2("unlink", ondisk_path);
798 if (rename(path, ondisk_path) == -1) {
799 err = got_error_from_errno3("rename", path, ondisk_path);
803 if (f != NULL && fclose(f) == EOF && err == NULL)
804 err = got_error_from_errno2("fclose", path);
811 /* forward declaration */
812 static const struct got_error *
813 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
814 const char *, const char *, uint16_t, const char *,
815 struct got_blob_object *, struct got_object_id *,
816 struct got_repository *, got_worktree_checkout_cb, void *);
819 * Merge a symlink into the work tree, where blob_orig acts as the common
820 * ancestor, deriv_target is the link target of the first derived version,
821 * and the symlink on disk acts as the second derived version.
822 * Assume that contents of both blobs represent symlinks.
824 static const struct got_error *
825 merge_symlink(struct got_worktree *worktree,
826 struct got_blob_object *blob_orig, const char *ondisk_path,
827 const char *path, const char *label_orig, const char *deriv_target,
828 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
829 got_worktree_checkout_cb progress_cb, void *progress_arg)
831 const struct got_error *err = NULL;
832 char *ancestor_target = NULL;
834 ssize_t ondisk_len, deriv_len;
835 char ondisk_target[PATH_MAX];
836 int have_local_change = 0;
837 int have_incoming_change = 0;
839 if (lstat(ondisk_path, &sb) == -1)
840 return got_error_from_errno2("lstat", ondisk_path);
842 ondisk_len = readlink(ondisk_path, ondisk_target,
843 sizeof(ondisk_target));
844 if (ondisk_len == -1) {
845 err = got_error_from_errno2("readlink",
849 ondisk_target[ondisk_len] = '\0';
852 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
857 if (ancestor_target == NULL ||
858 (ondisk_len != strlen(ancestor_target) ||
859 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
860 have_local_change = 1;
862 deriv_len = strlen(deriv_target);
863 if (ancestor_target == NULL ||
864 (deriv_len != strlen(ancestor_target) ||
865 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
866 have_incoming_change = 1;
868 if (!have_local_change && !have_incoming_change) {
869 if (ancestor_target) {
870 /* Both sides made the same change. */
871 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
873 } else if (deriv_len == ondisk_len &&
874 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
875 /* Both sides added the same symlink. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
879 /* Both sides added symlinks which don't match. */
880 err = install_symlink_conflict(deriv_target,
881 deriv_base_commit_id, ancestor_target,
882 label_orig, ondisk_target, ondisk_path);
885 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
888 } else if (!have_local_change && have_incoming_change) {
889 /* Apply the incoming change. */
890 err = update_symlink(ondisk_path, deriv_target,
891 strlen(deriv_target));
894 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
895 } else if (have_local_change && have_incoming_change) {
896 if (deriv_len == ondisk_len &&
897 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
898 /* Both sides made the same change. */
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
902 err = install_symlink_conflict(deriv_target,
903 deriv_base_commit_id, ancestor_target, label_orig,
904 ondisk_target, ondisk_path);
907 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 free(ancestor_target);
917 static const struct got_error *
918 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
920 const struct got_error *err = NULL;
921 char target_path[PATH_MAX];
930 return got_error_from_errno("got_opentemp");
931 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
932 if (target_len == -1) {
933 err = got_error_from_errno2("readlink", ondisk_path);
936 n = fwrite(target_path, 1, target_len, f);
937 if (n != target_len) {
938 err = got_ferror(f, GOT_ERR_IO);
941 if (fflush(f) == EOF) {
942 err = got_error_from_errno("fflush");
945 if (fseek(f, 0L, SEEK_SET) == -1) {
946 err = got_ferror(f, GOT_ERR_IO);
958 * Perform a 3-way merge where blob_orig acts as the common ancestor,
959 * blob_deriv acts as the first derived version, and the file on disk
960 * acts as the second derived version.
962 static const struct got_error *
963 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
964 struct got_blob_object *blob_orig, const char *ondisk_path,
965 const char *path, uint16_t st_mode, const char *label_orig,
966 struct got_blob_object *blob_deriv,
967 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
968 got_worktree_checkout_cb progress_cb, void *progress_arg)
970 const struct got_error *err = NULL;
971 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
972 char *blob_orig_path = NULL;
973 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
974 char *label_deriv = NULL, *parent = NULL;
976 *local_changes_subsumed = 0;
978 err = got_path_dirname(&parent, ondisk_path);
983 if (asprintf(&base_path, "%s/got-merge-blob-orig",
985 err = got_error_from_errno("asprintf");
990 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
993 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 * No common ancestor exists. This is an "add vs add" conflict
1001 * and we simply use an empty ancestor file to make both files
1002 * appear in the merged result in their entirety.
1004 f_orig = got_opentemp();
1005 if (f_orig == NULL) {
1006 err = got_error_from_errno("got_opentemp");
1011 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1012 err = got_error_from_errno("asprintf");
1017 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1020 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 err = got_object_id_str(&id_str, deriv_base_commit_id);
1028 if (asprintf(&label_deriv, "%s: commit %s",
1029 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1030 err = got_error_from_errno("asprintf");
1035 * In order the run a 3-way merge with a symlink we copy the symlink's
1036 * target path into a temporary file and use that file with diff3.
1038 if (S_ISLNK(st_mode)) {
1039 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1046 err = got_error_from_errno2("open", ondisk_path);
1049 f_deriv2 = fdopen(fd, "r");
1050 if (f_deriv2 == NULL) {
1051 err = got_error_from_errno2("fdopen", ondisk_path);
1057 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1058 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1059 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1061 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1062 err = got_error_from_errno("fclose");
1063 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1064 err = got_error_from_errno("fclose");
1065 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1068 if (blob_orig_path) {
1069 unlink(blob_orig_path);
1070 free(blob_orig_path);
1072 if (blob_deriv_path) {
1073 unlink(blob_deriv_path);
1074 free(blob_deriv_path);
1082 static const struct got_error *
1083 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1084 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1085 int wt_fd, const char *path, struct got_object_id *blob_id)
1087 const struct got_error *err = NULL;
1088 struct got_fileindex_entry *new_ie;
1092 err = got_fileindex_entry_alloc(&new_ie, path);
1096 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1097 blob_id->sha1, base_commit_id->sha1, 1);
1101 err = got_fileindex_entry_add(fileindex, new_ie);
1104 got_fileindex_entry_free(new_ie);
1111 get_ondisk_perms(int executable, mode_t st_mode)
1113 mode_t xbits = S_IXUSR;
1116 /* Map read bits to execute bits. */
1117 if (st_mode & S_IRGRP)
1119 if (st_mode & S_IROTH)
1121 return st_mode | xbits;
1124 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1127 /* forward declaration */
1128 static const struct got_error *
1129 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1130 const char *path, mode_t te_mode, mode_t st_mode,
1131 struct got_blob_object *blob, int restoring_missing_file,
1132 int reverting_versioned_file, int installing_bad_symlink,
1133 int path_is_unversioned, struct got_repository *repo,
1134 got_worktree_checkout_cb progress_cb, void *progress_arg);
1137 * This function assumes that the provided symlink target points at a
1138 * safe location in the work tree!
1140 static const struct got_error *
1141 replace_existing_symlink(int *did_something, const char *ondisk_path,
1142 const char *target_path, size_t target_len)
1144 const struct got_error *err = NULL;
1146 char etarget[PATH_MAX];
1152 * "Bad" symlinks (those pointing outside the work tree or into the
1153 * .got directory) are installed in the work tree as a regular file
1154 * which contains the bad symlink target path.
1155 * The new symlink target has already been checked for safety by our
1156 * caller. If we can successfully open a regular file then we simply
1157 * replace this file with a symlink below.
1159 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1161 if (!got_err_open_nofollow_on_symlink())
1162 return got_error_from_errno2("open", ondisk_path);
1164 /* We are updating an existing on-disk symlink. */
1165 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1167 return got_error_from_errno2("readlink", ondisk_path);
1169 if (elen == target_len &&
1170 memcmp(etarget, target_path, target_len) == 0)
1171 return NULL; /* nothing to do */
1175 err = update_symlink(ondisk_path, target_path, target_len);
1176 if (fd != -1 && close(fd) == -1 && err == NULL)
1177 err = got_error_from_errno2("close", ondisk_path);
1181 static const struct got_error *
1182 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1183 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1185 const struct got_error *err = NULL;
1186 char canonpath[PATH_MAX];
1187 char *path_got = NULL;
1189 *is_bad_symlink = 0;
1191 if (target_len >= sizeof(canonpath)) {
1192 *is_bad_symlink = 1;
1197 * We do not use realpath(3) to resolve the symlink's target
1198 * path because we don't want to resolve symlinks recursively.
1199 * Instead we make the path absolute and then canonicalize it.
1200 * Relative symlink target lookup should begin at the directory
1201 * in which the blob object is being installed.
1203 if (!got_path_is_absolute(target_path)) {
1204 char *abspath, *parent;
1205 err = got_path_dirname(&parent, ondisk_path);
1208 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1210 return got_error_from_errno("asprintf");
1213 if (strlen(abspath) >= sizeof(canonpath)) {
1214 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1223 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1228 /* Only allow symlinks pointing at paths within the work tree. */
1229 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1230 *is_bad_symlink = 1;
1234 /* Do not allow symlinks pointing into the .got directory. */
1235 if (asprintf(&path_got, "%s/%s", wtroot_path,
1236 GOT_WORKTREE_GOT_DIR) == -1)
1237 return got_error_from_errno("asprintf");
1238 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1239 *is_bad_symlink = 1;
1245 static const struct got_error *
1246 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1247 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1248 int restoring_missing_file, int reverting_versioned_file,
1249 int path_is_unversioned, int allow_bad_symlinks,
1250 struct got_repository *repo,
1251 got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 const struct got_error *err = NULL;
1254 char target_path[PATH_MAX];
1255 size_t len, target_len = 0;
1256 char *path_got = NULL;
1257 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1258 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1260 *is_bad_symlink = 0;
1263 * Blob object content specifies the target path of the link.
1264 * If a symbolic link cannot be installed we instead create
1265 * a regular file which contains the link target path stored
1266 * in the blob object.
1269 err = got_object_blob_read_block(&len, blob);
1270 if (len + target_len >= sizeof(target_path)) {
1271 /* Path too long; install as a regular file. */
1272 *is_bad_symlink = 1;
1273 got_object_blob_rewind(blob);
1274 return install_blob(worktree, ondisk_path, path,
1275 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1276 restoring_missing_file, reverting_versioned_file,
1277 1, path_is_unversioned, repo, progress_cb,
1281 /* Skip blob object header first time around. */
1282 memcpy(target_path + target_len, buf + hdrlen,
1284 target_len += len - hdrlen;
1288 target_path[target_len] = '\0';
1290 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1291 ondisk_path, worktree->root_path);
1295 if (*is_bad_symlink && !allow_bad_symlinks) {
1296 /* install as a regular file */
1297 got_object_blob_rewind(blob);
1298 err = install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file, 1,
1301 path_is_unversioned, repo, progress_cb, progress_arg);
1305 if (symlink(target_path, ondisk_path) == -1) {
1306 if (errno == EEXIST) {
1307 int symlink_replaced;
1308 if (path_is_unversioned) {
1309 err = (*progress_cb)(progress_arg,
1310 GOT_STATUS_UNVERSIONED, path);
1313 err = replace_existing_symlink(&symlink_replaced,
1314 ondisk_path, target_path, target_len);
1318 if (symlink_replaced) {
1319 err = (*progress_cb)(progress_arg,
1320 reverting_versioned_file ?
1322 GOT_STATUS_UPDATE, path);
1324 err = (*progress_cb)(progress_arg,
1325 GOT_STATUS_EXISTS, path);
1328 goto done; /* Nothing else to do. */
1331 if (errno == ENOENT) {
1333 err = got_path_dirname(&parent, ondisk_path);
1336 err = add_dir_on_disk(worktree, parent);
1341 * Retry, and fall through to error handling
1342 * below if this second attempt fails.
1344 if (symlink(target_path, ondisk_path) != -1) {
1345 err = NULL; /* success */
1350 /* Handle errors from first or second creation attempt. */
1351 if (errno == ENAMETOOLONG) {
1352 /* bad target path; install as a regular file */
1353 *is_bad_symlink = 1;
1354 got_object_blob_rewind(blob);
1355 err = install_blob(worktree, ondisk_path, path,
1356 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1357 restoring_missing_file, reverting_versioned_file, 1,
1358 path_is_unversioned, repo,
1359 progress_cb, progress_arg);
1360 } else if (errno == ENOTDIR) {
1361 err = got_error_path(ondisk_path,
1362 GOT_ERR_FILE_OBSTRUCTED);
1364 err = got_error_from_errno3("symlink",
1365 target_path, ondisk_path);
1367 } else if (progress_cb)
1368 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1369 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1375 static const struct got_error *
1376 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1377 const char *path, mode_t te_mode, mode_t st_mode,
1378 struct got_blob_object *blob, int restoring_missing_file,
1379 int reverting_versioned_file, int installing_bad_symlink,
1380 int path_is_unversioned, struct got_repository *repo,
1381 got_worktree_checkout_cb progress_cb, void *progress_arg)
1383 const struct got_error *err = NULL;
1387 char *tmppath = NULL;
1389 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1390 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1392 if (errno == ENOENT) {
1394 err = got_path_dirname(&parent, path);
1397 err = add_dir_on_disk(worktree, parent);
1401 fd = open(ondisk_path,
1402 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1403 GOT_DEFAULT_FILE_MODE);
1405 return got_error_from_errno2("open",
1407 } else if (errno == EEXIST) {
1408 if (path_is_unversioned) {
1409 err = (*progress_cb)(progress_arg,
1410 GOT_STATUS_UNVERSIONED, path);
1413 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1414 !S_ISREG(st_mode) && !installing_bad_symlink) {
1415 /* TODO file is obstructed; do something */
1416 err = got_error_path(ondisk_path,
1417 GOT_ERR_FILE_OBSTRUCTED);
1420 err = got_opentemp_named_fd(&tmppath, &fd,
1427 return got_error_from_errno2("open", ondisk_path);
1430 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1431 err = got_error_from_errno2("fchmod",
1432 update ? tmppath : ondisk_path);
1437 if (restoring_missing_file)
1438 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1440 else if (reverting_versioned_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1444 err = (*progress_cb)(progress_arg,
1445 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1450 hdrlen = got_object_blob_get_hdrlen(blob);
1452 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1453 err = got_object_blob_read_block(&len, blob);
1457 /* Skip blob object header first time around. */
1458 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1460 err = got_error_from_errno("write");
1462 } else if (outlen != len - hdrlen) {
1463 err = got_error(GOT_ERR_IO);
1470 if (fsync(fd) != 0) {
1471 err = got_error_from_errno("fsync");
1476 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1477 err = got_error_from_errno2("unlink", ondisk_path);
1480 if (rename(tmppath, ondisk_path) != 0) {
1481 err = got_error_from_errno3("rename", tmppath,
1490 if (fd != -1 && close(fd) == -1 && err == NULL)
1491 err = got_error_from_errno("close");
1492 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1493 err = got_error_from_errno2("unlink", tmppath);
1498 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1499 static const struct got_error *
1500 get_modified_file_content_status(unsigned char *status, FILE *f)
1502 const struct got_error *err = NULL;
1503 const char *markers[3] = {
1504 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1505 GOT_DIFF_CONFLICT_MARKER_SEP,
1506 GOT_DIFF_CONFLICT_MARKER_END
1510 size_t linesize = 0;
1513 while (*status == GOT_STATUS_MODIFY) {
1514 linelen = getline(&line, &linesize, f);
1515 if (linelen == -1) {
1518 err = got_ferror(f, GOT_ERR_IO);
1522 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1523 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1525 *status = GOT_STATUS_CONFLICT;
1536 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1538 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1539 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1543 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1545 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1546 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1547 ie->mtime_sec == sb->st_mtim.tv_sec &&
1548 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1549 ie->size == (sb->st_size & 0xffffffff) &&
1550 !xbit_differs(ie, sb->st_mode));
1553 static unsigned char
1554 get_staged_status(struct got_fileindex_entry *ie)
1556 switch (got_fileindex_entry_stage_get(ie)) {
1557 case GOT_FILEIDX_STAGE_ADD:
1558 return GOT_STATUS_ADD;
1559 case GOT_FILEIDX_STAGE_DELETE:
1560 return GOT_STATUS_DELETE;
1561 case GOT_FILEIDX_STAGE_MODIFY:
1562 return GOT_STATUS_MODIFY;
1564 return GOT_STATUS_NO_CHANGE;
1568 static const struct got_error *
1569 get_symlink_modification_status(unsigned char *status,
1570 struct got_fileindex_entry *ie, const char *abspath,
1571 int dirfd, const char *de_name, struct got_blob_object *blob)
1573 const struct got_error *err = NULL;
1574 char target_path[PATH_MAX];
1575 char etarget[PATH_MAX];
1577 size_t len, target_len = 0;
1578 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1581 *status = GOT_STATUS_NO_CHANGE;
1583 /* Blob object content specifies the target path of the link. */
1585 err = got_object_blob_read_block(&len, blob);
1588 if (len + target_len >= sizeof(target_path)) {
1590 * Should not happen. The blob contents were OK
1591 * when this symlink was installed.
1593 return got_error(GOT_ERR_NO_SPACE);
1596 /* Skip blob object header first time around. */
1597 memcpy(target_path + target_len, buf + hdrlen,
1599 target_len += len - hdrlen;
1603 target_path[target_len] = '\0';
1606 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1608 return got_error_from_errno2("readlinkat", abspath);
1610 elen = readlink(abspath, etarget, sizeof(etarget));
1612 return got_error_from_errno2("readlink", abspath);
1615 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1616 *status = GOT_STATUS_MODIFY;
1621 static const struct got_error *
1622 get_file_status(unsigned char *status, struct stat *sb,
1623 struct got_fileindex_entry *ie, const char *abspath,
1624 int dirfd, const char *de_name, struct got_repository *repo)
1626 const struct got_error *err = NULL;
1627 struct got_object_id id;
1629 int fd = -1, fd1 = -1;
1632 struct got_blob_object *blob = NULL;
1634 unsigned char staged_status = get_staged_status(ie);
1636 *status = GOT_STATUS_NO_CHANGE;
1637 memset(sb, 0, sizeof(*sb));
1640 * Whenever the caller provides a directory descriptor and a
1641 * directory entry name for the file, use them! This prevents
1642 * race conditions if filesystem paths change beneath our feet.
1645 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1646 if (errno == ENOENT) {
1647 if (got_fileindex_entry_has_file_on_disk(ie))
1648 *status = GOT_STATUS_MISSING;
1650 *status = GOT_STATUS_DELETE;
1653 err = got_error_from_errno2("fstatat", abspath);
1657 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1658 if (fd == -1 && errno != ENOENT &&
1659 !got_err_open_nofollow_on_symlink())
1660 return got_error_from_errno2("open", abspath);
1661 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1662 if (lstat(abspath, sb) == -1)
1663 return got_error_from_errno2("lstat", abspath);
1664 } else if (fd == -1 || fstat(fd, sb) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1669 *status = GOT_STATUS_DELETE;
1672 err = got_error_from_errno2("fstat", abspath);
1677 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1678 *status = GOT_STATUS_OBSTRUCTED;
1682 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1683 *status = GOT_STATUS_DELETE;
1685 } else if (!got_fileindex_entry_has_blob(ie) &&
1686 staged_status != GOT_STATUS_ADD) {
1687 *status = GOT_STATUS_ADD;
1691 if (!stat_info_differs(ie, sb))
1694 if (S_ISLNK(sb->st_mode) &&
1695 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1696 *status = GOT_STATUS_MODIFY;
1700 if (staged_status == GOT_STATUS_MODIFY ||
1701 staged_status == GOT_STATUS_ADD)
1702 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1704 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1706 fd1 = got_opentempfd();
1708 err = got_error_from_errno("got_opentempfd");
1711 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1715 if (S_ISLNK(sb->st_mode)) {
1716 err = get_symlink_modification_status(status, ie,
1717 abspath, dirfd, de_name, blob);
1722 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1724 err = got_error_from_errno2("openat", abspath);
1729 f = fdopen(fd, "r");
1731 err = got_error_from_errno2("fdopen", abspath);
1735 hdrlen = got_object_blob_get_hdrlen(blob);
1737 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1738 err = got_object_blob_read_block(&blen, blob);
1741 /* Skip length of blob object header first time around. */
1742 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1743 if (flen == 0 && ferror(f)) {
1744 err = got_error_from_errno("fread");
1747 if (blen - hdrlen == 0) {
1749 *status = GOT_STATUS_MODIFY;
1751 } else if (flen == 0) {
1752 if (blen - hdrlen != 0)
1753 *status = GOT_STATUS_MODIFY;
1755 } else if (blen - hdrlen == flen) {
1756 /* Skip blob object header first time around. */
1757 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1758 *status = GOT_STATUS_MODIFY;
1762 *status = GOT_STATUS_MODIFY;
1768 if (*status == GOT_STATUS_MODIFY) {
1770 err = get_modified_file_content_status(status, f);
1771 } else if (xbit_differs(ie, sb->st_mode))
1772 *status = GOT_STATUS_MODE_CHANGE;
1774 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1775 err = got_error_from_errno("close");
1777 got_object_blob_close(blob);
1778 if (f != NULL && fclose(f) == EOF && err == NULL)
1779 err = got_error_from_errno2("fclose", abspath);
1780 if (fd != -1 && close(fd) == -1 && err == NULL)
1781 err = got_error_from_errno2("close", abspath);
1786 * Update timestamps in the file index if a file is unmodified and
1787 * we had to run a full content comparison to find out.
1789 static const struct got_error *
1790 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1791 struct got_fileindex_entry *ie, struct stat *sb)
1793 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1794 return got_fileindex_entry_update(ie, wt_fd, path,
1795 ie->blob_sha1, ie->commit_sha1, 1);
1800 static const struct got_error *
1801 update_blob(struct got_worktree *worktree,
1802 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1803 struct got_tree_entry *te, const char *path,
1804 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1807 const struct got_error *err = NULL;
1808 struct got_blob_object *blob = NULL;
1809 char *ondisk_path = NULL;
1810 unsigned char status = GOT_STATUS_NO_CHANGE;
1812 int fd1 = -1, fd2 = -1;
1814 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1815 return got_error_from_errno("asprintf");
1818 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1819 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1822 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1826 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1827 sb.st_mode = got_fileindex_perms_to_st(ie);
1829 if (stat(ondisk_path, &sb) == -1) {
1830 if (errno != ENOENT) {
1831 err = got_error_from_errno2("stat",
1835 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1836 status = GOT_STATUS_UNVERSIONED;
1838 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1839 status = GOT_STATUS_UNVERSIONED;
1841 status = GOT_STATUS_OBSTRUCTED;
1845 if (status == GOT_STATUS_OBSTRUCTED) {
1847 got_fileindex_entry_mark_skipped(ie);
1848 err = (*progress_cb)(progress_arg, status, path);
1851 if (status == GOT_STATUS_CONFLICT) {
1853 got_fileindex_entry_mark_skipped(ie);
1854 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1859 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1860 (S_ISLNK(te->mode) ||
1861 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1863 * This is a regular file or an installed bad symlink.
1864 * If the file index indicates that this file is already
1865 * up-to-date with respect to the repository we can skip
1866 * updating contents of this file.
1868 if (got_fileindex_entry_has_commit(ie) &&
1869 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1870 SHA1_DIGEST_LENGTH) == 0) {
1872 err = sync_timestamps(worktree->root_fd,
1873 path, status, ie, &sb);
1876 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1880 if (got_fileindex_entry_has_blob(ie) &&
1881 memcmp(ie->blob_sha1, te->id.sha1,
1882 SHA1_DIGEST_LENGTH) == 0) {
1883 /* Different commit but the same blob. */
1884 err = sync_timestamps(worktree->root_fd,
1885 path, status, ie, &sb);
1888 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1894 fd1 = got_opentempfd();
1896 err = got_error_from_errno("got_opentempfd");
1899 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1903 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1904 int update_timestamps;
1905 struct got_blob_object *blob2 = NULL;
1906 char *label_orig = NULL;
1907 if (got_fileindex_entry_has_blob(ie)) {
1908 fd2 = got_opentempfd();
1910 err = got_error_from_errno("got_opentempfd");
1913 struct got_object_id id2;
1914 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1915 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1920 if (got_fileindex_entry_has_commit(ie)) {
1921 char id_str[SHA1_DIGEST_STRING_LENGTH];
1922 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1923 sizeof(id_str)) == NULL) {
1924 err = got_error_path(id_str,
1925 GOT_ERR_BAD_OBJ_ID_STR);
1928 if (asprintf(&label_orig, "%s: commit %s",
1929 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1930 err = got_error_from_errno("asprintf");
1934 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1936 err = got_object_blob_read_to_str(&link_target, blob);
1939 err = merge_symlink(worktree, blob2, ondisk_path, path,
1940 label_orig, link_target, worktree->base_commit_id,
1941 repo, progress_cb, progress_arg);
1944 err = merge_blob(&update_timestamps, worktree, blob2,
1945 ondisk_path, path, sb.st_mode, label_orig, blob,
1946 worktree->base_commit_id, repo,
1947 progress_cb, progress_arg);
1950 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1951 err = got_error_from_errno("close");
1955 got_object_blob_close(blob2);
1959 * Do not update timestamps of files with local changes.
1960 * Otherwise, a future status walk would treat them as
1961 * unmodified files again.
1963 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1964 blob->id.sha1, worktree->base_commit_id->sha1,
1966 } else if (status == GOT_STATUS_MODE_CHANGE) {
1967 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1968 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1969 } else if (status == GOT_STATUS_DELETE) {
1970 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1973 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1974 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1978 int is_bad_symlink = 0;
1979 if (S_ISLNK(te->mode)) {
1980 err = install_symlink(&is_bad_symlink, worktree,
1981 ondisk_path, path, blob,
1982 status == GOT_STATUS_MISSING, 0,
1983 status == GOT_STATUS_UNVERSIONED, 0,
1984 repo, progress_cb, progress_arg);
1986 err = install_blob(worktree, ondisk_path, path,
1987 te->mode, sb.st_mode, blob,
1988 status == GOT_STATUS_MISSING, 0, 0,
1989 status == GOT_STATUS_UNVERSIONED, repo,
1990 progress_cb, progress_arg);
1996 err = got_fileindex_entry_update(ie,
1997 worktree->root_fd, path, blob->id.sha1,
1998 worktree->base_commit_id->sha1, 1);
2000 err = create_fileindex_entry(&ie, fileindex,
2001 worktree->base_commit_id, worktree->root_fd, path,
2007 if (is_bad_symlink) {
2008 got_fileindex_entry_filetype_set(ie,
2009 GOT_FILEIDX_MODE_BAD_SYMLINK);
2013 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2014 err = got_error_from_errno("close");
2017 got_object_blob_close(blob);
2023 static const struct got_error *
2024 remove_ondisk_file(const char *root_path, const char *path)
2026 const struct got_error *err = NULL;
2027 char *ondisk_path = NULL, *parent = NULL;
2029 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2030 return got_error_from_errno("asprintf");
2032 if (unlink(ondisk_path) == -1) {
2033 if (errno != ENOENT)
2034 err = got_error_from_errno2("unlink", ondisk_path);
2036 size_t root_len = strlen(root_path);
2037 err = got_path_dirname(&parent, ondisk_path);
2040 while (got_path_cmp(parent, root_path,
2041 strlen(parent), root_len) != 0) {
2043 ondisk_path = parent;
2045 if (rmdir(ondisk_path) == -1) {
2046 if (errno != ENOTEMPTY)
2047 err = got_error_from_errno2("rmdir",
2051 err = got_path_dirname(&parent, ondisk_path);
2062 static const struct got_error *
2063 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2064 struct got_fileindex_entry *ie, struct got_repository *repo,
2065 got_worktree_checkout_cb progress_cb, void *progress_arg)
2067 const struct got_error *err = NULL;
2068 unsigned char status;
2072 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2073 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2075 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2077 return got_error_from_errno("asprintf");
2079 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2083 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2084 char ondisk_target[PATH_MAX];
2085 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2086 sizeof(ondisk_target));
2087 if (ondisk_len == -1) {
2088 err = got_error_from_errno2("readlink", ondisk_path);
2091 ondisk_target[ondisk_len] = '\0';
2092 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2093 NULL, NULL, /* XXX pass common ancestor info? */
2094 ondisk_target, ondisk_path);
2097 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2102 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2103 status == GOT_STATUS_ADD) {
2104 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2108 * Preserve the working file and change the deleted blob's
2109 * entry into a schedule-add entry.
2111 err = got_fileindex_entry_update(ie, worktree->root_fd,
2112 ie->path, NULL, NULL, 0);
2114 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2117 if (status == GOT_STATUS_NO_CHANGE) {
2118 err = remove_ondisk_file(worktree->root_path, ie->path);
2122 got_fileindex_entry_remove(fileindex, ie);
2129 struct diff_cb_arg {
2130 struct got_fileindex *fileindex;
2131 struct got_worktree *worktree;
2132 struct got_repository *repo;
2133 got_worktree_checkout_cb progress_cb;
2135 got_cancel_cb cancel_cb;
2139 static const struct got_error *
2140 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2141 struct got_tree_entry *te, const char *parent_path)
2143 struct diff_cb_arg *a = arg;
2145 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2146 return got_error(GOT_ERR_CANCELLED);
2148 return update_blob(a->worktree, a->fileindex, ie, te,
2149 ie->path, a->repo, a->progress_cb, a->progress_arg);
2152 static const struct got_error *
2153 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2155 struct diff_cb_arg *a = arg;
2157 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2158 return got_error(GOT_ERR_CANCELLED);
2160 return delete_blob(a->worktree, a->fileindex, ie,
2161 a->repo, a->progress_cb, a->progress_arg);
2164 static const struct got_error *
2165 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2167 struct diff_cb_arg *a = arg;
2168 const struct got_error *err;
2171 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2172 return got_error(GOT_ERR_CANCELLED);
2174 if (got_object_tree_entry_is_submodule(te))
2177 if (asprintf(&path, "%s%s%s", parent_path,
2178 parent_path[0] ? "/" : "", te->name)
2180 return got_error_from_errno("asprintf");
2182 if (S_ISDIR(te->mode))
2183 err = add_dir_on_disk(a->worktree, path);
2185 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2186 a->repo, a->progress_cb, a->progress_arg);
2192 const struct got_error *
2193 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2195 uint32_t uuid_status;
2197 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2198 if (uuid_status != uuid_s_ok) {
2200 return got_error_uuid(uuid_status, "uuid_to_string");
2206 static const struct got_error *
2207 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2209 const struct got_error *err = NULL;
2210 char *uuidstr = NULL;
2214 err = got_worktree_get_uuid(&uuidstr, worktree);
2218 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2219 err = got_error_from_errno("asprintf");
2226 const struct got_error *
2227 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2229 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2232 static const struct got_error *
2233 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2235 return get_ref_name(refname, worktree,
2236 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2239 static const struct got_error *
2240 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2242 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2245 static const struct got_error *
2246 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2248 return get_ref_name(refname, worktree,
2249 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2252 static const struct got_error *
2253 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree,
2256 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2259 static const struct got_error *
2260 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2262 return get_ref_name(refname, worktree,
2263 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2266 static const struct got_error *
2267 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2269 return get_ref_name(refname, worktree,
2270 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2273 static const struct got_error *
2274 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2276 return get_ref_name(refname, worktree,
2277 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2280 static const struct got_error *
2281 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2283 return get_ref_name(refname, worktree,
2284 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2287 const struct got_error *
2288 got_worktree_get_histedit_script_path(char **path,
2289 struct got_worktree *worktree)
2291 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2292 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2294 return got_error_from_errno("asprintf");
2299 static const struct got_error *
2300 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2302 return get_ref_name(refname, worktree,
2303 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2306 static const struct got_error *
2307 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 return get_ref_name(refname, worktree,
2310 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2314 * Prevent Git's garbage collector from deleting our base commit by
2315 * setting a reference to our base commit's ID.
2317 static const struct got_error *
2318 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2320 const struct got_error *err = NULL;
2321 struct got_reference *ref = NULL;
2324 err = got_worktree_get_base_ref_name(&refname, worktree);
2328 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2332 err = got_ref_write(ref, repo);
2340 static const struct got_error *
2341 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2343 const struct got_error *err = NULL;
2345 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2346 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2347 err = got_error_from_errno("asprintf");
2348 *fileindex_path = NULL;
2354 static const struct got_error *
2355 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2356 struct got_worktree *worktree)
2358 const struct got_error *err = NULL;
2361 *fileindex_path = NULL;
2362 *fileindex = got_fileindex_alloc();
2363 if (*fileindex == NULL)
2364 return got_error_from_errno("got_fileindex_alloc");
2366 err = get_fileindex_path(fileindex_path, worktree);
2370 index = fopen(*fileindex_path, "rbe");
2371 if (index == NULL) {
2372 if (errno != ENOENT)
2373 err = got_error_from_errno2("fopen", *fileindex_path);
2375 err = got_fileindex_read(*fileindex, index);
2376 if (fclose(index) == EOF && err == NULL)
2377 err = got_error_from_errno("fclose");
2381 free(*fileindex_path);
2382 *fileindex_path = NULL;
2383 got_fileindex_free(*fileindex);
2389 struct bump_base_commit_id_arg {
2390 struct got_object_id *base_commit_id;
2393 const char *entry_name;
2394 got_worktree_checkout_cb progress_cb;
2398 /* Bump base commit ID of all files within an updated part of the work tree. */
2399 static const struct got_error *
2400 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2402 const struct got_error *err;
2403 struct bump_base_commit_id_arg *a = arg;
2405 if (a->entry_name) {
2406 if (strcmp(ie->path, a->path) != 0)
2408 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2411 if (got_fileindex_entry_was_skipped(ie))
2414 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2415 SHA1_DIGEST_LENGTH) == 0)
2418 if (a->progress_cb) {
2419 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2424 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2428 static const struct got_error *
2429 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2430 struct got_fileindex *fileindex,
2431 got_worktree_checkout_cb progress_cb, void *progress_arg)
2433 struct bump_base_commit_id_arg bbc_arg;
2435 bbc_arg.base_commit_id = worktree->base_commit_id;
2436 bbc_arg.entry_name = NULL;
2438 bbc_arg.path_len = 0;
2439 bbc_arg.progress_cb = progress_cb;
2440 bbc_arg.progress_arg = progress_arg;
2442 return got_fileindex_for_each_entry_safe(fileindex,
2443 bump_base_commit_id, &bbc_arg);
2446 static const struct got_error *
2447 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2449 const struct got_error *err = NULL;
2450 char *new_fileindex_path = NULL;
2451 FILE *new_index = NULL;
2452 struct timespec timeout;
2454 err = got_opentemp_named(&new_fileindex_path, &new_index,
2459 err = got_fileindex_write(fileindex, new_index);
2463 if (rename(new_fileindex_path, fileindex_path) != 0) {
2464 err = got_error_from_errno3("rename", new_fileindex_path,
2466 unlink(new_fileindex_path);
2470 * Sleep for a short amount of time to ensure that files modified after
2471 * this program exits have a different time stamp from the one which
2472 * was recorded in the file index.
2475 timeout.tv_nsec = 1;
2476 nanosleep(&timeout, NULL);
2480 free(new_fileindex_path);
2484 static const struct got_error *
2485 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2486 struct got_object_id **tree_id, const char *wt_relpath,
2487 struct got_commit_object *base_commit, struct got_worktree *worktree,
2488 struct got_repository *repo)
2490 const struct got_error *err = NULL;
2491 struct got_object_id *id = NULL;
2492 char *in_repo_path = NULL;
2493 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2495 *entry_type = GOT_OBJ_TYPE_ANY;
2496 *tree_relpath = NULL;
2499 if (wt_relpath[0] == '\0') {
2500 /* Check out all files within the work tree. */
2501 *entry_type = GOT_OBJ_TYPE_TREE;
2502 *tree_relpath = strdup("");
2503 if (*tree_relpath == NULL) {
2504 err = got_error_from_errno("strdup");
2507 err = got_object_id_by_path(tree_id, repo, base_commit,
2508 worktree->path_prefix);
2514 /* Check out a subset of files in the work tree. */
2516 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2517 is_root_wt ? "" : "/", wt_relpath) == -1) {
2518 err = got_error_from_errno("asprintf");
2522 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2527 in_repo_path = NULL;
2529 err = got_object_get_type(entry_type, repo, id);
2533 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2534 /* Check out a single file. */
2535 if (strchr(wt_relpath, '/') == NULL) {
2536 /* Check out a single file in work tree's root dir. */
2537 in_repo_path = strdup(worktree->path_prefix);
2538 if (in_repo_path == NULL) {
2539 err = got_error_from_errno("strdup");
2542 *tree_relpath = strdup("");
2543 if (*tree_relpath == NULL) {
2544 err = got_error_from_errno("strdup");
2548 /* Check out a single file in a subdirectory. */
2549 err = got_path_dirname(tree_relpath, wt_relpath);
2552 if (asprintf(&in_repo_path, "%s%s%s",
2553 worktree->path_prefix, is_root_wt ? "" : "/",
2554 *tree_relpath) == -1) {
2555 err = got_error_from_errno("asprintf");
2559 err = got_object_id_by_path(tree_id, repo,
2560 base_commit, in_repo_path);
2562 /* Check out all files within a subdirectory. */
2563 *tree_id = got_object_id_dup(id);
2564 if (*tree_id == NULL) {
2565 err = got_error_from_errno("got_object_id_dup");
2568 *tree_relpath = strdup(wt_relpath);
2569 if (*tree_relpath == NULL) {
2570 err = got_error_from_errno("strdup");
2578 *entry_type = GOT_OBJ_TYPE_ANY;
2579 free(*tree_relpath);
2580 *tree_relpath = NULL;
2587 static const struct got_error *
2588 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2589 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2590 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2591 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2593 const struct got_error *err = NULL;
2594 struct got_commit_object *commit = NULL;
2595 struct got_tree_object *tree = NULL;
2596 struct got_fileindex_diff_tree_cb diff_cb;
2597 struct diff_cb_arg arg;
2599 err = ref_base_commit(worktree, repo);
2601 if (!(err->code == GOT_ERR_ERRNO &&
2602 (errno == EACCES || errno == EROFS)))
2604 err = (*progress_cb)(progress_arg,
2605 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2610 err = got_object_open_as_commit(&commit, repo,
2611 worktree->base_commit_id);
2615 err = got_object_open_as_tree(&tree, repo, tree_id);
2620 got_object_tree_find_entry(tree, entry_name) == NULL) {
2621 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2625 diff_cb.diff_old_new = diff_old_new;
2626 diff_cb.diff_old = diff_old;
2627 diff_cb.diff_new = diff_new;
2628 arg.fileindex = fileindex;
2629 arg.worktree = worktree;
2631 arg.progress_cb = progress_cb;
2632 arg.progress_arg = progress_arg;
2633 arg.cancel_cb = cancel_cb;
2634 arg.cancel_arg = cancel_arg;
2635 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2636 entry_name, repo, &diff_cb, &arg);
2639 got_object_tree_close(tree);
2641 got_object_commit_close(commit);
2645 const struct got_error *
2646 got_worktree_checkout_files(struct got_worktree *worktree,
2647 struct got_pathlist_head *paths, struct got_repository *repo,
2648 got_worktree_checkout_cb progress_cb, void *progress_arg,
2649 got_cancel_cb cancel_cb, void *cancel_arg)
2651 const struct got_error *err = NULL, *sync_err, *unlockerr;
2652 struct got_commit_object *commit = NULL;
2653 struct got_tree_object *tree = NULL;
2654 struct got_fileindex *fileindex = NULL;
2655 char *fileindex_path = NULL;
2656 struct got_pathlist_entry *pe;
2657 struct tree_path_data {
2658 STAILQ_ENTRY(tree_path_data) entry;
2659 struct got_object_id *tree_id;
2664 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2666 STAILQ_INIT(&tree_paths);
2668 err = lock_worktree(worktree, LOCK_EX);
2672 err = got_object_open_as_commit(&commit, repo,
2673 worktree->base_commit_id);
2677 /* Map all specified paths to in-repository trees. */
2678 TAILQ_FOREACH(pe, paths, entry) {
2679 tpd = malloc(sizeof(*tpd));
2681 err = got_error_from_errno("malloc");
2685 err = find_tree_entry_for_checkout(&tpd->entry_type,
2686 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2693 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2694 err = got_path_basename(&tpd->entry_name, pe->path);
2702 tpd->entry_name = NULL;
2704 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2708 * Read the file index.
2709 * Checking out files is supposed to be an idempotent operation.
2710 * If the on-disk file index is incomplete we will try to complete it.
2712 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2716 tpd = STAILQ_FIRST(&tree_paths);
2717 TAILQ_FOREACH(pe, paths, entry) {
2718 struct bump_base_commit_id_arg bbc_arg;
2720 err = checkout_files(worktree, fileindex, tpd->relpath,
2721 tpd->tree_id, tpd->entry_name, repo,
2722 progress_cb, progress_arg, cancel_cb, cancel_arg);
2726 bbc_arg.base_commit_id = worktree->base_commit_id;
2727 bbc_arg.entry_name = tpd->entry_name;
2728 bbc_arg.path = pe->path;
2729 bbc_arg.path_len = pe->path_len;
2730 bbc_arg.progress_cb = progress_cb;
2731 bbc_arg.progress_arg = progress_arg;
2732 err = got_fileindex_for_each_entry_safe(fileindex,
2733 bump_base_commit_id, &bbc_arg);
2737 tpd = STAILQ_NEXT(tpd, entry);
2739 sync_err = sync_fileindex(fileindex, fileindex_path);
2740 if (sync_err && err == NULL)
2743 free(fileindex_path);
2745 got_object_tree_close(tree);
2747 got_object_commit_close(commit);
2749 got_fileindex_free(fileindex);
2750 while (!STAILQ_EMPTY(&tree_paths)) {
2751 tpd = STAILQ_FIRST(&tree_paths);
2752 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2757 unlockerr = lock_worktree(worktree, LOCK_SH);
2758 if (unlockerr && err == NULL)
2763 struct merge_file_cb_arg {
2764 struct got_worktree *worktree;
2765 struct got_fileindex *fileindex;
2766 got_worktree_checkout_cb progress_cb;
2768 got_cancel_cb cancel_cb;
2770 const char *label_orig;
2771 struct got_object_id *commit_id2;
2772 int allow_bad_symlinks;
2775 static const struct got_error *
2776 merge_file_cb(void *arg, struct got_blob_object *blob1,
2777 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2778 struct got_object_id *id1, struct got_object_id *id2,
2779 const char *path1, const char *path2,
2780 mode_t mode1, mode_t mode2, struct got_repository *repo)
2782 static const struct got_error *err = NULL;
2783 struct merge_file_cb_arg *a = arg;
2784 struct got_fileindex_entry *ie;
2785 char *ondisk_path = NULL;
2787 unsigned char status;
2788 int local_changes_subsumed;
2789 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2790 char *id_str = NULL, *label_deriv2 = NULL;
2792 if (blob1 && blob2) {
2793 ie = got_fileindex_entry_get(a->fileindex, path2,
2796 return (*a->progress_cb)(a->progress_arg,
2797 GOT_STATUS_MISSING, path2);
2799 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2801 return got_error_from_errno("asprintf");
2803 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2808 if (status == GOT_STATUS_DELETE) {
2809 err = (*a->progress_cb)(a->progress_arg,
2810 GOT_STATUS_MERGE, path2);
2813 if (status != GOT_STATUS_NO_CHANGE &&
2814 status != GOT_STATUS_MODIFY &&
2815 status != GOT_STATUS_CONFLICT &&
2816 status != GOT_STATUS_ADD) {
2817 err = (*a->progress_cb)(a->progress_arg, status, path2);
2821 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2823 err = got_object_blob_read_to_str(&link_target2, blob2);
2826 err = merge_symlink(a->worktree, blob1, ondisk_path,
2827 path2, a->label_orig, link_target2, a->commit_id2,
2828 repo, a->progress_cb, a->progress_arg);
2833 f_orig = got_opentemp();
2834 if (f_orig == NULL) {
2835 err = got_error_from_errno("got_opentemp");
2838 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2843 f_deriv2 = got_opentemp();
2844 if (f_deriv2 == NULL)
2846 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2851 fd = open(ondisk_path,
2852 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2854 err = got_error_from_errno2("open",
2858 f_deriv = fdopen(fd, "r");
2859 if (f_deriv == NULL) {
2860 err = got_error_from_errno2("fdopen",
2865 err = got_object_id_str(&id_str, a->commit_id2);
2868 if (asprintf(&label_deriv2, "%s: commit %s",
2869 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2870 err = got_error_from_errno("asprintf");
2873 err = merge_file(&local_changes_subsumed, a->worktree,
2874 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2875 sb.st_mode, a->label_orig, NULL, label_deriv2,
2876 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2877 a->progress_cb, a->progress_arg);
2880 ie = got_fileindex_entry_get(a->fileindex, path1,
2883 return (*a->progress_cb)(a->progress_arg,
2884 GOT_STATUS_MISSING, path1);
2886 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2888 return got_error_from_errno("asprintf");
2890 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2896 case GOT_STATUS_NO_CHANGE:
2897 err = (*a->progress_cb)(a->progress_arg,
2898 GOT_STATUS_DELETE, path1);
2901 err = remove_ondisk_file(a->worktree->root_path, path1);
2905 got_fileindex_entry_mark_deleted_from_disk(ie);
2907 case GOT_STATUS_DELETE:
2908 case GOT_STATUS_MISSING:
2909 err = (*a->progress_cb)(a->progress_arg,
2910 GOT_STATUS_DELETE, path1);
2914 got_fileindex_entry_mark_deleted_from_disk(ie);
2916 case GOT_STATUS_ADD: {
2917 struct got_object_id *id;
2921 * Delete the added file only if its content already
2922 * exists in the repository.
2924 err = got_object_blob_file_create(&id, &blob1_f,
2925 &blob1_size, path1);
2928 if (got_object_id_cmp(id, id1) == 0) {
2929 err = (*a->progress_cb)(a->progress_arg,
2930 GOT_STATUS_DELETE, path1);
2933 err = remove_ondisk_file(a->worktree->root_path,
2938 got_fileindex_entry_remove(a->fileindex,
2941 err = (*a->progress_cb)(a->progress_arg,
2942 GOT_STATUS_CANNOT_DELETE, path1);
2944 if (fclose(blob1_f) == EOF && err == NULL)
2945 err = got_error_from_errno("fclose");
2951 case GOT_STATUS_MODIFY:
2952 case GOT_STATUS_CONFLICT:
2953 err = (*a->progress_cb)(a->progress_arg,
2954 GOT_STATUS_CANNOT_DELETE, path1);
2958 case GOT_STATUS_OBSTRUCTED:
2959 err = (*a->progress_cb)(a->progress_arg, status, path1);
2967 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2969 return got_error_from_errno("asprintf");
2970 ie = got_fileindex_entry_get(a->fileindex, path2,
2973 err = get_file_status(&status, &sb, ie, ondisk_path,
2977 if (status != GOT_STATUS_NO_CHANGE &&
2978 status != GOT_STATUS_MODIFY &&
2979 status != GOT_STATUS_CONFLICT &&
2980 status != GOT_STATUS_ADD) {
2981 err = (*a->progress_cb)(a->progress_arg,
2985 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2987 err = got_object_blob_read_to_str(&link_target2,
2991 err = merge_symlink(a->worktree, NULL,
2992 ondisk_path, path2, a->label_orig,
2993 link_target2, a->commit_id2, repo,
2994 a->progress_cb, a->progress_arg);
2996 } else if (S_ISREG(sb.st_mode)) {
2997 err = merge_blob(&local_changes_subsumed,
2998 a->worktree, NULL, ondisk_path, path2,
2999 sb.st_mode, a->label_orig, blob2,
3000 a->commit_id2, repo, a->progress_cb,
3003 err = got_error_path(ondisk_path,
3004 GOT_ERR_FILE_OBSTRUCTED);
3008 if (status == GOT_STATUS_DELETE) {
3009 err = got_fileindex_entry_update(ie,
3010 a->worktree->root_fd, path2, blob2->id.sha1,
3011 a->worktree->base_commit_id->sha1, 0);
3016 int is_bad_symlink = 0;
3017 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3018 if (S_ISLNK(mode2)) {
3019 err = install_symlink(&is_bad_symlink,
3020 a->worktree, ondisk_path, path2, blob2, 0,
3021 0, 1, a->allow_bad_symlinks, repo,
3022 a->progress_cb, a->progress_arg);
3024 err = install_blob(a->worktree, ondisk_path, path2,
3025 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3026 a->progress_cb, a->progress_arg);
3030 err = got_fileindex_entry_alloc(&ie, path2);
3033 err = got_fileindex_entry_update(ie,
3034 a->worktree->root_fd, path2, NULL, NULL, 1);
3036 got_fileindex_entry_free(ie);
3039 err = got_fileindex_entry_add(a->fileindex, ie);
3041 got_fileindex_entry_free(ie);
3044 if (is_bad_symlink) {
3045 got_fileindex_entry_filetype_set(ie,
3046 GOT_FILEIDX_MODE_BAD_SYMLINK);
3051 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3052 err = got_error_from_errno("fclose");
3053 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3054 err = got_error_from_errno("fclose");
3055 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3056 err = got_error_from_errno("fclose");
3063 static const struct got_error *
3064 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3066 struct got_worktree *worktree = arg;
3068 /* Reject merges into a work tree with mixed base commits. */
3069 if (got_fileindex_entry_has_commit(ie) &&
3070 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3071 SHA1_DIGEST_LENGTH) != 0)
3072 return got_error(GOT_ERR_MIXED_COMMITS);
3077 struct check_merge_conflicts_arg {
3078 struct got_worktree *worktree;
3079 struct got_fileindex *fileindex;
3080 struct got_repository *repo;
3083 static const struct got_error *
3084 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3085 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3086 struct got_object_id *id1, struct got_object_id *id2,
3087 const char *path1, const char *path2,
3088 mode_t mode1, mode_t mode2, struct got_repository *repo)
3090 const struct got_error *err = NULL;
3091 struct check_merge_conflicts_arg *a = arg;
3092 unsigned char status;
3094 struct got_fileindex_entry *ie;
3095 const char *path = path2 ? path2 : path1;
3096 struct got_object_id *id = id2 ? id2 : id1;
3102 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3106 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3108 return got_error_from_errno("asprintf");
3110 /* Reject merges into a work tree with conflicted files. */
3111 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3115 if (status == GOT_STATUS_CONFLICT)
3116 return got_error(GOT_ERR_CONFLICTS);
3121 static const struct got_error *
3122 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3123 const char *fileindex_path, struct got_object_id *commit_id1,
3124 struct got_object_id *commit_id2, struct got_repository *repo,
3125 got_worktree_checkout_cb progress_cb, void *progress_arg,
3126 got_cancel_cb cancel_cb, void *cancel_arg)
3128 const struct got_error *err = NULL, *sync_err;
3129 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3130 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3131 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3132 struct check_merge_conflicts_arg cmc_arg;
3133 struct merge_file_cb_arg arg;
3134 char *label_orig = NULL;
3135 FILE *f1 = NULL, *f2 = NULL;
3138 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3141 err = got_object_id_by_path(&tree_id1, repo, commit1,
3142 worktree->path_prefix);
3143 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3149 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3153 err = got_object_id_str(&id_str, commit_id1);
3157 if (asprintf(&label_orig, "%s: commit %s",
3158 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3159 err = got_error_from_errno("asprintf");
3165 f1 = got_opentemp();
3167 err = got_error_from_errno("got_opentemp");
3172 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3176 err = got_object_id_by_path(&tree_id2, repo, commit2,
3177 worktree->path_prefix);
3181 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3185 f2 = got_opentemp();
3187 err = got_error_from_errno("got_opentemp");
3191 cmc_arg.worktree = worktree;
3192 cmc_arg.fileindex = fileindex;
3193 cmc_arg.repo = repo;
3194 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3195 check_merge_conflicts, &cmc_arg, 0);
3199 arg.worktree = worktree;
3200 arg.fileindex = fileindex;
3201 arg.progress_cb = progress_cb;
3202 arg.progress_arg = progress_arg;
3203 arg.cancel_cb = cancel_cb;
3204 arg.cancel_arg = cancel_arg;
3205 arg.label_orig = label_orig;
3206 arg.commit_id2 = commit_id2;
3207 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3208 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3209 merge_file_cb, &arg, 1);
3210 sync_err = sync_fileindex(fileindex, fileindex_path);
3211 if (sync_err && err == NULL)
3215 got_object_commit_close(commit1);
3217 got_object_commit_close(commit2);
3219 got_object_tree_close(tree1);
3221 got_object_tree_close(tree2);
3222 if (f1 && fclose(f1) == EOF && err == NULL)
3223 err = got_error_from_errno("fclose");
3224 if (f2 && fclose(f2) == EOF && err == NULL)
3225 err = got_error_from_errno("fclose");
3230 const struct got_error *
3231 got_worktree_merge_files(struct got_worktree *worktree,
3232 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3233 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3234 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3236 const struct got_error *err, *unlockerr;
3237 char *fileindex_path = NULL;
3238 struct got_fileindex *fileindex = NULL;
3240 err = lock_worktree(worktree, LOCK_EX);
3244 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3248 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3253 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3254 commit_id2, repo, progress_cb, progress_arg,
3255 cancel_cb, cancel_arg);
3258 got_fileindex_free(fileindex);
3259 free(fileindex_path);
3260 unlockerr = lock_worktree(worktree, LOCK_SH);
3261 if (unlockerr && err == NULL)
3266 struct diff_dir_cb_arg {
3267 struct got_fileindex *fileindex;
3268 struct got_worktree *worktree;
3269 const char *status_path;
3270 size_t status_path_len;
3271 struct got_repository *repo;
3272 got_worktree_status_cb status_cb;
3274 got_cancel_cb cancel_cb;
3276 /* A pathlist containing per-directory pathlists of ignore patterns. */
3277 struct got_pathlist_head *ignores;
3278 int report_unchanged;
3282 static const struct got_error *
3283 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3284 int dirfd, const char *de_name,
3285 got_worktree_status_cb status_cb, void *status_arg,
3286 struct got_repository *repo, int report_unchanged)
3288 const struct got_error *err = NULL;
3289 unsigned char status = GOT_STATUS_NO_CHANGE;
3290 unsigned char staged_status = get_staged_status(ie);
3292 struct got_object_id blob_id, commit_id, staged_blob_id;
3293 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3294 struct got_object_id *staged_blob_idp = NULL;
3296 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3300 if (status == GOT_STATUS_NO_CHANGE &&
3301 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3304 if (got_fileindex_entry_has_blob(ie)) {
3305 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3306 blob_idp = &blob_id;
3308 if (got_fileindex_entry_has_commit(ie)) {
3309 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3310 commit_idp = &commit_id;
3312 if (staged_status == GOT_STATUS_ADD ||
3313 staged_status == GOT_STATUS_MODIFY) {
3314 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3315 SHA1_DIGEST_LENGTH);
3316 staged_blob_idp = &staged_blob_id;
3319 return (*status_cb)(status_arg, status, staged_status,
3320 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3323 static const struct got_error *
3324 status_old_new(void *arg, struct got_fileindex_entry *ie,
3325 struct dirent *de, const char *parent_path, int dirfd)
3327 const struct got_error *err = NULL;
3328 struct diff_dir_cb_arg *a = arg;
3331 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3332 return got_error(GOT_ERR_CANCELLED);
3334 if (got_path_cmp(parent_path, a->status_path,
3335 strlen(parent_path), a->status_path_len) != 0 &&
3336 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3339 if (parent_path[0]) {
3340 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3341 parent_path, de->d_name) == -1)
3342 return got_error_from_errno("asprintf");
3344 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3346 return got_error_from_errno("asprintf");
3349 err = report_file_status(ie, abspath, dirfd, de->d_name,
3350 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3355 static const struct got_error *
3356 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3358 struct diff_dir_cb_arg *a = arg;
3359 struct got_object_id blob_id, commit_id;
3360 unsigned char status;
3362 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3363 return got_error(GOT_ERR_CANCELLED);
3365 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3368 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3369 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3370 if (got_fileindex_entry_has_file_on_disk(ie))
3371 status = GOT_STATUS_MISSING;
3373 status = GOT_STATUS_DELETE;
3374 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3375 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3379 free_ignorelist(struct got_pathlist_head *ignorelist)
3381 struct got_pathlist_entry *pe;
3383 TAILQ_FOREACH(pe, ignorelist, entry)
3384 free((char *)pe->path);
3385 got_pathlist_free(ignorelist);
3389 free_ignores(struct got_pathlist_head *ignores)
3391 struct got_pathlist_entry *pe;
3393 TAILQ_FOREACH(pe, ignores, entry) {
3394 struct got_pathlist_head *ignorelist = pe->data;
3395 free_ignorelist(ignorelist);
3396 free((char *)pe->path);
3398 got_pathlist_free(ignores);
3401 static const struct got_error *
3402 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3404 const struct got_error *err = NULL;
3405 struct got_pathlist_entry *pe = NULL;
3406 struct got_pathlist_head *ignorelist;
3407 char *line = NULL, *pattern, *dirpath = NULL;
3408 size_t linesize = 0;
3411 ignorelist = calloc(1, sizeof(*ignorelist));
3412 if (ignorelist == NULL)
3413 return got_error_from_errno("calloc");
3414 TAILQ_INIT(ignorelist);
3416 while ((linelen = getline(&line, &linesize, f)) != -1) {
3417 if (linelen > 0 && line[linelen - 1] == '\n')
3418 line[linelen - 1] = '\0';
3420 /* Git's ignores may contain comments. */
3424 /* Git's negated patterns are not (yet?) supported. */
3428 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3430 err = got_error_from_errno("asprintf");
3433 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3438 err = got_error_from_errno("getline");
3442 dirpath = strdup(path);
3443 if (dirpath == NULL) {
3444 err = got_error_from_errno("strdup");
3447 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3450 if (err || pe == NULL) {
3452 free_ignorelist(ignorelist);
3458 match_ignores(struct got_pathlist_head *ignores, const char *path)
3460 struct got_pathlist_entry *pe;
3462 /* Handle patterns which match in all directories. */
3463 TAILQ_FOREACH(pe, ignores, entry) {
3464 struct got_pathlist_head *ignorelist = pe->data;
3465 struct got_pathlist_entry *pi;
3467 TAILQ_FOREACH(pi, ignorelist, entry) {
3468 const char *p, *pattern = pi->path;
3470 if (strncmp(pattern, "**/", 3) != 0)
3475 if (fnmatch(pattern, p,
3476 FNM_PATHNAME | FNM_LEADING_DIR)) {
3477 /* Retry in next directory. */
3478 while (*p && *p != '/')
3490 * The ignores pathlist contains ignore lists from children before
3491 * parents, so we can find the most specific ignorelist by walking
3492 * ignores backwards.
3494 pe = TAILQ_LAST(ignores, got_pathlist_head);
3496 if (got_path_is_child(path, pe->path, pe->path_len)) {
3497 struct got_pathlist_head *ignorelist = pe->data;
3498 struct got_pathlist_entry *pi;
3499 TAILQ_FOREACH(pi, ignorelist, entry) {
3500 const char *pattern = pi->path;
3501 int flags = FNM_LEADING_DIR;
3502 if (strstr(pattern, "/**/") == NULL)
3503 flags |= FNM_PATHNAME;
3504 if (fnmatch(pattern, path, flags))
3509 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3515 static const struct got_error *
3516 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3517 const char *path, int dirfd, const char *ignores_filename)
3519 const struct got_error *err = NULL;
3522 FILE *ignoresfile = NULL;
3524 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3525 path[0] ? "/" : "", ignores_filename) == -1)
3526 return got_error_from_errno("asprintf");
3529 fd = openat(dirfd, ignores_filename,
3530 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3532 if (errno != ENOENT && errno != EACCES)
3533 err = got_error_from_errno2("openat",
3536 ignoresfile = fdopen(fd, "r");
3537 if (ignoresfile == NULL)
3538 err = got_error_from_errno2("fdopen",
3542 err = read_ignores(ignores, path, ignoresfile);
3546 ignoresfile = fopen(ignorespath, "re");
3547 if (ignoresfile == NULL) {
3548 if (errno != ENOENT && errno != EACCES)
3549 err = got_error_from_errno2("fopen",
3552 err = read_ignores(ignores, path, ignoresfile);
3555 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3556 err = got_error_from_errno2("fclose", path);
3557 if (fd != -1 && close(fd) == -1 && err == NULL)
3558 err = got_error_from_errno2("close", path);
3563 static const struct got_error *
3564 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3567 const struct got_error *err = NULL;
3568 struct diff_dir_cb_arg *a = arg;
3574 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3575 return got_error(GOT_ERR_CANCELLED);
3577 if (parent_path[0]) {
3578 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3579 return got_error_from_errno("asprintf");
3584 if (de->d_type == DT_DIR) {
3585 if (!a->no_ignores && ignore != NULL &&
3586 match_ignores(a->ignores, path))
3588 } else if (!match_ignores(a->ignores, path) &&
3589 got_path_is_child(path, a->status_path, a->status_path_len))
3590 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3591 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3597 static const struct got_error *
3598 status_traverse(void *arg, const char *path, int dirfd)
3600 const struct got_error *err = NULL;
3601 struct diff_dir_cb_arg *a = arg;
3606 err = add_ignores(a->ignores, a->worktree->root_path,
3607 path, dirfd, ".cvsignore");
3611 err = add_ignores(a->ignores, a->worktree->root_path, path,
3612 dirfd, ".gitignore");
3617 static const struct got_error *
3618 report_single_file_status(const char *path, const char *ondisk_path,
3619 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3620 void *status_arg, struct got_repository *repo, int report_unchanged,
3621 struct got_pathlist_head *ignores, int no_ignores)
3623 struct got_fileindex_entry *ie;
3626 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3628 return report_file_status(ie, ondisk_path, -1, NULL,
3629 status_cb, status_arg, repo, report_unchanged);
3631 if (lstat(ondisk_path, &sb) == -1) {
3632 if (errno != ENOENT)
3633 return got_error_from_errno2("lstat", ondisk_path);
3634 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3635 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3638 if (!no_ignores && match_ignores(ignores, path))
3641 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3642 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3643 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3648 static const struct got_error *
3649 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3650 const char *root_path, const char *path)
3652 const struct got_error *err;
3653 char *parent_path, *next_parent_path = NULL;
3655 err = add_ignores(ignores, root_path, "", -1,
3660 err = add_ignores(ignores, root_path, "", -1,
3665 err = got_path_dirname(&parent_path, path);
3667 if (err->code == GOT_ERR_BAD_PATH)
3668 return NULL; /* cannot traverse parent */
3672 err = add_ignores(ignores, root_path, parent_path, -1,
3676 err = add_ignores(ignores, root_path, parent_path, -1,
3680 err = got_path_dirname(&next_parent_path, parent_path);
3682 if (err->code == GOT_ERR_BAD_PATH)
3683 err = NULL; /* traversed everything */
3686 if (got_path_is_root_dir(parent_path))
3689 parent_path = next_parent_path;
3690 next_parent_path = NULL;
3694 free(next_parent_path);
3698 static const struct got_error *
3699 worktree_status(struct got_worktree *worktree, const char *path,
3700 struct got_fileindex *fileindex, struct got_repository *repo,
3701 got_worktree_status_cb status_cb, void *status_arg,
3702 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3703 int report_unchanged)
3705 const struct got_error *err = NULL;
3707 struct got_fileindex_diff_dir_cb fdiff_cb;
3708 struct diff_dir_cb_arg arg;
3709 char *ondisk_path = NULL;
3710 struct got_pathlist_head ignores;
3711 struct got_fileindex_entry *ie;
3713 TAILQ_INIT(&ignores);
3715 if (asprintf(&ondisk_path, "%s%s%s",
3716 worktree->root_path, path[0] ? "/" : "", path) == -1)
3717 return got_error_from_errno("asprintf");
3719 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3721 err = report_single_file_status(path, ondisk_path,
3722 fileindex, status_cb, status_arg, repo,
3723 report_unchanged, &ignores, no_ignores);
3727 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3729 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3730 !got_err_open_nofollow_on_symlink())
3731 err = got_error_from_errno2("open", ondisk_path);
3734 err = add_ignores_from_parent_paths(&ignores,
3735 worktree->root_path, ondisk_path);
3739 err = report_single_file_status(path, ondisk_path,
3740 fileindex, status_cb, status_arg, repo,
3741 report_unchanged, &ignores, no_ignores);
3744 fdiff_cb.diff_old_new = status_old_new;
3745 fdiff_cb.diff_old = status_old;
3746 fdiff_cb.diff_new = status_new;
3747 fdiff_cb.diff_traverse = status_traverse;
3748 arg.fileindex = fileindex;
3749 arg.worktree = worktree;
3750 arg.status_path = path;
3751 arg.status_path_len = strlen(path);
3753 arg.status_cb = status_cb;
3754 arg.status_arg = status_arg;
3755 arg.cancel_cb = cancel_cb;
3756 arg.cancel_arg = cancel_arg;
3757 arg.report_unchanged = report_unchanged;
3758 arg.no_ignores = no_ignores;
3760 err = add_ignores_from_parent_paths(&ignores,
3761 worktree->root_path, path);
3765 arg.ignores = &ignores;
3766 err = got_fileindex_diff_dir(fileindex, fd,
3767 worktree->root_path, path, repo, &fdiff_cb, &arg);
3770 free_ignores(&ignores);
3771 if (fd != -1 && close(fd) == -1 && err == NULL)
3772 err = got_error_from_errno("close");
3777 const struct got_error *
3778 got_worktree_status(struct got_worktree *worktree,
3779 struct got_pathlist_head *paths, struct got_repository *repo,
3780 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3781 got_cancel_cb cancel_cb, void *cancel_arg)
3783 const struct got_error *err = NULL;
3784 char *fileindex_path = NULL;
3785 struct got_fileindex *fileindex = NULL;
3786 struct got_pathlist_entry *pe;
3788 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3792 TAILQ_FOREACH(pe, paths, entry) {
3793 err = worktree_status(worktree, pe->path, fileindex, repo,
3794 status_cb, status_arg, cancel_cb, cancel_arg,
3799 free(fileindex_path);
3800 got_fileindex_free(fileindex);
3804 const struct got_error *
3805 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3808 const struct got_error *err = NULL;
3809 char *resolved = NULL, *cwd = NULL, *path = NULL;
3812 char *abspath = NULL;
3813 char canonpath[PATH_MAX];
3817 cwd = getcwd(NULL, 0);
3819 return got_error_from_errno("getcwd");
3821 if (lstat(arg, &sb) == -1) {
3822 if (errno != ENOENT) {
3823 err = got_error_from_errno2("lstat", arg);
3828 if (S_ISLNK(sb.st_mode)) {
3830 * We cannot use realpath(3) with symlinks since we want to
3831 * operate on the symlink itself.
3832 * But we can make the path absolute, assuming it is relative
3833 * to the current working directory, and then canonicalize it.
3835 if (!got_path_is_absolute(arg)) {
3836 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3837 err = got_error_from_errno("asprintf");
3842 err = got_canonpath(abspath ? abspath : arg, canonpath,
3846 resolved = strdup(canonpath);
3847 if (resolved == NULL) {
3848 err = got_error_from_errno("strdup");
3852 resolved = realpath(arg, NULL);
3853 if (resolved == NULL) {
3854 if (errno != ENOENT) {
3855 err = got_error_from_errno2("realpath", arg);
3858 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3859 err = got_error_from_errno("asprintf");
3862 err = got_canonpath(abspath, canonpath,
3866 resolved = strdup(canonpath);
3867 if (resolved == NULL) {
3868 err = got_error_from_errno("strdup");
3874 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3875 strlen(got_worktree_get_root_path(worktree)))) {
3876 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3880 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3881 err = got_path_skip_common_ancestor(&path,
3882 got_worktree_get_root_path(worktree), resolved);
3888 err = got_error_from_errno("strdup");
3893 /* XXX status walk can't deal with trailing slash! */
3895 while (len > 0 && path[len - 1] == '/') {
3896 path[len - 1] = '\0';
3910 struct schedule_addition_args {
3911 struct got_worktree *worktree;
3912 struct got_fileindex *fileindex;
3913 got_worktree_checkout_cb progress_cb;
3915 struct got_repository *repo;
3918 static const struct got_error *
3919 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3920 const char *relpath, struct got_object_id *blob_id,
3921 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3922 int dirfd, const char *de_name)
3924 struct schedule_addition_args *a = arg;
3925 const struct got_error *err = NULL;
3926 struct got_fileindex_entry *ie;
3930 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3932 return got_error_from_errno("asprintf");
3934 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3936 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3940 /* Re-adding an existing entry is a no-op. */
3941 if (status == GOT_STATUS_ADD)
3943 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3948 if (status != GOT_STATUS_UNVERSIONED) {
3949 if (status == GOT_STATUS_NONEXISTENT)
3950 err = got_error_set_errno(ENOENT, ondisk_path);
3952 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3956 err = got_fileindex_entry_alloc(&ie, relpath);
3959 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3960 relpath, NULL, NULL, 1);
3962 got_fileindex_entry_free(ie);
3965 err = got_fileindex_entry_add(a->fileindex, ie);
3967 got_fileindex_entry_free(ie);
3974 if (status == GOT_STATUS_ADD)
3976 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3979 const struct got_error *
3980 got_worktree_schedule_add(struct got_worktree *worktree,
3981 struct got_pathlist_head *paths,
3982 got_worktree_checkout_cb progress_cb, void *progress_arg,
3983 struct got_repository *repo, int no_ignores)
3985 struct got_fileindex *fileindex = NULL;
3986 char *fileindex_path = NULL;
3987 const struct got_error *err = NULL, *sync_err, *unlockerr;
3988 struct got_pathlist_entry *pe;
3989 struct schedule_addition_args saa;
3991 err = lock_worktree(worktree, LOCK_EX);
3995 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3999 saa.worktree = worktree;
4000 saa.fileindex = fileindex;
4001 saa.progress_cb = progress_cb;
4002 saa.progress_arg = progress_arg;
4005 TAILQ_FOREACH(pe, paths, entry) {
4006 err = worktree_status(worktree, pe->path, fileindex, repo,
4007 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4011 sync_err = sync_fileindex(fileindex, fileindex_path);
4012 if (sync_err && err == NULL)
4015 free(fileindex_path);
4017 got_fileindex_free(fileindex);
4018 unlockerr = lock_worktree(worktree, LOCK_SH);
4019 if (unlockerr && err == NULL)
4024 struct schedule_deletion_args {
4025 struct got_worktree *worktree;
4026 struct got_fileindex *fileindex;
4027 got_worktree_delete_cb progress_cb;
4029 struct got_repository *repo;
4030 int delete_local_mods;
4032 int ignore_missing_paths;
4033 const char *status_codes;
4036 static const struct got_error *
4037 schedule_for_deletion(void *arg, unsigned char status,
4038 unsigned char staged_status, const char *relpath,
4039 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4040 struct got_object_id *commit_id, int dirfd, const char *de_name)
4042 struct schedule_deletion_args *a = arg;
4043 const struct got_error *err = NULL;
4044 struct got_fileindex_entry *ie = NULL;
4048 if (status == GOT_STATUS_NONEXISTENT) {
4049 if (a->ignore_missing_paths)
4051 return got_error_set_errno(ENOENT, relpath);
4054 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4056 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4058 staged_status = get_staged_status(ie);
4059 if (staged_status != GOT_STATUS_NO_CHANGE) {
4060 if (staged_status == GOT_STATUS_DELETE)
4062 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4065 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4067 return got_error_from_errno("asprintf");
4069 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4074 if (a->status_codes) {
4075 size_t ncodes = strlen(a->status_codes);
4077 for (i = 0; i < ncodes ; i++) {
4078 if (status == a->status_codes[i])
4082 /* Do not delete files in non-matching status. */
4086 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4087 a->status_codes[i] != GOT_STATUS_MISSING) {
4088 static char msg[64];
4089 snprintf(msg, sizeof(msg),
4090 "invalid status code '%c'", a->status_codes[i]);
4091 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4096 if (status != GOT_STATUS_NO_CHANGE) {
4097 if (status == GOT_STATUS_DELETE)
4099 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4100 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4103 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4104 err = got_error_set_errno(ENOENT, relpath);
4107 if (status != GOT_STATUS_MODIFY &&
4108 status != GOT_STATUS_MISSING) {
4109 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4114 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4118 if (unlinkat(dirfd, de_name, 0) != 0) {
4119 err = got_error_from_errno2("unlinkat",
4123 } else if (unlink(ondisk_path) != 0) {
4124 err = got_error_from_errno2("unlink", ondisk_path);
4128 root_len = strlen(a->worktree->root_path);
4131 err = got_path_dirname(&parent, ondisk_path);
4135 ondisk_path = parent;
4136 if (rmdir(ondisk_path) == -1) {
4137 if (errno != ENOTEMPTY)
4138 err = got_error_from_errno2("rmdir",
4142 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4143 strlen(ondisk_path), root_len) != 0);
4146 got_fileindex_entry_mark_deleted_from_disk(ie);
4151 if (status == GOT_STATUS_DELETE)
4153 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4154 staged_status, relpath);
4157 const struct got_error *
4158 got_worktree_schedule_delete(struct got_worktree *worktree,
4159 struct got_pathlist_head *paths, int delete_local_mods,
4160 const char *status_codes,
4161 got_worktree_delete_cb progress_cb, void *progress_arg,
4162 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4164 struct got_fileindex *fileindex = NULL;
4165 char *fileindex_path = NULL;
4166 const struct got_error *err = NULL, *sync_err, *unlockerr;
4167 struct got_pathlist_entry *pe;
4168 struct schedule_deletion_args sda;
4170 err = lock_worktree(worktree, LOCK_EX);
4174 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4178 sda.worktree = worktree;
4179 sda.fileindex = fileindex;
4180 sda.progress_cb = progress_cb;
4181 sda.progress_arg = progress_arg;
4183 sda.delete_local_mods = delete_local_mods;
4184 sda.keep_on_disk = keep_on_disk;
4185 sda.ignore_missing_paths = ignore_missing_paths;
4186 sda.status_codes = status_codes;
4188 TAILQ_FOREACH(pe, paths, entry) {
4189 err = worktree_status(worktree, pe->path, fileindex, repo,
4190 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4194 sync_err = sync_fileindex(fileindex, fileindex_path);
4195 if (sync_err && err == NULL)
4198 free(fileindex_path);
4200 got_fileindex_free(fileindex);
4201 unlockerr = lock_worktree(worktree, LOCK_SH);
4202 if (unlockerr && err == NULL)
4207 static const struct got_error *
4208 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4210 const struct got_error *err = NULL;
4212 size_t linesize = 0, n;
4215 linelen = getline(&line, &linesize, infile);
4216 if (linelen == -1) {
4217 if (ferror(infile)) {
4218 err = got_error_from_errno("getline");
4224 n = fwrite(line, 1, linelen, outfile);
4226 err = got_ferror(outfile, GOT_ERR_IO);
4231 n = fwrite(line, 1, linelen, rejectfile);
4233 err = got_ferror(outfile, GOT_ERR_IO);
4240 static const struct got_error *
4241 skip_one_line(FILE *f)
4244 size_t linesize = 0;
4247 linelen = getline(&line, &linesize, f);
4248 if (linelen == -1) {
4250 return got_error_from_errno("getline");
4257 static const struct got_error *
4258 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4259 int start_old, int end_old, int start_new, int end_new,
4260 FILE *outfile, FILE *rejectfile)
4262 const struct got_error *err;
4264 /* Copy old file's lines leading up to patch. */
4265 while (!feof(f1) && *line_cur1 < start_old) {
4266 err = copy_one_line(f1, outfile, NULL);
4271 /* Skip new file's lines leading up to patch. */
4272 while (!feof(f2) && *line_cur2 < start_new) {
4274 err = copy_one_line(f2, NULL, rejectfile);
4276 err = skip_one_line(f2);
4281 /* Copy patched lines. */
4282 while (!feof(f2) && *line_cur2 <= end_new) {
4283 err = copy_one_line(f2, outfile, NULL);
4288 /* Skip over old file's replaced lines. */
4289 while (!feof(f1) && *line_cur1 <= end_old) {
4291 err = copy_one_line(f1, NULL, rejectfile);
4293 err = skip_one_line(f1);
4302 static const struct got_error *
4303 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4304 FILE *outfile, FILE *rejectfile)
4306 const struct got_error *err;
4309 /* Copy old file's lines until EOF. */
4311 err = copy_one_line(f1, outfile, NULL);
4318 /* Copy new file's lines until EOF. */
4320 err = copy_one_line(f2, NULL, rejectfile);
4330 static const struct got_error *
4331 apply_or_reject_change(int *choice, int *nchunks_used,
4332 struct diff_result *diff_result, int n,
4333 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4334 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4335 got_worktree_patch_cb patch_cb, void *patch_arg)
4337 const struct got_error *err = NULL;
4338 struct diff_chunk_context cc = {};
4339 int start_old, end_old, start_new, end_new;
4341 struct diff_output_unidiff_state *diff_state;
4342 struct diff_input_info diff_info;
4345 *choice = GOT_PATCH_CHOICE_NONE;
4347 /* Get changed line numbers without context lines for copy_change(). */
4348 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4349 start_old = cc.left.start;
4350 end_old = cc.left.end;
4351 start_new = cc.right.start;
4352 end_new = cc.right.end;
4354 /* Get the same change with context lines for display. */
4355 memset(&cc, 0, sizeof(cc));
4356 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4358 memset(&diff_info, 0, sizeof(diff_info));
4359 diff_info.left_path = relpath;
4360 diff_info.right_path = relpath;
4362 diff_state = diff_output_unidiff_state_alloc();
4363 if (diff_state == NULL)
4364 return got_error_set_errno(ENOMEM,
4365 "diff_output_unidiff_state_alloc");
4367 hunkfile = got_opentemp();
4368 if (hunkfile == NULL) {
4369 err = got_error_from_errno("got_opentemp");
4373 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4375 if (rc != DIFF_RC_OK) {
4376 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4380 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4381 err = got_ferror(hunkfile, GOT_ERR_IO);
4385 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4386 hunkfile, changeno, nchanges);
4391 case GOT_PATCH_CHOICE_YES:
4392 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4393 end_old, start_new, end_new, outfile, rejectfile);
4395 case GOT_PATCH_CHOICE_NO:
4396 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4397 end_old, start_new, end_new, rejectfile, outfile);
4399 case GOT_PATCH_CHOICE_QUIT:
4402 err = got_error(GOT_ERR_PATCH_CHOICE);
4406 diff_output_unidiff_state_free(diff_state);
4407 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4408 err = got_error_from_errno("fclose");
4412 struct revert_file_args {
4413 struct got_worktree *worktree;
4414 struct got_fileindex *fileindex;
4415 got_worktree_checkout_cb progress_cb;
4417 got_worktree_patch_cb patch_cb;
4419 struct got_repository *repo;
4420 int unlink_added_files;
4423 static const struct got_error *
4424 create_patched_content(char **path_outfile, int reverse_patch,
4425 struct got_object_id *blob_id, const char *path2,
4426 int dirfd2, const char *de_name2,
4427 const char *relpath, struct got_repository *repo,
4428 got_worktree_patch_cb patch_cb, void *patch_arg)
4430 const struct got_error *err, *free_err;
4431 struct got_blob_object *blob = NULL;
4432 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4433 int fd = -1, fd2 = -1;
4434 char link_target[PATH_MAX];
4435 ssize_t link_len = 0;
4436 char *path1 = NULL, *id_str = NULL;
4438 struct got_diffreg_result *diffreg_result = NULL;
4439 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4440 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4442 *path_outfile = NULL;
4444 err = got_object_id_str(&id_str, blob_id);
4449 fd2 = openat(dirfd2, de_name2,
4450 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4452 if (!got_err_open_nofollow_on_symlink()) {
4453 err = got_error_from_errno2("openat", path2);
4456 link_len = readlinkat(dirfd2, de_name2,
4457 link_target, sizeof(link_target));
4458 if (link_len == -1) {
4459 return got_error_from_errno2("readlinkat",
4462 sb2.st_mode = S_IFLNK;
4463 sb2.st_size = link_len;
4466 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4468 if (!got_err_open_nofollow_on_symlink()) {
4469 err = got_error_from_errno2("open", path2);
4472 link_len = readlink(path2, link_target,
4473 sizeof(link_target));
4475 return got_error_from_errno2("readlink", path2);
4476 sb2.st_mode = S_IFLNK;
4477 sb2.st_size = link_len;
4481 if (fstat(fd2, &sb2) == -1) {
4482 err = got_error_from_errno2("fstat", path2);
4486 f2 = fdopen(fd2, "r");
4488 err = got_error_from_errno2("fdopen", path2);
4494 f2 = got_opentemp();
4496 err = got_error_from_errno2("got_opentemp", path2);
4499 n = fwrite(link_target, 1, link_len, f2);
4500 if (n != link_len) {
4501 err = got_ferror(f2, GOT_ERR_IO);
4504 if (fflush(f2) == EOF) {
4505 err = got_error_from_errno("fflush");
4511 fd = got_opentempfd();
4513 err = got_error_from_errno("got_opentempfd");
4517 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4521 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4525 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4529 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4534 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4538 if (fseek(f1, 0L, SEEK_SET) == -1)
4539 return got_ferror(f1, GOT_ERR_IO);
4540 if (fseek(f2, 0L, SEEK_SET) == -1)
4541 return got_ferror(f2, GOT_ERR_IO);
4543 /* Count the number of actual changes in the diff result. */
4544 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4545 struct diff_chunk_context cc = {};
4546 diff_chunk_context_load_change(&cc, &nchunks_used,
4547 diffreg_result->result, n, 0);
4550 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4552 err = apply_or_reject_change(&choice, &nchunks_used,
4553 diffreg_result->result, n, relpath, f1, f2,
4554 &line_cur1, &line_cur2,
4555 reverse_patch ? NULL : outfile,
4556 reverse_patch ? outfile : NULL,
4557 ++i, nchanges, patch_cb, patch_arg);
4560 if (choice == GOT_PATCH_CHOICE_YES)
4562 else if (choice == GOT_PATCH_CHOICE_QUIT)
4566 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4567 reverse_patch ? NULL : outfile,
4568 reverse_patch ? outfile : NULL);
4572 if (!S_ISLNK(sb2.st_mode)) {
4573 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4574 err = got_error_from_errno2("fchmod", path2);
4581 if (fd != -1 && close(fd) == -1 && err == NULL)
4582 err = got_error_from_errno("close");
4584 got_object_blob_close(blob);
4585 free_err = got_diffreg_result_free(diffreg_result);
4588 if (f1 && fclose(f1) == EOF && err == NULL)
4589 err = got_error_from_errno2("fclose", path1);
4590 if (f2 && fclose(f2) == EOF && err == NULL)
4591 err = got_error_from_errno2("fclose", path2);
4592 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4593 err = got_error_from_errno2("close", path2);
4594 if (outfile && fclose(outfile) == EOF && err == NULL)
4595 err = got_error_from_errno2("fclose", *path_outfile);
4596 if (path1 && unlink(path1) == -1 && err == NULL)
4597 err = got_error_from_errno2("unlink", path1);
4598 if (err || !have_content) {
4599 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4600 err = got_error_from_errno2("unlink", *path_outfile);
4601 free(*path_outfile);
4602 *path_outfile = NULL;
4608 static const struct got_error *
4609 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4610 const char *relpath, struct got_object_id *blob_id,
4611 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4612 int dirfd, const char *de_name)
4614 struct revert_file_args *a = arg;
4615 const struct got_error *err = NULL;
4616 char *parent_path = NULL;
4617 struct got_fileindex_entry *ie;
4618 struct got_commit_object *base_commit = NULL;
4619 struct got_tree_object *tree = NULL;
4620 struct got_object_id *tree_id = NULL;
4621 const struct got_tree_entry *te = NULL;
4622 char *tree_path = NULL, *te_name;
4623 char *ondisk_path = NULL, *path_content = NULL;
4624 struct got_blob_object *blob = NULL;
4627 /* Reverting a staged deletion is a no-op. */
4628 if (status == GOT_STATUS_DELETE &&
4629 staged_status != GOT_STATUS_NO_CHANGE)
4632 if (status == GOT_STATUS_UNVERSIONED)
4633 return (*a->progress_cb)(a->progress_arg,
4634 GOT_STATUS_UNVERSIONED, relpath);
4636 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4638 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4640 /* Construct in-repository path of tree which contains this blob. */
4641 err = got_path_dirname(&parent_path, ie->path);
4643 if (err->code != GOT_ERR_BAD_PATH)
4645 parent_path = strdup("/");
4646 if (parent_path == NULL) {
4647 err = got_error_from_errno("strdup");
4651 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4652 tree_path = strdup(parent_path);
4653 if (tree_path == NULL) {
4654 err = got_error_from_errno("strdup");
4658 if (got_path_is_root_dir(parent_path)) {
4659 tree_path = strdup(a->worktree->path_prefix);
4660 if (tree_path == NULL) {
4661 err = got_error_from_errno("strdup");
4665 if (asprintf(&tree_path, "%s/%s",
4666 a->worktree->path_prefix, parent_path) == -1) {
4667 err = got_error_from_errno("asprintf");
4673 err = got_object_open_as_commit(&base_commit, a->repo,
4674 a->worktree->base_commit_id);
4678 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4680 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4681 (status == GOT_STATUS_ADD ||
4682 staged_status == GOT_STATUS_ADD)))
4685 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4689 err = got_path_basename(&te_name, ie->path);
4693 te = got_object_tree_find_entry(tree, te_name);
4695 if (te == NULL && status != GOT_STATUS_ADD &&
4696 staged_status != GOT_STATUS_ADD) {
4697 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4703 case GOT_STATUS_ADD:
4705 int choice = GOT_PATCH_CHOICE_NONE;
4706 err = (*a->patch_cb)(&choice, a->patch_arg,
4707 status, ie->path, NULL, 1, 1);
4710 if (choice != GOT_PATCH_CHOICE_YES)
4713 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4717 got_fileindex_entry_remove(a->fileindex, ie);
4718 if (a->unlink_added_files) {
4719 if (asprintf(&ondisk_path, "%s/%s",
4720 got_worktree_get_root_path(a->worktree),
4722 err = got_error_from_errno("asprintf");
4725 if (unlink(ondisk_path) == -1) {
4726 err = got_error_from_errno2("unlink",
4732 case GOT_STATUS_DELETE:
4734 int choice = GOT_PATCH_CHOICE_NONE;
4735 err = (*a->patch_cb)(&choice, a->patch_arg,
4736 status, ie->path, NULL, 1, 1);
4739 if (choice != GOT_PATCH_CHOICE_YES)
4743 case GOT_STATUS_MODIFY:
4744 case GOT_STATUS_MODE_CHANGE:
4745 case GOT_STATUS_CONFLICT:
4746 case GOT_STATUS_MISSING: {
4747 struct got_object_id id;
4748 if (staged_status == GOT_STATUS_ADD ||
4749 staged_status == GOT_STATUS_MODIFY) {
4750 memcpy(id.sha1, ie->staged_blob_sha1,
4751 SHA1_DIGEST_LENGTH);
4753 memcpy(id.sha1, ie->blob_sha1,
4754 SHA1_DIGEST_LENGTH);
4755 fd = got_opentempfd();
4757 err = got_error_from_errno("got_opentempfd");
4761 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4765 if (asprintf(&ondisk_path, "%s/%s",
4766 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4767 err = got_error_from_errno("asprintf");
4771 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4772 status == GOT_STATUS_CONFLICT)) {
4773 int is_bad_symlink = 0;
4774 err = create_patched_content(&path_content, 1, &id,
4775 ondisk_path, dirfd, de_name, ie->path, a->repo,
4776 a->patch_cb, a->patch_arg);
4777 if (err || path_content == NULL)
4779 if (te && S_ISLNK(te->mode)) {
4780 if (unlink(path_content) == -1) {
4781 err = got_error_from_errno2("unlink",
4785 err = install_symlink(&is_bad_symlink,
4786 a->worktree, ondisk_path, ie->path,
4787 blob, 0, 1, 0, 0, a->repo,
4788 a->progress_cb, a->progress_arg);
4790 if (rename(path_content, ondisk_path) == -1) {
4791 err = got_error_from_errno3("rename",
4792 path_content, ondisk_path);
4797 int is_bad_symlink = 0;
4798 if (te && S_ISLNK(te->mode)) {
4799 err = install_symlink(&is_bad_symlink,
4800 a->worktree, ondisk_path, ie->path,
4801 blob, 0, 1, 0, 0, a->repo,
4802 a->progress_cb, a->progress_arg);
4804 err = install_blob(a->worktree, ondisk_path,
4806 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4807 got_fileindex_perms_to_st(ie), blob,
4808 0, 1, 0, 0, a->repo,
4809 a->progress_cb, a->progress_arg);
4813 if (status == GOT_STATUS_DELETE ||
4814 status == GOT_STATUS_MODE_CHANGE) {
4815 err = got_fileindex_entry_update(ie,
4816 a->worktree->root_fd, relpath,
4818 a->worktree->base_commit_id->sha1, 1);
4822 if (is_bad_symlink) {
4823 got_fileindex_entry_filetype_set(ie,
4824 GOT_FILEIDX_MODE_BAD_SYMLINK);
4837 if (fd != -1 && close(fd) == -1 && err == NULL)
4838 err = got_error_from_errno("close");
4840 got_object_blob_close(blob);
4842 got_object_tree_close(tree);
4845 got_object_commit_close(base_commit);
4849 const struct got_error *
4850 got_worktree_revert(struct got_worktree *worktree,
4851 struct got_pathlist_head *paths,
4852 got_worktree_checkout_cb progress_cb, void *progress_arg,
4853 got_worktree_patch_cb patch_cb, void *patch_arg,
4854 struct got_repository *repo)
4856 struct got_fileindex *fileindex = NULL;
4857 char *fileindex_path = NULL;
4858 const struct got_error *err = NULL, *unlockerr = NULL;
4859 const struct got_error *sync_err = NULL;
4860 struct got_pathlist_entry *pe;
4861 struct revert_file_args rfa;
4863 err = lock_worktree(worktree, LOCK_EX);
4867 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4871 rfa.worktree = worktree;
4872 rfa.fileindex = fileindex;
4873 rfa.progress_cb = progress_cb;
4874 rfa.progress_arg = progress_arg;
4875 rfa.patch_cb = patch_cb;
4876 rfa.patch_arg = patch_arg;
4878 rfa.unlink_added_files = 0;
4879 TAILQ_FOREACH(pe, paths, entry) {
4880 err = worktree_status(worktree, pe->path, fileindex, repo,
4881 revert_file, &rfa, NULL, NULL, 1, 0);
4885 sync_err = sync_fileindex(fileindex, fileindex_path);
4886 if (sync_err && err == NULL)
4889 free(fileindex_path);
4891 got_fileindex_free(fileindex);
4892 unlockerr = lock_worktree(worktree, LOCK_SH);
4893 if (unlockerr && err == NULL)
4899 free_commitable(struct got_commitable *ct)
4902 free(ct->in_repo_path);
4903 free(ct->ondisk_path);
4905 free(ct->base_blob_id);
4906 free(ct->staged_blob_id);
4907 free(ct->base_commit_id);
4911 struct collect_commitables_arg {
4912 struct got_pathlist_head *commitable_paths;
4913 struct got_repository *repo;
4914 struct got_worktree *worktree;
4915 struct got_fileindex *fileindex;
4916 int have_staged_files;
4917 int allow_bad_symlinks;
4920 static const struct got_error *
4921 collect_commitables(void *arg, unsigned char status,
4922 unsigned char staged_status, const char *relpath,
4923 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4924 struct got_object_id *commit_id, int dirfd, const char *de_name)
4926 struct collect_commitables_arg *a = arg;
4927 const struct got_error *err = NULL;
4928 struct got_commitable *ct = NULL;
4929 struct got_pathlist_entry *new = NULL;
4930 char *parent_path = NULL, *path = NULL;
4933 if (a->have_staged_files) {
4934 if (staged_status != GOT_STATUS_MODIFY &&
4935 staged_status != GOT_STATUS_ADD &&
4936 staged_status != GOT_STATUS_DELETE)
4939 if (status == GOT_STATUS_CONFLICT)
4940 return got_error(GOT_ERR_COMMIT_CONFLICT);
4942 if (status != GOT_STATUS_MODIFY &&
4943 status != GOT_STATUS_MODE_CHANGE &&
4944 status != GOT_STATUS_ADD &&
4945 status != GOT_STATUS_DELETE)
4949 if (asprintf(&path, "/%s", relpath) == -1) {
4950 err = got_error_from_errno("asprintf");
4953 if (strcmp(path, "/") == 0) {
4954 parent_path = strdup("");
4955 if (parent_path == NULL)
4956 return got_error_from_errno("strdup");
4958 err = got_path_dirname(&parent_path, path);
4963 ct = calloc(1, sizeof(*ct));
4965 err = got_error_from_errno("calloc");
4969 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4971 err = got_error_from_errno("asprintf");
4975 if (staged_status == GOT_STATUS_ADD ||
4976 staged_status == GOT_STATUS_MODIFY) {
4977 struct got_fileindex_entry *ie;
4978 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4979 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4980 case GOT_FILEIDX_MODE_REGULAR_FILE:
4981 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4984 case GOT_FILEIDX_MODE_SYMLINK:
4988 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4991 ct->mode |= got_fileindex_entry_perms_get(ie);
4992 } else if (status != GOT_STATUS_DELETE &&
4993 staged_status != GOT_STATUS_DELETE) {
4995 if (fstatat(dirfd, de_name, &sb,
4996 AT_SYMLINK_NOFOLLOW) == -1) {
4997 err = got_error_from_errno2("fstatat",
5001 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5002 err = got_error_from_errno2("lstat", ct->ondisk_path);
5005 ct->mode = sb.st_mode;
5008 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5009 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5011 err = got_error_from_errno("asprintf");
5015 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5016 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5018 char target_path[PATH_MAX];
5020 target_len = readlink(ct->ondisk_path, target_path,
5021 sizeof(target_path));
5022 if (target_len == -1) {
5023 err = got_error_from_errno2("readlink",
5027 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5028 target_len, ct->ondisk_path, a->worktree->root_path);
5031 if (is_bad_symlink) {
5032 err = got_error_path(ct->ondisk_path,
5033 GOT_ERR_BAD_SYMLINK);
5039 ct->status = status;
5040 ct->staged_status = staged_status;
5041 ct->blob_id = NULL; /* will be filled in when blob gets created */
5042 if (ct->status != GOT_STATUS_ADD &&
5043 ct->staged_status != GOT_STATUS_ADD) {
5044 ct->base_blob_id = got_object_id_dup(blob_id);
5045 if (ct->base_blob_id == NULL) {
5046 err = got_error_from_errno("got_object_id_dup");
5049 ct->base_commit_id = got_object_id_dup(commit_id);
5050 if (ct->base_commit_id == NULL) {
5051 err = got_error_from_errno("got_object_id_dup");
5055 if (ct->staged_status == GOT_STATUS_ADD ||
5056 ct->staged_status == GOT_STATUS_MODIFY) {
5057 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5058 if (ct->staged_blob_id == NULL) {
5059 err = got_error_from_errno("got_object_id_dup");
5063 ct->path = strdup(path);
5064 if (ct->path == NULL) {
5065 err = got_error_from_errno("strdup");
5068 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5070 if (ct && (err || new == NULL))
5071 free_commitable(ct);
5077 static const struct got_error *write_tree(struct got_object_id **, int *,
5078 struct got_tree_object *, const char *, struct got_pathlist_head *,
5079 got_worktree_status_cb status_cb, void *status_arg,
5080 struct got_repository *);
5082 static const struct got_error *
5083 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5084 struct got_tree_entry *te, const char *parent_path,
5085 struct got_pathlist_head *commitable_paths,
5086 got_worktree_status_cb status_cb, void *status_arg,
5087 struct got_repository *repo)
5089 const struct got_error *err = NULL;
5090 struct got_tree_object *subtree;
5093 if (asprintf(&subpath, "%s%s%s", parent_path,
5094 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5095 return got_error_from_errno("asprintf");
5097 err = got_object_open_as_tree(&subtree, repo, &te->id);
5101 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5102 commitable_paths, status_cb, status_arg, repo);
5103 got_object_tree_close(subtree);
5108 static const struct got_error *
5109 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5111 const struct got_error *err = NULL;
5112 char *ct_parent_path = NULL;
5116 if (strchr(ct->in_repo_path, '/') == NULL) {
5117 *match = got_path_is_root_dir(path);
5121 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5124 *match = (strcmp(path, ct_parent_path) == 0);
5125 free(ct_parent_path);
5130 get_ct_file_mode(struct got_commitable *ct)
5132 if (S_ISLNK(ct->mode))
5135 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5138 static const struct got_error *
5139 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5140 struct got_tree_entry *te, struct got_commitable *ct)
5142 const struct got_error *err = NULL;
5146 err = got_object_tree_entry_dup(new_te, te);
5150 (*new_te)->mode = get_ct_file_mode(ct);
5152 if (ct->staged_status == GOT_STATUS_MODIFY)
5153 memcpy(&(*new_te)->id, ct->staged_blob_id,
5154 sizeof((*new_te)->id));
5156 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5158 if (err && *new_te) {
5165 static const struct got_error *
5166 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5167 struct got_commitable *ct)
5169 const struct got_error *err = NULL;
5170 char *ct_name = NULL;
5174 *new_te = calloc(1, sizeof(**new_te));
5175 if (*new_te == NULL)
5176 return got_error_from_errno("calloc");
5178 err = got_path_basename(&ct_name, ct->path);
5181 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5182 sizeof((*new_te)->name)) {
5183 err = got_error(GOT_ERR_NO_SPACE);
5187 (*new_te)->mode = get_ct_file_mode(ct);
5189 if (ct->staged_status == GOT_STATUS_ADD)
5190 memcpy(&(*new_te)->id, ct->staged_blob_id,
5191 sizeof((*new_te)->id));
5193 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5196 if (err && *new_te) {
5203 static const struct got_error *
5204 insert_tree_entry(struct got_tree_entry *new_te,
5205 struct got_pathlist_head *paths)
5207 const struct got_error *err = NULL;
5208 struct got_pathlist_entry *new_pe;
5210 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5214 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5218 static const struct got_error *
5219 report_ct_status(struct got_commitable *ct,
5220 got_worktree_status_cb status_cb, void *status_arg)
5222 const char *ct_path = ct->path;
5223 unsigned char status;
5225 if (status_cb == NULL) /* no commit progress output desired */
5228 while (ct_path[0] == '/')
5231 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5232 status = ct->staged_status;
5234 status = ct->status;
5236 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5237 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5240 static const struct got_error *
5241 match_modified_subtree(int *modified, struct got_tree_entry *te,
5242 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5244 const struct got_error *err = NULL;
5245 struct got_pathlist_entry *pe;
5250 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5251 got_path_is_root_dir(base_tree_path) ? "" : "/",
5253 return got_error_from_errno("asprintf");
5255 TAILQ_FOREACH(pe, commitable_paths, entry) {
5256 struct got_commitable *ct = pe->data;
5257 *modified = got_path_is_child(ct->in_repo_path, te_path,
5267 static const struct got_error *
5268 match_deleted_or_modified_ct(struct got_commitable **ctp,
5269 struct got_tree_entry *te, const char *base_tree_path,
5270 struct got_pathlist_head *commitable_paths)
5272 const struct got_error *err = NULL;
5273 struct got_pathlist_entry *pe;
5277 TAILQ_FOREACH(pe, commitable_paths, entry) {
5278 struct got_commitable *ct = pe->data;
5279 char *ct_name = NULL;
5282 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5283 if (ct->status != GOT_STATUS_MODIFY &&
5284 ct->status != GOT_STATUS_MODE_CHANGE &&
5285 ct->status != GOT_STATUS_DELETE)
5288 if (ct->staged_status != GOT_STATUS_MODIFY &&
5289 ct->staged_status != GOT_STATUS_DELETE)
5293 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5296 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5302 err = got_path_basename(&ct_name, pe->path);
5306 if (strcmp(te->name, ct_name) != 0) {
5319 static const struct got_error *
5320 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5321 const char *child_path, const char *path_base_tree,
5322 struct got_pathlist_head *commitable_paths,
5323 got_worktree_status_cb status_cb, void *status_arg,
5324 struct got_repository *repo)
5326 const struct got_error *err = NULL;
5327 struct got_tree_entry *new_te;
5329 struct got_object_id *id = NULL;
5334 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5335 got_path_is_root_dir(path_base_tree) ? "" : "/",
5337 return got_error_from_errno("asprintf");
5339 new_te = calloc(1, sizeof(*new_te));
5341 return got_error_from_errno("calloc");
5342 new_te->mode = S_IFDIR;
5344 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5345 sizeof(new_te->name)) {
5346 err = got_error(GOT_ERR_NO_SPACE);
5349 err = write_tree(&id, &nentries, NULL, subtree_path,
5350 commitable_paths, status_cb, status_arg, repo);
5355 memcpy(&new_te->id, id, sizeof(new_te->id));
5364 static const struct got_error *
5365 write_tree(struct got_object_id **new_tree_id, int *nentries,
5366 struct got_tree_object *base_tree, const char *path_base_tree,
5367 struct got_pathlist_head *commitable_paths,
5368 got_worktree_status_cb status_cb, void *status_arg,
5369 struct got_repository *repo)
5371 const struct got_error *err = NULL;
5372 struct got_pathlist_head paths;
5373 struct got_tree_entry *te, *new_te = NULL;
5374 struct got_pathlist_entry *pe;
5379 /* Insert, and recurse into, newly added entries first. */
5380 TAILQ_FOREACH(pe, commitable_paths, entry) {
5381 struct got_commitable *ct = pe->data;
5382 char *child_path = NULL, *slash;
5384 if ((ct->status != GOT_STATUS_ADD &&
5385 ct->staged_status != GOT_STATUS_ADD) ||
5386 (ct->flags & GOT_COMMITABLE_ADDED))
5389 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5390 strlen(path_base_tree)))
5393 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5398 slash = strchr(child_path, '/');
5399 if (slash == NULL) {
5400 err = alloc_added_blob_tree_entry(&new_te, ct);
5403 err = report_ct_status(ct, status_cb, status_arg);
5406 ct->flags |= GOT_COMMITABLE_ADDED;
5407 err = insert_tree_entry(new_te, &paths);
5412 *slash = '\0'; /* trim trailing path components */
5413 if (base_tree == NULL ||
5414 got_object_tree_find_entry(base_tree, child_path)
5416 err = make_subtree_for_added_blob(&new_te,
5417 child_path, path_base_tree,
5418 commitable_paths, status_cb, status_arg,
5422 err = insert_tree_entry(new_te, &paths);
5431 int i, nbase_entries;
5432 /* Handle modified and deleted entries. */
5433 nbase_entries = got_object_tree_get_nentries(base_tree);
5434 for (i = 0; i < nbase_entries; i++) {
5435 struct got_commitable *ct = NULL;
5437 te = got_object_tree_get_entry(base_tree, i);
5438 if (got_object_tree_entry_is_submodule(te)) {
5439 /* Entry is a submodule; just copy it. */
5440 err = got_object_tree_entry_dup(&new_te, te);
5443 err = insert_tree_entry(new_te, &paths);
5450 if (S_ISDIR(te->mode)) {
5452 err = got_object_tree_entry_dup(&new_te, te);
5455 err = match_modified_subtree(&modified, te,
5456 path_base_tree, commitable_paths);
5459 /* Avoid recursion into unmodified subtrees. */
5461 struct got_object_id *new_id;
5463 err = write_subtree(&new_id,
5465 path_base_tree, commitable_paths,
5466 status_cb, status_arg, repo);
5469 if (nsubentries == 0) {
5470 /* All entries were deleted. */
5474 memcpy(&new_te->id, new_id,
5475 sizeof(new_te->id));
5478 err = insert_tree_entry(new_te, &paths);
5485 err = match_deleted_or_modified_ct(&ct, te,
5486 path_base_tree, commitable_paths);
5490 /* NB: Deleted entries get dropped here. */
5491 if (ct->status == GOT_STATUS_MODIFY ||
5492 ct->status == GOT_STATUS_MODE_CHANGE ||
5493 ct->staged_status == GOT_STATUS_MODIFY) {
5494 err = alloc_modified_blob_tree_entry(
5498 err = insert_tree_entry(new_te, &paths);
5503 err = report_ct_status(ct, status_cb,
5508 /* Entry is unchanged; just copy it. */
5509 err = got_object_tree_entry_dup(&new_te, te);
5512 err = insert_tree_entry(new_te, &paths);
5520 /* Write new list of entries; deleted entries have been dropped. */
5521 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5523 got_pathlist_free(&paths);
5527 static const struct got_error *
5528 update_fileindex_after_commit(struct got_worktree *worktree,
5529 struct got_pathlist_head *commitable_paths,
5530 struct got_object_id *new_base_commit_id,
5531 struct got_fileindex *fileindex, int have_staged_files)
5533 const struct got_error *err = NULL;
5534 struct got_pathlist_entry *pe;
5535 char *relpath = NULL;
5537 TAILQ_FOREACH(pe, commitable_paths, entry) {
5538 struct got_fileindex_entry *ie;
5539 struct got_commitable *ct = pe->data;
5541 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5543 err = got_path_skip_common_ancestor(&relpath,
5544 worktree->root_path, ct->ondisk_path);
5549 if (ct->status == GOT_STATUS_DELETE ||
5550 ct->staged_status == GOT_STATUS_DELETE) {
5551 got_fileindex_entry_remove(fileindex, ie);
5552 } else if (ct->staged_status == GOT_STATUS_ADD ||
5553 ct->staged_status == GOT_STATUS_MODIFY) {
5554 got_fileindex_entry_stage_set(ie,
5555 GOT_FILEIDX_STAGE_NONE);
5556 got_fileindex_entry_staged_filetype_set(ie, 0);
5558 err = got_fileindex_entry_update(ie,
5559 worktree->root_fd, relpath,
5560 ct->staged_blob_id->sha1,
5561 new_base_commit_id->sha1,
5562 !have_staged_files);
5564 err = got_fileindex_entry_update(ie,
5565 worktree->root_fd, relpath,
5567 new_base_commit_id->sha1,
5568 !have_staged_files);
5570 err = got_fileindex_entry_alloc(&ie, pe->path);
5573 err = got_fileindex_entry_update(ie,
5574 worktree->root_fd, relpath, ct->blob_id->sha1,
5575 new_base_commit_id->sha1, 1);
5577 got_fileindex_entry_free(ie);
5580 err = got_fileindex_entry_add(fileindex, ie);
5582 got_fileindex_entry_free(ie);
5595 static const struct got_error *
5596 check_out_of_date(const char *in_repo_path, unsigned char status,
5597 unsigned char staged_status, struct got_object_id *base_blob_id,
5598 struct got_object_id *base_commit_id,
5599 struct got_object_id *head_commit_id, struct got_repository *repo,
5602 const struct got_error *err = NULL;
5603 struct got_commit_object *commit = NULL;
5604 struct got_object_id *id = NULL;
5606 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5607 /* Trivial case: base commit == head commit */
5608 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5611 * Ensure file content which local changes were based
5612 * on matches file content in the branch head.
5614 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5617 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5619 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5620 err = got_error(ood_errcode);
5622 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5623 err = got_error(ood_errcode);
5625 /* Require that added files don't exist in the branch head. */
5626 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5629 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5630 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5632 err = id ? got_error(ood_errcode) : NULL;
5637 got_object_commit_close(commit);
5641 static const struct got_error *
5642 commit_worktree(struct got_object_id **new_commit_id,
5643 struct got_pathlist_head *commitable_paths,
5644 struct got_object_id *head_commit_id,
5645 struct got_object_id *parent_id2,
5646 struct got_worktree *worktree,
5647 const char *author, const char *committer,
5648 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5649 got_worktree_status_cb status_cb, void *status_arg,
5650 struct got_repository *repo)
5652 const struct got_error *err = NULL, *unlockerr = NULL;
5653 struct got_pathlist_entry *pe;
5654 const char *head_ref_name = NULL;
5655 struct got_commit_object *head_commit = NULL;
5656 struct got_reference *head_ref2 = NULL;
5657 struct got_object_id *head_commit_id2 = NULL;
5658 struct got_tree_object *head_tree = NULL;
5659 struct got_object_id *new_tree_id = NULL;
5660 int nentries, nparents = 0;
5661 struct got_object_id_queue parent_ids;
5662 struct got_object_qid *pid = NULL;
5663 char *logmsg = NULL;
5665 *new_commit_id = NULL;
5667 STAILQ_INIT(&parent_ids);
5669 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5673 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5677 if (commit_msg_cb != NULL) {
5678 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5683 if (logmsg == NULL || strlen(logmsg) == 0) {
5684 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5688 /* Create blobs from added and modified files and record their IDs. */
5689 TAILQ_FOREACH(pe, commitable_paths, entry) {
5690 struct got_commitable *ct = pe->data;
5693 /* Blobs for staged files already exist. */
5694 if (ct->staged_status == GOT_STATUS_ADD ||
5695 ct->staged_status == GOT_STATUS_MODIFY)
5698 if (ct->status != GOT_STATUS_ADD &&
5699 ct->status != GOT_STATUS_MODIFY &&
5700 ct->status != GOT_STATUS_MODE_CHANGE)
5703 if (asprintf(&ondisk_path, "%s/%s",
5704 worktree->root_path, pe->path) == -1) {
5705 err = got_error_from_errno("asprintf");
5708 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5714 /* Recursively write new tree objects. */
5715 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5716 commitable_paths, status_cb, status_arg, repo);
5720 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5723 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5726 err = got_object_qid_alloc(&pid, parent_id2);
5729 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5732 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5733 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5739 /* Check if a concurrent commit to our branch has occurred. */
5740 head_ref_name = got_worktree_get_head_ref_name(worktree);
5741 if (head_ref_name == NULL) {
5742 err = got_error_from_errno("got_worktree_get_head_ref_name");
5745 /* Lock the reference here to prevent concurrent modification. */
5746 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5749 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5752 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5753 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5756 /* Update branch head in repository. */
5757 err = got_ref_change_ref(head_ref2, *new_commit_id);
5760 err = got_ref_write(head_ref2, repo);
5764 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5768 err = ref_base_commit(worktree, repo);
5772 got_object_id_queue_free(&parent_ids);
5774 got_object_tree_close(head_tree);
5776 got_object_commit_close(head_commit);
5777 free(head_commit_id2);
5779 unlockerr = got_ref_unlock(head_ref2);
5780 if (unlockerr && err == NULL)
5782 got_ref_close(head_ref2);
5787 static const struct got_error *
5788 check_path_is_commitable(const char *path,
5789 struct got_pathlist_head *commitable_paths)
5791 struct got_pathlist_entry *cpe = NULL;
5792 size_t path_len = strlen(path);
5794 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5795 struct got_commitable *ct = cpe->data;
5796 const char *ct_path = ct->path;
5798 while (ct_path[0] == '/')
5801 if (strcmp(path, ct_path) == 0 ||
5802 got_path_is_child(ct_path, path, path_len))
5807 return got_error_path(path, GOT_ERR_BAD_PATH);
5812 static const struct got_error *
5813 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5815 int *have_staged_files = arg;
5817 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5818 *have_staged_files = 1;
5819 return got_error(GOT_ERR_CANCELLED);
5825 static const struct got_error *
5826 check_non_staged_files(struct got_fileindex *fileindex,
5827 struct got_pathlist_head *paths)
5829 struct got_pathlist_entry *pe;
5830 struct got_fileindex_entry *ie;
5832 TAILQ_FOREACH(pe, paths, entry) {
5833 if (pe->path[0] == '\0')
5835 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5837 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5838 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5839 return got_error_path(pe->path,
5840 GOT_ERR_FILE_NOT_STAGED);
5846 const struct got_error *
5847 got_worktree_commit(struct got_object_id **new_commit_id,
5848 struct got_worktree *worktree, struct got_pathlist_head *paths,
5849 const char *author, const char *committer, int allow_bad_symlinks,
5850 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5851 got_worktree_status_cb status_cb, void *status_arg,
5852 struct got_repository *repo)
5854 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5855 struct got_fileindex *fileindex = NULL;
5856 char *fileindex_path = NULL;
5857 struct got_pathlist_head commitable_paths;
5858 struct collect_commitables_arg cc_arg;
5859 struct got_pathlist_entry *pe;
5860 struct got_reference *head_ref = NULL;
5861 struct got_object_id *head_commit_id = NULL;
5862 int have_staged_files = 0;
5864 *new_commit_id = NULL;
5866 TAILQ_INIT(&commitable_paths);
5868 err = lock_worktree(worktree, LOCK_EX);
5872 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5876 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5880 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5884 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5885 &have_staged_files);
5886 if (err && err->code != GOT_ERR_CANCELLED)
5888 if (have_staged_files) {
5889 err = check_non_staged_files(fileindex, paths);
5894 cc_arg.commitable_paths = &commitable_paths;
5895 cc_arg.worktree = worktree;
5896 cc_arg.fileindex = fileindex;
5898 cc_arg.have_staged_files = have_staged_files;
5899 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5900 TAILQ_FOREACH(pe, paths, entry) {
5901 err = worktree_status(worktree, pe->path, fileindex, repo,
5902 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5907 if (TAILQ_EMPTY(&commitable_paths)) {
5908 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5912 TAILQ_FOREACH(pe, paths, entry) {
5913 err = check_path_is_commitable(pe->path, &commitable_paths);
5918 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5919 struct got_commitable *ct = pe->data;
5920 const char *ct_path = ct->in_repo_path;
5922 while (ct_path[0] == '/')
5924 err = check_out_of_date(ct_path, ct->status,
5925 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5926 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5932 err = commit_worktree(new_commit_id, &commitable_paths,
5933 head_commit_id, NULL, worktree, author, committer,
5934 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5938 err = update_fileindex_after_commit(worktree, &commitable_paths,
5939 *new_commit_id, fileindex, have_staged_files);
5940 sync_err = sync_fileindex(fileindex, fileindex_path);
5941 if (sync_err && err == NULL)
5945 got_fileindex_free(fileindex);
5946 free(fileindex_path);
5947 unlockerr = lock_worktree(worktree, LOCK_SH);
5948 if (unlockerr && err == NULL)
5950 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5951 struct got_commitable *ct = pe->data;
5952 free_commitable(ct);
5954 got_pathlist_free(&commitable_paths);
5959 got_commitable_get_path(struct got_commitable *ct)
5965 got_commitable_get_status(struct got_commitable *ct)
5970 struct check_rebase_ok_arg {
5971 struct got_worktree *worktree;
5972 struct got_repository *repo;
5975 static const struct got_error *
5976 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5978 const struct got_error *err = NULL;
5979 struct check_rebase_ok_arg *a = arg;
5980 unsigned char status;
5984 /* Reject rebase of a work tree with mixed base commits. */
5985 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5986 SHA1_DIGEST_LENGTH))
5987 return got_error(GOT_ERR_MIXED_COMMITS);
5989 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5991 return got_error_from_errno("asprintf");
5993 /* Reject rebase of a work tree with modified or staged files. */
5994 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5999 if (status != GOT_STATUS_NO_CHANGE)
6000 return got_error(GOT_ERR_MODIFIED);
6001 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6002 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6007 const struct got_error *
6008 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6009 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6010 struct got_worktree *worktree, struct got_reference *branch,
6011 struct got_repository *repo)
6013 const struct got_error *err = NULL;
6014 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6015 char *branch_ref_name = NULL;
6016 char *fileindex_path = NULL;
6017 struct check_rebase_ok_arg ok_arg;
6018 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6019 struct got_object_id *wt_branch_tip = NULL;
6021 *new_base_branch_ref = NULL;
6025 err = lock_worktree(worktree, LOCK_EX);
6029 err = open_fileindex(fileindex, &fileindex_path, worktree);
6033 ok_arg.worktree = worktree;
6035 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6040 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6044 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6048 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6052 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6057 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6060 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6061 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6065 err = got_ref_alloc_symref(new_base_branch_ref,
6066 new_base_branch_ref_name, wt_branch);
6069 err = got_ref_write(*new_base_branch_ref, repo);
6073 /* TODO Lock original branch's ref while rebasing? */
6075 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6079 err = got_ref_write(branch_ref, repo);
6083 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6084 worktree->base_commit_id);
6087 err = got_ref_write(*tmp_branch, repo);
6091 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6095 free(fileindex_path);
6096 free(tmp_branch_name);
6097 free(new_base_branch_ref_name);
6098 free(branch_ref_name);
6100 got_ref_close(branch_ref);
6102 got_ref_close(wt_branch);
6103 free(wt_branch_tip);
6105 if (*new_base_branch_ref) {
6106 got_ref_close(*new_base_branch_ref);
6107 *new_base_branch_ref = NULL;
6110 got_ref_close(*tmp_branch);
6114 got_fileindex_free(*fileindex);
6117 lock_worktree(worktree, LOCK_SH);
6122 const struct got_error *
6123 got_worktree_rebase_continue(struct got_object_id **commit_id,
6124 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6125 struct got_reference **branch, struct got_fileindex **fileindex,
6126 struct got_worktree *worktree, struct got_repository *repo)
6128 const struct got_error *err;
6129 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6130 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6131 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6132 char *fileindex_path = NULL;
6133 int have_staged_files = 0;
6136 *new_base_branch = NULL;
6141 err = lock_worktree(worktree, LOCK_EX);
6145 err = open_fileindex(fileindex, &fileindex_path, worktree);
6149 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6150 &have_staged_files);
6151 if (err && err->code != GOT_ERR_CANCELLED)
6153 if (have_staged_files) {
6154 err = got_error(GOT_ERR_STAGED_PATHS);
6158 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6162 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6166 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6170 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6174 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6178 err = got_ref_open(branch, repo,
6179 got_ref_get_symref_target(branch_ref), 0);
6183 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6187 err = got_ref_resolve(commit_id, repo, commit_ref);
6191 err = got_ref_open(new_base_branch, repo,
6192 new_base_branch_ref_name, 0);
6196 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6200 free(commit_ref_name);
6201 free(branch_ref_name);
6202 free(fileindex_path);
6204 got_ref_close(commit_ref);
6206 got_ref_close(branch_ref);
6211 got_ref_close(*tmp_branch);
6214 if (*new_base_branch) {
6215 got_ref_close(*new_base_branch);
6216 *new_base_branch = NULL;
6219 got_ref_close(*branch);
6223 got_fileindex_free(*fileindex);
6226 lock_worktree(worktree, LOCK_SH);
6231 const struct got_error *
6232 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6234 const struct got_error *err;
6235 char *tmp_branch_name = NULL;
6237 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6241 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6242 free(tmp_branch_name);
6246 static const struct got_error *
6247 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6248 char **logmsg, void *arg)
6254 static const struct got_error *
6255 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6256 const char *path, struct got_object_id *blob_id,
6257 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6258 int dirfd, const char *de_name)
6263 struct collect_merged_paths_arg {
6264 got_worktree_checkout_cb progress_cb;
6266 struct got_pathlist_head *merged_paths;
6269 static const struct got_error *
6270 collect_merged_paths(void *arg, unsigned char status, const char *path)
6272 const struct got_error *err;
6273 struct collect_merged_paths_arg *a = arg;
6275 struct got_pathlist_entry *new;
6277 err = (*a->progress_cb)(a->progress_arg, status, path);
6281 if (status != GOT_STATUS_MERGE &&
6282 status != GOT_STATUS_ADD &&
6283 status != GOT_STATUS_DELETE &&
6284 status != GOT_STATUS_CONFLICT)
6289 return got_error_from_errno("strdup");
6291 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6292 if (err || new == NULL)
6298 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6300 struct got_pathlist_entry *pe;
6302 TAILQ_FOREACH(pe, merged_paths, entry)
6303 free((char *)pe->path);
6305 got_pathlist_free(merged_paths);
6308 static const struct got_error *
6309 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6310 int is_rebase, struct got_repository *repo)
6312 const struct got_error *err;
6313 struct got_reference *commit_ref = NULL;
6315 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6317 if (err->code != GOT_ERR_NOT_REF)
6319 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6322 err = got_ref_write(commit_ref, repo);
6325 } else if (is_rebase) {
6326 struct got_object_id *stored_id;
6329 err = got_ref_resolve(&stored_id, repo, commit_ref);
6332 cmp = got_object_id_cmp(commit_id, stored_id);
6335 err = got_error(GOT_ERR_REBASE_COMMITID);
6341 got_ref_close(commit_ref);
6345 static const struct got_error *
6346 rebase_merge_files(struct got_pathlist_head *merged_paths,
6347 const char *commit_ref_name, struct got_worktree *worktree,
6348 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6349 struct got_object_id *commit_id, struct got_repository *repo,
6350 got_worktree_checkout_cb progress_cb, void *progress_arg,
6351 got_cancel_cb cancel_cb, void *cancel_arg)
6353 const struct got_error *err;
6354 struct got_reference *commit_ref = NULL;
6355 struct collect_merged_paths_arg cmp_arg;
6356 char *fileindex_path;
6358 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6360 err = get_fileindex_path(&fileindex_path, worktree);
6364 cmp_arg.progress_cb = progress_cb;
6365 cmp_arg.progress_arg = progress_arg;
6366 cmp_arg.merged_paths = merged_paths;
6367 err = merge_files(worktree, fileindex, fileindex_path,
6368 parent_commit_id, commit_id, repo, collect_merged_paths,
6369 &cmp_arg, cancel_cb, cancel_arg);
6371 got_ref_close(commit_ref);
6375 const struct got_error *
6376 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6377 struct got_worktree *worktree, struct got_fileindex *fileindex,
6378 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6379 struct got_repository *repo,
6380 got_worktree_checkout_cb progress_cb, void *progress_arg,
6381 got_cancel_cb cancel_cb, void *cancel_arg)
6383 const struct got_error *err;
6384 char *commit_ref_name;
6386 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6390 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6394 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6395 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6396 progress_arg, cancel_cb, cancel_arg);
6398 free(commit_ref_name);
6402 const struct got_error *
6403 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6404 struct got_worktree *worktree, struct got_fileindex *fileindex,
6405 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6406 struct got_repository *repo,
6407 got_worktree_checkout_cb progress_cb, void *progress_arg,
6408 got_cancel_cb cancel_cb, void *cancel_arg)
6410 const struct got_error *err;
6411 char *commit_ref_name;
6413 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6417 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6421 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6422 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6423 progress_arg, cancel_cb, cancel_arg);
6425 free(commit_ref_name);
6429 static const struct got_error *
6430 rebase_commit(struct got_object_id **new_commit_id,
6431 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6432 struct got_worktree *worktree, struct got_fileindex *fileindex,
6433 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6434 const char *new_logmsg, struct got_repository *repo)
6436 const struct got_error *err, *sync_err;
6437 struct got_pathlist_head commitable_paths;
6438 struct collect_commitables_arg cc_arg;
6439 char *fileindex_path = NULL;
6440 struct got_reference *head_ref = NULL;
6441 struct got_object_id *head_commit_id = NULL;
6442 char *logmsg = NULL;
6444 TAILQ_INIT(&commitable_paths);
6445 *new_commit_id = NULL;
6447 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6449 err = get_fileindex_path(&fileindex_path, worktree);
6453 cc_arg.commitable_paths = &commitable_paths;
6454 cc_arg.worktree = worktree;
6456 cc_arg.have_staged_files = 0;
6458 * If possible get the status of individual files directly to
6459 * avoid crawling the entire work tree once per rebased commit.
6461 * Ideally, merged_paths would contain a list of commitables
6462 * we could use so we could skip worktree_status() entirely.
6463 * However, we would then need carefully keep track of cumulative
6464 * effects of operations such as file additions and deletions
6465 * in 'got histedit -f' (folding multiple commits into one),
6466 * and this extra complexity is not really worth it.
6469 struct got_pathlist_entry *pe;
6470 TAILQ_FOREACH(pe, merged_paths, entry) {
6471 err = worktree_status(worktree, pe->path, fileindex,
6472 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6478 err = worktree_status(worktree, "", fileindex, repo,
6479 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6484 if (TAILQ_EMPTY(&commitable_paths)) {
6485 /* No-op change; commit will be elided. */
6486 err = got_ref_delete(commit_ref, repo);
6489 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6493 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6497 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6502 logmsg = strdup(new_logmsg);
6503 if (logmsg == NULL) {
6504 err = got_error_from_errno("strdup");
6508 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6513 /* NB: commit_worktree will call free(logmsg) */
6514 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6515 NULL, worktree, got_object_commit_get_author(orig_commit),
6516 got_object_commit_get_committer(orig_commit),
6517 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6521 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6525 err = got_ref_delete(commit_ref, repo);
6529 err = update_fileindex_after_commit(worktree, &commitable_paths,
6530 *new_commit_id, fileindex, 0);
6531 sync_err = sync_fileindex(fileindex, fileindex_path);
6532 if (sync_err && err == NULL)
6535 free(fileindex_path);
6536 free(head_commit_id);
6538 got_ref_close(head_ref);
6540 free(*new_commit_id);
6541 *new_commit_id = NULL;
6546 const struct got_error *
6547 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6548 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6549 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6550 struct got_commit_object *orig_commit,
6551 struct got_object_id *orig_commit_id, struct got_repository *repo)
6553 const struct got_error *err;
6554 char *commit_ref_name;
6555 struct got_reference *commit_ref = NULL;
6556 struct got_object_id *commit_id = NULL;
6558 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6562 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6565 err = got_ref_resolve(&commit_id, repo, commit_ref);
6568 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6569 err = got_error(GOT_ERR_REBASE_COMMITID);
6573 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6574 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6577 got_ref_close(commit_ref);
6578 free(commit_ref_name);
6583 const struct got_error *
6584 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6585 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6586 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6587 struct got_commit_object *orig_commit,
6588 struct got_object_id *orig_commit_id, const char *new_logmsg,
6589 struct got_repository *repo)
6591 const struct got_error *err;
6592 char *commit_ref_name;
6593 struct got_reference *commit_ref = NULL;
6595 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6599 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6603 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6604 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6607 got_ref_close(commit_ref);
6608 free(commit_ref_name);
6612 const struct got_error *
6613 got_worktree_rebase_postpone(struct got_worktree *worktree,
6614 struct got_fileindex *fileindex)
6617 got_fileindex_free(fileindex);
6618 return lock_worktree(worktree, LOCK_SH);
6621 static const struct got_error *
6622 delete_ref(const char *name, struct got_repository *repo)
6624 const struct got_error *err;
6625 struct got_reference *ref;
6627 err = got_ref_open(&ref, repo, name, 0);
6629 if (err->code == GOT_ERR_NOT_REF)
6634 err = got_ref_delete(ref, repo);
6639 static const struct got_error *
6640 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6642 const struct got_error *err;
6643 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6644 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6646 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6649 err = delete_ref(tmp_branch_name, repo);
6653 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6656 err = delete_ref(new_base_branch_ref_name, repo);
6660 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6663 err = delete_ref(branch_ref_name, repo);
6667 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6670 err = delete_ref(commit_ref_name, repo);
6675 free(tmp_branch_name);
6676 free(new_base_branch_ref_name);
6677 free(branch_ref_name);
6678 free(commit_ref_name);
6682 static const struct got_error *
6683 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6684 struct got_object_id *new_commit_id, struct got_repository *repo)
6686 const struct got_error *err;
6687 struct got_reference *ref = NULL;
6688 struct got_object_id *old_commit_id = NULL;
6689 const char *branch_name = NULL;
6690 char *new_id_str = NULL;
6691 char *refname = NULL;
6693 branch_name = got_ref_get_name(branch);
6694 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6695 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6698 err = got_object_id_str(&new_id_str, new_commit_id);
6702 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6703 new_id_str) == -1) {
6704 err = got_error_from_errno("asprintf");
6708 err = got_ref_resolve(&old_commit_id, repo, branch);
6712 err = got_ref_alloc(&ref, refname, old_commit_id);
6716 err = got_ref_write(ref, repo);
6720 free(old_commit_id);
6726 const struct got_error *
6727 got_worktree_rebase_complete(struct got_worktree *worktree,
6728 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6729 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6730 struct got_repository *repo, int create_backup)
6732 const struct got_error *err, *unlockerr, *sync_err;
6733 struct got_object_id *new_head_commit_id = NULL;
6734 char *fileindex_path = NULL;
6736 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6740 if (create_backup) {
6741 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6742 rebased_branch, new_head_commit_id, repo);
6747 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6751 err = got_ref_write(rebased_branch, repo);
6755 err = got_worktree_set_head_ref(worktree, rebased_branch);
6759 err = delete_rebase_refs(worktree, repo);
6763 err = get_fileindex_path(&fileindex_path, worktree);
6766 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6767 sync_err = sync_fileindex(fileindex, fileindex_path);
6768 if (sync_err && err == NULL)
6771 got_fileindex_free(fileindex);
6772 free(fileindex_path);
6773 free(new_head_commit_id);
6774 unlockerr = lock_worktree(worktree, LOCK_SH);
6775 if (unlockerr && err == NULL)
6780 const struct got_error *
6781 got_worktree_rebase_abort(struct got_worktree *worktree,
6782 struct got_fileindex *fileindex, struct got_repository *repo,
6783 struct got_reference *new_base_branch,
6784 got_worktree_checkout_cb progress_cb, void *progress_arg)
6786 const struct got_error *err, *unlockerr, *sync_err;
6787 struct got_reference *resolved = NULL;
6788 struct got_object_id *commit_id = NULL;
6789 struct got_commit_object *commit = NULL;
6790 char *fileindex_path = NULL;
6791 struct revert_file_args rfa;
6792 struct got_object_id *tree_id = NULL;
6794 err = lock_worktree(worktree, LOCK_EX);
6798 err = got_object_open_as_commit(&commit, repo,
6799 worktree->base_commit_id);
6803 err = got_ref_open(&resolved, repo,
6804 got_ref_get_symref_target(new_base_branch), 0);
6808 err = got_worktree_set_head_ref(worktree, resolved);
6813 * XXX commits to the base branch could have happened while
6814 * we were busy rebasing; should we store the original commit ID
6815 * when rebase begins and read it back here?
6817 err = got_ref_resolve(&commit_id, repo, resolved);
6821 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6825 err = got_object_id_by_path(&tree_id, repo, commit,
6826 worktree->path_prefix);
6830 err = delete_rebase_refs(worktree, repo);
6834 err = get_fileindex_path(&fileindex_path, worktree);
6838 rfa.worktree = worktree;
6839 rfa.fileindex = fileindex;
6840 rfa.progress_cb = progress_cb;
6841 rfa.progress_arg = progress_arg;
6842 rfa.patch_cb = NULL;
6843 rfa.patch_arg = NULL;
6845 rfa.unlink_added_files = 0;
6846 err = worktree_status(worktree, "", fileindex, repo,
6847 revert_file, &rfa, NULL, NULL, 1, 0);
6851 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6852 repo, progress_cb, progress_arg, NULL, NULL);
6854 sync_err = sync_fileindex(fileindex, fileindex_path);
6855 if (sync_err && err == NULL)
6858 got_ref_close(resolved);
6862 got_object_commit_close(commit);
6864 got_fileindex_free(fileindex);
6865 free(fileindex_path);
6867 unlockerr = lock_worktree(worktree, LOCK_SH);
6868 if (unlockerr && err == NULL)
6873 const struct got_error *
6874 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6875 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6876 struct got_fileindex **fileindex, struct got_worktree *worktree,
6877 struct got_repository *repo)
6879 const struct got_error *err = NULL;
6880 char *tmp_branch_name = NULL;
6881 char *branch_ref_name = NULL;
6882 char *base_commit_ref_name = NULL;
6883 char *fileindex_path = NULL;
6884 struct check_rebase_ok_arg ok_arg;
6885 struct got_reference *wt_branch = NULL;
6886 struct got_reference *base_commit_ref = NULL;
6890 *base_commit_id = NULL;
6893 err = lock_worktree(worktree, LOCK_EX);
6897 err = open_fileindex(fileindex, &fileindex_path, worktree);
6901 ok_arg.worktree = worktree;
6903 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6908 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6912 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6916 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6921 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6926 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6930 err = got_ref_write(*branch_ref, repo);
6934 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6935 worktree->base_commit_id);
6938 err = got_ref_write(base_commit_ref, repo);
6941 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6942 if (*base_commit_id == NULL) {
6943 err = got_error_from_errno("got_object_id_dup");
6947 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6948 worktree->base_commit_id);
6951 err = got_ref_write(*tmp_branch, repo);
6955 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6959 free(fileindex_path);
6960 free(tmp_branch_name);
6961 free(branch_ref_name);
6962 free(base_commit_ref_name);
6964 got_ref_close(wt_branch);
6967 got_ref_close(*branch_ref);
6971 got_ref_close(*tmp_branch);
6974 free(*base_commit_id);
6976 got_fileindex_free(*fileindex);
6979 lock_worktree(worktree, LOCK_SH);
6984 const struct got_error *
6985 got_worktree_histedit_postpone(struct got_worktree *worktree,
6986 struct got_fileindex *fileindex)
6989 got_fileindex_free(fileindex);
6990 return lock_worktree(worktree, LOCK_SH);
6993 const struct got_error *
6994 got_worktree_histedit_in_progress(int *in_progress,
6995 struct got_worktree *worktree)
6997 const struct got_error *err;
6998 char *tmp_branch_name = NULL;
7000 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7004 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7005 free(tmp_branch_name);
7009 const struct got_error *
7010 got_worktree_histedit_continue(struct got_object_id **commit_id,
7011 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7012 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7013 struct got_worktree *worktree, struct got_repository *repo)
7015 const struct got_error *err;
7016 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7017 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7018 struct got_reference *commit_ref = NULL;
7019 struct got_reference *base_commit_ref = NULL;
7020 char *fileindex_path = NULL;
7021 int have_staged_files = 0;
7025 *base_commit_id = NULL;
7028 err = lock_worktree(worktree, LOCK_EX);
7032 err = open_fileindex(fileindex, &fileindex_path, worktree);
7036 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7037 &have_staged_files);
7038 if (err && err->code != GOT_ERR_CANCELLED)
7040 if (have_staged_files) {
7041 err = got_error(GOT_ERR_STAGED_PATHS);
7045 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7049 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7053 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7057 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7062 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7066 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7069 err = got_ref_resolve(commit_id, repo, commit_ref);
7073 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7076 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7080 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7084 free(commit_ref_name);
7085 free(branch_ref_name);
7086 free(fileindex_path);
7088 got_ref_close(commit_ref);
7089 if (base_commit_ref)
7090 got_ref_close(base_commit_ref);
7094 free(*base_commit_id);
7095 *base_commit_id = NULL;
7097 got_ref_close(*tmp_branch);
7101 got_fileindex_free(*fileindex);
7104 lock_worktree(worktree, LOCK_EX);
7109 static const struct got_error *
7110 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7112 const struct got_error *err;
7113 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7114 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7116 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7119 err = delete_ref(tmp_branch_name, repo);
7123 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7127 err = delete_ref(base_commit_ref_name, repo);
7131 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7134 err = delete_ref(branch_ref_name, repo);
7138 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7141 err = delete_ref(commit_ref_name, repo);
7145 free(tmp_branch_name);
7146 free(base_commit_ref_name);
7147 free(branch_ref_name);
7148 free(commit_ref_name);
7152 const struct got_error *
7153 got_worktree_histedit_abort(struct got_worktree *worktree,
7154 struct got_fileindex *fileindex, struct got_repository *repo,
7155 struct got_reference *branch, struct got_object_id *base_commit_id,
7156 got_worktree_checkout_cb progress_cb, void *progress_arg)
7158 const struct got_error *err, *unlockerr, *sync_err;
7159 struct got_reference *resolved = NULL;
7160 char *fileindex_path = NULL;
7161 struct got_commit_object *commit = NULL;
7162 struct got_object_id *tree_id = NULL;
7163 struct revert_file_args rfa;
7165 err = lock_worktree(worktree, LOCK_EX);
7169 err = got_object_open_as_commit(&commit, repo,
7170 worktree->base_commit_id);
7174 err = got_ref_open(&resolved, repo,
7175 got_ref_get_symref_target(branch), 0);
7179 err = got_worktree_set_head_ref(worktree, resolved);
7183 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7187 err = got_object_id_by_path(&tree_id, repo, commit,
7188 worktree->path_prefix);
7192 err = delete_histedit_refs(worktree, repo);
7196 err = get_fileindex_path(&fileindex_path, worktree);
7200 rfa.worktree = worktree;
7201 rfa.fileindex = fileindex;
7202 rfa.progress_cb = progress_cb;
7203 rfa.progress_arg = progress_arg;
7204 rfa.patch_cb = NULL;
7205 rfa.patch_arg = NULL;
7207 rfa.unlink_added_files = 0;
7208 err = worktree_status(worktree, "", fileindex, repo,
7209 revert_file, &rfa, NULL, NULL, 1, 0);
7213 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7214 repo, progress_cb, progress_arg, NULL, NULL);
7216 sync_err = sync_fileindex(fileindex, fileindex_path);
7217 if (sync_err && err == NULL)
7220 got_ref_close(resolved);
7222 free(fileindex_path);
7224 unlockerr = lock_worktree(worktree, LOCK_SH);
7225 if (unlockerr && err == NULL)
7230 const struct got_error *
7231 got_worktree_histedit_complete(struct got_worktree *worktree,
7232 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7233 struct got_reference *edited_branch, struct got_repository *repo)
7235 const struct got_error *err, *unlockerr, *sync_err;
7236 struct got_object_id *new_head_commit_id = NULL;
7237 struct got_reference *resolved = NULL;
7238 char *fileindex_path = NULL;
7240 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7244 err = got_ref_open(&resolved, repo,
7245 got_ref_get_symref_target(edited_branch), 0);
7249 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7250 resolved, new_head_commit_id, repo);
7254 err = got_ref_change_ref(resolved, new_head_commit_id);
7258 err = got_ref_write(resolved, repo);
7262 err = got_worktree_set_head_ref(worktree, resolved);
7266 err = delete_histedit_refs(worktree, repo);
7270 err = get_fileindex_path(&fileindex_path, worktree);
7273 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7274 sync_err = sync_fileindex(fileindex, fileindex_path);
7275 if (sync_err && err == NULL)
7278 got_fileindex_free(fileindex);
7279 free(fileindex_path);
7280 free(new_head_commit_id);
7281 unlockerr = lock_worktree(worktree, LOCK_SH);
7282 if (unlockerr && err == NULL)
7287 const struct got_error *
7288 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7289 struct got_object_id *commit_id, struct got_repository *repo)
7291 const struct got_error *err;
7292 char *commit_ref_name;
7294 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7298 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7302 err = delete_ref(commit_ref_name, repo);
7304 free(commit_ref_name);
7308 const struct got_error *
7309 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7310 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7311 struct got_worktree *worktree, const char *refname,
7312 struct got_repository *repo)
7314 const struct got_error *err = NULL;
7315 char *fileindex_path = NULL;
7316 struct check_rebase_ok_arg ok_arg;
7320 *base_branch_ref = NULL;
7322 err = lock_worktree(worktree, LOCK_EX);
7326 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7327 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7328 "cannot integrate a branch into itself; "
7329 "update -b or different branch name required");
7333 err = open_fileindex(fileindex, &fileindex_path, worktree);
7337 /* Preconditions are the same as for rebase. */
7338 ok_arg.worktree = worktree;
7340 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7345 err = got_ref_open(branch_ref, repo, refname, 1);
7349 err = got_ref_open(base_branch_ref, repo,
7350 got_worktree_get_head_ref_name(worktree), 1);
7354 got_ref_close(*branch_ref);
7357 if (*base_branch_ref) {
7358 got_ref_close(*base_branch_ref);
7359 *base_branch_ref = NULL;
7362 got_fileindex_free(*fileindex);
7365 lock_worktree(worktree, LOCK_SH);
7370 const struct got_error *
7371 got_worktree_integrate_continue(struct got_worktree *worktree,
7372 struct got_fileindex *fileindex, struct got_repository *repo,
7373 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7374 got_worktree_checkout_cb progress_cb, void *progress_arg,
7375 got_cancel_cb cancel_cb, void *cancel_arg)
7377 const struct got_error *err = NULL, *sync_err, *unlockerr;
7378 char *fileindex_path = NULL;
7379 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7380 struct got_commit_object *commit = NULL;
7382 err = get_fileindex_path(&fileindex_path, worktree);
7386 err = got_ref_resolve(&commit_id, repo, branch_ref);
7390 err = got_object_open_as_commit(&commit, repo, commit_id);
7394 err = got_object_id_by_path(&tree_id, repo, commit,
7395 worktree->path_prefix);
7399 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7403 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7404 progress_cb, progress_arg, cancel_cb, cancel_arg);
7408 err = got_ref_change_ref(base_branch_ref, commit_id);
7412 err = got_ref_write(base_branch_ref, repo);
7416 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7418 sync_err = sync_fileindex(fileindex, fileindex_path);
7419 if (sync_err && err == NULL)
7423 unlockerr = got_ref_unlock(branch_ref);
7424 if (unlockerr && err == NULL)
7426 got_ref_close(branch_ref);
7428 unlockerr = got_ref_unlock(base_branch_ref);
7429 if (unlockerr && err == NULL)
7431 got_ref_close(base_branch_ref);
7433 got_fileindex_free(fileindex);
7434 free(fileindex_path);
7437 got_object_commit_close(commit);
7439 unlockerr = lock_worktree(worktree, LOCK_SH);
7440 if (unlockerr && err == NULL)
7445 const struct got_error *
7446 got_worktree_integrate_abort(struct got_worktree *worktree,
7447 struct got_fileindex *fileindex, struct got_repository *repo,
7448 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7450 const struct got_error *err = NULL, *unlockerr = NULL;
7452 got_fileindex_free(fileindex);
7454 err = lock_worktree(worktree, LOCK_SH);
7456 unlockerr = got_ref_unlock(branch_ref);
7457 if (unlockerr && err == NULL)
7459 got_ref_close(branch_ref);
7461 unlockerr = got_ref_unlock(base_branch_ref);
7462 if (unlockerr && err == NULL)
7464 got_ref_close(base_branch_ref);
7469 const struct got_error *
7470 got_worktree_merge_postpone(struct got_worktree *worktree,
7471 struct got_fileindex *fileindex)
7473 const struct got_error *err, *sync_err;
7474 char *fileindex_path = NULL;
7476 err = get_fileindex_path(&fileindex_path, worktree);
7480 sync_err = sync_fileindex(fileindex, fileindex_path);
7482 err = lock_worktree(worktree, LOCK_SH);
7483 if (sync_err && err == NULL)
7486 got_fileindex_free(fileindex);
7487 free(fileindex_path);
7491 static const struct got_error *
7492 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7494 const struct got_error *err;
7495 char *branch_refname = NULL, *commit_refname = NULL;
7497 err = get_merge_branch_ref_name(&branch_refname, worktree);
7500 err = delete_ref(branch_refname, repo);
7504 err = get_merge_commit_ref_name(&commit_refname, worktree);
7507 err = delete_ref(commit_refname, repo);
7512 free(branch_refname);
7513 free(commit_refname);
7517 struct merge_commit_msg_arg {
7518 struct got_worktree *worktree;
7519 const char *branch_name;
7522 static const struct got_error *
7523 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7526 struct merge_commit_msg_arg *a = arg;
7528 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7529 got_worktree_get_head_ref_name(a->worktree)) == -1)
7530 return got_error_from_errno("asprintf");
7536 const struct got_error *
7537 got_worktree_merge_branch(struct got_worktree *worktree,
7538 struct got_fileindex *fileindex,
7539 struct got_object_id *yca_commit_id,
7540 struct got_object_id *branch_tip,
7541 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7542 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7544 const struct got_error *err;
7545 char *fileindex_path = NULL;
7547 err = get_fileindex_path(&fileindex_path, worktree);
7551 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7556 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7557 branch_tip, repo, progress_cb, progress_arg,
7558 cancel_cb, cancel_arg);
7560 free(fileindex_path);
7564 const struct got_error *
7565 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7566 struct got_worktree *worktree, struct got_fileindex *fileindex,
7567 const char *author, const char *committer, int allow_bad_symlinks,
7568 struct got_object_id *branch_tip, const char *branch_name,
7569 struct got_repository *repo,
7570 got_worktree_status_cb status_cb, void *status_arg)
7573 const struct got_error *err = NULL, *sync_err;
7574 struct got_pathlist_head commitable_paths;
7575 struct collect_commitables_arg cc_arg;
7576 struct got_pathlist_entry *pe;
7577 struct got_reference *head_ref = NULL;
7578 struct got_object_id *head_commit_id = NULL;
7579 int have_staged_files = 0;
7580 struct merge_commit_msg_arg mcm_arg;
7581 char *fileindex_path = NULL;
7583 *new_commit_id = NULL;
7585 TAILQ_INIT(&commitable_paths);
7587 err = get_fileindex_path(&fileindex_path, worktree);
7591 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7595 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7599 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7600 &have_staged_files);
7601 if (err && err->code != GOT_ERR_CANCELLED)
7603 if (have_staged_files) {
7604 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7608 cc_arg.commitable_paths = &commitable_paths;
7609 cc_arg.worktree = worktree;
7610 cc_arg.fileindex = fileindex;
7612 cc_arg.have_staged_files = have_staged_files;
7613 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7614 err = worktree_status(worktree, "", fileindex, repo,
7615 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7619 if (TAILQ_EMPTY(&commitable_paths)) {
7620 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7621 "merge of %s cannot proceed", branch_name);
7625 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7626 struct got_commitable *ct = pe->data;
7627 const char *ct_path = ct->in_repo_path;
7629 while (ct_path[0] == '/')
7631 err = check_out_of_date(ct_path, ct->status,
7632 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7633 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7639 mcm_arg.worktree = worktree;
7640 mcm_arg.branch_name = branch_name;
7641 err = commit_worktree(new_commit_id, &commitable_paths,
7642 head_commit_id, branch_tip, worktree, author, committer,
7643 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7647 err = update_fileindex_after_commit(worktree, &commitable_paths,
7648 *new_commit_id, fileindex, have_staged_files);
7649 sync_err = sync_fileindex(fileindex, fileindex_path);
7650 if (sync_err && err == NULL)
7653 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7654 struct got_commitable *ct = pe->data;
7655 free_commitable(ct);
7657 got_pathlist_free(&commitable_paths);
7658 free(fileindex_path);
7662 const struct got_error *
7663 got_worktree_merge_complete(struct got_worktree *worktree,
7664 struct got_fileindex *fileindex, struct got_repository *repo)
7666 const struct got_error *err, *unlockerr, *sync_err;
7667 char *fileindex_path = NULL;
7669 err = delete_merge_refs(worktree, repo);
7673 err = get_fileindex_path(&fileindex_path, worktree);
7676 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7677 sync_err = sync_fileindex(fileindex, fileindex_path);
7678 if (sync_err && err == NULL)
7681 got_fileindex_free(fileindex);
7682 free(fileindex_path);
7683 unlockerr = lock_worktree(worktree, LOCK_SH);
7684 if (unlockerr && err == NULL)
7689 const struct got_error *
7690 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7691 struct got_repository *repo)
7693 const struct got_error *err;
7694 char *branch_refname = NULL;
7695 struct got_reference *branch_ref = NULL;
7699 err = get_merge_branch_ref_name(&branch_refname, worktree);
7702 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7703 free(branch_refname);
7705 if (err->code != GOT_ERR_NOT_REF)
7713 const struct got_error *got_worktree_merge_prepare(
7714 struct got_fileindex **fileindex, struct got_worktree *worktree,
7715 struct got_reference *branch, struct got_repository *repo)
7717 const struct got_error *err = NULL;
7718 char *fileindex_path = NULL;
7719 char *branch_refname = NULL, *commit_refname = NULL;
7720 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7721 struct got_reference *commit_ref = NULL;
7722 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7723 struct check_rebase_ok_arg ok_arg;
7727 err = lock_worktree(worktree, LOCK_EX);
7731 err = open_fileindex(fileindex, &fileindex_path, worktree);
7735 /* Preconditions are the same as for rebase. */
7736 ok_arg.worktree = worktree;
7738 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7743 err = get_merge_branch_ref_name(&branch_refname, worktree);
7747 err = get_merge_commit_ref_name(&commit_refname, worktree);
7751 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7756 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7760 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7761 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7765 err = got_ref_resolve(&branch_tip, repo, branch);
7769 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7772 err = got_ref_write(branch_ref, repo);
7776 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7779 err = got_ref_write(commit_ref, repo);
7784 free(branch_refname);
7785 free(commit_refname);
7786 free(fileindex_path);
7788 got_ref_close(branch_ref);
7790 got_ref_close(commit_ref);
7792 got_ref_close(wt_branch);
7793 free(wt_branch_tip);
7796 got_fileindex_free(*fileindex);
7799 lock_worktree(worktree, LOCK_SH);
7804 const struct got_error *
7805 got_worktree_merge_continue(char **branch_name,
7806 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7807 struct got_worktree *worktree, struct got_repository *repo)
7809 const struct got_error *err;
7810 char *commit_refname = NULL, *branch_refname = NULL;
7811 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7812 char *fileindex_path = NULL;
7813 int have_staged_files = 0;
7815 *branch_name = NULL;
7819 err = lock_worktree(worktree, LOCK_EX);
7823 err = open_fileindex(fileindex, &fileindex_path, worktree);
7827 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7828 &have_staged_files);
7829 if (err && err->code != GOT_ERR_CANCELLED)
7831 if (have_staged_files) {
7832 err = got_error(GOT_ERR_STAGED_PATHS);
7836 err = get_merge_branch_ref_name(&branch_refname, worktree);
7840 err = get_merge_commit_ref_name(&commit_refname, worktree);
7844 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7848 if (!got_ref_is_symbolic(branch_ref)) {
7849 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7850 "%s is not a symbolic reference",
7851 got_ref_get_name(branch_ref));
7854 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7855 if (*branch_name == NULL) {
7856 err = got_error_from_errno("strdup");
7860 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7864 err = got_ref_resolve(branch_tip, repo, commit_ref);
7868 free(commit_refname);
7869 free(branch_refname);
7870 free(fileindex_path);
7872 got_ref_close(commit_ref);
7874 got_ref_close(branch_ref);
7878 *branch_name = NULL;
7883 got_fileindex_free(*fileindex);
7886 lock_worktree(worktree, LOCK_SH);
7891 const struct got_error *
7892 got_worktree_merge_abort(struct got_worktree *worktree,
7893 struct got_fileindex *fileindex, struct got_repository *repo,
7894 got_worktree_checkout_cb progress_cb, void *progress_arg)
7896 const struct got_error *err, *unlockerr, *sync_err;
7897 struct got_object_id *commit_id = NULL;
7898 struct got_commit_object *commit = NULL;
7899 char *fileindex_path = NULL;
7900 struct revert_file_args rfa;
7901 struct got_object_id *tree_id = NULL;
7903 err = got_object_open_as_commit(&commit, repo,
7904 worktree->base_commit_id);
7908 err = got_object_id_by_path(&tree_id, repo, commit,
7909 worktree->path_prefix);
7913 err = delete_merge_refs(worktree, repo);
7917 err = get_fileindex_path(&fileindex_path, worktree);
7921 rfa.worktree = worktree;
7922 rfa.fileindex = fileindex;
7923 rfa.progress_cb = progress_cb;
7924 rfa.progress_arg = progress_arg;
7925 rfa.patch_cb = NULL;
7926 rfa.patch_arg = NULL;
7928 rfa.unlink_added_files = 1;
7929 err = worktree_status(worktree, "", fileindex, repo,
7930 revert_file, &rfa, NULL, NULL, 1, 0);
7934 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7935 repo, progress_cb, progress_arg, NULL, NULL);
7937 sync_err = sync_fileindex(fileindex, fileindex_path);
7938 if (sync_err && err == NULL)
7944 got_object_commit_close(commit);
7946 got_fileindex_free(fileindex);
7947 free(fileindex_path);
7949 unlockerr = lock_worktree(worktree, LOCK_SH);
7950 if (unlockerr && err == NULL)
7955 struct check_stage_ok_arg {
7956 struct got_object_id *head_commit_id;
7957 struct got_worktree *worktree;
7958 struct got_fileindex *fileindex;
7959 struct got_repository *repo;
7963 static const struct got_error *
7964 check_stage_ok(void *arg, unsigned char status,
7965 unsigned char staged_status, const char *relpath,
7966 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7967 struct got_object_id *commit_id, int dirfd, const char *de_name)
7969 struct check_stage_ok_arg *a = arg;
7970 const struct got_error *err = NULL;
7971 struct got_fileindex_entry *ie;
7972 struct got_object_id base_commit_id;
7973 struct got_object_id *base_commit_idp = NULL;
7974 char *in_repo_path = NULL, *p;
7976 if (status == GOT_STATUS_UNVERSIONED ||
7977 status == GOT_STATUS_NO_CHANGE)
7979 if (status == GOT_STATUS_NONEXISTENT)
7980 return got_error_set_errno(ENOENT, relpath);
7982 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7984 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7986 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7987 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7989 return got_error_from_errno("asprintf");
7991 if (got_fileindex_entry_has_commit(ie)) {
7992 memcpy(base_commit_id.sha1, ie->commit_sha1,
7993 SHA1_DIGEST_LENGTH);
7994 base_commit_idp = &base_commit_id;
7997 if (status == GOT_STATUS_CONFLICT) {
7998 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8000 } else if (status != GOT_STATUS_ADD &&
8001 status != GOT_STATUS_MODIFY &&
8002 status != GOT_STATUS_DELETE) {
8003 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8007 a->have_changes = 1;
8012 err = check_out_of_date(p, status, staged_status,
8013 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8014 GOT_ERR_STAGE_OUT_OF_DATE);
8020 struct stage_path_arg {
8021 struct got_worktree *worktree;
8022 struct got_fileindex *fileindex;
8023 struct got_repository *repo;
8024 got_worktree_status_cb status_cb;
8026 got_worktree_patch_cb patch_cb;
8028 int staged_something;
8029 int allow_bad_symlinks;
8032 static const struct got_error *
8033 stage_path(void *arg, unsigned char status,
8034 unsigned char staged_status, const char *relpath,
8035 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8036 struct got_object_id *commit_id, int dirfd, const char *de_name)
8038 struct stage_path_arg *a = arg;
8039 const struct got_error *err = NULL;
8040 struct got_fileindex_entry *ie;
8041 char *ondisk_path = NULL, *path_content = NULL;
8043 struct got_object_id *new_staged_blob_id = NULL;
8046 if (status == GOT_STATUS_UNVERSIONED)
8049 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8051 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8053 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8055 return got_error_from_errno("asprintf");
8058 case GOT_STATUS_ADD:
8059 case GOT_STATUS_MODIFY:
8060 /* XXX could sb.st_mode be passed in by our caller? */
8061 if (lstat(ondisk_path, &sb) == -1) {
8062 err = got_error_from_errno2("lstat", ondisk_path);
8066 if (status == GOT_STATUS_ADD) {
8067 int choice = GOT_PATCH_CHOICE_NONE;
8068 err = (*a->patch_cb)(&choice, a->patch_arg,
8069 status, ie->path, NULL, 1, 1);
8072 if (choice != GOT_PATCH_CHOICE_YES)
8075 err = create_patched_content(&path_content, 0,
8076 staged_blob_id ? staged_blob_id : blob_id,
8077 ondisk_path, dirfd, de_name, ie->path,
8078 a->repo, a->patch_cb, a->patch_arg);
8079 if (err || path_content == NULL)
8083 err = got_object_blob_create(&new_staged_blob_id,
8084 path_content ? path_content : ondisk_path, a->repo);
8087 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8088 SHA1_DIGEST_LENGTH);
8089 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8090 stage = GOT_FILEIDX_STAGE_ADD;
8092 stage = GOT_FILEIDX_STAGE_MODIFY;
8093 got_fileindex_entry_stage_set(ie, stage);
8094 if (S_ISLNK(sb.st_mode)) {
8095 int is_bad_symlink = 0;
8096 if (!a->allow_bad_symlinks) {
8097 char target_path[PATH_MAX];
8099 target_len = readlink(ondisk_path, target_path,
8100 sizeof(target_path));
8101 if (target_len == -1) {
8102 err = got_error_from_errno2("readlink",
8106 err = is_bad_symlink_target(&is_bad_symlink,
8107 target_path, target_len, ondisk_path,
8108 a->worktree->root_path);
8111 if (is_bad_symlink) {
8112 err = got_error_path(ondisk_path,
8113 GOT_ERR_BAD_SYMLINK);
8118 got_fileindex_entry_staged_filetype_set(ie,
8119 GOT_FILEIDX_MODE_BAD_SYMLINK);
8121 got_fileindex_entry_staged_filetype_set(ie,
8122 GOT_FILEIDX_MODE_SYMLINK);
8124 got_fileindex_entry_staged_filetype_set(ie,
8125 GOT_FILEIDX_MODE_REGULAR_FILE);
8127 a->staged_something = 1;
8128 if (a->status_cb == NULL)
8130 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8131 get_staged_status(ie), relpath, blob_id,
8132 new_staged_blob_id, NULL, dirfd, de_name);
8136 * When staging the reverse of the staged diff,
8137 * implicitly unstage the file.
8139 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8140 sizeof(ie->blob_sha1)) == 0) {
8141 got_fileindex_entry_stage_set(ie,
8142 GOT_FILEIDX_STAGE_NONE);
8145 case GOT_STATUS_DELETE:
8146 if (staged_status == GOT_STATUS_DELETE)
8149 int choice = GOT_PATCH_CHOICE_NONE;
8150 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8151 ie->path, NULL, 1, 1);
8154 if (choice == GOT_PATCH_CHOICE_NO)
8156 if (choice != GOT_PATCH_CHOICE_YES) {
8157 err = got_error(GOT_ERR_PATCH_CHOICE);
8161 stage = GOT_FILEIDX_STAGE_DELETE;
8162 got_fileindex_entry_stage_set(ie, stage);
8163 a->staged_something = 1;
8164 if (a->status_cb == NULL)
8166 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8167 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8170 case GOT_STATUS_NO_CHANGE:
8172 case GOT_STATUS_CONFLICT:
8173 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8175 case GOT_STATUS_NONEXISTENT:
8176 err = got_error_set_errno(ENOENT, relpath);
8179 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8183 if (path_content && unlink(path_content) == -1 && err == NULL)
8184 err = got_error_from_errno2("unlink", path_content);
8187 free(new_staged_blob_id);
8191 const struct got_error *
8192 got_worktree_stage(struct got_worktree *worktree,
8193 struct got_pathlist_head *paths,
8194 got_worktree_status_cb status_cb, void *status_arg,
8195 got_worktree_patch_cb patch_cb, void *patch_arg,
8196 int allow_bad_symlinks, struct got_repository *repo)
8198 const struct got_error *err = NULL, *sync_err, *unlockerr;
8199 struct got_pathlist_entry *pe;
8200 struct got_fileindex *fileindex = NULL;
8201 char *fileindex_path = NULL;
8202 struct got_reference *head_ref = NULL;
8203 struct got_object_id *head_commit_id = NULL;
8204 struct check_stage_ok_arg oka;
8205 struct stage_path_arg spa;
8207 err = lock_worktree(worktree, LOCK_EX);
8211 err = got_ref_open(&head_ref, repo,
8212 got_worktree_get_head_ref_name(worktree), 0);
8215 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8218 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8222 /* Check pre-conditions before staging anything. */
8223 oka.head_commit_id = head_commit_id;
8224 oka.worktree = worktree;
8225 oka.fileindex = fileindex;
8227 oka.have_changes = 0;
8228 TAILQ_FOREACH(pe, paths, entry) {
8229 err = worktree_status(worktree, pe->path, fileindex, repo,
8230 check_stage_ok, &oka, NULL, NULL, 1, 0);
8234 if (!oka.have_changes) {
8235 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8239 spa.worktree = worktree;
8240 spa.fileindex = fileindex;
8242 spa.patch_cb = patch_cb;
8243 spa.patch_arg = patch_arg;
8244 spa.status_cb = status_cb;
8245 spa.status_arg = status_arg;
8246 spa.staged_something = 0;
8247 spa.allow_bad_symlinks = allow_bad_symlinks;
8248 TAILQ_FOREACH(pe, paths, entry) {
8249 err = worktree_status(worktree, pe->path, fileindex, repo,
8250 stage_path, &spa, NULL, NULL, 1, 0);
8254 if (!spa.staged_something) {
8255 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8259 sync_err = sync_fileindex(fileindex, fileindex_path);
8260 if (sync_err && err == NULL)
8264 got_ref_close(head_ref);
8265 free(head_commit_id);
8266 free(fileindex_path);
8268 got_fileindex_free(fileindex);
8269 unlockerr = lock_worktree(worktree, LOCK_SH);
8270 if (unlockerr && err == NULL)
8275 struct unstage_path_arg {
8276 struct got_worktree *worktree;
8277 struct got_fileindex *fileindex;
8278 struct got_repository *repo;
8279 got_worktree_checkout_cb progress_cb;
8281 got_worktree_patch_cb patch_cb;
8285 static const struct got_error *
8286 create_unstaged_content(char **path_unstaged_content,
8287 char **path_new_staged_content, struct got_object_id *blob_id,
8288 struct got_object_id *staged_blob_id, const char *relpath,
8289 struct got_repository *repo,
8290 got_worktree_patch_cb patch_cb, void *patch_arg)
8292 const struct got_error *err, *free_err;
8293 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8294 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8295 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8296 struct got_diffreg_result *diffreg_result = NULL;
8297 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8298 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8299 int fd1 = -1, fd2 = -1;
8301 *path_unstaged_content = NULL;
8302 *path_new_staged_content = NULL;
8304 err = got_object_id_str(&label1, blob_id);
8308 fd1 = got_opentempfd();
8310 err = got_error_from_errno("got_opentempfd");
8313 fd2 = got_opentempfd();
8315 err = got_error_from_errno("got_opentempfd");
8319 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8323 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8327 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8331 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8336 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8340 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8344 err = got_diff_files(&diffreg_result, f1, label1, f2,
8345 path2, 3, 0, 1, NULL);
8349 err = got_opentemp_named(path_unstaged_content, &outfile,
8350 "got-unstaged-content");
8353 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8354 "got-new-staged-content");
8358 if (fseek(f1, 0L, SEEK_SET) == -1) {
8359 err = got_ferror(f1, GOT_ERR_IO);
8362 if (fseek(f2, 0L, SEEK_SET) == -1) {
8363 err = got_ferror(f2, GOT_ERR_IO);
8366 /* Count the number of actual changes in the diff result. */
8367 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8368 struct diff_chunk_context cc = {};
8369 diff_chunk_context_load_change(&cc, &nchunks_used,
8370 diffreg_result->result, n, 0);
8373 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8375 err = apply_or_reject_change(&choice, &nchunks_used,
8376 diffreg_result->result, n, relpath, f1, f2,
8377 &line_cur1, &line_cur2,
8378 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8381 if (choice == GOT_PATCH_CHOICE_YES)
8384 have_rejected_content = 1;
8385 if (choice == GOT_PATCH_CHOICE_QUIT)
8388 if (have_content || have_rejected_content)
8389 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8390 outfile, rejectfile);
8393 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8394 err = got_error_from_errno("close");
8396 got_object_blob_close(blob);
8397 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8398 err = got_error_from_errno("close");
8400 got_object_blob_close(staged_blob);
8401 free_err = got_diffreg_result_free(diffreg_result);
8402 if (free_err && err == NULL)
8404 if (f1 && fclose(f1) == EOF && err == NULL)
8405 err = got_error_from_errno2("fclose", path1);
8406 if (f2 && fclose(f2) == EOF && err == NULL)
8407 err = got_error_from_errno2("fclose", path2);
8408 if (outfile && fclose(outfile) == EOF && err == NULL)
8409 err = got_error_from_errno2("fclose", *path_unstaged_content);
8410 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8411 err = got_error_from_errno2("fclose", *path_new_staged_content);
8412 if (path1 && unlink(path1) == -1 && err == NULL)
8413 err = got_error_from_errno2("unlink", path1);
8414 if (path2 && unlink(path2) == -1 && err == NULL)
8415 err = got_error_from_errno2("unlink", path2);
8416 if (err || !have_content) {
8417 if (*path_unstaged_content &&
8418 unlink(*path_unstaged_content) == -1 && err == NULL)
8419 err = got_error_from_errno2("unlink",
8420 *path_unstaged_content);
8421 free(*path_unstaged_content);
8422 *path_unstaged_content = NULL;
8424 if (err || !have_content || !have_rejected_content) {
8425 if (*path_new_staged_content &&
8426 unlink(*path_new_staged_content) == -1 && err == NULL)
8427 err = got_error_from_errno2("unlink",
8428 *path_new_staged_content);
8429 free(*path_new_staged_content);
8430 *path_new_staged_content = NULL;
8437 static const struct got_error *
8438 unstage_hunks(struct got_object_id *staged_blob_id,
8439 struct got_blob_object *blob_base,
8440 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8441 const char *ondisk_path, const char *label_orig,
8442 struct got_worktree *worktree, struct got_repository *repo,
8443 got_worktree_patch_cb patch_cb, void *patch_arg,
8444 got_worktree_checkout_cb progress_cb, void *progress_arg)
8446 const struct got_error *err = NULL;
8447 char *path_unstaged_content = NULL;
8448 char *path_new_staged_content = NULL;
8449 char *parent = NULL, *base_path = NULL;
8450 char *blob_base_path = NULL;
8451 struct got_object_id *new_staged_blob_id = NULL;
8452 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8455 err = create_unstaged_content(&path_unstaged_content,
8456 &path_new_staged_content, blob_id, staged_blob_id,
8457 ie->path, repo, patch_cb, patch_arg);
8461 if (path_unstaged_content == NULL)
8464 if (path_new_staged_content) {
8465 err = got_object_blob_create(&new_staged_blob_id,
8466 path_new_staged_content, repo);
8471 f = fopen(path_unstaged_content, "re");
8473 err = got_error_from_errno2("fopen",
8474 path_unstaged_content);
8477 if (fstat(fileno(f), &sb) == -1) {
8478 err = got_error_from_errno2("fstat", path_unstaged_content);
8481 if (got_fileindex_entry_staged_filetype_get(ie) ==
8482 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8483 char link_target[PATH_MAX];
8485 r = fread(link_target, 1, sizeof(link_target), f);
8486 if (r == 0 && ferror(f)) {
8487 err = got_error_from_errno("fread");
8490 if (r >= sizeof(link_target)) { /* should not happen */
8491 err = got_error(GOT_ERR_NO_SPACE);
8494 link_target[r] = '\0';
8495 err = merge_symlink(worktree, blob_base,
8496 ondisk_path, ie->path, label_orig, link_target,
8497 worktree->base_commit_id, repo, progress_cb,
8500 int local_changes_subsumed;
8502 err = got_path_dirname(&parent, ondisk_path);
8506 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8508 err = got_error_from_errno("asprintf");
8513 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8516 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8522 * In order the run a 3-way merge with a symlink we copy the symlink's
8523 * target path into a temporary file and use that file with diff3.
8525 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8526 err = dump_symlink_target_path_to_file(&f_deriv2,
8532 fd = open(ondisk_path,
8533 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8535 err = got_error_from_errno2("open", ondisk_path);
8538 f_deriv2 = fdopen(fd, "r");
8539 if (f_deriv2 == NULL) {
8540 err = got_error_from_errno2("fdopen", ondisk_path);
8546 err = merge_file(&local_changes_subsumed, worktree,
8547 f_base, f, f_deriv2, ondisk_path, ie->path,
8548 got_fileindex_perms_to_st(ie),
8549 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8550 repo, progress_cb, progress_arg);
8555 if (new_staged_blob_id) {
8556 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8557 SHA1_DIGEST_LENGTH);
8559 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8560 got_fileindex_entry_staged_filetype_set(ie, 0);
8563 free(new_staged_blob_id);
8564 if (path_unstaged_content &&
8565 unlink(path_unstaged_content) == -1 && err == NULL)
8566 err = got_error_from_errno2("unlink", path_unstaged_content);
8567 if (path_new_staged_content &&
8568 unlink(path_new_staged_content) == -1 && err == NULL)
8569 err = got_error_from_errno2("unlink", path_new_staged_content);
8570 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8571 err = got_error_from_errno2("unlink", blob_base_path);
8572 if (f_base && fclose(f_base) == EOF && err == NULL)
8573 err = got_error_from_errno2("fclose", path_unstaged_content);
8574 if (f && fclose(f) == EOF && err == NULL)
8575 err = got_error_from_errno2("fclose", path_unstaged_content);
8576 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8577 err = got_error_from_errno2("fclose", ondisk_path);
8578 free(path_unstaged_content);
8579 free(path_new_staged_content);
8580 free(blob_base_path);
8586 static const struct got_error *
8587 unstage_path(void *arg, unsigned char status,
8588 unsigned char staged_status, const char *relpath,
8589 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8590 struct got_object_id *commit_id, int dirfd, const char *de_name)
8592 const struct got_error *err = NULL;
8593 struct unstage_path_arg *a = arg;
8594 struct got_fileindex_entry *ie;
8595 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8596 char *ondisk_path = NULL;
8597 char *id_str = NULL, *label_orig = NULL;
8598 int local_changes_subsumed;
8600 int fd1 = -1, fd2 = -1;
8602 if (staged_status != GOT_STATUS_ADD &&
8603 staged_status != GOT_STATUS_MODIFY &&
8604 staged_status != GOT_STATUS_DELETE)
8607 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8609 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8611 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8613 return got_error_from_errno("asprintf");
8615 err = got_object_id_str(&id_str,
8616 commit_id ? commit_id : a->worktree->base_commit_id);
8619 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8621 err = got_error_from_errno("asprintf");
8625 fd1 = got_opentempfd();
8627 err = got_error_from_errno("got_opentempfd");
8630 fd2 = got_opentempfd();
8632 err = got_error_from_errno("got_opentempfd");
8636 switch (staged_status) {
8637 case GOT_STATUS_MODIFY:
8638 err = got_object_open_as_blob(&blob_base, a->repo,
8639 blob_id, 8192, fd1);
8643 case GOT_STATUS_ADD:
8645 if (staged_status == GOT_STATUS_ADD) {
8646 int choice = GOT_PATCH_CHOICE_NONE;
8647 err = (*a->patch_cb)(&choice, a->patch_arg,
8648 staged_status, ie->path, NULL, 1, 1);
8651 if (choice != GOT_PATCH_CHOICE_YES)
8654 err = unstage_hunks(staged_blob_id,
8655 blob_base, blob_id, ie, ondisk_path,
8656 label_orig, a->worktree, a->repo,
8657 a->patch_cb, a->patch_arg,
8658 a->progress_cb, a->progress_arg);
8659 break; /* Done with this file. */
8662 err = got_object_open_as_blob(&blob_staged, a->repo,
8663 staged_blob_id, 8192, fd2);
8666 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8667 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8668 case GOT_FILEIDX_MODE_REGULAR_FILE:
8669 err = merge_blob(&local_changes_subsumed, a->worktree,
8670 blob_base, ondisk_path, relpath,
8671 got_fileindex_perms_to_st(ie), label_orig,
8672 blob_staged, commit_id ? commit_id :
8673 a->worktree->base_commit_id, a->repo,
8674 a->progress_cb, a->progress_arg);
8676 case GOT_FILEIDX_MODE_SYMLINK:
8677 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8678 char *staged_target;
8679 err = got_object_blob_read_to_str(
8680 &staged_target, blob_staged);
8683 err = merge_symlink(a->worktree, blob_base,
8684 ondisk_path, relpath, label_orig,
8685 staged_target, commit_id ? commit_id :
8686 a->worktree->base_commit_id,
8687 a->repo, a->progress_cb, a->progress_arg);
8688 free(staged_target);
8690 err = merge_blob(&local_changes_subsumed,
8691 a->worktree, blob_base, ondisk_path,
8692 relpath, got_fileindex_perms_to_st(ie),
8693 label_orig, blob_staged,
8694 commit_id ? commit_id :
8695 a->worktree->base_commit_id, a->repo,
8696 a->progress_cb, a->progress_arg);
8700 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8704 got_fileindex_entry_stage_set(ie,
8705 GOT_FILEIDX_STAGE_NONE);
8706 got_fileindex_entry_staged_filetype_set(ie, 0);
8709 case GOT_STATUS_DELETE:
8711 int choice = GOT_PATCH_CHOICE_NONE;
8712 err = (*a->patch_cb)(&choice, a->patch_arg,
8713 staged_status, ie->path, NULL, 1, 1);
8716 if (choice == GOT_PATCH_CHOICE_NO)
8718 if (choice != GOT_PATCH_CHOICE_YES) {
8719 err = got_error(GOT_ERR_PATCH_CHOICE);
8723 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8724 got_fileindex_entry_staged_filetype_set(ie, 0);
8725 err = get_file_status(&status, &sb, ie, ondisk_path,
8726 dirfd, de_name, a->repo);
8729 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8734 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8735 err = got_error_from_errno("close");
8737 got_object_blob_close(blob_base);
8738 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8739 err = got_error_from_errno("close");
8741 got_object_blob_close(blob_staged);
8747 const struct got_error *
8748 got_worktree_unstage(struct got_worktree *worktree,
8749 struct got_pathlist_head *paths,
8750 got_worktree_checkout_cb progress_cb, void *progress_arg,
8751 got_worktree_patch_cb patch_cb, void *patch_arg,
8752 struct got_repository *repo)
8754 const struct got_error *err = NULL, *sync_err, *unlockerr;
8755 struct got_pathlist_entry *pe;
8756 struct got_fileindex *fileindex = NULL;
8757 char *fileindex_path = NULL;
8758 struct unstage_path_arg upa;
8760 err = lock_worktree(worktree, LOCK_EX);
8764 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8768 upa.worktree = worktree;
8769 upa.fileindex = fileindex;
8771 upa.progress_cb = progress_cb;
8772 upa.progress_arg = progress_arg;
8773 upa.patch_cb = patch_cb;
8774 upa.patch_arg = patch_arg;
8775 TAILQ_FOREACH(pe, paths, entry) {
8776 err = worktree_status(worktree, pe->path, fileindex, repo,
8777 unstage_path, &upa, NULL, NULL, 1, 0);
8782 sync_err = sync_fileindex(fileindex, fileindex_path);
8783 if (sync_err && err == NULL)
8786 free(fileindex_path);
8788 got_fileindex_free(fileindex);
8789 unlockerr = lock_worktree(worktree, LOCK_SH);
8790 if (unlockerr && err == NULL)
8795 struct report_file_info_arg {
8796 struct got_worktree *worktree;
8797 got_worktree_path_info_cb info_cb;
8799 struct got_pathlist_head *paths;
8800 got_cancel_cb cancel_cb;
8804 static const struct got_error *
8805 report_file_info(void *arg, struct got_fileindex_entry *ie)
8807 struct report_file_info_arg *a = arg;
8808 struct got_pathlist_entry *pe;
8809 struct got_object_id blob_id, staged_blob_id, commit_id;
8810 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8811 struct got_object_id *commit_idp = NULL;
8814 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8815 return got_error(GOT_ERR_CANCELLED);
8817 TAILQ_FOREACH(pe, a->paths, entry) {
8818 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8819 got_path_is_child(ie->path, pe->path, pe->path_len))
8822 if (pe == NULL) /* not found */
8825 if (got_fileindex_entry_has_blob(ie)) {
8826 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8827 blob_idp = &blob_id;
8829 stage = got_fileindex_entry_stage_get(ie);
8830 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8831 stage == GOT_FILEIDX_STAGE_ADD) {
8832 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8833 SHA1_DIGEST_LENGTH);
8834 staged_blob_idp = &staged_blob_id;
8837 if (got_fileindex_entry_has_commit(ie)) {
8838 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8839 commit_idp = &commit_id;
8842 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8843 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8846 const struct got_error *
8847 got_worktree_path_info(struct got_worktree *worktree,
8848 struct got_pathlist_head *paths,
8849 got_worktree_path_info_cb info_cb, void *info_arg,
8850 got_cancel_cb cancel_cb, void *cancel_arg)
8853 const struct got_error *err = NULL, *unlockerr;
8854 struct got_fileindex *fileindex = NULL;
8855 char *fileindex_path = NULL;
8856 struct report_file_info_arg arg;
8858 err = lock_worktree(worktree, LOCK_SH);
8862 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8866 arg.worktree = worktree;
8867 arg.info_cb = info_cb;
8868 arg.info_arg = info_arg;
8870 arg.cancel_cb = cancel_cb;
8871 arg.cancel_arg = cancel_arg;
8872 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8875 free(fileindex_path);
8877 got_fileindex_free(fileindex);
8878 unlockerr = lock_worktree(worktree, LOCK_UN);
8879 if (unlockerr && err == NULL)
8884 static const struct got_error *
8885 patch_check_path(const char *p, char **path, unsigned char *status,
8886 unsigned char *staged_status, struct got_fileindex *fileindex,
8887 struct got_worktree *worktree, struct got_repository *repo)
8889 const struct got_error *err;
8890 struct got_fileindex_entry *ie;
8892 char *ondisk_path = NULL;
8894 err = got_worktree_resolve_path(path, worktree, p);
8898 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8899 *path[0] ? "/" : "", *path) == -1)
8900 return got_error_from_errno("asprintf");
8902 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8904 *staged_status = get_staged_status(ie);
8905 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8910 *staged_status = GOT_STATUS_NO_CHANGE;
8911 *status = GOT_STATUS_UNVERSIONED;
8912 if (lstat(ondisk_path, &sb) == -1) {
8913 if (errno != ENOENT) {
8914 err = got_error_from_errno2("lstat",
8918 *status = GOT_STATUS_NONEXISTENT;
8927 static const struct got_error *
8928 patch_can_rm(const char *path, unsigned char status,
8929 unsigned char staged_status)
8931 if (status == GOT_STATUS_NONEXISTENT)
8932 return got_error_set_errno(ENOENT, path);
8933 if (status != GOT_STATUS_NO_CHANGE &&
8934 status != GOT_STATUS_ADD &&
8935 status != GOT_STATUS_MODIFY &&
8936 status != GOT_STATUS_MODE_CHANGE)
8937 return got_error_path(path, GOT_ERR_FILE_STATUS);
8938 if (staged_status == GOT_STATUS_DELETE)
8939 return got_error_path(path, GOT_ERR_FILE_STATUS);
8943 static const struct got_error *
8944 patch_can_add(const char *path, unsigned char status)
8946 if (status != GOT_STATUS_NONEXISTENT)
8947 return got_error_path(path, GOT_ERR_FILE_STATUS);
8951 static const struct got_error *
8952 patch_can_edit(const char *path, unsigned char status,
8953 unsigned char staged_status)
8955 if (status == GOT_STATUS_NONEXISTENT)
8956 return got_error_set_errno(ENOENT, path);
8957 if (status != GOT_STATUS_NO_CHANGE &&
8958 status != GOT_STATUS_ADD &&
8959 status != GOT_STATUS_MODIFY)
8960 return got_error_path(path, GOT_ERR_FILE_STATUS);
8961 if (staged_status == GOT_STATUS_DELETE)
8962 return got_error_path(path, GOT_ERR_FILE_STATUS);
8966 const struct got_error *
8967 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8968 char **fileindex_path, struct got_worktree *worktree)
8970 return open_fileindex(fileindex, fileindex_path, worktree);
8973 const struct got_error *
8974 got_worktree_patch_check_path(const char *old, const char *new,
8975 char **oldpath, char **newpath, struct got_worktree *worktree,
8976 struct got_repository *repo, struct got_fileindex *fileindex)
8978 const struct got_error *err = NULL;
8979 int file_renamed = 0;
8980 unsigned char status_old, staged_status_old;
8981 unsigned char status_new, staged_status_new;
8986 err = patch_check_path(old != NULL ? old : new, oldpath,
8987 &status_old, &staged_status_old, fileindex, worktree, repo);
8991 err = patch_check_path(new != NULL ? new : old, newpath,
8992 &status_new, &staged_status_new, fileindex, worktree, repo);
8996 if (old != NULL && new != NULL && strcmp(old, new) != 0)
8999 if (old != NULL && new == NULL)
9000 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9001 else if (file_renamed) {
9002 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9004 err = patch_can_add(*newpath, status_new);
9005 } else if (old == NULL)
9006 err = patch_can_add(*newpath, status_new);
9008 err = patch_can_edit(*newpath, status_new, staged_status_new);
9020 const struct got_error *
9021 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9022 struct got_worktree *worktree, struct got_fileindex *fileindex,
9023 got_worktree_checkout_cb progress_cb, void *progress_arg)
9025 struct schedule_addition_args saa;
9027 memset(&saa, 0, sizeof(saa));
9028 saa.worktree = worktree;
9029 saa.fileindex = fileindex;
9030 saa.progress_cb = progress_cb;
9031 saa.progress_arg = progress_arg;
9034 return worktree_status(worktree, path, fileindex, repo,
9035 schedule_addition, &saa, NULL, NULL, 1, 0);
9038 const struct got_error *
9039 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9040 struct got_worktree *worktree, struct got_fileindex *fileindex,
9041 got_worktree_delete_cb progress_cb, void *progress_arg)
9043 struct schedule_deletion_args sda;
9045 memset(&sda, 0, sizeof(sda));
9046 sda.worktree = worktree;
9047 sda.fileindex = fileindex;
9048 sda.progress_cb = progress_cb;
9049 sda.progress_arg = progress_arg;
9051 sda.delete_local_mods = 0;
9052 sda.keep_on_disk = 0;
9053 sda.ignore_missing_paths = 0;
9054 sda.status_codes = NULL;
9056 return worktree_status(worktree, path, fileindex, repo,
9057 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9060 const struct got_error *
9061 got_worktree_patch_complete(struct got_fileindex *fileindex,
9062 const char *fileindex_path)
9064 const struct got_error *err = NULL;
9066 err = sync_fileindex(fileindex, fileindex_path);
9067 got_fileindex_free(fileindex);