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>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t apply_umask(mode_t);
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), apply_umask(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), apply_umask(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;
1131 apply_umask(mode_t mode)
1140 /* forward declaration */
1141 static const struct got_error *
1142 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1143 const char *path, mode_t te_mode, mode_t st_mode,
1144 struct got_blob_object *blob, int restoring_missing_file,
1145 int reverting_versioned_file, int installing_bad_symlink,
1146 int path_is_unversioned, struct got_repository *repo,
1147 got_worktree_checkout_cb progress_cb, void *progress_arg);
1150 * This function assumes that the provided symlink target points at a
1151 * safe location in the work tree!
1153 static const struct got_error *
1154 replace_existing_symlink(int *did_something, const char *ondisk_path,
1155 const char *target_path, size_t target_len)
1157 const struct got_error *err = NULL;
1159 char etarget[PATH_MAX];
1165 * "Bad" symlinks (those pointing outside the work tree or into the
1166 * .got directory) are installed in the work tree as a regular file
1167 * which contains the bad symlink target path.
1168 * The new symlink target has already been checked for safety by our
1169 * caller. If we can successfully open a regular file then we simply
1170 * replace this file with a symlink below.
1172 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 if (!got_err_open_nofollow_on_symlink())
1175 return got_error_from_errno2("open", ondisk_path);
1177 /* We are updating an existing on-disk symlink. */
1178 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 return got_error_from_errno2("readlink", ondisk_path);
1182 if (elen == target_len &&
1183 memcmp(etarget, target_path, target_len) == 0)
1184 return NULL; /* nothing to do */
1188 err = update_symlink(ondisk_path, target_path, target_len);
1189 if (fd != -1 && close(fd) == -1 && err == NULL)
1190 err = got_error_from_errno2("close", ondisk_path);
1194 static const struct got_error *
1195 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1196 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1198 const struct got_error *err = NULL;
1199 char canonpath[PATH_MAX];
1200 char *path_got = NULL;
1202 *is_bad_symlink = 0;
1204 if (target_len >= sizeof(canonpath)) {
1205 *is_bad_symlink = 1;
1210 * We do not use realpath(3) to resolve the symlink's target
1211 * path because we don't want to resolve symlinks recursively.
1212 * Instead we make the path absolute and then canonicalize it.
1213 * Relative symlink target lookup should begin at the directory
1214 * in which the blob object is being installed.
1216 if (!got_path_is_absolute(target_path)) {
1217 char *abspath, *parent;
1218 err = got_path_dirname(&parent, ondisk_path);
1221 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 return got_error_from_errno("asprintf");
1226 if (strlen(abspath) >= sizeof(canonpath)) {
1227 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1231 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1236 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1241 /* Only allow symlinks pointing at paths within the work tree. */
1242 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1243 *is_bad_symlink = 1;
1247 /* Do not allow symlinks pointing into the .got directory. */
1248 if (asprintf(&path_got, "%s/%s", wtroot_path,
1249 GOT_WORKTREE_GOT_DIR) == -1)
1250 return got_error_from_errno("asprintf");
1251 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1252 *is_bad_symlink = 1;
1258 static const struct got_error *
1259 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1260 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1261 int restoring_missing_file, int reverting_versioned_file,
1262 int path_is_unversioned, int allow_bad_symlinks,
1263 struct got_repository *repo,
1264 got_worktree_checkout_cb progress_cb, void *progress_arg)
1266 const struct got_error *err = NULL;
1267 char target_path[PATH_MAX];
1268 size_t len, target_len = 0;
1269 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1270 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1272 *is_bad_symlink = 0;
1275 * Blob object content specifies the target path of the link.
1276 * If a symbolic link cannot be installed we instead create
1277 * a regular file which contains the link target path stored
1278 * in the blob object.
1281 err = got_object_blob_read_block(&len, blob);
1285 if (len + target_len >= sizeof(target_path)) {
1286 /* Path too long; install as a regular file. */
1287 *is_bad_symlink = 1;
1288 got_object_blob_rewind(blob);
1289 return install_blob(worktree, ondisk_path, path,
1290 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1291 restoring_missing_file, reverting_versioned_file,
1292 1, path_is_unversioned, repo, progress_cb,
1296 /* Skip blob object header first time around. */
1297 memcpy(target_path + target_len, buf + hdrlen,
1299 target_len += len - hdrlen;
1303 target_path[target_len] = '\0';
1305 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1306 ondisk_path, worktree->root_path);
1310 if (*is_bad_symlink && !allow_bad_symlinks) {
1311 /* install as a regular file */
1312 got_object_blob_rewind(blob);
1313 err = install_blob(worktree, ondisk_path, path,
1314 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1315 restoring_missing_file, reverting_versioned_file, 1,
1316 path_is_unversioned, repo, progress_cb, progress_arg);
1320 if (symlink(target_path, ondisk_path) == -1) {
1321 if (errno == EEXIST) {
1322 int symlink_replaced;
1323 if (path_is_unversioned) {
1324 err = (*progress_cb)(progress_arg,
1325 GOT_STATUS_UNVERSIONED, path);
1328 err = replace_existing_symlink(&symlink_replaced,
1329 ondisk_path, target_path, target_len);
1333 if (symlink_replaced) {
1334 err = (*progress_cb)(progress_arg,
1335 reverting_versioned_file ?
1337 GOT_STATUS_UPDATE, path);
1339 err = (*progress_cb)(progress_arg,
1340 GOT_STATUS_EXISTS, path);
1343 return err; /* Nothing else to do. */
1346 if (errno == ENOENT) {
1348 err = got_path_dirname(&parent, ondisk_path);
1351 err = add_dir_on_disk(worktree, parent);
1356 * Retry, and fall through to error handling
1357 * below if this second attempt fails.
1359 if (symlink(target_path, ondisk_path) != -1) {
1360 err = NULL; /* success */
1365 /* Handle errors from first or second creation attempt. */
1366 if (errno == ENAMETOOLONG) {
1367 /* bad target path; install as a regular file */
1368 *is_bad_symlink = 1;
1369 got_object_blob_rewind(blob);
1370 err = install_blob(worktree, ondisk_path, path,
1371 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1372 restoring_missing_file, reverting_versioned_file, 1,
1373 path_is_unversioned, repo,
1374 progress_cb, progress_arg);
1375 } else if (errno == ENOTDIR) {
1376 err = got_error_path(ondisk_path,
1377 GOT_ERR_FILE_OBSTRUCTED);
1379 err = got_error_from_errno3("symlink",
1380 target_path, ondisk_path);
1382 } else if (progress_cb)
1383 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1384 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1388 static const struct got_error *
1389 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1390 const char *path, mode_t te_mode, mode_t st_mode,
1391 struct got_blob_object *blob, int restoring_missing_file,
1392 int reverting_versioned_file, int installing_bad_symlink,
1393 int path_is_unversioned, struct got_repository *repo,
1394 got_worktree_checkout_cb progress_cb, void *progress_arg)
1396 const struct got_error *err = NULL;
1400 char *tmppath = NULL;
1403 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1404 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1407 if (errno == ENOENT) {
1409 err = got_path_dirname(&parent, path);
1412 err = add_dir_on_disk(worktree, parent);
1416 fd = open(ondisk_path,
1417 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1420 return got_error_from_errno2("open",
1422 } else if (errno == EEXIST) {
1423 if (path_is_unversioned) {
1424 err = (*progress_cb)(progress_arg,
1425 GOT_STATUS_UNVERSIONED, path);
1428 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1429 !S_ISREG(st_mode) && !installing_bad_symlink) {
1430 /* TODO file is obstructed; do something */
1431 err = got_error_path(ondisk_path,
1432 GOT_ERR_FILE_OBSTRUCTED);
1435 err = got_opentemp_named_fd(&tmppath, &fd,
1441 if (fchmod(fd, apply_umask(mode)) == -1) {
1442 err = got_error_from_errno2("fchmod",
1448 return got_error_from_errno2("open", ondisk_path);
1452 if (restoring_missing_file)
1453 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1455 else if (reverting_versioned_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1459 err = (*progress_cb)(progress_arg,
1460 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1465 hdrlen = got_object_blob_get_hdrlen(blob);
1467 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1468 err = got_object_blob_read_block(&len, blob);
1472 /* Skip blob object header first time around. */
1473 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 err = got_error_from_errno("write");
1477 } else if (outlen != len - hdrlen) {
1478 err = got_error(GOT_ERR_IO);
1485 if (fsync(fd) != 0) {
1486 err = got_error_from_errno("fsync");
1491 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1492 err = got_error_from_errno2("unlink", ondisk_path);
1495 if (rename(tmppath, ondisk_path) != 0) {
1496 err = got_error_from_errno3("rename", tmppath,
1505 if (fd != -1 && close(fd) == -1 && err == NULL)
1506 err = got_error_from_errno("close");
1507 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1508 err = got_error_from_errno2("unlink", tmppath);
1513 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1514 static const struct got_error *
1515 get_modified_file_content_status(unsigned char *status, FILE *f)
1517 const struct got_error *err = NULL;
1518 const char *markers[3] = {
1519 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1520 GOT_DIFF_CONFLICT_MARKER_SEP,
1521 GOT_DIFF_CONFLICT_MARKER_END
1525 size_t linesize = 0;
1528 while (*status == GOT_STATUS_MODIFY) {
1529 linelen = getline(&line, &linesize, f);
1530 if (linelen == -1) {
1533 err = got_ferror(f, GOT_ERR_IO);
1537 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1538 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1540 *status = GOT_STATUS_CONFLICT;
1551 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1553 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1554 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1558 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1560 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1561 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1562 ie->mtime_sec == sb->st_mtim.tv_sec &&
1563 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1564 ie->size == (sb->st_size & 0xffffffff) &&
1565 !xbit_differs(ie, sb->st_mode));
1568 static unsigned char
1569 get_staged_status(struct got_fileindex_entry *ie)
1571 switch (got_fileindex_entry_stage_get(ie)) {
1572 case GOT_FILEIDX_STAGE_ADD:
1573 return GOT_STATUS_ADD;
1574 case GOT_FILEIDX_STAGE_DELETE:
1575 return GOT_STATUS_DELETE;
1576 case GOT_FILEIDX_STAGE_MODIFY:
1577 return GOT_STATUS_MODIFY;
1579 return GOT_STATUS_NO_CHANGE;
1583 static const struct got_error *
1584 get_symlink_modification_status(unsigned char *status,
1585 struct got_fileindex_entry *ie, const char *abspath,
1586 int dirfd, const char *de_name, struct got_blob_object *blob)
1588 const struct got_error *err = NULL;
1589 char target_path[PATH_MAX];
1590 char etarget[PATH_MAX];
1592 size_t len, target_len = 0;
1593 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1594 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1596 *status = GOT_STATUS_NO_CHANGE;
1598 /* Blob object content specifies the target path of the link. */
1600 err = got_object_blob_read_block(&len, blob);
1603 if (len + target_len >= sizeof(target_path)) {
1605 * Should not happen. The blob contents were OK
1606 * when this symlink was installed.
1608 return got_error(GOT_ERR_NO_SPACE);
1611 /* Skip blob object header first time around. */
1612 memcpy(target_path + target_len, buf + hdrlen,
1614 target_len += len - hdrlen;
1618 target_path[target_len] = '\0';
1621 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1623 return got_error_from_errno2("readlinkat", abspath);
1625 elen = readlink(abspath, etarget, sizeof(etarget));
1627 return got_error_from_errno2("readlink", abspath);
1630 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1631 *status = GOT_STATUS_MODIFY;
1636 static const struct got_error *
1637 get_file_status(unsigned char *status, struct stat *sb,
1638 struct got_fileindex_entry *ie, const char *abspath,
1639 int dirfd, const char *de_name, struct got_repository *repo)
1641 const struct got_error *err = NULL;
1642 struct got_object_id id;
1644 int fd = -1, fd1 = -1;
1647 struct got_blob_object *blob = NULL;
1649 unsigned char staged_status = get_staged_status(ie);
1651 *status = GOT_STATUS_NO_CHANGE;
1652 memset(sb, 0, sizeof(*sb));
1655 * Whenever the caller provides a directory descriptor and a
1656 * directory entry name for the file, use them! This prevents
1657 * race conditions if filesystem paths change beneath our feet.
1660 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1661 if (errno == ENOENT) {
1662 if (got_fileindex_entry_has_file_on_disk(ie))
1663 *status = GOT_STATUS_MISSING;
1665 *status = GOT_STATUS_DELETE;
1668 err = got_error_from_errno2("fstatat", abspath);
1672 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1673 if (fd == -1 && errno != ENOENT &&
1674 !got_err_open_nofollow_on_symlink())
1675 return got_error_from_errno2("open", abspath);
1676 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1677 if (lstat(abspath, sb) == -1)
1678 return got_error_from_errno2("lstat", abspath);
1679 } else if (fd == -1 || fstat(fd, sb) == -1) {
1680 if (errno == ENOENT) {
1681 if (got_fileindex_entry_has_file_on_disk(ie))
1682 *status = GOT_STATUS_MISSING;
1684 *status = GOT_STATUS_DELETE;
1687 err = got_error_from_errno2("fstat", abspath);
1692 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1693 *status = GOT_STATUS_OBSTRUCTED;
1697 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1698 *status = GOT_STATUS_DELETE;
1700 } else if (!got_fileindex_entry_has_blob(ie) &&
1701 staged_status != GOT_STATUS_ADD) {
1702 *status = GOT_STATUS_ADD;
1706 if (!stat_info_differs(ie, sb))
1709 if (S_ISLNK(sb->st_mode) &&
1710 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1711 *status = GOT_STATUS_MODIFY;
1715 if (staged_status == GOT_STATUS_MODIFY ||
1716 staged_status == GOT_STATUS_ADD)
1717 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1719 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1721 fd1 = got_opentempfd();
1723 err = got_error_from_errno("got_opentempfd");
1726 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1730 if (S_ISLNK(sb->st_mode)) {
1731 err = get_symlink_modification_status(status, ie,
1732 abspath, dirfd, de_name, blob);
1737 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1739 err = got_error_from_errno2("openat", abspath);
1744 f = fdopen(fd, "r");
1746 err = got_error_from_errno2("fdopen", abspath);
1750 hdrlen = got_object_blob_get_hdrlen(blob);
1752 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1753 err = got_object_blob_read_block(&blen, blob);
1756 /* Skip length of blob object header first time around. */
1757 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1758 if (flen == 0 && ferror(f)) {
1759 err = got_error_from_errno("fread");
1762 if (blen - hdrlen == 0) {
1764 *status = GOT_STATUS_MODIFY;
1766 } else if (flen == 0) {
1767 if (blen - hdrlen != 0)
1768 *status = GOT_STATUS_MODIFY;
1770 } else if (blen - hdrlen == flen) {
1771 /* Skip blob object header first time around. */
1772 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1773 *status = GOT_STATUS_MODIFY;
1777 *status = GOT_STATUS_MODIFY;
1783 if (*status == GOT_STATUS_MODIFY) {
1785 err = get_modified_file_content_status(status, f);
1786 } else if (xbit_differs(ie, sb->st_mode))
1787 *status = GOT_STATUS_MODE_CHANGE;
1789 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1790 err = got_error_from_errno("close");
1792 got_object_blob_close(blob);
1793 if (f != NULL && fclose(f) == EOF && err == NULL)
1794 err = got_error_from_errno2("fclose", abspath);
1795 if (fd != -1 && close(fd) == -1 && err == NULL)
1796 err = got_error_from_errno2("close", abspath);
1801 * Update timestamps in the file index if a file is unmodified and
1802 * we had to run a full content comparison to find out.
1804 static const struct got_error *
1805 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1806 struct got_fileindex_entry *ie, struct stat *sb)
1808 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1809 return got_fileindex_entry_update(ie, wt_fd, path,
1810 ie->blob_sha1, ie->commit_sha1, 1);
1815 static const struct got_error *
1816 update_blob(struct got_worktree *worktree,
1817 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1818 struct got_tree_entry *te, const char *path,
1819 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1822 const struct got_error *err = NULL;
1823 struct got_blob_object *blob = NULL;
1824 char *ondisk_path = NULL;
1825 unsigned char status = GOT_STATUS_NO_CHANGE;
1827 int fd1 = -1, fd2 = -1;
1829 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1830 return got_error_from_errno("asprintf");
1833 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1834 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1837 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1841 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1842 sb.st_mode = got_fileindex_perms_to_st(ie);
1844 if (stat(ondisk_path, &sb) == -1) {
1845 if (errno != ENOENT) {
1846 err = got_error_from_errno2("stat",
1850 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1851 status = GOT_STATUS_UNVERSIONED;
1853 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1854 status = GOT_STATUS_UNVERSIONED;
1856 status = GOT_STATUS_OBSTRUCTED;
1860 if (status == GOT_STATUS_OBSTRUCTED) {
1862 got_fileindex_entry_mark_skipped(ie);
1863 err = (*progress_cb)(progress_arg, status, path);
1866 if (status == GOT_STATUS_CONFLICT) {
1868 got_fileindex_entry_mark_skipped(ie);
1869 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1874 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1875 (S_ISLNK(te->mode) ||
1876 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1878 * This is a regular file or an installed bad symlink.
1879 * If the file index indicates that this file is already
1880 * up-to-date with respect to the repository we can skip
1881 * updating contents of this file.
1883 if (got_fileindex_entry_has_commit(ie) &&
1884 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1885 SHA1_DIGEST_LENGTH) == 0) {
1887 err = sync_timestamps(worktree->root_fd,
1888 path, status, ie, &sb);
1891 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1895 if (got_fileindex_entry_has_blob(ie) &&
1896 memcmp(ie->blob_sha1, te->id.sha1,
1897 SHA1_DIGEST_LENGTH) == 0) {
1898 /* Different commit but the same blob. */
1899 err = sync_timestamps(worktree->root_fd,
1900 path, status, ie, &sb);
1903 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1909 fd1 = got_opentempfd();
1911 err = got_error_from_errno("got_opentempfd");
1914 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1918 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1919 int update_timestamps;
1920 struct got_blob_object *blob2 = NULL;
1921 char *label_orig = NULL;
1922 if (got_fileindex_entry_has_blob(ie)) {
1923 fd2 = got_opentempfd();
1925 err = got_error_from_errno("got_opentempfd");
1928 struct got_object_id id2;
1929 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1930 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1935 if (got_fileindex_entry_has_commit(ie)) {
1936 char id_str[SHA1_DIGEST_STRING_LENGTH];
1937 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1938 sizeof(id_str)) == NULL) {
1939 err = got_error_path(id_str,
1940 GOT_ERR_BAD_OBJ_ID_STR);
1943 if (asprintf(&label_orig, "%s: commit %s",
1944 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1945 err = got_error_from_errno("asprintf");
1949 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1951 err = got_object_blob_read_to_str(&link_target, blob);
1954 err = merge_symlink(worktree, blob2, ondisk_path, path,
1955 label_orig, link_target, worktree->base_commit_id,
1956 repo, progress_cb, progress_arg);
1959 err = merge_blob(&update_timestamps, worktree, blob2,
1960 ondisk_path, path, sb.st_mode, label_orig, blob,
1961 worktree->base_commit_id, repo,
1962 progress_cb, progress_arg);
1965 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1966 err = got_error_from_errno("close");
1970 got_object_blob_close(blob2);
1974 * Do not update timestamps of files with local changes.
1975 * Otherwise, a future status walk would treat them as
1976 * unmodified files again.
1978 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1979 blob->id.sha1, worktree->base_commit_id->sha1,
1981 } else if (status == GOT_STATUS_MODE_CHANGE) {
1982 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1983 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1984 } else if (status == GOT_STATUS_DELETE) {
1985 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1988 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1989 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1993 int is_bad_symlink = 0;
1994 if (S_ISLNK(te->mode)) {
1995 err = install_symlink(&is_bad_symlink, worktree,
1996 ondisk_path, path, blob,
1997 status == GOT_STATUS_MISSING, 0,
1998 status == GOT_STATUS_UNVERSIONED, 0,
1999 repo, progress_cb, progress_arg);
2001 err = install_blob(worktree, ondisk_path, path,
2002 te->mode, sb.st_mode, blob,
2003 status == GOT_STATUS_MISSING, 0, 0,
2004 status == GOT_STATUS_UNVERSIONED, repo,
2005 progress_cb, progress_arg);
2011 err = got_fileindex_entry_update(ie,
2012 worktree->root_fd, path, blob->id.sha1,
2013 worktree->base_commit_id->sha1, 1);
2015 err = create_fileindex_entry(&ie, fileindex,
2016 worktree->base_commit_id, worktree->root_fd, path,
2022 if (is_bad_symlink) {
2023 got_fileindex_entry_filetype_set(ie,
2024 GOT_FILEIDX_MODE_BAD_SYMLINK);
2028 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2029 err = got_error_from_errno("close");
2032 got_object_blob_close(blob);
2038 static const struct got_error *
2039 remove_ondisk_file(const char *root_path, const char *path)
2041 const struct got_error *err = NULL;
2042 char *ondisk_path = NULL, *parent = NULL;
2044 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2045 return got_error_from_errno("asprintf");
2047 if (unlink(ondisk_path) == -1) {
2048 if (errno != ENOENT)
2049 err = got_error_from_errno2("unlink", ondisk_path);
2051 size_t root_len = strlen(root_path);
2052 err = got_path_dirname(&parent, ondisk_path);
2055 while (got_path_cmp(parent, root_path,
2056 strlen(parent), root_len) != 0) {
2058 ondisk_path = parent;
2060 if (rmdir(ondisk_path) == -1) {
2061 if (errno != ENOTEMPTY)
2062 err = got_error_from_errno2("rmdir",
2066 err = got_path_dirname(&parent, ondisk_path);
2077 static const struct got_error *
2078 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2079 struct got_fileindex_entry *ie, struct got_repository *repo,
2080 got_worktree_checkout_cb progress_cb, void *progress_arg)
2082 const struct got_error *err = NULL;
2083 unsigned char status;
2087 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2088 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2090 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2092 return got_error_from_errno("asprintf");
2094 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2098 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2099 char ondisk_target[PATH_MAX];
2100 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2101 sizeof(ondisk_target));
2102 if (ondisk_len == -1) {
2103 err = got_error_from_errno2("readlink", ondisk_path);
2106 ondisk_target[ondisk_len] = '\0';
2107 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2108 NULL, NULL, /* XXX pass common ancestor info? */
2109 ondisk_target, ondisk_path);
2112 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2117 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2118 status == GOT_STATUS_ADD) {
2119 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2123 * Preserve the working file and change the deleted blob's
2124 * entry into a schedule-add entry.
2126 err = got_fileindex_entry_update(ie, worktree->root_fd,
2127 ie->path, NULL, NULL, 0);
2129 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2132 if (status == GOT_STATUS_NO_CHANGE) {
2133 err = remove_ondisk_file(worktree->root_path, ie->path);
2137 got_fileindex_entry_remove(fileindex, ie);
2144 struct diff_cb_arg {
2145 struct got_fileindex *fileindex;
2146 struct got_worktree *worktree;
2147 struct got_repository *repo;
2148 got_worktree_checkout_cb progress_cb;
2150 got_cancel_cb cancel_cb;
2154 static const struct got_error *
2155 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2156 struct got_tree_entry *te, 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 update_blob(a->worktree, a->fileindex, ie, te,
2164 ie->path, a->repo, a->progress_cb, a->progress_arg);
2167 static const struct got_error *
2168 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2170 struct diff_cb_arg *a = arg;
2172 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2173 return got_error(GOT_ERR_CANCELLED);
2175 return delete_blob(a->worktree, a->fileindex, ie,
2176 a->repo, a->progress_cb, a->progress_arg);
2179 static const struct got_error *
2180 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2182 struct diff_cb_arg *a = arg;
2183 const struct got_error *err;
2186 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2187 return got_error(GOT_ERR_CANCELLED);
2189 if (got_object_tree_entry_is_submodule(te))
2192 if (asprintf(&path, "%s%s%s", parent_path,
2193 parent_path[0] ? "/" : "", te->name)
2195 return got_error_from_errno("asprintf");
2197 if (S_ISDIR(te->mode))
2198 err = add_dir_on_disk(a->worktree, path);
2200 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2201 a->repo, a->progress_cb, a->progress_arg);
2207 const struct got_error *
2208 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2210 uint32_t uuid_status;
2212 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2213 if (uuid_status != uuid_s_ok) {
2215 return got_error_uuid(uuid_status, "uuid_to_string");
2221 static const struct got_error *
2222 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2224 const struct got_error *err = NULL;
2225 char *uuidstr = NULL;
2229 err = got_worktree_get_uuid(&uuidstr, worktree);
2233 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2234 err = got_error_from_errno("asprintf");
2241 const struct got_error *
2242 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2244 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2247 static const struct got_error *
2248 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2250 return get_ref_name(refname, worktree,
2251 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2254 static const struct got_error *
2255 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2257 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2260 static const struct got_error *
2261 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2263 return get_ref_name(refname, worktree,
2264 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2267 static const struct got_error *
2268 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2270 return get_ref_name(refname, worktree,
2271 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2274 static const struct got_error *
2275 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2277 return get_ref_name(refname, worktree,
2278 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2281 static const struct got_error *
2282 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2284 return get_ref_name(refname, worktree,
2285 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2288 static const struct got_error *
2289 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2291 return get_ref_name(refname, worktree,
2292 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2295 static const struct got_error *
2296 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2298 return get_ref_name(refname, worktree,
2299 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2302 const struct got_error *
2303 got_worktree_get_histedit_script_path(char **path,
2304 struct got_worktree *worktree)
2306 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2307 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2309 return got_error_from_errno("asprintf");
2314 static const struct got_error *
2315 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2317 return get_ref_name(refname, worktree,
2318 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2321 static const struct got_error *
2322 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree,
2325 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2329 * Prevent Git's garbage collector from deleting our base commit by
2330 * setting a reference to our base commit's ID.
2332 static const struct got_error *
2333 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2335 const struct got_error *err = NULL;
2336 struct got_reference *ref = NULL;
2339 err = got_worktree_get_base_ref_name(&refname, worktree);
2343 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2347 err = got_ref_write(ref, repo);
2355 static const struct got_error *
2356 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2358 const struct got_error *err = NULL;
2360 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2361 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2362 err = got_error_from_errno("asprintf");
2363 *fileindex_path = NULL;
2369 static const struct got_error *
2370 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2371 struct got_worktree *worktree)
2373 const struct got_error *err = NULL;
2376 *fileindex_path = NULL;
2377 *fileindex = got_fileindex_alloc();
2378 if (*fileindex == NULL)
2379 return got_error_from_errno("got_fileindex_alloc");
2381 err = get_fileindex_path(fileindex_path, worktree);
2385 index = fopen(*fileindex_path, "rbe");
2386 if (index == NULL) {
2387 if (errno != ENOENT)
2388 err = got_error_from_errno2("fopen", *fileindex_path);
2390 err = got_fileindex_read(*fileindex, index);
2391 if (fclose(index) == EOF && err == NULL)
2392 err = got_error_from_errno("fclose");
2396 free(*fileindex_path);
2397 *fileindex_path = NULL;
2398 got_fileindex_free(*fileindex);
2404 struct bump_base_commit_id_arg {
2405 struct got_object_id *base_commit_id;
2408 const char *entry_name;
2409 got_worktree_checkout_cb progress_cb;
2413 /* Bump base commit ID of all files within an updated part of the work tree. */
2414 static const struct got_error *
2415 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2417 const struct got_error *err;
2418 struct bump_base_commit_id_arg *a = arg;
2420 if (a->entry_name) {
2421 if (strcmp(ie->path, a->path) != 0)
2423 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2426 if (got_fileindex_entry_was_skipped(ie))
2429 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2430 SHA1_DIGEST_LENGTH) == 0)
2433 if (a->progress_cb) {
2434 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2439 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2443 static const struct got_error *
2444 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2445 struct got_fileindex *fileindex,
2446 got_worktree_checkout_cb progress_cb, void *progress_arg)
2448 struct bump_base_commit_id_arg bbc_arg;
2450 bbc_arg.base_commit_id = worktree->base_commit_id;
2451 bbc_arg.entry_name = NULL;
2453 bbc_arg.path_len = 0;
2454 bbc_arg.progress_cb = progress_cb;
2455 bbc_arg.progress_arg = progress_arg;
2457 return got_fileindex_for_each_entry_safe(fileindex,
2458 bump_base_commit_id, &bbc_arg);
2461 static const struct got_error *
2462 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2464 const struct got_error *err = NULL;
2465 char *new_fileindex_path = NULL;
2466 FILE *new_index = NULL;
2467 struct timespec timeout;
2469 err = got_opentemp_named(&new_fileindex_path, &new_index,
2474 err = got_fileindex_write(fileindex, new_index);
2478 if (rename(new_fileindex_path, fileindex_path) != 0) {
2479 err = got_error_from_errno3("rename", new_fileindex_path,
2481 unlink(new_fileindex_path);
2485 * Sleep for a short amount of time to ensure that files modified after
2486 * this program exits have a different time stamp from the one which
2487 * was recorded in the file index.
2490 timeout.tv_nsec = 1;
2491 nanosleep(&timeout, NULL);
2495 free(new_fileindex_path);
2499 static const struct got_error *
2500 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2501 struct got_object_id **tree_id, const char *wt_relpath,
2502 struct got_commit_object *base_commit, struct got_worktree *worktree,
2503 struct got_repository *repo)
2505 const struct got_error *err = NULL;
2506 struct got_object_id *id = NULL;
2507 char *in_repo_path = NULL;
2508 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2510 *entry_type = GOT_OBJ_TYPE_ANY;
2511 *tree_relpath = NULL;
2514 if (wt_relpath[0] == '\0') {
2515 /* Check out all files within the work tree. */
2516 *entry_type = GOT_OBJ_TYPE_TREE;
2517 *tree_relpath = strdup("");
2518 if (*tree_relpath == NULL) {
2519 err = got_error_from_errno("strdup");
2522 err = got_object_id_by_path(tree_id, repo, base_commit,
2523 worktree->path_prefix);
2529 /* Check out a subset of files in the work tree. */
2531 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2532 is_root_wt ? "" : "/", wt_relpath) == -1) {
2533 err = got_error_from_errno("asprintf");
2537 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2542 in_repo_path = NULL;
2544 err = got_object_get_type(entry_type, repo, id);
2548 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2549 /* Check out a single file. */
2550 if (strchr(wt_relpath, '/') == NULL) {
2551 /* Check out a single file in work tree's root dir. */
2552 in_repo_path = strdup(worktree->path_prefix);
2553 if (in_repo_path == NULL) {
2554 err = got_error_from_errno("strdup");
2557 *tree_relpath = strdup("");
2558 if (*tree_relpath == NULL) {
2559 err = got_error_from_errno("strdup");
2563 /* Check out a single file in a subdirectory. */
2564 err = got_path_dirname(tree_relpath, wt_relpath);
2567 if (asprintf(&in_repo_path, "%s%s%s",
2568 worktree->path_prefix, is_root_wt ? "" : "/",
2569 *tree_relpath) == -1) {
2570 err = got_error_from_errno("asprintf");
2574 err = got_object_id_by_path(tree_id, repo,
2575 base_commit, in_repo_path);
2577 /* Check out all files within a subdirectory. */
2578 *tree_id = got_object_id_dup(id);
2579 if (*tree_id == NULL) {
2580 err = got_error_from_errno("got_object_id_dup");
2583 *tree_relpath = strdup(wt_relpath);
2584 if (*tree_relpath == NULL) {
2585 err = got_error_from_errno("strdup");
2593 *entry_type = GOT_OBJ_TYPE_ANY;
2594 free(*tree_relpath);
2595 *tree_relpath = NULL;
2602 static const struct got_error *
2603 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2604 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2605 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2606 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2608 const struct got_error *err = NULL;
2609 struct got_commit_object *commit = NULL;
2610 struct got_tree_object *tree = NULL;
2611 struct got_fileindex_diff_tree_cb diff_cb;
2612 struct diff_cb_arg arg;
2614 err = ref_base_commit(worktree, repo);
2616 if (!(err->code == GOT_ERR_ERRNO &&
2617 (errno == EACCES || errno == EROFS)))
2619 err = (*progress_cb)(progress_arg,
2620 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2625 err = got_object_open_as_commit(&commit, repo,
2626 worktree->base_commit_id);
2630 err = got_object_open_as_tree(&tree, repo, tree_id);
2635 got_object_tree_find_entry(tree, entry_name) == NULL) {
2636 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2640 diff_cb.diff_old_new = diff_old_new;
2641 diff_cb.diff_old = diff_old;
2642 diff_cb.diff_new = diff_new;
2643 arg.fileindex = fileindex;
2644 arg.worktree = worktree;
2646 arg.progress_cb = progress_cb;
2647 arg.progress_arg = progress_arg;
2648 arg.cancel_cb = cancel_cb;
2649 arg.cancel_arg = cancel_arg;
2650 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2651 entry_name, repo, &diff_cb, &arg);
2654 got_object_tree_close(tree);
2656 got_object_commit_close(commit);
2660 const struct got_error *
2661 got_worktree_checkout_files(struct got_worktree *worktree,
2662 struct got_pathlist_head *paths, struct got_repository *repo,
2663 got_worktree_checkout_cb progress_cb, void *progress_arg,
2664 got_cancel_cb cancel_cb, void *cancel_arg)
2666 const struct got_error *err = NULL, *sync_err, *unlockerr;
2667 struct got_commit_object *commit = NULL;
2668 struct got_tree_object *tree = NULL;
2669 struct got_fileindex *fileindex = NULL;
2670 char *fileindex_path = NULL;
2671 struct got_pathlist_entry *pe;
2672 struct tree_path_data {
2673 STAILQ_ENTRY(tree_path_data) entry;
2674 struct got_object_id *tree_id;
2679 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2681 STAILQ_INIT(&tree_paths);
2683 err = lock_worktree(worktree, LOCK_EX);
2687 err = got_object_open_as_commit(&commit, repo,
2688 worktree->base_commit_id);
2692 /* Map all specified paths to in-repository trees. */
2693 TAILQ_FOREACH(pe, paths, entry) {
2694 tpd = malloc(sizeof(*tpd));
2696 err = got_error_from_errno("malloc");
2700 err = find_tree_entry_for_checkout(&tpd->entry_type,
2701 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2708 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2709 err = got_path_basename(&tpd->entry_name, pe->path);
2717 tpd->entry_name = NULL;
2719 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2723 * Read the file index.
2724 * Checking out files is supposed to be an idempotent operation.
2725 * If the on-disk file index is incomplete we will try to complete it.
2727 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2731 tpd = STAILQ_FIRST(&tree_paths);
2732 TAILQ_FOREACH(pe, paths, entry) {
2733 struct bump_base_commit_id_arg bbc_arg;
2735 err = checkout_files(worktree, fileindex, tpd->relpath,
2736 tpd->tree_id, tpd->entry_name, repo,
2737 progress_cb, progress_arg, cancel_cb, cancel_arg);
2741 bbc_arg.base_commit_id = worktree->base_commit_id;
2742 bbc_arg.entry_name = tpd->entry_name;
2743 bbc_arg.path = pe->path;
2744 bbc_arg.path_len = pe->path_len;
2745 bbc_arg.progress_cb = progress_cb;
2746 bbc_arg.progress_arg = progress_arg;
2747 err = got_fileindex_for_each_entry_safe(fileindex,
2748 bump_base_commit_id, &bbc_arg);
2752 tpd = STAILQ_NEXT(tpd, entry);
2754 sync_err = sync_fileindex(fileindex, fileindex_path);
2755 if (sync_err && err == NULL)
2758 free(fileindex_path);
2760 got_object_tree_close(tree);
2762 got_object_commit_close(commit);
2764 got_fileindex_free(fileindex);
2765 while (!STAILQ_EMPTY(&tree_paths)) {
2766 tpd = STAILQ_FIRST(&tree_paths);
2767 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2772 unlockerr = lock_worktree(worktree, LOCK_SH);
2773 if (unlockerr && err == NULL)
2778 struct merge_file_cb_arg {
2779 struct got_worktree *worktree;
2780 struct got_fileindex *fileindex;
2781 got_worktree_checkout_cb progress_cb;
2783 got_cancel_cb cancel_cb;
2785 const char *label_orig;
2786 struct got_object_id *commit_id2;
2787 int allow_bad_symlinks;
2790 static const struct got_error *
2791 merge_file_cb(void *arg, struct got_blob_object *blob1,
2792 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2793 struct got_object_id *id1, struct got_object_id *id2,
2794 const char *path1, const char *path2,
2795 mode_t mode1, mode_t mode2, struct got_repository *repo)
2797 static const struct got_error *err = NULL;
2798 struct merge_file_cb_arg *a = arg;
2799 struct got_fileindex_entry *ie;
2800 char *ondisk_path = NULL;
2802 unsigned char status;
2803 int local_changes_subsumed;
2804 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2805 char *id_str = NULL, *label_deriv2 = NULL;
2807 if (blob1 && blob2) {
2808 ie = got_fileindex_entry_get(a->fileindex, path2,
2811 return (*a->progress_cb)(a->progress_arg,
2812 GOT_STATUS_MISSING, path2);
2814 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2816 return got_error_from_errno("asprintf");
2818 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2823 if (status == GOT_STATUS_DELETE) {
2824 err = (*a->progress_cb)(a->progress_arg,
2825 GOT_STATUS_MERGE, path2);
2828 if (status != GOT_STATUS_NO_CHANGE &&
2829 status != GOT_STATUS_MODIFY &&
2830 status != GOT_STATUS_CONFLICT &&
2831 status != GOT_STATUS_ADD) {
2832 err = (*a->progress_cb)(a->progress_arg, status, path2);
2836 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2838 err = got_object_blob_read_to_str(&link_target2, blob2);
2841 err = merge_symlink(a->worktree, blob1, ondisk_path,
2842 path2, a->label_orig, link_target2, a->commit_id2,
2843 repo, a->progress_cb, a->progress_arg);
2848 f_orig = got_opentemp();
2849 if (f_orig == NULL) {
2850 err = got_error_from_errno("got_opentemp");
2853 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2858 f_deriv2 = got_opentemp();
2859 if (f_deriv2 == NULL)
2861 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2866 fd = open(ondisk_path,
2867 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2869 err = got_error_from_errno2("open",
2873 f_deriv = fdopen(fd, "r");
2874 if (f_deriv == NULL) {
2875 err = got_error_from_errno2("fdopen",
2880 err = got_object_id_str(&id_str, a->commit_id2);
2883 if (asprintf(&label_deriv2, "%s: commit %s",
2884 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2885 err = got_error_from_errno("asprintf");
2888 err = merge_file(&local_changes_subsumed, a->worktree,
2889 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2890 sb.st_mode, a->label_orig, NULL, label_deriv2,
2891 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2892 a->progress_cb, a->progress_arg);
2895 ie = got_fileindex_entry_get(a->fileindex, path1,
2898 return (*a->progress_cb)(a->progress_arg,
2899 GOT_STATUS_MISSING, path1);
2901 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2903 return got_error_from_errno("asprintf");
2905 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2911 case GOT_STATUS_NO_CHANGE:
2912 err = (*a->progress_cb)(a->progress_arg,
2913 GOT_STATUS_DELETE, path1);
2916 err = remove_ondisk_file(a->worktree->root_path, path1);
2920 got_fileindex_entry_mark_deleted_from_disk(ie);
2922 case GOT_STATUS_DELETE:
2923 case GOT_STATUS_MISSING:
2924 err = (*a->progress_cb)(a->progress_arg,
2925 GOT_STATUS_DELETE, path1);
2929 got_fileindex_entry_mark_deleted_from_disk(ie);
2931 case GOT_STATUS_ADD: {
2932 struct got_object_id *id;
2936 * Delete the added file only if its content already
2937 * exists in the repository.
2939 err = got_object_blob_file_create(&id, &blob1_f,
2940 &blob1_size, path1);
2943 if (got_object_id_cmp(id, id1) == 0) {
2944 err = (*a->progress_cb)(a->progress_arg,
2945 GOT_STATUS_DELETE, path1);
2948 err = remove_ondisk_file(a->worktree->root_path,
2953 got_fileindex_entry_remove(a->fileindex,
2956 err = (*a->progress_cb)(a->progress_arg,
2957 GOT_STATUS_CANNOT_DELETE, path1);
2959 if (fclose(blob1_f) == EOF && err == NULL)
2960 err = got_error_from_errno("fclose");
2966 case GOT_STATUS_MODIFY:
2967 case GOT_STATUS_CONFLICT:
2968 err = (*a->progress_cb)(a->progress_arg,
2969 GOT_STATUS_CANNOT_DELETE, path1);
2973 case GOT_STATUS_OBSTRUCTED:
2974 err = (*a->progress_cb)(a->progress_arg, status, path1);
2982 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2984 return got_error_from_errno("asprintf");
2985 ie = got_fileindex_entry_get(a->fileindex, path2,
2988 err = get_file_status(&status, &sb, ie, ondisk_path,
2992 if (status != GOT_STATUS_NO_CHANGE &&
2993 status != GOT_STATUS_MODIFY &&
2994 status != GOT_STATUS_CONFLICT &&
2995 status != GOT_STATUS_ADD) {
2996 err = (*a->progress_cb)(a->progress_arg,
3000 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3002 err = got_object_blob_read_to_str(&link_target2,
3006 err = merge_symlink(a->worktree, NULL,
3007 ondisk_path, path2, a->label_orig,
3008 link_target2, a->commit_id2, repo,
3009 a->progress_cb, a->progress_arg);
3011 } else if (S_ISREG(sb.st_mode)) {
3012 err = merge_blob(&local_changes_subsumed,
3013 a->worktree, NULL, ondisk_path, path2,
3014 sb.st_mode, a->label_orig, blob2,
3015 a->commit_id2, repo, a->progress_cb,
3018 err = got_error_path(ondisk_path,
3019 GOT_ERR_FILE_OBSTRUCTED);
3023 if (status == GOT_STATUS_DELETE) {
3024 err = got_fileindex_entry_update(ie,
3025 a->worktree->root_fd, path2, blob2->id.sha1,
3026 a->worktree->base_commit_id->sha1, 0);
3031 int is_bad_symlink = 0;
3032 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3033 if (S_ISLNK(mode2)) {
3034 err = install_symlink(&is_bad_symlink,
3035 a->worktree, ondisk_path, path2, blob2, 0,
3036 0, 1, a->allow_bad_symlinks, repo,
3037 a->progress_cb, a->progress_arg);
3039 err = install_blob(a->worktree, ondisk_path, path2,
3040 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3041 a->progress_cb, a->progress_arg);
3045 err = got_fileindex_entry_alloc(&ie, path2);
3048 err = got_fileindex_entry_update(ie,
3049 a->worktree->root_fd, path2, NULL, NULL, 1);
3051 got_fileindex_entry_free(ie);
3054 err = got_fileindex_entry_add(a->fileindex, ie);
3056 got_fileindex_entry_free(ie);
3059 if (is_bad_symlink) {
3060 got_fileindex_entry_filetype_set(ie,
3061 GOT_FILEIDX_MODE_BAD_SYMLINK);
3066 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3067 err = got_error_from_errno("fclose");
3068 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3069 err = got_error_from_errno("fclose");
3070 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3071 err = got_error_from_errno("fclose");
3078 static const struct got_error *
3079 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3081 struct got_worktree *worktree = arg;
3083 /* Reject merges into a work tree with mixed base commits. */
3084 if (got_fileindex_entry_has_commit(ie) &&
3085 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3086 SHA1_DIGEST_LENGTH) != 0)
3087 return got_error(GOT_ERR_MIXED_COMMITS);
3092 struct check_merge_conflicts_arg {
3093 struct got_worktree *worktree;
3094 struct got_fileindex *fileindex;
3095 struct got_repository *repo;
3098 static const struct got_error *
3099 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3100 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3101 struct got_object_id *id1, struct got_object_id *id2,
3102 const char *path1, const char *path2,
3103 mode_t mode1, mode_t mode2, struct got_repository *repo)
3105 const struct got_error *err = NULL;
3106 struct check_merge_conflicts_arg *a = arg;
3107 unsigned char status;
3109 struct got_fileindex_entry *ie;
3110 const char *path = path2 ? path2 : path1;
3111 struct got_object_id *id = id2 ? id2 : id1;
3117 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3121 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3123 return got_error_from_errno("asprintf");
3125 /* Reject merges into a work tree with conflicted files. */
3126 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3130 if (status == GOT_STATUS_CONFLICT)
3131 return got_error(GOT_ERR_CONFLICTS);
3136 static const struct got_error *
3137 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3138 const char *fileindex_path, struct got_object_id *commit_id1,
3139 struct got_object_id *commit_id2, struct got_repository *repo,
3140 got_worktree_checkout_cb progress_cb, void *progress_arg,
3141 got_cancel_cb cancel_cb, void *cancel_arg)
3143 const struct got_error *err = NULL, *sync_err;
3144 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3145 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3146 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3147 struct check_merge_conflicts_arg cmc_arg;
3148 struct merge_file_cb_arg arg;
3149 char *label_orig = NULL;
3150 FILE *f1 = NULL, *f2 = NULL;
3151 int fd1 = -1, fd2 = -1;
3154 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3157 err = got_object_id_by_path(&tree_id1, repo, commit1,
3158 worktree->path_prefix);
3159 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3165 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3169 err = got_object_id_str(&id_str, commit_id1);
3173 if (asprintf(&label_orig, "%s: commit %s",
3174 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3175 err = got_error_from_errno("asprintf");
3181 f1 = got_opentemp();
3183 err = got_error_from_errno("got_opentemp");
3188 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3192 err = got_object_id_by_path(&tree_id2, repo, commit2,
3193 worktree->path_prefix);
3197 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3201 f2 = got_opentemp();
3203 err = got_error_from_errno("got_opentemp");
3207 fd1 = got_opentempfd();
3209 err = got_error_from_errno("got_opentempfd");
3213 fd2 = got_opentempfd();
3215 err = got_error_from_errno("got_opentempfd");
3219 cmc_arg.worktree = worktree;
3220 cmc_arg.fileindex = fileindex;
3221 cmc_arg.repo = repo;
3222 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3223 check_merge_conflicts, &cmc_arg, 0);
3227 arg.worktree = worktree;
3228 arg.fileindex = fileindex;
3229 arg.progress_cb = progress_cb;
3230 arg.progress_arg = progress_arg;
3231 arg.cancel_cb = cancel_cb;
3232 arg.cancel_arg = cancel_arg;
3233 arg.label_orig = label_orig;
3234 arg.commit_id2 = commit_id2;
3235 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3236 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3237 merge_file_cb, &arg, 1);
3238 sync_err = sync_fileindex(fileindex, fileindex_path);
3239 if (sync_err && err == NULL)
3243 got_object_commit_close(commit1);
3245 got_object_commit_close(commit2);
3247 got_object_tree_close(tree1);
3249 got_object_tree_close(tree2);
3250 if (f1 && fclose(f1) == EOF && err == NULL)
3251 err = got_error_from_errno("fclose");
3252 if (f2 && fclose(f2) == EOF && err == NULL)
3253 err = got_error_from_errno("fclose");
3254 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3255 err = got_error_from_errno("close");
3256 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3257 err = got_error_from_errno("close");
3262 const struct got_error *
3263 got_worktree_merge_files(struct got_worktree *worktree,
3264 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3265 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3266 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3268 const struct got_error *err, *unlockerr;
3269 char *fileindex_path = NULL;
3270 struct got_fileindex *fileindex = NULL;
3272 err = lock_worktree(worktree, LOCK_EX);
3276 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3280 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3285 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3286 commit_id2, repo, progress_cb, progress_arg,
3287 cancel_cb, cancel_arg);
3290 got_fileindex_free(fileindex);
3291 free(fileindex_path);
3292 unlockerr = lock_worktree(worktree, LOCK_SH);
3293 if (unlockerr && err == NULL)
3298 struct diff_dir_cb_arg {
3299 struct got_fileindex *fileindex;
3300 struct got_worktree *worktree;
3301 const char *status_path;
3302 size_t status_path_len;
3303 struct got_repository *repo;
3304 got_worktree_status_cb status_cb;
3306 got_cancel_cb cancel_cb;
3308 /* A pathlist containing per-directory pathlists of ignore patterns. */
3309 struct got_pathlist_head *ignores;
3310 int report_unchanged;
3314 static const struct got_error *
3315 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3316 int dirfd, const char *de_name,
3317 got_worktree_status_cb status_cb, void *status_arg,
3318 struct got_repository *repo, int report_unchanged)
3320 const struct got_error *err = NULL;
3321 unsigned char status = GOT_STATUS_NO_CHANGE;
3322 unsigned char staged_status = get_staged_status(ie);
3324 struct got_object_id blob_id, commit_id, staged_blob_id;
3325 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3326 struct got_object_id *staged_blob_idp = NULL;
3328 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3332 if (status == GOT_STATUS_NO_CHANGE &&
3333 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3336 if (got_fileindex_entry_has_blob(ie)) {
3337 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3338 blob_idp = &blob_id;
3340 if (got_fileindex_entry_has_commit(ie)) {
3341 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3342 commit_idp = &commit_id;
3344 if (staged_status == GOT_STATUS_ADD ||
3345 staged_status == GOT_STATUS_MODIFY) {
3346 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3347 SHA1_DIGEST_LENGTH);
3348 staged_blob_idp = &staged_blob_id;
3351 return (*status_cb)(status_arg, status, staged_status,
3352 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3355 static const struct got_error *
3356 status_old_new(void *arg, struct got_fileindex_entry *ie,
3357 struct dirent *de, const char *parent_path, int dirfd)
3359 const struct got_error *err = NULL;
3360 struct diff_dir_cb_arg *a = arg;
3363 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3364 return got_error(GOT_ERR_CANCELLED);
3366 if (got_path_cmp(parent_path, a->status_path,
3367 strlen(parent_path), a->status_path_len) != 0 &&
3368 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3371 if (parent_path[0]) {
3372 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3373 parent_path, de->d_name) == -1)
3374 return got_error_from_errno("asprintf");
3376 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3378 return got_error_from_errno("asprintf");
3381 err = report_file_status(ie, abspath, dirfd, de->d_name,
3382 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3387 static const struct got_error *
3388 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3390 struct diff_dir_cb_arg *a = arg;
3391 struct got_object_id blob_id, commit_id;
3392 unsigned char status;
3394 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3395 return got_error(GOT_ERR_CANCELLED);
3397 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3400 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3401 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3402 if (got_fileindex_entry_has_file_on_disk(ie))
3403 status = GOT_STATUS_MISSING;
3405 status = GOT_STATUS_DELETE;
3406 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3407 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3411 free_ignorelist(struct got_pathlist_head *ignorelist)
3413 struct got_pathlist_entry *pe;
3415 TAILQ_FOREACH(pe, ignorelist, entry)
3416 free((char *)pe->path);
3417 got_pathlist_free(ignorelist);
3421 free_ignores(struct got_pathlist_head *ignores)
3423 struct got_pathlist_entry *pe;
3425 TAILQ_FOREACH(pe, ignores, entry) {
3426 struct got_pathlist_head *ignorelist = pe->data;
3427 free_ignorelist(ignorelist);
3428 free((char *)pe->path);
3430 got_pathlist_free(ignores);
3433 static const struct got_error *
3434 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3436 const struct got_error *err = NULL;
3437 struct got_pathlist_entry *pe = NULL;
3438 struct got_pathlist_head *ignorelist;
3439 char *line = NULL, *pattern, *dirpath = NULL;
3440 size_t linesize = 0;
3443 ignorelist = calloc(1, sizeof(*ignorelist));
3444 if (ignorelist == NULL)
3445 return got_error_from_errno("calloc");
3446 TAILQ_INIT(ignorelist);
3448 while ((linelen = getline(&line, &linesize, f)) != -1) {
3449 if (linelen > 0 && line[linelen - 1] == '\n')
3450 line[linelen - 1] = '\0';
3452 /* Git's ignores may contain comments. */
3456 /* Git's negated patterns are not (yet?) supported. */
3460 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3462 err = got_error_from_errno("asprintf");
3465 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3470 err = got_error_from_errno("getline");
3474 dirpath = strdup(path);
3475 if (dirpath == NULL) {
3476 err = got_error_from_errno("strdup");
3479 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3482 if (err || pe == NULL) {
3484 free_ignorelist(ignorelist);
3490 match_ignores(struct got_pathlist_head *ignores, const char *path)
3492 struct got_pathlist_entry *pe;
3494 /* Handle patterns which match in all directories. */
3495 TAILQ_FOREACH(pe, ignores, entry) {
3496 struct got_pathlist_head *ignorelist = pe->data;
3497 struct got_pathlist_entry *pi;
3499 TAILQ_FOREACH(pi, ignorelist, entry) {
3500 const char *p, *pattern = pi->path;
3502 if (strncmp(pattern, "**/", 3) != 0)
3507 if (fnmatch(pattern, p,
3508 FNM_PATHNAME | FNM_LEADING_DIR)) {
3509 /* Retry in next directory. */
3510 while (*p && *p != '/')
3522 * The ignores pathlist contains ignore lists from children before
3523 * parents, so we can find the most specific ignorelist by walking
3524 * ignores backwards.
3526 pe = TAILQ_LAST(ignores, got_pathlist_head);
3528 if (got_path_is_child(path, pe->path, pe->path_len)) {
3529 struct got_pathlist_head *ignorelist = pe->data;
3530 struct got_pathlist_entry *pi;
3531 TAILQ_FOREACH(pi, ignorelist, entry) {
3532 const char *pattern = pi->path;
3533 int flags = FNM_LEADING_DIR;
3534 if (strstr(pattern, "/**/") == NULL)
3535 flags |= FNM_PATHNAME;
3536 if (fnmatch(pattern, path, flags))
3541 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3547 static const struct got_error *
3548 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3549 const char *path, int dirfd, const char *ignores_filename)
3551 const struct got_error *err = NULL;
3554 FILE *ignoresfile = NULL;
3556 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3557 path[0] ? "/" : "", ignores_filename) == -1)
3558 return got_error_from_errno("asprintf");
3561 fd = openat(dirfd, ignores_filename,
3562 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3564 if (errno != ENOENT && errno != EACCES)
3565 err = got_error_from_errno2("openat",
3568 ignoresfile = fdopen(fd, "r");
3569 if (ignoresfile == NULL)
3570 err = got_error_from_errno2("fdopen",
3574 err = read_ignores(ignores, path, ignoresfile);
3578 ignoresfile = fopen(ignorespath, "re");
3579 if (ignoresfile == NULL) {
3580 if (errno != ENOENT && errno != EACCES)
3581 err = got_error_from_errno2("fopen",
3584 err = read_ignores(ignores, path, ignoresfile);
3587 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3588 err = got_error_from_errno2("fclose", path);
3589 if (fd != -1 && close(fd) == -1 && err == NULL)
3590 err = got_error_from_errno2("close", path);
3595 static const struct got_error *
3596 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3599 const struct got_error *err = NULL;
3600 struct diff_dir_cb_arg *a = arg;
3606 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3607 return got_error(GOT_ERR_CANCELLED);
3609 if (parent_path[0]) {
3610 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3611 return got_error_from_errno("asprintf");
3616 if (de->d_type == DT_DIR) {
3617 if (!a->no_ignores && ignore != NULL &&
3618 match_ignores(a->ignores, path))
3620 } else if (!match_ignores(a->ignores, path) &&
3621 got_path_is_child(path, a->status_path, a->status_path_len))
3622 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3623 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3629 static const struct got_error *
3630 status_traverse(void *arg, const char *path, int dirfd)
3632 const struct got_error *err = NULL;
3633 struct diff_dir_cb_arg *a = arg;
3638 err = add_ignores(a->ignores, a->worktree->root_path,
3639 path, dirfd, ".cvsignore");
3643 err = add_ignores(a->ignores, a->worktree->root_path, path,
3644 dirfd, ".gitignore");
3649 static const struct got_error *
3650 report_single_file_status(const char *path, const char *ondisk_path,
3651 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3652 void *status_arg, struct got_repository *repo, int report_unchanged,
3653 struct got_pathlist_head *ignores, int no_ignores)
3655 struct got_fileindex_entry *ie;
3658 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3660 return report_file_status(ie, ondisk_path, -1, NULL,
3661 status_cb, status_arg, repo, report_unchanged);
3663 if (lstat(ondisk_path, &sb) == -1) {
3664 if (errno != ENOENT)
3665 return got_error_from_errno2("lstat", ondisk_path);
3666 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3667 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3670 if (!no_ignores && match_ignores(ignores, path))
3673 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3674 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3675 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3680 static const struct got_error *
3681 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3682 const char *root_path, const char *path)
3684 const struct got_error *err;
3685 char *parent_path, *next_parent_path = NULL;
3687 err = add_ignores(ignores, root_path, "", -1,
3692 err = add_ignores(ignores, root_path, "", -1,
3697 err = got_path_dirname(&parent_path, path);
3699 if (err->code == GOT_ERR_BAD_PATH)
3700 return NULL; /* cannot traverse parent */
3704 err = add_ignores(ignores, root_path, parent_path, -1,
3708 err = add_ignores(ignores, root_path, parent_path, -1,
3712 err = got_path_dirname(&next_parent_path, parent_path);
3714 if (err->code == GOT_ERR_BAD_PATH)
3715 err = NULL; /* traversed everything */
3718 if (got_path_is_root_dir(parent_path))
3721 parent_path = next_parent_path;
3722 next_parent_path = NULL;
3726 free(next_parent_path);
3730 static const struct got_error *
3731 worktree_status(struct got_worktree *worktree, const char *path,
3732 struct got_fileindex *fileindex, struct got_repository *repo,
3733 got_worktree_status_cb status_cb, void *status_arg,
3734 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3735 int report_unchanged)
3737 const struct got_error *err = NULL;
3739 struct got_fileindex_diff_dir_cb fdiff_cb;
3740 struct diff_dir_cb_arg arg;
3741 char *ondisk_path = NULL;
3742 struct got_pathlist_head ignores;
3743 struct got_fileindex_entry *ie;
3745 TAILQ_INIT(&ignores);
3747 if (asprintf(&ondisk_path, "%s%s%s",
3748 worktree->root_path, path[0] ? "/" : "", path) == -1)
3749 return got_error_from_errno("asprintf");
3751 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3753 err = report_single_file_status(path, ondisk_path,
3754 fileindex, status_cb, status_arg, repo,
3755 report_unchanged, &ignores, no_ignores);
3759 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3761 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3762 !got_err_open_nofollow_on_symlink())
3763 err = got_error_from_errno2("open", ondisk_path);
3766 err = add_ignores_from_parent_paths(&ignores,
3767 worktree->root_path, ondisk_path);
3771 err = report_single_file_status(path, ondisk_path,
3772 fileindex, status_cb, status_arg, repo,
3773 report_unchanged, &ignores, no_ignores);
3776 fdiff_cb.diff_old_new = status_old_new;
3777 fdiff_cb.diff_old = status_old;
3778 fdiff_cb.diff_new = status_new;
3779 fdiff_cb.diff_traverse = status_traverse;
3780 arg.fileindex = fileindex;
3781 arg.worktree = worktree;
3782 arg.status_path = path;
3783 arg.status_path_len = strlen(path);
3785 arg.status_cb = status_cb;
3786 arg.status_arg = status_arg;
3787 arg.cancel_cb = cancel_cb;
3788 arg.cancel_arg = cancel_arg;
3789 arg.report_unchanged = report_unchanged;
3790 arg.no_ignores = no_ignores;
3792 err = add_ignores_from_parent_paths(&ignores,
3793 worktree->root_path, path);
3797 arg.ignores = &ignores;
3798 err = got_fileindex_diff_dir(fileindex, fd,
3799 worktree->root_path, path, repo, &fdiff_cb, &arg);
3802 free_ignores(&ignores);
3803 if (fd != -1 && close(fd) == -1 && err == NULL)
3804 err = got_error_from_errno("close");
3809 const struct got_error *
3810 got_worktree_status(struct got_worktree *worktree,
3811 struct got_pathlist_head *paths, struct got_repository *repo,
3812 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3813 got_cancel_cb cancel_cb, void *cancel_arg)
3815 const struct got_error *err = NULL;
3816 char *fileindex_path = NULL;
3817 struct got_fileindex *fileindex = NULL;
3818 struct got_pathlist_entry *pe;
3820 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3824 TAILQ_FOREACH(pe, paths, entry) {
3825 err = worktree_status(worktree, pe->path, fileindex, repo,
3826 status_cb, status_arg, cancel_cb, cancel_arg,
3831 free(fileindex_path);
3832 got_fileindex_free(fileindex);
3836 const struct got_error *
3837 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3840 const struct got_error *err = NULL;
3841 char *resolved = NULL, *cwd = NULL, *path = NULL;
3844 char *abspath = NULL;
3845 char canonpath[PATH_MAX];
3849 cwd = getcwd(NULL, 0);
3851 return got_error_from_errno("getcwd");
3853 if (lstat(arg, &sb) == -1) {
3854 if (errno != ENOENT) {
3855 err = got_error_from_errno2("lstat", arg);
3860 if (S_ISLNK(sb.st_mode)) {
3862 * We cannot use realpath(3) with symlinks since we want to
3863 * operate on the symlink itself.
3864 * But we can make the path absolute, assuming it is relative
3865 * to the current working directory, and then canonicalize it.
3867 if (!got_path_is_absolute(arg)) {
3868 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3869 err = got_error_from_errno("asprintf");
3874 err = got_canonpath(abspath ? abspath : arg, canonpath,
3878 resolved = strdup(canonpath);
3879 if (resolved == NULL) {
3880 err = got_error_from_errno("strdup");
3884 resolved = realpath(arg, NULL);
3885 if (resolved == NULL) {
3886 if (errno != ENOENT) {
3887 err = got_error_from_errno2("realpath", arg);
3890 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3891 err = got_error_from_errno("asprintf");
3894 err = got_canonpath(abspath, canonpath,
3898 resolved = strdup(canonpath);
3899 if (resolved == NULL) {
3900 err = got_error_from_errno("strdup");
3906 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3907 strlen(got_worktree_get_root_path(worktree)))) {
3908 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3912 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3913 err = got_path_skip_common_ancestor(&path,
3914 got_worktree_get_root_path(worktree), resolved);
3920 err = got_error_from_errno("strdup");
3925 /* XXX status walk can't deal with trailing slash! */
3927 while (len > 0 && path[len - 1] == '/') {
3928 path[len - 1] = '\0';
3942 struct schedule_addition_args {
3943 struct got_worktree *worktree;
3944 struct got_fileindex *fileindex;
3945 got_worktree_checkout_cb progress_cb;
3947 struct got_repository *repo;
3950 static const struct got_error *
3951 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3952 const char *relpath, struct got_object_id *blob_id,
3953 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3954 int dirfd, const char *de_name)
3956 struct schedule_addition_args *a = arg;
3957 const struct got_error *err = NULL;
3958 struct got_fileindex_entry *ie;
3962 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3964 return got_error_from_errno("asprintf");
3966 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3968 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3972 /* Re-adding an existing entry is a no-op. */
3973 if (status == GOT_STATUS_ADD)
3975 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3980 if (status != GOT_STATUS_UNVERSIONED) {
3981 if (status == GOT_STATUS_NONEXISTENT)
3982 err = got_error_set_errno(ENOENT, ondisk_path);
3984 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3988 err = got_fileindex_entry_alloc(&ie, relpath);
3991 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3992 relpath, NULL, NULL, 1);
3994 got_fileindex_entry_free(ie);
3997 err = got_fileindex_entry_add(a->fileindex, ie);
3999 got_fileindex_entry_free(ie);
4006 if (status == GOT_STATUS_ADD)
4008 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4011 const struct got_error *
4012 got_worktree_schedule_add(struct got_worktree *worktree,
4013 struct got_pathlist_head *paths,
4014 got_worktree_checkout_cb progress_cb, void *progress_arg,
4015 struct got_repository *repo, int no_ignores)
4017 struct got_fileindex *fileindex = NULL;
4018 char *fileindex_path = NULL;
4019 const struct got_error *err = NULL, *sync_err, *unlockerr;
4020 struct got_pathlist_entry *pe;
4021 struct schedule_addition_args saa;
4023 err = lock_worktree(worktree, LOCK_EX);
4027 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4031 saa.worktree = worktree;
4032 saa.fileindex = fileindex;
4033 saa.progress_cb = progress_cb;
4034 saa.progress_arg = progress_arg;
4037 TAILQ_FOREACH(pe, paths, entry) {
4038 err = worktree_status(worktree, pe->path, fileindex, repo,
4039 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4043 sync_err = sync_fileindex(fileindex, fileindex_path);
4044 if (sync_err && err == NULL)
4047 free(fileindex_path);
4049 got_fileindex_free(fileindex);
4050 unlockerr = lock_worktree(worktree, LOCK_SH);
4051 if (unlockerr && err == NULL)
4056 struct schedule_deletion_args {
4057 struct got_worktree *worktree;
4058 struct got_fileindex *fileindex;
4059 got_worktree_delete_cb progress_cb;
4061 struct got_repository *repo;
4062 int delete_local_mods;
4064 int ignore_missing_paths;
4065 const char *status_codes;
4068 static const struct got_error *
4069 schedule_for_deletion(void *arg, unsigned char status,
4070 unsigned char staged_status, const char *relpath,
4071 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4072 struct got_object_id *commit_id, int dirfd, const char *de_name)
4074 struct schedule_deletion_args *a = arg;
4075 const struct got_error *err = NULL;
4076 struct got_fileindex_entry *ie = NULL;
4080 if (status == GOT_STATUS_NONEXISTENT) {
4081 if (a->ignore_missing_paths)
4083 return got_error_set_errno(ENOENT, relpath);
4086 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4088 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4090 staged_status = get_staged_status(ie);
4091 if (staged_status != GOT_STATUS_NO_CHANGE) {
4092 if (staged_status == GOT_STATUS_DELETE)
4094 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4097 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4099 return got_error_from_errno("asprintf");
4101 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4106 if (a->status_codes) {
4107 size_t ncodes = strlen(a->status_codes);
4109 for (i = 0; i < ncodes ; i++) {
4110 if (status == a->status_codes[i])
4114 /* Do not delete files in non-matching status. */
4118 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4119 a->status_codes[i] != GOT_STATUS_MISSING) {
4120 static char msg[64];
4121 snprintf(msg, sizeof(msg),
4122 "invalid status code '%c'", a->status_codes[i]);
4123 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4128 if (status != GOT_STATUS_NO_CHANGE) {
4129 if (status == GOT_STATUS_DELETE)
4131 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4132 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4135 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4136 err = got_error_set_errno(ENOENT, relpath);
4139 if (status != GOT_STATUS_MODIFY &&
4140 status != GOT_STATUS_MISSING) {
4141 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4146 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4150 if (unlinkat(dirfd, de_name, 0) == -1) {
4151 err = got_error_from_errno2("unlinkat",
4155 } else if (unlink(ondisk_path) == -1) {
4156 err = got_error_from_errno2("unlink", ondisk_path);
4160 root_len = strlen(a->worktree->root_path);
4163 err = got_path_dirname(&parent, ondisk_path);
4167 ondisk_path = parent;
4168 if (rmdir(ondisk_path) == -1) {
4169 if (errno != ENOTEMPTY)
4170 err = got_error_from_errno2("rmdir",
4174 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4175 strlen(ondisk_path), root_len) != 0);
4178 got_fileindex_entry_mark_deleted_from_disk(ie);
4183 if (status == GOT_STATUS_DELETE)
4185 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4186 staged_status, relpath);
4189 const struct got_error *
4190 got_worktree_schedule_delete(struct got_worktree *worktree,
4191 struct got_pathlist_head *paths, int delete_local_mods,
4192 const char *status_codes,
4193 got_worktree_delete_cb progress_cb, void *progress_arg,
4194 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4196 struct got_fileindex *fileindex = NULL;
4197 char *fileindex_path = NULL;
4198 const struct got_error *err = NULL, *sync_err, *unlockerr;
4199 struct got_pathlist_entry *pe;
4200 struct schedule_deletion_args sda;
4202 err = lock_worktree(worktree, LOCK_EX);
4206 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4210 sda.worktree = worktree;
4211 sda.fileindex = fileindex;
4212 sda.progress_cb = progress_cb;
4213 sda.progress_arg = progress_arg;
4215 sda.delete_local_mods = delete_local_mods;
4216 sda.keep_on_disk = keep_on_disk;
4217 sda.ignore_missing_paths = ignore_missing_paths;
4218 sda.status_codes = status_codes;
4220 TAILQ_FOREACH(pe, paths, entry) {
4221 err = worktree_status(worktree, pe->path, fileindex, repo,
4222 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4226 sync_err = sync_fileindex(fileindex, fileindex_path);
4227 if (sync_err && err == NULL)
4230 free(fileindex_path);
4232 got_fileindex_free(fileindex);
4233 unlockerr = lock_worktree(worktree, LOCK_SH);
4234 if (unlockerr && err == NULL)
4239 static const struct got_error *
4240 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4242 const struct got_error *err = NULL;
4244 size_t linesize = 0, n;
4247 linelen = getline(&line, &linesize, infile);
4248 if (linelen == -1) {
4249 if (ferror(infile)) {
4250 err = got_error_from_errno("getline");
4256 n = fwrite(line, 1, linelen, outfile);
4258 err = got_ferror(outfile, GOT_ERR_IO);
4263 n = fwrite(line, 1, linelen, rejectfile);
4265 err = got_ferror(outfile, GOT_ERR_IO);
4272 static const struct got_error *
4273 skip_one_line(FILE *f)
4276 size_t linesize = 0;
4279 linelen = getline(&line, &linesize, f);
4280 if (linelen == -1) {
4282 return got_error_from_errno("getline");
4289 static const struct got_error *
4290 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4291 int start_old, int end_old, int start_new, int end_new,
4292 FILE *outfile, FILE *rejectfile)
4294 const struct got_error *err;
4296 /* Copy old file's lines leading up to patch. */
4297 while (!feof(f1) && *line_cur1 < start_old) {
4298 err = copy_one_line(f1, outfile, NULL);
4303 /* Skip new file's lines leading up to patch. */
4304 while (!feof(f2) && *line_cur2 < start_new) {
4306 err = copy_one_line(f2, NULL, rejectfile);
4308 err = skip_one_line(f2);
4313 /* Copy patched lines. */
4314 while (!feof(f2) && *line_cur2 <= end_new) {
4315 err = copy_one_line(f2, outfile, NULL);
4320 /* Skip over old file's replaced lines. */
4321 while (!feof(f1) && *line_cur1 <= end_old) {
4323 err = copy_one_line(f1, NULL, rejectfile);
4325 err = skip_one_line(f1);
4334 static const struct got_error *
4335 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4336 FILE *outfile, FILE *rejectfile)
4338 const struct got_error *err;
4341 /* Copy old file's lines until EOF. */
4343 err = copy_one_line(f1, outfile, NULL);
4350 /* Copy new file's lines until EOF. */
4352 err = copy_one_line(f2, NULL, rejectfile);
4362 static const struct got_error *
4363 apply_or_reject_change(int *choice, int *nchunks_used,
4364 struct diff_result *diff_result, int n,
4365 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4366 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4367 got_worktree_patch_cb patch_cb, void *patch_arg)
4369 const struct got_error *err = NULL;
4370 struct diff_chunk_context cc = {};
4371 int start_old, end_old, start_new, end_new;
4373 struct diff_output_unidiff_state *diff_state;
4374 struct diff_input_info diff_info;
4377 *choice = GOT_PATCH_CHOICE_NONE;
4379 /* Get changed line numbers without context lines for copy_change(). */
4380 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4381 start_old = cc.left.start;
4382 end_old = cc.left.end;
4383 start_new = cc.right.start;
4384 end_new = cc.right.end;
4386 /* Get the same change with context lines for display. */
4387 memset(&cc, 0, sizeof(cc));
4388 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4390 memset(&diff_info, 0, sizeof(diff_info));
4391 diff_info.left_path = relpath;
4392 diff_info.right_path = relpath;
4394 diff_state = diff_output_unidiff_state_alloc();
4395 if (diff_state == NULL)
4396 return got_error_set_errno(ENOMEM,
4397 "diff_output_unidiff_state_alloc");
4399 hunkfile = got_opentemp();
4400 if (hunkfile == NULL) {
4401 err = got_error_from_errno("got_opentemp");
4405 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4407 if (rc != DIFF_RC_OK) {
4408 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4412 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4413 err = got_ferror(hunkfile, GOT_ERR_IO);
4417 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4418 hunkfile, changeno, nchanges);
4423 case GOT_PATCH_CHOICE_YES:
4424 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4425 end_old, start_new, end_new, outfile, rejectfile);
4427 case GOT_PATCH_CHOICE_NO:
4428 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4429 end_old, start_new, end_new, rejectfile, outfile);
4431 case GOT_PATCH_CHOICE_QUIT:
4434 err = got_error(GOT_ERR_PATCH_CHOICE);
4438 diff_output_unidiff_state_free(diff_state);
4439 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4440 err = got_error_from_errno("fclose");
4444 struct revert_file_args {
4445 struct got_worktree *worktree;
4446 struct got_fileindex *fileindex;
4447 got_worktree_checkout_cb progress_cb;
4449 got_worktree_patch_cb patch_cb;
4451 struct got_repository *repo;
4452 int unlink_added_files;
4455 static const struct got_error *
4456 create_patched_content(char **path_outfile, int reverse_patch,
4457 struct got_object_id *blob_id, const char *path2,
4458 int dirfd2, const char *de_name2,
4459 const char *relpath, struct got_repository *repo,
4460 got_worktree_patch_cb patch_cb, void *patch_arg)
4462 const struct got_error *err, *free_err;
4463 struct got_blob_object *blob = NULL;
4464 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4465 int fd = -1, fd2 = -1;
4466 char link_target[PATH_MAX];
4467 ssize_t link_len = 0;
4468 char *path1 = NULL, *id_str = NULL;
4470 struct got_diffreg_result *diffreg_result = NULL;
4471 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4472 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4474 *path_outfile = NULL;
4476 err = got_object_id_str(&id_str, blob_id);
4481 fd2 = openat(dirfd2, de_name2,
4482 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4484 if (!got_err_open_nofollow_on_symlink()) {
4485 err = got_error_from_errno2("openat", path2);
4488 link_len = readlinkat(dirfd2, de_name2,
4489 link_target, sizeof(link_target));
4490 if (link_len == -1) {
4491 return got_error_from_errno2("readlinkat",
4494 sb2.st_mode = S_IFLNK;
4495 sb2.st_size = link_len;
4498 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4500 if (!got_err_open_nofollow_on_symlink()) {
4501 err = got_error_from_errno2("open", path2);
4504 link_len = readlink(path2, link_target,
4505 sizeof(link_target));
4507 return got_error_from_errno2("readlink", path2);
4508 sb2.st_mode = S_IFLNK;
4509 sb2.st_size = link_len;
4513 if (fstat(fd2, &sb2) == -1) {
4514 err = got_error_from_errno2("fstat", path2);
4518 f2 = fdopen(fd2, "r");
4520 err = got_error_from_errno2("fdopen", path2);
4526 f2 = got_opentemp();
4528 err = got_error_from_errno2("got_opentemp", path2);
4531 n = fwrite(link_target, 1, link_len, f2);
4532 if (n != link_len) {
4533 err = got_ferror(f2, GOT_ERR_IO);
4536 if (fflush(f2) == EOF) {
4537 err = got_error_from_errno("fflush");
4543 fd = got_opentempfd();
4545 err = got_error_from_errno("got_opentempfd");
4549 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4553 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4557 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4561 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4562 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4566 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4570 if (fseek(f1, 0L, SEEK_SET) == -1)
4571 return got_ferror(f1, GOT_ERR_IO);
4572 if (fseek(f2, 0L, SEEK_SET) == -1)
4573 return got_ferror(f2, GOT_ERR_IO);
4575 /* Count the number of actual changes in the diff result. */
4576 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4577 struct diff_chunk_context cc = {};
4578 diff_chunk_context_load_change(&cc, &nchunks_used,
4579 diffreg_result->result, n, 0);
4582 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4584 err = apply_or_reject_change(&choice, &nchunks_used,
4585 diffreg_result->result, n, relpath, f1, f2,
4586 &line_cur1, &line_cur2,
4587 reverse_patch ? NULL : outfile,
4588 reverse_patch ? outfile : NULL,
4589 ++i, nchanges, patch_cb, patch_arg);
4592 if (choice == GOT_PATCH_CHOICE_YES)
4594 else if (choice == GOT_PATCH_CHOICE_QUIT)
4598 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4599 reverse_patch ? NULL : outfile,
4600 reverse_patch ? outfile : NULL);
4604 if (!S_ISLNK(sb2.st_mode)) {
4607 mode = apply_umask(sb2.st_mode);
4608 if (fchmod(fileno(outfile), mode) == -1) {
4609 err = got_error_from_errno2("fchmod", path2);
4616 if (fd != -1 && close(fd) == -1 && err == NULL)
4617 err = got_error_from_errno("close");
4619 got_object_blob_close(blob);
4620 free_err = got_diffreg_result_free(diffreg_result);
4623 if (f1 && fclose(f1) == EOF && err == NULL)
4624 err = got_error_from_errno2("fclose", path1);
4625 if (f2 && fclose(f2) == EOF && err == NULL)
4626 err = got_error_from_errno2("fclose", path2);
4627 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4628 err = got_error_from_errno2("close", path2);
4629 if (outfile && fclose(outfile) == EOF && err == NULL)
4630 err = got_error_from_errno2("fclose", *path_outfile);
4631 if (path1 && unlink(path1) == -1 && err == NULL)
4632 err = got_error_from_errno2("unlink", path1);
4633 if (err || !have_content) {
4634 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4635 err = got_error_from_errno2("unlink", *path_outfile);
4636 free(*path_outfile);
4637 *path_outfile = NULL;
4643 static const struct got_error *
4644 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4645 const char *relpath, struct got_object_id *blob_id,
4646 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4647 int dirfd, const char *de_name)
4649 struct revert_file_args *a = arg;
4650 const struct got_error *err = NULL;
4651 char *parent_path = NULL;
4652 struct got_fileindex_entry *ie;
4653 struct got_commit_object *base_commit = NULL;
4654 struct got_tree_object *tree = NULL;
4655 struct got_object_id *tree_id = NULL;
4656 const struct got_tree_entry *te = NULL;
4657 char *tree_path = NULL, *te_name;
4658 char *ondisk_path = NULL, *path_content = NULL;
4659 struct got_blob_object *blob = NULL;
4662 /* Reverting a staged deletion is a no-op. */
4663 if (status == GOT_STATUS_DELETE &&
4664 staged_status != GOT_STATUS_NO_CHANGE)
4667 if (status == GOT_STATUS_UNVERSIONED)
4668 return (*a->progress_cb)(a->progress_arg,
4669 GOT_STATUS_UNVERSIONED, relpath);
4671 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4673 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4675 /* Construct in-repository path of tree which contains this blob. */
4676 err = got_path_dirname(&parent_path, ie->path);
4678 if (err->code != GOT_ERR_BAD_PATH)
4680 parent_path = strdup("/");
4681 if (parent_path == NULL) {
4682 err = got_error_from_errno("strdup");
4686 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4687 tree_path = strdup(parent_path);
4688 if (tree_path == NULL) {
4689 err = got_error_from_errno("strdup");
4693 if (got_path_is_root_dir(parent_path)) {
4694 tree_path = strdup(a->worktree->path_prefix);
4695 if (tree_path == NULL) {
4696 err = got_error_from_errno("strdup");
4700 if (asprintf(&tree_path, "%s/%s",
4701 a->worktree->path_prefix, parent_path) == -1) {
4702 err = got_error_from_errno("asprintf");
4708 err = got_object_open_as_commit(&base_commit, a->repo,
4709 a->worktree->base_commit_id);
4713 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4715 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4716 (status == GOT_STATUS_ADD ||
4717 staged_status == GOT_STATUS_ADD)))
4720 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4724 err = got_path_basename(&te_name, ie->path);
4728 te = got_object_tree_find_entry(tree, te_name);
4730 if (te == NULL && status != GOT_STATUS_ADD &&
4731 staged_status != GOT_STATUS_ADD) {
4732 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4738 case GOT_STATUS_ADD:
4740 int choice = GOT_PATCH_CHOICE_NONE;
4741 err = (*a->patch_cb)(&choice, a->patch_arg,
4742 status, ie->path, NULL, 1, 1);
4745 if (choice != GOT_PATCH_CHOICE_YES)
4748 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4752 got_fileindex_entry_remove(a->fileindex, ie);
4753 if (a->unlink_added_files) {
4754 if (asprintf(&ondisk_path, "%s/%s",
4755 got_worktree_get_root_path(a->worktree),
4757 err = got_error_from_errno("asprintf");
4760 if (unlink(ondisk_path) == -1) {
4761 err = got_error_from_errno2("unlink",
4767 case GOT_STATUS_DELETE:
4769 int choice = GOT_PATCH_CHOICE_NONE;
4770 err = (*a->patch_cb)(&choice, a->patch_arg,
4771 status, ie->path, NULL, 1, 1);
4774 if (choice != GOT_PATCH_CHOICE_YES)
4778 case GOT_STATUS_MODIFY:
4779 case GOT_STATUS_MODE_CHANGE:
4780 case GOT_STATUS_CONFLICT:
4781 case GOT_STATUS_MISSING: {
4782 struct got_object_id id;
4783 if (staged_status == GOT_STATUS_ADD ||
4784 staged_status == GOT_STATUS_MODIFY) {
4785 memcpy(id.sha1, ie->staged_blob_sha1,
4786 SHA1_DIGEST_LENGTH);
4788 memcpy(id.sha1, ie->blob_sha1,
4789 SHA1_DIGEST_LENGTH);
4790 fd = got_opentempfd();
4792 err = got_error_from_errno("got_opentempfd");
4796 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4800 if (asprintf(&ondisk_path, "%s/%s",
4801 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4802 err = got_error_from_errno("asprintf");
4806 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4807 status == GOT_STATUS_CONFLICT)) {
4808 int is_bad_symlink = 0;
4809 err = create_patched_content(&path_content, 1, &id,
4810 ondisk_path, dirfd, de_name, ie->path, a->repo,
4811 a->patch_cb, a->patch_arg);
4812 if (err || path_content == NULL)
4814 if (te && S_ISLNK(te->mode)) {
4815 if (unlink(path_content) == -1) {
4816 err = got_error_from_errno2("unlink",
4820 err = install_symlink(&is_bad_symlink,
4821 a->worktree, ondisk_path, ie->path,
4822 blob, 0, 1, 0, 0, a->repo,
4823 a->progress_cb, a->progress_arg);
4825 if (rename(path_content, ondisk_path) == -1) {
4826 err = got_error_from_errno3("rename",
4827 path_content, ondisk_path);
4832 int is_bad_symlink = 0;
4833 if (te && S_ISLNK(te->mode)) {
4834 err = install_symlink(&is_bad_symlink,
4835 a->worktree, ondisk_path, ie->path,
4836 blob, 0, 1, 0, 0, a->repo,
4837 a->progress_cb, a->progress_arg);
4839 err = install_blob(a->worktree, ondisk_path,
4841 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4842 got_fileindex_perms_to_st(ie), blob,
4843 0, 1, 0, 0, a->repo,
4844 a->progress_cb, a->progress_arg);
4848 if (status == GOT_STATUS_DELETE ||
4849 status == GOT_STATUS_MODE_CHANGE) {
4850 err = got_fileindex_entry_update(ie,
4851 a->worktree->root_fd, relpath,
4853 a->worktree->base_commit_id->sha1, 1);
4857 if (is_bad_symlink) {
4858 got_fileindex_entry_filetype_set(ie,
4859 GOT_FILEIDX_MODE_BAD_SYMLINK);
4872 if (fd != -1 && close(fd) == -1 && err == NULL)
4873 err = got_error_from_errno("close");
4875 got_object_blob_close(blob);
4877 got_object_tree_close(tree);
4880 got_object_commit_close(base_commit);
4884 const struct got_error *
4885 got_worktree_revert(struct got_worktree *worktree,
4886 struct got_pathlist_head *paths,
4887 got_worktree_checkout_cb progress_cb, void *progress_arg,
4888 got_worktree_patch_cb patch_cb, void *patch_arg,
4889 struct got_repository *repo)
4891 struct got_fileindex *fileindex = NULL;
4892 char *fileindex_path = NULL;
4893 const struct got_error *err = NULL, *unlockerr = NULL;
4894 const struct got_error *sync_err = NULL;
4895 struct got_pathlist_entry *pe;
4896 struct revert_file_args rfa;
4898 err = lock_worktree(worktree, LOCK_EX);
4902 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4906 rfa.worktree = worktree;
4907 rfa.fileindex = fileindex;
4908 rfa.progress_cb = progress_cb;
4909 rfa.progress_arg = progress_arg;
4910 rfa.patch_cb = patch_cb;
4911 rfa.patch_arg = patch_arg;
4913 rfa.unlink_added_files = 0;
4914 TAILQ_FOREACH(pe, paths, entry) {
4915 err = worktree_status(worktree, pe->path, fileindex, repo,
4916 revert_file, &rfa, NULL, NULL, 1, 0);
4920 sync_err = sync_fileindex(fileindex, fileindex_path);
4921 if (sync_err && err == NULL)
4924 free(fileindex_path);
4926 got_fileindex_free(fileindex);
4927 unlockerr = lock_worktree(worktree, LOCK_SH);
4928 if (unlockerr && err == NULL)
4934 free_commitable(struct got_commitable *ct)
4937 free(ct->in_repo_path);
4938 free(ct->ondisk_path);
4940 free(ct->base_blob_id);
4941 free(ct->staged_blob_id);
4942 free(ct->base_commit_id);
4946 struct collect_commitables_arg {
4947 struct got_pathlist_head *commitable_paths;
4948 struct got_repository *repo;
4949 struct got_worktree *worktree;
4950 struct got_fileindex *fileindex;
4951 int have_staged_files;
4952 int allow_bad_symlinks;
4955 static const struct got_error *
4956 collect_commitables(void *arg, unsigned char status,
4957 unsigned char staged_status, const char *relpath,
4958 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4959 struct got_object_id *commit_id, int dirfd, const char *de_name)
4961 struct collect_commitables_arg *a = arg;
4962 const struct got_error *err = NULL;
4963 struct got_commitable *ct = NULL;
4964 struct got_pathlist_entry *new = NULL;
4965 char *parent_path = NULL, *path = NULL;
4968 if (a->have_staged_files) {
4969 if (staged_status != GOT_STATUS_MODIFY &&
4970 staged_status != GOT_STATUS_ADD &&
4971 staged_status != GOT_STATUS_DELETE)
4974 if (status == GOT_STATUS_CONFLICT)
4975 return got_error(GOT_ERR_COMMIT_CONFLICT);
4977 if (status != GOT_STATUS_MODIFY &&
4978 status != GOT_STATUS_MODE_CHANGE &&
4979 status != GOT_STATUS_ADD &&
4980 status != GOT_STATUS_DELETE)
4984 if (asprintf(&path, "/%s", relpath) == -1) {
4985 err = got_error_from_errno("asprintf");
4988 if (strcmp(path, "/") == 0) {
4989 parent_path = strdup("");
4990 if (parent_path == NULL)
4991 return got_error_from_errno("strdup");
4993 err = got_path_dirname(&parent_path, path);
4998 ct = calloc(1, sizeof(*ct));
5000 err = got_error_from_errno("calloc");
5004 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5006 err = got_error_from_errno("asprintf");
5010 if (staged_status == GOT_STATUS_ADD ||
5011 staged_status == GOT_STATUS_MODIFY) {
5012 struct got_fileindex_entry *ie;
5013 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5014 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5015 case GOT_FILEIDX_MODE_REGULAR_FILE:
5016 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5019 case GOT_FILEIDX_MODE_SYMLINK:
5023 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5026 ct->mode |= got_fileindex_entry_perms_get(ie);
5027 } else if (status != GOT_STATUS_DELETE &&
5028 staged_status != GOT_STATUS_DELETE) {
5030 if (fstatat(dirfd, de_name, &sb,
5031 AT_SYMLINK_NOFOLLOW) == -1) {
5032 err = got_error_from_errno2("fstatat",
5036 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5037 err = got_error_from_errno2("lstat", ct->ondisk_path);
5040 ct->mode = sb.st_mode;
5043 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5044 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5046 err = got_error_from_errno("asprintf");
5050 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5051 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5053 char target_path[PATH_MAX];
5055 target_len = readlink(ct->ondisk_path, target_path,
5056 sizeof(target_path));
5057 if (target_len == -1) {
5058 err = got_error_from_errno2("readlink",
5062 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5063 target_len, ct->ondisk_path, a->worktree->root_path);
5066 if (is_bad_symlink) {
5067 err = got_error_path(ct->ondisk_path,
5068 GOT_ERR_BAD_SYMLINK);
5074 ct->status = status;
5075 ct->staged_status = staged_status;
5076 ct->blob_id = NULL; /* will be filled in when blob gets created */
5077 if (ct->status != GOT_STATUS_ADD &&
5078 ct->staged_status != GOT_STATUS_ADD) {
5079 ct->base_blob_id = got_object_id_dup(blob_id);
5080 if (ct->base_blob_id == NULL) {
5081 err = got_error_from_errno("got_object_id_dup");
5084 ct->base_commit_id = got_object_id_dup(commit_id);
5085 if (ct->base_commit_id == NULL) {
5086 err = got_error_from_errno("got_object_id_dup");
5090 if (ct->staged_status == GOT_STATUS_ADD ||
5091 ct->staged_status == GOT_STATUS_MODIFY) {
5092 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5093 if (ct->staged_blob_id == NULL) {
5094 err = got_error_from_errno("got_object_id_dup");
5098 ct->path = strdup(path);
5099 if (ct->path == NULL) {
5100 err = got_error_from_errno("strdup");
5103 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5105 if (ct && (err || new == NULL))
5106 free_commitable(ct);
5112 static const struct got_error *write_tree(struct got_object_id **, int *,
5113 struct got_tree_object *, const char *, struct got_pathlist_head *,
5114 got_worktree_status_cb status_cb, void *status_arg,
5115 struct got_repository *);
5117 static const struct got_error *
5118 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5119 struct got_tree_entry *te, const char *parent_path,
5120 struct got_pathlist_head *commitable_paths,
5121 got_worktree_status_cb status_cb, void *status_arg,
5122 struct got_repository *repo)
5124 const struct got_error *err = NULL;
5125 struct got_tree_object *subtree;
5128 if (asprintf(&subpath, "%s%s%s", parent_path,
5129 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5130 return got_error_from_errno("asprintf");
5132 err = got_object_open_as_tree(&subtree, repo, &te->id);
5136 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5137 commitable_paths, status_cb, status_arg, repo);
5138 got_object_tree_close(subtree);
5143 static const struct got_error *
5144 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5146 const struct got_error *err = NULL;
5147 char *ct_parent_path = NULL;
5151 if (strchr(ct->in_repo_path, '/') == NULL) {
5152 *match = got_path_is_root_dir(path);
5156 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5159 *match = (strcmp(path, ct_parent_path) == 0);
5160 free(ct_parent_path);
5165 get_ct_file_mode(struct got_commitable *ct)
5167 if (S_ISLNK(ct->mode))
5170 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5173 static const struct got_error *
5174 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5175 struct got_tree_entry *te, struct got_commitable *ct)
5177 const struct got_error *err = NULL;
5181 err = got_object_tree_entry_dup(new_te, te);
5185 (*new_te)->mode = get_ct_file_mode(ct);
5187 if (ct->staged_status == GOT_STATUS_MODIFY)
5188 memcpy(&(*new_te)->id, ct->staged_blob_id,
5189 sizeof((*new_te)->id));
5191 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5193 if (err && *new_te) {
5200 static const struct got_error *
5201 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5202 struct got_commitable *ct)
5204 const struct got_error *err = NULL;
5205 char *ct_name = NULL;
5209 *new_te = calloc(1, sizeof(**new_te));
5210 if (*new_te == NULL)
5211 return got_error_from_errno("calloc");
5213 err = got_path_basename(&ct_name, ct->path);
5216 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5217 sizeof((*new_te)->name)) {
5218 err = got_error(GOT_ERR_NO_SPACE);
5222 (*new_te)->mode = get_ct_file_mode(ct);
5224 if (ct->staged_status == GOT_STATUS_ADD)
5225 memcpy(&(*new_te)->id, ct->staged_blob_id,
5226 sizeof((*new_te)->id));
5228 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5231 if (err && *new_te) {
5238 static const struct got_error *
5239 insert_tree_entry(struct got_tree_entry *new_te,
5240 struct got_pathlist_head *paths)
5242 const struct got_error *err = NULL;
5243 struct got_pathlist_entry *new_pe;
5245 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5249 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5253 static const struct got_error *
5254 report_ct_status(struct got_commitable *ct,
5255 got_worktree_status_cb status_cb, void *status_arg)
5257 const char *ct_path = ct->path;
5258 unsigned char status;
5260 if (status_cb == NULL) /* no commit progress output desired */
5263 while (ct_path[0] == '/')
5266 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5267 status = ct->staged_status;
5269 status = ct->status;
5271 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5272 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5275 static const struct got_error *
5276 match_modified_subtree(int *modified, struct got_tree_entry *te,
5277 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5279 const struct got_error *err = NULL;
5280 struct got_pathlist_entry *pe;
5285 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5286 got_path_is_root_dir(base_tree_path) ? "" : "/",
5288 return got_error_from_errno("asprintf");
5290 TAILQ_FOREACH(pe, commitable_paths, entry) {
5291 struct got_commitable *ct = pe->data;
5292 *modified = got_path_is_child(ct->in_repo_path, te_path,
5302 static const struct got_error *
5303 match_deleted_or_modified_ct(struct got_commitable **ctp,
5304 struct got_tree_entry *te, const char *base_tree_path,
5305 struct got_pathlist_head *commitable_paths)
5307 const struct got_error *err = NULL;
5308 struct got_pathlist_entry *pe;
5312 TAILQ_FOREACH(pe, commitable_paths, entry) {
5313 struct got_commitable *ct = pe->data;
5314 char *ct_name = NULL;
5317 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5318 if (ct->status != GOT_STATUS_MODIFY &&
5319 ct->status != GOT_STATUS_MODE_CHANGE &&
5320 ct->status != GOT_STATUS_DELETE)
5323 if (ct->staged_status != GOT_STATUS_MODIFY &&
5324 ct->staged_status != GOT_STATUS_DELETE)
5328 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5331 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5337 err = got_path_basename(&ct_name, pe->path);
5341 if (strcmp(te->name, ct_name) != 0) {
5354 static const struct got_error *
5355 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5356 const char *child_path, const char *path_base_tree,
5357 struct got_pathlist_head *commitable_paths,
5358 got_worktree_status_cb status_cb, void *status_arg,
5359 struct got_repository *repo)
5361 const struct got_error *err = NULL;
5362 struct got_tree_entry *new_te;
5364 struct got_object_id *id = NULL;
5369 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5370 got_path_is_root_dir(path_base_tree) ? "" : "/",
5372 return got_error_from_errno("asprintf");
5374 new_te = calloc(1, sizeof(*new_te));
5376 return got_error_from_errno("calloc");
5377 new_te->mode = S_IFDIR;
5379 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5380 sizeof(new_te->name)) {
5381 err = got_error(GOT_ERR_NO_SPACE);
5384 err = write_tree(&id, &nentries, NULL, subtree_path,
5385 commitable_paths, status_cb, status_arg, repo);
5390 memcpy(&new_te->id, id, sizeof(new_te->id));
5399 static const struct got_error *
5400 write_tree(struct got_object_id **new_tree_id, int *nentries,
5401 struct got_tree_object *base_tree, const char *path_base_tree,
5402 struct got_pathlist_head *commitable_paths,
5403 got_worktree_status_cb status_cb, void *status_arg,
5404 struct got_repository *repo)
5406 const struct got_error *err = NULL;
5407 struct got_pathlist_head paths;
5408 struct got_tree_entry *te, *new_te = NULL;
5409 struct got_pathlist_entry *pe;
5414 /* Insert, and recurse into, newly added entries first. */
5415 TAILQ_FOREACH(pe, commitable_paths, entry) {
5416 struct got_commitable *ct = pe->data;
5417 char *child_path = NULL, *slash;
5419 if ((ct->status != GOT_STATUS_ADD &&
5420 ct->staged_status != GOT_STATUS_ADD) ||
5421 (ct->flags & GOT_COMMITABLE_ADDED))
5424 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5425 strlen(path_base_tree)))
5428 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5433 slash = strchr(child_path, '/');
5434 if (slash == NULL) {
5435 err = alloc_added_blob_tree_entry(&new_te, ct);
5438 err = report_ct_status(ct, status_cb, status_arg);
5441 ct->flags |= GOT_COMMITABLE_ADDED;
5442 err = insert_tree_entry(new_te, &paths);
5447 *slash = '\0'; /* trim trailing path components */
5448 if (base_tree == NULL ||
5449 got_object_tree_find_entry(base_tree, child_path)
5451 err = make_subtree_for_added_blob(&new_te,
5452 child_path, path_base_tree,
5453 commitable_paths, status_cb, status_arg,
5457 err = insert_tree_entry(new_te, &paths);
5466 int i, nbase_entries;
5467 /* Handle modified and deleted entries. */
5468 nbase_entries = got_object_tree_get_nentries(base_tree);
5469 for (i = 0; i < nbase_entries; i++) {
5470 struct got_commitable *ct = NULL;
5472 te = got_object_tree_get_entry(base_tree, i);
5473 if (got_object_tree_entry_is_submodule(te)) {
5474 /* Entry is a submodule; just copy it. */
5475 err = got_object_tree_entry_dup(&new_te, te);
5478 err = insert_tree_entry(new_te, &paths);
5485 if (S_ISDIR(te->mode)) {
5487 err = got_object_tree_entry_dup(&new_te, te);
5490 err = match_modified_subtree(&modified, te,
5491 path_base_tree, commitable_paths);
5494 /* Avoid recursion into unmodified subtrees. */
5496 struct got_object_id *new_id;
5498 err = write_subtree(&new_id,
5500 path_base_tree, commitable_paths,
5501 status_cb, status_arg, repo);
5504 if (nsubentries == 0) {
5505 /* All entries were deleted. */
5509 memcpy(&new_te->id, new_id,
5510 sizeof(new_te->id));
5513 err = insert_tree_entry(new_te, &paths);
5520 err = match_deleted_or_modified_ct(&ct, te,
5521 path_base_tree, commitable_paths);
5525 /* NB: Deleted entries get dropped here. */
5526 if (ct->status == GOT_STATUS_MODIFY ||
5527 ct->status == GOT_STATUS_MODE_CHANGE ||
5528 ct->staged_status == GOT_STATUS_MODIFY) {
5529 err = alloc_modified_blob_tree_entry(
5533 err = insert_tree_entry(new_te, &paths);
5538 err = report_ct_status(ct, status_cb,
5543 /* Entry is unchanged; just copy it. */
5544 err = got_object_tree_entry_dup(&new_te, te);
5547 err = insert_tree_entry(new_te, &paths);
5555 /* Write new list of entries; deleted entries have been dropped. */
5556 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5558 got_pathlist_free(&paths);
5562 static const struct got_error *
5563 update_fileindex_after_commit(struct got_worktree *worktree,
5564 struct got_pathlist_head *commitable_paths,
5565 struct got_object_id *new_base_commit_id,
5566 struct got_fileindex *fileindex, int have_staged_files)
5568 const struct got_error *err = NULL;
5569 struct got_pathlist_entry *pe;
5570 char *relpath = NULL;
5572 TAILQ_FOREACH(pe, commitable_paths, entry) {
5573 struct got_fileindex_entry *ie;
5574 struct got_commitable *ct = pe->data;
5576 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5578 err = got_path_skip_common_ancestor(&relpath,
5579 worktree->root_path, ct->ondisk_path);
5584 if (ct->status == GOT_STATUS_DELETE ||
5585 ct->staged_status == GOT_STATUS_DELETE) {
5586 got_fileindex_entry_remove(fileindex, ie);
5587 } else if (ct->staged_status == GOT_STATUS_ADD ||
5588 ct->staged_status == GOT_STATUS_MODIFY) {
5589 got_fileindex_entry_stage_set(ie,
5590 GOT_FILEIDX_STAGE_NONE);
5591 got_fileindex_entry_staged_filetype_set(ie, 0);
5593 err = got_fileindex_entry_update(ie,
5594 worktree->root_fd, relpath,
5595 ct->staged_blob_id->sha1,
5596 new_base_commit_id->sha1,
5597 !have_staged_files);
5599 err = got_fileindex_entry_update(ie,
5600 worktree->root_fd, relpath,
5602 new_base_commit_id->sha1,
5603 !have_staged_files);
5605 err = got_fileindex_entry_alloc(&ie, pe->path);
5608 err = got_fileindex_entry_update(ie,
5609 worktree->root_fd, relpath, ct->blob_id->sha1,
5610 new_base_commit_id->sha1, 1);
5612 got_fileindex_entry_free(ie);
5615 err = got_fileindex_entry_add(fileindex, ie);
5617 got_fileindex_entry_free(ie);
5630 static const struct got_error *
5631 check_out_of_date(const char *in_repo_path, unsigned char status,
5632 unsigned char staged_status, struct got_object_id *base_blob_id,
5633 struct got_object_id *base_commit_id,
5634 struct got_object_id *head_commit_id, struct got_repository *repo,
5637 const struct got_error *err = NULL;
5638 struct got_commit_object *commit = NULL;
5639 struct got_object_id *id = NULL;
5641 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5642 /* Trivial case: base commit == head commit */
5643 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5646 * Ensure file content which local changes were based
5647 * on matches file content in the branch head.
5649 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5652 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5654 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5655 err = got_error(ood_errcode);
5657 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5658 err = got_error(ood_errcode);
5660 /* Require that added files don't exist in the branch head. */
5661 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5664 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5665 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5667 err = id ? got_error(ood_errcode) : NULL;
5672 got_object_commit_close(commit);
5676 static const struct got_error *
5677 commit_worktree(struct got_object_id **new_commit_id,
5678 struct got_pathlist_head *commitable_paths,
5679 struct got_object_id *head_commit_id,
5680 struct got_object_id *parent_id2,
5681 struct got_worktree *worktree,
5682 const char *author, const char *committer,
5683 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5684 got_worktree_status_cb status_cb, void *status_arg,
5685 struct got_repository *repo)
5687 const struct got_error *err = NULL, *unlockerr = NULL;
5688 struct got_pathlist_entry *pe;
5689 const char *head_ref_name = NULL;
5690 struct got_commit_object *head_commit = NULL;
5691 struct got_reference *head_ref2 = NULL;
5692 struct got_object_id *head_commit_id2 = NULL;
5693 struct got_tree_object *head_tree = NULL;
5694 struct got_object_id *new_tree_id = NULL;
5695 int nentries, nparents = 0;
5696 struct got_object_id_queue parent_ids;
5697 struct got_object_qid *pid = NULL;
5698 char *logmsg = NULL;
5701 *new_commit_id = NULL;
5703 STAILQ_INIT(&parent_ids);
5705 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5709 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5713 if (commit_msg_cb != NULL) {
5714 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5719 if (logmsg == NULL || strlen(logmsg) == 0) {
5720 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5724 /* Create blobs from added and modified files and record their IDs. */
5725 TAILQ_FOREACH(pe, commitable_paths, entry) {
5726 struct got_commitable *ct = pe->data;
5729 /* Blobs for staged files already exist. */
5730 if (ct->staged_status == GOT_STATUS_ADD ||
5731 ct->staged_status == GOT_STATUS_MODIFY)
5734 if (ct->status != GOT_STATUS_ADD &&
5735 ct->status != GOT_STATUS_MODIFY &&
5736 ct->status != GOT_STATUS_MODE_CHANGE)
5739 if (asprintf(&ondisk_path, "%s/%s",
5740 worktree->root_path, pe->path) == -1) {
5741 err = got_error_from_errno("asprintf");
5744 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5750 /* Recursively write new tree objects. */
5751 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5752 commitable_paths, status_cb, status_arg, repo);
5756 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5759 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5762 err = got_object_qid_alloc(&pid, parent_id2);
5765 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5768 timestamp = time(NULL);
5769 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5770 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5776 /* Check if a concurrent commit to our branch has occurred. */
5777 head_ref_name = got_worktree_get_head_ref_name(worktree);
5778 if (head_ref_name == NULL) {
5779 err = got_error_from_errno("got_worktree_get_head_ref_name");
5782 /* Lock the reference here to prevent concurrent modification. */
5783 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5786 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5789 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5790 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5793 /* Update branch head in repository. */
5794 err = got_ref_change_ref(head_ref2, *new_commit_id);
5797 err = got_ref_write(head_ref2, repo);
5801 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5805 err = ref_base_commit(worktree, repo);
5809 got_object_id_queue_free(&parent_ids);
5811 got_object_tree_close(head_tree);
5813 got_object_commit_close(head_commit);
5814 free(head_commit_id2);
5816 unlockerr = got_ref_unlock(head_ref2);
5817 if (unlockerr && err == NULL)
5819 got_ref_close(head_ref2);
5824 static const struct got_error *
5825 check_path_is_commitable(const char *path,
5826 struct got_pathlist_head *commitable_paths)
5828 struct got_pathlist_entry *cpe = NULL;
5829 size_t path_len = strlen(path);
5831 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5832 struct got_commitable *ct = cpe->data;
5833 const char *ct_path = ct->path;
5835 while (ct_path[0] == '/')
5838 if (strcmp(path, ct_path) == 0 ||
5839 got_path_is_child(ct_path, path, path_len))
5844 return got_error_path(path, GOT_ERR_BAD_PATH);
5849 static const struct got_error *
5850 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5852 int *have_staged_files = arg;
5854 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5855 *have_staged_files = 1;
5856 return got_error(GOT_ERR_CANCELLED);
5862 static const struct got_error *
5863 check_non_staged_files(struct got_fileindex *fileindex,
5864 struct got_pathlist_head *paths)
5866 struct got_pathlist_entry *pe;
5867 struct got_fileindex_entry *ie;
5869 TAILQ_FOREACH(pe, paths, entry) {
5870 if (pe->path[0] == '\0')
5872 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5874 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5875 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5876 return got_error_path(pe->path,
5877 GOT_ERR_FILE_NOT_STAGED);
5883 const struct got_error *
5884 got_worktree_commit(struct got_object_id **new_commit_id,
5885 struct got_worktree *worktree, struct got_pathlist_head *paths,
5886 const char *author, const char *committer, int allow_bad_symlinks,
5887 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5888 got_worktree_status_cb status_cb, void *status_arg,
5889 struct got_repository *repo)
5891 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5892 struct got_fileindex *fileindex = NULL;
5893 char *fileindex_path = NULL;
5894 struct got_pathlist_head commitable_paths;
5895 struct collect_commitables_arg cc_arg;
5896 struct got_pathlist_entry *pe;
5897 struct got_reference *head_ref = NULL;
5898 struct got_object_id *head_commit_id = NULL;
5899 int have_staged_files = 0;
5901 *new_commit_id = NULL;
5903 TAILQ_INIT(&commitable_paths);
5905 err = lock_worktree(worktree, LOCK_EX);
5909 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5913 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5917 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5921 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5922 &have_staged_files);
5923 if (err && err->code != GOT_ERR_CANCELLED)
5925 if (have_staged_files) {
5926 err = check_non_staged_files(fileindex, paths);
5931 cc_arg.commitable_paths = &commitable_paths;
5932 cc_arg.worktree = worktree;
5933 cc_arg.fileindex = fileindex;
5935 cc_arg.have_staged_files = have_staged_files;
5936 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5937 TAILQ_FOREACH(pe, paths, entry) {
5938 err = worktree_status(worktree, pe->path, fileindex, repo,
5939 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5944 if (TAILQ_EMPTY(&commitable_paths)) {
5945 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5949 TAILQ_FOREACH(pe, paths, entry) {
5950 err = check_path_is_commitable(pe->path, &commitable_paths);
5955 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5956 struct got_commitable *ct = pe->data;
5957 const char *ct_path = ct->in_repo_path;
5959 while (ct_path[0] == '/')
5961 err = check_out_of_date(ct_path, ct->status,
5962 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5963 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5969 err = commit_worktree(new_commit_id, &commitable_paths,
5970 head_commit_id, NULL, worktree, author, committer,
5971 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5975 err = update_fileindex_after_commit(worktree, &commitable_paths,
5976 *new_commit_id, fileindex, have_staged_files);
5977 sync_err = sync_fileindex(fileindex, fileindex_path);
5978 if (sync_err && err == NULL)
5982 got_fileindex_free(fileindex);
5983 free(fileindex_path);
5984 unlockerr = lock_worktree(worktree, LOCK_SH);
5985 if (unlockerr && err == NULL)
5987 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5988 struct got_commitable *ct = pe->data;
5989 free_commitable(ct);
5991 got_pathlist_free(&commitable_paths);
5996 got_commitable_get_path(struct got_commitable *ct)
6002 got_commitable_get_status(struct got_commitable *ct)
6007 struct check_rebase_ok_arg {
6008 struct got_worktree *worktree;
6009 struct got_repository *repo;
6012 static const struct got_error *
6013 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6015 const struct got_error *err = NULL;
6016 struct check_rebase_ok_arg *a = arg;
6017 unsigned char status;
6021 /* Reject rebase of a work tree with mixed base commits. */
6022 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6023 SHA1_DIGEST_LENGTH))
6024 return got_error(GOT_ERR_MIXED_COMMITS);
6026 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6028 return got_error_from_errno("asprintf");
6030 /* Reject rebase of a work tree with modified or staged files. */
6031 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6036 if (status != GOT_STATUS_NO_CHANGE)
6037 return got_error(GOT_ERR_MODIFIED);
6038 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6039 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6044 const struct got_error *
6045 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6046 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6047 struct got_worktree *worktree, struct got_reference *branch,
6048 struct got_repository *repo)
6050 const struct got_error *err = NULL;
6051 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6052 char *branch_ref_name = NULL;
6053 char *fileindex_path = NULL;
6054 struct check_rebase_ok_arg ok_arg;
6055 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6056 struct got_object_id *wt_branch_tip = NULL;
6058 *new_base_branch_ref = NULL;
6062 err = lock_worktree(worktree, LOCK_EX);
6066 err = open_fileindex(fileindex, &fileindex_path, worktree);
6070 ok_arg.worktree = worktree;
6072 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6077 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6081 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6085 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6089 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6094 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6097 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6098 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6102 err = got_ref_alloc_symref(new_base_branch_ref,
6103 new_base_branch_ref_name, wt_branch);
6106 err = got_ref_write(*new_base_branch_ref, repo);
6110 /* TODO Lock original branch's ref while rebasing? */
6112 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6116 err = got_ref_write(branch_ref, repo);
6120 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6121 worktree->base_commit_id);
6124 err = got_ref_write(*tmp_branch, repo);
6128 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6132 free(fileindex_path);
6133 free(tmp_branch_name);
6134 free(new_base_branch_ref_name);
6135 free(branch_ref_name);
6137 got_ref_close(branch_ref);
6139 got_ref_close(wt_branch);
6140 free(wt_branch_tip);
6142 if (*new_base_branch_ref) {
6143 got_ref_close(*new_base_branch_ref);
6144 *new_base_branch_ref = NULL;
6147 got_ref_close(*tmp_branch);
6151 got_fileindex_free(*fileindex);
6154 lock_worktree(worktree, LOCK_SH);
6159 const struct got_error *
6160 got_worktree_rebase_continue(struct got_object_id **commit_id,
6161 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6162 struct got_reference **branch, struct got_fileindex **fileindex,
6163 struct got_worktree *worktree, struct got_repository *repo)
6165 const struct got_error *err;
6166 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6167 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6168 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6169 char *fileindex_path = NULL;
6170 int have_staged_files = 0;
6173 *new_base_branch = NULL;
6178 err = lock_worktree(worktree, LOCK_EX);
6182 err = open_fileindex(fileindex, &fileindex_path, worktree);
6186 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6187 &have_staged_files);
6188 if (err && err->code != GOT_ERR_CANCELLED)
6190 if (have_staged_files) {
6191 err = got_error(GOT_ERR_STAGED_PATHS);
6195 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6199 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6203 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6207 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6211 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6215 err = got_ref_open(branch, repo,
6216 got_ref_get_symref_target(branch_ref), 0);
6220 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6224 err = got_ref_resolve(commit_id, repo, commit_ref);
6228 err = got_ref_open(new_base_branch, repo,
6229 new_base_branch_ref_name, 0);
6233 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6237 free(commit_ref_name);
6238 free(branch_ref_name);
6239 free(fileindex_path);
6241 got_ref_close(commit_ref);
6243 got_ref_close(branch_ref);
6248 got_ref_close(*tmp_branch);
6251 if (*new_base_branch) {
6252 got_ref_close(*new_base_branch);
6253 *new_base_branch = NULL;
6256 got_ref_close(*branch);
6260 got_fileindex_free(*fileindex);
6263 lock_worktree(worktree, LOCK_SH);
6268 const struct got_error *
6269 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6271 const struct got_error *err;
6272 char *tmp_branch_name = NULL;
6274 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6278 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6279 free(tmp_branch_name);
6283 static const struct got_error *
6284 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6285 char **logmsg, void *arg)
6291 static const struct got_error *
6292 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6293 const char *path, struct got_object_id *blob_id,
6294 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6295 int dirfd, const char *de_name)
6300 struct collect_merged_paths_arg {
6301 got_worktree_checkout_cb progress_cb;
6303 struct got_pathlist_head *merged_paths;
6306 static const struct got_error *
6307 collect_merged_paths(void *arg, unsigned char status, const char *path)
6309 const struct got_error *err;
6310 struct collect_merged_paths_arg *a = arg;
6312 struct got_pathlist_entry *new;
6314 err = (*a->progress_cb)(a->progress_arg, status, path);
6318 if (status != GOT_STATUS_MERGE &&
6319 status != GOT_STATUS_ADD &&
6320 status != GOT_STATUS_DELETE &&
6321 status != GOT_STATUS_CONFLICT)
6326 return got_error_from_errno("strdup");
6328 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6329 if (err || new == NULL)
6335 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6337 struct got_pathlist_entry *pe;
6339 TAILQ_FOREACH(pe, merged_paths, entry)
6340 free((char *)pe->path);
6342 got_pathlist_free(merged_paths);
6345 static const struct got_error *
6346 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6347 int is_rebase, struct got_repository *repo)
6349 const struct got_error *err;
6350 struct got_reference *commit_ref = NULL;
6352 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6354 if (err->code != GOT_ERR_NOT_REF)
6356 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6359 err = got_ref_write(commit_ref, repo);
6362 } else if (is_rebase) {
6363 struct got_object_id *stored_id;
6366 err = got_ref_resolve(&stored_id, repo, commit_ref);
6369 cmp = got_object_id_cmp(commit_id, stored_id);
6372 err = got_error(GOT_ERR_REBASE_COMMITID);
6378 got_ref_close(commit_ref);
6382 static const struct got_error *
6383 rebase_merge_files(struct got_pathlist_head *merged_paths,
6384 const char *commit_ref_name, struct got_worktree *worktree,
6385 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6386 struct got_object_id *commit_id, struct got_repository *repo,
6387 got_worktree_checkout_cb progress_cb, void *progress_arg,
6388 got_cancel_cb cancel_cb, void *cancel_arg)
6390 const struct got_error *err;
6391 struct got_reference *commit_ref = NULL;
6392 struct collect_merged_paths_arg cmp_arg;
6393 char *fileindex_path;
6395 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6397 err = get_fileindex_path(&fileindex_path, worktree);
6401 cmp_arg.progress_cb = progress_cb;
6402 cmp_arg.progress_arg = progress_arg;
6403 cmp_arg.merged_paths = merged_paths;
6404 err = merge_files(worktree, fileindex, fileindex_path,
6405 parent_commit_id, commit_id, repo, collect_merged_paths,
6406 &cmp_arg, cancel_cb, cancel_arg);
6408 got_ref_close(commit_ref);
6412 const struct got_error *
6413 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6414 struct got_worktree *worktree, struct got_fileindex *fileindex,
6415 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6416 struct got_repository *repo,
6417 got_worktree_checkout_cb progress_cb, void *progress_arg,
6418 got_cancel_cb cancel_cb, void *cancel_arg)
6420 const struct got_error *err;
6421 char *commit_ref_name;
6423 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6427 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6431 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6432 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6433 progress_arg, cancel_cb, cancel_arg);
6435 free(commit_ref_name);
6439 const struct got_error *
6440 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6441 struct got_worktree *worktree, struct got_fileindex *fileindex,
6442 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6443 struct got_repository *repo,
6444 got_worktree_checkout_cb progress_cb, void *progress_arg,
6445 got_cancel_cb cancel_cb, void *cancel_arg)
6447 const struct got_error *err;
6448 char *commit_ref_name;
6450 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6454 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6458 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6459 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6460 progress_arg, cancel_cb, cancel_arg);
6462 free(commit_ref_name);
6466 static const struct got_error *
6467 rebase_commit(struct got_object_id **new_commit_id,
6468 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6469 struct got_worktree *worktree, struct got_fileindex *fileindex,
6470 struct got_reference *tmp_branch, const char *committer,
6471 struct got_commit_object *orig_commit, const char *new_logmsg,
6472 struct got_repository *repo)
6474 const struct got_error *err, *sync_err;
6475 struct got_pathlist_head commitable_paths;
6476 struct collect_commitables_arg cc_arg;
6477 char *fileindex_path = NULL;
6478 struct got_reference *head_ref = NULL;
6479 struct got_object_id *head_commit_id = NULL;
6480 char *logmsg = NULL;
6482 TAILQ_INIT(&commitable_paths);
6483 *new_commit_id = NULL;
6485 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6487 err = get_fileindex_path(&fileindex_path, worktree);
6491 cc_arg.commitable_paths = &commitable_paths;
6492 cc_arg.worktree = worktree;
6494 cc_arg.have_staged_files = 0;
6496 * If possible get the status of individual files directly to
6497 * avoid crawling the entire work tree once per rebased commit.
6499 * Ideally, merged_paths would contain a list of commitables
6500 * we could use so we could skip worktree_status() entirely.
6501 * However, we would then need carefully keep track of cumulative
6502 * effects of operations such as file additions and deletions
6503 * in 'got histedit -f' (folding multiple commits into one),
6504 * and this extra complexity is not really worth it.
6507 struct got_pathlist_entry *pe;
6508 TAILQ_FOREACH(pe, merged_paths, entry) {
6509 err = worktree_status(worktree, pe->path, fileindex,
6510 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6516 err = worktree_status(worktree, "", fileindex, repo,
6517 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6522 if (TAILQ_EMPTY(&commitable_paths)) {
6523 /* No-op change; commit will be elided. */
6524 err = got_ref_delete(commit_ref, repo);
6527 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6531 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6535 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6540 logmsg = strdup(new_logmsg);
6541 if (logmsg == NULL) {
6542 err = got_error_from_errno("strdup");
6546 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6551 /* NB: commit_worktree will call free(logmsg) */
6552 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6553 NULL, worktree, got_object_commit_get_author(orig_commit),
6554 committer ? committer :
6555 got_object_commit_get_committer(orig_commit),
6556 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6560 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6564 err = got_ref_delete(commit_ref, repo);
6568 err = update_fileindex_after_commit(worktree, &commitable_paths,
6569 *new_commit_id, fileindex, 0);
6570 sync_err = sync_fileindex(fileindex, fileindex_path);
6571 if (sync_err && err == NULL)
6574 free(fileindex_path);
6575 free(head_commit_id);
6577 got_ref_close(head_ref);
6579 free(*new_commit_id);
6580 *new_commit_id = NULL;
6585 const struct got_error *
6586 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6587 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6588 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6589 const char *committer, struct got_commit_object *orig_commit,
6590 struct got_object_id *orig_commit_id, struct got_repository *repo)
6592 const struct got_error *err;
6593 char *commit_ref_name;
6594 struct got_reference *commit_ref = NULL;
6595 struct got_object_id *commit_id = NULL;
6597 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6601 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6604 err = got_ref_resolve(&commit_id, repo, commit_ref);
6607 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6608 err = got_error(GOT_ERR_REBASE_COMMITID);
6612 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6613 worktree, fileindex, tmp_branch, committer, orig_commit,
6617 got_ref_close(commit_ref);
6618 free(commit_ref_name);
6623 const struct got_error *
6624 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6625 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6626 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6627 const char *committer, struct got_commit_object *orig_commit,
6628 struct got_object_id *orig_commit_id, const char *new_logmsg,
6629 struct got_repository *repo)
6631 const struct got_error *err;
6632 char *commit_ref_name;
6633 struct got_reference *commit_ref = NULL;
6635 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6639 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6643 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6644 worktree, fileindex, tmp_branch, committer, orig_commit,
6648 got_ref_close(commit_ref);
6649 free(commit_ref_name);
6653 const struct got_error *
6654 got_worktree_rebase_postpone(struct got_worktree *worktree,
6655 struct got_fileindex *fileindex)
6658 got_fileindex_free(fileindex);
6659 return lock_worktree(worktree, LOCK_SH);
6662 static const struct got_error *
6663 delete_ref(const char *name, struct got_repository *repo)
6665 const struct got_error *err;
6666 struct got_reference *ref;
6668 err = got_ref_open(&ref, repo, name, 0);
6670 if (err->code == GOT_ERR_NOT_REF)
6675 err = got_ref_delete(ref, repo);
6680 static const struct got_error *
6681 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6683 const struct got_error *err;
6684 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6685 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6687 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6690 err = delete_ref(tmp_branch_name, repo);
6694 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6697 err = delete_ref(new_base_branch_ref_name, repo);
6701 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6704 err = delete_ref(branch_ref_name, repo);
6708 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6711 err = delete_ref(commit_ref_name, repo);
6716 free(tmp_branch_name);
6717 free(new_base_branch_ref_name);
6718 free(branch_ref_name);
6719 free(commit_ref_name);
6723 static const struct got_error *
6724 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6725 struct got_object_id *new_commit_id, struct got_repository *repo)
6727 const struct got_error *err;
6728 struct got_reference *ref = NULL;
6729 struct got_object_id *old_commit_id = NULL;
6730 const char *branch_name = NULL;
6731 char *new_id_str = NULL;
6732 char *refname = NULL;
6734 branch_name = got_ref_get_name(branch);
6735 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6736 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6739 err = got_object_id_str(&new_id_str, new_commit_id);
6743 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6744 new_id_str) == -1) {
6745 err = got_error_from_errno("asprintf");
6749 err = got_ref_resolve(&old_commit_id, repo, branch);
6753 err = got_ref_alloc(&ref, refname, old_commit_id);
6757 err = got_ref_write(ref, repo);
6761 free(old_commit_id);
6767 const struct got_error *
6768 got_worktree_rebase_complete(struct got_worktree *worktree,
6769 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6770 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6771 struct got_repository *repo, int create_backup)
6773 const struct got_error *err, *unlockerr, *sync_err;
6774 struct got_object_id *new_head_commit_id = NULL;
6775 char *fileindex_path = NULL;
6777 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6781 if (create_backup) {
6782 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6783 rebased_branch, new_head_commit_id, repo);
6788 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6792 err = got_ref_write(rebased_branch, repo);
6796 err = got_worktree_set_head_ref(worktree, rebased_branch);
6800 err = delete_rebase_refs(worktree, repo);
6804 err = get_fileindex_path(&fileindex_path, worktree);
6807 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6808 sync_err = sync_fileindex(fileindex, fileindex_path);
6809 if (sync_err && err == NULL)
6812 got_fileindex_free(fileindex);
6813 free(fileindex_path);
6814 free(new_head_commit_id);
6815 unlockerr = lock_worktree(worktree, LOCK_SH);
6816 if (unlockerr && err == NULL)
6821 const struct got_error *
6822 got_worktree_rebase_abort(struct got_worktree *worktree,
6823 struct got_fileindex *fileindex, struct got_repository *repo,
6824 struct got_reference *new_base_branch,
6825 got_worktree_checkout_cb progress_cb, void *progress_arg)
6827 const struct got_error *err, *unlockerr, *sync_err;
6828 struct got_reference *resolved = NULL;
6829 struct got_object_id *commit_id = NULL;
6830 struct got_commit_object *commit = NULL;
6831 char *fileindex_path = NULL;
6832 struct revert_file_args rfa;
6833 struct got_object_id *tree_id = NULL;
6835 err = lock_worktree(worktree, LOCK_EX);
6839 err = got_object_open_as_commit(&commit, repo,
6840 worktree->base_commit_id);
6844 err = got_ref_open(&resolved, repo,
6845 got_ref_get_symref_target(new_base_branch), 0);
6849 err = got_worktree_set_head_ref(worktree, resolved);
6854 * XXX commits to the base branch could have happened while
6855 * we were busy rebasing; should we store the original commit ID
6856 * when rebase begins and read it back here?
6858 err = got_ref_resolve(&commit_id, repo, resolved);
6862 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6866 err = got_object_id_by_path(&tree_id, repo, commit,
6867 worktree->path_prefix);
6871 err = delete_rebase_refs(worktree, repo);
6875 err = get_fileindex_path(&fileindex_path, worktree);
6879 rfa.worktree = worktree;
6880 rfa.fileindex = fileindex;
6881 rfa.progress_cb = progress_cb;
6882 rfa.progress_arg = progress_arg;
6883 rfa.patch_cb = NULL;
6884 rfa.patch_arg = NULL;
6886 rfa.unlink_added_files = 0;
6887 err = worktree_status(worktree, "", fileindex, repo,
6888 revert_file, &rfa, NULL, NULL, 1, 0);
6892 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6893 repo, progress_cb, progress_arg, NULL, NULL);
6895 sync_err = sync_fileindex(fileindex, fileindex_path);
6896 if (sync_err && err == NULL)
6899 got_ref_close(resolved);
6903 got_object_commit_close(commit);
6905 got_fileindex_free(fileindex);
6906 free(fileindex_path);
6908 unlockerr = lock_worktree(worktree, LOCK_SH);
6909 if (unlockerr && err == NULL)
6914 const struct got_error *
6915 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6916 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6917 struct got_fileindex **fileindex, struct got_worktree *worktree,
6918 struct got_repository *repo)
6920 const struct got_error *err = NULL;
6921 char *tmp_branch_name = NULL;
6922 char *branch_ref_name = NULL;
6923 char *base_commit_ref_name = NULL;
6924 char *fileindex_path = NULL;
6925 struct check_rebase_ok_arg ok_arg;
6926 struct got_reference *wt_branch = NULL;
6927 struct got_reference *base_commit_ref = NULL;
6931 *base_commit_id = NULL;
6934 err = lock_worktree(worktree, LOCK_EX);
6938 err = open_fileindex(fileindex, &fileindex_path, worktree);
6942 ok_arg.worktree = worktree;
6944 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6949 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6953 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6957 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6962 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6967 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6971 err = got_ref_write(*branch_ref, repo);
6975 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6976 worktree->base_commit_id);
6979 err = got_ref_write(base_commit_ref, repo);
6982 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6983 if (*base_commit_id == NULL) {
6984 err = got_error_from_errno("got_object_id_dup");
6988 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6989 worktree->base_commit_id);
6992 err = got_ref_write(*tmp_branch, repo);
6996 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7000 free(fileindex_path);
7001 free(tmp_branch_name);
7002 free(branch_ref_name);
7003 free(base_commit_ref_name);
7005 got_ref_close(wt_branch);
7008 got_ref_close(*branch_ref);
7012 got_ref_close(*tmp_branch);
7015 free(*base_commit_id);
7017 got_fileindex_free(*fileindex);
7020 lock_worktree(worktree, LOCK_SH);
7025 const struct got_error *
7026 got_worktree_histedit_postpone(struct got_worktree *worktree,
7027 struct got_fileindex *fileindex)
7030 got_fileindex_free(fileindex);
7031 return lock_worktree(worktree, LOCK_SH);
7034 const struct got_error *
7035 got_worktree_histedit_in_progress(int *in_progress,
7036 struct got_worktree *worktree)
7038 const struct got_error *err;
7039 char *tmp_branch_name = NULL;
7041 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7045 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7046 free(tmp_branch_name);
7050 const struct got_error *
7051 got_worktree_histedit_continue(struct got_object_id **commit_id,
7052 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7053 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7054 struct got_worktree *worktree, struct got_repository *repo)
7056 const struct got_error *err;
7057 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7058 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7059 struct got_reference *commit_ref = NULL;
7060 struct got_reference *base_commit_ref = NULL;
7061 char *fileindex_path = NULL;
7062 int have_staged_files = 0;
7066 *base_commit_id = NULL;
7069 err = lock_worktree(worktree, LOCK_EX);
7073 err = open_fileindex(fileindex, &fileindex_path, worktree);
7077 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7078 &have_staged_files);
7079 if (err && err->code != GOT_ERR_CANCELLED)
7081 if (have_staged_files) {
7082 err = got_error(GOT_ERR_STAGED_PATHS);
7086 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7090 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7094 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7098 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7103 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7107 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7110 err = got_ref_resolve(commit_id, repo, commit_ref);
7114 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7117 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7121 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7125 free(commit_ref_name);
7126 free(branch_ref_name);
7127 free(fileindex_path);
7129 got_ref_close(commit_ref);
7130 if (base_commit_ref)
7131 got_ref_close(base_commit_ref);
7135 free(*base_commit_id);
7136 *base_commit_id = NULL;
7138 got_ref_close(*tmp_branch);
7142 got_fileindex_free(*fileindex);
7145 lock_worktree(worktree, LOCK_EX);
7150 static const struct got_error *
7151 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7153 const struct got_error *err;
7154 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7155 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7157 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7160 err = delete_ref(tmp_branch_name, repo);
7164 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7168 err = delete_ref(base_commit_ref_name, repo);
7172 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7175 err = delete_ref(branch_ref_name, repo);
7179 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7182 err = delete_ref(commit_ref_name, repo);
7186 free(tmp_branch_name);
7187 free(base_commit_ref_name);
7188 free(branch_ref_name);
7189 free(commit_ref_name);
7193 const struct got_error *
7194 got_worktree_histedit_abort(struct got_worktree *worktree,
7195 struct got_fileindex *fileindex, struct got_repository *repo,
7196 struct got_reference *branch, struct got_object_id *base_commit_id,
7197 got_worktree_checkout_cb progress_cb, void *progress_arg)
7199 const struct got_error *err, *unlockerr, *sync_err;
7200 struct got_reference *resolved = NULL;
7201 char *fileindex_path = NULL;
7202 struct got_commit_object *commit = NULL;
7203 struct got_object_id *tree_id = NULL;
7204 struct revert_file_args rfa;
7206 err = lock_worktree(worktree, LOCK_EX);
7210 err = got_object_open_as_commit(&commit, repo,
7211 worktree->base_commit_id);
7215 err = got_ref_open(&resolved, repo,
7216 got_ref_get_symref_target(branch), 0);
7220 err = got_worktree_set_head_ref(worktree, resolved);
7224 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7228 err = got_object_id_by_path(&tree_id, repo, commit,
7229 worktree->path_prefix);
7233 err = delete_histedit_refs(worktree, repo);
7237 err = get_fileindex_path(&fileindex_path, worktree);
7241 rfa.worktree = worktree;
7242 rfa.fileindex = fileindex;
7243 rfa.progress_cb = progress_cb;
7244 rfa.progress_arg = progress_arg;
7245 rfa.patch_cb = NULL;
7246 rfa.patch_arg = NULL;
7248 rfa.unlink_added_files = 0;
7249 err = worktree_status(worktree, "", fileindex, repo,
7250 revert_file, &rfa, NULL, NULL, 1, 0);
7254 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7255 repo, progress_cb, progress_arg, NULL, NULL);
7257 sync_err = sync_fileindex(fileindex, fileindex_path);
7258 if (sync_err && err == NULL)
7261 got_ref_close(resolved);
7263 free(fileindex_path);
7265 unlockerr = lock_worktree(worktree, LOCK_SH);
7266 if (unlockerr && err == NULL)
7271 const struct got_error *
7272 got_worktree_histedit_complete(struct got_worktree *worktree,
7273 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7274 struct got_reference *edited_branch, struct got_repository *repo)
7276 const struct got_error *err, *unlockerr, *sync_err;
7277 struct got_object_id *new_head_commit_id = NULL;
7278 struct got_reference *resolved = NULL;
7279 char *fileindex_path = NULL;
7281 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7285 err = got_ref_open(&resolved, repo,
7286 got_ref_get_symref_target(edited_branch), 0);
7290 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7291 resolved, new_head_commit_id, repo);
7295 err = got_ref_change_ref(resolved, new_head_commit_id);
7299 err = got_ref_write(resolved, repo);
7303 err = got_worktree_set_head_ref(worktree, resolved);
7307 err = delete_histedit_refs(worktree, repo);
7311 err = get_fileindex_path(&fileindex_path, worktree);
7314 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7315 sync_err = sync_fileindex(fileindex, fileindex_path);
7316 if (sync_err && err == NULL)
7319 got_fileindex_free(fileindex);
7320 free(fileindex_path);
7321 free(new_head_commit_id);
7322 unlockerr = lock_worktree(worktree, LOCK_SH);
7323 if (unlockerr && err == NULL)
7328 const struct got_error *
7329 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7330 struct got_object_id *commit_id, struct got_repository *repo)
7332 const struct got_error *err;
7333 char *commit_ref_name;
7335 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7339 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7343 err = delete_ref(commit_ref_name, repo);
7345 free(commit_ref_name);
7349 const struct got_error *
7350 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7351 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7352 struct got_worktree *worktree, const char *refname,
7353 struct got_repository *repo)
7355 const struct got_error *err = NULL;
7356 char *fileindex_path = NULL;
7357 struct check_rebase_ok_arg ok_arg;
7361 *base_branch_ref = NULL;
7363 err = lock_worktree(worktree, LOCK_EX);
7367 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7368 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7369 "cannot integrate a branch into itself; "
7370 "update -b or different branch name required");
7374 err = open_fileindex(fileindex, &fileindex_path, worktree);
7378 /* Preconditions are the same as for rebase. */
7379 ok_arg.worktree = worktree;
7381 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7386 err = got_ref_open(branch_ref, repo, refname, 1);
7390 err = got_ref_open(base_branch_ref, repo,
7391 got_worktree_get_head_ref_name(worktree), 1);
7395 got_ref_close(*branch_ref);
7398 if (*base_branch_ref) {
7399 got_ref_close(*base_branch_ref);
7400 *base_branch_ref = NULL;
7403 got_fileindex_free(*fileindex);
7406 lock_worktree(worktree, LOCK_SH);
7411 const struct got_error *
7412 got_worktree_integrate_continue(struct got_worktree *worktree,
7413 struct got_fileindex *fileindex, struct got_repository *repo,
7414 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7415 got_worktree_checkout_cb progress_cb, void *progress_arg,
7416 got_cancel_cb cancel_cb, void *cancel_arg)
7418 const struct got_error *err = NULL, *sync_err, *unlockerr;
7419 char *fileindex_path = NULL;
7420 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7421 struct got_commit_object *commit = NULL;
7423 err = get_fileindex_path(&fileindex_path, worktree);
7427 err = got_ref_resolve(&commit_id, repo, branch_ref);
7431 err = got_object_open_as_commit(&commit, repo, commit_id);
7435 err = got_object_id_by_path(&tree_id, repo, commit,
7436 worktree->path_prefix);
7440 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7444 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7445 progress_cb, progress_arg, cancel_cb, cancel_arg);
7449 err = got_ref_change_ref(base_branch_ref, commit_id);
7453 err = got_ref_write(base_branch_ref, repo);
7457 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7459 sync_err = sync_fileindex(fileindex, fileindex_path);
7460 if (sync_err && err == NULL)
7464 unlockerr = got_ref_unlock(branch_ref);
7465 if (unlockerr && err == NULL)
7467 got_ref_close(branch_ref);
7469 unlockerr = got_ref_unlock(base_branch_ref);
7470 if (unlockerr && err == NULL)
7472 got_ref_close(base_branch_ref);
7474 got_fileindex_free(fileindex);
7475 free(fileindex_path);
7478 got_object_commit_close(commit);
7480 unlockerr = lock_worktree(worktree, LOCK_SH);
7481 if (unlockerr && err == NULL)
7486 const struct got_error *
7487 got_worktree_integrate_abort(struct got_worktree *worktree,
7488 struct got_fileindex *fileindex, struct got_repository *repo,
7489 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7491 const struct got_error *err = NULL, *unlockerr = NULL;
7493 got_fileindex_free(fileindex);
7495 err = lock_worktree(worktree, LOCK_SH);
7497 unlockerr = got_ref_unlock(branch_ref);
7498 if (unlockerr && err == NULL)
7500 got_ref_close(branch_ref);
7502 unlockerr = got_ref_unlock(base_branch_ref);
7503 if (unlockerr && err == NULL)
7505 got_ref_close(base_branch_ref);
7510 const struct got_error *
7511 got_worktree_merge_postpone(struct got_worktree *worktree,
7512 struct got_fileindex *fileindex)
7514 const struct got_error *err, *sync_err;
7515 char *fileindex_path = NULL;
7517 err = get_fileindex_path(&fileindex_path, worktree);
7521 sync_err = sync_fileindex(fileindex, fileindex_path);
7523 err = lock_worktree(worktree, LOCK_SH);
7524 if (sync_err && err == NULL)
7527 got_fileindex_free(fileindex);
7528 free(fileindex_path);
7532 static const struct got_error *
7533 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7535 const struct got_error *err;
7536 char *branch_refname = NULL, *commit_refname = NULL;
7538 err = get_merge_branch_ref_name(&branch_refname, worktree);
7541 err = delete_ref(branch_refname, repo);
7545 err = get_merge_commit_ref_name(&commit_refname, worktree);
7548 err = delete_ref(commit_refname, repo);
7553 free(branch_refname);
7554 free(commit_refname);
7558 struct merge_commit_msg_arg {
7559 struct got_worktree *worktree;
7560 const char *branch_name;
7563 static const struct got_error *
7564 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7567 struct merge_commit_msg_arg *a = arg;
7569 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7570 got_worktree_get_head_ref_name(a->worktree)) == -1)
7571 return got_error_from_errno("asprintf");
7577 const struct got_error *
7578 got_worktree_merge_branch(struct got_worktree *worktree,
7579 struct got_fileindex *fileindex,
7580 struct got_object_id *yca_commit_id,
7581 struct got_object_id *branch_tip,
7582 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7583 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7585 const struct got_error *err;
7586 char *fileindex_path = NULL;
7588 err = get_fileindex_path(&fileindex_path, worktree);
7592 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7597 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7598 branch_tip, repo, progress_cb, progress_arg,
7599 cancel_cb, cancel_arg);
7601 free(fileindex_path);
7605 const struct got_error *
7606 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7607 struct got_worktree *worktree, struct got_fileindex *fileindex,
7608 const char *author, const char *committer, int allow_bad_symlinks,
7609 struct got_object_id *branch_tip, const char *branch_name,
7610 struct got_repository *repo,
7611 got_worktree_status_cb status_cb, void *status_arg)
7614 const struct got_error *err = NULL, *sync_err;
7615 struct got_pathlist_head commitable_paths;
7616 struct collect_commitables_arg cc_arg;
7617 struct got_pathlist_entry *pe;
7618 struct got_reference *head_ref = NULL;
7619 struct got_object_id *head_commit_id = NULL;
7620 int have_staged_files = 0;
7621 struct merge_commit_msg_arg mcm_arg;
7622 char *fileindex_path = NULL;
7624 *new_commit_id = NULL;
7626 TAILQ_INIT(&commitable_paths);
7628 err = get_fileindex_path(&fileindex_path, worktree);
7632 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7636 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7640 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7641 &have_staged_files);
7642 if (err && err->code != GOT_ERR_CANCELLED)
7644 if (have_staged_files) {
7645 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7649 cc_arg.commitable_paths = &commitable_paths;
7650 cc_arg.worktree = worktree;
7651 cc_arg.fileindex = fileindex;
7653 cc_arg.have_staged_files = have_staged_files;
7654 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7655 err = worktree_status(worktree, "", fileindex, repo,
7656 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7660 if (TAILQ_EMPTY(&commitable_paths)) {
7661 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7662 "merge of %s cannot proceed", branch_name);
7666 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7667 struct got_commitable *ct = pe->data;
7668 const char *ct_path = ct->in_repo_path;
7670 while (ct_path[0] == '/')
7672 err = check_out_of_date(ct_path, ct->status,
7673 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7674 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7680 mcm_arg.worktree = worktree;
7681 mcm_arg.branch_name = branch_name;
7682 err = commit_worktree(new_commit_id, &commitable_paths,
7683 head_commit_id, branch_tip, worktree, author, committer,
7684 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7688 err = update_fileindex_after_commit(worktree, &commitable_paths,
7689 *new_commit_id, fileindex, have_staged_files);
7690 sync_err = sync_fileindex(fileindex, fileindex_path);
7691 if (sync_err && err == NULL)
7694 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7695 struct got_commitable *ct = pe->data;
7696 free_commitable(ct);
7698 got_pathlist_free(&commitable_paths);
7699 free(fileindex_path);
7703 const struct got_error *
7704 got_worktree_merge_complete(struct got_worktree *worktree,
7705 struct got_fileindex *fileindex, struct got_repository *repo)
7707 const struct got_error *err, *unlockerr, *sync_err;
7708 char *fileindex_path = NULL;
7710 err = delete_merge_refs(worktree, repo);
7714 err = get_fileindex_path(&fileindex_path, worktree);
7717 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7718 sync_err = sync_fileindex(fileindex, fileindex_path);
7719 if (sync_err && err == NULL)
7722 got_fileindex_free(fileindex);
7723 free(fileindex_path);
7724 unlockerr = lock_worktree(worktree, LOCK_SH);
7725 if (unlockerr && err == NULL)
7730 const struct got_error *
7731 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7732 struct got_repository *repo)
7734 const struct got_error *err;
7735 char *branch_refname = NULL;
7736 struct got_reference *branch_ref = NULL;
7740 err = get_merge_branch_ref_name(&branch_refname, worktree);
7743 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7744 free(branch_refname);
7746 if (err->code != GOT_ERR_NOT_REF)
7754 const struct got_error *got_worktree_merge_prepare(
7755 struct got_fileindex **fileindex, struct got_worktree *worktree,
7756 struct got_reference *branch, struct got_repository *repo)
7758 const struct got_error *err = NULL;
7759 char *fileindex_path = NULL;
7760 char *branch_refname = NULL, *commit_refname = NULL;
7761 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7762 struct got_reference *commit_ref = NULL;
7763 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7764 struct check_rebase_ok_arg ok_arg;
7768 err = lock_worktree(worktree, LOCK_EX);
7772 err = open_fileindex(fileindex, &fileindex_path, worktree);
7776 /* Preconditions are the same as for rebase. */
7777 ok_arg.worktree = worktree;
7779 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7784 err = get_merge_branch_ref_name(&branch_refname, worktree);
7788 err = get_merge_commit_ref_name(&commit_refname, worktree);
7792 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7797 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7801 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7802 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7806 err = got_ref_resolve(&branch_tip, repo, branch);
7810 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7813 err = got_ref_write(branch_ref, repo);
7817 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7820 err = got_ref_write(commit_ref, repo);
7825 free(branch_refname);
7826 free(commit_refname);
7827 free(fileindex_path);
7829 got_ref_close(branch_ref);
7831 got_ref_close(commit_ref);
7833 got_ref_close(wt_branch);
7834 free(wt_branch_tip);
7837 got_fileindex_free(*fileindex);
7840 lock_worktree(worktree, LOCK_SH);
7845 const struct got_error *
7846 got_worktree_merge_continue(char **branch_name,
7847 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7848 struct got_worktree *worktree, struct got_repository *repo)
7850 const struct got_error *err;
7851 char *commit_refname = NULL, *branch_refname = NULL;
7852 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7853 char *fileindex_path = NULL;
7854 int have_staged_files = 0;
7856 *branch_name = NULL;
7860 err = lock_worktree(worktree, LOCK_EX);
7864 err = open_fileindex(fileindex, &fileindex_path, worktree);
7868 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7869 &have_staged_files);
7870 if (err && err->code != GOT_ERR_CANCELLED)
7872 if (have_staged_files) {
7873 err = got_error(GOT_ERR_STAGED_PATHS);
7877 err = get_merge_branch_ref_name(&branch_refname, worktree);
7881 err = get_merge_commit_ref_name(&commit_refname, worktree);
7885 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7889 if (!got_ref_is_symbolic(branch_ref)) {
7890 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7891 "%s is not a symbolic reference",
7892 got_ref_get_name(branch_ref));
7895 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7896 if (*branch_name == NULL) {
7897 err = got_error_from_errno("strdup");
7901 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7905 err = got_ref_resolve(branch_tip, repo, commit_ref);
7909 free(commit_refname);
7910 free(branch_refname);
7911 free(fileindex_path);
7913 got_ref_close(commit_ref);
7915 got_ref_close(branch_ref);
7919 *branch_name = NULL;
7924 got_fileindex_free(*fileindex);
7927 lock_worktree(worktree, LOCK_SH);
7932 const struct got_error *
7933 got_worktree_merge_abort(struct got_worktree *worktree,
7934 struct got_fileindex *fileindex, struct got_repository *repo,
7935 got_worktree_checkout_cb progress_cb, void *progress_arg)
7937 const struct got_error *err, *unlockerr, *sync_err;
7938 struct got_object_id *commit_id = NULL;
7939 struct got_commit_object *commit = NULL;
7940 char *fileindex_path = NULL;
7941 struct revert_file_args rfa;
7942 struct got_object_id *tree_id = NULL;
7944 err = got_object_open_as_commit(&commit, repo,
7945 worktree->base_commit_id);
7949 err = got_object_id_by_path(&tree_id, repo, commit,
7950 worktree->path_prefix);
7954 err = delete_merge_refs(worktree, repo);
7958 err = get_fileindex_path(&fileindex_path, worktree);
7962 rfa.worktree = worktree;
7963 rfa.fileindex = fileindex;
7964 rfa.progress_cb = progress_cb;
7965 rfa.progress_arg = progress_arg;
7966 rfa.patch_cb = NULL;
7967 rfa.patch_arg = NULL;
7969 rfa.unlink_added_files = 1;
7970 err = worktree_status(worktree, "", fileindex, repo,
7971 revert_file, &rfa, NULL, NULL, 1, 0);
7975 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7976 repo, progress_cb, progress_arg, NULL, NULL);
7978 sync_err = sync_fileindex(fileindex, fileindex_path);
7979 if (sync_err && err == NULL)
7985 got_object_commit_close(commit);
7987 got_fileindex_free(fileindex);
7988 free(fileindex_path);
7990 unlockerr = lock_worktree(worktree, LOCK_SH);
7991 if (unlockerr && err == NULL)
7996 struct check_stage_ok_arg {
7997 struct got_object_id *head_commit_id;
7998 struct got_worktree *worktree;
7999 struct got_fileindex *fileindex;
8000 struct got_repository *repo;
8004 static const struct got_error *
8005 check_stage_ok(void *arg, unsigned char status,
8006 unsigned char staged_status, const char *relpath,
8007 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8008 struct got_object_id *commit_id, int dirfd, const char *de_name)
8010 struct check_stage_ok_arg *a = arg;
8011 const struct got_error *err = NULL;
8012 struct got_fileindex_entry *ie;
8013 struct got_object_id base_commit_id;
8014 struct got_object_id *base_commit_idp = NULL;
8015 char *in_repo_path = NULL, *p;
8017 if (status == GOT_STATUS_UNVERSIONED ||
8018 status == GOT_STATUS_NO_CHANGE)
8020 if (status == GOT_STATUS_NONEXISTENT)
8021 return got_error_set_errno(ENOENT, relpath);
8023 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8025 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8027 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8028 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8030 return got_error_from_errno("asprintf");
8032 if (got_fileindex_entry_has_commit(ie)) {
8033 memcpy(base_commit_id.sha1, ie->commit_sha1,
8034 SHA1_DIGEST_LENGTH);
8035 base_commit_idp = &base_commit_id;
8038 if (status == GOT_STATUS_CONFLICT) {
8039 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8041 } else if (status != GOT_STATUS_ADD &&
8042 status != GOT_STATUS_MODIFY &&
8043 status != GOT_STATUS_DELETE) {
8044 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8048 a->have_changes = 1;
8053 err = check_out_of_date(p, status, staged_status,
8054 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8055 GOT_ERR_STAGE_OUT_OF_DATE);
8061 struct stage_path_arg {
8062 struct got_worktree *worktree;
8063 struct got_fileindex *fileindex;
8064 struct got_repository *repo;
8065 got_worktree_status_cb status_cb;
8067 got_worktree_patch_cb patch_cb;
8069 int staged_something;
8070 int allow_bad_symlinks;
8073 static const struct got_error *
8074 stage_path(void *arg, unsigned char status,
8075 unsigned char staged_status, const char *relpath,
8076 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8077 struct got_object_id *commit_id, int dirfd, const char *de_name)
8079 struct stage_path_arg *a = arg;
8080 const struct got_error *err = NULL;
8081 struct got_fileindex_entry *ie;
8082 char *ondisk_path = NULL, *path_content = NULL;
8084 struct got_object_id *new_staged_blob_id = NULL;
8087 if (status == GOT_STATUS_UNVERSIONED)
8090 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8092 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8094 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8096 return got_error_from_errno("asprintf");
8099 case GOT_STATUS_ADD:
8100 case GOT_STATUS_MODIFY:
8101 /* XXX could sb.st_mode be passed in by our caller? */
8102 if (lstat(ondisk_path, &sb) == -1) {
8103 err = got_error_from_errno2("lstat", ondisk_path);
8107 if (status == GOT_STATUS_ADD) {
8108 int choice = GOT_PATCH_CHOICE_NONE;
8109 err = (*a->patch_cb)(&choice, a->patch_arg,
8110 status, ie->path, NULL, 1, 1);
8113 if (choice != GOT_PATCH_CHOICE_YES)
8116 err = create_patched_content(&path_content, 0,
8117 staged_blob_id ? staged_blob_id : blob_id,
8118 ondisk_path, dirfd, de_name, ie->path,
8119 a->repo, a->patch_cb, a->patch_arg);
8120 if (err || path_content == NULL)
8124 err = got_object_blob_create(&new_staged_blob_id,
8125 path_content ? path_content : ondisk_path, a->repo);
8128 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8129 SHA1_DIGEST_LENGTH);
8130 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8131 stage = GOT_FILEIDX_STAGE_ADD;
8133 stage = GOT_FILEIDX_STAGE_MODIFY;
8134 got_fileindex_entry_stage_set(ie, stage);
8135 if (S_ISLNK(sb.st_mode)) {
8136 int is_bad_symlink = 0;
8137 if (!a->allow_bad_symlinks) {
8138 char target_path[PATH_MAX];
8140 target_len = readlink(ondisk_path, target_path,
8141 sizeof(target_path));
8142 if (target_len == -1) {
8143 err = got_error_from_errno2("readlink",
8147 err = is_bad_symlink_target(&is_bad_symlink,
8148 target_path, target_len, ondisk_path,
8149 a->worktree->root_path);
8152 if (is_bad_symlink) {
8153 err = got_error_path(ondisk_path,
8154 GOT_ERR_BAD_SYMLINK);
8159 got_fileindex_entry_staged_filetype_set(ie,
8160 GOT_FILEIDX_MODE_BAD_SYMLINK);
8162 got_fileindex_entry_staged_filetype_set(ie,
8163 GOT_FILEIDX_MODE_SYMLINK);
8165 got_fileindex_entry_staged_filetype_set(ie,
8166 GOT_FILEIDX_MODE_REGULAR_FILE);
8168 a->staged_something = 1;
8169 if (a->status_cb == NULL)
8171 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8172 get_staged_status(ie), relpath, blob_id,
8173 new_staged_blob_id, NULL, dirfd, de_name);
8177 * When staging the reverse of the staged diff,
8178 * implicitly unstage the file.
8180 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8181 sizeof(ie->blob_sha1)) == 0) {
8182 got_fileindex_entry_stage_set(ie,
8183 GOT_FILEIDX_STAGE_NONE);
8186 case GOT_STATUS_DELETE:
8187 if (staged_status == GOT_STATUS_DELETE)
8190 int choice = GOT_PATCH_CHOICE_NONE;
8191 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8192 ie->path, NULL, 1, 1);
8195 if (choice == GOT_PATCH_CHOICE_NO)
8197 if (choice != GOT_PATCH_CHOICE_YES) {
8198 err = got_error(GOT_ERR_PATCH_CHOICE);
8202 stage = GOT_FILEIDX_STAGE_DELETE;
8203 got_fileindex_entry_stage_set(ie, stage);
8204 a->staged_something = 1;
8205 if (a->status_cb == NULL)
8207 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8208 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8211 case GOT_STATUS_NO_CHANGE:
8213 case GOT_STATUS_CONFLICT:
8214 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8216 case GOT_STATUS_NONEXISTENT:
8217 err = got_error_set_errno(ENOENT, relpath);
8220 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8224 if (path_content && unlink(path_content) == -1 && err == NULL)
8225 err = got_error_from_errno2("unlink", path_content);
8228 free(new_staged_blob_id);
8232 const struct got_error *
8233 got_worktree_stage(struct got_worktree *worktree,
8234 struct got_pathlist_head *paths,
8235 got_worktree_status_cb status_cb, void *status_arg,
8236 got_worktree_patch_cb patch_cb, void *patch_arg,
8237 int allow_bad_symlinks, struct got_repository *repo)
8239 const struct got_error *err = NULL, *sync_err, *unlockerr;
8240 struct got_pathlist_entry *pe;
8241 struct got_fileindex *fileindex = NULL;
8242 char *fileindex_path = NULL;
8243 struct got_reference *head_ref = NULL;
8244 struct got_object_id *head_commit_id = NULL;
8245 struct check_stage_ok_arg oka;
8246 struct stage_path_arg spa;
8248 err = lock_worktree(worktree, LOCK_EX);
8252 err = got_ref_open(&head_ref, repo,
8253 got_worktree_get_head_ref_name(worktree), 0);
8256 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8259 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8263 /* Check pre-conditions before staging anything. */
8264 oka.head_commit_id = head_commit_id;
8265 oka.worktree = worktree;
8266 oka.fileindex = fileindex;
8268 oka.have_changes = 0;
8269 TAILQ_FOREACH(pe, paths, entry) {
8270 err = worktree_status(worktree, pe->path, fileindex, repo,
8271 check_stage_ok, &oka, NULL, NULL, 1, 0);
8275 if (!oka.have_changes) {
8276 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8280 spa.worktree = worktree;
8281 spa.fileindex = fileindex;
8283 spa.patch_cb = patch_cb;
8284 spa.patch_arg = patch_arg;
8285 spa.status_cb = status_cb;
8286 spa.status_arg = status_arg;
8287 spa.staged_something = 0;
8288 spa.allow_bad_symlinks = allow_bad_symlinks;
8289 TAILQ_FOREACH(pe, paths, entry) {
8290 err = worktree_status(worktree, pe->path, fileindex, repo,
8291 stage_path, &spa, NULL, NULL, 1, 0);
8295 if (!spa.staged_something) {
8296 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8300 sync_err = sync_fileindex(fileindex, fileindex_path);
8301 if (sync_err && err == NULL)
8305 got_ref_close(head_ref);
8306 free(head_commit_id);
8307 free(fileindex_path);
8309 got_fileindex_free(fileindex);
8310 unlockerr = lock_worktree(worktree, LOCK_SH);
8311 if (unlockerr && err == NULL)
8316 struct unstage_path_arg {
8317 struct got_worktree *worktree;
8318 struct got_fileindex *fileindex;
8319 struct got_repository *repo;
8320 got_worktree_checkout_cb progress_cb;
8322 got_worktree_patch_cb patch_cb;
8326 static const struct got_error *
8327 create_unstaged_content(char **path_unstaged_content,
8328 char **path_new_staged_content, struct got_object_id *blob_id,
8329 struct got_object_id *staged_blob_id, const char *relpath,
8330 struct got_repository *repo,
8331 got_worktree_patch_cb patch_cb, void *patch_arg)
8333 const struct got_error *err, *free_err;
8334 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8335 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8336 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8337 struct got_diffreg_result *diffreg_result = NULL;
8338 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8339 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8340 int fd1 = -1, fd2 = -1;
8342 *path_unstaged_content = NULL;
8343 *path_new_staged_content = NULL;
8345 err = got_object_id_str(&label1, blob_id);
8349 fd1 = got_opentempfd();
8351 err = got_error_from_errno("got_opentempfd");
8354 fd2 = got_opentempfd();
8356 err = got_error_from_errno("got_opentempfd");
8360 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8364 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8368 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8372 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8377 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8381 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8385 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8386 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8390 err = got_opentemp_named(path_unstaged_content, &outfile,
8391 "got-unstaged-content");
8394 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8395 "got-new-staged-content");
8399 if (fseek(f1, 0L, SEEK_SET) == -1) {
8400 err = got_ferror(f1, GOT_ERR_IO);
8403 if (fseek(f2, 0L, SEEK_SET) == -1) {
8404 err = got_ferror(f2, GOT_ERR_IO);
8407 /* Count the number of actual changes in the diff result. */
8408 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8409 struct diff_chunk_context cc = {};
8410 diff_chunk_context_load_change(&cc, &nchunks_used,
8411 diffreg_result->result, n, 0);
8414 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8416 err = apply_or_reject_change(&choice, &nchunks_used,
8417 diffreg_result->result, n, relpath, f1, f2,
8418 &line_cur1, &line_cur2,
8419 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8422 if (choice == GOT_PATCH_CHOICE_YES)
8425 have_rejected_content = 1;
8426 if (choice == GOT_PATCH_CHOICE_QUIT)
8429 if (have_content || have_rejected_content)
8430 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8431 outfile, rejectfile);
8434 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8435 err = got_error_from_errno("close");
8437 got_object_blob_close(blob);
8438 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8439 err = got_error_from_errno("close");
8441 got_object_blob_close(staged_blob);
8442 free_err = got_diffreg_result_free(diffreg_result);
8443 if (free_err && err == NULL)
8445 if (f1 && fclose(f1) == EOF && err == NULL)
8446 err = got_error_from_errno2("fclose", path1);
8447 if (f2 && fclose(f2) == EOF && err == NULL)
8448 err = got_error_from_errno2("fclose", path2);
8449 if (outfile && fclose(outfile) == EOF && err == NULL)
8450 err = got_error_from_errno2("fclose", *path_unstaged_content);
8451 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8452 err = got_error_from_errno2("fclose", *path_new_staged_content);
8453 if (path1 && unlink(path1) == -1 && err == NULL)
8454 err = got_error_from_errno2("unlink", path1);
8455 if (path2 && unlink(path2) == -1 && err == NULL)
8456 err = got_error_from_errno2("unlink", path2);
8457 if (err || !have_content) {
8458 if (*path_unstaged_content &&
8459 unlink(*path_unstaged_content) == -1 && err == NULL)
8460 err = got_error_from_errno2("unlink",
8461 *path_unstaged_content);
8462 free(*path_unstaged_content);
8463 *path_unstaged_content = NULL;
8465 if (err || !have_content || !have_rejected_content) {
8466 if (*path_new_staged_content &&
8467 unlink(*path_new_staged_content) == -1 && err == NULL)
8468 err = got_error_from_errno2("unlink",
8469 *path_new_staged_content);
8470 free(*path_new_staged_content);
8471 *path_new_staged_content = NULL;
8478 static const struct got_error *
8479 unstage_hunks(struct got_object_id *staged_blob_id,
8480 struct got_blob_object *blob_base,
8481 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8482 const char *ondisk_path, const char *label_orig,
8483 struct got_worktree *worktree, struct got_repository *repo,
8484 got_worktree_patch_cb patch_cb, void *patch_arg,
8485 got_worktree_checkout_cb progress_cb, void *progress_arg)
8487 const struct got_error *err = NULL;
8488 char *path_unstaged_content = NULL;
8489 char *path_new_staged_content = NULL;
8490 char *parent = NULL, *base_path = NULL;
8491 char *blob_base_path = NULL;
8492 struct got_object_id *new_staged_blob_id = NULL;
8493 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8496 err = create_unstaged_content(&path_unstaged_content,
8497 &path_new_staged_content, blob_id, staged_blob_id,
8498 ie->path, repo, patch_cb, patch_arg);
8502 if (path_unstaged_content == NULL)
8505 if (path_new_staged_content) {
8506 err = got_object_blob_create(&new_staged_blob_id,
8507 path_new_staged_content, repo);
8512 f = fopen(path_unstaged_content, "re");
8514 err = got_error_from_errno2("fopen",
8515 path_unstaged_content);
8518 if (fstat(fileno(f), &sb) == -1) {
8519 err = got_error_from_errno2("fstat", path_unstaged_content);
8522 if (got_fileindex_entry_staged_filetype_get(ie) ==
8523 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8524 char link_target[PATH_MAX];
8526 r = fread(link_target, 1, sizeof(link_target), f);
8527 if (r == 0 && ferror(f)) {
8528 err = got_error_from_errno("fread");
8531 if (r >= sizeof(link_target)) { /* should not happen */
8532 err = got_error(GOT_ERR_NO_SPACE);
8535 link_target[r] = '\0';
8536 err = merge_symlink(worktree, blob_base,
8537 ondisk_path, ie->path, label_orig, link_target,
8538 worktree->base_commit_id, repo, progress_cb,
8541 int local_changes_subsumed;
8543 err = got_path_dirname(&parent, ondisk_path);
8547 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8549 err = got_error_from_errno("asprintf");
8554 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8557 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8563 * In order the run a 3-way merge with a symlink we copy the symlink's
8564 * target path into a temporary file and use that file with diff3.
8566 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8567 err = dump_symlink_target_path_to_file(&f_deriv2,
8573 fd = open(ondisk_path,
8574 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8576 err = got_error_from_errno2("open", ondisk_path);
8579 f_deriv2 = fdopen(fd, "r");
8580 if (f_deriv2 == NULL) {
8581 err = got_error_from_errno2("fdopen", ondisk_path);
8587 err = merge_file(&local_changes_subsumed, worktree,
8588 f_base, f, f_deriv2, ondisk_path, ie->path,
8589 got_fileindex_perms_to_st(ie),
8590 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8591 repo, progress_cb, progress_arg);
8596 if (new_staged_blob_id) {
8597 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8598 SHA1_DIGEST_LENGTH);
8600 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8601 got_fileindex_entry_staged_filetype_set(ie, 0);
8604 free(new_staged_blob_id);
8605 if (path_unstaged_content &&
8606 unlink(path_unstaged_content) == -1 && err == NULL)
8607 err = got_error_from_errno2("unlink", path_unstaged_content);
8608 if (path_new_staged_content &&
8609 unlink(path_new_staged_content) == -1 && err == NULL)
8610 err = got_error_from_errno2("unlink", path_new_staged_content);
8611 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8612 err = got_error_from_errno2("unlink", blob_base_path);
8613 if (f_base && fclose(f_base) == EOF && err == NULL)
8614 err = got_error_from_errno2("fclose", path_unstaged_content);
8615 if (f && fclose(f) == EOF && err == NULL)
8616 err = got_error_from_errno2("fclose", path_unstaged_content);
8617 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8618 err = got_error_from_errno2("fclose", ondisk_path);
8619 free(path_unstaged_content);
8620 free(path_new_staged_content);
8621 free(blob_base_path);
8627 static const struct got_error *
8628 unstage_path(void *arg, unsigned char status,
8629 unsigned char staged_status, const char *relpath,
8630 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8631 struct got_object_id *commit_id, int dirfd, const char *de_name)
8633 const struct got_error *err = NULL;
8634 struct unstage_path_arg *a = arg;
8635 struct got_fileindex_entry *ie;
8636 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8637 char *ondisk_path = NULL;
8638 char *id_str = NULL, *label_orig = NULL;
8639 int local_changes_subsumed;
8641 int fd1 = -1, fd2 = -1;
8643 if (staged_status != GOT_STATUS_ADD &&
8644 staged_status != GOT_STATUS_MODIFY &&
8645 staged_status != GOT_STATUS_DELETE)
8648 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8650 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8652 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8654 return got_error_from_errno("asprintf");
8656 err = got_object_id_str(&id_str,
8657 commit_id ? commit_id : a->worktree->base_commit_id);
8660 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8662 err = got_error_from_errno("asprintf");
8666 fd1 = got_opentempfd();
8668 err = got_error_from_errno("got_opentempfd");
8671 fd2 = got_opentempfd();
8673 err = got_error_from_errno("got_opentempfd");
8677 switch (staged_status) {
8678 case GOT_STATUS_MODIFY:
8679 err = got_object_open_as_blob(&blob_base, a->repo,
8680 blob_id, 8192, fd1);
8684 case GOT_STATUS_ADD:
8686 if (staged_status == GOT_STATUS_ADD) {
8687 int choice = GOT_PATCH_CHOICE_NONE;
8688 err = (*a->patch_cb)(&choice, a->patch_arg,
8689 staged_status, ie->path, NULL, 1, 1);
8692 if (choice != GOT_PATCH_CHOICE_YES)
8695 err = unstage_hunks(staged_blob_id,
8696 blob_base, blob_id, ie, ondisk_path,
8697 label_orig, a->worktree, a->repo,
8698 a->patch_cb, a->patch_arg,
8699 a->progress_cb, a->progress_arg);
8700 break; /* Done with this file. */
8703 err = got_object_open_as_blob(&blob_staged, a->repo,
8704 staged_blob_id, 8192, fd2);
8707 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8708 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8709 case GOT_FILEIDX_MODE_REGULAR_FILE:
8710 err = merge_blob(&local_changes_subsumed, a->worktree,
8711 blob_base, ondisk_path, relpath,
8712 got_fileindex_perms_to_st(ie), label_orig,
8713 blob_staged, commit_id ? commit_id :
8714 a->worktree->base_commit_id, a->repo,
8715 a->progress_cb, a->progress_arg);
8717 case GOT_FILEIDX_MODE_SYMLINK:
8718 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8719 char *staged_target;
8720 err = got_object_blob_read_to_str(
8721 &staged_target, blob_staged);
8724 err = merge_symlink(a->worktree, blob_base,
8725 ondisk_path, relpath, label_orig,
8726 staged_target, commit_id ? commit_id :
8727 a->worktree->base_commit_id,
8728 a->repo, a->progress_cb, a->progress_arg);
8729 free(staged_target);
8731 err = merge_blob(&local_changes_subsumed,
8732 a->worktree, blob_base, ondisk_path,
8733 relpath, got_fileindex_perms_to_st(ie),
8734 label_orig, blob_staged,
8735 commit_id ? commit_id :
8736 a->worktree->base_commit_id, a->repo,
8737 a->progress_cb, a->progress_arg);
8741 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8745 got_fileindex_entry_stage_set(ie,
8746 GOT_FILEIDX_STAGE_NONE);
8747 got_fileindex_entry_staged_filetype_set(ie, 0);
8750 case GOT_STATUS_DELETE:
8752 int choice = GOT_PATCH_CHOICE_NONE;
8753 err = (*a->patch_cb)(&choice, a->patch_arg,
8754 staged_status, ie->path, NULL, 1, 1);
8757 if (choice == GOT_PATCH_CHOICE_NO)
8759 if (choice != GOT_PATCH_CHOICE_YES) {
8760 err = got_error(GOT_ERR_PATCH_CHOICE);
8764 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8765 got_fileindex_entry_staged_filetype_set(ie, 0);
8766 err = get_file_status(&status, &sb, ie, ondisk_path,
8767 dirfd, de_name, a->repo);
8770 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8775 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8776 err = got_error_from_errno("close");
8778 got_object_blob_close(blob_base);
8779 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8780 err = got_error_from_errno("close");
8782 got_object_blob_close(blob_staged);
8788 const struct got_error *
8789 got_worktree_unstage(struct got_worktree *worktree,
8790 struct got_pathlist_head *paths,
8791 got_worktree_checkout_cb progress_cb, void *progress_arg,
8792 got_worktree_patch_cb patch_cb, void *patch_arg,
8793 struct got_repository *repo)
8795 const struct got_error *err = NULL, *sync_err, *unlockerr;
8796 struct got_pathlist_entry *pe;
8797 struct got_fileindex *fileindex = NULL;
8798 char *fileindex_path = NULL;
8799 struct unstage_path_arg upa;
8801 err = lock_worktree(worktree, LOCK_EX);
8805 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8809 upa.worktree = worktree;
8810 upa.fileindex = fileindex;
8812 upa.progress_cb = progress_cb;
8813 upa.progress_arg = progress_arg;
8814 upa.patch_cb = patch_cb;
8815 upa.patch_arg = patch_arg;
8816 TAILQ_FOREACH(pe, paths, entry) {
8817 err = worktree_status(worktree, pe->path, fileindex, repo,
8818 unstage_path, &upa, NULL, NULL, 1, 0);
8823 sync_err = sync_fileindex(fileindex, fileindex_path);
8824 if (sync_err && err == NULL)
8827 free(fileindex_path);
8829 got_fileindex_free(fileindex);
8830 unlockerr = lock_worktree(worktree, LOCK_SH);
8831 if (unlockerr && err == NULL)
8836 struct report_file_info_arg {
8837 struct got_worktree *worktree;
8838 got_worktree_path_info_cb info_cb;
8840 struct got_pathlist_head *paths;
8841 got_cancel_cb cancel_cb;
8845 static const struct got_error *
8846 report_file_info(void *arg, struct got_fileindex_entry *ie)
8848 struct report_file_info_arg *a = arg;
8849 struct got_pathlist_entry *pe;
8850 struct got_object_id blob_id, staged_blob_id, commit_id;
8851 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8852 struct got_object_id *commit_idp = NULL;
8855 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8856 return got_error(GOT_ERR_CANCELLED);
8858 TAILQ_FOREACH(pe, a->paths, entry) {
8859 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8860 got_path_is_child(ie->path, pe->path, pe->path_len))
8863 if (pe == NULL) /* not found */
8866 if (got_fileindex_entry_has_blob(ie)) {
8867 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8868 blob_idp = &blob_id;
8870 stage = got_fileindex_entry_stage_get(ie);
8871 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8872 stage == GOT_FILEIDX_STAGE_ADD) {
8873 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8874 SHA1_DIGEST_LENGTH);
8875 staged_blob_idp = &staged_blob_id;
8878 if (got_fileindex_entry_has_commit(ie)) {
8879 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8880 commit_idp = &commit_id;
8883 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8884 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8887 const struct got_error *
8888 got_worktree_path_info(struct got_worktree *worktree,
8889 struct got_pathlist_head *paths,
8890 got_worktree_path_info_cb info_cb, void *info_arg,
8891 got_cancel_cb cancel_cb, void *cancel_arg)
8894 const struct got_error *err = NULL, *unlockerr;
8895 struct got_fileindex *fileindex = NULL;
8896 char *fileindex_path = NULL;
8897 struct report_file_info_arg arg;
8899 err = lock_worktree(worktree, LOCK_SH);
8903 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8907 arg.worktree = worktree;
8908 arg.info_cb = info_cb;
8909 arg.info_arg = info_arg;
8911 arg.cancel_cb = cancel_cb;
8912 arg.cancel_arg = cancel_arg;
8913 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8916 free(fileindex_path);
8918 got_fileindex_free(fileindex);
8919 unlockerr = lock_worktree(worktree, LOCK_UN);
8920 if (unlockerr && err == NULL)
8925 static const struct got_error *
8926 patch_check_path(const char *p, char **path, unsigned char *status,
8927 unsigned char *staged_status, struct got_fileindex *fileindex,
8928 struct got_worktree *worktree, struct got_repository *repo)
8930 const struct got_error *err;
8931 struct got_fileindex_entry *ie;
8933 char *ondisk_path = NULL;
8935 err = got_worktree_resolve_path(path, worktree, p);
8939 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8940 *path[0] ? "/" : "", *path) == -1)
8941 return got_error_from_errno("asprintf");
8943 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8945 *staged_status = get_staged_status(ie);
8946 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8951 *staged_status = GOT_STATUS_NO_CHANGE;
8952 *status = GOT_STATUS_UNVERSIONED;
8953 if (lstat(ondisk_path, &sb) == -1) {
8954 if (errno != ENOENT) {
8955 err = got_error_from_errno2("lstat",
8959 *status = GOT_STATUS_NONEXISTENT;
8968 static const struct got_error *
8969 patch_can_rm(const char *path, unsigned char status,
8970 unsigned char staged_status)
8972 if (status == GOT_STATUS_NONEXISTENT)
8973 return got_error_set_errno(ENOENT, path);
8974 if (status != GOT_STATUS_NO_CHANGE &&
8975 status != GOT_STATUS_ADD &&
8976 status != GOT_STATUS_MODIFY &&
8977 status != GOT_STATUS_MODE_CHANGE)
8978 return got_error_path(path, GOT_ERR_FILE_STATUS);
8979 if (staged_status == GOT_STATUS_DELETE)
8980 return got_error_path(path, GOT_ERR_FILE_STATUS);
8984 static const struct got_error *
8985 patch_can_add(const char *path, unsigned char status)
8987 if (status != GOT_STATUS_NONEXISTENT)
8988 return got_error_path(path, GOT_ERR_FILE_STATUS);
8992 static const struct got_error *
8993 patch_can_edit(const char *path, unsigned char status,
8994 unsigned char staged_status)
8996 if (status == GOT_STATUS_NONEXISTENT)
8997 return got_error_set_errno(ENOENT, path);
8998 if (status != GOT_STATUS_NO_CHANGE &&
8999 status != GOT_STATUS_ADD &&
9000 status != GOT_STATUS_MODIFY)
9001 return got_error_path(path, GOT_ERR_FILE_STATUS);
9002 if (staged_status == GOT_STATUS_DELETE)
9003 return got_error_path(path, GOT_ERR_FILE_STATUS);
9007 const struct got_error *
9008 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9009 char **fileindex_path, struct got_worktree *worktree)
9011 return open_fileindex(fileindex, fileindex_path, worktree);
9014 const struct got_error *
9015 got_worktree_patch_check_path(const char *old, const char *new,
9016 char **oldpath, char **newpath, struct got_worktree *worktree,
9017 struct got_repository *repo, struct got_fileindex *fileindex)
9019 const struct got_error *err = NULL;
9020 int file_renamed = 0;
9021 unsigned char status_old, staged_status_old;
9022 unsigned char status_new, staged_status_new;
9027 err = patch_check_path(old != NULL ? old : new, oldpath,
9028 &status_old, &staged_status_old, fileindex, worktree, repo);
9032 err = patch_check_path(new != NULL ? new : old, newpath,
9033 &status_new, &staged_status_new, fileindex, worktree, repo);
9037 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9040 if (old != NULL && new == NULL)
9041 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9042 else if (file_renamed) {
9043 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9045 err = patch_can_add(*newpath, status_new);
9046 } else if (old == NULL)
9047 err = patch_can_add(*newpath, status_new);
9049 err = patch_can_edit(*newpath, status_new, staged_status_new);
9061 const struct got_error *
9062 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9063 struct got_worktree *worktree, struct got_fileindex *fileindex,
9064 got_worktree_checkout_cb progress_cb, void *progress_arg)
9066 struct schedule_addition_args saa;
9068 memset(&saa, 0, sizeof(saa));
9069 saa.worktree = worktree;
9070 saa.fileindex = fileindex;
9071 saa.progress_cb = progress_cb;
9072 saa.progress_arg = progress_arg;
9075 return worktree_status(worktree, path, fileindex, repo,
9076 schedule_addition, &saa, NULL, NULL, 1, 0);
9079 const struct got_error *
9080 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9081 struct got_worktree *worktree, struct got_fileindex *fileindex,
9082 got_worktree_delete_cb progress_cb, void *progress_arg)
9084 struct schedule_deletion_args sda;
9086 memset(&sda, 0, sizeof(sda));
9087 sda.worktree = worktree;
9088 sda.fileindex = fileindex;
9089 sda.progress_cb = progress_cb;
9090 sda.progress_arg = progress_arg;
9092 sda.delete_local_mods = 0;
9093 sda.keep_on_disk = 0;
9094 sda.ignore_missing_paths = 0;
9095 sda.status_codes = NULL;
9097 return worktree_status(worktree, path, fileindex, repo,
9098 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9101 const struct got_error *
9102 got_worktree_patch_complete(struct got_fileindex *fileindex,
9103 const char *fileindex_path)
9105 const struct got_error *err = NULL;
9107 err = sync_fileindex(fileindex, fileindex_path);
9108 got_fileindex_free(fileindex);