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 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1260 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1262 *is_bad_symlink = 0;
1265 * Blob object content specifies the target path of the link.
1266 * If a symbolic link cannot be installed we instead create
1267 * a regular file which contains the link target path stored
1268 * in the blob object.
1271 err = got_object_blob_read_block(&len, blob);
1275 if (len + target_len >= sizeof(target_path)) {
1276 /* Path too long; install as a regular file. */
1277 *is_bad_symlink = 1;
1278 got_object_blob_rewind(blob);
1279 return install_blob(worktree, ondisk_path, path,
1280 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1281 restoring_missing_file, reverting_versioned_file,
1282 1, path_is_unversioned, repo, progress_cb,
1286 /* Skip blob object header first time around. */
1287 memcpy(target_path + target_len, buf + hdrlen,
1289 target_len += len - hdrlen;
1293 target_path[target_len] = '\0';
1295 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1296 ondisk_path, worktree->root_path);
1300 if (*is_bad_symlink && !allow_bad_symlinks) {
1301 /* install as a regular file */
1302 got_object_blob_rewind(blob);
1303 err = install_blob(worktree, ondisk_path, path,
1304 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1305 restoring_missing_file, reverting_versioned_file, 1,
1306 path_is_unversioned, repo, progress_cb, progress_arg);
1310 if (symlink(target_path, ondisk_path) == -1) {
1311 if (errno == EEXIST) {
1312 int symlink_replaced;
1313 if (path_is_unversioned) {
1314 err = (*progress_cb)(progress_arg,
1315 GOT_STATUS_UNVERSIONED, path);
1318 err = replace_existing_symlink(&symlink_replaced,
1319 ondisk_path, target_path, target_len);
1323 if (symlink_replaced) {
1324 err = (*progress_cb)(progress_arg,
1325 reverting_versioned_file ?
1327 GOT_STATUS_UPDATE, path);
1329 err = (*progress_cb)(progress_arg,
1330 GOT_STATUS_EXISTS, path);
1333 return err; /* Nothing else to do. */
1336 if (errno == ENOENT) {
1338 err = got_path_dirname(&parent, ondisk_path);
1341 err = add_dir_on_disk(worktree, parent);
1346 * Retry, and fall through to error handling
1347 * below if this second attempt fails.
1349 if (symlink(target_path, ondisk_path) != -1) {
1350 err = NULL; /* success */
1355 /* Handle errors from first or second creation attempt. */
1356 if (errno == ENAMETOOLONG) {
1357 /* bad target path; install as a regular file */
1358 *is_bad_symlink = 1;
1359 got_object_blob_rewind(blob);
1360 err = install_blob(worktree, ondisk_path, path,
1361 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1362 restoring_missing_file, reverting_versioned_file, 1,
1363 path_is_unversioned, repo,
1364 progress_cb, progress_arg);
1365 } else if (errno == ENOTDIR) {
1366 err = got_error_path(ondisk_path,
1367 GOT_ERR_FILE_OBSTRUCTED);
1369 err = got_error_from_errno3("symlink",
1370 target_path, ondisk_path);
1372 } else if (progress_cb)
1373 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1374 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;
3139 int fd1 = -1, fd2 = -1;
3142 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3145 err = got_object_id_by_path(&tree_id1, repo, commit1,
3146 worktree->path_prefix);
3147 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3153 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3157 err = got_object_id_str(&id_str, commit_id1);
3161 if (asprintf(&label_orig, "%s: commit %s",
3162 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3163 err = got_error_from_errno("asprintf");
3169 f1 = got_opentemp();
3171 err = got_error_from_errno("got_opentemp");
3176 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3180 err = got_object_id_by_path(&tree_id2, repo, commit2,
3181 worktree->path_prefix);
3185 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3189 f2 = got_opentemp();
3191 err = got_error_from_errno("got_opentemp");
3195 fd1 = got_opentempfd();
3197 err = got_error_from_errno("got_opentempfd");
3201 fd2 = got_opentempfd();
3203 err = got_error_from_errno("got_opentempfd");
3207 cmc_arg.worktree = worktree;
3208 cmc_arg.fileindex = fileindex;
3209 cmc_arg.repo = repo;
3210 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3211 check_merge_conflicts, &cmc_arg, 0);
3215 arg.worktree = worktree;
3216 arg.fileindex = fileindex;
3217 arg.progress_cb = progress_cb;
3218 arg.progress_arg = progress_arg;
3219 arg.cancel_cb = cancel_cb;
3220 arg.cancel_arg = cancel_arg;
3221 arg.label_orig = label_orig;
3222 arg.commit_id2 = commit_id2;
3223 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3224 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3225 merge_file_cb, &arg, 1);
3226 sync_err = sync_fileindex(fileindex, fileindex_path);
3227 if (sync_err && err == NULL)
3231 got_object_commit_close(commit1);
3233 got_object_commit_close(commit2);
3235 got_object_tree_close(tree1);
3237 got_object_tree_close(tree2);
3238 if (f1 && fclose(f1) == EOF && err == NULL)
3239 err = got_error_from_errno("fclose");
3240 if (f2 && fclose(f2) == EOF && err == NULL)
3241 err = got_error_from_errno("fclose");
3242 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3243 err = got_error_from_errno("close");
3244 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3245 err = got_error_from_errno("close");
3250 const struct got_error *
3251 got_worktree_merge_files(struct got_worktree *worktree,
3252 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3253 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3254 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3256 const struct got_error *err, *unlockerr;
3257 char *fileindex_path = NULL;
3258 struct got_fileindex *fileindex = NULL;
3260 err = lock_worktree(worktree, LOCK_EX);
3264 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3268 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3273 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3274 commit_id2, repo, progress_cb, progress_arg,
3275 cancel_cb, cancel_arg);
3278 got_fileindex_free(fileindex);
3279 free(fileindex_path);
3280 unlockerr = lock_worktree(worktree, LOCK_SH);
3281 if (unlockerr && err == NULL)
3286 struct diff_dir_cb_arg {
3287 struct got_fileindex *fileindex;
3288 struct got_worktree *worktree;
3289 const char *status_path;
3290 size_t status_path_len;
3291 struct got_repository *repo;
3292 got_worktree_status_cb status_cb;
3294 got_cancel_cb cancel_cb;
3296 /* A pathlist containing per-directory pathlists of ignore patterns. */
3297 struct got_pathlist_head *ignores;
3298 int report_unchanged;
3302 static const struct got_error *
3303 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3304 int dirfd, const char *de_name,
3305 got_worktree_status_cb status_cb, void *status_arg,
3306 struct got_repository *repo, int report_unchanged)
3308 const struct got_error *err = NULL;
3309 unsigned char status = GOT_STATUS_NO_CHANGE;
3310 unsigned char staged_status = get_staged_status(ie);
3312 struct got_object_id blob_id, commit_id, staged_blob_id;
3313 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3314 struct got_object_id *staged_blob_idp = NULL;
3316 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3320 if (status == GOT_STATUS_NO_CHANGE &&
3321 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3324 if (got_fileindex_entry_has_blob(ie)) {
3325 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3326 blob_idp = &blob_id;
3328 if (got_fileindex_entry_has_commit(ie)) {
3329 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3330 commit_idp = &commit_id;
3332 if (staged_status == GOT_STATUS_ADD ||
3333 staged_status == GOT_STATUS_MODIFY) {
3334 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3335 SHA1_DIGEST_LENGTH);
3336 staged_blob_idp = &staged_blob_id;
3339 return (*status_cb)(status_arg, status, staged_status,
3340 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3343 static const struct got_error *
3344 status_old_new(void *arg, struct got_fileindex_entry *ie,
3345 struct dirent *de, const char *parent_path, int dirfd)
3347 const struct got_error *err = NULL;
3348 struct diff_dir_cb_arg *a = arg;
3351 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3352 return got_error(GOT_ERR_CANCELLED);
3354 if (got_path_cmp(parent_path, a->status_path,
3355 strlen(parent_path), a->status_path_len) != 0 &&
3356 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3359 if (parent_path[0]) {
3360 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3361 parent_path, de->d_name) == -1)
3362 return got_error_from_errno("asprintf");
3364 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3366 return got_error_from_errno("asprintf");
3369 err = report_file_status(ie, abspath, dirfd, de->d_name,
3370 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3375 static const struct got_error *
3376 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3378 struct diff_dir_cb_arg *a = arg;
3379 struct got_object_id blob_id, commit_id;
3380 unsigned char status;
3382 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3383 return got_error(GOT_ERR_CANCELLED);
3385 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3388 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3389 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3390 if (got_fileindex_entry_has_file_on_disk(ie))
3391 status = GOT_STATUS_MISSING;
3393 status = GOT_STATUS_DELETE;
3394 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3395 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3399 free_ignorelist(struct got_pathlist_head *ignorelist)
3401 struct got_pathlist_entry *pe;
3403 TAILQ_FOREACH(pe, ignorelist, entry)
3404 free((char *)pe->path);
3405 got_pathlist_free(ignorelist);
3409 free_ignores(struct got_pathlist_head *ignores)
3411 struct got_pathlist_entry *pe;
3413 TAILQ_FOREACH(pe, ignores, entry) {
3414 struct got_pathlist_head *ignorelist = pe->data;
3415 free_ignorelist(ignorelist);
3416 free((char *)pe->path);
3418 got_pathlist_free(ignores);
3421 static const struct got_error *
3422 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3424 const struct got_error *err = NULL;
3425 struct got_pathlist_entry *pe = NULL;
3426 struct got_pathlist_head *ignorelist;
3427 char *line = NULL, *pattern, *dirpath = NULL;
3428 size_t linesize = 0;
3431 ignorelist = calloc(1, sizeof(*ignorelist));
3432 if (ignorelist == NULL)
3433 return got_error_from_errno("calloc");
3434 TAILQ_INIT(ignorelist);
3436 while ((linelen = getline(&line, &linesize, f)) != -1) {
3437 if (linelen > 0 && line[linelen - 1] == '\n')
3438 line[linelen - 1] = '\0';
3440 /* Git's ignores may contain comments. */
3444 /* Git's negated patterns are not (yet?) supported. */
3448 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3450 err = got_error_from_errno("asprintf");
3453 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3458 err = got_error_from_errno("getline");
3462 dirpath = strdup(path);
3463 if (dirpath == NULL) {
3464 err = got_error_from_errno("strdup");
3467 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3470 if (err || pe == NULL) {
3472 free_ignorelist(ignorelist);
3478 match_ignores(struct got_pathlist_head *ignores, const char *path)
3480 struct got_pathlist_entry *pe;
3482 /* Handle patterns which match in all directories. */
3483 TAILQ_FOREACH(pe, ignores, entry) {
3484 struct got_pathlist_head *ignorelist = pe->data;
3485 struct got_pathlist_entry *pi;
3487 TAILQ_FOREACH(pi, ignorelist, entry) {
3488 const char *p, *pattern = pi->path;
3490 if (strncmp(pattern, "**/", 3) != 0)
3495 if (fnmatch(pattern, p,
3496 FNM_PATHNAME | FNM_LEADING_DIR)) {
3497 /* Retry in next directory. */
3498 while (*p && *p != '/')
3510 * The ignores pathlist contains ignore lists from children before
3511 * parents, so we can find the most specific ignorelist by walking
3512 * ignores backwards.
3514 pe = TAILQ_LAST(ignores, got_pathlist_head);
3516 if (got_path_is_child(path, pe->path, pe->path_len)) {
3517 struct got_pathlist_head *ignorelist = pe->data;
3518 struct got_pathlist_entry *pi;
3519 TAILQ_FOREACH(pi, ignorelist, entry) {
3520 const char *pattern = pi->path;
3521 int flags = FNM_LEADING_DIR;
3522 if (strstr(pattern, "/**/") == NULL)
3523 flags |= FNM_PATHNAME;
3524 if (fnmatch(pattern, path, flags))
3529 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3535 static const struct got_error *
3536 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3537 const char *path, int dirfd, const char *ignores_filename)
3539 const struct got_error *err = NULL;
3542 FILE *ignoresfile = NULL;
3544 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3545 path[0] ? "/" : "", ignores_filename) == -1)
3546 return got_error_from_errno("asprintf");
3549 fd = openat(dirfd, ignores_filename,
3550 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3552 if (errno != ENOENT && errno != EACCES)
3553 err = got_error_from_errno2("openat",
3556 ignoresfile = fdopen(fd, "r");
3557 if (ignoresfile == NULL)
3558 err = got_error_from_errno2("fdopen",
3562 err = read_ignores(ignores, path, ignoresfile);
3566 ignoresfile = fopen(ignorespath, "re");
3567 if (ignoresfile == NULL) {
3568 if (errno != ENOENT && errno != EACCES)
3569 err = got_error_from_errno2("fopen",
3572 err = read_ignores(ignores, path, ignoresfile);
3575 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3576 err = got_error_from_errno2("fclose", path);
3577 if (fd != -1 && close(fd) == -1 && err == NULL)
3578 err = got_error_from_errno2("close", path);
3583 static const struct got_error *
3584 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3587 const struct got_error *err = NULL;
3588 struct diff_dir_cb_arg *a = arg;
3594 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3595 return got_error(GOT_ERR_CANCELLED);
3597 if (parent_path[0]) {
3598 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3599 return got_error_from_errno("asprintf");
3604 if (de->d_type == DT_DIR) {
3605 if (!a->no_ignores && ignore != NULL &&
3606 match_ignores(a->ignores, path))
3608 } else if (!match_ignores(a->ignores, path) &&
3609 got_path_is_child(path, a->status_path, a->status_path_len))
3610 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3611 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3617 static const struct got_error *
3618 status_traverse(void *arg, const char *path, int dirfd)
3620 const struct got_error *err = NULL;
3621 struct diff_dir_cb_arg *a = arg;
3626 err = add_ignores(a->ignores, a->worktree->root_path,
3627 path, dirfd, ".cvsignore");
3631 err = add_ignores(a->ignores, a->worktree->root_path, path,
3632 dirfd, ".gitignore");
3637 static const struct got_error *
3638 report_single_file_status(const char *path, const char *ondisk_path,
3639 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3640 void *status_arg, struct got_repository *repo, int report_unchanged,
3641 struct got_pathlist_head *ignores, int no_ignores)
3643 struct got_fileindex_entry *ie;
3646 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3648 return report_file_status(ie, ondisk_path, -1, NULL,
3649 status_cb, status_arg, repo, report_unchanged);
3651 if (lstat(ondisk_path, &sb) == -1) {
3652 if (errno != ENOENT)
3653 return got_error_from_errno2("lstat", ondisk_path);
3654 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3655 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3658 if (!no_ignores && match_ignores(ignores, path))
3661 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3662 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3663 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3668 static const struct got_error *
3669 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3670 const char *root_path, const char *path)
3672 const struct got_error *err;
3673 char *parent_path, *next_parent_path = NULL;
3675 err = add_ignores(ignores, root_path, "", -1,
3680 err = add_ignores(ignores, root_path, "", -1,
3685 err = got_path_dirname(&parent_path, path);
3687 if (err->code == GOT_ERR_BAD_PATH)
3688 return NULL; /* cannot traverse parent */
3692 err = add_ignores(ignores, root_path, parent_path, -1,
3696 err = add_ignores(ignores, root_path, parent_path, -1,
3700 err = got_path_dirname(&next_parent_path, parent_path);
3702 if (err->code == GOT_ERR_BAD_PATH)
3703 err = NULL; /* traversed everything */
3706 if (got_path_is_root_dir(parent_path))
3709 parent_path = next_parent_path;
3710 next_parent_path = NULL;
3714 free(next_parent_path);
3718 static const struct got_error *
3719 worktree_status(struct got_worktree *worktree, const char *path,
3720 struct got_fileindex *fileindex, struct got_repository *repo,
3721 got_worktree_status_cb status_cb, void *status_arg,
3722 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3723 int report_unchanged)
3725 const struct got_error *err = NULL;
3727 struct got_fileindex_diff_dir_cb fdiff_cb;
3728 struct diff_dir_cb_arg arg;
3729 char *ondisk_path = NULL;
3730 struct got_pathlist_head ignores;
3731 struct got_fileindex_entry *ie;
3733 TAILQ_INIT(&ignores);
3735 if (asprintf(&ondisk_path, "%s%s%s",
3736 worktree->root_path, path[0] ? "/" : "", path) == -1)
3737 return got_error_from_errno("asprintf");
3739 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3741 err = report_single_file_status(path, ondisk_path,
3742 fileindex, status_cb, status_arg, repo,
3743 report_unchanged, &ignores, no_ignores);
3747 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3749 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3750 !got_err_open_nofollow_on_symlink())
3751 err = got_error_from_errno2("open", ondisk_path);
3754 err = add_ignores_from_parent_paths(&ignores,
3755 worktree->root_path, ondisk_path);
3759 err = report_single_file_status(path, ondisk_path,
3760 fileindex, status_cb, status_arg, repo,
3761 report_unchanged, &ignores, no_ignores);
3764 fdiff_cb.diff_old_new = status_old_new;
3765 fdiff_cb.diff_old = status_old;
3766 fdiff_cb.diff_new = status_new;
3767 fdiff_cb.diff_traverse = status_traverse;
3768 arg.fileindex = fileindex;
3769 arg.worktree = worktree;
3770 arg.status_path = path;
3771 arg.status_path_len = strlen(path);
3773 arg.status_cb = status_cb;
3774 arg.status_arg = status_arg;
3775 arg.cancel_cb = cancel_cb;
3776 arg.cancel_arg = cancel_arg;
3777 arg.report_unchanged = report_unchanged;
3778 arg.no_ignores = no_ignores;
3780 err = add_ignores_from_parent_paths(&ignores,
3781 worktree->root_path, path);
3785 arg.ignores = &ignores;
3786 err = got_fileindex_diff_dir(fileindex, fd,
3787 worktree->root_path, path, repo, &fdiff_cb, &arg);
3790 free_ignores(&ignores);
3791 if (fd != -1 && close(fd) == -1 && err == NULL)
3792 err = got_error_from_errno("close");
3797 const struct got_error *
3798 got_worktree_status(struct got_worktree *worktree,
3799 struct got_pathlist_head *paths, struct got_repository *repo,
3800 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3801 got_cancel_cb cancel_cb, void *cancel_arg)
3803 const struct got_error *err = NULL;
3804 char *fileindex_path = NULL;
3805 struct got_fileindex *fileindex = NULL;
3806 struct got_pathlist_entry *pe;
3808 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3812 TAILQ_FOREACH(pe, paths, entry) {
3813 err = worktree_status(worktree, pe->path, fileindex, repo,
3814 status_cb, status_arg, cancel_cb, cancel_arg,
3819 free(fileindex_path);
3820 got_fileindex_free(fileindex);
3824 const struct got_error *
3825 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3828 const struct got_error *err = NULL;
3829 char *resolved = NULL, *cwd = NULL, *path = NULL;
3832 char *abspath = NULL;
3833 char canonpath[PATH_MAX];
3837 cwd = getcwd(NULL, 0);
3839 return got_error_from_errno("getcwd");
3841 if (lstat(arg, &sb) == -1) {
3842 if (errno != ENOENT) {
3843 err = got_error_from_errno2("lstat", arg);
3848 if (S_ISLNK(sb.st_mode)) {
3850 * We cannot use realpath(3) with symlinks since we want to
3851 * operate on the symlink itself.
3852 * But we can make the path absolute, assuming it is relative
3853 * to the current working directory, and then canonicalize it.
3855 if (!got_path_is_absolute(arg)) {
3856 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3857 err = got_error_from_errno("asprintf");
3862 err = got_canonpath(abspath ? abspath : arg, canonpath,
3866 resolved = strdup(canonpath);
3867 if (resolved == NULL) {
3868 err = got_error_from_errno("strdup");
3872 resolved = realpath(arg, NULL);
3873 if (resolved == NULL) {
3874 if (errno != ENOENT) {
3875 err = got_error_from_errno2("realpath", arg);
3878 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3879 err = got_error_from_errno("asprintf");
3882 err = got_canonpath(abspath, canonpath,
3886 resolved = strdup(canonpath);
3887 if (resolved == NULL) {
3888 err = got_error_from_errno("strdup");
3894 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3895 strlen(got_worktree_get_root_path(worktree)))) {
3896 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3900 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3901 err = got_path_skip_common_ancestor(&path,
3902 got_worktree_get_root_path(worktree), resolved);
3908 err = got_error_from_errno("strdup");
3913 /* XXX status walk can't deal with trailing slash! */
3915 while (len > 0 && path[len - 1] == '/') {
3916 path[len - 1] = '\0';
3930 struct schedule_addition_args {
3931 struct got_worktree *worktree;
3932 struct got_fileindex *fileindex;
3933 got_worktree_checkout_cb progress_cb;
3935 struct got_repository *repo;
3938 static const struct got_error *
3939 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3940 const char *relpath, struct got_object_id *blob_id,
3941 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3942 int dirfd, const char *de_name)
3944 struct schedule_addition_args *a = arg;
3945 const struct got_error *err = NULL;
3946 struct got_fileindex_entry *ie;
3950 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3952 return got_error_from_errno("asprintf");
3954 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3956 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3960 /* Re-adding an existing entry is a no-op. */
3961 if (status == GOT_STATUS_ADD)
3963 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3968 if (status != GOT_STATUS_UNVERSIONED) {
3969 if (status == GOT_STATUS_NONEXISTENT)
3970 err = got_error_set_errno(ENOENT, ondisk_path);
3972 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3976 err = got_fileindex_entry_alloc(&ie, relpath);
3979 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3980 relpath, NULL, NULL, 1);
3982 got_fileindex_entry_free(ie);
3985 err = got_fileindex_entry_add(a->fileindex, ie);
3987 got_fileindex_entry_free(ie);
3994 if (status == GOT_STATUS_ADD)
3996 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3999 const struct got_error *
4000 got_worktree_schedule_add(struct got_worktree *worktree,
4001 struct got_pathlist_head *paths,
4002 got_worktree_checkout_cb progress_cb, void *progress_arg,
4003 struct got_repository *repo, int no_ignores)
4005 struct got_fileindex *fileindex = NULL;
4006 char *fileindex_path = NULL;
4007 const struct got_error *err = NULL, *sync_err, *unlockerr;
4008 struct got_pathlist_entry *pe;
4009 struct schedule_addition_args saa;
4011 err = lock_worktree(worktree, LOCK_EX);
4015 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4019 saa.worktree = worktree;
4020 saa.fileindex = fileindex;
4021 saa.progress_cb = progress_cb;
4022 saa.progress_arg = progress_arg;
4025 TAILQ_FOREACH(pe, paths, entry) {
4026 err = worktree_status(worktree, pe->path, fileindex, repo,
4027 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4031 sync_err = sync_fileindex(fileindex, fileindex_path);
4032 if (sync_err && err == NULL)
4035 free(fileindex_path);
4037 got_fileindex_free(fileindex);
4038 unlockerr = lock_worktree(worktree, LOCK_SH);
4039 if (unlockerr && err == NULL)
4044 struct schedule_deletion_args {
4045 struct got_worktree *worktree;
4046 struct got_fileindex *fileindex;
4047 got_worktree_delete_cb progress_cb;
4049 struct got_repository *repo;
4050 int delete_local_mods;
4052 int ignore_missing_paths;
4053 const char *status_codes;
4056 static const struct got_error *
4057 schedule_for_deletion(void *arg, unsigned char status,
4058 unsigned char staged_status, const char *relpath,
4059 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4060 struct got_object_id *commit_id, int dirfd, const char *de_name)
4062 struct schedule_deletion_args *a = arg;
4063 const struct got_error *err = NULL;
4064 struct got_fileindex_entry *ie = NULL;
4068 if (status == GOT_STATUS_NONEXISTENT) {
4069 if (a->ignore_missing_paths)
4071 return got_error_set_errno(ENOENT, relpath);
4074 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4076 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4078 staged_status = get_staged_status(ie);
4079 if (staged_status != GOT_STATUS_NO_CHANGE) {
4080 if (staged_status == GOT_STATUS_DELETE)
4082 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4085 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4087 return got_error_from_errno("asprintf");
4089 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4094 if (a->status_codes) {
4095 size_t ncodes = strlen(a->status_codes);
4097 for (i = 0; i < ncodes ; i++) {
4098 if (status == a->status_codes[i])
4102 /* Do not delete files in non-matching status. */
4106 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4107 a->status_codes[i] != GOT_STATUS_MISSING) {
4108 static char msg[64];
4109 snprintf(msg, sizeof(msg),
4110 "invalid status code '%c'", a->status_codes[i]);
4111 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4116 if (status != GOT_STATUS_NO_CHANGE) {
4117 if (status == GOT_STATUS_DELETE)
4119 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4120 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4123 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4124 err = got_error_set_errno(ENOENT, relpath);
4127 if (status != GOT_STATUS_MODIFY &&
4128 status != GOT_STATUS_MISSING) {
4129 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4134 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4138 if (unlinkat(dirfd, de_name, 0) != 0) {
4139 err = got_error_from_errno2("unlinkat",
4143 } else if (unlink(ondisk_path) != 0) {
4144 err = got_error_from_errno2("unlink", ondisk_path);
4148 root_len = strlen(a->worktree->root_path);
4151 err = got_path_dirname(&parent, ondisk_path);
4155 ondisk_path = parent;
4156 if (rmdir(ondisk_path) == -1) {
4157 if (errno != ENOTEMPTY)
4158 err = got_error_from_errno2("rmdir",
4162 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4163 strlen(ondisk_path), root_len) != 0);
4166 got_fileindex_entry_mark_deleted_from_disk(ie);
4171 if (status == GOT_STATUS_DELETE)
4173 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4174 staged_status, relpath);
4177 const struct got_error *
4178 got_worktree_schedule_delete(struct got_worktree *worktree,
4179 struct got_pathlist_head *paths, int delete_local_mods,
4180 const char *status_codes,
4181 got_worktree_delete_cb progress_cb, void *progress_arg,
4182 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4184 struct got_fileindex *fileindex = NULL;
4185 char *fileindex_path = NULL;
4186 const struct got_error *err = NULL, *sync_err, *unlockerr;
4187 struct got_pathlist_entry *pe;
4188 struct schedule_deletion_args sda;
4190 err = lock_worktree(worktree, LOCK_EX);
4194 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4198 sda.worktree = worktree;
4199 sda.fileindex = fileindex;
4200 sda.progress_cb = progress_cb;
4201 sda.progress_arg = progress_arg;
4203 sda.delete_local_mods = delete_local_mods;
4204 sda.keep_on_disk = keep_on_disk;
4205 sda.ignore_missing_paths = ignore_missing_paths;
4206 sda.status_codes = status_codes;
4208 TAILQ_FOREACH(pe, paths, entry) {
4209 err = worktree_status(worktree, pe->path, fileindex, repo,
4210 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4214 sync_err = sync_fileindex(fileindex, fileindex_path);
4215 if (sync_err && err == NULL)
4218 free(fileindex_path);
4220 got_fileindex_free(fileindex);
4221 unlockerr = lock_worktree(worktree, LOCK_SH);
4222 if (unlockerr && err == NULL)
4227 static const struct got_error *
4228 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4230 const struct got_error *err = NULL;
4232 size_t linesize = 0, n;
4235 linelen = getline(&line, &linesize, infile);
4236 if (linelen == -1) {
4237 if (ferror(infile)) {
4238 err = got_error_from_errno("getline");
4244 n = fwrite(line, 1, linelen, outfile);
4246 err = got_ferror(outfile, GOT_ERR_IO);
4251 n = fwrite(line, 1, linelen, rejectfile);
4253 err = got_ferror(outfile, GOT_ERR_IO);
4260 static const struct got_error *
4261 skip_one_line(FILE *f)
4264 size_t linesize = 0;
4267 linelen = getline(&line, &linesize, f);
4268 if (linelen == -1) {
4270 return got_error_from_errno("getline");
4277 static const struct got_error *
4278 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4279 int start_old, int end_old, int start_new, int end_new,
4280 FILE *outfile, FILE *rejectfile)
4282 const struct got_error *err;
4284 /* Copy old file's lines leading up to patch. */
4285 while (!feof(f1) && *line_cur1 < start_old) {
4286 err = copy_one_line(f1, outfile, NULL);
4291 /* Skip new file's lines leading up to patch. */
4292 while (!feof(f2) && *line_cur2 < start_new) {
4294 err = copy_one_line(f2, NULL, rejectfile);
4296 err = skip_one_line(f2);
4301 /* Copy patched lines. */
4302 while (!feof(f2) && *line_cur2 <= end_new) {
4303 err = copy_one_line(f2, outfile, NULL);
4308 /* Skip over old file's replaced lines. */
4309 while (!feof(f1) && *line_cur1 <= end_old) {
4311 err = copy_one_line(f1, NULL, rejectfile);
4313 err = skip_one_line(f1);
4322 static const struct got_error *
4323 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4324 FILE *outfile, FILE *rejectfile)
4326 const struct got_error *err;
4329 /* Copy old file's lines until EOF. */
4331 err = copy_one_line(f1, outfile, NULL);
4338 /* Copy new file's lines until EOF. */
4340 err = copy_one_line(f2, NULL, rejectfile);
4350 static const struct got_error *
4351 apply_or_reject_change(int *choice, int *nchunks_used,
4352 struct diff_result *diff_result, int n,
4353 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4354 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4355 got_worktree_patch_cb patch_cb, void *patch_arg)
4357 const struct got_error *err = NULL;
4358 struct diff_chunk_context cc = {};
4359 int start_old, end_old, start_new, end_new;
4361 struct diff_output_unidiff_state *diff_state;
4362 struct diff_input_info diff_info;
4365 *choice = GOT_PATCH_CHOICE_NONE;
4367 /* Get changed line numbers without context lines for copy_change(). */
4368 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4369 start_old = cc.left.start;
4370 end_old = cc.left.end;
4371 start_new = cc.right.start;
4372 end_new = cc.right.end;
4374 /* Get the same change with context lines for display. */
4375 memset(&cc, 0, sizeof(cc));
4376 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4378 memset(&diff_info, 0, sizeof(diff_info));
4379 diff_info.left_path = relpath;
4380 diff_info.right_path = relpath;
4382 diff_state = diff_output_unidiff_state_alloc();
4383 if (diff_state == NULL)
4384 return got_error_set_errno(ENOMEM,
4385 "diff_output_unidiff_state_alloc");
4387 hunkfile = got_opentemp();
4388 if (hunkfile == NULL) {
4389 err = got_error_from_errno("got_opentemp");
4393 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4395 if (rc != DIFF_RC_OK) {
4396 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4400 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4401 err = got_ferror(hunkfile, GOT_ERR_IO);
4405 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4406 hunkfile, changeno, nchanges);
4411 case GOT_PATCH_CHOICE_YES:
4412 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4413 end_old, start_new, end_new, outfile, rejectfile);
4415 case GOT_PATCH_CHOICE_NO:
4416 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4417 end_old, start_new, end_new, rejectfile, outfile);
4419 case GOT_PATCH_CHOICE_QUIT:
4422 err = got_error(GOT_ERR_PATCH_CHOICE);
4426 diff_output_unidiff_state_free(diff_state);
4427 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4428 err = got_error_from_errno("fclose");
4432 struct revert_file_args {
4433 struct got_worktree *worktree;
4434 struct got_fileindex *fileindex;
4435 got_worktree_checkout_cb progress_cb;
4437 got_worktree_patch_cb patch_cb;
4439 struct got_repository *repo;
4440 int unlink_added_files;
4443 static const struct got_error *
4444 create_patched_content(char **path_outfile, int reverse_patch,
4445 struct got_object_id *blob_id, const char *path2,
4446 int dirfd2, const char *de_name2,
4447 const char *relpath, struct got_repository *repo,
4448 got_worktree_patch_cb patch_cb, void *patch_arg)
4450 const struct got_error *err, *free_err;
4451 struct got_blob_object *blob = NULL;
4452 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4453 int fd = -1, fd2 = -1;
4454 char link_target[PATH_MAX];
4455 ssize_t link_len = 0;
4456 char *path1 = NULL, *id_str = NULL;
4458 struct got_diffreg_result *diffreg_result = NULL;
4459 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4460 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4462 *path_outfile = NULL;
4464 err = got_object_id_str(&id_str, blob_id);
4469 fd2 = openat(dirfd2, de_name2,
4470 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4472 if (!got_err_open_nofollow_on_symlink()) {
4473 err = got_error_from_errno2("openat", path2);
4476 link_len = readlinkat(dirfd2, de_name2,
4477 link_target, sizeof(link_target));
4478 if (link_len == -1) {
4479 return got_error_from_errno2("readlinkat",
4482 sb2.st_mode = S_IFLNK;
4483 sb2.st_size = link_len;
4486 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4488 if (!got_err_open_nofollow_on_symlink()) {
4489 err = got_error_from_errno2("open", path2);
4492 link_len = readlink(path2, link_target,
4493 sizeof(link_target));
4495 return got_error_from_errno2("readlink", path2);
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4501 if (fstat(fd2, &sb2) == -1) {
4502 err = got_error_from_errno2("fstat", path2);
4506 f2 = fdopen(fd2, "r");
4508 err = got_error_from_errno2("fdopen", path2);
4514 f2 = got_opentemp();
4516 err = got_error_from_errno2("got_opentemp", path2);
4519 n = fwrite(link_target, 1, link_len, f2);
4520 if (n != link_len) {
4521 err = got_ferror(f2, GOT_ERR_IO);
4524 if (fflush(f2) == EOF) {
4525 err = got_error_from_errno("fflush");
4531 fd = got_opentempfd();
4533 err = got_error_from_errno("got_opentempfd");
4537 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4541 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4545 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4549 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4550 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4554 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4558 if (fseek(f1, 0L, SEEK_SET) == -1)
4559 return got_ferror(f1, GOT_ERR_IO);
4560 if (fseek(f2, 0L, SEEK_SET) == -1)
4561 return got_ferror(f2, GOT_ERR_IO);
4563 /* Count the number of actual changes in the diff result. */
4564 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4565 struct diff_chunk_context cc = {};
4566 diff_chunk_context_load_change(&cc, &nchunks_used,
4567 diffreg_result->result, n, 0);
4570 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4572 err = apply_or_reject_change(&choice, &nchunks_used,
4573 diffreg_result->result, n, relpath, f1, f2,
4574 &line_cur1, &line_cur2,
4575 reverse_patch ? NULL : outfile,
4576 reverse_patch ? outfile : NULL,
4577 ++i, nchanges, patch_cb, patch_arg);
4580 if (choice == GOT_PATCH_CHOICE_YES)
4582 else if (choice == GOT_PATCH_CHOICE_QUIT)
4586 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4587 reverse_patch ? NULL : outfile,
4588 reverse_patch ? outfile : NULL);
4592 if (!S_ISLNK(sb2.st_mode)) {
4593 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4594 err = got_error_from_errno2("fchmod", path2);
4601 if (fd != -1 && close(fd) == -1 && err == NULL)
4602 err = got_error_from_errno("close");
4604 got_object_blob_close(blob);
4605 free_err = got_diffreg_result_free(diffreg_result);
4608 if (f1 && fclose(f1) == EOF && err == NULL)
4609 err = got_error_from_errno2("fclose", path1);
4610 if (f2 && fclose(f2) == EOF && err == NULL)
4611 err = got_error_from_errno2("fclose", path2);
4612 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4613 err = got_error_from_errno2("close", path2);
4614 if (outfile && fclose(outfile) == EOF && err == NULL)
4615 err = got_error_from_errno2("fclose", *path_outfile);
4616 if (path1 && unlink(path1) == -1 && err == NULL)
4617 err = got_error_from_errno2("unlink", path1);
4618 if (err || !have_content) {
4619 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4620 err = got_error_from_errno2("unlink", *path_outfile);
4621 free(*path_outfile);
4622 *path_outfile = NULL;
4628 static const struct got_error *
4629 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4630 const char *relpath, struct got_object_id *blob_id,
4631 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4632 int dirfd, const char *de_name)
4634 struct revert_file_args *a = arg;
4635 const struct got_error *err = NULL;
4636 char *parent_path = NULL;
4637 struct got_fileindex_entry *ie;
4638 struct got_commit_object *base_commit = NULL;
4639 struct got_tree_object *tree = NULL;
4640 struct got_object_id *tree_id = NULL;
4641 const struct got_tree_entry *te = NULL;
4642 char *tree_path = NULL, *te_name;
4643 char *ondisk_path = NULL, *path_content = NULL;
4644 struct got_blob_object *blob = NULL;
4647 /* Reverting a staged deletion is a no-op. */
4648 if (status == GOT_STATUS_DELETE &&
4649 staged_status != GOT_STATUS_NO_CHANGE)
4652 if (status == GOT_STATUS_UNVERSIONED)
4653 return (*a->progress_cb)(a->progress_arg,
4654 GOT_STATUS_UNVERSIONED, relpath);
4656 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4658 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4660 /* Construct in-repository path of tree which contains this blob. */
4661 err = got_path_dirname(&parent_path, ie->path);
4663 if (err->code != GOT_ERR_BAD_PATH)
4665 parent_path = strdup("/");
4666 if (parent_path == NULL) {
4667 err = got_error_from_errno("strdup");
4671 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4672 tree_path = strdup(parent_path);
4673 if (tree_path == NULL) {
4674 err = got_error_from_errno("strdup");
4678 if (got_path_is_root_dir(parent_path)) {
4679 tree_path = strdup(a->worktree->path_prefix);
4680 if (tree_path == NULL) {
4681 err = got_error_from_errno("strdup");
4685 if (asprintf(&tree_path, "%s/%s",
4686 a->worktree->path_prefix, parent_path) == -1) {
4687 err = got_error_from_errno("asprintf");
4693 err = got_object_open_as_commit(&base_commit, a->repo,
4694 a->worktree->base_commit_id);
4698 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4700 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4701 (status == GOT_STATUS_ADD ||
4702 staged_status == GOT_STATUS_ADD)))
4705 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4709 err = got_path_basename(&te_name, ie->path);
4713 te = got_object_tree_find_entry(tree, te_name);
4715 if (te == NULL && status != GOT_STATUS_ADD &&
4716 staged_status != GOT_STATUS_ADD) {
4717 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4723 case GOT_STATUS_ADD:
4725 int choice = GOT_PATCH_CHOICE_NONE;
4726 err = (*a->patch_cb)(&choice, a->patch_arg,
4727 status, ie->path, NULL, 1, 1);
4730 if (choice != GOT_PATCH_CHOICE_YES)
4733 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4737 got_fileindex_entry_remove(a->fileindex, ie);
4738 if (a->unlink_added_files) {
4739 if (asprintf(&ondisk_path, "%s/%s",
4740 got_worktree_get_root_path(a->worktree),
4742 err = got_error_from_errno("asprintf");
4745 if (unlink(ondisk_path) == -1) {
4746 err = got_error_from_errno2("unlink",
4752 case GOT_STATUS_DELETE:
4754 int choice = GOT_PATCH_CHOICE_NONE;
4755 err = (*a->patch_cb)(&choice, a->patch_arg,
4756 status, ie->path, NULL, 1, 1);
4759 if (choice != GOT_PATCH_CHOICE_YES)
4763 case GOT_STATUS_MODIFY:
4764 case GOT_STATUS_MODE_CHANGE:
4765 case GOT_STATUS_CONFLICT:
4766 case GOT_STATUS_MISSING: {
4767 struct got_object_id id;
4768 if (staged_status == GOT_STATUS_ADD ||
4769 staged_status == GOT_STATUS_MODIFY) {
4770 memcpy(id.sha1, ie->staged_blob_sha1,
4771 SHA1_DIGEST_LENGTH);
4773 memcpy(id.sha1, ie->blob_sha1,
4774 SHA1_DIGEST_LENGTH);
4775 fd = got_opentempfd();
4777 err = got_error_from_errno("got_opentempfd");
4781 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4785 if (asprintf(&ondisk_path, "%s/%s",
4786 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4787 err = got_error_from_errno("asprintf");
4791 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4792 status == GOT_STATUS_CONFLICT)) {
4793 int is_bad_symlink = 0;
4794 err = create_patched_content(&path_content, 1, &id,
4795 ondisk_path, dirfd, de_name, ie->path, a->repo,
4796 a->patch_cb, a->patch_arg);
4797 if (err || path_content == NULL)
4799 if (te && S_ISLNK(te->mode)) {
4800 if (unlink(path_content) == -1) {
4801 err = got_error_from_errno2("unlink",
4805 err = install_symlink(&is_bad_symlink,
4806 a->worktree, ondisk_path, ie->path,
4807 blob, 0, 1, 0, 0, a->repo,
4808 a->progress_cb, a->progress_arg);
4810 if (rename(path_content, ondisk_path) == -1) {
4811 err = got_error_from_errno3("rename",
4812 path_content, ondisk_path);
4817 int is_bad_symlink = 0;
4818 if (te && S_ISLNK(te->mode)) {
4819 err = install_symlink(&is_bad_symlink,
4820 a->worktree, ondisk_path, ie->path,
4821 blob, 0, 1, 0, 0, a->repo,
4822 a->progress_cb, a->progress_arg);
4824 err = install_blob(a->worktree, ondisk_path,
4826 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4827 got_fileindex_perms_to_st(ie), blob,
4828 0, 1, 0, 0, a->repo,
4829 a->progress_cb, a->progress_arg);
4833 if (status == GOT_STATUS_DELETE ||
4834 status == GOT_STATUS_MODE_CHANGE) {
4835 err = got_fileindex_entry_update(ie,
4836 a->worktree->root_fd, relpath,
4838 a->worktree->base_commit_id->sha1, 1);
4842 if (is_bad_symlink) {
4843 got_fileindex_entry_filetype_set(ie,
4844 GOT_FILEIDX_MODE_BAD_SYMLINK);
4857 if (fd != -1 && close(fd) == -1 && err == NULL)
4858 err = got_error_from_errno("close");
4860 got_object_blob_close(blob);
4862 got_object_tree_close(tree);
4865 got_object_commit_close(base_commit);
4869 const struct got_error *
4870 got_worktree_revert(struct got_worktree *worktree,
4871 struct got_pathlist_head *paths,
4872 got_worktree_checkout_cb progress_cb, void *progress_arg,
4873 got_worktree_patch_cb patch_cb, void *patch_arg,
4874 struct got_repository *repo)
4876 struct got_fileindex *fileindex = NULL;
4877 char *fileindex_path = NULL;
4878 const struct got_error *err = NULL, *unlockerr = NULL;
4879 const struct got_error *sync_err = NULL;
4880 struct got_pathlist_entry *pe;
4881 struct revert_file_args rfa;
4883 err = lock_worktree(worktree, LOCK_EX);
4887 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4891 rfa.worktree = worktree;
4892 rfa.fileindex = fileindex;
4893 rfa.progress_cb = progress_cb;
4894 rfa.progress_arg = progress_arg;
4895 rfa.patch_cb = patch_cb;
4896 rfa.patch_arg = patch_arg;
4898 rfa.unlink_added_files = 0;
4899 TAILQ_FOREACH(pe, paths, entry) {
4900 err = worktree_status(worktree, pe->path, fileindex, repo,
4901 revert_file, &rfa, NULL, NULL, 1, 0);
4905 sync_err = sync_fileindex(fileindex, fileindex_path);
4906 if (sync_err && err == NULL)
4909 free(fileindex_path);
4911 got_fileindex_free(fileindex);
4912 unlockerr = lock_worktree(worktree, LOCK_SH);
4913 if (unlockerr && err == NULL)
4919 free_commitable(struct got_commitable *ct)
4922 free(ct->in_repo_path);
4923 free(ct->ondisk_path);
4925 free(ct->base_blob_id);
4926 free(ct->staged_blob_id);
4927 free(ct->base_commit_id);
4931 struct collect_commitables_arg {
4932 struct got_pathlist_head *commitable_paths;
4933 struct got_repository *repo;
4934 struct got_worktree *worktree;
4935 struct got_fileindex *fileindex;
4936 int have_staged_files;
4937 int allow_bad_symlinks;
4940 static const struct got_error *
4941 collect_commitables(void *arg, unsigned char status,
4942 unsigned char staged_status, const char *relpath,
4943 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4944 struct got_object_id *commit_id, int dirfd, const char *de_name)
4946 struct collect_commitables_arg *a = arg;
4947 const struct got_error *err = NULL;
4948 struct got_commitable *ct = NULL;
4949 struct got_pathlist_entry *new = NULL;
4950 char *parent_path = NULL, *path = NULL;
4953 if (a->have_staged_files) {
4954 if (staged_status != GOT_STATUS_MODIFY &&
4955 staged_status != GOT_STATUS_ADD &&
4956 staged_status != GOT_STATUS_DELETE)
4959 if (status == GOT_STATUS_CONFLICT)
4960 return got_error(GOT_ERR_COMMIT_CONFLICT);
4962 if (status != GOT_STATUS_MODIFY &&
4963 status != GOT_STATUS_MODE_CHANGE &&
4964 status != GOT_STATUS_ADD &&
4965 status != GOT_STATUS_DELETE)
4969 if (asprintf(&path, "/%s", relpath) == -1) {
4970 err = got_error_from_errno("asprintf");
4973 if (strcmp(path, "/") == 0) {
4974 parent_path = strdup("");
4975 if (parent_path == NULL)
4976 return got_error_from_errno("strdup");
4978 err = got_path_dirname(&parent_path, path);
4983 ct = calloc(1, sizeof(*ct));
4985 err = got_error_from_errno("calloc");
4989 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4991 err = got_error_from_errno("asprintf");
4995 if (staged_status == GOT_STATUS_ADD ||
4996 staged_status == GOT_STATUS_MODIFY) {
4997 struct got_fileindex_entry *ie;
4998 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4999 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5000 case GOT_FILEIDX_MODE_REGULAR_FILE:
5001 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5004 case GOT_FILEIDX_MODE_SYMLINK:
5008 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5011 ct->mode |= got_fileindex_entry_perms_get(ie);
5012 } else if (status != GOT_STATUS_DELETE &&
5013 staged_status != GOT_STATUS_DELETE) {
5015 if (fstatat(dirfd, de_name, &sb,
5016 AT_SYMLINK_NOFOLLOW) == -1) {
5017 err = got_error_from_errno2("fstatat",
5021 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5022 err = got_error_from_errno2("lstat", ct->ondisk_path);
5025 ct->mode = sb.st_mode;
5028 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5029 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5031 err = got_error_from_errno("asprintf");
5035 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5036 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5038 char target_path[PATH_MAX];
5040 target_len = readlink(ct->ondisk_path, target_path,
5041 sizeof(target_path));
5042 if (target_len == -1) {
5043 err = got_error_from_errno2("readlink",
5047 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5048 target_len, ct->ondisk_path, a->worktree->root_path);
5051 if (is_bad_symlink) {
5052 err = got_error_path(ct->ondisk_path,
5053 GOT_ERR_BAD_SYMLINK);
5059 ct->status = status;
5060 ct->staged_status = staged_status;
5061 ct->blob_id = NULL; /* will be filled in when blob gets created */
5062 if (ct->status != GOT_STATUS_ADD &&
5063 ct->staged_status != GOT_STATUS_ADD) {
5064 ct->base_blob_id = got_object_id_dup(blob_id);
5065 if (ct->base_blob_id == NULL) {
5066 err = got_error_from_errno("got_object_id_dup");
5069 ct->base_commit_id = got_object_id_dup(commit_id);
5070 if (ct->base_commit_id == NULL) {
5071 err = got_error_from_errno("got_object_id_dup");
5075 if (ct->staged_status == GOT_STATUS_ADD ||
5076 ct->staged_status == GOT_STATUS_MODIFY) {
5077 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5078 if (ct->staged_blob_id == NULL) {
5079 err = got_error_from_errno("got_object_id_dup");
5083 ct->path = strdup(path);
5084 if (ct->path == NULL) {
5085 err = got_error_from_errno("strdup");
5088 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5090 if (ct && (err || new == NULL))
5091 free_commitable(ct);
5097 static const struct got_error *write_tree(struct got_object_id **, int *,
5098 struct got_tree_object *, const char *, struct got_pathlist_head *,
5099 got_worktree_status_cb status_cb, void *status_arg,
5100 struct got_repository *);
5102 static const struct got_error *
5103 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5104 struct got_tree_entry *te, const char *parent_path,
5105 struct got_pathlist_head *commitable_paths,
5106 got_worktree_status_cb status_cb, void *status_arg,
5107 struct got_repository *repo)
5109 const struct got_error *err = NULL;
5110 struct got_tree_object *subtree;
5113 if (asprintf(&subpath, "%s%s%s", parent_path,
5114 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5115 return got_error_from_errno("asprintf");
5117 err = got_object_open_as_tree(&subtree, repo, &te->id);
5121 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5122 commitable_paths, status_cb, status_arg, repo);
5123 got_object_tree_close(subtree);
5128 static const struct got_error *
5129 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5131 const struct got_error *err = NULL;
5132 char *ct_parent_path = NULL;
5136 if (strchr(ct->in_repo_path, '/') == NULL) {
5137 *match = got_path_is_root_dir(path);
5141 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5144 *match = (strcmp(path, ct_parent_path) == 0);
5145 free(ct_parent_path);
5150 get_ct_file_mode(struct got_commitable *ct)
5152 if (S_ISLNK(ct->mode))
5155 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5158 static const struct got_error *
5159 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5160 struct got_tree_entry *te, struct got_commitable *ct)
5162 const struct got_error *err = NULL;
5166 err = got_object_tree_entry_dup(new_te, te);
5170 (*new_te)->mode = get_ct_file_mode(ct);
5172 if (ct->staged_status == GOT_STATUS_MODIFY)
5173 memcpy(&(*new_te)->id, ct->staged_blob_id,
5174 sizeof((*new_te)->id));
5176 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5178 if (err && *new_te) {
5185 static const struct got_error *
5186 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5187 struct got_commitable *ct)
5189 const struct got_error *err = NULL;
5190 char *ct_name = NULL;
5194 *new_te = calloc(1, sizeof(**new_te));
5195 if (*new_te == NULL)
5196 return got_error_from_errno("calloc");
5198 err = got_path_basename(&ct_name, ct->path);
5201 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5202 sizeof((*new_te)->name)) {
5203 err = got_error(GOT_ERR_NO_SPACE);
5207 (*new_te)->mode = get_ct_file_mode(ct);
5209 if (ct->staged_status == GOT_STATUS_ADD)
5210 memcpy(&(*new_te)->id, ct->staged_blob_id,
5211 sizeof((*new_te)->id));
5213 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5216 if (err && *new_te) {
5223 static const struct got_error *
5224 insert_tree_entry(struct got_tree_entry *new_te,
5225 struct got_pathlist_head *paths)
5227 const struct got_error *err = NULL;
5228 struct got_pathlist_entry *new_pe;
5230 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5234 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5238 static const struct got_error *
5239 report_ct_status(struct got_commitable *ct,
5240 got_worktree_status_cb status_cb, void *status_arg)
5242 const char *ct_path = ct->path;
5243 unsigned char status;
5245 if (status_cb == NULL) /* no commit progress output desired */
5248 while (ct_path[0] == '/')
5251 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5252 status = ct->staged_status;
5254 status = ct->status;
5256 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5257 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5260 static const struct got_error *
5261 match_modified_subtree(int *modified, struct got_tree_entry *te,
5262 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5264 const struct got_error *err = NULL;
5265 struct got_pathlist_entry *pe;
5270 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5271 got_path_is_root_dir(base_tree_path) ? "" : "/",
5273 return got_error_from_errno("asprintf");
5275 TAILQ_FOREACH(pe, commitable_paths, entry) {
5276 struct got_commitable *ct = pe->data;
5277 *modified = got_path_is_child(ct->in_repo_path, te_path,
5287 static const struct got_error *
5288 match_deleted_or_modified_ct(struct got_commitable **ctp,
5289 struct got_tree_entry *te, const char *base_tree_path,
5290 struct got_pathlist_head *commitable_paths)
5292 const struct got_error *err = NULL;
5293 struct got_pathlist_entry *pe;
5297 TAILQ_FOREACH(pe, commitable_paths, entry) {
5298 struct got_commitable *ct = pe->data;
5299 char *ct_name = NULL;
5302 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5303 if (ct->status != GOT_STATUS_MODIFY &&
5304 ct->status != GOT_STATUS_MODE_CHANGE &&
5305 ct->status != GOT_STATUS_DELETE)
5308 if (ct->staged_status != GOT_STATUS_MODIFY &&
5309 ct->staged_status != GOT_STATUS_DELETE)
5313 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5316 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5322 err = got_path_basename(&ct_name, pe->path);
5326 if (strcmp(te->name, ct_name) != 0) {
5339 static const struct got_error *
5340 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5341 const char *child_path, const char *path_base_tree,
5342 struct got_pathlist_head *commitable_paths,
5343 got_worktree_status_cb status_cb, void *status_arg,
5344 struct got_repository *repo)
5346 const struct got_error *err = NULL;
5347 struct got_tree_entry *new_te;
5349 struct got_object_id *id = NULL;
5354 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5355 got_path_is_root_dir(path_base_tree) ? "" : "/",
5357 return got_error_from_errno("asprintf");
5359 new_te = calloc(1, sizeof(*new_te));
5361 return got_error_from_errno("calloc");
5362 new_te->mode = S_IFDIR;
5364 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5365 sizeof(new_te->name)) {
5366 err = got_error(GOT_ERR_NO_SPACE);
5369 err = write_tree(&id, &nentries, NULL, subtree_path,
5370 commitable_paths, status_cb, status_arg, repo);
5375 memcpy(&new_te->id, id, sizeof(new_te->id));
5384 static const struct got_error *
5385 write_tree(struct got_object_id **new_tree_id, int *nentries,
5386 struct got_tree_object *base_tree, const char *path_base_tree,
5387 struct got_pathlist_head *commitable_paths,
5388 got_worktree_status_cb status_cb, void *status_arg,
5389 struct got_repository *repo)
5391 const struct got_error *err = NULL;
5392 struct got_pathlist_head paths;
5393 struct got_tree_entry *te, *new_te = NULL;
5394 struct got_pathlist_entry *pe;
5399 /* Insert, and recurse into, newly added entries first. */
5400 TAILQ_FOREACH(pe, commitable_paths, entry) {
5401 struct got_commitable *ct = pe->data;
5402 char *child_path = NULL, *slash;
5404 if ((ct->status != GOT_STATUS_ADD &&
5405 ct->staged_status != GOT_STATUS_ADD) ||
5406 (ct->flags & GOT_COMMITABLE_ADDED))
5409 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5410 strlen(path_base_tree)))
5413 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5418 slash = strchr(child_path, '/');
5419 if (slash == NULL) {
5420 err = alloc_added_blob_tree_entry(&new_te, ct);
5423 err = report_ct_status(ct, status_cb, status_arg);
5426 ct->flags |= GOT_COMMITABLE_ADDED;
5427 err = insert_tree_entry(new_te, &paths);
5432 *slash = '\0'; /* trim trailing path components */
5433 if (base_tree == NULL ||
5434 got_object_tree_find_entry(base_tree, child_path)
5436 err = make_subtree_for_added_blob(&new_te,
5437 child_path, path_base_tree,
5438 commitable_paths, status_cb, status_arg,
5442 err = insert_tree_entry(new_te, &paths);
5451 int i, nbase_entries;
5452 /* Handle modified and deleted entries. */
5453 nbase_entries = got_object_tree_get_nentries(base_tree);
5454 for (i = 0; i < nbase_entries; i++) {
5455 struct got_commitable *ct = NULL;
5457 te = got_object_tree_get_entry(base_tree, i);
5458 if (got_object_tree_entry_is_submodule(te)) {
5459 /* Entry is a submodule; just copy it. */
5460 err = got_object_tree_entry_dup(&new_te, te);
5463 err = insert_tree_entry(new_te, &paths);
5470 if (S_ISDIR(te->mode)) {
5472 err = got_object_tree_entry_dup(&new_te, te);
5475 err = match_modified_subtree(&modified, te,
5476 path_base_tree, commitable_paths);
5479 /* Avoid recursion into unmodified subtrees. */
5481 struct got_object_id *new_id;
5483 err = write_subtree(&new_id,
5485 path_base_tree, commitable_paths,
5486 status_cb, status_arg, repo);
5489 if (nsubentries == 0) {
5490 /* All entries were deleted. */
5494 memcpy(&new_te->id, new_id,
5495 sizeof(new_te->id));
5498 err = insert_tree_entry(new_te, &paths);
5505 err = match_deleted_or_modified_ct(&ct, te,
5506 path_base_tree, commitable_paths);
5510 /* NB: Deleted entries get dropped here. */
5511 if (ct->status == GOT_STATUS_MODIFY ||
5512 ct->status == GOT_STATUS_MODE_CHANGE ||
5513 ct->staged_status == GOT_STATUS_MODIFY) {
5514 err = alloc_modified_blob_tree_entry(
5518 err = insert_tree_entry(new_te, &paths);
5523 err = report_ct_status(ct, status_cb,
5528 /* Entry is unchanged; just copy it. */
5529 err = got_object_tree_entry_dup(&new_te, te);
5532 err = insert_tree_entry(new_te, &paths);
5540 /* Write new list of entries; deleted entries have been dropped. */
5541 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5543 got_pathlist_free(&paths);
5547 static const struct got_error *
5548 update_fileindex_after_commit(struct got_worktree *worktree,
5549 struct got_pathlist_head *commitable_paths,
5550 struct got_object_id *new_base_commit_id,
5551 struct got_fileindex *fileindex, int have_staged_files)
5553 const struct got_error *err = NULL;
5554 struct got_pathlist_entry *pe;
5555 char *relpath = NULL;
5557 TAILQ_FOREACH(pe, commitable_paths, entry) {
5558 struct got_fileindex_entry *ie;
5559 struct got_commitable *ct = pe->data;
5561 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5563 err = got_path_skip_common_ancestor(&relpath,
5564 worktree->root_path, ct->ondisk_path);
5569 if (ct->status == GOT_STATUS_DELETE ||
5570 ct->staged_status == GOT_STATUS_DELETE) {
5571 got_fileindex_entry_remove(fileindex, ie);
5572 } else if (ct->staged_status == GOT_STATUS_ADD ||
5573 ct->staged_status == GOT_STATUS_MODIFY) {
5574 got_fileindex_entry_stage_set(ie,
5575 GOT_FILEIDX_STAGE_NONE);
5576 got_fileindex_entry_staged_filetype_set(ie, 0);
5578 err = got_fileindex_entry_update(ie,
5579 worktree->root_fd, relpath,
5580 ct->staged_blob_id->sha1,
5581 new_base_commit_id->sha1,
5582 !have_staged_files);
5584 err = got_fileindex_entry_update(ie,
5585 worktree->root_fd, relpath,
5587 new_base_commit_id->sha1,
5588 !have_staged_files);
5590 err = got_fileindex_entry_alloc(&ie, pe->path);
5593 err = got_fileindex_entry_update(ie,
5594 worktree->root_fd, relpath, ct->blob_id->sha1,
5595 new_base_commit_id->sha1, 1);
5597 got_fileindex_entry_free(ie);
5600 err = got_fileindex_entry_add(fileindex, ie);
5602 got_fileindex_entry_free(ie);
5615 static const struct got_error *
5616 check_out_of_date(const char *in_repo_path, unsigned char status,
5617 unsigned char staged_status, struct got_object_id *base_blob_id,
5618 struct got_object_id *base_commit_id,
5619 struct got_object_id *head_commit_id, struct got_repository *repo,
5622 const struct got_error *err = NULL;
5623 struct got_commit_object *commit = NULL;
5624 struct got_object_id *id = NULL;
5626 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5627 /* Trivial case: base commit == head commit */
5628 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5631 * Ensure file content which local changes were based
5632 * on matches file content in the branch head.
5634 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5637 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5639 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5640 err = got_error(ood_errcode);
5642 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5643 err = got_error(ood_errcode);
5645 /* Require that added files don't exist in the branch head. */
5646 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5649 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5650 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5652 err = id ? got_error(ood_errcode) : NULL;
5657 got_object_commit_close(commit);
5661 static const struct got_error *
5662 commit_worktree(struct got_object_id **new_commit_id,
5663 struct got_pathlist_head *commitable_paths,
5664 struct got_object_id *head_commit_id,
5665 struct got_object_id *parent_id2,
5666 struct got_worktree *worktree,
5667 const char *author, const char *committer,
5668 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5669 got_worktree_status_cb status_cb, void *status_arg,
5670 struct got_repository *repo)
5672 const struct got_error *err = NULL, *unlockerr = NULL;
5673 struct got_pathlist_entry *pe;
5674 const char *head_ref_name = NULL;
5675 struct got_commit_object *head_commit = NULL;
5676 struct got_reference *head_ref2 = NULL;
5677 struct got_object_id *head_commit_id2 = NULL;
5678 struct got_tree_object *head_tree = NULL;
5679 struct got_object_id *new_tree_id = NULL;
5680 int nentries, nparents = 0;
5681 struct got_object_id_queue parent_ids;
5682 struct got_object_qid *pid = NULL;
5683 char *logmsg = NULL;
5686 *new_commit_id = NULL;
5688 STAILQ_INIT(&parent_ids);
5690 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5694 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5698 if (commit_msg_cb != NULL) {
5699 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5704 if (logmsg == NULL || strlen(logmsg) == 0) {
5705 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5709 /* Create blobs from added and modified files and record their IDs. */
5710 TAILQ_FOREACH(pe, commitable_paths, entry) {
5711 struct got_commitable *ct = pe->data;
5714 /* Blobs for staged files already exist. */
5715 if (ct->staged_status == GOT_STATUS_ADD ||
5716 ct->staged_status == GOT_STATUS_MODIFY)
5719 if (ct->status != GOT_STATUS_ADD &&
5720 ct->status != GOT_STATUS_MODIFY &&
5721 ct->status != GOT_STATUS_MODE_CHANGE)
5724 if (asprintf(&ondisk_path, "%s/%s",
5725 worktree->root_path, pe->path) == -1) {
5726 err = got_error_from_errno("asprintf");
5729 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5735 /* Recursively write new tree objects. */
5736 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5737 commitable_paths, status_cb, status_arg, repo);
5741 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5744 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5747 err = got_object_qid_alloc(&pid, parent_id2);
5750 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5753 timestamp = time(NULL);
5754 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5755 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5761 /* Check if a concurrent commit to our branch has occurred. */
5762 head_ref_name = got_worktree_get_head_ref_name(worktree);
5763 if (head_ref_name == NULL) {
5764 err = got_error_from_errno("got_worktree_get_head_ref_name");
5767 /* Lock the reference here to prevent concurrent modification. */
5768 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5771 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5774 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5775 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5778 /* Update branch head in repository. */
5779 err = got_ref_change_ref(head_ref2, *new_commit_id);
5782 err = got_ref_write(head_ref2, repo);
5786 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5790 err = ref_base_commit(worktree, repo);
5794 got_object_id_queue_free(&parent_ids);
5796 got_object_tree_close(head_tree);
5798 got_object_commit_close(head_commit);
5799 free(head_commit_id2);
5801 unlockerr = got_ref_unlock(head_ref2);
5802 if (unlockerr && err == NULL)
5804 got_ref_close(head_ref2);
5809 static const struct got_error *
5810 check_path_is_commitable(const char *path,
5811 struct got_pathlist_head *commitable_paths)
5813 struct got_pathlist_entry *cpe = NULL;
5814 size_t path_len = strlen(path);
5816 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5817 struct got_commitable *ct = cpe->data;
5818 const char *ct_path = ct->path;
5820 while (ct_path[0] == '/')
5823 if (strcmp(path, ct_path) == 0 ||
5824 got_path_is_child(ct_path, path, path_len))
5829 return got_error_path(path, GOT_ERR_BAD_PATH);
5834 static const struct got_error *
5835 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5837 int *have_staged_files = arg;
5839 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5840 *have_staged_files = 1;
5841 return got_error(GOT_ERR_CANCELLED);
5847 static const struct got_error *
5848 check_non_staged_files(struct got_fileindex *fileindex,
5849 struct got_pathlist_head *paths)
5851 struct got_pathlist_entry *pe;
5852 struct got_fileindex_entry *ie;
5854 TAILQ_FOREACH(pe, paths, entry) {
5855 if (pe->path[0] == '\0')
5857 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5859 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5860 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5861 return got_error_path(pe->path,
5862 GOT_ERR_FILE_NOT_STAGED);
5868 const struct got_error *
5869 got_worktree_commit(struct got_object_id **new_commit_id,
5870 struct got_worktree *worktree, struct got_pathlist_head *paths,
5871 const char *author, const char *committer, int allow_bad_symlinks,
5872 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5873 got_worktree_status_cb status_cb, void *status_arg,
5874 struct got_repository *repo)
5876 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5877 struct got_fileindex *fileindex = NULL;
5878 char *fileindex_path = NULL;
5879 struct got_pathlist_head commitable_paths;
5880 struct collect_commitables_arg cc_arg;
5881 struct got_pathlist_entry *pe;
5882 struct got_reference *head_ref = NULL;
5883 struct got_object_id *head_commit_id = NULL;
5884 int have_staged_files = 0;
5886 *new_commit_id = NULL;
5888 TAILQ_INIT(&commitable_paths);
5890 err = lock_worktree(worktree, LOCK_EX);
5894 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5898 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5902 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5906 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5907 &have_staged_files);
5908 if (err && err->code != GOT_ERR_CANCELLED)
5910 if (have_staged_files) {
5911 err = check_non_staged_files(fileindex, paths);
5916 cc_arg.commitable_paths = &commitable_paths;
5917 cc_arg.worktree = worktree;
5918 cc_arg.fileindex = fileindex;
5920 cc_arg.have_staged_files = have_staged_files;
5921 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5922 TAILQ_FOREACH(pe, paths, entry) {
5923 err = worktree_status(worktree, pe->path, fileindex, repo,
5924 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5929 if (TAILQ_EMPTY(&commitable_paths)) {
5930 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5934 TAILQ_FOREACH(pe, paths, entry) {
5935 err = check_path_is_commitable(pe->path, &commitable_paths);
5940 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5941 struct got_commitable *ct = pe->data;
5942 const char *ct_path = ct->in_repo_path;
5944 while (ct_path[0] == '/')
5946 err = check_out_of_date(ct_path, ct->status,
5947 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5948 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5954 err = commit_worktree(new_commit_id, &commitable_paths,
5955 head_commit_id, NULL, worktree, author, committer,
5956 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5960 err = update_fileindex_after_commit(worktree, &commitable_paths,
5961 *new_commit_id, fileindex, have_staged_files);
5962 sync_err = sync_fileindex(fileindex, fileindex_path);
5963 if (sync_err && err == NULL)
5967 got_fileindex_free(fileindex);
5968 free(fileindex_path);
5969 unlockerr = lock_worktree(worktree, LOCK_SH);
5970 if (unlockerr && err == NULL)
5972 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5973 struct got_commitable *ct = pe->data;
5974 free_commitable(ct);
5976 got_pathlist_free(&commitable_paths);
5981 got_commitable_get_path(struct got_commitable *ct)
5987 got_commitable_get_status(struct got_commitable *ct)
5992 struct check_rebase_ok_arg {
5993 struct got_worktree *worktree;
5994 struct got_repository *repo;
5997 static const struct got_error *
5998 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6000 const struct got_error *err = NULL;
6001 struct check_rebase_ok_arg *a = arg;
6002 unsigned char status;
6006 /* Reject rebase of a work tree with mixed base commits. */
6007 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6008 SHA1_DIGEST_LENGTH))
6009 return got_error(GOT_ERR_MIXED_COMMITS);
6011 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6013 return got_error_from_errno("asprintf");
6015 /* Reject rebase of a work tree with modified or staged files. */
6016 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6021 if (status != GOT_STATUS_NO_CHANGE)
6022 return got_error(GOT_ERR_MODIFIED);
6023 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6024 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6029 const struct got_error *
6030 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6031 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6032 struct got_worktree *worktree, struct got_reference *branch,
6033 struct got_repository *repo)
6035 const struct got_error *err = NULL;
6036 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6037 char *branch_ref_name = NULL;
6038 char *fileindex_path = NULL;
6039 struct check_rebase_ok_arg ok_arg;
6040 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6041 struct got_object_id *wt_branch_tip = NULL;
6043 *new_base_branch_ref = NULL;
6047 err = lock_worktree(worktree, LOCK_EX);
6051 err = open_fileindex(fileindex, &fileindex_path, worktree);
6055 ok_arg.worktree = worktree;
6057 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6062 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6066 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6070 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6074 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6079 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6082 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6083 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6087 err = got_ref_alloc_symref(new_base_branch_ref,
6088 new_base_branch_ref_name, wt_branch);
6091 err = got_ref_write(*new_base_branch_ref, repo);
6095 /* TODO Lock original branch's ref while rebasing? */
6097 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6101 err = got_ref_write(branch_ref, repo);
6105 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6106 worktree->base_commit_id);
6109 err = got_ref_write(*tmp_branch, repo);
6113 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6117 free(fileindex_path);
6118 free(tmp_branch_name);
6119 free(new_base_branch_ref_name);
6120 free(branch_ref_name);
6122 got_ref_close(branch_ref);
6124 got_ref_close(wt_branch);
6125 free(wt_branch_tip);
6127 if (*new_base_branch_ref) {
6128 got_ref_close(*new_base_branch_ref);
6129 *new_base_branch_ref = NULL;
6132 got_ref_close(*tmp_branch);
6136 got_fileindex_free(*fileindex);
6139 lock_worktree(worktree, LOCK_SH);
6144 const struct got_error *
6145 got_worktree_rebase_continue(struct got_object_id **commit_id,
6146 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6147 struct got_reference **branch, struct got_fileindex **fileindex,
6148 struct got_worktree *worktree, struct got_repository *repo)
6150 const struct got_error *err;
6151 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6152 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6153 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6154 char *fileindex_path = NULL;
6155 int have_staged_files = 0;
6158 *new_base_branch = NULL;
6163 err = lock_worktree(worktree, LOCK_EX);
6167 err = open_fileindex(fileindex, &fileindex_path, worktree);
6171 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6172 &have_staged_files);
6173 if (err && err->code != GOT_ERR_CANCELLED)
6175 if (have_staged_files) {
6176 err = got_error(GOT_ERR_STAGED_PATHS);
6180 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6184 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6188 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6192 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6196 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6200 err = got_ref_open(branch, repo,
6201 got_ref_get_symref_target(branch_ref), 0);
6205 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6209 err = got_ref_resolve(commit_id, repo, commit_ref);
6213 err = got_ref_open(new_base_branch, repo,
6214 new_base_branch_ref_name, 0);
6218 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6222 free(commit_ref_name);
6223 free(branch_ref_name);
6224 free(fileindex_path);
6226 got_ref_close(commit_ref);
6228 got_ref_close(branch_ref);
6233 got_ref_close(*tmp_branch);
6236 if (*new_base_branch) {
6237 got_ref_close(*new_base_branch);
6238 *new_base_branch = NULL;
6241 got_ref_close(*branch);
6245 got_fileindex_free(*fileindex);
6248 lock_worktree(worktree, LOCK_SH);
6253 const struct got_error *
6254 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6256 const struct got_error *err;
6257 char *tmp_branch_name = NULL;
6259 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6263 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6264 free(tmp_branch_name);
6268 static const struct got_error *
6269 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6270 char **logmsg, void *arg)
6276 static const struct got_error *
6277 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6278 const char *path, struct got_object_id *blob_id,
6279 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6280 int dirfd, const char *de_name)
6285 struct collect_merged_paths_arg {
6286 got_worktree_checkout_cb progress_cb;
6288 struct got_pathlist_head *merged_paths;
6291 static const struct got_error *
6292 collect_merged_paths(void *arg, unsigned char status, const char *path)
6294 const struct got_error *err;
6295 struct collect_merged_paths_arg *a = arg;
6297 struct got_pathlist_entry *new;
6299 err = (*a->progress_cb)(a->progress_arg, status, path);
6303 if (status != GOT_STATUS_MERGE &&
6304 status != GOT_STATUS_ADD &&
6305 status != GOT_STATUS_DELETE &&
6306 status != GOT_STATUS_CONFLICT)
6311 return got_error_from_errno("strdup");
6313 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6314 if (err || new == NULL)
6320 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6322 struct got_pathlist_entry *pe;
6324 TAILQ_FOREACH(pe, merged_paths, entry)
6325 free((char *)pe->path);
6327 got_pathlist_free(merged_paths);
6330 static const struct got_error *
6331 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6332 int is_rebase, struct got_repository *repo)
6334 const struct got_error *err;
6335 struct got_reference *commit_ref = NULL;
6337 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6339 if (err->code != GOT_ERR_NOT_REF)
6341 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6344 err = got_ref_write(commit_ref, repo);
6347 } else if (is_rebase) {
6348 struct got_object_id *stored_id;
6351 err = got_ref_resolve(&stored_id, repo, commit_ref);
6354 cmp = got_object_id_cmp(commit_id, stored_id);
6357 err = got_error(GOT_ERR_REBASE_COMMITID);
6363 got_ref_close(commit_ref);
6367 static const struct got_error *
6368 rebase_merge_files(struct got_pathlist_head *merged_paths,
6369 const char *commit_ref_name, struct got_worktree *worktree,
6370 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6371 struct got_object_id *commit_id, struct got_repository *repo,
6372 got_worktree_checkout_cb progress_cb, void *progress_arg,
6373 got_cancel_cb cancel_cb, void *cancel_arg)
6375 const struct got_error *err;
6376 struct got_reference *commit_ref = NULL;
6377 struct collect_merged_paths_arg cmp_arg;
6378 char *fileindex_path;
6380 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6382 err = get_fileindex_path(&fileindex_path, worktree);
6386 cmp_arg.progress_cb = progress_cb;
6387 cmp_arg.progress_arg = progress_arg;
6388 cmp_arg.merged_paths = merged_paths;
6389 err = merge_files(worktree, fileindex, fileindex_path,
6390 parent_commit_id, commit_id, repo, collect_merged_paths,
6391 &cmp_arg, cancel_cb, cancel_arg);
6393 got_ref_close(commit_ref);
6397 const struct got_error *
6398 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6399 struct got_worktree *worktree, struct got_fileindex *fileindex,
6400 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6401 struct got_repository *repo,
6402 got_worktree_checkout_cb progress_cb, void *progress_arg,
6403 got_cancel_cb cancel_cb, void *cancel_arg)
6405 const struct got_error *err;
6406 char *commit_ref_name;
6408 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6412 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6416 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6417 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6418 progress_arg, cancel_cb, cancel_arg);
6420 free(commit_ref_name);
6424 const struct got_error *
6425 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6426 struct got_worktree *worktree, struct got_fileindex *fileindex,
6427 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6428 struct got_repository *repo,
6429 got_worktree_checkout_cb progress_cb, void *progress_arg,
6430 got_cancel_cb cancel_cb, void *cancel_arg)
6432 const struct got_error *err;
6433 char *commit_ref_name;
6435 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6439 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6443 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6444 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6445 progress_arg, cancel_cb, cancel_arg);
6447 free(commit_ref_name);
6451 static const struct got_error *
6452 rebase_commit(struct got_object_id **new_commit_id,
6453 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6454 struct got_worktree *worktree, struct got_fileindex *fileindex,
6455 struct got_reference *tmp_branch, const char *committer,
6456 struct got_commit_object *orig_commit, const char *new_logmsg,
6457 struct got_repository *repo)
6459 const struct got_error *err, *sync_err;
6460 struct got_pathlist_head commitable_paths;
6461 struct collect_commitables_arg cc_arg;
6462 char *fileindex_path = NULL;
6463 struct got_reference *head_ref = NULL;
6464 struct got_object_id *head_commit_id = NULL;
6465 char *logmsg = NULL;
6467 TAILQ_INIT(&commitable_paths);
6468 *new_commit_id = NULL;
6470 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6472 err = get_fileindex_path(&fileindex_path, worktree);
6476 cc_arg.commitable_paths = &commitable_paths;
6477 cc_arg.worktree = worktree;
6479 cc_arg.have_staged_files = 0;
6481 * If possible get the status of individual files directly to
6482 * avoid crawling the entire work tree once per rebased commit.
6484 * Ideally, merged_paths would contain a list of commitables
6485 * we could use so we could skip worktree_status() entirely.
6486 * However, we would then need carefully keep track of cumulative
6487 * effects of operations such as file additions and deletions
6488 * in 'got histedit -f' (folding multiple commits into one),
6489 * and this extra complexity is not really worth it.
6492 struct got_pathlist_entry *pe;
6493 TAILQ_FOREACH(pe, merged_paths, entry) {
6494 err = worktree_status(worktree, pe->path, fileindex,
6495 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6501 err = worktree_status(worktree, "", fileindex, repo,
6502 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6507 if (TAILQ_EMPTY(&commitable_paths)) {
6508 /* No-op change; commit will be elided. */
6509 err = got_ref_delete(commit_ref, repo);
6512 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6516 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6520 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6525 logmsg = strdup(new_logmsg);
6526 if (logmsg == NULL) {
6527 err = got_error_from_errno("strdup");
6531 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6536 /* NB: commit_worktree will call free(logmsg) */
6537 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6538 NULL, worktree, got_object_commit_get_author(orig_commit),
6539 committer ? committer :
6540 got_object_commit_get_committer(orig_commit),
6541 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6545 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6549 err = got_ref_delete(commit_ref, repo);
6553 err = update_fileindex_after_commit(worktree, &commitable_paths,
6554 *new_commit_id, fileindex, 0);
6555 sync_err = sync_fileindex(fileindex, fileindex_path);
6556 if (sync_err && err == NULL)
6559 free(fileindex_path);
6560 free(head_commit_id);
6562 got_ref_close(head_ref);
6564 free(*new_commit_id);
6565 *new_commit_id = NULL;
6570 const struct got_error *
6571 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6572 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6573 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6574 const char *committer, struct got_commit_object *orig_commit,
6575 struct got_object_id *orig_commit_id, struct got_repository *repo)
6577 const struct got_error *err;
6578 char *commit_ref_name;
6579 struct got_reference *commit_ref = NULL;
6580 struct got_object_id *commit_id = NULL;
6582 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6586 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6589 err = got_ref_resolve(&commit_id, repo, commit_ref);
6592 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6593 err = got_error(GOT_ERR_REBASE_COMMITID);
6597 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6598 worktree, fileindex, tmp_branch, committer, orig_commit,
6602 got_ref_close(commit_ref);
6603 free(commit_ref_name);
6608 const struct got_error *
6609 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6610 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6611 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6612 const char *committer, struct got_commit_object *orig_commit,
6613 struct got_object_id *orig_commit_id, const char *new_logmsg,
6614 struct got_repository *repo)
6616 const struct got_error *err;
6617 char *commit_ref_name;
6618 struct got_reference *commit_ref = NULL;
6620 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6624 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6628 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6629 worktree, fileindex, tmp_branch, committer, orig_commit,
6633 got_ref_close(commit_ref);
6634 free(commit_ref_name);
6638 const struct got_error *
6639 got_worktree_rebase_postpone(struct got_worktree *worktree,
6640 struct got_fileindex *fileindex)
6643 got_fileindex_free(fileindex);
6644 return lock_worktree(worktree, LOCK_SH);
6647 static const struct got_error *
6648 delete_ref(const char *name, struct got_repository *repo)
6650 const struct got_error *err;
6651 struct got_reference *ref;
6653 err = got_ref_open(&ref, repo, name, 0);
6655 if (err->code == GOT_ERR_NOT_REF)
6660 err = got_ref_delete(ref, repo);
6665 static const struct got_error *
6666 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6668 const struct got_error *err;
6669 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6670 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6672 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6675 err = delete_ref(tmp_branch_name, repo);
6679 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6682 err = delete_ref(new_base_branch_ref_name, repo);
6686 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6689 err = delete_ref(branch_ref_name, repo);
6693 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6696 err = delete_ref(commit_ref_name, repo);
6701 free(tmp_branch_name);
6702 free(new_base_branch_ref_name);
6703 free(branch_ref_name);
6704 free(commit_ref_name);
6708 static const struct got_error *
6709 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6710 struct got_object_id *new_commit_id, struct got_repository *repo)
6712 const struct got_error *err;
6713 struct got_reference *ref = NULL;
6714 struct got_object_id *old_commit_id = NULL;
6715 const char *branch_name = NULL;
6716 char *new_id_str = NULL;
6717 char *refname = NULL;
6719 branch_name = got_ref_get_name(branch);
6720 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6721 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6724 err = got_object_id_str(&new_id_str, new_commit_id);
6728 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6729 new_id_str) == -1) {
6730 err = got_error_from_errno("asprintf");
6734 err = got_ref_resolve(&old_commit_id, repo, branch);
6738 err = got_ref_alloc(&ref, refname, old_commit_id);
6742 err = got_ref_write(ref, repo);
6746 free(old_commit_id);
6752 const struct got_error *
6753 got_worktree_rebase_complete(struct got_worktree *worktree,
6754 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6755 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6756 struct got_repository *repo, int create_backup)
6758 const struct got_error *err, *unlockerr, *sync_err;
6759 struct got_object_id *new_head_commit_id = NULL;
6760 char *fileindex_path = NULL;
6762 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6766 if (create_backup) {
6767 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6768 rebased_branch, new_head_commit_id, repo);
6773 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6777 err = got_ref_write(rebased_branch, repo);
6781 err = got_worktree_set_head_ref(worktree, rebased_branch);
6785 err = delete_rebase_refs(worktree, repo);
6789 err = get_fileindex_path(&fileindex_path, worktree);
6792 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6793 sync_err = sync_fileindex(fileindex, fileindex_path);
6794 if (sync_err && err == NULL)
6797 got_fileindex_free(fileindex);
6798 free(fileindex_path);
6799 free(new_head_commit_id);
6800 unlockerr = lock_worktree(worktree, LOCK_SH);
6801 if (unlockerr && err == NULL)
6806 const struct got_error *
6807 got_worktree_rebase_abort(struct got_worktree *worktree,
6808 struct got_fileindex *fileindex, struct got_repository *repo,
6809 struct got_reference *new_base_branch,
6810 got_worktree_checkout_cb progress_cb, void *progress_arg)
6812 const struct got_error *err, *unlockerr, *sync_err;
6813 struct got_reference *resolved = NULL;
6814 struct got_object_id *commit_id = NULL;
6815 struct got_commit_object *commit = NULL;
6816 char *fileindex_path = NULL;
6817 struct revert_file_args rfa;
6818 struct got_object_id *tree_id = NULL;
6820 err = lock_worktree(worktree, LOCK_EX);
6824 err = got_object_open_as_commit(&commit, repo,
6825 worktree->base_commit_id);
6829 err = got_ref_open(&resolved, repo,
6830 got_ref_get_symref_target(new_base_branch), 0);
6834 err = got_worktree_set_head_ref(worktree, resolved);
6839 * XXX commits to the base branch could have happened while
6840 * we were busy rebasing; should we store the original commit ID
6841 * when rebase begins and read it back here?
6843 err = got_ref_resolve(&commit_id, repo, resolved);
6847 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6851 err = got_object_id_by_path(&tree_id, repo, commit,
6852 worktree->path_prefix);
6856 err = delete_rebase_refs(worktree, repo);
6860 err = get_fileindex_path(&fileindex_path, worktree);
6864 rfa.worktree = worktree;
6865 rfa.fileindex = fileindex;
6866 rfa.progress_cb = progress_cb;
6867 rfa.progress_arg = progress_arg;
6868 rfa.patch_cb = NULL;
6869 rfa.patch_arg = NULL;
6871 rfa.unlink_added_files = 0;
6872 err = worktree_status(worktree, "", fileindex, repo,
6873 revert_file, &rfa, NULL, NULL, 1, 0);
6877 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6878 repo, progress_cb, progress_arg, NULL, NULL);
6880 sync_err = sync_fileindex(fileindex, fileindex_path);
6881 if (sync_err && err == NULL)
6884 got_ref_close(resolved);
6888 got_object_commit_close(commit);
6890 got_fileindex_free(fileindex);
6891 free(fileindex_path);
6893 unlockerr = lock_worktree(worktree, LOCK_SH);
6894 if (unlockerr && err == NULL)
6899 const struct got_error *
6900 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6901 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6902 struct got_fileindex **fileindex, struct got_worktree *worktree,
6903 struct got_repository *repo)
6905 const struct got_error *err = NULL;
6906 char *tmp_branch_name = NULL;
6907 char *branch_ref_name = NULL;
6908 char *base_commit_ref_name = NULL;
6909 char *fileindex_path = NULL;
6910 struct check_rebase_ok_arg ok_arg;
6911 struct got_reference *wt_branch = NULL;
6912 struct got_reference *base_commit_ref = NULL;
6916 *base_commit_id = NULL;
6919 err = lock_worktree(worktree, LOCK_EX);
6923 err = open_fileindex(fileindex, &fileindex_path, worktree);
6927 ok_arg.worktree = worktree;
6929 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6934 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6938 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6942 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6947 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6952 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6956 err = got_ref_write(*branch_ref, repo);
6960 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6961 worktree->base_commit_id);
6964 err = got_ref_write(base_commit_ref, repo);
6967 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6968 if (*base_commit_id == NULL) {
6969 err = got_error_from_errno("got_object_id_dup");
6973 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6974 worktree->base_commit_id);
6977 err = got_ref_write(*tmp_branch, repo);
6981 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6985 free(fileindex_path);
6986 free(tmp_branch_name);
6987 free(branch_ref_name);
6988 free(base_commit_ref_name);
6990 got_ref_close(wt_branch);
6993 got_ref_close(*branch_ref);
6997 got_ref_close(*tmp_branch);
7000 free(*base_commit_id);
7002 got_fileindex_free(*fileindex);
7005 lock_worktree(worktree, LOCK_SH);
7010 const struct got_error *
7011 got_worktree_histedit_postpone(struct got_worktree *worktree,
7012 struct got_fileindex *fileindex)
7015 got_fileindex_free(fileindex);
7016 return lock_worktree(worktree, LOCK_SH);
7019 const struct got_error *
7020 got_worktree_histedit_in_progress(int *in_progress,
7021 struct got_worktree *worktree)
7023 const struct got_error *err;
7024 char *tmp_branch_name = NULL;
7026 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7030 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7031 free(tmp_branch_name);
7035 const struct got_error *
7036 got_worktree_histedit_continue(struct got_object_id **commit_id,
7037 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7038 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7039 struct got_worktree *worktree, struct got_repository *repo)
7041 const struct got_error *err;
7042 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7043 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7044 struct got_reference *commit_ref = NULL;
7045 struct got_reference *base_commit_ref = NULL;
7046 char *fileindex_path = NULL;
7047 int have_staged_files = 0;
7051 *base_commit_id = NULL;
7054 err = lock_worktree(worktree, LOCK_EX);
7058 err = open_fileindex(fileindex, &fileindex_path, worktree);
7062 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7063 &have_staged_files);
7064 if (err && err->code != GOT_ERR_CANCELLED)
7066 if (have_staged_files) {
7067 err = got_error(GOT_ERR_STAGED_PATHS);
7071 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7075 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7079 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7083 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7088 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7092 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7095 err = got_ref_resolve(commit_id, repo, commit_ref);
7099 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7102 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7106 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7110 free(commit_ref_name);
7111 free(branch_ref_name);
7112 free(fileindex_path);
7114 got_ref_close(commit_ref);
7115 if (base_commit_ref)
7116 got_ref_close(base_commit_ref);
7120 free(*base_commit_id);
7121 *base_commit_id = NULL;
7123 got_ref_close(*tmp_branch);
7127 got_fileindex_free(*fileindex);
7130 lock_worktree(worktree, LOCK_EX);
7135 static const struct got_error *
7136 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7138 const struct got_error *err;
7139 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7140 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7142 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7145 err = delete_ref(tmp_branch_name, repo);
7149 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7153 err = delete_ref(base_commit_ref_name, repo);
7157 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7160 err = delete_ref(branch_ref_name, repo);
7164 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7167 err = delete_ref(commit_ref_name, repo);
7171 free(tmp_branch_name);
7172 free(base_commit_ref_name);
7173 free(branch_ref_name);
7174 free(commit_ref_name);
7178 const struct got_error *
7179 got_worktree_histedit_abort(struct got_worktree *worktree,
7180 struct got_fileindex *fileindex, struct got_repository *repo,
7181 struct got_reference *branch, struct got_object_id *base_commit_id,
7182 got_worktree_checkout_cb progress_cb, void *progress_arg)
7184 const struct got_error *err, *unlockerr, *sync_err;
7185 struct got_reference *resolved = NULL;
7186 char *fileindex_path = NULL;
7187 struct got_commit_object *commit = NULL;
7188 struct got_object_id *tree_id = NULL;
7189 struct revert_file_args rfa;
7191 err = lock_worktree(worktree, LOCK_EX);
7195 err = got_object_open_as_commit(&commit, repo,
7196 worktree->base_commit_id);
7200 err = got_ref_open(&resolved, repo,
7201 got_ref_get_symref_target(branch), 0);
7205 err = got_worktree_set_head_ref(worktree, resolved);
7209 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7213 err = got_object_id_by_path(&tree_id, repo, commit,
7214 worktree->path_prefix);
7218 err = delete_histedit_refs(worktree, repo);
7222 err = get_fileindex_path(&fileindex_path, worktree);
7226 rfa.worktree = worktree;
7227 rfa.fileindex = fileindex;
7228 rfa.progress_cb = progress_cb;
7229 rfa.progress_arg = progress_arg;
7230 rfa.patch_cb = NULL;
7231 rfa.patch_arg = NULL;
7233 rfa.unlink_added_files = 0;
7234 err = worktree_status(worktree, "", fileindex, repo,
7235 revert_file, &rfa, NULL, NULL, 1, 0);
7239 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7240 repo, progress_cb, progress_arg, NULL, NULL);
7242 sync_err = sync_fileindex(fileindex, fileindex_path);
7243 if (sync_err && err == NULL)
7246 got_ref_close(resolved);
7248 free(fileindex_path);
7250 unlockerr = lock_worktree(worktree, LOCK_SH);
7251 if (unlockerr && err == NULL)
7256 const struct got_error *
7257 got_worktree_histedit_complete(struct got_worktree *worktree,
7258 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7259 struct got_reference *edited_branch, struct got_repository *repo)
7261 const struct got_error *err, *unlockerr, *sync_err;
7262 struct got_object_id *new_head_commit_id = NULL;
7263 struct got_reference *resolved = NULL;
7264 char *fileindex_path = NULL;
7266 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7270 err = got_ref_open(&resolved, repo,
7271 got_ref_get_symref_target(edited_branch), 0);
7275 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7276 resolved, new_head_commit_id, repo);
7280 err = got_ref_change_ref(resolved, new_head_commit_id);
7284 err = got_ref_write(resolved, repo);
7288 err = got_worktree_set_head_ref(worktree, resolved);
7292 err = delete_histedit_refs(worktree, repo);
7296 err = get_fileindex_path(&fileindex_path, worktree);
7299 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7300 sync_err = sync_fileindex(fileindex, fileindex_path);
7301 if (sync_err && err == NULL)
7304 got_fileindex_free(fileindex);
7305 free(fileindex_path);
7306 free(new_head_commit_id);
7307 unlockerr = lock_worktree(worktree, LOCK_SH);
7308 if (unlockerr && err == NULL)
7313 const struct got_error *
7314 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7315 struct got_object_id *commit_id, struct got_repository *repo)
7317 const struct got_error *err;
7318 char *commit_ref_name;
7320 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7324 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7328 err = delete_ref(commit_ref_name, repo);
7330 free(commit_ref_name);
7334 const struct got_error *
7335 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7336 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7337 struct got_worktree *worktree, const char *refname,
7338 struct got_repository *repo)
7340 const struct got_error *err = NULL;
7341 char *fileindex_path = NULL;
7342 struct check_rebase_ok_arg ok_arg;
7346 *base_branch_ref = NULL;
7348 err = lock_worktree(worktree, LOCK_EX);
7352 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7353 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7354 "cannot integrate a branch into itself; "
7355 "update -b or different branch name required");
7359 err = open_fileindex(fileindex, &fileindex_path, worktree);
7363 /* Preconditions are the same as for rebase. */
7364 ok_arg.worktree = worktree;
7366 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7371 err = got_ref_open(branch_ref, repo, refname, 1);
7375 err = got_ref_open(base_branch_ref, repo,
7376 got_worktree_get_head_ref_name(worktree), 1);
7380 got_ref_close(*branch_ref);
7383 if (*base_branch_ref) {
7384 got_ref_close(*base_branch_ref);
7385 *base_branch_ref = NULL;
7388 got_fileindex_free(*fileindex);
7391 lock_worktree(worktree, LOCK_SH);
7396 const struct got_error *
7397 got_worktree_integrate_continue(struct got_worktree *worktree,
7398 struct got_fileindex *fileindex, struct got_repository *repo,
7399 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7400 got_worktree_checkout_cb progress_cb, void *progress_arg,
7401 got_cancel_cb cancel_cb, void *cancel_arg)
7403 const struct got_error *err = NULL, *sync_err, *unlockerr;
7404 char *fileindex_path = NULL;
7405 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7406 struct got_commit_object *commit = NULL;
7408 err = get_fileindex_path(&fileindex_path, worktree);
7412 err = got_ref_resolve(&commit_id, repo, branch_ref);
7416 err = got_object_open_as_commit(&commit, repo, commit_id);
7420 err = got_object_id_by_path(&tree_id, repo, commit,
7421 worktree->path_prefix);
7425 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7429 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7430 progress_cb, progress_arg, cancel_cb, cancel_arg);
7434 err = got_ref_change_ref(base_branch_ref, commit_id);
7438 err = got_ref_write(base_branch_ref, repo);
7442 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7444 sync_err = sync_fileindex(fileindex, fileindex_path);
7445 if (sync_err && err == NULL)
7449 unlockerr = got_ref_unlock(branch_ref);
7450 if (unlockerr && err == NULL)
7452 got_ref_close(branch_ref);
7454 unlockerr = got_ref_unlock(base_branch_ref);
7455 if (unlockerr && err == NULL)
7457 got_ref_close(base_branch_ref);
7459 got_fileindex_free(fileindex);
7460 free(fileindex_path);
7463 got_object_commit_close(commit);
7465 unlockerr = lock_worktree(worktree, LOCK_SH);
7466 if (unlockerr && err == NULL)
7471 const struct got_error *
7472 got_worktree_integrate_abort(struct got_worktree *worktree,
7473 struct got_fileindex *fileindex, struct got_repository *repo,
7474 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7476 const struct got_error *err = NULL, *unlockerr = NULL;
7478 got_fileindex_free(fileindex);
7480 err = lock_worktree(worktree, LOCK_SH);
7482 unlockerr = got_ref_unlock(branch_ref);
7483 if (unlockerr && err == NULL)
7485 got_ref_close(branch_ref);
7487 unlockerr = got_ref_unlock(base_branch_ref);
7488 if (unlockerr && err == NULL)
7490 got_ref_close(base_branch_ref);
7495 const struct got_error *
7496 got_worktree_merge_postpone(struct got_worktree *worktree,
7497 struct got_fileindex *fileindex)
7499 const struct got_error *err, *sync_err;
7500 char *fileindex_path = NULL;
7502 err = get_fileindex_path(&fileindex_path, worktree);
7506 sync_err = sync_fileindex(fileindex, fileindex_path);
7508 err = lock_worktree(worktree, LOCK_SH);
7509 if (sync_err && err == NULL)
7512 got_fileindex_free(fileindex);
7513 free(fileindex_path);
7517 static const struct got_error *
7518 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7520 const struct got_error *err;
7521 char *branch_refname = NULL, *commit_refname = NULL;
7523 err = get_merge_branch_ref_name(&branch_refname, worktree);
7526 err = delete_ref(branch_refname, repo);
7530 err = get_merge_commit_ref_name(&commit_refname, worktree);
7533 err = delete_ref(commit_refname, repo);
7538 free(branch_refname);
7539 free(commit_refname);
7543 struct merge_commit_msg_arg {
7544 struct got_worktree *worktree;
7545 const char *branch_name;
7548 static const struct got_error *
7549 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7552 struct merge_commit_msg_arg *a = arg;
7554 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7555 got_worktree_get_head_ref_name(a->worktree)) == -1)
7556 return got_error_from_errno("asprintf");
7562 const struct got_error *
7563 got_worktree_merge_branch(struct got_worktree *worktree,
7564 struct got_fileindex *fileindex,
7565 struct got_object_id *yca_commit_id,
7566 struct got_object_id *branch_tip,
7567 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7568 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7570 const struct got_error *err;
7571 char *fileindex_path = NULL;
7573 err = get_fileindex_path(&fileindex_path, worktree);
7577 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7582 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7583 branch_tip, repo, progress_cb, progress_arg,
7584 cancel_cb, cancel_arg);
7586 free(fileindex_path);
7590 const struct got_error *
7591 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7592 struct got_worktree *worktree, struct got_fileindex *fileindex,
7593 const char *author, const char *committer, int allow_bad_symlinks,
7594 struct got_object_id *branch_tip, const char *branch_name,
7595 struct got_repository *repo,
7596 got_worktree_status_cb status_cb, void *status_arg)
7599 const struct got_error *err = NULL, *sync_err;
7600 struct got_pathlist_head commitable_paths;
7601 struct collect_commitables_arg cc_arg;
7602 struct got_pathlist_entry *pe;
7603 struct got_reference *head_ref = NULL;
7604 struct got_object_id *head_commit_id = NULL;
7605 int have_staged_files = 0;
7606 struct merge_commit_msg_arg mcm_arg;
7607 char *fileindex_path = NULL;
7609 *new_commit_id = NULL;
7611 TAILQ_INIT(&commitable_paths);
7613 err = get_fileindex_path(&fileindex_path, worktree);
7617 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7621 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7625 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7626 &have_staged_files);
7627 if (err && err->code != GOT_ERR_CANCELLED)
7629 if (have_staged_files) {
7630 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7634 cc_arg.commitable_paths = &commitable_paths;
7635 cc_arg.worktree = worktree;
7636 cc_arg.fileindex = fileindex;
7638 cc_arg.have_staged_files = have_staged_files;
7639 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7640 err = worktree_status(worktree, "", fileindex, repo,
7641 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7645 if (TAILQ_EMPTY(&commitable_paths)) {
7646 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7647 "merge of %s cannot proceed", branch_name);
7651 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7652 struct got_commitable *ct = pe->data;
7653 const char *ct_path = ct->in_repo_path;
7655 while (ct_path[0] == '/')
7657 err = check_out_of_date(ct_path, ct->status,
7658 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7659 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7665 mcm_arg.worktree = worktree;
7666 mcm_arg.branch_name = branch_name;
7667 err = commit_worktree(new_commit_id, &commitable_paths,
7668 head_commit_id, branch_tip, worktree, author, committer,
7669 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7673 err = update_fileindex_after_commit(worktree, &commitable_paths,
7674 *new_commit_id, fileindex, have_staged_files);
7675 sync_err = sync_fileindex(fileindex, fileindex_path);
7676 if (sync_err && err == NULL)
7679 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7680 struct got_commitable *ct = pe->data;
7681 free_commitable(ct);
7683 got_pathlist_free(&commitable_paths);
7684 free(fileindex_path);
7688 const struct got_error *
7689 got_worktree_merge_complete(struct got_worktree *worktree,
7690 struct got_fileindex *fileindex, struct got_repository *repo)
7692 const struct got_error *err, *unlockerr, *sync_err;
7693 char *fileindex_path = NULL;
7695 err = delete_merge_refs(worktree, repo);
7699 err = get_fileindex_path(&fileindex_path, worktree);
7702 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7703 sync_err = sync_fileindex(fileindex, fileindex_path);
7704 if (sync_err && err == NULL)
7707 got_fileindex_free(fileindex);
7708 free(fileindex_path);
7709 unlockerr = lock_worktree(worktree, LOCK_SH);
7710 if (unlockerr && err == NULL)
7715 const struct got_error *
7716 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7717 struct got_repository *repo)
7719 const struct got_error *err;
7720 char *branch_refname = NULL;
7721 struct got_reference *branch_ref = NULL;
7725 err = get_merge_branch_ref_name(&branch_refname, worktree);
7728 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7729 free(branch_refname);
7731 if (err->code != GOT_ERR_NOT_REF)
7739 const struct got_error *got_worktree_merge_prepare(
7740 struct got_fileindex **fileindex, struct got_worktree *worktree,
7741 struct got_reference *branch, struct got_repository *repo)
7743 const struct got_error *err = NULL;
7744 char *fileindex_path = NULL;
7745 char *branch_refname = NULL, *commit_refname = NULL;
7746 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7747 struct got_reference *commit_ref = NULL;
7748 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7749 struct check_rebase_ok_arg ok_arg;
7753 err = lock_worktree(worktree, LOCK_EX);
7757 err = open_fileindex(fileindex, &fileindex_path, worktree);
7761 /* Preconditions are the same as for rebase. */
7762 ok_arg.worktree = worktree;
7764 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7769 err = get_merge_branch_ref_name(&branch_refname, worktree);
7773 err = get_merge_commit_ref_name(&commit_refname, worktree);
7777 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7782 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7786 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7787 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7791 err = got_ref_resolve(&branch_tip, repo, branch);
7795 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7798 err = got_ref_write(branch_ref, repo);
7802 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7805 err = got_ref_write(commit_ref, repo);
7810 free(branch_refname);
7811 free(commit_refname);
7812 free(fileindex_path);
7814 got_ref_close(branch_ref);
7816 got_ref_close(commit_ref);
7818 got_ref_close(wt_branch);
7819 free(wt_branch_tip);
7822 got_fileindex_free(*fileindex);
7825 lock_worktree(worktree, LOCK_SH);
7830 const struct got_error *
7831 got_worktree_merge_continue(char **branch_name,
7832 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7833 struct got_worktree *worktree, struct got_repository *repo)
7835 const struct got_error *err;
7836 char *commit_refname = NULL, *branch_refname = NULL;
7837 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7838 char *fileindex_path = NULL;
7839 int have_staged_files = 0;
7841 *branch_name = NULL;
7845 err = lock_worktree(worktree, LOCK_EX);
7849 err = open_fileindex(fileindex, &fileindex_path, worktree);
7853 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7854 &have_staged_files);
7855 if (err && err->code != GOT_ERR_CANCELLED)
7857 if (have_staged_files) {
7858 err = got_error(GOT_ERR_STAGED_PATHS);
7862 err = get_merge_branch_ref_name(&branch_refname, worktree);
7866 err = get_merge_commit_ref_name(&commit_refname, worktree);
7870 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7874 if (!got_ref_is_symbolic(branch_ref)) {
7875 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7876 "%s is not a symbolic reference",
7877 got_ref_get_name(branch_ref));
7880 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7881 if (*branch_name == NULL) {
7882 err = got_error_from_errno("strdup");
7886 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7890 err = got_ref_resolve(branch_tip, repo, commit_ref);
7894 free(commit_refname);
7895 free(branch_refname);
7896 free(fileindex_path);
7898 got_ref_close(commit_ref);
7900 got_ref_close(branch_ref);
7904 *branch_name = NULL;
7909 got_fileindex_free(*fileindex);
7912 lock_worktree(worktree, LOCK_SH);
7917 const struct got_error *
7918 got_worktree_merge_abort(struct got_worktree *worktree,
7919 struct got_fileindex *fileindex, struct got_repository *repo,
7920 got_worktree_checkout_cb progress_cb, void *progress_arg)
7922 const struct got_error *err, *unlockerr, *sync_err;
7923 struct got_object_id *commit_id = NULL;
7924 struct got_commit_object *commit = NULL;
7925 char *fileindex_path = NULL;
7926 struct revert_file_args rfa;
7927 struct got_object_id *tree_id = NULL;
7929 err = got_object_open_as_commit(&commit, repo,
7930 worktree->base_commit_id);
7934 err = got_object_id_by_path(&tree_id, repo, commit,
7935 worktree->path_prefix);
7939 err = delete_merge_refs(worktree, repo);
7943 err = get_fileindex_path(&fileindex_path, worktree);
7947 rfa.worktree = worktree;
7948 rfa.fileindex = fileindex;
7949 rfa.progress_cb = progress_cb;
7950 rfa.progress_arg = progress_arg;
7951 rfa.patch_cb = NULL;
7952 rfa.patch_arg = NULL;
7954 rfa.unlink_added_files = 1;
7955 err = worktree_status(worktree, "", fileindex, repo,
7956 revert_file, &rfa, NULL, NULL, 1, 0);
7960 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7961 repo, progress_cb, progress_arg, NULL, NULL);
7963 sync_err = sync_fileindex(fileindex, fileindex_path);
7964 if (sync_err && err == NULL)
7970 got_object_commit_close(commit);
7972 got_fileindex_free(fileindex);
7973 free(fileindex_path);
7975 unlockerr = lock_worktree(worktree, LOCK_SH);
7976 if (unlockerr && err == NULL)
7981 struct check_stage_ok_arg {
7982 struct got_object_id *head_commit_id;
7983 struct got_worktree *worktree;
7984 struct got_fileindex *fileindex;
7985 struct got_repository *repo;
7989 static const struct got_error *
7990 check_stage_ok(void *arg, unsigned char status,
7991 unsigned char staged_status, const char *relpath,
7992 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7993 struct got_object_id *commit_id, int dirfd, const char *de_name)
7995 struct check_stage_ok_arg *a = arg;
7996 const struct got_error *err = NULL;
7997 struct got_fileindex_entry *ie;
7998 struct got_object_id base_commit_id;
7999 struct got_object_id *base_commit_idp = NULL;
8000 char *in_repo_path = NULL, *p;
8002 if (status == GOT_STATUS_UNVERSIONED ||
8003 status == GOT_STATUS_NO_CHANGE)
8005 if (status == GOT_STATUS_NONEXISTENT)
8006 return got_error_set_errno(ENOENT, relpath);
8008 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8010 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8012 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8013 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8015 return got_error_from_errno("asprintf");
8017 if (got_fileindex_entry_has_commit(ie)) {
8018 memcpy(base_commit_id.sha1, ie->commit_sha1,
8019 SHA1_DIGEST_LENGTH);
8020 base_commit_idp = &base_commit_id;
8023 if (status == GOT_STATUS_CONFLICT) {
8024 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8026 } else if (status != GOT_STATUS_ADD &&
8027 status != GOT_STATUS_MODIFY &&
8028 status != GOT_STATUS_DELETE) {
8029 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8033 a->have_changes = 1;
8038 err = check_out_of_date(p, status, staged_status,
8039 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8040 GOT_ERR_STAGE_OUT_OF_DATE);
8046 struct stage_path_arg {
8047 struct got_worktree *worktree;
8048 struct got_fileindex *fileindex;
8049 struct got_repository *repo;
8050 got_worktree_status_cb status_cb;
8052 got_worktree_patch_cb patch_cb;
8054 int staged_something;
8055 int allow_bad_symlinks;
8058 static const struct got_error *
8059 stage_path(void *arg, unsigned char status,
8060 unsigned char staged_status, const char *relpath,
8061 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8062 struct got_object_id *commit_id, int dirfd, const char *de_name)
8064 struct stage_path_arg *a = arg;
8065 const struct got_error *err = NULL;
8066 struct got_fileindex_entry *ie;
8067 char *ondisk_path = NULL, *path_content = NULL;
8069 struct got_object_id *new_staged_blob_id = NULL;
8072 if (status == GOT_STATUS_UNVERSIONED)
8075 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8077 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8079 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8081 return got_error_from_errno("asprintf");
8084 case GOT_STATUS_ADD:
8085 case GOT_STATUS_MODIFY:
8086 /* XXX could sb.st_mode be passed in by our caller? */
8087 if (lstat(ondisk_path, &sb) == -1) {
8088 err = got_error_from_errno2("lstat", ondisk_path);
8092 if (status == GOT_STATUS_ADD) {
8093 int choice = GOT_PATCH_CHOICE_NONE;
8094 err = (*a->patch_cb)(&choice, a->patch_arg,
8095 status, ie->path, NULL, 1, 1);
8098 if (choice != GOT_PATCH_CHOICE_YES)
8101 err = create_patched_content(&path_content, 0,
8102 staged_blob_id ? staged_blob_id : blob_id,
8103 ondisk_path, dirfd, de_name, ie->path,
8104 a->repo, a->patch_cb, a->patch_arg);
8105 if (err || path_content == NULL)
8109 err = got_object_blob_create(&new_staged_blob_id,
8110 path_content ? path_content : ondisk_path, a->repo);
8113 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8114 SHA1_DIGEST_LENGTH);
8115 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8116 stage = GOT_FILEIDX_STAGE_ADD;
8118 stage = GOT_FILEIDX_STAGE_MODIFY;
8119 got_fileindex_entry_stage_set(ie, stage);
8120 if (S_ISLNK(sb.st_mode)) {
8121 int is_bad_symlink = 0;
8122 if (!a->allow_bad_symlinks) {
8123 char target_path[PATH_MAX];
8125 target_len = readlink(ondisk_path, target_path,
8126 sizeof(target_path));
8127 if (target_len == -1) {
8128 err = got_error_from_errno2("readlink",
8132 err = is_bad_symlink_target(&is_bad_symlink,
8133 target_path, target_len, ondisk_path,
8134 a->worktree->root_path);
8137 if (is_bad_symlink) {
8138 err = got_error_path(ondisk_path,
8139 GOT_ERR_BAD_SYMLINK);
8144 got_fileindex_entry_staged_filetype_set(ie,
8145 GOT_FILEIDX_MODE_BAD_SYMLINK);
8147 got_fileindex_entry_staged_filetype_set(ie,
8148 GOT_FILEIDX_MODE_SYMLINK);
8150 got_fileindex_entry_staged_filetype_set(ie,
8151 GOT_FILEIDX_MODE_REGULAR_FILE);
8153 a->staged_something = 1;
8154 if (a->status_cb == NULL)
8156 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8157 get_staged_status(ie), relpath, blob_id,
8158 new_staged_blob_id, NULL, dirfd, de_name);
8162 * When staging the reverse of the staged diff,
8163 * implicitly unstage the file.
8165 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8166 sizeof(ie->blob_sha1)) == 0) {
8167 got_fileindex_entry_stage_set(ie,
8168 GOT_FILEIDX_STAGE_NONE);
8171 case GOT_STATUS_DELETE:
8172 if (staged_status == GOT_STATUS_DELETE)
8175 int choice = GOT_PATCH_CHOICE_NONE;
8176 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8177 ie->path, NULL, 1, 1);
8180 if (choice == GOT_PATCH_CHOICE_NO)
8182 if (choice != GOT_PATCH_CHOICE_YES) {
8183 err = got_error(GOT_ERR_PATCH_CHOICE);
8187 stage = GOT_FILEIDX_STAGE_DELETE;
8188 got_fileindex_entry_stage_set(ie, stage);
8189 a->staged_something = 1;
8190 if (a->status_cb == NULL)
8192 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8193 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8196 case GOT_STATUS_NO_CHANGE:
8198 case GOT_STATUS_CONFLICT:
8199 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8201 case GOT_STATUS_NONEXISTENT:
8202 err = got_error_set_errno(ENOENT, relpath);
8205 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8209 if (path_content && unlink(path_content) == -1 && err == NULL)
8210 err = got_error_from_errno2("unlink", path_content);
8213 free(new_staged_blob_id);
8217 const struct got_error *
8218 got_worktree_stage(struct got_worktree *worktree,
8219 struct got_pathlist_head *paths,
8220 got_worktree_status_cb status_cb, void *status_arg,
8221 got_worktree_patch_cb patch_cb, void *patch_arg,
8222 int allow_bad_symlinks, struct got_repository *repo)
8224 const struct got_error *err = NULL, *sync_err, *unlockerr;
8225 struct got_pathlist_entry *pe;
8226 struct got_fileindex *fileindex = NULL;
8227 char *fileindex_path = NULL;
8228 struct got_reference *head_ref = NULL;
8229 struct got_object_id *head_commit_id = NULL;
8230 struct check_stage_ok_arg oka;
8231 struct stage_path_arg spa;
8233 err = lock_worktree(worktree, LOCK_EX);
8237 err = got_ref_open(&head_ref, repo,
8238 got_worktree_get_head_ref_name(worktree), 0);
8241 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8244 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8248 /* Check pre-conditions before staging anything. */
8249 oka.head_commit_id = head_commit_id;
8250 oka.worktree = worktree;
8251 oka.fileindex = fileindex;
8253 oka.have_changes = 0;
8254 TAILQ_FOREACH(pe, paths, entry) {
8255 err = worktree_status(worktree, pe->path, fileindex, repo,
8256 check_stage_ok, &oka, NULL, NULL, 1, 0);
8260 if (!oka.have_changes) {
8261 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8265 spa.worktree = worktree;
8266 spa.fileindex = fileindex;
8268 spa.patch_cb = patch_cb;
8269 spa.patch_arg = patch_arg;
8270 spa.status_cb = status_cb;
8271 spa.status_arg = status_arg;
8272 spa.staged_something = 0;
8273 spa.allow_bad_symlinks = allow_bad_symlinks;
8274 TAILQ_FOREACH(pe, paths, entry) {
8275 err = worktree_status(worktree, pe->path, fileindex, repo,
8276 stage_path, &spa, NULL, NULL, 1, 0);
8280 if (!spa.staged_something) {
8281 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8285 sync_err = sync_fileindex(fileindex, fileindex_path);
8286 if (sync_err && err == NULL)
8290 got_ref_close(head_ref);
8291 free(head_commit_id);
8292 free(fileindex_path);
8294 got_fileindex_free(fileindex);
8295 unlockerr = lock_worktree(worktree, LOCK_SH);
8296 if (unlockerr && err == NULL)
8301 struct unstage_path_arg {
8302 struct got_worktree *worktree;
8303 struct got_fileindex *fileindex;
8304 struct got_repository *repo;
8305 got_worktree_checkout_cb progress_cb;
8307 got_worktree_patch_cb patch_cb;
8311 static const struct got_error *
8312 create_unstaged_content(char **path_unstaged_content,
8313 char **path_new_staged_content, struct got_object_id *blob_id,
8314 struct got_object_id *staged_blob_id, const char *relpath,
8315 struct got_repository *repo,
8316 got_worktree_patch_cb patch_cb, void *patch_arg)
8318 const struct got_error *err, *free_err;
8319 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8320 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8321 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8322 struct got_diffreg_result *diffreg_result = NULL;
8323 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8324 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8325 int fd1 = -1, fd2 = -1;
8327 *path_unstaged_content = NULL;
8328 *path_new_staged_content = NULL;
8330 err = got_object_id_str(&label1, blob_id);
8334 fd1 = got_opentempfd();
8336 err = got_error_from_errno("got_opentempfd");
8339 fd2 = got_opentempfd();
8341 err = got_error_from_errno("got_opentempfd");
8345 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8349 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8353 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8357 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8362 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8366 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8370 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8371 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8375 err = got_opentemp_named(path_unstaged_content, &outfile,
8376 "got-unstaged-content");
8379 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8380 "got-new-staged-content");
8384 if (fseek(f1, 0L, SEEK_SET) == -1) {
8385 err = got_ferror(f1, GOT_ERR_IO);
8388 if (fseek(f2, 0L, SEEK_SET) == -1) {
8389 err = got_ferror(f2, GOT_ERR_IO);
8392 /* Count the number of actual changes in the diff result. */
8393 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8394 struct diff_chunk_context cc = {};
8395 diff_chunk_context_load_change(&cc, &nchunks_used,
8396 diffreg_result->result, n, 0);
8399 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8401 err = apply_or_reject_change(&choice, &nchunks_used,
8402 diffreg_result->result, n, relpath, f1, f2,
8403 &line_cur1, &line_cur2,
8404 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8407 if (choice == GOT_PATCH_CHOICE_YES)
8410 have_rejected_content = 1;
8411 if (choice == GOT_PATCH_CHOICE_QUIT)
8414 if (have_content || have_rejected_content)
8415 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8416 outfile, rejectfile);
8419 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8420 err = got_error_from_errno("close");
8422 got_object_blob_close(blob);
8423 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8424 err = got_error_from_errno("close");
8426 got_object_blob_close(staged_blob);
8427 free_err = got_diffreg_result_free(diffreg_result);
8428 if (free_err && err == NULL)
8430 if (f1 && fclose(f1) == EOF && err == NULL)
8431 err = got_error_from_errno2("fclose", path1);
8432 if (f2 && fclose(f2) == EOF && err == NULL)
8433 err = got_error_from_errno2("fclose", path2);
8434 if (outfile && fclose(outfile) == EOF && err == NULL)
8435 err = got_error_from_errno2("fclose", *path_unstaged_content);
8436 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8437 err = got_error_from_errno2("fclose", *path_new_staged_content);
8438 if (path1 && unlink(path1) == -1 && err == NULL)
8439 err = got_error_from_errno2("unlink", path1);
8440 if (path2 && unlink(path2) == -1 && err == NULL)
8441 err = got_error_from_errno2("unlink", path2);
8442 if (err || !have_content) {
8443 if (*path_unstaged_content &&
8444 unlink(*path_unstaged_content) == -1 && err == NULL)
8445 err = got_error_from_errno2("unlink",
8446 *path_unstaged_content);
8447 free(*path_unstaged_content);
8448 *path_unstaged_content = NULL;
8450 if (err || !have_content || !have_rejected_content) {
8451 if (*path_new_staged_content &&
8452 unlink(*path_new_staged_content) == -1 && err == NULL)
8453 err = got_error_from_errno2("unlink",
8454 *path_new_staged_content);
8455 free(*path_new_staged_content);
8456 *path_new_staged_content = NULL;
8463 static const struct got_error *
8464 unstage_hunks(struct got_object_id *staged_blob_id,
8465 struct got_blob_object *blob_base,
8466 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8467 const char *ondisk_path, const char *label_orig,
8468 struct got_worktree *worktree, struct got_repository *repo,
8469 got_worktree_patch_cb patch_cb, void *patch_arg,
8470 got_worktree_checkout_cb progress_cb, void *progress_arg)
8472 const struct got_error *err = NULL;
8473 char *path_unstaged_content = NULL;
8474 char *path_new_staged_content = NULL;
8475 char *parent = NULL, *base_path = NULL;
8476 char *blob_base_path = NULL;
8477 struct got_object_id *new_staged_blob_id = NULL;
8478 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8481 err = create_unstaged_content(&path_unstaged_content,
8482 &path_new_staged_content, blob_id, staged_blob_id,
8483 ie->path, repo, patch_cb, patch_arg);
8487 if (path_unstaged_content == NULL)
8490 if (path_new_staged_content) {
8491 err = got_object_blob_create(&new_staged_blob_id,
8492 path_new_staged_content, repo);
8497 f = fopen(path_unstaged_content, "re");
8499 err = got_error_from_errno2("fopen",
8500 path_unstaged_content);
8503 if (fstat(fileno(f), &sb) == -1) {
8504 err = got_error_from_errno2("fstat", path_unstaged_content);
8507 if (got_fileindex_entry_staged_filetype_get(ie) ==
8508 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8509 char link_target[PATH_MAX];
8511 r = fread(link_target, 1, sizeof(link_target), f);
8512 if (r == 0 && ferror(f)) {
8513 err = got_error_from_errno("fread");
8516 if (r >= sizeof(link_target)) { /* should not happen */
8517 err = got_error(GOT_ERR_NO_SPACE);
8520 link_target[r] = '\0';
8521 err = merge_symlink(worktree, blob_base,
8522 ondisk_path, ie->path, label_orig, link_target,
8523 worktree->base_commit_id, repo, progress_cb,
8526 int local_changes_subsumed;
8528 err = got_path_dirname(&parent, ondisk_path);
8532 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8534 err = got_error_from_errno("asprintf");
8539 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8542 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8548 * In order the run a 3-way merge with a symlink we copy the symlink's
8549 * target path into a temporary file and use that file with diff3.
8551 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8552 err = dump_symlink_target_path_to_file(&f_deriv2,
8558 fd = open(ondisk_path,
8559 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8561 err = got_error_from_errno2("open", ondisk_path);
8564 f_deriv2 = fdopen(fd, "r");
8565 if (f_deriv2 == NULL) {
8566 err = got_error_from_errno2("fdopen", ondisk_path);
8572 err = merge_file(&local_changes_subsumed, worktree,
8573 f_base, f, f_deriv2, ondisk_path, ie->path,
8574 got_fileindex_perms_to_st(ie),
8575 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8576 repo, progress_cb, progress_arg);
8581 if (new_staged_blob_id) {
8582 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8583 SHA1_DIGEST_LENGTH);
8585 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8586 got_fileindex_entry_staged_filetype_set(ie, 0);
8589 free(new_staged_blob_id);
8590 if (path_unstaged_content &&
8591 unlink(path_unstaged_content) == -1 && err == NULL)
8592 err = got_error_from_errno2("unlink", path_unstaged_content);
8593 if (path_new_staged_content &&
8594 unlink(path_new_staged_content) == -1 && err == NULL)
8595 err = got_error_from_errno2("unlink", path_new_staged_content);
8596 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8597 err = got_error_from_errno2("unlink", blob_base_path);
8598 if (f_base && fclose(f_base) == EOF && err == NULL)
8599 err = got_error_from_errno2("fclose", path_unstaged_content);
8600 if (f && fclose(f) == EOF && err == NULL)
8601 err = got_error_from_errno2("fclose", path_unstaged_content);
8602 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8603 err = got_error_from_errno2("fclose", ondisk_path);
8604 free(path_unstaged_content);
8605 free(path_new_staged_content);
8606 free(blob_base_path);
8612 static const struct got_error *
8613 unstage_path(void *arg, unsigned char status,
8614 unsigned char staged_status, const char *relpath,
8615 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8616 struct got_object_id *commit_id, int dirfd, const char *de_name)
8618 const struct got_error *err = NULL;
8619 struct unstage_path_arg *a = arg;
8620 struct got_fileindex_entry *ie;
8621 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8622 char *ondisk_path = NULL;
8623 char *id_str = NULL, *label_orig = NULL;
8624 int local_changes_subsumed;
8626 int fd1 = -1, fd2 = -1;
8628 if (staged_status != GOT_STATUS_ADD &&
8629 staged_status != GOT_STATUS_MODIFY &&
8630 staged_status != GOT_STATUS_DELETE)
8633 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8635 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8637 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8639 return got_error_from_errno("asprintf");
8641 err = got_object_id_str(&id_str,
8642 commit_id ? commit_id : a->worktree->base_commit_id);
8645 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8647 err = got_error_from_errno("asprintf");
8651 fd1 = got_opentempfd();
8653 err = got_error_from_errno("got_opentempfd");
8656 fd2 = got_opentempfd();
8658 err = got_error_from_errno("got_opentempfd");
8662 switch (staged_status) {
8663 case GOT_STATUS_MODIFY:
8664 err = got_object_open_as_blob(&blob_base, a->repo,
8665 blob_id, 8192, fd1);
8669 case GOT_STATUS_ADD:
8671 if (staged_status == GOT_STATUS_ADD) {
8672 int choice = GOT_PATCH_CHOICE_NONE;
8673 err = (*a->patch_cb)(&choice, a->patch_arg,
8674 staged_status, ie->path, NULL, 1, 1);
8677 if (choice != GOT_PATCH_CHOICE_YES)
8680 err = unstage_hunks(staged_blob_id,
8681 blob_base, blob_id, ie, ondisk_path,
8682 label_orig, a->worktree, a->repo,
8683 a->patch_cb, a->patch_arg,
8684 a->progress_cb, a->progress_arg);
8685 break; /* Done with this file. */
8688 err = got_object_open_as_blob(&blob_staged, a->repo,
8689 staged_blob_id, 8192, fd2);
8692 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8693 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8694 case GOT_FILEIDX_MODE_REGULAR_FILE:
8695 err = merge_blob(&local_changes_subsumed, a->worktree,
8696 blob_base, ondisk_path, relpath,
8697 got_fileindex_perms_to_st(ie), label_orig,
8698 blob_staged, commit_id ? commit_id :
8699 a->worktree->base_commit_id, a->repo,
8700 a->progress_cb, a->progress_arg);
8702 case GOT_FILEIDX_MODE_SYMLINK:
8703 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8704 char *staged_target;
8705 err = got_object_blob_read_to_str(
8706 &staged_target, blob_staged);
8709 err = merge_symlink(a->worktree, blob_base,
8710 ondisk_path, relpath, label_orig,
8711 staged_target, commit_id ? commit_id :
8712 a->worktree->base_commit_id,
8713 a->repo, a->progress_cb, a->progress_arg);
8714 free(staged_target);
8716 err = merge_blob(&local_changes_subsumed,
8717 a->worktree, blob_base, ondisk_path,
8718 relpath, got_fileindex_perms_to_st(ie),
8719 label_orig, blob_staged,
8720 commit_id ? commit_id :
8721 a->worktree->base_commit_id, a->repo,
8722 a->progress_cb, a->progress_arg);
8726 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8730 got_fileindex_entry_stage_set(ie,
8731 GOT_FILEIDX_STAGE_NONE);
8732 got_fileindex_entry_staged_filetype_set(ie, 0);
8735 case GOT_STATUS_DELETE:
8737 int choice = GOT_PATCH_CHOICE_NONE;
8738 err = (*a->patch_cb)(&choice, a->patch_arg,
8739 staged_status, ie->path, NULL, 1, 1);
8742 if (choice == GOT_PATCH_CHOICE_NO)
8744 if (choice != GOT_PATCH_CHOICE_YES) {
8745 err = got_error(GOT_ERR_PATCH_CHOICE);
8749 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8750 got_fileindex_entry_staged_filetype_set(ie, 0);
8751 err = get_file_status(&status, &sb, ie, ondisk_path,
8752 dirfd, de_name, a->repo);
8755 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8760 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8761 err = got_error_from_errno("close");
8763 got_object_blob_close(blob_base);
8764 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8765 err = got_error_from_errno("close");
8767 got_object_blob_close(blob_staged);
8773 const struct got_error *
8774 got_worktree_unstage(struct got_worktree *worktree,
8775 struct got_pathlist_head *paths,
8776 got_worktree_checkout_cb progress_cb, void *progress_arg,
8777 got_worktree_patch_cb patch_cb, void *patch_arg,
8778 struct got_repository *repo)
8780 const struct got_error *err = NULL, *sync_err, *unlockerr;
8781 struct got_pathlist_entry *pe;
8782 struct got_fileindex *fileindex = NULL;
8783 char *fileindex_path = NULL;
8784 struct unstage_path_arg upa;
8786 err = lock_worktree(worktree, LOCK_EX);
8790 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8794 upa.worktree = worktree;
8795 upa.fileindex = fileindex;
8797 upa.progress_cb = progress_cb;
8798 upa.progress_arg = progress_arg;
8799 upa.patch_cb = patch_cb;
8800 upa.patch_arg = patch_arg;
8801 TAILQ_FOREACH(pe, paths, entry) {
8802 err = worktree_status(worktree, pe->path, fileindex, repo,
8803 unstage_path, &upa, NULL, NULL, 1, 0);
8808 sync_err = sync_fileindex(fileindex, fileindex_path);
8809 if (sync_err && err == NULL)
8812 free(fileindex_path);
8814 got_fileindex_free(fileindex);
8815 unlockerr = lock_worktree(worktree, LOCK_SH);
8816 if (unlockerr && err == NULL)
8821 struct report_file_info_arg {
8822 struct got_worktree *worktree;
8823 got_worktree_path_info_cb info_cb;
8825 struct got_pathlist_head *paths;
8826 got_cancel_cb cancel_cb;
8830 static const struct got_error *
8831 report_file_info(void *arg, struct got_fileindex_entry *ie)
8833 struct report_file_info_arg *a = arg;
8834 struct got_pathlist_entry *pe;
8835 struct got_object_id blob_id, staged_blob_id, commit_id;
8836 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8837 struct got_object_id *commit_idp = NULL;
8840 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8841 return got_error(GOT_ERR_CANCELLED);
8843 TAILQ_FOREACH(pe, a->paths, entry) {
8844 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8845 got_path_is_child(ie->path, pe->path, pe->path_len))
8848 if (pe == NULL) /* not found */
8851 if (got_fileindex_entry_has_blob(ie)) {
8852 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8853 blob_idp = &blob_id;
8855 stage = got_fileindex_entry_stage_get(ie);
8856 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8857 stage == GOT_FILEIDX_STAGE_ADD) {
8858 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8859 SHA1_DIGEST_LENGTH);
8860 staged_blob_idp = &staged_blob_id;
8863 if (got_fileindex_entry_has_commit(ie)) {
8864 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8865 commit_idp = &commit_id;
8868 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8869 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8872 const struct got_error *
8873 got_worktree_path_info(struct got_worktree *worktree,
8874 struct got_pathlist_head *paths,
8875 got_worktree_path_info_cb info_cb, void *info_arg,
8876 got_cancel_cb cancel_cb, void *cancel_arg)
8879 const struct got_error *err = NULL, *unlockerr;
8880 struct got_fileindex *fileindex = NULL;
8881 char *fileindex_path = NULL;
8882 struct report_file_info_arg arg;
8884 err = lock_worktree(worktree, LOCK_SH);
8888 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8892 arg.worktree = worktree;
8893 arg.info_cb = info_cb;
8894 arg.info_arg = info_arg;
8896 arg.cancel_cb = cancel_cb;
8897 arg.cancel_arg = cancel_arg;
8898 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8901 free(fileindex_path);
8903 got_fileindex_free(fileindex);
8904 unlockerr = lock_worktree(worktree, LOCK_UN);
8905 if (unlockerr && err == NULL)
8910 static const struct got_error *
8911 patch_check_path(const char *p, char **path, unsigned char *status,
8912 unsigned char *staged_status, struct got_fileindex *fileindex,
8913 struct got_worktree *worktree, struct got_repository *repo)
8915 const struct got_error *err;
8916 struct got_fileindex_entry *ie;
8918 char *ondisk_path = NULL;
8920 err = got_worktree_resolve_path(path, worktree, p);
8924 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8925 *path[0] ? "/" : "", *path) == -1)
8926 return got_error_from_errno("asprintf");
8928 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8930 *staged_status = get_staged_status(ie);
8931 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8936 *staged_status = GOT_STATUS_NO_CHANGE;
8937 *status = GOT_STATUS_UNVERSIONED;
8938 if (lstat(ondisk_path, &sb) == -1) {
8939 if (errno != ENOENT) {
8940 err = got_error_from_errno2("lstat",
8944 *status = GOT_STATUS_NONEXISTENT;
8953 static const struct got_error *
8954 patch_can_rm(const char *path, unsigned char status,
8955 unsigned char staged_status)
8957 if (status == GOT_STATUS_NONEXISTENT)
8958 return got_error_set_errno(ENOENT, path);
8959 if (status != GOT_STATUS_NO_CHANGE &&
8960 status != GOT_STATUS_ADD &&
8961 status != GOT_STATUS_MODIFY &&
8962 status != GOT_STATUS_MODE_CHANGE)
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 static const struct got_error *
8970 patch_can_add(const char *path, unsigned char status)
8972 if (status != GOT_STATUS_NONEXISTENT)
8973 return got_error_path(path, GOT_ERR_FILE_STATUS);
8977 static const struct got_error *
8978 patch_can_edit(const char *path, unsigned char status,
8979 unsigned char staged_status)
8981 if (status == GOT_STATUS_NONEXISTENT)
8982 return got_error_set_errno(ENOENT, path);
8983 if (status != GOT_STATUS_NO_CHANGE &&
8984 status != GOT_STATUS_ADD &&
8985 status != GOT_STATUS_MODIFY)
8986 return got_error_path(path, GOT_ERR_FILE_STATUS);
8987 if (staged_status == GOT_STATUS_DELETE)
8988 return got_error_path(path, GOT_ERR_FILE_STATUS);
8992 const struct got_error *
8993 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8994 char **fileindex_path, struct got_worktree *worktree)
8996 return open_fileindex(fileindex, fileindex_path, worktree);
8999 const struct got_error *
9000 got_worktree_patch_check_path(const char *old, const char *new,
9001 char **oldpath, char **newpath, struct got_worktree *worktree,
9002 struct got_repository *repo, struct got_fileindex *fileindex)
9004 const struct got_error *err = NULL;
9005 int file_renamed = 0;
9006 unsigned char status_old, staged_status_old;
9007 unsigned char status_new, staged_status_new;
9012 err = patch_check_path(old != NULL ? old : new, oldpath,
9013 &status_old, &staged_status_old, fileindex, worktree, repo);
9017 err = patch_check_path(new != NULL ? new : old, newpath,
9018 &status_new, &staged_status_new, fileindex, worktree, repo);
9022 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9025 if (old != NULL && new == NULL)
9026 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9027 else if (file_renamed) {
9028 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9030 err = patch_can_add(*newpath, status_new);
9031 } else if (old == NULL)
9032 err = patch_can_add(*newpath, status_new);
9034 err = patch_can_edit(*newpath, status_new, staged_status_new);
9046 const struct got_error *
9047 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9048 struct got_worktree *worktree, struct got_fileindex *fileindex,
9049 got_worktree_checkout_cb progress_cb, void *progress_arg)
9051 struct schedule_addition_args saa;
9053 memset(&saa, 0, sizeof(saa));
9054 saa.worktree = worktree;
9055 saa.fileindex = fileindex;
9056 saa.progress_cb = progress_cb;
9057 saa.progress_arg = progress_arg;
9060 return worktree_status(worktree, path, fileindex, repo,
9061 schedule_addition, &saa, NULL, NULL, 1, 0);
9064 const struct got_error *
9065 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9066 struct got_worktree *worktree, struct got_fileindex *fileindex,
9067 got_worktree_delete_cb progress_cb, void *progress_arg)
9069 struct schedule_deletion_args sda;
9071 memset(&sda, 0, sizeof(sda));
9072 sda.worktree = worktree;
9073 sda.fileindex = fileindex;
9074 sda.progress_cb = progress_cb;
9075 sda.progress_arg = progress_arg;
9077 sda.delete_local_mods = 0;
9078 sda.keep_on_disk = 0;
9079 sda.ignore_missing_paths = 0;
9080 sda.status_codes = NULL;
9082 return worktree_status(worktree, path, fileindex, repo,
9083 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9086 const struct got_error *
9087 got_worktree_patch_complete(struct got_fileindex *fileindex,
9088 const char *fileindex_path)
9090 const struct got_error *err = NULL;
9092 err = sync_fileindex(fileindex, fileindex_path);
9093 got_fileindex_free(fileindex);