2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
129 return got_error_from_errno("got_ref_to_str");
131 refstr = strdup(got_ref_get_name(head_ref));
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
147 uint32_t uuid_status;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
160 err = got_ref_resolve(&commit_id, repo, head_ref);
163 err = got_object_get_type(&obj_type, repo, commit_id);
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
301 err = write_head_ref(path_got, head_ref);
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
336 err = got_object_open(&obj, repo, commit_id);
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
361 got_object_close(obj);
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
413 size_t flen1 = 0, flen2 = 0;
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
432 } else if (flen2 == 0) {
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
466 if (size1 != size2) {
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
495 return got_error_from_errno("fread");
499 outlen = write(outfd, fbuf, flen);
501 return got_error_from_errno("write");
503 return got_error(GOT_ERR_IO);
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
587 err = got_error_from_errno("dprintf");
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
596 err = got_error_from_errno("dprintf");
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
672 if (err->code != GOT_ERR_FILE_BINARY)
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
706 if (fchmod(fileno(f_merged), st_mode) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
779 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
780 err = got_error_from_errno2("fchmod", path);
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
852 ondisk_target[ondisk_len] = '\0';
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
916 free(ancestor_target);
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
988 err = got_error_from_errno("asprintf");
993 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
996 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1003 * No common ancestor exists. This is an "add vs add" conflict
1004 * and we simply use an empty ancestor file to make both files
1005 * appear in the merged result in their entirety.
1007 f_orig = got_opentemp();
1008 if (f_orig == NULL) {
1009 err = got_error_from_errno("got_opentemp");
1014 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 err = got_error_from_errno("asprintf");
1020 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1023 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1028 err = got_object_id_str(&id_str, deriv_base_commit_id);
1031 if (asprintf(&label_deriv, "%s: commit %s",
1032 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 err = got_error_from_errno("asprintf");
1038 * In order the run a 3-way merge with a symlink we copy the symlink's
1039 * target path into a temporary file and use that file with diff3.
1041 if (S_ISLNK(st_mode)) {
1042 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1047 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 err = got_error_from_errno2("open", ondisk_path);
1052 f_deriv2 = fdopen(fd, "r");
1053 if (f_deriv2 == NULL) {
1054 err = got_error_from_errno2("fdopen", ondisk_path);
1060 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1071 if (blob_orig_path) {
1072 unlink(blob_orig_path);
1073 free(blob_orig_path);
1075 if (blob_deriv_path) {
1076 unlink(blob_deriv_path);
1077 free(blob_deriv_path);
1085 static const struct got_error *
1086 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 int wt_fd, const char *path, struct got_object_id *blob_id)
1090 const struct got_error *err = NULL;
1091 struct got_fileindex_entry *new_ie;
1095 err = got_fileindex_entry_alloc(&new_ie, path);
1099 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 blob_id->sha1, base_commit_id->sha1, 1);
1104 err = got_fileindex_entry_add(fileindex, new_ie);
1107 got_fileindex_entry_free(new_ie);
1114 get_ondisk_perms(int executable, mode_t st_mode)
1116 mode_t xbits = S_IXUSR;
1119 /* Map read bits to execute bits. */
1120 if (st_mode & S_IRGRP)
1122 if (st_mode & S_IROTH)
1124 return st_mode | xbits;
1127 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1130 /* forward declaration */
1131 static const struct got_error *
1132 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 const char *path, mode_t te_mode, mode_t st_mode,
1134 struct got_blob_object *blob, int restoring_missing_file,
1135 int reverting_versioned_file, int installing_bad_symlink,
1136 int path_is_unversioned, struct got_repository *repo,
1137 got_worktree_checkout_cb progress_cb, void *progress_arg);
1140 * This function assumes that the provided symlink target points at a
1141 * safe location in the work tree!
1143 static const struct got_error *
1144 replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 const char *target_path, size_t target_len)
1147 const struct got_error *err = NULL;
1149 char etarget[PATH_MAX];
1155 * "Bad" symlinks (those pointing outside the work tree or into the
1156 * .got directory) are installed in the work tree as a regular file
1157 * which contains the bad symlink target path.
1158 * The new symlink target has already been checked for safety by our
1159 * caller. If we can successfully open a regular file then we simply
1160 * replace this file with a symlink below.
1162 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1164 if (!got_err_open_nofollow_on_symlink())
1165 return got_error_from_errno2("open", ondisk_path);
1167 /* We are updating an existing on-disk symlink. */
1168 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1170 return got_error_from_errno2("readlink", ondisk_path);
1172 if (elen == target_len &&
1173 memcmp(etarget, target_path, target_len) == 0)
1174 return NULL; /* nothing to do */
1178 err = update_symlink(ondisk_path, target_path, target_len);
1179 if (fd != -1 && close(fd) == -1 && err == NULL)
1180 err = got_error_from_errno2("close", ondisk_path);
1184 static const struct got_error *
1185 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1188 const struct got_error *err = NULL;
1189 char canonpath[PATH_MAX];
1190 char *path_got = NULL;
1192 *is_bad_symlink = 0;
1194 if (target_len >= sizeof(canonpath)) {
1195 *is_bad_symlink = 1;
1200 * We do not use realpath(3) to resolve the symlink's target
1201 * path because we don't want to resolve symlinks recursively.
1202 * Instead we make the path absolute and then canonicalize it.
1203 * Relative symlink target lookup should begin at the directory
1204 * in which the blob object is being installed.
1206 if (!got_path_is_absolute(target_path)) {
1207 char *abspath, *parent;
1208 err = got_path_dirname(&parent, ondisk_path);
1211 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1213 return got_error_from_errno("asprintf");
1216 if (strlen(abspath) >= sizeof(canonpath)) {
1217 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1221 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1226 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1231 /* Only allow symlinks pointing at paths within the work tree. */
1232 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 *is_bad_symlink = 1;
1237 /* Do not allow symlinks pointing into the .got directory. */
1238 if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 GOT_WORKTREE_GOT_DIR) == -1)
1240 return got_error_from_errno("asprintf");
1241 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 *is_bad_symlink = 1;
1248 static const struct got_error *
1249 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 int restoring_missing_file, int reverting_versioned_file,
1252 int path_is_unversioned, int allow_bad_symlinks,
1253 struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *path_got = NULL;
1260 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1263 *is_bad_symlink = 0;
1266 * Blob object content specifies the target path of the link.
1267 * If a symbolic link cannot be installed we instead create
1268 * a regular file which contains the link target path stored
1269 * in the blob object.
1272 err = got_object_blob_read_block(&len, blob);
1273 if (len + target_len >= sizeof(target_path)) {
1274 /* Path too long; install as a regular file. */
1275 *is_bad_symlink = 1;
1276 got_object_blob_rewind(blob);
1277 return install_blob(worktree, ondisk_path, path,
1278 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 restoring_missing_file, reverting_versioned_file,
1280 1, path_is_unversioned, repo, progress_cb,
1284 /* Skip blob object header first time around. */
1285 memcpy(target_path + target_len, buf + hdrlen,
1287 target_len += len - hdrlen;
1291 target_path[target_len] = '\0';
1293 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 ondisk_path, worktree->root_path);
1298 if (*is_bad_symlink && !allow_bad_symlinks) {
1299 /* install as a regular file */
1300 got_object_blob_rewind(blob);
1301 err = install_blob(worktree, ondisk_path, path,
1302 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 restoring_missing_file, reverting_versioned_file, 1,
1304 path_is_unversioned, repo, progress_cb, progress_arg);
1308 if (symlink(target_path, ondisk_path) == -1) {
1309 if (errno == EEXIST) {
1310 int symlink_replaced;
1311 if (path_is_unversioned) {
1312 err = (*progress_cb)(progress_arg,
1313 GOT_STATUS_UNVERSIONED, path);
1316 err = replace_existing_symlink(&symlink_replaced,
1317 ondisk_path, target_path, target_len);
1321 if (symlink_replaced) {
1322 err = (*progress_cb)(progress_arg,
1323 reverting_versioned_file ?
1325 GOT_STATUS_UPDATE, path);
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_EXISTS, path);
1331 goto done; /* Nothing else to do. */
1334 if (errno == ENOENT) {
1336 err = got_path_dirname(&parent, ondisk_path);
1339 err = add_dir_on_disk(worktree, parent);
1344 * Retry, and fall through to error handling
1345 * below if this second attempt fails.
1347 if (symlink(target_path, ondisk_path) != -1) {
1348 err = NULL; /* success */
1353 /* Handle errors from first or second creation attempt. */
1354 if (errno == ENAMETOOLONG) {
1355 /* bad target path; install as a regular file */
1356 *is_bad_symlink = 1;
1357 got_object_blob_rewind(blob);
1358 err = install_blob(worktree, ondisk_path, path,
1359 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 restoring_missing_file, reverting_versioned_file, 1,
1361 path_is_unversioned, repo,
1362 progress_cb, progress_arg);
1363 } else if (errno == ENOTDIR) {
1364 err = got_error_path(ondisk_path,
1365 GOT_ERR_FILE_OBSTRUCTED);
1367 err = got_error_from_errno3("symlink",
1368 target_path, ondisk_path);
1370 } else if (progress_cb)
1371 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1378 static const struct got_error *
1379 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 const char *path, mode_t te_mode, mode_t st_mode,
1381 struct got_blob_object *blob, int restoring_missing_file,
1382 int reverting_versioned_file, int installing_bad_symlink,
1383 int path_is_unversioned, struct got_repository *repo,
1384 got_worktree_checkout_cb progress_cb, void *progress_arg)
1386 const struct got_error *err = NULL;
1390 char *tmppath = NULL;
1392 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1395 if (errno == ENOENT) {
1397 err = got_path_dirname(&parent, path);
1400 err = add_dir_on_disk(worktree, parent);
1404 fd = open(ondisk_path,
1405 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 GOT_DEFAULT_FILE_MODE);
1408 return got_error_from_errno2("open",
1410 } else if (errno == EEXIST) {
1411 if (path_is_unversioned) {
1412 err = (*progress_cb)(progress_arg,
1413 GOT_STATUS_UNVERSIONED, path);
1416 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 /* TODO file is obstructed; do something */
1419 err = got_error_path(ondisk_path,
1420 GOT_ERR_FILE_OBSTRUCTED);
1423 err = got_opentemp_named_fd(&tmppath, &fd,
1430 return got_error_from_errno2("open", ondisk_path);
1433 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 err = got_error_from_errno2("fchmod",
1435 update ? tmppath : ondisk_path);
1440 if (restoring_missing_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1443 else if (reverting_versioned_file)
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1447 err = (*progress_cb)(progress_arg,
1448 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1453 hdrlen = got_object_blob_get_hdrlen(blob);
1455 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 err = got_object_blob_read_block(&len, blob);
1460 /* Skip blob object header first time around. */
1461 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1463 err = got_error_from_errno("write");
1465 } else if (outlen != len - hdrlen) {
1466 err = got_error(GOT_ERR_IO);
1473 if (fsync(fd) != 0) {
1474 err = got_error_from_errno("fsync");
1479 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 err = got_error_from_errno2("unlink", ondisk_path);
1483 if (rename(tmppath, ondisk_path) != 0) {
1484 err = got_error_from_errno3("rename", tmppath,
1493 if (fd != -1 && close(fd) == -1 && err == NULL)
1494 err = got_error_from_errno("close");
1495 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 err = got_error_from_errno2("unlink", tmppath);
1501 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 static const struct got_error *
1503 get_modified_file_content_status(unsigned char *status, FILE *f)
1505 const struct got_error *err = NULL;
1506 const char *markers[3] = {
1507 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 GOT_DIFF_CONFLICT_MARKER_SEP,
1509 GOT_DIFF_CONFLICT_MARKER_END
1513 size_t linesize = 0;
1516 while (*status == GOT_STATUS_MODIFY) {
1517 linelen = getline(&line, &linesize, f);
1518 if (linelen == -1) {
1521 err = got_ferror(f, GOT_ERR_IO);
1525 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1528 *status = GOT_STATUS_CONFLICT;
1539 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1541 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1546 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1548 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 ie->size == (sb->st_size & 0xffffffff) &&
1553 !xbit_differs(ie, sb->st_mode));
1556 static unsigned char
1557 get_staged_status(struct got_fileindex_entry *ie)
1559 switch (got_fileindex_entry_stage_get(ie)) {
1560 case GOT_FILEIDX_STAGE_ADD:
1561 return GOT_STATUS_ADD;
1562 case GOT_FILEIDX_STAGE_DELETE:
1563 return GOT_STATUS_DELETE;
1564 case GOT_FILEIDX_STAGE_MODIFY:
1565 return GOT_STATUS_MODIFY;
1567 return GOT_STATUS_NO_CHANGE;
1571 static const struct got_error *
1572 get_symlink_modification_status(unsigned char *status,
1573 struct got_fileindex_entry *ie, const char *abspath,
1574 int dirfd, const char *de_name, struct got_blob_object *blob)
1576 const struct got_error *err = NULL;
1577 char target_path[PATH_MAX];
1578 char etarget[PATH_MAX];
1580 size_t len, target_len = 0;
1581 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1584 *status = GOT_STATUS_NO_CHANGE;
1586 /* Blob object content specifies the target path of the link. */
1588 err = got_object_blob_read_block(&len, blob);
1591 if (len + target_len >= sizeof(target_path)) {
1593 * Should not happen. The blob contents were OK
1594 * when this symlink was installed.
1596 return got_error(GOT_ERR_NO_SPACE);
1599 /* Skip blob object header first time around. */
1600 memcpy(target_path + target_len, buf + hdrlen,
1602 target_len += len - hdrlen;
1606 target_path[target_len] = '\0';
1609 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1611 return got_error_from_errno2("readlinkat", abspath);
1613 elen = readlink(abspath, etarget, sizeof(etarget));
1615 return got_error_from_errno2("readlink", abspath);
1618 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 *status = GOT_STATUS_MODIFY;
1624 static const struct got_error *
1625 get_file_status(unsigned char *status, struct stat *sb,
1626 struct got_fileindex_entry *ie, const char *abspath,
1627 int dirfd, const char *de_name, struct got_repository *repo)
1629 const struct got_error *err = NULL;
1630 struct got_object_id id;
1632 int fd = -1, fd1 = -1;
1635 struct got_blob_object *blob = NULL;
1637 unsigned char staged_status = get_staged_status(ie);
1639 *status = GOT_STATUS_NO_CHANGE;
1640 memset(sb, 0, sizeof(*sb));
1643 * Whenever the caller provides a directory descriptor and a
1644 * directory entry name for the file, use them! This prevents
1645 * race conditions if filesystem paths change beneath our feet.
1648 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 if (errno == ENOENT) {
1650 if (got_fileindex_entry_has_file_on_disk(ie))
1651 *status = GOT_STATUS_MISSING;
1653 *status = GOT_STATUS_DELETE;
1656 err = got_error_from_errno2("fstatat", abspath);
1660 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 if (fd == -1 && errno != ENOENT &&
1662 !got_err_open_nofollow_on_symlink())
1663 return got_error_from_errno2("open", abspath);
1664 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 if (lstat(abspath, sb) == -1)
1666 return got_error_from_errno2("lstat", abspath);
1667 } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 if (errno == ENOENT) {
1669 if (got_fileindex_entry_has_file_on_disk(ie))
1670 *status = GOT_STATUS_MISSING;
1672 *status = GOT_STATUS_DELETE;
1675 err = got_error_from_errno2("fstat", abspath);
1680 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 *status = GOT_STATUS_OBSTRUCTED;
1685 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 *status = GOT_STATUS_DELETE;
1688 } else if (!got_fileindex_entry_has_blob(ie) &&
1689 staged_status != GOT_STATUS_ADD) {
1690 *status = GOT_STATUS_ADD;
1694 if (!stat_info_differs(ie, sb))
1697 if (S_ISLNK(sb->st_mode) &&
1698 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 *status = GOT_STATUS_MODIFY;
1703 if (staged_status == GOT_STATUS_MODIFY ||
1704 staged_status == GOT_STATUS_ADD)
1705 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1707 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1709 fd1 = got_opentempfd();
1711 err = got_error_from_errno("got_opentempfd");
1714 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1718 if (S_ISLNK(sb->st_mode)) {
1719 err = get_symlink_modification_status(status, ie,
1720 abspath, dirfd, de_name, blob);
1725 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1727 err = got_error_from_errno2("openat", abspath);
1732 f = fdopen(fd, "r");
1734 err = got_error_from_errno2("fdopen", abspath);
1738 hdrlen = got_object_blob_get_hdrlen(blob);
1740 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1741 err = got_object_blob_read_block(&blen, blob);
1744 /* Skip length of blob object header first time around. */
1745 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1746 if (flen == 0 && ferror(f)) {
1747 err = got_error_from_errno("fread");
1750 if (blen - hdrlen == 0) {
1752 *status = GOT_STATUS_MODIFY;
1754 } else if (flen == 0) {
1755 if (blen - hdrlen != 0)
1756 *status = GOT_STATUS_MODIFY;
1758 } else if (blen - hdrlen == flen) {
1759 /* Skip blob object header first time around. */
1760 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1761 *status = GOT_STATUS_MODIFY;
1765 *status = GOT_STATUS_MODIFY;
1771 if (*status == GOT_STATUS_MODIFY) {
1773 err = get_modified_file_content_status(status, f);
1774 } else if (xbit_differs(ie, sb->st_mode))
1775 *status = GOT_STATUS_MODE_CHANGE;
1777 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1778 err = got_error_from_errno("close");
1780 got_object_blob_close(blob);
1781 if (f != NULL && fclose(f) == EOF && err == NULL)
1782 err = got_error_from_errno2("fclose", abspath);
1783 if (fd != -1 && close(fd) == -1 && err == NULL)
1784 err = got_error_from_errno2("close", abspath);
1789 * Update timestamps in the file index if a file is unmodified and
1790 * we had to run a full content comparison to find out.
1792 static const struct got_error *
1793 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1794 struct got_fileindex_entry *ie, struct stat *sb)
1796 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1797 return got_fileindex_entry_update(ie, wt_fd, path,
1798 ie->blob_sha1, ie->commit_sha1, 1);
1803 static const struct got_error *
1804 update_blob(struct got_worktree *worktree,
1805 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1806 struct got_tree_entry *te, const char *path,
1807 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1810 const struct got_error *err = NULL;
1811 struct got_blob_object *blob = NULL;
1812 char *ondisk_path = NULL;
1813 unsigned char status = GOT_STATUS_NO_CHANGE;
1815 int fd1 = -1, fd2 = -1;
1817 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1818 return got_error_from_errno("asprintf");
1821 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1822 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1825 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1829 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1830 sb.st_mode = got_fileindex_perms_to_st(ie);
1832 if (stat(ondisk_path, &sb) == -1) {
1833 if (errno != ENOENT) {
1834 err = got_error_from_errno2("stat",
1838 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1839 status = GOT_STATUS_UNVERSIONED;
1841 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1842 status = GOT_STATUS_UNVERSIONED;
1844 status = GOT_STATUS_OBSTRUCTED;
1848 if (status == GOT_STATUS_OBSTRUCTED) {
1850 got_fileindex_entry_mark_skipped(ie);
1851 err = (*progress_cb)(progress_arg, status, path);
1854 if (status == GOT_STATUS_CONFLICT) {
1856 got_fileindex_entry_mark_skipped(ie);
1857 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1862 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1863 (S_ISLNK(te->mode) ||
1864 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1866 * This is a regular file or an installed bad symlink.
1867 * If the file index indicates that this file is already
1868 * up-to-date with respect to the repository we can skip
1869 * updating contents of this file.
1871 if (got_fileindex_entry_has_commit(ie) &&
1872 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1873 SHA1_DIGEST_LENGTH) == 0) {
1875 err = sync_timestamps(worktree->root_fd,
1876 path, status, ie, &sb);
1879 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1883 if (got_fileindex_entry_has_blob(ie) &&
1884 memcmp(ie->blob_sha1, te->id.sha1,
1885 SHA1_DIGEST_LENGTH) == 0) {
1886 /* Different commit but the same blob. */
1887 err = sync_timestamps(worktree->root_fd,
1888 path, status, ie, &sb);
1891 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1897 fd1 = got_opentempfd();
1899 err = got_error_from_errno("got_opentempfd");
1902 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1906 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1907 int update_timestamps;
1908 struct got_blob_object *blob2 = NULL;
1909 char *label_orig = NULL;
1910 if (got_fileindex_entry_has_blob(ie)) {
1911 fd2 = got_opentempfd();
1913 err = got_error_from_errno("got_opentempfd");
1916 struct got_object_id id2;
1917 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1918 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1923 if (got_fileindex_entry_has_commit(ie)) {
1924 char id_str[SHA1_DIGEST_STRING_LENGTH];
1925 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1926 sizeof(id_str)) == NULL) {
1927 err = got_error_path(id_str,
1928 GOT_ERR_BAD_OBJ_ID_STR);
1931 if (asprintf(&label_orig, "%s: commit %s",
1932 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1933 err = got_error_from_errno("asprintf");
1937 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1939 err = got_object_blob_read_to_str(&link_target, blob);
1942 err = merge_symlink(worktree, blob2, ondisk_path, path,
1943 label_orig, link_target, worktree->base_commit_id,
1944 repo, progress_cb, progress_arg);
1947 err = merge_blob(&update_timestamps, worktree, blob2,
1948 ondisk_path, path, sb.st_mode, label_orig, blob,
1949 worktree->base_commit_id, repo,
1950 progress_cb, progress_arg);
1953 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1954 err = got_error_from_errno("close");
1958 got_object_blob_close(blob2);
1962 * Do not update timestamps of files with local changes.
1963 * Otherwise, a future status walk would treat them as
1964 * unmodified files again.
1966 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1967 blob->id.sha1, worktree->base_commit_id->sha1,
1969 } else if (status == GOT_STATUS_MODE_CHANGE) {
1970 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1971 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1972 } else if (status == GOT_STATUS_DELETE) {
1973 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1976 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1977 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1981 int is_bad_symlink = 0;
1982 if (S_ISLNK(te->mode)) {
1983 err = install_symlink(&is_bad_symlink, worktree,
1984 ondisk_path, path, blob,
1985 status == GOT_STATUS_MISSING, 0,
1986 status == GOT_STATUS_UNVERSIONED, 0,
1987 repo, progress_cb, progress_arg);
1989 err = install_blob(worktree, ondisk_path, path,
1990 te->mode, sb.st_mode, blob,
1991 status == GOT_STATUS_MISSING, 0, 0,
1992 status == GOT_STATUS_UNVERSIONED, repo,
1993 progress_cb, progress_arg);
1999 err = got_fileindex_entry_update(ie,
2000 worktree->root_fd, path, blob->id.sha1,
2001 worktree->base_commit_id->sha1, 1);
2003 err = create_fileindex_entry(&ie, fileindex,
2004 worktree->base_commit_id, worktree->root_fd, path,
2010 if (is_bad_symlink) {
2011 got_fileindex_entry_filetype_set(ie,
2012 GOT_FILEIDX_MODE_BAD_SYMLINK);
2016 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2017 err = got_error_from_errno("close");
2020 got_object_blob_close(blob);
2026 static const struct got_error *
2027 remove_ondisk_file(const char *root_path, const char *path)
2029 const struct got_error *err = NULL;
2030 char *ondisk_path = NULL, *parent = NULL;
2032 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2033 return got_error_from_errno("asprintf");
2035 if (unlink(ondisk_path) == -1) {
2036 if (errno != ENOENT)
2037 err = got_error_from_errno2("unlink", ondisk_path);
2039 size_t root_len = strlen(root_path);
2040 err = got_path_dirname(&parent, ondisk_path);
2043 while (got_path_cmp(parent, root_path,
2044 strlen(parent), root_len) != 0) {
2046 ondisk_path = parent;
2048 if (rmdir(ondisk_path) == -1) {
2049 if (errno != ENOTEMPTY)
2050 err = got_error_from_errno2("rmdir",
2054 err = got_path_dirname(&parent, ondisk_path);
2065 static const struct got_error *
2066 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2067 struct got_fileindex_entry *ie, struct got_repository *repo,
2068 got_worktree_checkout_cb progress_cb, void *progress_arg)
2070 const struct got_error *err = NULL;
2071 unsigned char status;
2075 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2076 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2078 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2080 return got_error_from_errno("asprintf");
2082 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2086 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2087 char ondisk_target[PATH_MAX];
2088 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2089 sizeof(ondisk_target));
2090 if (ondisk_len == -1) {
2091 err = got_error_from_errno2("readlink", ondisk_path);
2094 ondisk_target[ondisk_len] = '\0';
2095 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2096 NULL, NULL, /* XXX pass common ancestor info? */
2097 ondisk_target, ondisk_path);
2100 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2105 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2106 status == GOT_STATUS_ADD) {
2107 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2111 * Preserve the working file and change the deleted blob's
2112 * entry into a schedule-add entry.
2114 err = got_fileindex_entry_update(ie, worktree->root_fd,
2115 ie->path, NULL, NULL, 0);
2117 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2120 if (status == GOT_STATUS_NO_CHANGE) {
2121 err = remove_ondisk_file(worktree->root_path, ie->path);
2125 got_fileindex_entry_remove(fileindex, ie);
2132 struct diff_cb_arg {
2133 struct got_fileindex *fileindex;
2134 struct got_worktree *worktree;
2135 struct got_repository *repo;
2136 got_worktree_checkout_cb progress_cb;
2138 got_cancel_cb cancel_cb;
2142 static const struct got_error *
2143 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2144 struct got_tree_entry *te, const char *parent_path)
2146 struct diff_cb_arg *a = arg;
2148 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2149 return got_error(GOT_ERR_CANCELLED);
2151 return update_blob(a->worktree, a->fileindex, ie, te,
2152 ie->path, a->repo, a->progress_cb, a->progress_arg);
2155 static const struct got_error *
2156 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2158 struct diff_cb_arg *a = arg;
2160 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2161 return got_error(GOT_ERR_CANCELLED);
2163 return delete_blob(a->worktree, a->fileindex, ie,
2164 a->repo, a->progress_cb, a->progress_arg);
2167 static const struct got_error *
2168 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2170 struct diff_cb_arg *a = arg;
2171 const struct got_error *err;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 if (got_object_tree_entry_is_submodule(te))
2180 if (asprintf(&path, "%s%s%s", parent_path,
2181 parent_path[0] ? "/" : "", te->name)
2183 return got_error_from_errno("asprintf");
2185 if (S_ISDIR(te->mode))
2186 err = add_dir_on_disk(a->worktree, path);
2188 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2189 a->repo, a->progress_cb, a->progress_arg);
2195 const struct got_error *
2196 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2198 uint32_t uuid_status;
2200 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2201 if (uuid_status != uuid_s_ok) {
2203 return got_error_uuid(uuid_status, "uuid_to_string");
2209 static const struct got_error *
2210 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2212 const struct got_error *err = NULL;
2213 char *uuidstr = NULL;
2217 err = got_worktree_get_uuid(&uuidstr, worktree);
2221 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2222 err = got_error_from_errno("asprintf");
2229 const struct got_error *
2230 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2232 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2235 static const struct got_error *
2236 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2238 return get_ref_name(refname, worktree,
2239 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2242 static const struct got_error *
2243 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2245 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2248 static const struct got_error *
2249 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2255 static const struct got_error *
2256 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2262 static const struct got_error *
2263 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2269 static const struct got_error *
2270 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2290 const struct got_error *
2291 got_worktree_get_histedit_script_path(char **path,
2292 struct got_worktree *worktree)
2294 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2295 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2297 return got_error_from_errno("asprintf");
2302 static const struct got_error *
2303 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2305 return get_ref_name(refname, worktree,
2306 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2309 static const struct got_error *
2310 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2312 return get_ref_name(refname, worktree,
2313 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2317 * Prevent Git's garbage collector from deleting our base commit by
2318 * setting a reference to our base commit's ID.
2320 static const struct got_error *
2321 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2323 const struct got_error *err = NULL;
2324 struct got_reference *ref = NULL;
2327 err = got_worktree_get_base_ref_name(&refname, worktree);
2331 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2335 err = got_ref_write(ref, repo);
2343 static const struct got_error *
2344 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2346 const struct got_error *err = NULL;
2348 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2349 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2350 err = got_error_from_errno("asprintf");
2351 *fileindex_path = NULL;
2357 static const struct got_error *
2358 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2359 struct got_worktree *worktree)
2361 const struct got_error *err = NULL;
2364 *fileindex_path = NULL;
2365 *fileindex = got_fileindex_alloc();
2366 if (*fileindex == NULL)
2367 return got_error_from_errno("got_fileindex_alloc");
2369 err = get_fileindex_path(fileindex_path, worktree);
2373 index = fopen(*fileindex_path, "rbe");
2374 if (index == NULL) {
2375 if (errno != ENOENT)
2376 err = got_error_from_errno2("fopen", *fileindex_path);
2378 err = got_fileindex_read(*fileindex, index);
2379 if (fclose(index) == EOF && err == NULL)
2380 err = got_error_from_errno("fclose");
2384 free(*fileindex_path);
2385 *fileindex_path = NULL;
2386 got_fileindex_free(*fileindex);
2392 struct bump_base_commit_id_arg {
2393 struct got_object_id *base_commit_id;
2396 const char *entry_name;
2397 got_worktree_checkout_cb progress_cb;
2401 /* Bump base commit ID of all files within an updated part of the work tree. */
2402 static const struct got_error *
2403 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2405 const struct got_error *err;
2406 struct bump_base_commit_id_arg *a = arg;
2408 if (a->entry_name) {
2409 if (strcmp(ie->path, a->path) != 0)
2411 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2414 if (got_fileindex_entry_was_skipped(ie))
2417 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2418 SHA1_DIGEST_LENGTH) == 0)
2421 if (a->progress_cb) {
2422 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2427 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2431 static const struct got_error *
2432 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2433 struct got_fileindex *fileindex,
2434 got_worktree_checkout_cb progress_cb, void *progress_arg)
2436 struct bump_base_commit_id_arg bbc_arg;
2438 bbc_arg.base_commit_id = worktree->base_commit_id;
2439 bbc_arg.entry_name = NULL;
2441 bbc_arg.path_len = 0;
2442 bbc_arg.progress_cb = progress_cb;
2443 bbc_arg.progress_arg = progress_arg;
2445 return got_fileindex_for_each_entry_safe(fileindex,
2446 bump_base_commit_id, &bbc_arg);
2449 static const struct got_error *
2450 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2452 const struct got_error *err = NULL;
2453 char *new_fileindex_path = NULL;
2454 FILE *new_index = NULL;
2455 struct timespec timeout;
2457 err = got_opentemp_named(&new_fileindex_path, &new_index,
2462 err = got_fileindex_write(fileindex, new_index);
2466 if (rename(new_fileindex_path, fileindex_path) != 0) {
2467 err = got_error_from_errno3("rename", new_fileindex_path,
2469 unlink(new_fileindex_path);
2473 * Sleep for a short amount of time to ensure that files modified after
2474 * this program exits have a different time stamp from the one which
2475 * was recorded in the file index.
2478 timeout.tv_nsec = 1;
2479 nanosleep(&timeout, NULL);
2483 free(new_fileindex_path);
2487 static const struct got_error *
2488 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2489 struct got_object_id **tree_id, const char *wt_relpath,
2490 struct got_commit_object *base_commit, struct got_worktree *worktree,
2491 struct got_repository *repo)
2493 const struct got_error *err = NULL;
2494 struct got_object_id *id = NULL;
2495 char *in_repo_path = NULL;
2496 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2498 *entry_type = GOT_OBJ_TYPE_ANY;
2499 *tree_relpath = NULL;
2502 if (wt_relpath[0] == '\0') {
2503 /* Check out all files within the work tree. */
2504 *entry_type = GOT_OBJ_TYPE_TREE;
2505 *tree_relpath = strdup("");
2506 if (*tree_relpath == NULL) {
2507 err = got_error_from_errno("strdup");
2510 err = got_object_id_by_path(tree_id, repo, base_commit,
2511 worktree->path_prefix);
2517 /* Check out a subset of files in the work tree. */
2519 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2520 is_root_wt ? "" : "/", wt_relpath) == -1) {
2521 err = got_error_from_errno("asprintf");
2525 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2530 in_repo_path = NULL;
2532 err = got_object_get_type(entry_type, repo, id);
2536 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2537 /* Check out a single file. */
2538 if (strchr(wt_relpath, '/') == NULL) {
2539 /* Check out a single file in work tree's root dir. */
2540 in_repo_path = strdup(worktree->path_prefix);
2541 if (in_repo_path == NULL) {
2542 err = got_error_from_errno("strdup");
2545 *tree_relpath = strdup("");
2546 if (*tree_relpath == NULL) {
2547 err = got_error_from_errno("strdup");
2551 /* Check out a single file in a subdirectory. */
2552 err = got_path_dirname(tree_relpath, wt_relpath);
2555 if (asprintf(&in_repo_path, "%s%s%s",
2556 worktree->path_prefix, is_root_wt ? "" : "/",
2557 *tree_relpath) == -1) {
2558 err = got_error_from_errno("asprintf");
2562 err = got_object_id_by_path(tree_id, repo,
2563 base_commit, in_repo_path);
2565 /* Check out all files within a subdirectory. */
2566 *tree_id = got_object_id_dup(id);
2567 if (*tree_id == NULL) {
2568 err = got_error_from_errno("got_object_id_dup");
2571 *tree_relpath = strdup(wt_relpath);
2572 if (*tree_relpath == NULL) {
2573 err = got_error_from_errno("strdup");
2581 *entry_type = GOT_OBJ_TYPE_ANY;
2582 free(*tree_relpath);
2583 *tree_relpath = NULL;
2590 static const struct got_error *
2591 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2592 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2593 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2594 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2596 const struct got_error *err = NULL;
2597 struct got_commit_object *commit = NULL;
2598 struct got_tree_object *tree = NULL;
2599 struct got_fileindex_diff_tree_cb diff_cb;
2600 struct diff_cb_arg arg;
2602 err = ref_base_commit(worktree, repo);
2604 if (!(err->code == GOT_ERR_ERRNO &&
2605 (errno == EACCES || errno == EROFS)))
2607 err = (*progress_cb)(progress_arg,
2608 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2613 err = got_object_open_as_commit(&commit, repo,
2614 worktree->base_commit_id);
2618 err = got_object_open_as_tree(&tree, repo, tree_id);
2623 got_object_tree_find_entry(tree, entry_name) == NULL) {
2624 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2628 diff_cb.diff_old_new = diff_old_new;
2629 diff_cb.diff_old = diff_old;
2630 diff_cb.diff_new = diff_new;
2631 arg.fileindex = fileindex;
2632 arg.worktree = worktree;
2634 arg.progress_cb = progress_cb;
2635 arg.progress_arg = progress_arg;
2636 arg.cancel_cb = cancel_cb;
2637 arg.cancel_arg = cancel_arg;
2638 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2639 entry_name, repo, &diff_cb, &arg);
2642 got_object_tree_close(tree);
2644 got_object_commit_close(commit);
2648 const struct got_error *
2649 got_worktree_checkout_files(struct got_worktree *worktree,
2650 struct got_pathlist_head *paths, struct got_repository *repo,
2651 got_worktree_checkout_cb progress_cb, void *progress_arg,
2652 got_cancel_cb cancel_cb, void *cancel_arg)
2654 const struct got_error *err = NULL, *sync_err, *unlockerr;
2655 struct got_commit_object *commit = NULL;
2656 struct got_tree_object *tree = NULL;
2657 struct got_fileindex *fileindex = NULL;
2658 char *fileindex_path = NULL;
2659 struct got_pathlist_entry *pe;
2660 struct tree_path_data {
2661 STAILQ_ENTRY(tree_path_data) entry;
2662 struct got_object_id *tree_id;
2667 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2669 STAILQ_INIT(&tree_paths);
2671 err = lock_worktree(worktree, LOCK_EX);
2675 err = got_object_open_as_commit(&commit, repo,
2676 worktree->base_commit_id);
2680 /* Map all specified paths to in-repository trees. */
2681 TAILQ_FOREACH(pe, paths, entry) {
2682 tpd = malloc(sizeof(*tpd));
2684 err = got_error_from_errno("malloc");
2688 err = find_tree_entry_for_checkout(&tpd->entry_type,
2689 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2696 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2697 err = got_path_basename(&tpd->entry_name, pe->path);
2705 tpd->entry_name = NULL;
2707 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2711 * Read the file index.
2712 * Checking out files is supposed to be an idempotent operation.
2713 * If the on-disk file index is incomplete we will try to complete it.
2715 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2719 tpd = STAILQ_FIRST(&tree_paths);
2720 TAILQ_FOREACH(pe, paths, entry) {
2721 struct bump_base_commit_id_arg bbc_arg;
2723 err = checkout_files(worktree, fileindex, tpd->relpath,
2724 tpd->tree_id, tpd->entry_name, repo,
2725 progress_cb, progress_arg, cancel_cb, cancel_arg);
2729 bbc_arg.base_commit_id = worktree->base_commit_id;
2730 bbc_arg.entry_name = tpd->entry_name;
2731 bbc_arg.path = pe->path;
2732 bbc_arg.path_len = pe->path_len;
2733 bbc_arg.progress_cb = progress_cb;
2734 bbc_arg.progress_arg = progress_arg;
2735 err = got_fileindex_for_each_entry_safe(fileindex,
2736 bump_base_commit_id, &bbc_arg);
2740 tpd = STAILQ_NEXT(tpd, entry);
2742 sync_err = sync_fileindex(fileindex, fileindex_path);
2743 if (sync_err && err == NULL)
2746 free(fileindex_path);
2748 got_object_tree_close(tree);
2750 got_object_commit_close(commit);
2752 got_fileindex_free(fileindex);
2753 while (!STAILQ_EMPTY(&tree_paths)) {
2754 tpd = STAILQ_FIRST(&tree_paths);
2755 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2760 unlockerr = lock_worktree(worktree, LOCK_SH);
2761 if (unlockerr && err == NULL)
2766 struct merge_file_cb_arg {
2767 struct got_worktree *worktree;
2768 struct got_fileindex *fileindex;
2769 got_worktree_checkout_cb progress_cb;
2771 got_cancel_cb cancel_cb;
2773 const char *label_orig;
2774 struct got_object_id *commit_id2;
2775 int allow_bad_symlinks;
2778 static const struct got_error *
2779 merge_file_cb(void *arg, struct got_blob_object *blob1,
2780 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2781 struct got_object_id *id1, struct got_object_id *id2,
2782 const char *path1, const char *path2,
2783 mode_t mode1, mode_t mode2, struct got_repository *repo)
2785 static const struct got_error *err = NULL;
2786 struct merge_file_cb_arg *a = arg;
2787 struct got_fileindex_entry *ie;
2788 char *ondisk_path = NULL;
2790 unsigned char status;
2791 int local_changes_subsumed;
2792 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2793 char *id_str = NULL, *label_deriv2 = NULL;
2795 if (blob1 && blob2) {
2796 ie = got_fileindex_entry_get(a->fileindex, path2,
2799 return (*a->progress_cb)(a->progress_arg,
2800 GOT_STATUS_MISSING, path2);
2802 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2804 return got_error_from_errno("asprintf");
2806 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2811 if (status == GOT_STATUS_DELETE) {
2812 err = (*a->progress_cb)(a->progress_arg,
2813 GOT_STATUS_MERGE, path2);
2816 if (status != GOT_STATUS_NO_CHANGE &&
2817 status != GOT_STATUS_MODIFY &&
2818 status != GOT_STATUS_CONFLICT &&
2819 status != GOT_STATUS_ADD) {
2820 err = (*a->progress_cb)(a->progress_arg, status, path2);
2824 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2826 err = got_object_blob_read_to_str(&link_target2, blob2);
2829 err = merge_symlink(a->worktree, blob1, ondisk_path,
2830 path2, a->label_orig, link_target2, a->commit_id2,
2831 repo, a->progress_cb, a->progress_arg);
2836 f_orig = got_opentemp();
2837 if (f_orig == NULL) {
2838 err = got_error_from_errno("got_opentemp");
2841 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2846 f_deriv2 = got_opentemp();
2847 if (f_deriv2 == NULL)
2849 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2854 fd = open(ondisk_path,
2855 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2857 err = got_error_from_errno2("open",
2861 f_deriv = fdopen(fd, "r");
2862 if (f_deriv == NULL) {
2863 err = got_error_from_errno2("fdopen",
2868 err = got_object_id_str(&id_str, a->commit_id2);
2871 if (asprintf(&label_deriv2, "%s: commit %s",
2872 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2873 err = got_error_from_errno("asprintf");
2876 err = merge_file(&local_changes_subsumed, a->worktree,
2877 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2878 sb.st_mode, a->label_orig, NULL, label_deriv2,
2879 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2880 a->progress_cb, a->progress_arg);
2883 ie = got_fileindex_entry_get(a->fileindex, path1,
2886 return (*a->progress_cb)(a->progress_arg,
2887 GOT_STATUS_MISSING, path1);
2889 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2891 return got_error_from_errno("asprintf");
2893 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2899 case GOT_STATUS_NO_CHANGE:
2900 err = (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_DELETE, path1);
2904 err = remove_ondisk_file(a->worktree->root_path, path1);
2908 got_fileindex_entry_mark_deleted_from_disk(ie);
2910 case GOT_STATUS_DELETE:
2911 case GOT_STATUS_MISSING:
2912 err = (*a->progress_cb)(a->progress_arg,
2913 GOT_STATUS_DELETE, path1);
2917 got_fileindex_entry_mark_deleted_from_disk(ie);
2919 case GOT_STATUS_ADD: {
2920 struct got_object_id *id;
2924 * Delete the added file only if its content already
2925 * exists in the repository.
2927 err = got_object_blob_file_create(&id, &blob1_f,
2928 &blob1_size, path1);
2931 if (got_object_id_cmp(id, id1) == 0) {
2932 err = (*a->progress_cb)(a->progress_arg,
2933 GOT_STATUS_DELETE, path1);
2936 err = remove_ondisk_file(a->worktree->root_path,
2941 got_fileindex_entry_remove(a->fileindex,
2944 err = (*a->progress_cb)(a->progress_arg,
2945 GOT_STATUS_CANNOT_DELETE, path1);
2947 if (fclose(blob1_f) == EOF && err == NULL)
2948 err = got_error_from_errno("fclose");
2954 case GOT_STATUS_MODIFY:
2955 case GOT_STATUS_CONFLICT:
2956 err = (*a->progress_cb)(a->progress_arg,
2957 GOT_STATUS_CANNOT_DELETE, path1);
2961 case GOT_STATUS_OBSTRUCTED:
2962 err = (*a->progress_cb)(a->progress_arg, status, path1);
2970 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2972 return got_error_from_errno("asprintf");
2973 ie = got_fileindex_entry_get(a->fileindex, path2,
2976 err = get_file_status(&status, &sb, ie, ondisk_path,
2980 if (status != GOT_STATUS_NO_CHANGE &&
2981 status != GOT_STATUS_MODIFY &&
2982 status != GOT_STATUS_CONFLICT &&
2983 status != GOT_STATUS_ADD) {
2984 err = (*a->progress_cb)(a->progress_arg,
2988 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2990 err = got_object_blob_read_to_str(&link_target2,
2994 err = merge_symlink(a->worktree, NULL,
2995 ondisk_path, path2, a->label_orig,
2996 link_target2, a->commit_id2, repo,
2997 a->progress_cb, a->progress_arg);
2999 } else if (S_ISREG(sb.st_mode)) {
3000 err = merge_blob(&local_changes_subsumed,
3001 a->worktree, NULL, ondisk_path, path2,
3002 sb.st_mode, a->label_orig, blob2,
3003 a->commit_id2, repo, a->progress_cb,
3006 err = got_error_path(ondisk_path,
3007 GOT_ERR_FILE_OBSTRUCTED);
3011 if (status == GOT_STATUS_DELETE) {
3012 err = got_fileindex_entry_update(ie,
3013 a->worktree->root_fd, path2, blob2->id.sha1,
3014 a->worktree->base_commit_id->sha1, 0);
3019 int is_bad_symlink = 0;
3020 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3021 if (S_ISLNK(mode2)) {
3022 err = install_symlink(&is_bad_symlink,
3023 a->worktree, ondisk_path, path2, blob2, 0,
3024 0, 1, a->allow_bad_symlinks, repo,
3025 a->progress_cb, a->progress_arg);
3027 err = install_blob(a->worktree, ondisk_path, path2,
3028 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3029 a->progress_cb, a->progress_arg);
3033 err = got_fileindex_entry_alloc(&ie, path2);
3036 err = got_fileindex_entry_update(ie,
3037 a->worktree->root_fd, path2, NULL, NULL, 1);
3039 got_fileindex_entry_free(ie);
3042 err = got_fileindex_entry_add(a->fileindex, ie);
3044 got_fileindex_entry_free(ie);
3047 if (is_bad_symlink) {
3048 got_fileindex_entry_filetype_set(ie,
3049 GOT_FILEIDX_MODE_BAD_SYMLINK);
3054 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3055 err = got_error_from_errno("fclose");
3056 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3057 err = got_error_from_errno("fclose");
3058 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3059 err = got_error_from_errno("fclose");
3066 static const struct got_error *
3067 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3069 struct got_worktree *worktree = arg;
3071 /* Reject merges into a work tree with mixed base commits. */
3072 if (got_fileindex_entry_has_commit(ie) &&
3073 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3074 SHA1_DIGEST_LENGTH) != 0)
3075 return got_error(GOT_ERR_MIXED_COMMITS);
3080 struct check_merge_conflicts_arg {
3081 struct got_worktree *worktree;
3082 struct got_fileindex *fileindex;
3083 struct got_repository *repo;
3086 static const struct got_error *
3087 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3088 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3089 struct got_object_id *id1, struct got_object_id *id2,
3090 const char *path1, const char *path2,
3091 mode_t mode1, mode_t mode2, struct got_repository *repo)
3093 const struct got_error *err = NULL;
3094 struct check_merge_conflicts_arg *a = arg;
3095 unsigned char status;
3097 struct got_fileindex_entry *ie;
3098 const char *path = path2 ? path2 : path1;
3099 struct got_object_id *id = id2 ? id2 : id1;
3105 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3109 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3111 return got_error_from_errno("asprintf");
3113 /* Reject merges into a work tree with conflicted files. */
3114 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3118 if (status == GOT_STATUS_CONFLICT)
3119 return got_error(GOT_ERR_CONFLICTS);
3124 static const struct got_error *
3125 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3126 const char *fileindex_path, struct got_object_id *commit_id1,
3127 struct got_object_id *commit_id2, struct got_repository *repo,
3128 got_worktree_checkout_cb progress_cb, void *progress_arg,
3129 got_cancel_cb cancel_cb, void *cancel_arg)
3131 const struct got_error *err = NULL, *sync_err;
3132 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3133 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3134 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3135 struct check_merge_conflicts_arg cmc_arg;
3136 struct merge_file_cb_arg arg;
3137 char *label_orig = NULL;
3138 FILE *f1 = NULL, *f2 = NULL;
3141 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3144 err = got_object_id_by_path(&tree_id1, repo, commit1,
3145 worktree->path_prefix);
3146 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3152 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3156 err = got_object_id_str(&id_str, commit_id1);
3160 if (asprintf(&label_orig, "%s: commit %s",
3161 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3162 err = got_error_from_errno("asprintf");
3168 f1 = got_opentemp();
3170 err = got_error_from_errno("got_opentemp");
3175 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3179 err = got_object_id_by_path(&tree_id2, repo, commit2,
3180 worktree->path_prefix);
3184 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3188 f2 = got_opentemp();
3190 err = got_error_from_errno("got_opentemp");
3194 cmc_arg.worktree = worktree;
3195 cmc_arg.fileindex = fileindex;
3196 cmc_arg.repo = repo;
3197 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3198 check_merge_conflicts, &cmc_arg, 0);
3202 arg.worktree = worktree;
3203 arg.fileindex = fileindex;
3204 arg.progress_cb = progress_cb;
3205 arg.progress_arg = progress_arg;
3206 arg.cancel_cb = cancel_cb;
3207 arg.cancel_arg = cancel_arg;
3208 arg.label_orig = label_orig;
3209 arg.commit_id2 = commit_id2;
3210 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3211 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3212 merge_file_cb, &arg, 1);
3213 sync_err = sync_fileindex(fileindex, fileindex_path);
3214 if (sync_err && err == NULL)
3218 got_object_commit_close(commit1);
3220 got_object_commit_close(commit2);
3222 got_object_tree_close(tree1);
3224 got_object_tree_close(tree2);
3225 if (f1 && fclose(f1) == EOF && err == NULL)
3226 err = got_error_from_errno("fclose");
3227 if (f2 && fclose(f2) == EOF && err == NULL)
3228 err = got_error_from_errno("fclose");
3233 const struct got_error *
3234 got_worktree_merge_files(struct got_worktree *worktree,
3235 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3236 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3237 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3239 const struct got_error *err, *unlockerr;
3240 char *fileindex_path = NULL;
3241 struct got_fileindex *fileindex = NULL;
3243 err = lock_worktree(worktree, LOCK_EX);
3247 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3251 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3256 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3257 commit_id2, repo, progress_cb, progress_arg,
3258 cancel_cb, cancel_arg);
3261 got_fileindex_free(fileindex);
3262 free(fileindex_path);
3263 unlockerr = lock_worktree(worktree, LOCK_SH);
3264 if (unlockerr && err == NULL)
3269 struct diff_dir_cb_arg {
3270 struct got_fileindex *fileindex;
3271 struct got_worktree *worktree;
3272 const char *status_path;
3273 size_t status_path_len;
3274 struct got_repository *repo;
3275 got_worktree_status_cb status_cb;
3277 got_cancel_cb cancel_cb;
3279 /* A pathlist containing per-directory pathlists of ignore patterns. */
3280 struct got_pathlist_head *ignores;
3281 int report_unchanged;
3285 static const struct got_error *
3286 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3287 int dirfd, const char *de_name,
3288 got_worktree_status_cb status_cb, void *status_arg,
3289 struct got_repository *repo, int report_unchanged)
3291 const struct got_error *err = NULL;
3292 unsigned char status = GOT_STATUS_NO_CHANGE;
3293 unsigned char staged_status = get_staged_status(ie);
3295 struct got_object_id blob_id, commit_id, staged_blob_id;
3296 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3297 struct got_object_id *staged_blob_idp = NULL;
3299 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3303 if (status == GOT_STATUS_NO_CHANGE &&
3304 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3307 if (got_fileindex_entry_has_blob(ie)) {
3308 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3309 blob_idp = &blob_id;
3311 if (got_fileindex_entry_has_commit(ie)) {
3312 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3313 commit_idp = &commit_id;
3315 if (staged_status == GOT_STATUS_ADD ||
3316 staged_status == GOT_STATUS_MODIFY) {
3317 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3318 SHA1_DIGEST_LENGTH);
3319 staged_blob_idp = &staged_blob_id;
3322 return (*status_cb)(status_arg, status, staged_status,
3323 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3326 static const struct got_error *
3327 status_old_new(void *arg, struct got_fileindex_entry *ie,
3328 struct dirent *de, const char *parent_path, int dirfd)
3330 const struct got_error *err = NULL;
3331 struct diff_dir_cb_arg *a = arg;
3334 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3335 return got_error(GOT_ERR_CANCELLED);
3337 if (got_path_cmp(parent_path, a->status_path,
3338 strlen(parent_path), a->status_path_len) != 0 &&
3339 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3342 if (parent_path[0]) {
3343 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3344 parent_path, de->d_name) == -1)
3345 return got_error_from_errno("asprintf");
3347 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3349 return got_error_from_errno("asprintf");
3352 err = report_file_status(ie, abspath, dirfd, de->d_name,
3353 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3358 static const struct got_error *
3359 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3361 struct diff_dir_cb_arg *a = arg;
3362 struct got_object_id blob_id, commit_id;
3363 unsigned char status;
3365 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3366 return got_error(GOT_ERR_CANCELLED);
3368 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3371 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3372 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3373 if (got_fileindex_entry_has_file_on_disk(ie))
3374 status = GOT_STATUS_MISSING;
3376 status = GOT_STATUS_DELETE;
3377 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3378 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3382 free_ignorelist(struct got_pathlist_head *ignorelist)
3384 struct got_pathlist_entry *pe;
3386 TAILQ_FOREACH(pe, ignorelist, entry)
3387 free((char *)pe->path);
3388 got_pathlist_free(ignorelist);
3392 free_ignores(struct got_pathlist_head *ignores)
3394 struct got_pathlist_entry *pe;
3396 TAILQ_FOREACH(pe, ignores, entry) {
3397 struct got_pathlist_head *ignorelist = pe->data;
3398 free_ignorelist(ignorelist);
3399 free((char *)pe->path);
3401 got_pathlist_free(ignores);
3404 static const struct got_error *
3405 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3407 const struct got_error *err = NULL;
3408 struct got_pathlist_entry *pe = NULL;
3409 struct got_pathlist_head *ignorelist;
3410 char *line = NULL, *pattern, *dirpath = NULL;
3411 size_t linesize = 0;
3414 ignorelist = calloc(1, sizeof(*ignorelist));
3415 if (ignorelist == NULL)
3416 return got_error_from_errno("calloc");
3417 TAILQ_INIT(ignorelist);
3419 while ((linelen = getline(&line, &linesize, f)) != -1) {
3420 if (linelen > 0 && line[linelen - 1] == '\n')
3421 line[linelen - 1] = '\0';
3423 /* Git's ignores may contain comments. */
3427 /* Git's negated patterns are not (yet?) supported. */
3431 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3433 err = got_error_from_errno("asprintf");
3436 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3441 err = got_error_from_errno("getline");
3445 dirpath = strdup(path);
3446 if (dirpath == NULL) {
3447 err = got_error_from_errno("strdup");
3450 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3453 if (err || pe == NULL) {
3455 free_ignorelist(ignorelist);
3461 match_ignores(struct got_pathlist_head *ignores, const char *path)
3463 struct got_pathlist_entry *pe;
3465 /* Handle patterns which match in all directories. */
3466 TAILQ_FOREACH(pe, ignores, entry) {
3467 struct got_pathlist_head *ignorelist = pe->data;
3468 struct got_pathlist_entry *pi;
3470 TAILQ_FOREACH(pi, ignorelist, entry) {
3471 const char *p, *pattern = pi->path;
3473 if (strncmp(pattern, "**/", 3) != 0)
3478 if (fnmatch(pattern, p,
3479 FNM_PATHNAME | FNM_LEADING_DIR)) {
3480 /* Retry in next directory. */
3481 while (*p && *p != '/')
3493 * The ignores pathlist contains ignore lists from children before
3494 * parents, so we can find the most specific ignorelist by walking
3495 * ignores backwards.
3497 pe = TAILQ_LAST(ignores, got_pathlist_head);
3499 if (got_path_is_child(path, pe->path, pe->path_len)) {
3500 struct got_pathlist_head *ignorelist = pe->data;
3501 struct got_pathlist_entry *pi;
3502 TAILQ_FOREACH(pi, ignorelist, entry) {
3503 const char *pattern = pi->path;
3504 int flags = FNM_LEADING_DIR;
3505 if (strstr(pattern, "/**/") == NULL)
3506 flags |= FNM_PATHNAME;
3507 if (fnmatch(pattern, path, flags))
3512 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3518 static const struct got_error *
3519 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3520 const char *path, int dirfd, const char *ignores_filename)
3522 const struct got_error *err = NULL;
3525 FILE *ignoresfile = NULL;
3527 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3528 path[0] ? "/" : "", ignores_filename) == -1)
3529 return got_error_from_errno("asprintf");
3532 fd = openat(dirfd, ignores_filename,
3533 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3535 if (errno != ENOENT && errno != EACCES)
3536 err = got_error_from_errno2("openat",
3539 ignoresfile = fdopen(fd, "r");
3540 if (ignoresfile == NULL)
3541 err = got_error_from_errno2("fdopen",
3545 err = read_ignores(ignores, path, ignoresfile);
3549 ignoresfile = fopen(ignorespath, "re");
3550 if (ignoresfile == NULL) {
3551 if (errno != ENOENT && errno != EACCES)
3552 err = got_error_from_errno2("fopen",
3555 err = read_ignores(ignores, path, ignoresfile);
3558 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3559 err = got_error_from_errno2("fclose", path);
3560 if (fd != -1 && close(fd) == -1 && err == NULL)
3561 err = got_error_from_errno2("close", path);
3566 static const struct got_error *
3567 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3570 const struct got_error *err = NULL;
3571 struct diff_dir_cb_arg *a = arg;
3577 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3578 return got_error(GOT_ERR_CANCELLED);
3580 if (parent_path[0]) {
3581 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3582 return got_error_from_errno("asprintf");
3587 if (de->d_type == DT_DIR) {
3588 if (!a->no_ignores && ignore != NULL &&
3589 match_ignores(a->ignores, path))
3591 } else if (!match_ignores(a->ignores, path) &&
3592 got_path_is_child(path, a->status_path, a->status_path_len))
3593 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3594 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3600 static const struct got_error *
3601 status_traverse(void *arg, const char *path, int dirfd)
3603 const struct got_error *err = NULL;
3604 struct diff_dir_cb_arg *a = arg;
3609 err = add_ignores(a->ignores, a->worktree->root_path,
3610 path, dirfd, ".cvsignore");
3614 err = add_ignores(a->ignores, a->worktree->root_path, path,
3615 dirfd, ".gitignore");
3620 static const struct got_error *
3621 report_single_file_status(const char *path, const char *ondisk_path,
3622 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3623 void *status_arg, struct got_repository *repo, int report_unchanged,
3624 struct got_pathlist_head *ignores, int no_ignores)
3626 struct got_fileindex_entry *ie;
3629 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3631 return report_file_status(ie, ondisk_path, -1, NULL,
3632 status_cb, status_arg, repo, report_unchanged);
3634 if (lstat(ondisk_path, &sb) == -1) {
3635 if (errno != ENOENT)
3636 return got_error_from_errno2("lstat", ondisk_path);
3637 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3638 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3641 if (!no_ignores && match_ignores(ignores, path))
3644 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3645 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3646 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3651 static const struct got_error *
3652 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3653 const char *root_path, const char *path)
3655 const struct got_error *err;
3656 char *parent_path, *next_parent_path = NULL;
3658 err = add_ignores(ignores, root_path, "", -1,
3663 err = add_ignores(ignores, root_path, "", -1,
3668 err = got_path_dirname(&parent_path, path);
3670 if (err->code == GOT_ERR_BAD_PATH)
3671 return NULL; /* cannot traverse parent */
3675 err = add_ignores(ignores, root_path, parent_path, -1,
3679 err = add_ignores(ignores, root_path, parent_path, -1,
3683 err = got_path_dirname(&next_parent_path, parent_path);
3685 if (err->code == GOT_ERR_BAD_PATH)
3686 err = NULL; /* traversed everything */
3689 if (got_path_is_root_dir(parent_path))
3692 parent_path = next_parent_path;
3693 next_parent_path = NULL;
3697 free(next_parent_path);
3701 static const struct got_error *
3702 worktree_status(struct got_worktree *worktree, const char *path,
3703 struct got_fileindex *fileindex, struct got_repository *repo,
3704 got_worktree_status_cb status_cb, void *status_arg,
3705 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3706 int report_unchanged)
3708 const struct got_error *err = NULL;
3710 struct got_fileindex_diff_dir_cb fdiff_cb;
3711 struct diff_dir_cb_arg arg;
3712 char *ondisk_path = NULL;
3713 struct got_pathlist_head ignores;
3714 struct got_fileindex_entry *ie;
3716 TAILQ_INIT(&ignores);
3718 if (asprintf(&ondisk_path, "%s%s%s",
3719 worktree->root_path, path[0] ? "/" : "", path) == -1)
3720 return got_error_from_errno("asprintf");
3722 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3724 err = report_single_file_status(path, ondisk_path,
3725 fileindex, status_cb, status_arg, repo,
3726 report_unchanged, &ignores, no_ignores);
3730 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3732 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3733 !got_err_open_nofollow_on_symlink())
3734 err = got_error_from_errno2("open", ondisk_path);
3737 err = add_ignores_from_parent_paths(&ignores,
3738 worktree->root_path, ondisk_path);
3742 err = report_single_file_status(path, ondisk_path,
3743 fileindex, status_cb, status_arg, repo,
3744 report_unchanged, &ignores, no_ignores);
3747 fdiff_cb.diff_old_new = status_old_new;
3748 fdiff_cb.diff_old = status_old;
3749 fdiff_cb.diff_new = status_new;
3750 fdiff_cb.diff_traverse = status_traverse;
3751 arg.fileindex = fileindex;
3752 arg.worktree = worktree;
3753 arg.status_path = path;
3754 arg.status_path_len = strlen(path);
3756 arg.status_cb = status_cb;
3757 arg.status_arg = status_arg;
3758 arg.cancel_cb = cancel_cb;
3759 arg.cancel_arg = cancel_arg;
3760 arg.report_unchanged = report_unchanged;
3761 arg.no_ignores = no_ignores;
3763 err = add_ignores_from_parent_paths(&ignores,
3764 worktree->root_path, path);
3768 arg.ignores = &ignores;
3769 err = got_fileindex_diff_dir(fileindex, fd,
3770 worktree->root_path, path, repo, &fdiff_cb, &arg);
3773 free_ignores(&ignores);
3774 if (fd != -1 && close(fd) == -1 && err == NULL)
3775 err = got_error_from_errno("close");
3780 const struct got_error *
3781 got_worktree_status(struct got_worktree *worktree,
3782 struct got_pathlist_head *paths, struct got_repository *repo,
3783 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3784 got_cancel_cb cancel_cb, void *cancel_arg)
3786 const struct got_error *err = NULL;
3787 char *fileindex_path = NULL;
3788 struct got_fileindex *fileindex = NULL;
3789 struct got_pathlist_entry *pe;
3791 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3795 TAILQ_FOREACH(pe, paths, entry) {
3796 err = worktree_status(worktree, pe->path, fileindex, repo,
3797 status_cb, status_arg, cancel_cb, cancel_arg,
3802 free(fileindex_path);
3803 got_fileindex_free(fileindex);
3807 const struct got_error *
3808 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3811 const struct got_error *err = NULL;
3812 char *resolved = NULL, *cwd = NULL, *path = NULL;
3815 char *abspath = NULL;
3816 char canonpath[PATH_MAX];
3820 cwd = getcwd(NULL, 0);
3822 return got_error_from_errno("getcwd");
3824 if (lstat(arg, &sb) == -1) {
3825 if (errno != ENOENT) {
3826 err = got_error_from_errno2("lstat", arg);
3831 if (S_ISLNK(sb.st_mode)) {
3833 * We cannot use realpath(3) with symlinks since we want to
3834 * operate on the symlink itself.
3835 * But we can make the path absolute, assuming it is relative
3836 * to the current working directory, and then canonicalize it.
3838 if (!got_path_is_absolute(arg)) {
3839 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3840 err = got_error_from_errno("asprintf");
3845 err = got_canonpath(abspath ? abspath : arg, canonpath,
3849 resolved = strdup(canonpath);
3850 if (resolved == NULL) {
3851 err = got_error_from_errno("strdup");
3855 resolved = realpath(arg, NULL);
3856 if (resolved == NULL) {
3857 if (errno != ENOENT) {
3858 err = got_error_from_errno2("realpath", arg);
3861 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3862 err = got_error_from_errno("asprintf");
3865 err = got_canonpath(abspath, canonpath,
3869 resolved = strdup(canonpath);
3870 if (resolved == NULL) {
3871 err = got_error_from_errno("strdup");
3877 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3878 strlen(got_worktree_get_root_path(worktree)))) {
3879 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3883 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3884 err = got_path_skip_common_ancestor(&path,
3885 got_worktree_get_root_path(worktree), resolved);
3891 err = got_error_from_errno("strdup");
3896 /* XXX status walk can't deal with trailing slash! */
3898 while (len > 0 && path[len - 1] == '/') {
3899 path[len - 1] = '\0';
3913 struct schedule_addition_args {
3914 struct got_worktree *worktree;
3915 struct got_fileindex *fileindex;
3916 got_worktree_checkout_cb progress_cb;
3918 struct got_repository *repo;
3921 static const struct got_error *
3922 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3923 const char *relpath, struct got_object_id *blob_id,
3924 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3925 int dirfd, const char *de_name)
3927 struct schedule_addition_args *a = arg;
3928 const struct got_error *err = NULL;
3929 struct got_fileindex_entry *ie;
3933 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3935 return got_error_from_errno("asprintf");
3937 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3939 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3943 /* Re-adding an existing entry is a no-op. */
3944 if (status == GOT_STATUS_ADD)
3946 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3951 if (status != GOT_STATUS_UNVERSIONED) {
3952 if (status == GOT_STATUS_NONEXISTENT)
3953 err = got_error_set_errno(ENOENT, ondisk_path);
3955 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3959 err = got_fileindex_entry_alloc(&ie, relpath);
3962 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3963 relpath, NULL, NULL, 1);
3965 got_fileindex_entry_free(ie);
3968 err = got_fileindex_entry_add(a->fileindex, ie);
3970 got_fileindex_entry_free(ie);
3977 if (status == GOT_STATUS_ADD)
3979 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3982 const struct got_error *
3983 got_worktree_schedule_add(struct got_worktree *worktree,
3984 struct got_pathlist_head *paths,
3985 got_worktree_checkout_cb progress_cb, void *progress_arg,
3986 struct got_repository *repo, int no_ignores)
3988 struct got_fileindex *fileindex = NULL;
3989 char *fileindex_path = NULL;
3990 const struct got_error *err = NULL, *sync_err, *unlockerr;
3991 struct got_pathlist_entry *pe;
3992 struct schedule_addition_args saa;
3994 err = lock_worktree(worktree, LOCK_EX);
3998 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4002 saa.worktree = worktree;
4003 saa.fileindex = fileindex;
4004 saa.progress_cb = progress_cb;
4005 saa.progress_arg = progress_arg;
4008 TAILQ_FOREACH(pe, paths, entry) {
4009 err = worktree_status(worktree, pe->path, fileindex, repo,
4010 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4014 sync_err = sync_fileindex(fileindex, fileindex_path);
4015 if (sync_err && err == NULL)
4018 free(fileindex_path);
4020 got_fileindex_free(fileindex);
4021 unlockerr = lock_worktree(worktree, LOCK_SH);
4022 if (unlockerr && err == NULL)
4027 struct schedule_deletion_args {
4028 struct got_worktree *worktree;
4029 struct got_fileindex *fileindex;
4030 got_worktree_delete_cb progress_cb;
4032 struct got_repository *repo;
4033 int delete_local_mods;
4035 int ignore_missing_paths;
4036 const char *status_codes;
4039 static const struct got_error *
4040 schedule_for_deletion(void *arg, unsigned char status,
4041 unsigned char staged_status, const char *relpath,
4042 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4043 struct got_object_id *commit_id, int dirfd, const char *de_name)
4045 struct schedule_deletion_args *a = arg;
4046 const struct got_error *err = NULL;
4047 struct got_fileindex_entry *ie = NULL;
4051 if (status == GOT_STATUS_NONEXISTENT) {
4052 if (a->ignore_missing_paths)
4054 return got_error_set_errno(ENOENT, relpath);
4057 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4059 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4061 staged_status = get_staged_status(ie);
4062 if (staged_status != GOT_STATUS_NO_CHANGE) {
4063 if (staged_status == GOT_STATUS_DELETE)
4065 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4068 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4070 return got_error_from_errno("asprintf");
4072 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4077 if (a->status_codes) {
4078 size_t ncodes = strlen(a->status_codes);
4080 for (i = 0; i < ncodes ; i++) {
4081 if (status == a->status_codes[i])
4085 /* Do not delete files in non-matching status. */
4089 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4090 a->status_codes[i] != GOT_STATUS_MISSING) {
4091 static char msg[64];
4092 snprintf(msg, sizeof(msg),
4093 "invalid status code '%c'", a->status_codes[i]);
4094 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4099 if (status != GOT_STATUS_NO_CHANGE) {
4100 if (status == GOT_STATUS_DELETE)
4102 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4103 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4106 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4107 err = got_error_set_errno(ENOENT, relpath);
4110 if (status != GOT_STATUS_MODIFY &&
4111 status != GOT_STATUS_MISSING) {
4112 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4117 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4121 if (unlinkat(dirfd, de_name, 0) != 0) {
4122 err = got_error_from_errno2("unlinkat",
4126 } else if (unlink(ondisk_path) != 0) {
4127 err = got_error_from_errno2("unlink", ondisk_path);
4131 root_len = strlen(a->worktree->root_path);
4134 err = got_path_dirname(&parent, ondisk_path);
4138 ondisk_path = parent;
4139 if (rmdir(ondisk_path) == -1) {
4140 if (errno != ENOTEMPTY)
4141 err = got_error_from_errno2("rmdir",
4145 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4146 strlen(ondisk_path), root_len) != 0);
4149 got_fileindex_entry_mark_deleted_from_disk(ie);
4154 if (status == GOT_STATUS_DELETE)
4156 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4157 staged_status, relpath);
4160 const struct got_error *
4161 got_worktree_schedule_delete(struct got_worktree *worktree,
4162 struct got_pathlist_head *paths, int delete_local_mods,
4163 const char *status_codes,
4164 got_worktree_delete_cb progress_cb, void *progress_arg,
4165 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4167 struct got_fileindex *fileindex = NULL;
4168 char *fileindex_path = NULL;
4169 const struct got_error *err = NULL, *sync_err, *unlockerr;
4170 struct got_pathlist_entry *pe;
4171 struct schedule_deletion_args sda;
4173 err = lock_worktree(worktree, LOCK_EX);
4177 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4181 sda.worktree = worktree;
4182 sda.fileindex = fileindex;
4183 sda.progress_cb = progress_cb;
4184 sda.progress_arg = progress_arg;
4186 sda.delete_local_mods = delete_local_mods;
4187 sda.keep_on_disk = keep_on_disk;
4188 sda.ignore_missing_paths = ignore_missing_paths;
4189 sda.status_codes = status_codes;
4191 TAILQ_FOREACH(pe, paths, entry) {
4192 err = worktree_status(worktree, pe->path, fileindex, repo,
4193 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4197 sync_err = sync_fileindex(fileindex, fileindex_path);
4198 if (sync_err && err == NULL)
4201 free(fileindex_path);
4203 got_fileindex_free(fileindex);
4204 unlockerr = lock_worktree(worktree, LOCK_SH);
4205 if (unlockerr && err == NULL)
4210 static const struct got_error *
4211 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4213 const struct got_error *err = NULL;
4215 size_t linesize = 0, n;
4218 linelen = getline(&line, &linesize, infile);
4219 if (linelen == -1) {
4220 if (ferror(infile)) {
4221 err = got_error_from_errno("getline");
4227 n = fwrite(line, 1, linelen, outfile);
4229 err = got_ferror(outfile, GOT_ERR_IO);
4234 n = fwrite(line, 1, linelen, rejectfile);
4236 err = got_ferror(outfile, GOT_ERR_IO);
4243 static const struct got_error *
4244 skip_one_line(FILE *f)
4247 size_t linesize = 0;
4250 linelen = getline(&line, &linesize, f);
4251 if (linelen == -1) {
4253 return got_error_from_errno("getline");
4260 static const struct got_error *
4261 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4262 int start_old, int end_old, int start_new, int end_new,
4263 FILE *outfile, FILE *rejectfile)
4265 const struct got_error *err;
4267 /* Copy old file's lines leading up to patch. */
4268 while (!feof(f1) && *line_cur1 < start_old) {
4269 err = copy_one_line(f1, outfile, NULL);
4274 /* Skip new file's lines leading up to patch. */
4275 while (!feof(f2) && *line_cur2 < start_new) {
4277 err = copy_one_line(f2, NULL, rejectfile);
4279 err = skip_one_line(f2);
4284 /* Copy patched lines. */
4285 while (!feof(f2) && *line_cur2 <= end_new) {
4286 err = copy_one_line(f2, outfile, NULL);
4291 /* Skip over old file's replaced lines. */
4292 while (!feof(f1) && *line_cur1 <= end_old) {
4294 err = copy_one_line(f1, NULL, rejectfile);
4296 err = skip_one_line(f1);
4305 static const struct got_error *
4306 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4307 FILE *outfile, FILE *rejectfile)
4309 const struct got_error *err;
4312 /* Copy old file's lines until EOF. */
4314 err = copy_one_line(f1, outfile, NULL);
4321 /* Copy new file's lines until EOF. */
4323 err = copy_one_line(f2, NULL, rejectfile);
4333 static const struct got_error *
4334 apply_or_reject_change(int *choice, int *nchunks_used,
4335 struct diff_result *diff_result, int n,
4336 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4337 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4338 got_worktree_patch_cb patch_cb, void *patch_arg)
4340 const struct got_error *err = NULL;
4341 struct diff_chunk_context cc = {};
4342 int start_old, end_old, start_new, end_new;
4344 struct diff_output_unidiff_state *diff_state;
4345 struct diff_input_info diff_info;
4348 *choice = GOT_PATCH_CHOICE_NONE;
4350 /* Get changed line numbers without context lines for copy_change(). */
4351 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4352 start_old = cc.left.start;
4353 end_old = cc.left.end;
4354 start_new = cc.right.start;
4355 end_new = cc.right.end;
4357 /* Get the same change with context lines for display. */
4358 memset(&cc, 0, sizeof(cc));
4359 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4361 memset(&diff_info, 0, sizeof(diff_info));
4362 diff_info.left_path = relpath;
4363 diff_info.right_path = relpath;
4365 diff_state = diff_output_unidiff_state_alloc();
4366 if (diff_state == NULL)
4367 return got_error_set_errno(ENOMEM,
4368 "diff_output_unidiff_state_alloc");
4370 hunkfile = got_opentemp();
4371 if (hunkfile == NULL) {
4372 err = got_error_from_errno("got_opentemp");
4376 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4378 if (rc != DIFF_RC_OK) {
4379 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4383 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4384 err = got_ferror(hunkfile, GOT_ERR_IO);
4388 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4389 hunkfile, changeno, nchanges);
4394 case GOT_PATCH_CHOICE_YES:
4395 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4396 end_old, start_new, end_new, outfile, rejectfile);
4398 case GOT_PATCH_CHOICE_NO:
4399 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4400 end_old, start_new, end_new, rejectfile, outfile);
4402 case GOT_PATCH_CHOICE_QUIT:
4405 err = got_error(GOT_ERR_PATCH_CHOICE);
4409 diff_output_unidiff_state_free(diff_state);
4410 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4411 err = got_error_from_errno("fclose");
4415 struct revert_file_args {
4416 struct got_worktree *worktree;
4417 struct got_fileindex *fileindex;
4418 got_worktree_checkout_cb progress_cb;
4420 got_worktree_patch_cb patch_cb;
4422 struct got_repository *repo;
4423 int unlink_added_files;
4426 static const struct got_error *
4427 create_patched_content(char **path_outfile, int reverse_patch,
4428 struct got_object_id *blob_id, const char *path2,
4429 int dirfd2, const char *de_name2,
4430 const char *relpath, struct got_repository *repo,
4431 got_worktree_patch_cb patch_cb, void *patch_arg)
4433 const struct got_error *err, *free_err;
4434 struct got_blob_object *blob = NULL;
4435 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4436 int fd = -1, fd2 = -1;
4437 char link_target[PATH_MAX];
4438 ssize_t link_len = 0;
4439 char *path1 = NULL, *id_str = NULL;
4441 struct got_diffreg_result *diffreg_result = NULL;
4442 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4443 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4445 *path_outfile = NULL;
4447 err = got_object_id_str(&id_str, blob_id);
4452 fd2 = openat(dirfd2, de_name2,
4453 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4455 if (!got_err_open_nofollow_on_symlink()) {
4456 err = got_error_from_errno2("openat", path2);
4459 link_len = readlinkat(dirfd2, de_name2,
4460 link_target, sizeof(link_target));
4461 if (link_len == -1) {
4462 return got_error_from_errno2("readlinkat",
4465 sb2.st_mode = S_IFLNK;
4466 sb2.st_size = link_len;
4469 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4471 if (!got_err_open_nofollow_on_symlink()) {
4472 err = got_error_from_errno2("open", path2);
4475 link_len = readlink(path2, link_target,
4476 sizeof(link_target));
4478 return got_error_from_errno2("readlink", path2);
4479 sb2.st_mode = S_IFLNK;
4480 sb2.st_size = link_len;
4484 if (fstat(fd2, &sb2) == -1) {
4485 err = got_error_from_errno2("fstat", path2);
4489 f2 = fdopen(fd2, "r");
4491 err = got_error_from_errno2("fdopen", path2);
4497 f2 = got_opentemp();
4499 err = got_error_from_errno2("got_opentemp", path2);
4502 n = fwrite(link_target, 1, link_len, f2);
4503 if (n != link_len) {
4504 err = got_ferror(f2, GOT_ERR_IO);
4507 if (fflush(f2) == EOF) {
4508 err = got_error_from_errno("fflush");
4514 fd = got_opentempfd();
4516 err = got_error_from_errno("got_opentempfd");
4520 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4524 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4528 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4532 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4537 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4541 if (fseek(f1, 0L, SEEK_SET) == -1)
4542 return got_ferror(f1, GOT_ERR_IO);
4543 if (fseek(f2, 0L, SEEK_SET) == -1)
4544 return got_ferror(f2, GOT_ERR_IO);
4546 /* Count the number of actual changes in the diff result. */
4547 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4548 struct diff_chunk_context cc = {};
4549 diff_chunk_context_load_change(&cc, &nchunks_used,
4550 diffreg_result->result, n, 0);
4553 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4555 err = apply_or_reject_change(&choice, &nchunks_used,
4556 diffreg_result->result, n, relpath, f1, f2,
4557 &line_cur1, &line_cur2,
4558 reverse_patch ? NULL : outfile,
4559 reverse_patch ? outfile : NULL,
4560 ++i, nchanges, patch_cb, patch_arg);
4563 if (choice == GOT_PATCH_CHOICE_YES)
4565 else if (choice == GOT_PATCH_CHOICE_QUIT)
4569 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4570 reverse_patch ? NULL : outfile,
4571 reverse_patch ? outfile : NULL);
4575 if (!S_ISLNK(sb2.st_mode)) {
4576 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4577 err = got_error_from_errno2("fchmod", path2);
4584 if (fd != -1 && close(fd) == -1 && err == NULL)
4585 err = got_error_from_errno("close");
4587 got_object_blob_close(blob);
4588 free_err = got_diffreg_result_free(diffreg_result);
4591 if (f1 && fclose(f1) == EOF && err == NULL)
4592 err = got_error_from_errno2("fclose", path1);
4593 if (f2 && fclose(f2) == EOF && err == NULL)
4594 err = got_error_from_errno2("fclose", path2);
4595 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4596 err = got_error_from_errno2("close", path2);
4597 if (outfile && fclose(outfile) == EOF && err == NULL)
4598 err = got_error_from_errno2("fclose", *path_outfile);
4599 if (path1 && unlink(path1) == -1 && err == NULL)
4600 err = got_error_from_errno2("unlink", path1);
4601 if (err || !have_content) {
4602 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4603 err = got_error_from_errno2("unlink", *path_outfile);
4604 free(*path_outfile);
4605 *path_outfile = NULL;
4611 static const struct got_error *
4612 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4613 const char *relpath, struct got_object_id *blob_id,
4614 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4615 int dirfd, const char *de_name)
4617 struct revert_file_args *a = arg;
4618 const struct got_error *err = NULL;
4619 char *parent_path = NULL;
4620 struct got_fileindex_entry *ie;
4621 struct got_commit_object *base_commit = NULL;
4622 struct got_tree_object *tree = NULL;
4623 struct got_object_id *tree_id = NULL;
4624 const struct got_tree_entry *te = NULL;
4625 char *tree_path = NULL, *te_name;
4626 char *ondisk_path = NULL, *path_content = NULL;
4627 struct got_blob_object *blob = NULL;
4630 /* Reverting a staged deletion is a no-op. */
4631 if (status == GOT_STATUS_DELETE &&
4632 staged_status != GOT_STATUS_NO_CHANGE)
4635 if (status == GOT_STATUS_UNVERSIONED)
4636 return (*a->progress_cb)(a->progress_arg,
4637 GOT_STATUS_UNVERSIONED, relpath);
4639 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4641 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4643 /* Construct in-repository path of tree which contains this blob. */
4644 err = got_path_dirname(&parent_path, ie->path);
4646 if (err->code != GOT_ERR_BAD_PATH)
4648 parent_path = strdup("/");
4649 if (parent_path == NULL) {
4650 err = got_error_from_errno("strdup");
4654 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4655 tree_path = strdup(parent_path);
4656 if (tree_path == NULL) {
4657 err = got_error_from_errno("strdup");
4661 if (got_path_is_root_dir(parent_path)) {
4662 tree_path = strdup(a->worktree->path_prefix);
4663 if (tree_path == NULL) {
4664 err = got_error_from_errno("strdup");
4668 if (asprintf(&tree_path, "%s/%s",
4669 a->worktree->path_prefix, parent_path) == -1) {
4670 err = got_error_from_errno("asprintf");
4676 err = got_object_open_as_commit(&base_commit, a->repo,
4677 a->worktree->base_commit_id);
4681 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4683 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4684 (status == GOT_STATUS_ADD ||
4685 staged_status == GOT_STATUS_ADD)))
4688 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4692 err = got_path_basename(&te_name, ie->path);
4696 te = got_object_tree_find_entry(tree, te_name);
4698 if (te == NULL && status != GOT_STATUS_ADD &&
4699 staged_status != GOT_STATUS_ADD) {
4700 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4706 case GOT_STATUS_ADD:
4708 int choice = GOT_PATCH_CHOICE_NONE;
4709 err = (*a->patch_cb)(&choice, a->patch_arg,
4710 status, ie->path, NULL, 1, 1);
4713 if (choice != GOT_PATCH_CHOICE_YES)
4716 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4720 got_fileindex_entry_remove(a->fileindex, ie);
4721 if (a->unlink_added_files) {
4722 if (asprintf(&ondisk_path, "%s/%s",
4723 got_worktree_get_root_path(a->worktree),
4725 err = got_error_from_errno("asprintf");
4728 if (unlink(ondisk_path) == -1) {
4729 err = got_error_from_errno2("unlink",
4735 case GOT_STATUS_DELETE:
4737 int choice = GOT_PATCH_CHOICE_NONE;
4738 err = (*a->patch_cb)(&choice, a->patch_arg,
4739 status, ie->path, NULL, 1, 1);
4742 if (choice != GOT_PATCH_CHOICE_YES)
4746 case GOT_STATUS_MODIFY:
4747 case GOT_STATUS_MODE_CHANGE:
4748 case GOT_STATUS_CONFLICT:
4749 case GOT_STATUS_MISSING: {
4750 struct got_object_id id;
4751 if (staged_status == GOT_STATUS_ADD ||
4752 staged_status == GOT_STATUS_MODIFY) {
4753 memcpy(id.sha1, ie->staged_blob_sha1,
4754 SHA1_DIGEST_LENGTH);
4756 memcpy(id.sha1, ie->blob_sha1,
4757 SHA1_DIGEST_LENGTH);
4758 fd = got_opentempfd();
4760 err = got_error_from_errno("got_opentempfd");
4764 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4768 if (asprintf(&ondisk_path, "%s/%s",
4769 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4770 err = got_error_from_errno("asprintf");
4774 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4775 status == GOT_STATUS_CONFLICT)) {
4776 int is_bad_symlink = 0;
4777 err = create_patched_content(&path_content, 1, &id,
4778 ondisk_path, dirfd, de_name, ie->path, a->repo,
4779 a->patch_cb, a->patch_arg);
4780 if (err || path_content == NULL)
4782 if (te && S_ISLNK(te->mode)) {
4783 if (unlink(path_content) == -1) {
4784 err = got_error_from_errno2("unlink",
4788 err = install_symlink(&is_bad_symlink,
4789 a->worktree, ondisk_path, ie->path,
4790 blob, 0, 1, 0, 0, a->repo,
4791 a->progress_cb, a->progress_arg);
4793 if (rename(path_content, ondisk_path) == -1) {
4794 err = got_error_from_errno3("rename",
4795 path_content, ondisk_path);
4800 int is_bad_symlink = 0;
4801 if (te && S_ISLNK(te->mode)) {
4802 err = install_symlink(&is_bad_symlink,
4803 a->worktree, ondisk_path, ie->path,
4804 blob, 0, 1, 0, 0, a->repo,
4805 a->progress_cb, a->progress_arg);
4807 err = install_blob(a->worktree, ondisk_path,
4809 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4810 got_fileindex_perms_to_st(ie), blob,
4811 0, 1, 0, 0, a->repo,
4812 a->progress_cb, a->progress_arg);
4816 if (status == GOT_STATUS_DELETE ||
4817 status == GOT_STATUS_MODE_CHANGE) {
4818 err = got_fileindex_entry_update(ie,
4819 a->worktree->root_fd, relpath,
4821 a->worktree->base_commit_id->sha1, 1);
4825 if (is_bad_symlink) {
4826 got_fileindex_entry_filetype_set(ie,
4827 GOT_FILEIDX_MODE_BAD_SYMLINK);
4840 if (fd != -1 && close(fd) == -1 && err == NULL)
4841 err = got_error_from_errno("close");
4843 got_object_blob_close(blob);
4845 got_object_tree_close(tree);
4848 got_object_commit_close(base_commit);
4852 const struct got_error *
4853 got_worktree_revert(struct got_worktree *worktree,
4854 struct got_pathlist_head *paths,
4855 got_worktree_checkout_cb progress_cb, void *progress_arg,
4856 got_worktree_patch_cb patch_cb, void *patch_arg,
4857 struct got_repository *repo)
4859 struct got_fileindex *fileindex = NULL;
4860 char *fileindex_path = NULL;
4861 const struct got_error *err = NULL, *unlockerr = NULL;
4862 const struct got_error *sync_err = NULL;
4863 struct got_pathlist_entry *pe;
4864 struct revert_file_args rfa;
4866 err = lock_worktree(worktree, LOCK_EX);
4870 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4874 rfa.worktree = worktree;
4875 rfa.fileindex = fileindex;
4876 rfa.progress_cb = progress_cb;
4877 rfa.progress_arg = progress_arg;
4878 rfa.patch_cb = patch_cb;
4879 rfa.patch_arg = patch_arg;
4881 rfa.unlink_added_files = 0;
4882 TAILQ_FOREACH(pe, paths, entry) {
4883 err = worktree_status(worktree, pe->path, fileindex, repo,
4884 revert_file, &rfa, NULL, NULL, 1, 0);
4888 sync_err = sync_fileindex(fileindex, fileindex_path);
4889 if (sync_err && err == NULL)
4892 free(fileindex_path);
4894 got_fileindex_free(fileindex);
4895 unlockerr = lock_worktree(worktree, LOCK_SH);
4896 if (unlockerr && err == NULL)
4902 free_commitable(struct got_commitable *ct)
4905 free(ct->in_repo_path);
4906 free(ct->ondisk_path);
4908 free(ct->base_blob_id);
4909 free(ct->staged_blob_id);
4910 free(ct->base_commit_id);
4914 struct collect_commitables_arg {
4915 struct got_pathlist_head *commitable_paths;
4916 struct got_repository *repo;
4917 struct got_worktree *worktree;
4918 struct got_fileindex *fileindex;
4919 int have_staged_files;
4920 int allow_bad_symlinks;
4923 static const struct got_error *
4924 collect_commitables(void *arg, unsigned char status,
4925 unsigned char staged_status, const char *relpath,
4926 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4927 struct got_object_id *commit_id, int dirfd, const char *de_name)
4929 struct collect_commitables_arg *a = arg;
4930 const struct got_error *err = NULL;
4931 struct got_commitable *ct = NULL;
4932 struct got_pathlist_entry *new = NULL;
4933 char *parent_path = NULL, *path = NULL;
4936 if (a->have_staged_files) {
4937 if (staged_status != GOT_STATUS_MODIFY &&
4938 staged_status != GOT_STATUS_ADD &&
4939 staged_status != GOT_STATUS_DELETE)
4942 if (status == GOT_STATUS_CONFLICT)
4943 return got_error(GOT_ERR_COMMIT_CONFLICT);
4945 if (status != GOT_STATUS_MODIFY &&
4946 status != GOT_STATUS_MODE_CHANGE &&
4947 status != GOT_STATUS_ADD &&
4948 status != GOT_STATUS_DELETE)
4952 if (asprintf(&path, "/%s", relpath) == -1) {
4953 err = got_error_from_errno("asprintf");
4956 if (strcmp(path, "/") == 0) {
4957 parent_path = strdup("");
4958 if (parent_path == NULL)
4959 return got_error_from_errno("strdup");
4961 err = got_path_dirname(&parent_path, path);
4966 ct = calloc(1, sizeof(*ct));
4968 err = got_error_from_errno("calloc");
4972 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4974 err = got_error_from_errno("asprintf");
4978 if (staged_status == GOT_STATUS_ADD ||
4979 staged_status == GOT_STATUS_MODIFY) {
4980 struct got_fileindex_entry *ie;
4981 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4982 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4983 case GOT_FILEIDX_MODE_REGULAR_FILE:
4984 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4987 case GOT_FILEIDX_MODE_SYMLINK:
4991 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4994 ct->mode |= got_fileindex_entry_perms_get(ie);
4995 } else if (status != GOT_STATUS_DELETE &&
4996 staged_status != GOT_STATUS_DELETE) {
4998 if (fstatat(dirfd, de_name, &sb,
4999 AT_SYMLINK_NOFOLLOW) == -1) {
5000 err = got_error_from_errno2("fstatat",
5004 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5005 err = got_error_from_errno2("lstat", ct->ondisk_path);
5008 ct->mode = sb.st_mode;
5011 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5012 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5014 err = got_error_from_errno("asprintf");
5018 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5019 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5021 char target_path[PATH_MAX];
5023 target_len = readlink(ct->ondisk_path, target_path,
5024 sizeof(target_path));
5025 if (target_len == -1) {
5026 err = got_error_from_errno2("readlink",
5030 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5031 target_len, ct->ondisk_path, a->worktree->root_path);
5034 if (is_bad_symlink) {
5035 err = got_error_path(ct->ondisk_path,
5036 GOT_ERR_BAD_SYMLINK);
5042 ct->status = status;
5043 ct->staged_status = staged_status;
5044 ct->blob_id = NULL; /* will be filled in when blob gets created */
5045 if (ct->status != GOT_STATUS_ADD &&
5046 ct->staged_status != GOT_STATUS_ADD) {
5047 ct->base_blob_id = got_object_id_dup(blob_id);
5048 if (ct->base_blob_id == NULL) {
5049 err = got_error_from_errno("got_object_id_dup");
5052 ct->base_commit_id = got_object_id_dup(commit_id);
5053 if (ct->base_commit_id == NULL) {
5054 err = got_error_from_errno("got_object_id_dup");
5058 if (ct->staged_status == GOT_STATUS_ADD ||
5059 ct->staged_status == GOT_STATUS_MODIFY) {
5060 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5061 if (ct->staged_blob_id == NULL) {
5062 err = got_error_from_errno("got_object_id_dup");
5066 ct->path = strdup(path);
5067 if (ct->path == NULL) {
5068 err = got_error_from_errno("strdup");
5071 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5073 if (ct && (err || new == NULL))
5074 free_commitable(ct);
5080 static const struct got_error *write_tree(struct got_object_id **, int *,
5081 struct got_tree_object *, const char *, struct got_pathlist_head *,
5082 got_worktree_status_cb status_cb, void *status_arg,
5083 struct got_repository *);
5085 static const struct got_error *
5086 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5087 struct got_tree_entry *te, const char *parent_path,
5088 struct got_pathlist_head *commitable_paths,
5089 got_worktree_status_cb status_cb, void *status_arg,
5090 struct got_repository *repo)
5092 const struct got_error *err = NULL;
5093 struct got_tree_object *subtree;
5096 if (asprintf(&subpath, "%s%s%s", parent_path,
5097 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5098 return got_error_from_errno("asprintf");
5100 err = got_object_open_as_tree(&subtree, repo, &te->id);
5104 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5105 commitable_paths, status_cb, status_arg, repo);
5106 got_object_tree_close(subtree);
5111 static const struct got_error *
5112 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5114 const struct got_error *err = NULL;
5115 char *ct_parent_path = NULL;
5119 if (strchr(ct->in_repo_path, '/') == NULL) {
5120 *match = got_path_is_root_dir(path);
5124 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5127 *match = (strcmp(path, ct_parent_path) == 0);
5128 free(ct_parent_path);
5133 get_ct_file_mode(struct got_commitable *ct)
5135 if (S_ISLNK(ct->mode))
5138 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5141 static const struct got_error *
5142 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5143 struct got_tree_entry *te, struct got_commitable *ct)
5145 const struct got_error *err = NULL;
5149 err = got_object_tree_entry_dup(new_te, te);
5153 (*new_te)->mode = get_ct_file_mode(ct);
5155 if (ct->staged_status == GOT_STATUS_MODIFY)
5156 memcpy(&(*new_te)->id, ct->staged_blob_id,
5157 sizeof((*new_te)->id));
5159 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5161 if (err && *new_te) {
5168 static const struct got_error *
5169 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5170 struct got_commitable *ct)
5172 const struct got_error *err = NULL;
5173 char *ct_name = NULL;
5177 *new_te = calloc(1, sizeof(**new_te));
5178 if (*new_te == NULL)
5179 return got_error_from_errno("calloc");
5181 err = got_path_basename(&ct_name, ct->path);
5184 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5185 sizeof((*new_te)->name)) {
5186 err = got_error(GOT_ERR_NO_SPACE);
5190 (*new_te)->mode = get_ct_file_mode(ct);
5192 if (ct->staged_status == GOT_STATUS_ADD)
5193 memcpy(&(*new_te)->id, ct->staged_blob_id,
5194 sizeof((*new_te)->id));
5196 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5199 if (err && *new_te) {
5206 static const struct got_error *
5207 insert_tree_entry(struct got_tree_entry *new_te,
5208 struct got_pathlist_head *paths)
5210 const struct got_error *err = NULL;
5211 struct got_pathlist_entry *new_pe;
5213 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5217 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5221 static const struct got_error *
5222 report_ct_status(struct got_commitable *ct,
5223 got_worktree_status_cb status_cb, void *status_arg)
5225 const char *ct_path = ct->path;
5226 unsigned char status;
5228 if (status_cb == NULL) /* no commit progress output desired */
5231 while (ct_path[0] == '/')
5234 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5235 status = ct->staged_status;
5237 status = ct->status;
5239 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5240 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5243 static const struct got_error *
5244 match_modified_subtree(int *modified, struct got_tree_entry *te,
5245 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5247 const struct got_error *err = NULL;
5248 struct got_pathlist_entry *pe;
5253 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5254 got_path_is_root_dir(base_tree_path) ? "" : "/",
5256 return got_error_from_errno("asprintf");
5258 TAILQ_FOREACH(pe, commitable_paths, entry) {
5259 struct got_commitable *ct = pe->data;
5260 *modified = got_path_is_child(ct->in_repo_path, te_path,
5270 static const struct got_error *
5271 match_deleted_or_modified_ct(struct got_commitable **ctp,
5272 struct got_tree_entry *te, const char *base_tree_path,
5273 struct got_pathlist_head *commitable_paths)
5275 const struct got_error *err = NULL;
5276 struct got_pathlist_entry *pe;
5280 TAILQ_FOREACH(pe, commitable_paths, entry) {
5281 struct got_commitable *ct = pe->data;
5282 char *ct_name = NULL;
5285 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5286 if (ct->status != GOT_STATUS_MODIFY &&
5287 ct->status != GOT_STATUS_MODE_CHANGE &&
5288 ct->status != GOT_STATUS_DELETE)
5291 if (ct->staged_status != GOT_STATUS_MODIFY &&
5292 ct->staged_status != GOT_STATUS_DELETE)
5296 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5299 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5305 err = got_path_basename(&ct_name, pe->path);
5309 if (strcmp(te->name, ct_name) != 0) {
5322 static const struct got_error *
5323 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5324 const char *child_path, const char *path_base_tree,
5325 struct got_pathlist_head *commitable_paths,
5326 got_worktree_status_cb status_cb, void *status_arg,
5327 struct got_repository *repo)
5329 const struct got_error *err = NULL;
5330 struct got_tree_entry *new_te;
5332 struct got_object_id *id = NULL;
5337 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5338 got_path_is_root_dir(path_base_tree) ? "" : "/",
5340 return got_error_from_errno("asprintf");
5342 new_te = calloc(1, sizeof(*new_te));
5344 return got_error_from_errno("calloc");
5345 new_te->mode = S_IFDIR;
5347 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5348 sizeof(new_te->name)) {
5349 err = got_error(GOT_ERR_NO_SPACE);
5352 err = write_tree(&id, &nentries, NULL, subtree_path,
5353 commitable_paths, status_cb, status_arg, repo);
5358 memcpy(&new_te->id, id, sizeof(new_te->id));
5367 static const struct got_error *
5368 write_tree(struct got_object_id **new_tree_id, int *nentries,
5369 struct got_tree_object *base_tree, const char *path_base_tree,
5370 struct got_pathlist_head *commitable_paths,
5371 got_worktree_status_cb status_cb, void *status_arg,
5372 struct got_repository *repo)
5374 const struct got_error *err = NULL;
5375 struct got_pathlist_head paths;
5376 struct got_tree_entry *te, *new_te = NULL;
5377 struct got_pathlist_entry *pe;
5382 /* Insert, and recurse into, newly added entries first. */
5383 TAILQ_FOREACH(pe, commitable_paths, entry) {
5384 struct got_commitable *ct = pe->data;
5385 char *child_path = NULL, *slash;
5387 if ((ct->status != GOT_STATUS_ADD &&
5388 ct->staged_status != GOT_STATUS_ADD) ||
5389 (ct->flags & GOT_COMMITABLE_ADDED))
5392 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5393 strlen(path_base_tree)))
5396 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5401 slash = strchr(child_path, '/');
5402 if (slash == NULL) {
5403 err = alloc_added_blob_tree_entry(&new_te, ct);
5406 err = report_ct_status(ct, status_cb, status_arg);
5409 ct->flags |= GOT_COMMITABLE_ADDED;
5410 err = insert_tree_entry(new_te, &paths);
5415 *slash = '\0'; /* trim trailing path components */
5416 if (base_tree == NULL ||
5417 got_object_tree_find_entry(base_tree, child_path)
5419 err = make_subtree_for_added_blob(&new_te,
5420 child_path, path_base_tree,
5421 commitable_paths, status_cb, status_arg,
5425 err = insert_tree_entry(new_te, &paths);
5434 int i, nbase_entries;
5435 /* Handle modified and deleted entries. */
5436 nbase_entries = got_object_tree_get_nentries(base_tree);
5437 for (i = 0; i < nbase_entries; i++) {
5438 struct got_commitable *ct = NULL;
5440 te = got_object_tree_get_entry(base_tree, i);
5441 if (got_object_tree_entry_is_submodule(te)) {
5442 /* Entry is a submodule; just copy it. */
5443 err = got_object_tree_entry_dup(&new_te, te);
5446 err = insert_tree_entry(new_te, &paths);
5453 if (S_ISDIR(te->mode)) {
5455 err = got_object_tree_entry_dup(&new_te, te);
5458 err = match_modified_subtree(&modified, te,
5459 path_base_tree, commitable_paths);
5462 /* Avoid recursion into unmodified subtrees. */
5464 struct got_object_id *new_id;
5466 err = write_subtree(&new_id,
5468 path_base_tree, commitable_paths,
5469 status_cb, status_arg, repo);
5472 if (nsubentries == 0) {
5473 /* All entries were deleted. */
5477 memcpy(&new_te->id, new_id,
5478 sizeof(new_te->id));
5481 err = insert_tree_entry(new_te, &paths);
5488 err = match_deleted_or_modified_ct(&ct, te,
5489 path_base_tree, commitable_paths);
5493 /* NB: Deleted entries get dropped here. */
5494 if (ct->status == GOT_STATUS_MODIFY ||
5495 ct->status == GOT_STATUS_MODE_CHANGE ||
5496 ct->staged_status == GOT_STATUS_MODIFY) {
5497 err = alloc_modified_blob_tree_entry(
5501 err = insert_tree_entry(new_te, &paths);
5506 err = report_ct_status(ct, status_cb,
5511 /* Entry is unchanged; just copy it. */
5512 err = got_object_tree_entry_dup(&new_te, te);
5515 err = insert_tree_entry(new_te, &paths);
5523 /* Write new list of entries; deleted entries have been dropped. */
5524 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5526 got_pathlist_free(&paths);
5530 static const struct got_error *
5531 update_fileindex_after_commit(struct got_worktree *worktree,
5532 struct got_pathlist_head *commitable_paths,
5533 struct got_object_id *new_base_commit_id,
5534 struct got_fileindex *fileindex, int have_staged_files)
5536 const struct got_error *err = NULL;
5537 struct got_pathlist_entry *pe;
5538 char *relpath = NULL;
5540 TAILQ_FOREACH(pe, commitable_paths, entry) {
5541 struct got_fileindex_entry *ie;
5542 struct got_commitable *ct = pe->data;
5544 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5546 err = got_path_skip_common_ancestor(&relpath,
5547 worktree->root_path, ct->ondisk_path);
5552 if (ct->status == GOT_STATUS_DELETE ||
5553 ct->staged_status == GOT_STATUS_DELETE) {
5554 got_fileindex_entry_remove(fileindex, ie);
5555 } else if (ct->staged_status == GOT_STATUS_ADD ||
5556 ct->staged_status == GOT_STATUS_MODIFY) {
5557 got_fileindex_entry_stage_set(ie,
5558 GOT_FILEIDX_STAGE_NONE);
5559 got_fileindex_entry_staged_filetype_set(ie, 0);
5561 err = got_fileindex_entry_update(ie,
5562 worktree->root_fd, relpath,
5563 ct->staged_blob_id->sha1,
5564 new_base_commit_id->sha1,
5565 !have_staged_files);
5567 err = got_fileindex_entry_update(ie,
5568 worktree->root_fd, relpath,
5570 new_base_commit_id->sha1,
5571 !have_staged_files);
5573 err = got_fileindex_entry_alloc(&ie, pe->path);
5576 err = got_fileindex_entry_update(ie,
5577 worktree->root_fd, relpath, ct->blob_id->sha1,
5578 new_base_commit_id->sha1, 1);
5580 got_fileindex_entry_free(ie);
5583 err = got_fileindex_entry_add(fileindex, ie);
5585 got_fileindex_entry_free(ie);
5598 static const struct got_error *
5599 check_out_of_date(const char *in_repo_path, unsigned char status,
5600 unsigned char staged_status, struct got_object_id *base_blob_id,
5601 struct got_object_id *base_commit_id,
5602 struct got_object_id *head_commit_id, struct got_repository *repo,
5605 const struct got_error *err = NULL;
5606 struct got_commit_object *commit = NULL;
5607 struct got_object_id *id = NULL;
5609 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5610 /* Trivial case: base commit == head commit */
5611 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5614 * Ensure file content which local changes were based
5615 * on matches file content in the branch head.
5617 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5620 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5622 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5623 err = got_error(ood_errcode);
5625 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5626 err = got_error(ood_errcode);
5628 /* Require that added files don't exist in the branch head. */
5629 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5632 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5633 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5635 err = id ? got_error(ood_errcode) : NULL;
5640 got_object_commit_close(commit);
5644 static const struct got_error *
5645 commit_worktree(struct got_object_id **new_commit_id,
5646 struct got_pathlist_head *commitable_paths,
5647 struct got_object_id *head_commit_id,
5648 struct got_object_id *parent_id2,
5649 struct got_worktree *worktree,
5650 const char *author, const char *committer,
5651 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5652 got_worktree_status_cb status_cb, void *status_arg,
5653 struct got_repository *repo)
5655 const struct got_error *err = NULL, *unlockerr = NULL;
5656 struct got_pathlist_entry *pe;
5657 const char *head_ref_name = NULL;
5658 struct got_commit_object *head_commit = NULL;
5659 struct got_reference *head_ref2 = NULL;
5660 struct got_object_id *head_commit_id2 = NULL;
5661 struct got_tree_object *head_tree = NULL;
5662 struct got_object_id *new_tree_id = NULL;
5663 int nentries, nparents = 0;
5664 struct got_object_id_queue parent_ids;
5665 struct got_object_qid *pid = NULL;
5666 char *logmsg = NULL;
5668 *new_commit_id = NULL;
5670 STAILQ_INIT(&parent_ids);
5672 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5676 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5680 if (commit_msg_cb != NULL) {
5681 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5686 if (logmsg == NULL || strlen(logmsg) == 0) {
5687 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5691 /* Create blobs from added and modified files and record their IDs. */
5692 TAILQ_FOREACH(pe, commitable_paths, entry) {
5693 struct got_commitable *ct = pe->data;
5696 /* Blobs for staged files already exist. */
5697 if (ct->staged_status == GOT_STATUS_ADD ||
5698 ct->staged_status == GOT_STATUS_MODIFY)
5701 if (ct->status != GOT_STATUS_ADD &&
5702 ct->status != GOT_STATUS_MODIFY &&
5703 ct->status != GOT_STATUS_MODE_CHANGE)
5706 if (asprintf(&ondisk_path, "%s/%s",
5707 worktree->root_path, pe->path) == -1) {
5708 err = got_error_from_errno("asprintf");
5711 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5717 /* Recursively write new tree objects. */
5718 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5719 commitable_paths, status_cb, status_arg, repo);
5723 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5726 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5729 err = got_object_qid_alloc(&pid, parent_id2);
5732 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5735 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5736 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5742 /* Check if a concurrent commit to our branch has occurred. */
5743 head_ref_name = got_worktree_get_head_ref_name(worktree);
5744 if (head_ref_name == NULL) {
5745 err = got_error_from_errno("got_worktree_get_head_ref_name");
5748 /* Lock the reference here to prevent concurrent modification. */
5749 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5752 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5755 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5756 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5759 /* Update branch head in repository. */
5760 err = got_ref_change_ref(head_ref2, *new_commit_id);
5763 err = got_ref_write(head_ref2, repo);
5767 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5771 err = ref_base_commit(worktree, repo);
5775 got_object_id_queue_free(&parent_ids);
5777 got_object_tree_close(head_tree);
5779 got_object_commit_close(head_commit);
5780 free(head_commit_id2);
5782 unlockerr = got_ref_unlock(head_ref2);
5783 if (unlockerr && err == NULL)
5785 got_ref_close(head_ref2);
5790 static const struct got_error *
5791 check_path_is_commitable(const char *path,
5792 struct got_pathlist_head *commitable_paths)
5794 struct got_pathlist_entry *cpe = NULL;
5795 size_t path_len = strlen(path);
5797 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5798 struct got_commitable *ct = cpe->data;
5799 const char *ct_path = ct->path;
5801 while (ct_path[0] == '/')
5804 if (strcmp(path, ct_path) == 0 ||
5805 got_path_is_child(ct_path, path, path_len))
5810 return got_error_path(path, GOT_ERR_BAD_PATH);
5815 static const struct got_error *
5816 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5818 int *have_staged_files = arg;
5820 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5821 *have_staged_files = 1;
5822 return got_error(GOT_ERR_CANCELLED);
5828 static const struct got_error *
5829 check_non_staged_files(struct got_fileindex *fileindex,
5830 struct got_pathlist_head *paths)
5832 struct got_pathlist_entry *pe;
5833 struct got_fileindex_entry *ie;
5835 TAILQ_FOREACH(pe, paths, entry) {
5836 if (pe->path[0] == '\0')
5838 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5840 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5841 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5842 return got_error_path(pe->path,
5843 GOT_ERR_FILE_NOT_STAGED);
5849 const struct got_error *
5850 got_worktree_commit(struct got_object_id **new_commit_id,
5851 struct got_worktree *worktree, struct got_pathlist_head *paths,
5852 const char *author, const char *committer, int allow_bad_symlinks,
5853 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5854 got_worktree_status_cb status_cb, void *status_arg,
5855 struct got_repository *repo)
5857 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5858 struct got_fileindex *fileindex = NULL;
5859 char *fileindex_path = NULL;
5860 struct got_pathlist_head commitable_paths;
5861 struct collect_commitables_arg cc_arg;
5862 struct got_pathlist_entry *pe;
5863 struct got_reference *head_ref = NULL;
5864 struct got_object_id *head_commit_id = NULL;
5865 int have_staged_files = 0;
5867 *new_commit_id = NULL;
5869 TAILQ_INIT(&commitable_paths);
5871 err = lock_worktree(worktree, LOCK_EX);
5875 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5879 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5883 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5887 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5888 &have_staged_files);
5889 if (err && err->code != GOT_ERR_CANCELLED)
5891 if (have_staged_files) {
5892 err = check_non_staged_files(fileindex, paths);
5897 cc_arg.commitable_paths = &commitable_paths;
5898 cc_arg.worktree = worktree;
5899 cc_arg.fileindex = fileindex;
5901 cc_arg.have_staged_files = have_staged_files;
5902 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5903 TAILQ_FOREACH(pe, paths, entry) {
5904 err = worktree_status(worktree, pe->path, fileindex, repo,
5905 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5910 if (TAILQ_EMPTY(&commitable_paths)) {
5911 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5915 TAILQ_FOREACH(pe, paths, entry) {
5916 err = check_path_is_commitable(pe->path, &commitable_paths);
5921 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5922 struct got_commitable *ct = pe->data;
5923 const char *ct_path = ct->in_repo_path;
5925 while (ct_path[0] == '/')
5927 err = check_out_of_date(ct_path, ct->status,
5928 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5929 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5935 err = commit_worktree(new_commit_id, &commitable_paths,
5936 head_commit_id, NULL, worktree, author, committer,
5937 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5941 err = update_fileindex_after_commit(worktree, &commitable_paths,
5942 *new_commit_id, fileindex, have_staged_files);
5943 sync_err = sync_fileindex(fileindex, fileindex_path);
5944 if (sync_err && err == NULL)
5948 got_fileindex_free(fileindex);
5949 free(fileindex_path);
5950 unlockerr = lock_worktree(worktree, LOCK_SH);
5951 if (unlockerr && err == NULL)
5953 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5954 struct got_commitable *ct = pe->data;
5955 free_commitable(ct);
5957 got_pathlist_free(&commitable_paths);
5962 got_commitable_get_path(struct got_commitable *ct)
5968 got_commitable_get_status(struct got_commitable *ct)
5973 struct check_rebase_ok_arg {
5974 struct got_worktree *worktree;
5975 struct got_repository *repo;
5978 static const struct got_error *
5979 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5981 const struct got_error *err = NULL;
5982 struct check_rebase_ok_arg *a = arg;
5983 unsigned char status;
5987 /* Reject rebase of a work tree with mixed base commits. */
5988 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5989 SHA1_DIGEST_LENGTH))
5990 return got_error(GOT_ERR_MIXED_COMMITS);
5992 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5994 return got_error_from_errno("asprintf");
5996 /* Reject rebase of a work tree with modified or staged files. */
5997 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6002 if (status != GOT_STATUS_NO_CHANGE)
6003 return got_error(GOT_ERR_MODIFIED);
6004 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6005 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6010 const struct got_error *
6011 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6012 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6013 struct got_worktree *worktree, struct got_reference *branch,
6014 struct got_repository *repo)
6016 const struct got_error *err = NULL;
6017 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6018 char *branch_ref_name = NULL;
6019 char *fileindex_path = NULL;
6020 struct check_rebase_ok_arg ok_arg;
6021 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6022 struct got_object_id *wt_branch_tip = NULL;
6024 *new_base_branch_ref = NULL;
6028 err = lock_worktree(worktree, LOCK_EX);
6032 err = open_fileindex(fileindex, &fileindex_path, worktree);
6036 ok_arg.worktree = worktree;
6038 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6043 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6047 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6051 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6055 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6060 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6063 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6064 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6068 err = got_ref_alloc_symref(new_base_branch_ref,
6069 new_base_branch_ref_name, wt_branch);
6072 err = got_ref_write(*new_base_branch_ref, repo);
6076 /* TODO Lock original branch's ref while rebasing? */
6078 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6082 err = got_ref_write(branch_ref, repo);
6086 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6087 worktree->base_commit_id);
6090 err = got_ref_write(*tmp_branch, repo);
6094 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6098 free(fileindex_path);
6099 free(tmp_branch_name);
6100 free(new_base_branch_ref_name);
6101 free(branch_ref_name);
6103 got_ref_close(branch_ref);
6105 got_ref_close(wt_branch);
6106 free(wt_branch_tip);
6108 if (*new_base_branch_ref) {
6109 got_ref_close(*new_base_branch_ref);
6110 *new_base_branch_ref = NULL;
6113 got_ref_close(*tmp_branch);
6117 got_fileindex_free(*fileindex);
6120 lock_worktree(worktree, LOCK_SH);
6125 const struct got_error *
6126 got_worktree_rebase_continue(struct got_object_id **commit_id,
6127 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6128 struct got_reference **branch, struct got_fileindex **fileindex,
6129 struct got_worktree *worktree, struct got_repository *repo)
6131 const struct got_error *err;
6132 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6133 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6134 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6135 char *fileindex_path = NULL;
6136 int have_staged_files = 0;
6139 *new_base_branch = NULL;
6144 err = lock_worktree(worktree, LOCK_EX);
6148 err = open_fileindex(fileindex, &fileindex_path, worktree);
6152 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6153 &have_staged_files);
6154 if (err && err->code != GOT_ERR_CANCELLED)
6156 if (have_staged_files) {
6157 err = got_error(GOT_ERR_STAGED_PATHS);
6161 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6165 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6169 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6173 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6177 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6181 err = got_ref_open(branch, repo,
6182 got_ref_get_symref_target(branch_ref), 0);
6186 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6190 err = got_ref_resolve(commit_id, repo, commit_ref);
6194 err = got_ref_open(new_base_branch, repo,
6195 new_base_branch_ref_name, 0);
6199 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6203 free(commit_ref_name);
6204 free(branch_ref_name);
6205 free(fileindex_path);
6207 got_ref_close(commit_ref);
6209 got_ref_close(branch_ref);
6214 got_ref_close(*tmp_branch);
6217 if (*new_base_branch) {
6218 got_ref_close(*new_base_branch);
6219 *new_base_branch = NULL;
6222 got_ref_close(*branch);
6226 got_fileindex_free(*fileindex);
6229 lock_worktree(worktree, LOCK_SH);
6234 const struct got_error *
6235 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6237 const struct got_error *err;
6238 char *tmp_branch_name = NULL;
6240 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6244 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6245 free(tmp_branch_name);
6249 static const struct got_error *
6250 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6251 char **logmsg, void *arg)
6257 static const struct got_error *
6258 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6259 const char *path, struct got_object_id *blob_id,
6260 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6261 int dirfd, const char *de_name)
6266 struct collect_merged_paths_arg {
6267 got_worktree_checkout_cb progress_cb;
6269 struct got_pathlist_head *merged_paths;
6272 static const struct got_error *
6273 collect_merged_paths(void *arg, unsigned char status, const char *path)
6275 const struct got_error *err;
6276 struct collect_merged_paths_arg *a = arg;
6278 struct got_pathlist_entry *new;
6280 err = (*a->progress_cb)(a->progress_arg, status, path);
6284 if (status != GOT_STATUS_MERGE &&
6285 status != GOT_STATUS_ADD &&
6286 status != GOT_STATUS_DELETE &&
6287 status != GOT_STATUS_CONFLICT)
6292 return got_error_from_errno("strdup");
6294 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6295 if (err || new == NULL)
6301 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6303 struct got_pathlist_entry *pe;
6305 TAILQ_FOREACH(pe, merged_paths, entry)
6306 free((char *)pe->path);
6308 got_pathlist_free(merged_paths);
6311 static const struct got_error *
6312 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6313 int is_rebase, struct got_repository *repo)
6315 const struct got_error *err;
6316 struct got_reference *commit_ref = NULL;
6318 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6320 if (err->code != GOT_ERR_NOT_REF)
6322 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6325 err = got_ref_write(commit_ref, repo);
6328 } else if (is_rebase) {
6329 struct got_object_id *stored_id;
6332 err = got_ref_resolve(&stored_id, repo, commit_ref);
6335 cmp = got_object_id_cmp(commit_id, stored_id);
6338 err = got_error(GOT_ERR_REBASE_COMMITID);
6344 got_ref_close(commit_ref);
6348 static const struct got_error *
6349 rebase_merge_files(struct got_pathlist_head *merged_paths,
6350 const char *commit_ref_name, struct got_worktree *worktree,
6351 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6352 struct got_object_id *commit_id, struct got_repository *repo,
6353 got_worktree_checkout_cb progress_cb, void *progress_arg,
6354 got_cancel_cb cancel_cb, void *cancel_arg)
6356 const struct got_error *err;
6357 struct got_reference *commit_ref = NULL;
6358 struct collect_merged_paths_arg cmp_arg;
6359 char *fileindex_path;
6361 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6363 err = get_fileindex_path(&fileindex_path, worktree);
6367 cmp_arg.progress_cb = progress_cb;
6368 cmp_arg.progress_arg = progress_arg;
6369 cmp_arg.merged_paths = merged_paths;
6370 err = merge_files(worktree, fileindex, fileindex_path,
6371 parent_commit_id, commit_id, repo, collect_merged_paths,
6372 &cmp_arg, cancel_cb, cancel_arg);
6374 got_ref_close(commit_ref);
6378 const struct got_error *
6379 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6380 struct got_worktree *worktree, struct got_fileindex *fileindex,
6381 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6382 struct got_repository *repo,
6383 got_worktree_checkout_cb progress_cb, void *progress_arg,
6384 got_cancel_cb cancel_cb, void *cancel_arg)
6386 const struct got_error *err;
6387 char *commit_ref_name;
6389 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6393 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6397 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6398 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6399 progress_arg, cancel_cb, cancel_arg);
6401 free(commit_ref_name);
6405 const struct got_error *
6406 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6407 struct got_worktree *worktree, struct got_fileindex *fileindex,
6408 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6409 struct got_repository *repo,
6410 got_worktree_checkout_cb progress_cb, void *progress_arg,
6411 got_cancel_cb cancel_cb, void *cancel_arg)
6413 const struct got_error *err;
6414 char *commit_ref_name;
6416 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6420 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6424 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6425 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6426 progress_arg, cancel_cb, cancel_arg);
6428 free(commit_ref_name);
6432 static const struct got_error *
6433 rebase_commit(struct got_object_id **new_commit_id,
6434 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6435 struct got_worktree *worktree, struct got_fileindex *fileindex,
6436 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6437 const char *new_logmsg, struct got_repository *repo)
6439 const struct got_error *err, *sync_err;
6440 struct got_pathlist_head commitable_paths;
6441 struct collect_commitables_arg cc_arg;
6442 char *fileindex_path = NULL;
6443 struct got_reference *head_ref = NULL;
6444 struct got_object_id *head_commit_id = NULL;
6445 char *logmsg = NULL;
6447 TAILQ_INIT(&commitable_paths);
6448 *new_commit_id = NULL;
6450 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6452 err = get_fileindex_path(&fileindex_path, worktree);
6456 cc_arg.commitable_paths = &commitable_paths;
6457 cc_arg.worktree = worktree;
6459 cc_arg.have_staged_files = 0;
6461 * If possible get the status of individual files directly to
6462 * avoid crawling the entire work tree once per rebased commit.
6464 * Ideally, merged_paths would contain a list of commitables
6465 * we could use so we could skip worktree_status() entirely.
6466 * However, we would then need carefully keep track of cumulative
6467 * effects of operations such as file additions and deletions
6468 * in 'got histedit -f' (folding multiple commits into one),
6469 * and this extra complexity is not really worth it.
6472 struct got_pathlist_entry *pe;
6473 TAILQ_FOREACH(pe, merged_paths, entry) {
6474 err = worktree_status(worktree, pe->path, fileindex,
6475 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6481 err = worktree_status(worktree, "", fileindex, repo,
6482 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6487 if (TAILQ_EMPTY(&commitable_paths)) {
6488 /* No-op change; commit will be elided. */
6489 err = got_ref_delete(commit_ref, repo);
6492 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6496 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6500 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6505 logmsg = strdup(new_logmsg);
6506 if (logmsg == NULL) {
6507 err = got_error_from_errno("strdup");
6511 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6516 /* NB: commit_worktree will call free(logmsg) */
6517 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6518 NULL, worktree, got_object_commit_get_author(orig_commit),
6519 got_object_commit_get_committer(orig_commit),
6520 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6524 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6528 err = got_ref_delete(commit_ref, repo);
6532 err = update_fileindex_after_commit(worktree, &commitable_paths,
6533 *new_commit_id, fileindex, 0);
6534 sync_err = sync_fileindex(fileindex, fileindex_path);
6535 if (sync_err && err == NULL)
6538 free(fileindex_path);
6539 free(head_commit_id);
6541 got_ref_close(head_ref);
6543 free(*new_commit_id);
6544 *new_commit_id = NULL;
6549 const struct got_error *
6550 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6551 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6552 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6553 struct got_commit_object *orig_commit,
6554 struct got_object_id *orig_commit_id, struct got_repository *repo)
6556 const struct got_error *err;
6557 char *commit_ref_name;
6558 struct got_reference *commit_ref = NULL;
6559 struct got_object_id *commit_id = NULL;
6561 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6565 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6568 err = got_ref_resolve(&commit_id, repo, commit_ref);
6571 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6572 err = got_error(GOT_ERR_REBASE_COMMITID);
6576 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6577 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6580 got_ref_close(commit_ref);
6581 free(commit_ref_name);
6586 const struct got_error *
6587 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6588 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6589 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6590 struct got_commit_object *orig_commit,
6591 struct got_object_id *orig_commit_id, const char *new_logmsg,
6592 struct got_repository *repo)
6594 const struct got_error *err;
6595 char *commit_ref_name;
6596 struct got_reference *commit_ref = NULL;
6598 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6602 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6606 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6607 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6610 got_ref_close(commit_ref);
6611 free(commit_ref_name);
6615 const struct got_error *
6616 got_worktree_rebase_postpone(struct got_worktree *worktree,
6617 struct got_fileindex *fileindex)
6620 got_fileindex_free(fileindex);
6621 return lock_worktree(worktree, LOCK_SH);
6624 static const struct got_error *
6625 delete_ref(const char *name, struct got_repository *repo)
6627 const struct got_error *err;
6628 struct got_reference *ref;
6630 err = got_ref_open(&ref, repo, name, 0);
6632 if (err->code == GOT_ERR_NOT_REF)
6637 err = got_ref_delete(ref, repo);
6642 static const struct got_error *
6643 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6645 const struct got_error *err;
6646 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6647 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6649 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6652 err = delete_ref(tmp_branch_name, repo);
6656 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6659 err = delete_ref(new_base_branch_ref_name, repo);
6663 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6666 err = delete_ref(branch_ref_name, repo);
6670 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6673 err = delete_ref(commit_ref_name, repo);
6678 free(tmp_branch_name);
6679 free(new_base_branch_ref_name);
6680 free(branch_ref_name);
6681 free(commit_ref_name);
6685 static const struct got_error *
6686 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6687 struct got_object_id *new_commit_id, struct got_repository *repo)
6689 const struct got_error *err;
6690 struct got_reference *ref = NULL;
6691 struct got_object_id *old_commit_id = NULL;
6692 const char *branch_name = NULL;
6693 char *new_id_str = NULL;
6694 char *refname = NULL;
6696 branch_name = got_ref_get_name(branch);
6697 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6698 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6701 err = got_object_id_str(&new_id_str, new_commit_id);
6705 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6706 new_id_str) == -1) {
6707 err = got_error_from_errno("asprintf");
6711 err = got_ref_resolve(&old_commit_id, repo, branch);
6715 err = got_ref_alloc(&ref, refname, old_commit_id);
6719 err = got_ref_write(ref, repo);
6723 free(old_commit_id);
6729 const struct got_error *
6730 got_worktree_rebase_complete(struct got_worktree *worktree,
6731 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6732 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6733 struct got_repository *repo, int create_backup)
6735 const struct got_error *err, *unlockerr, *sync_err;
6736 struct got_object_id *new_head_commit_id = NULL;
6737 char *fileindex_path = NULL;
6739 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6743 if (create_backup) {
6744 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6745 rebased_branch, new_head_commit_id, repo);
6750 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6754 err = got_ref_write(rebased_branch, repo);
6758 err = got_worktree_set_head_ref(worktree, rebased_branch);
6762 err = delete_rebase_refs(worktree, repo);
6766 err = get_fileindex_path(&fileindex_path, worktree);
6769 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6770 sync_err = sync_fileindex(fileindex, fileindex_path);
6771 if (sync_err && err == NULL)
6774 got_fileindex_free(fileindex);
6775 free(fileindex_path);
6776 free(new_head_commit_id);
6777 unlockerr = lock_worktree(worktree, LOCK_SH);
6778 if (unlockerr && err == NULL)
6783 const struct got_error *
6784 got_worktree_rebase_abort(struct got_worktree *worktree,
6785 struct got_fileindex *fileindex, struct got_repository *repo,
6786 struct got_reference *new_base_branch,
6787 got_worktree_checkout_cb progress_cb, void *progress_arg)
6789 const struct got_error *err, *unlockerr, *sync_err;
6790 struct got_reference *resolved = NULL;
6791 struct got_object_id *commit_id = NULL;
6792 struct got_commit_object *commit = NULL;
6793 char *fileindex_path = NULL;
6794 struct revert_file_args rfa;
6795 struct got_object_id *tree_id = NULL;
6797 err = lock_worktree(worktree, LOCK_EX);
6801 err = got_object_open_as_commit(&commit, repo,
6802 worktree->base_commit_id);
6806 err = got_ref_open(&resolved, repo,
6807 got_ref_get_symref_target(new_base_branch), 0);
6811 err = got_worktree_set_head_ref(worktree, resolved);
6816 * XXX commits to the base branch could have happened while
6817 * we were busy rebasing; should we store the original commit ID
6818 * when rebase begins and read it back here?
6820 err = got_ref_resolve(&commit_id, repo, resolved);
6824 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6828 err = got_object_id_by_path(&tree_id, repo, commit,
6829 worktree->path_prefix);
6833 err = delete_rebase_refs(worktree, repo);
6837 err = get_fileindex_path(&fileindex_path, worktree);
6841 rfa.worktree = worktree;
6842 rfa.fileindex = fileindex;
6843 rfa.progress_cb = progress_cb;
6844 rfa.progress_arg = progress_arg;
6845 rfa.patch_cb = NULL;
6846 rfa.patch_arg = NULL;
6848 rfa.unlink_added_files = 0;
6849 err = worktree_status(worktree, "", fileindex, repo,
6850 revert_file, &rfa, NULL, NULL, 1, 0);
6854 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6855 repo, progress_cb, progress_arg, NULL, NULL);
6857 sync_err = sync_fileindex(fileindex, fileindex_path);
6858 if (sync_err && err == NULL)
6861 got_ref_close(resolved);
6865 got_object_commit_close(commit);
6867 got_fileindex_free(fileindex);
6868 free(fileindex_path);
6870 unlockerr = lock_worktree(worktree, LOCK_SH);
6871 if (unlockerr && err == NULL)
6876 const struct got_error *
6877 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6878 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6879 struct got_fileindex **fileindex, struct got_worktree *worktree,
6880 struct got_repository *repo)
6882 const struct got_error *err = NULL;
6883 char *tmp_branch_name = NULL;
6884 char *branch_ref_name = NULL;
6885 char *base_commit_ref_name = NULL;
6886 char *fileindex_path = NULL;
6887 struct check_rebase_ok_arg ok_arg;
6888 struct got_reference *wt_branch = NULL;
6889 struct got_reference *base_commit_ref = NULL;
6893 *base_commit_id = NULL;
6896 err = lock_worktree(worktree, LOCK_EX);
6900 err = open_fileindex(fileindex, &fileindex_path, worktree);
6904 ok_arg.worktree = worktree;
6906 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6911 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6915 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6919 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6924 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6929 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6933 err = got_ref_write(*branch_ref, repo);
6937 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6938 worktree->base_commit_id);
6941 err = got_ref_write(base_commit_ref, repo);
6944 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6945 if (*base_commit_id == NULL) {
6946 err = got_error_from_errno("got_object_id_dup");
6950 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6951 worktree->base_commit_id);
6954 err = got_ref_write(*tmp_branch, repo);
6958 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6962 free(fileindex_path);
6963 free(tmp_branch_name);
6964 free(branch_ref_name);
6965 free(base_commit_ref_name);
6967 got_ref_close(wt_branch);
6970 got_ref_close(*branch_ref);
6974 got_ref_close(*tmp_branch);
6977 free(*base_commit_id);
6979 got_fileindex_free(*fileindex);
6982 lock_worktree(worktree, LOCK_SH);
6987 const struct got_error *
6988 got_worktree_histedit_postpone(struct got_worktree *worktree,
6989 struct got_fileindex *fileindex)
6992 got_fileindex_free(fileindex);
6993 return lock_worktree(worktree, LOCK_SH);
6996 const struct got_error *
6997 got_worktree_histedit_in_progress(int *in_progress,
6998 struct got_worktree *worktree)
7000 const struct got_error *err;
7001 char *tmp_branch_name = NULL;
7003 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7007 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7008 free(tmp_branch_name);
7012 const struct got_error *
7013 got_worktree_histedit_continue(struct got_object_id **commit_id,
7014 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7015 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7016 struct got_worktree *worktree, struct got_repository *repo)
7018 const struct got_error *err;
7019 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7020 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7021 struct got_reference *commit_ref = NULL;
7022 struct got_reference *base_commit_ref = NULL;
7023 char *fileindex_path = NULL;
7024 int have_staged_files = 0;
7028 *base_commit_id = NULL;
7031 err = lock_worktree(worktree, LOCK_EX);
7035 err = open_fileindex(fileindex, &fileindex_path, worktree);
7039 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7040 &have_staged_files);
7041 if (err && err->code != GOT_ERR_CANCELLED)
7043 if (have_staged_files) {
7044 err = got_error(GOT_ERR_STAGED_PATHS);
7048 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7052 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7056 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7060 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7065 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7069 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7072 err = got_ref_resolve(commit_id, repo, commit_ref);
7076 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7079 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7083 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7087 free(commit_ref_name);
7088 free(branch_ref_name);
7089 free(fileindex_path);
7091 got_ref_close(commit_ref);
7092 if (base_commit_ref)
7093 got_ref_close(base_commit_ref);
7097 free(*base_commit_id);
7098 *base_commit_id = NULL;
7100 got_ref_close(*tmp_branch);
7104 got_fileindex_free(*fileindex);
7107 lock_worktree(worktree, LOCK_EX);
7112 static const struct got_error *
7113 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7115 const struct got_error *err;
7116 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7117 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7119 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7122 err = delete_ref(tmp_branch_name, repo);
7126 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7130 err = delete_ref(base_commit_ref_name, repo);
7134 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7137 err = delete_ref(branch_ref_name, repo);
7141 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7144 err = delete_ref(commit_ref_name, repo);
7148 free(tmp_branch_name);
7149 free(base_commit_ref_name);
7150 free(branch_ref_name);
7151 free(commit_ref_name);
7155 const struct got_error *
7156 got_worktree_histedit_abort(struct got_worktree *worktree,
7157 struct got_fileindex *fileindex, struct got_repository *repo,
7158 struct got_reference *branch, struct got_object_id *base_commit_id,
7159 got_worktree_checkout_cb progress_cb, void *progress_arg)
7161 const struct got_error *err, *unlockerr, *sync_err;
7162 struct got_reference *resolved = NULL;
7163 char *fileindex_path = NULL;
7164 struct got_commit_object *commit = NULL;
7165 struct got_object_id *tree_id = NULL;
7166 struct revert_file_args rfa;
7168 err = lock_worktree(worktree, LOCK_EX);
7172 err = got_object_open_as_commit(&commit, repo,
7173 worktree->base_commit_id);
7177 err = got_ref_open(&resolved, repo,
7178 got_ref_get_symref_target(branch), 0);
7182 err = got_worktree_set_head_ref(worktree, resolved);
7186 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7190 err = got_object_id_by_path(&tree_id, repo, commit,
7191 worktree->path_prefix);
7195 err = delete_histedit_refs(worktree, repo);
7199 err = get_fileindex_path(&fileindex_path, worktree);
7203 rfa.worktree = worktree;
7204 rfa.fileindex = fileindex;
7205 rfa.progress_cb = progress_cb;
7206 rfa.progress_arg = progress_arg;
7207 rfa.patch_cb = NULL;
7208 rfa.patch_arg = NULL;
7210 rfa.unlink_added_files = 0;
7211 err = worktree_status(worktree, "", fileindex, repo,
7212 revert_file, &rfa, NULL, NULL, 1, 0);
7216 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7217 repo, progress_cb, progress_arg, NULL, NULL);
7219 sync_err = sync_fileindex(fileindex, fileindex_path);
7220 if (sync_err && err == NULL)
7223 got_ref_close(resolved);
7225 free(fileindex_path);
7227 unlockerr = lock_worktree(worktree, LOCK_SH);
7228 if (unlockerr && err == NULL)
7233 const struct got_error *
7234 got_worktree_histedit_complete(struct got_worktree *worktree,
7235 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7236 struct got_reference *edited_branch, struct got_repository *repo)
7238 const struct got_error *err, *unlockerr, *sync_err;
7239 struct got_object_id *new_head_commit_id = NULL;
7240 struct got_reference *resolved = NULL;
7241 char *fileindex_path = NULL;
7243 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7247 err = got_ref_open(&resolved, repo,
7248 got_ref_get_symref_target(edited_branch), 0);
7252 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7253 resolved, new_head_commit_id, repo);
7257 err = got_ref_change_ref(resolved, new_head_commit_id);
7261 err = got_ref_write(resolved, repo);
7265 err = got_worktree_set_head_ref(worktree, resolved);
7269 err = delete_histedit_refs(worktree, repo);
7273 err = get_fileindex_path(&fileindex_path, worktree);
7276 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7277 sync_err = sync_fileindex(fileindex, fileindex_path);
7278 if (sync_err && err == NULL)
7281 got_fileindex_free(fileindex);
7282 free(fileindex_path);
7283 free(new_head_commit_id);
7284 unlockerr = lock_worktree(worktree, LOCK_SH);
7285 if (unlockerr && err == NULL)
7290 const struct got_error *
7291 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7292 struct got_object_id *commit_id, struct got_repository *repo)
7294 const struct got_error *err;
7295 char *commit_ref_name;
7297 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7301 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7305 err = delete_ref(commit_ref_name, repo);
7307 free(commit_ref_name);
7311 const struct got_error *
7312 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7313 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7314 struct got_worktree *worktree, const char *refname,
7315 struct got_repository *repo)
7317 const struct got_error *err = NULL;
7318 char *fileindex_path = NULL;
7319 struct check_rebase_ok_arg ok_arg;
7323 *base_branch_ref = NULL;
7325 err = lock_worktree(worktree, LOCK_EX);
7329 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7330 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7331 "cannot integrate a branch into itself; "
7332 "update -b or different branch name required");
7336 err = open_fileindex(fileindex, &fileindex_path, worktree);
7340 /* Preconditions are the same as for rebase. */
7341 ok_arg.worktree = worktree;
7343 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7348 err = got_ref_open(branch_ref, repo, refname, 1);
7352 err = got_ref_open(base_branch_ref, repo,
7353 got_worktree_get_head_ref_name(worktree), 1);
7357 got_ref_close(*branch_ref);
7360 if (*base_branch_ref) {
7361 got_ref_close(*base_branch_ref);
7362 *base_branch_ref = NULL;
7365 got_fileindex_free(*fileindex);
7368 lock_worktree(worktree, LOCK_SH);
7373 const struct got_error *
7374 got_worktree_integrate_continue(struct got_worktree *worktree,
7375 struct got_fileindex *fileindex, struct got_repository *repo,
7376 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7377 got_worktree_checkout_cb progress_cb, void *progress_arg,
7378 got_cancel_cb cancel_cb, void *cancel_arg)
7380 const struct got_error *err = NULL, *sync_err, *unlockerr;
7381 char *fileindex_path = NULL;
7382 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7383 struct got_commit_object *commit = NULL;
7385 err = get_fileindex_path(&fileindex_path, worktree);
7389 err = got_ref_resolve(&commit_id, repo, branch_ref);
7393 err = got_object_open_as_commit(&commit, repo, commit_id);
7397 err = got_object_id_by_path(&tree_id, repo, commit,
7398 worktree->path_prefix);
7402 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7406 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7407 progress_cb, progress_arg, cancel_cb, cancel_arg);
7411 err = got_ref_change_ref(base_branch_ref, commit_id);
7415 err = got_ref_write(base_branch_ref, repo);
7419 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7421 sync_err = sync_fileindex(fileindex, fileindex_path);
7422 if (sync_err && err == NULL)
7426 unlockerr = got_ref_unlock(branch_ref);
7427 if (unlockerr && err == NULL)
7429 got_ref_close(branch_ref);
7431 unlockerr = got_ref_unlock(base_branch_ref);
7432 if (unlockerr && err == NULL)
7434 got_ref_close(base_branch_ref);
7436 got_fileindex_free(fileindex);
7437 free(fileindex_path);
7440 got_object_commit_close(commit);
7442 unlockerr = lock_worktree(worktree, LOCK_SH);
7443 if (unlockerr && err == NULL)
7448 const struct got_error *
7449 got_worktree_integrate_abort(struct got_worktree *worktree,
7450 struct got_fileindex *fileindex, struct got_repository *repo,
7451 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7453 const struct got_error *err = NULL, *unlockerr = NULL;
7455 got_fileindex_free(fileindex);
7457 err = lock_worktree(worktree, LOCK_SH);
7459 unlockerr = got_ref_unlock(branch_ref);
7460 if (unlockerr && err == NULL)
7462 got_ref_close(branch_ref);
7464 unlockerr = got_ref_unlock(base_branch_ref);
7465 if (unlockerr && err == NULL)
7467 got_ref_close(base_branch_ref);
7472 const struct got_error *
7473 got_worktree_merge_postpone(struct got_worktree *worktree,
7474 struct got_fileindex *fileindex)
7476 const struct got_error *err, *sync_err;
7477 char *fileindex_path = NULL;
7479 err = get_fileindex_path(&fileindex_path, worktree);
7483 sync_err = sync_fileindex(fileindex, fileindex_path);
7485 err = lock_worktree(worktree, LOCK_SH);
7486 if (sync_err && err == NULL)
7489 got_fileindex_free(fileindex);
7490 free(fileindex_path);
7494 static const struct got_error *
7495 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7497 const struct got_error *err;
7498 char *branch_refname = NULL, *commit_refname = NULL;
7500 err = get_merge_branch_ref_name(&branch_refname, worktree);
7503 err = delete_ref(branch_refname, repo);
7507 err = get_merge_commit_ref_name(&commit_refname, worktree);
7510 err = delete_ref(commit_refname, repo);
7515 free(branch_refname);
7516 free(commit_refname);
7520 struct merge_commit_msg_arg {
7521 struct got_worktree *worktree;
7522 const char *branch_name;
7525 static const struct got_error *
7526 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7529 struct merge_commit_msg_arg *a = arg;
7531 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7532 got_worktree_get_head_ref_name(a->worktree)) == -1)
7533 return got_error_from_errno("asprintf");
7539 const struct got_error *
7540 got_worktree_merge_branch(struct got_worktree *worktree,
7541 struct got_fileindex *fileindex,
7542 struct got_object_id *yca_commit_id,
7543 struct got_object_id *branch_tip,
7544 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7545 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7547 const struct got_error *err;
7548 char *fileindex_path = NULL;
7550 err = get_fileindex_path(&fileindex_path, worktree);
7554 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7559 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7560 branch_tip, repo, progress_cb, progress_arg,
7561 cancel_cb, cancel_arg);
7563 free(fileindex_path);
7567 const struct got_error *
7568 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7569 struct got_worktree *worktree, struct got_fileindex *fileindex,
7570 const char *author, const char *committer, int allow_bad_symlinks,
7571 struct got_object_id *branch_tip, const char *branch_name,
7572 struct got_repository *repo,
7573 got_worktree_status_cb status_cb, void *status_arg)
7576 const struct got_error *err = NULL, *sync_err;
7577 struct got_pathlist_head commitable_paths;
7578 struct collect_commitables_arg cc_arg;
7579 struct got_pathlist_entry *pe;
7580 struct got_reference *head_ref = NULL;
7581 struct got_object_id *head_commit_id = NULL;
7582 int have_staged_files = 0;
7583 struct merge_commit_msg_arg mcm_arg;
7584 char *fileindex_path = NULL;
7586 *new_commit_id = NULL;
7588 TAILQ_INIT(&commitable_paths);
7590 err = get_fileindex_path(&fileindex_path, worktree);
7594 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7598 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7602 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7603 &have_staged_files);
7604 if (err && err->code != GOT_ERR_CANCELLED)
7606 if (have_staged_files) {
7607 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7611 cc_arg.commitable_paths = &commitable_paths;
7612 cc_arg.worktree = worktree;
7613 cc_arg.fileindex = fileindex;
7615 cc_arg.have_staged_files = have_staged_files;
7616 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7617 err = worktree_status(worktree, "", fileindex, repo,
7618 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7622 if (TAILQ_EMPTY(&commitable_paths)) {
7623 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7624 "merge of %s cannot proceed", branch_name);
7628 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7629 struct got_commitable *ct = pe->data;
7630 const char *ct_path = ct->in_repo_path;
7632 while (ct_path[0] == '/')
7634 err = check_out_of_date(ct_path, ct->status,
7635 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7636 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7642 mcm_arg.worktree = worktree;
7643 mcm_arg.branch_name = branch_name;
7644 err = commit_worktree(new_commit_id, &commitable_paths,
7645 head_commit_id, branch_tip, worktree, author, committer,
7646 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7650 err = update_fileindex_after_commit(worktree, &commitable_paths,
7651 *new_commit_id, fileindex, have_staged_files);
7652 sync_err = sync_fileindex(fileindex, fileindex_path);
7653 if (sync_err && err == NULL)
7656 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7657 struct got_commitable *ct = pe->data;
7658 free_commitable(ct);
7660 got_pathlist_free(&commitable_paths);
7661 free(fileindex_path);
7665 const struct got_error *
7666 got_worktree_merge_complete(struct got_worktree *worktree,
7667 struct got_fileindex *fileindex, struct got_repository *repo)
7669 const struct got_error *err, *unlockerr, *sync_err;
7670 char *fileindex_path = NULL;
7672 err = delete_merge_refs(worktree, repo);
7676 err = get_fileindex_path(&fileindex_path, worktree);
7679 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7680 sync_err = sync_fileindex(fileindex, fileindex_path);
7681 if (sync_err && err == NULL)
7684 got_fileindex_free(fileindex);
7685 free(fileindex_path);
7686 unlockerr = lock_worktree(worktree, LOCK_SH);
7687 if (unlockerr && err == NULL)
7692 const struct got_error *
7693 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7694 struct got_repository *repo)
7696 const struct got_error *err;
7697 char *branch_refname = NULL;
7698 struct got_reference *branch_ref = NULL;
7702 err = get_merge_branch_ref_name(&branch_refname, worktree);
7705 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7706 free(branch_refname);
7708 if (err->code != GOT_ERR_NOT_REF)
7716 const struct got_error *got_worktree_merge_prepare(
7717 struct got_fileindex **fileindex, struct got_worktree *worktree,
7718 struct got_reference *branch, struct got_repository *repo)
7720 const struct got_error *err = NULL;
7721 char *fileindex_path = NULL;
7722 char *branch_refname = NULL, *commit_refname = NULL;
7723 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7724 struct got_reference *commit_ref = NULL;
7725 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7726 struct check_rebase_ok_arg ok_arg;
7730 err = lock_worktree(worktree, LOCK_EX);
7734 err = open_fileindex(fileindex, &fileindex_path, worktree);
7738 /* Preconditions are the same as for rebase. */
7739 ok_arg.worktree = worktree;
7741 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7746 err = get_merge_branch_ref_name(&branch_refname, worktree);
7750 err = get_merge_commit_ref_name(&commit_refname, worktree);
7754 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7759 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7763 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7764 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7768 err = got_ref_resolve(&branch_tip, repo, branch);
7772 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7775 err = got_ref_write(branch_ref, repo);
7779 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7782 err = got_ref_write(commit_ref, repo);
7787 free(branch_refname);
7788 free(commit_refname);
7789 free(fileindex_path);
7791 got_ref_close(branch_ref);
7793 got_ref_close(commit_ref);
7795 got_ref_close(wt_branch);
7796 free(wt_branch_tip);
7799 got_fileindex_free(*fileindex);
7802 lock_worktree(worktree, LOCK_SH);
7807 const struct got_error *
7808 got_worktree_merge_continue(char **branch_name,
7809 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7810 struct got_worktree *worktree, struct got_repository *repo)
7812 const struct got_error *err;
7813 char *commit_refname = NULL, *branch_refname = NULL;
7814 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7815 char *fileindex_path = NULL;
7816 int have_staged_files = 0;
7818 *branch_name = NULL;
7822 err = lock_worktree(worktree, LOCK_EX);
7826 err = open_fileindex(fileindex, &fileindex_path, worktree);
7830 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7831 &have_staged_files);
7832 if (err && err->code != GOT_ERR_CANCELLED)
7834 if (have_staged_files) {
7835 err = got_error(GOT_ERR_STAGED_PATHS);
7839 err = get_merge_branch_ref_name(&branch_refname, worktree);
7843 err = get_merge_commit_ref_name(&commit_refname, worktree);
7847 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7851 if (!got_ref_is_symbolic(branch_ref)) {
7852 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7853 "%s is not a symbolic reference",
7854 got_ref_get_name(branch_ref));
7857 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7858 if (*branch_name == NULL) {
7859 err = got_error_from_errno("strdup");
7863 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7867 err = got_ref_resolve(branch_tip, repo, commit_ref);
7871 free(commit_refname);
7872 free(branch_refname);
7873 free(fileindex_path);
7875 got_ref_close(commit_ref);
7877 got_ref_close(branch_ref);
7881 *branch_name = NULL;
7886 got_fileindex_free(*fileindex);
7889 lock_worktree(worktree, LOCK_SH);
7894 const struct got_error *
7895 got_worktree_merge_abort(struct got_worktree *worktree,
7896 struct got_fileindex *fileindex, struct got_repository *repo,
7897 got_worktree_checkout_cb progress_cb, void *progress_arg)
7899 const struct got_error *err, *unlockerr, *sync_err;
7900 struct got_object_id *commit_id = NULL;
7901 struct got_commit_object *commit = NULL;
7902 char *fileindex_path = NULL;
7903 struct revert_file_args rfa;
7904 struct got_object_id *tree_id = NULL;
7906 err = got_object_open_as_commit(&commit, repo,
7907 worktree->base_commit_id);
7911 err = got_object_id_by_path(&tree_id, repo, commit,
7912 worktree->path_prefix);
7916 err = delete_merge_refs(worktree, repo);
7920 err = get_fileindex_path(&fileindex_path, worktree);
7924 rfa.worktree = worktree;
7925 rfa.fileindex = fileindex;
7926 rfa.progress_cb = progress_cb;
7927 rfa.progress_arg = progress_arg;
7928 rfa.patch_cb = NULL;
7929 rfa.patch_arg = NULL;
7931 rfa.unlink_added_files = 1;
7932 err = worktree_status(worktree, "", fileindex, repo,
7933 revert_file, &rfa, NULL, NULL, 1, 0);
7937 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7938 repo, progress_cb, progress_arg, NULL, NULL);
7940 sync_err = sync_fileindex(fileindex, fileindex_path);
7941 if (sync_err && err == NULL)
7947 got_object_commit_close(commit);
7949 got_fileindex_free(fileindex);
7950 free(fileindex_path);
7952 unlockerr = lock_worktree(worktree, LOCK_SH);
7953 if (unlockerr && err == NULL)
7958 struct check_stage_ok_arg {
7959 struct got_object_id *head_commit_id;
7960 struct got_worktree *worktree;
7961 struct got_fileindex *fileindex;
7962 struct got_repository *repo;
7966 static const struct got_error *
7967 check_stage_ok(void *arg, unsigned char status,
7968 unsigned char staged_status, const char *relpath,
7969 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7970 struct got_object_id *commit_id, int dirfd, const char *de_name)
7972 struct check_stage_ok_arg *a = arg;
7973 const struct got_error *err = NULL;
7974 struct got_fileindex_entry *ie;
7975 struct got_object_id base_commit_id;
7976 struct got_object_id *base_commit_idp = NULL;
7977 char *in_repo_path = NULL, *p;
7979 if (status == GOT_STATUS_UNVERSIONED ||
7980 status == GOT_STATUS_NO_CHANGE)
7982 if (status == GOT_STATUS_NONEXISTENT)
7983 return got_error_set_errno(ENOENT, relpath);
7985 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7987 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7989 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7990 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7992 return got_error_from_errno("asprintf");
7994 if (got_fileindex_entry_has_commit(ie)) {
7995 memcpy(base_commit_id.sha1, ie->commit_sha1,
7996 SHA1_DIGEST_LENGTH);
7997 base_commit_idp = &base_commit_id;
8000 if (status == GOT_STATUS_CONFLICT) {
8001 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8003 } else if (status != GOT_STATUS_ADD &&
8004 status != GOT_STATUS_MODIFY &&
8005 status != GOT_STATUS_DELETE) {
8006 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8010 a->have_changes = 1;
8015 err = check_out_of_date(p, status, staged_status,
8016 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8017 GOT_ERR_STAGE_OUT_OF_DATE);
8023 struct stage_path_arg {
8024 struct got_worktree *worktree;
8025 struct got_fileindex *fileindex;
8026 struct got_repository *repo;
8027 got_worktree_status_cb status_cb;
8029 got_worktree_patch_cb patch_cb;
8031 int staged_something;
8032 int allow_bad_symlinks;
8035 static const struct got_error *
8036 stage_path(void *arg, unsigned char status,
8037 unsigned char staged_status, const char *relpath,
8038 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8039 struct got_object_id *commit_id, int dirfd, const char *de_name)
8041 struct stage_path_arg *a = arg;
8042 const struct got_error *err = NULL;
8043 struct got_fileindex_entry *ie;
8044 char *ondisk_path = NULL, *path_content = NULL;
8046 struct got_object_id *new_staged_blob_id = NULL;
8049 if (status == GOT_STATUS_UNVERSIONED)
8052 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8054 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8056 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8058 return got_error_from_errno("asprintf");
8061 case GOT_STATUS_ADD:
8062 case GOT_STATUS_MODIFY:
8063 /* XXX could sb.st_mode be passed in by our caller? */
8064 if (lstat(ondisk_path, &sb) == -1) {
8065 err = got_error_from_errno2("lstat", ondisk_path);
8069 if (status == GOT_STATUS_ADD) {
8070 int choice = GOT_PATCH_CHOICE_NONE;
8071 err = (*a->patch_cb)(&choice, a->patch_arg,
8072 status, ie->path, NULL, 1, 1);
8075 if (choice != GOT_PATCH_CHOICE_YES)
8078 err = create_patched_content(&path_content, 0,
8079 staged_blob_id ? staged_blob_id : blob_id,
8080 ondisk_path, dirfd, de_name, ie->path,
8081 a->repo, a->patch_cb, a->patch_arg);
8082 if (err || path_content == NULL)
8086 err = got_object_blob_create(&new_staged_blob_id,
8087 path_content ? path_content : ondisk_path, a->repo);
8090 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8091 SHA1_DIGEST_LENGTH);
8092 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8093 stage = GOT_FILEIDX_STAGE_ADD;
8095 stage = GOT_FILEIDX_STAGE_MODIFY;
8096 got_fileindex_entry_stage_set(ie, stage);
8097 if (S_ISLNK(sb.st_mode)) {
8098 int is_bad_symlink = 0;
8099 if (!a->allow_bad_symlinks) {
8100 char target_path[PATH_MAX];
8102 target_len = readlink(ondisk_path, target_path,
8103 sizeof(target_path));
8104 if (target_len == -1) {
8105 err = got_error_from_errno2("readlink",
8109 err = is_bad_symlink_target(&is_bad_symlink,
8110 target_path, target_len, ondisk_path,
8111 a->worktree->root_path);
8114 if (is_bad_symlink) {
8115 err = got_error_path(ondisk_path,
8116 GOT_ERR_BAD_SYMLINK);
8121 got_fileindex_entry_staged_filetype_set(ie,
8122 GOT_FILEIDX_MODE_BAD_SYMLINK);
8124 got_fileindex_entry_staged_filetype_set(ie,
8125 GOT_FILEIDX_MODE_SYMLINK);
8127 got_fileindex_entry_staged_filetype_set(ie,
8128 GOT_FILEIDX_MODE_REGULAR_FILE);
8130 a->staged_something = 1;
8131 if (a->status_cb == NULL)
8133 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8134 get_staged_status(ie), relpath, blob_id,
8135 new_staged_blob_id, NULL, dirfd, de_name);
8139 * When staging the reverse of the staged diff,
8140 * implicitly unstage the file.
8142 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8143 sizeof(ie->blob_sha1)) == 0) {
8144 got_fileindex_entry_stage_set(ie,
8145 GOT_FILEIDX_STAGE_NONE);
8148 case GOT_STATUS_DELETE:
8149 if (staged_status == GOT_STATUS_DELETE)
8152 int choice = GOT_PATCH_CHOICE_NONE;
8153 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8154 ie->path, NULL, 1, 1);
8157 if (choice == GOT_PATCH_CHOICE_NO)
8159 if (choice != GOT_PATCH_CHOICE_YES) {
8160 err = got_error(GOT_ERR_PATCH_CHOICE);
8164 stage = GOT_FILEIDX_STAGE_DELETE;
8165 got_fileindex_entry_stage_set(ie, stage);
8166 a->staged_something = 1;
8167 if (a->status_cb == NULL)
8169 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8170 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8173 case GOT_STATUS_NO_CHANGE:
8175 case GOT_STATUS_CONFLICT:
8176 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8178 case GOT_STATUS_NONEXISTENT:
8179 err = got_error_set_errno(ENOENT, relpath);
8182 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8186 if (path_content && unlink(path_content) == -1 && err == NULL)
8187 err = got_error_from_errno2("unlink", path_content);
8190 free(new_staged_blob_id);
8194 const struct got_error *
8195 got_worktree_stage(struct got_worktree *worktree,
8196 struct got_pathlist_head *paths,
8197 got_worktree_status_cb status_cb, void *status_arg,
8198 got_worktree_patch_cb patch_cb, void *patch_arg,
8199 int allow_bad_symlinks, struct got_repository *repo)
8201 const struct got_error *err = NULL, *sync_err, *unlockerr;
8202 struct got_pathlist_entry *pe;
8203 struct got_fileindex *fileindex = NULL;
8204 char *fileindex_path = NULL;
8205 struct got_reference *head_ref = NULL;
8206 struct got_object_id *head_commit_id = NULL;
8207 struct check_stage_ok_arg oka;
8208 struct stage_path_arg spa;
8210 err = lock_worktree(worktree, LOCK_EX);
8214 err = got_ref_open(&head_ref, repo,
8215 got_worktree_get_head_ref_name(worktree), 0);
8218 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8221 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8225 /* Check pre-conditions before staging anything. */
8226 oka.head_commit_id = head_commit_id;
8227 oka.worktree = worktree;
8228 oka.fileindex = fileindex;
8230 oka.have_changes = 0;
8231 TAILQ_FOREACH(pe, paths, entry) {
8232 err = worktree_status(worktree, pe->path, fileindex, repo,
8233 check_stage_ok, &oka, NULL, NULL, 1, 0);
8237 if (!oka.have_changes) {
8238 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8242 spa.worktree = worktree;
8243 spa.fileindex = fileindex;
8245 spa.patch_cb = patch_cb;
8246 spa.patch_arg = patch_arg;
8247 spa.status_cb = status_cb;
8248 spa.status_arg = status_arg;
8249 spa.staged_something = 0;
8250 spa.allow_bad_symlinks = allow_bad_symlinks;
8251 TAILQ_FOREACH(pe, paths, entry) {
8252 err = worktree_status(worktree, pe->path, fileindex, repo,
8253 stage_path, &spa, NULL, NULL, 1, 0);
8257 if (!spa.staged_something) {
8258 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8262 sync_err = sync_fileindex(fileindex, fileindex_path);
8263 if (sync_err && err == NULL)
8267 got_ref_close(head_ref);
8268 free(head_commit_id);
8269 free(fileindex_path);
8271 got_fileindex_free(fileindex);
8272 unlockerr = lock_worktree(worktree, LOCK_SH);
8273 if (unlockerr && err == NULL)
8278 struct unstage_path_arg {
8279 struct got_worktree *worktree;
8280 struct got_fileindex *fileindex;
8281 struct got_repository *repo;
8282 got_worktree_checkout_cb progress_cb;
8284 got_worktree_patch_cb patch_cb;
8288 static const struct got_error *
8289 create_unstaged_content(char **path_unstaged_content,
8290 char **path_new_staged_content, struct got_object_id *blob_id,
8291 struct got_object_id *staged_blob_id, const char *relpath,
8292 struct got_repository *repo,
8293 got_worktree_patch_cb patch_cb, void *patch_arg)
8295 const struct got_error *err, *free_err;
8296 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8297 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8298 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8299 struct got_diffreg_result *diffreg_result = NULL;
8300 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8301 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8302 int fd1 = -1, fd2 = -1;
8304 *path_unstaged_content = NULL;
8305 *path_new_staged_content = NULL;
8307 err = got_object_id_str(&label1, blob_id);
8311 fd1 = got_opentempfd();
8313 err = got_error_from_errno("got_opentempfd");
8316 fd2 = got_opentempfd();
8318 err = got_error_from_errno("got_opentempfd");
8322 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8326 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8330 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8334 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8339 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8343 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8347 err = got_diff_files(&diffreg_result, f1, label1, f2,
8348 path2, 3, 0, 1, NULL);
8352 err = got_opentemp_named(path_unstaged_content, &outfile,
8353 "got-unstaged-content");
8356 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8357 "got-new-staged-content");
8361 if (fseek(f1, 0L, SEEK_SET) == -1) {
8362 err = got_ferror(f1, GOT_ERR_IO);
8365 if (fseek(f2, 0L, SEEK_SET) == -1) {
8366 err = got_ferror(f2, GOT_ERR_IO);
8369 /* Count the number of actual changes in the diff result. */
8370 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8371 struct diff_chunk_context cc = {};
8372 diff_chunk_context_load_change(&cc, &nchunks_used,
8373 diffreg_result->result, n, 0);
8376 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8378 err = apply_or_reject_change(&choice, &nchunks_used,
8379 diffreg_result->result, n, relpath, f1, f2,
8380 &line_cur1, &line_cur2,
8381 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8384 if (choice == GOT_PATCH_CHOICE_YES)
8387 have_rejected_content = 1;
8388 if (choice == GOT_PATCH_CHOICE_QUIT)
8391 if (have_content || have_rejected_content)
8392 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8393 outfile, rejectfile);
8396 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8397 err = got_error_from_errno("close");
8399 got_object_blob_close(blob);
8400 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8401 err = got_error_from_errno("close");
8403 got_object_blob_close(staged_blob);
8404 free_err = got_diffreg_result_free(diffreg_result);
8405 if (free_err && err == NULL)
8407 if (f1 && fclose(f1) == EOF && err == NULL)
8408 err = got_error_from_errno2("fclose", path1);
8409 if (f2 && fclose(f2) == EOF && err == NULL)
8410 err = got_error_from_errno2("fclose", path2);
8411 if (outfile && fclose(outfile) == EOF && err == NULL)
8412 err = got_error_from_errno2("fclose", *path_unstaged_content);
8413 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8414 err = got_error_from_errno2("fclose", *path_new_staged_content);
8415 if (path1 && unlink(path1) == -1 && err == NULL)
8416 err = got_error_from_errno2("unlink", path1);
8417 if (path2 && unlink(path2) == -1 && err == NULL)
8418 err = got_error_from_errno2("unlink", path2);
8419 if (err || !have_content) {
8420 if (*path_unstaged_content &&
8421 unlink(*path_unstaged_content) == -1 && err == NULL)
8422 err = got_error_from_errno2("unlink",
8423 *path_unstaged_content);
8424 free(*path_unstaged_content);
8425 *path_unstaged_content = NULL;
8427 if (err || !have_content || !have_rejected_content) {
8428 if (*path_new_staged_content &&
8429 unlink(*path_new_staged_content) == -1 && err == NULL)
8430 err = got_error_from_errno2("unlink",
8431 *path_new_staged_content);
8432 free(*path_new_staged_content);
8433 *path_new_staged_content = NULL;
8440 static const struct got_error *
8441 unstage_hunks(struct got_object_id *staged_blob_id,
8442 struct got_blob_object *blob_base,
8443 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8444 const char *ondisk_path, const char *label_orig,
8445 struct got_worktree *worktree, struct got_repository *repo,
8446 got_worktree_patch_cb patch_cb, void *patch_arg,
8447 got_worktree_checkout_cb progress_cb, void *progress_arg)
8449 const struct got_error *err = NULL;
8450 char *path_unstaged_content = NULL;
8451 char *path_new_staged_content = NULL;
8452 char *parent = NULL, *base_path = NULL;
8453 char *blob_base_path = NULL;
8454 struct got_object_id *new_staged_blob_id = NULL;
8455 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8458 err = create_unstaged_content(&path_unstaged_content,
8459 &path_new_staged_content, blob_id, staged_blob_id,
8460 ie->path, repo, patch_cb, patch_arg);
8464 if (path_unstaged_content == NULL)
8467 if (path_new_staged_content) {
8468 err = got_object_blob_create(&new_staged_blob_id,
8469 path_new_staged_content, repo);
8474 f = fopen(path_unstaged_content, "re");
8476 err = got_error_from_errno2("fopen",
8477 path_unstaged_content);
8480 if (fstat(fileno(f), &sb) == -1) {
8481 err = got_error_from_errno2("fstat", path_unstaged_content);
8484 if (got_fileindex_entry_staged_filetype_get(ie) ==
8485 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8486 char link_target[PATH_MAX];
8488 r = fread(link_target, 1, sizeof(link_target), f);
8489 if (r == 0 && ferror(f)) {
8490 err = got_error_from_errno("fread");
8493 if (r >= sizeof(link_target)) { /* should not happen */
8494 err = got_error(GOT_ERR_NO_SPACE);
8497 link_target[r] = '\0';
8498 err = merge_symlink(worktree, blob_base,
8499 ondisk_path, ie->path, label_orig, link_target,
8500 worktree->base_commit_id, repo, progress_cb,
8503 int local_changes_subsumed;
8505 err = got_path_dirname(&parent, ondisk_path);
8509 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8511 err = got_error_from_errno("asprintf");
8516 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8519 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8525 * In order the run a 3-way merge with a symlink we copy the symlink's
8526 * target path into a temporary file and use that file with diff3.
8528 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8529 err = dump_symlink_target_path_to_file(&f_deriv2,
8535 fd = open(ondisk_path,
8536 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8538 err = got_error_from_errno2("open", ondisk_path);
8541 f_deriv2 = fdopen(fd, "r");
8542 if (f_deriv2 == NULL) {
8543 err = got_error_from_errno2("fdopen", ondisk_path);
8549 err = merge_file(&local_changes_subsumed, worktree,
8550 f_base, f, f_deriv2, ondisk_path, ie->path,
8551 got_fileindex_perms_to_st(ie),
8552 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8553 repo, progress_cb, progress_arg);
8558 if (new_staged_blob_id) {
8559 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8560 SHA1_DIGEST_LENGTH);
8562 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8563 got_fileindex_entry_staged_filetype_set(ie, 0);
8566 free(new_staged_blob_id);
8567 if (path_unstaged_content &&
8568 unlink(path_unstaged_content) == -1 && err == NULL)
8569 err = got_error_from_errno2("unlink", path_unstaged_content);
8570 if (path_new_staged_content &&
8571 unlink(path_new_staged_content) == -1 && err == NULL)
8572 err = got_error_from_errno2("unlink", path_new_staged_content);
8573 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8574 err = got_error_from_errno2("unlink", blob_base_path);
8575 if (f_base && fclose(f_base) == EOF && err == NULL)
8576 err = got_error_from_errno2("fclose", path_unstaged_content);
8577 if (f && fclose(f) == EOF && err == NULL)
8578 err = got_error_from_errno2("fclose", path_unstaged_content);
8579 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8580 err = got_error_from_errno2("fclose", ondisk_path);
8581 free(path_unstaged_content);
8582 free(path_new_staged_content);
8583 free(blob_base_path);
8589 static const struct got_error *
8590 unstage_path(void *arg, unsigned char status,
8591 unsigned char staged_status, const char *relpath,
8592 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8593 struct got_object_id *commit_id, int dirfd, const char *de_name)
8595 const struct got_error *err = NULL;
8596 struct unstage_path_arg *a = arg;
8597 struct got_fileindex_entry *ie;
8598 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8599 char *ondisk_path = NULL;
8600 char *id_str = NULL, *label_orig = NULL;
8601 int local_changes_subsumed;
8603 int fd1 = -1, fd2 = -1;
8605 if (staged_status != GOT_STATUS_ADD &&
8606 staged_status != GOT_STATUS_MODIFY &&
8607 staged_status != GOT_STATUS_DELETE)
8610 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8612 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8614 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8616 return got_error_from_errno("asprintf");
8618 err = got_object_id_str(&id_str,
8619 commit_id ? commit_id : a->worktree->base_commit_id);
8622 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8624 err = got_error_from_errno("asprintf");
8628 fd1 = got_opentempfd();
8630 err = got_error_from_errno("got_opentempfd");
8633 fd2 = got_opentempfd();
8635 err = got_error_from_errno("got_opentempfd");
8639 switch (staged_status) {
8640 case GOT_STATUS_MODIFY:
8641 err = got_object_open_as_blob(&blob_base, a->repo,
8642 blob_id, 8192, fd1);
8646 case GOT_STATUS_ADD:
8648 if (staged_status == GOT_STATUS_ADD) {
8649 int choice = GOT_PATCH_CHOICE_NONE;
8650 err = (*a->patch_cb)(&choice, a->patch_arg,
8651 staged_status, ie->path, NULL, 1, 1);
8654 if (choice != GOT_PATCH_CHOICE_YES)
8657 err = unstage_hunks(staged_blob_id,
8658 blob_base, blob_id, ie, ondisk_path,
8659 label_orig, a->worktree, a->repo,
8660 a->patch_cb, a->patch_arg,
8661 a->progress_cb, a->progress_arg);
8662 break; /* Done with this file. */
8665 err = got_object_open_as_blob(&blob_staged, a->repo,
8666 staged_blob_id, 8192, fd2);
8669 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8670 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8671 case GOT_FILEIDX_MODE_REGULAR_FILE:
8672 err = merge_blob(&local_changes_subsumed, a->worktree,
8673 blob_base, ondisk_path, relpath,
8674 got_fileindex_perms_to_st(ie), label_orig,
8675 blob_staged, commit_id ? commit_id :
8676 a->worktree->base_commit_id, a->repo,
8677 a->progress_cb, a->progress_arg);
8679 case GOT_FILEIDX_MODE_SYMLINK:
8680 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8681 char *staged_target;
8682 err = got_object_blob_read_to_str(
8683 &staged_target, blob_staged);
8686 err = merge_symlink(a->worktree, blob_base,
8687 ondisk_path, relpath, label_orig,
8688 staged_target, commit_id ? commit_id :
8689 a->worktree->base_commit_id,
8690 a->repo, a->progress_cb, a->progress_arg);
8691 free(staged_target);
8693 err = merge_blob(&local_changes_subsumed,
8694 a->worktree, blob_base, ondisk_path,
8695 relpath, got_fileindex_perms_to_st(ie),
8696 label_orig, blob_staged,
8697 commit_id ? commit_id :
8698 a->worktree->base_commit_id, a->repo,
8699 a->progress_cb, a->progress_arg);
8703 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8707 got_fileindex_entry_stage_set(ie,
8708 GOT_FILEIDX_STAGE_NONE);
8709 got_fileindex_entry_staged_filetype_set(ie, 0);
8712 case GOT_STATUS_DELETE:
8714 int choice = GOT_PATCH_CHOICE_NONE;
8715 err = (*a->patch_cb)(&choice, a->patch_arg,
8716 staged_status, ie->path, NULL, 1, 1);
8719 if (choice == GOT_PATCH_CHOICE_NO)
8721 if (choice != GOT_PATCH_CHOICE_YES) {
8722 err = got_error(GOT_ERR_PATCH_CHOICE);
8726 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8727 got_fileindex_entry_staged_filetype_set(ie, 0);
8728 err = get_file_status(&status, &sb, ie, ondisk_path,
8729 dirfd, de_name, a->repo);
8732 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8737 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8738 err = got_error_from_errno("close");
8740 got_object_blob_close(blob_base);
8741 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8742 err = got_error_from_errno("close");
8744 got_object_blob_close(blob_staged);
8750 const struct got_error *
8751 got_worktree_unstage(struct got_worktree *worktree,
8752 struct got_pathlist_head *paths,
8753 got_worktree_checkout_cb progress_cb, void *progress_arg,
8754 got_worktree_patch_cb patch_cb, void *patch_arg,
8755 struct got_repository *repo)
8757 const struct got_error *err = NULL, *sync_err, *unlockerr;
8758 struct got_pathlist_entry *pe;
8759 struct got_fileindex *fileindex = NULL;
8760 char *fileindex_path = NULL;
8761 struct unstage_path_arg upa;
8763 err = lock_worktree(worktree, LOCK_EX);
8767 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8771 upa.worktree = worktree;
8772 upa.fileindex = fileindex;
8774 upa.progress_cb = progress_cb;
8775 upa.progress_arg = progress_arg;
8776 upa.patch_cb = patch_cb;
8777 upa.patch_arg = patch_arg;
8778 TAILQ_FOREACH(pe, paths, entry) {
8779 err = worktree_status(worktree, pe->path, fileindex, repo,
8780 unstage_path, &upa, NULL, NULL, 1, 0);
8785 sync_err = sync_fileindex(fileindex, fileindex_path);
8786 if (sync_err && err == NULL)
8789 free(fileindex_path);
8791 got_fileindex_free(fileindex);
8792 unlockerr = lock_worktree(worktree, LOCK_SH);
8793 if (unlockerr && err == NULL)
8798 struct report_file_info_arg {
8799 struct got_worktree *worktree;
8800 got_worktree_path_info_cb info_cb;
8802 struct got_pathlist_head *paths;
8803 got_cancel_cb cancel_cb;
8807 static const struct got_error *
8808 report_file_info(void *arg, struct got_fileindex_entry *ie)
8810 struct report_file_info_arg *a = arg;
8811 struct got_pathlist_entry *pe;
8812 struct got_object_id blob_id, staged_blob_id, commit_id;
8813 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8814 struct got_object_id *commit_idp = NULL;
8817 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8818 return got_error(GOT_ERR_CANCELLED);
8820 TAILQ_FOREACH(pe, a->paths, entry) {
8821 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8822 got_path_is_child(ie->path, pe->path, pe->path_len))
8825 if (pe == NULL) /* not found */
8828 if (got_fileindex_entry_has_blob(ie)) {
8829 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8830 blob_idp = &blob_id;
8832 stage = got_fileindex_entry_stage_get(ie);
8833 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8834 stage == GOT_FILEIDX_STAGE_ADD) {
8835 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8836 SHA1_DIGEST_LENGTH);
8837 staged_blob_idp = &staged_blob_id;
8840 if (got_fileindex_entry_has_commit(ie)) {
8841 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8842 commit_idp = &commit_id;
8845 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8846 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8849 const struct got_error *
8850 got_worktree_path_info(struct got_worktree *worktree,
8851 struct got_pathlist_head *paths,
8852 got_worktree_path_info_cb info_cb, void *info_arg,
8853 got_cancel_cb cancel_cb, void *cancel_arg)
8856 const struct got_error *err = NULL, *unlockerr;
8857 struct got_fileindex *fileindex = NULL;
8858 char *fileindex_path = NULL;
8859 struct report_file_info_arg arg;
8861 err = lock_worktree(worktree, LOCK_SH);
8865 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8869 arg.worktree = worktree;
8870 arg.info_cb = info_cb;
8871 arg.info_arg = info_arg;
8873 arg.cancel_cb = cancel_cb;
8874 arg.cancel_arg = cancel_arg;
8875 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8878 free(fileindex_path);
8880 got_fileindex_free(fileindex);
8881 unlockerr = lock_worktree(worktree, LOCK_UN);
8882 if (unlockerr && err == NULL)
8887 static const struct got_error *
8888 patch_check_path(const char *p, char **path, unsigned char *status,
8889 unsigned char *staged_status, struct got_fileindex *fileindex,
8890 struct got_worktree *worktree, struct got_repository *repo)
8892 const struct got_error *err;
8893 struct got_fileindex_entry *ie;
8895 char *ondisk_path = NULL;
8897 err = got_worktree_resolve_path(path, worktree, p);
8901 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8902 *path[0] ? "/" : "", *path) == -1)
8903 return got_error_from_errno("asprintf");
8905 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8907 *staged_status = get_staged_status(ie);
8908 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8913 *staged_status = GOT_STATUS_NO_CHANGE;
8914 *status = GOT_STATUS_UNVERSIONED;
8915 if (lstat(ondisk_path, &sb) == -1) {
8916 if (errno != ENOENT) {
8917 err = got_error_from_errno2("lstat",
8921 *status = GOT_STATUS_NONEXISTENT;
8930 static const struct got_error *
8931 patch_can_rm(const char *path, unsigned char status,
8932 unsigned char staged_status)
8934 if (status == GOT_STATUS_NONEXISTENT)
8935 return got_error_set_errno(ENOENT, path);
8936 if (status != GOT_STATUS_NO_CHANGE &&
8937 status != GOT_STATUS_ADD &&
8938 status != GOT_STATUS_MODIFY &&
8939 status != GOT_STATUS_MODE_CHANGE)
8940 return got_error_path(path, GOT_ERR_FILE_STATUS);
8941 if (staged_status == GOT_STATUS_DELETE)
8942 return got_error_path(path, GOT_ERR_FILE_STATUS);
8946 static const struct got_error *
8947 patch_can_add(const char *path, unsigned char status)
8949 if (status != GOT_STATUS_NONEXISTENT)
8950 return got_error_path(path, GOT_ERR_FILE_STATUS);
8954 static const struct got_error *
8955 patch_can_edit(const char *path, unsigned char status,
8956 unsigned char staged_status)
8958 if (status == GOT_STATUS_NONEXISTENT)
8959 return got_error_set_errno(ENOENT, path);
8960 if (status != GOT_STATUS_NO_CHANGE &&
8961 status != GOT_STATUS_ADD &&
8962 status != GOT_STATUS_MODIFY)
8963 return got_error_path(path, GOT_ERR_FILE_STATUS);
8964 if (staged_status == GOT_STATUS_DELETE)
8965 return got_error_path(path, GOT_ERR_FILE_STATUS);
8969 const struct got_error *
8970 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8971 char **fileindex_path, struct got_worktree *worktree)
8973 return open_fileindex(fileindex, fileindex_path, worktree);
8976 const struct got_error *
8977 got_worktree_patch_check_path(const char *old, const char *new,
8978 char **oldpath, char **newpath, struct got_worktree *worktree,
8979 struct got_repository *repo, struct got_fileindex *fileindex)
8981 const struct got_error *err = NULL;
8982 int file_renamed = 0;
8983 unsigned char status_old, staged_status_old;
8984 unsigned char status_new, staged_status_new;
8989 err = patch_check_path(old != NULL ? old : new, oldpath,
8990 &status_old, &staged_status_old, fileindex, worktree, repo);
8994 err = patch_check_path(new != NULL ? new : old, newpath,
8995 &status_new, &staged_status_new, fileindex, worktree, repo);
8999 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9002 if (old != NULL && new == NULL)
9003 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9004 else if (file_renamed) {
9005 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9007 err = patch_can_add(*newpath, status_new);
9008 } else if (old == NULL)
9009 err = patch_can_add(*newpath, status_new);
9011 err = patch_can_edit(*newpath, status_new, staged_status_new);
9023 const struct got_error *
9024 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9025 struct got_worktree *worktree, struct got_fileindex *fileindex,
9026 got_worktree_checkout_cb progress_cb, void *progress_arg)
9028 struct schedule_addition_args saa;
9030 memset(&saa, 0, sizeof(saa));
9031 saa.worktree = worktree;
9032 saa.fileindex = fileindex;
9033 saa.progress_cb = progress_cb;
9034 saa.progress_arg = progress_arg;
9037 return worktree_status(worktree, path, fileindex, repo,
9038 schedule_addition, &saa, NULL, NULL, 1, 0);
9041 const struct got_error *
9042 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9043 struct got_worktree *worktree, struct got_fileindex *fileindex,
9044 got_worktree_delete_cb progress_cb, void *progress_arg)
9046 struct schedule_deletion_args sda;
9048 memset(&sda, 0, sizeof(sda));
9049 sda.worktree = worktree;
9050 sda.fileindex = fileindex;
9051 sda.progress_cb = progress_cb;
9052 sda.progress_arg = progress_arg;
9054 sda.delete_local_mods = 0;
9055 sda.keep_on_disk = 0;
9056 sda.ignore_missing_paths = 0;
9057 sda.status_codes = NULL;
9059 return worktree_status(worktree, path, fileindex, repo,
9060 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9063 const struct got_error *
9064 got_worktree_patch_complete(struct got_fileindex *fileindex,
9065 const char *fileindex_path)
9067 const struct got_error *err = NULL;
9069 err = sync_fileindex(fileindex, fileindex_path);
9070 got_fileindex_free(fileindex);