2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
129 return got_error_from_errno("got_ref_to_str");
131 refstr = strdup(got_ref_get_name(head_ref));
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
147 uint32_t uuid_status;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
160 err = got_ref_resolve(&commit_id, repo, head_ref);
163 err = got_object_get_type(&obj_type, repo, commit_id);
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
301 err = write_head_ref(path_got, head_ref);
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
336 err = got_object_open(&obj, repo, commit_id);
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
361 got_object_close(obj);
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
413 size_t flen1 = 0, flen2 = 0;
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
432 } else if (flen2 == 0) {
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
466 if (size1 != size2) {
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
495 return got_error_from_errno("fread");
499 outlen = write(outfd, fbuf, flen);
501 return got_error_from_errno("write");
503 return got_error(GOT_ERR_IO);
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
587 err = got_error_from_errno("dprintf");
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
596 err = got_error_from_errno("dprintf");
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
672 if (err->code != GOT_ERR_FILE_BINARY)
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
706 if (fchmod(fileno(f_merged), st_mode) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
779 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
780 err = got_error_from_errno2("fchmod", path);
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
852 ondisk_target[ondisk_len] = '\0';
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
916 free(ancestor_target);
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
988 err = got_error_from_errno("asprintf");
993 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
996 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1003 * No common ancestor exists. This is an "add vs add" conflict
1004 * and we simply use an empty ancestor file to make both files
1005 * appear in the merged result in their entirety.
1007 f_orig = got_opentemp();
1008 if (f_orig == NULL) {
1009 err = got_error_from_errno("got_opentemp");
1014 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 err = got_error_from_errno("asprintf");
1020 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1023 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1028 err = got_object_id_str(&id_str, deriv_base_commit_id);
1031 if (asprintf(&label_deriv, "%s: commit %s",
1032 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 err = got_error_from_errno("asprintf");
1038 * In order the run a 3-way merge with a symlink we copy the symlink's
1039 * target path into a temporary file and use that file with diff3.
1041 if (S_ISLNK(st_mode)) {
1042 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1047 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 err = got_error_from_errno2("open", ondisk_path);
1052 f_deriv2 = fdopen(fd, "r");
1053 if (f_deriv2 == NULL) {
1054 err = got_error_from_errno2("fdopen", ondisk_path);
1060 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1071 if (blob_orig_path) {
1072 unlink(blob_orig_path);
1073 free(blob_orig_path);
1075 if (blob_deriv_path) {
1076 unlink(blob_deriv_path);
1077 free(blob_deriv_path);
1085 static const struct got_error *
1086 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 int wt_fd, const char *path, struct got_object_id *blob_id)
1090 const struct got_error *err = NULL;
1091 struct got_fileindex_entry *new_ie;
1095 err = got_fileindex_entry_alloc(&new_ie, path);
1099 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 blob_id->sha1, base_commit_id->sha1, 1);
1104 err = got_fileindex_entry_add(fileindex, new_ie);
1107 got_fileindex_entry_free(new_ie);
1114 get_ondisk_perms(int executable, mode_t st_mode)
1116 mode_t xbits = S_IXUSR;
1119 /* Map read bits to execute bits. */
1120 if (st_mode & S_IRGRP)
1122 if (st_mode & S_IROTH)
1124 return st_mode | xbits;
1127 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1130 /* forward declaration */
1131 static const struct got_error *
1132 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 const char *path, mode_t te_mode, mode_t st_mode,
1134 struct got_blob_object *blob, int restoring_missing_file,
1135 int reverting_versioned_file, int installing_bad_symlink,
1136 int path_is_unversioned, struct got_repository *repo,
1137 got_worktree_checkout_cb progress_cb, void *progress_arg);
1140 * This function assumes that the provided symlink target points at a
1141 * safe location in the work tree!
1143 static const struct got_error *
1144 replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 const char *target_path, size_t target_len)
1147 const struct got_error *err = NULL;
1149 char etarget[PATH_MAX];
1155 * "Bad" symlinks (those pointing outside the work tree or into the
1156 * .got directory) are installed in the work tree as a regular file
1157 * which contains the bad symlink target path.
1158 * The new symlink target has already been checked for safety by our
1159 * caller. If we can successfully open a regular file then we simply
1160 * replace this file with a symlink below.
1162 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1164 if (!got_err_open_nofollow_on_symlink())
1165 return got_error_from_errno2("open", ondisk_path);
1167 /* We are updating an existing on-disk symlink. */
1168 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1170 return got_error_from_errno2("readlink", ondisk_path);
1172 if (elen == target_len &&
1173 memcmp(etarget, target_path, target_len) == 0)
1174 return NULL; /* nothing to do */
1178 err = update_symlink(ondisk_path, target_path, target_len);
1179 if (fd != -1 && close(fd) == -1 && err == NULL)
1180 err = got_error_from_errno2("close", ondisk_path);
1184 static const struct got_error *
1185 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1188 const struct got_error *err = NULL;
1189 char canonpath[PATH_MAX];
1190 char *path_got = NULL;
1192 *is_bad_symlink = 0;
1194 if (target_len >= sizeof(canonpath)) {
1195 *is_bad_symlink = 1;
1200 * We do not use realpath(3) to resolve the symlink's target
1201 * path because we don't want to resolve symlinks recursively.
1202 * Instead we make the path absolute and then canonicalize it.
1203 * Relative symlink target lookup should begin at the directory
1204 * in which the blob object is being installed.
1206 if (!got_path_is_absolute(target_path)) {
1207 char *abspath, *parent;
1208 err = got_path_dirname(&parent, ondisk_path);
1211 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1213 return got_error_from_errno("asprintf");
1216 if (strlen(abspath) >= sizeof(canonpath)) {
1217 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1221 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1226 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1231 /* Only allow symlinks pointing at paths within the work tree. */
1232 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 *is_bad_symlink = 1;
1237 /* Do not allow symlinks pointing into the .got directory. */
1238 if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 GOT_WORKTREE_GOT_DIR) == -1)
1240 return got_error_from_errno("asprintf");
1241 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 *is_bad_symlink = 1;
1248 static const struct got_error *
1249 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 int restoring_missing_file, int reverting_versioned_file,
1252 int path_is_unversioned, int allow_bad_symlinks,
1253 struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *path_got = NULL;
1260 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1263 *is_bad_symlink = 0;
1266 * Blob object content specifies the target path of the link.
1267 * If a symbolic link cannot be installed we instead create
1268 * a regular file which contains the link target path stored
1269 * in the blob object.
1272 err = got_object_blob_read_block(&len, blob);
1273 if (len + target_len >= sizeof(target_path)) {
1274 /* Path too long; install as a regular file. */
1275 *is_bad_symlink = 1;
1276 got_object_blob_rewind(blob);
1277 return install_blob(worktree, ondisk_path, path,
1278 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 restoring_missing_file, reverting_versioned_file,
1280 1, path_is_unversioned, repo, progress_cb,
1284 /* Skip blob object header first time around. */
1285 memcpy(target_path + target_len, buf + hdrlen,
1287 target_len += len - hdrlen;
1291 target_path[target_len] = '\0';
1293 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 ondisk_path, worktree->root_path);
1298 if (*is_bad_symlink && !allow_bad_symlinks) {
1299 /* install as a regular file */
1300 got_object_blob_rewind(blob);
1301 err = install_blob(worktree, ondisk_path, path,
1302 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 restoring_missing_file, reverting_versioned_file, 1,
1304 path_is_unversioned, repo, progress_cb, progress_arg);
1308 if (symlink(target_path, ondisk_path) == -1) {
1309 if (errno == EEXIST) {
1310 int symlink_replaced;
1311 if (path_is_unversioned) {
1312 err = (*progress_cb)(progress_arg,
1313 GOT_STATUS_UNVERSIONED, path);
1316 err = replace_existing_symlink(&symlink_replaced,
1317 ondisk_path, target_path, target_len);
1321 if (symlink_replaced) {
1322 err = (*progress_cb)(progress_arg,
1323 reverting_versioned_file ?
1325 GOT_STATUS_UPDATE, path);
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_EXISTS, path);
1331 goto done; /* Nothing else to do. */
1334 if (errno == ENOENT) {
1336 err = got_path_dirname(&parent, ondisk_path);
1339 err = add_dir_on_disk(worktree, parent);
1344 * Retry, and fall through to error handling
1345 * below if this second attempt fails.
1347 if (symlink(target_path, ondisk_path) != -1) {
1348 err = NULL; /* success */
1353 /* Handle errors from first or second creation attempt. */
1354 if (errno == ENAMETOOLONG) {
1355 /* bad target path; install as a regular file */
1356 *is_bad_symlink = 1;
1357 got_object_blob_rewind(blob);
1358 err = install_blob(worktree, ondisk_path, path,
1359 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 restoring_missing_file, reverting_versioned_file, 1,
1361 path_is_unversioned, repo,
1362 progress_cb, progress_arg);
1363 } else if (errno == ENOTDIR) {
1364 err = got_error_path(ondisk_path,
1365 GOT_ERR_FILE_OBSTRUCTED);
1367 err = got_error_from_errno3("symlink",
1368 target_path, ondisk_path);
1370 } else if (progress_cb)
1371 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1378 static const struct got_error *
1379 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 const char *path, mode_t te_mode, mode_t st_mode,
1381 struct got_blob_object *blob, int restoring_missing_file,
1382 int reverting_versioned_file, int installing_bad_symlink,
1383 int path_is_unversioned, struct got_repository *repo,
1384 got_worktree_checkout_cb progress_cb, void *progress_arg)
1386 const struct got_error *err = NULL;
1390 char *tmppath = NULL;
1392 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1395 if (errno == ENOENT) {
1397 err = got_path_dirname(&parent, path);
1400 err = add_dir_on_disk(worktree, parent);
1404 fd = open(ondisk_path,
1405 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 GOT_DEFAULT_FILE_MODE);
1408 return got_error_from_errno2("open",
1410 } else if (errno == EEXIST) {
1411 if (path_is_unversioned) {
1412 err = (*progress_cb)(progress_arg,
1413 GOT_STATUS_UNVERSIONED, path);
1416 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 /* TODO file is obstructed; do something */
1419 err = got_error_path(ondisk_path,
1420 GOT_ERR_FILE_OBSTRUCTED);
1423 err = got_opentemp_named_fd(&tmppath, &fd,
1430 return got_error_from_errno2("open", ondisk_path);
1433 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 err = got_error_from_errno2("fchmod",
1435 update ? tmppath : ondisk_path);
1440 if (restoring_missing_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1443 else if (reverting_versioned_file)
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1447 err = (*progress_cb)(progress_arg,
1448 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1453 hdrlen = got_object_blob_get_hdrlen(blob);
1455 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 err = got_object_blob_read_block(&len, blob);
1460 /* Skip blob object header first time around. */
1461 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1463 err = got_error_from_errno("write");
1465 } else if (outlen != len - hdrlen) {
1466 err = got_error(GOT_ERR_IO);
1473 if (fsync(fd) != 0) {
1474 err = got_error_from_errno("fsync");
1479 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 err = got_error_from_errno2("unlink", ondisk_path);
1483 if (rename(tmppath, ondisk_path) != 0) {
1484 err = got_error_from_errno3("rename", tmppath,
1493 if (fd != -1 && close(fd) == -1 && err == NULL)
1494 err = got_error_from_errno("close");
1495 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 err = got_error_from_errno2("unlink", tmppath);
1501 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 static const struct got_error *
1503 get_modified_file_content_status(unsigned char *status, FILE *f)
1505 const struct got_error *err = NULL;
1506 const char *markers[3] = {
1507 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 GOT_DIFF_CONFLICT_MARKER_SEP,
1509 GOT_DIFF_CONFLICT_MARKER_END
1513 size_t linesize = 0;
1516 while (*status == GOT_STATUS_MODIFY) {
1517 linelen = getline(&line, &linesize, f);
1518 if (linelen == -1) {
1521 err = got_ferror(f, GOT_ERR_IO);
1525 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1528 *status = GOT_STATUS_CONFLICT;
1539 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1541 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1546 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1548 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 ie->size == (sb->st_size & 0xffffffff) &&
1553 !xbit_differs(ie, sb->st_mode));
1556 static unsigned char
1557 get_staged_status(struct got_fileindex_entry *ie)
1559 switch (got_fileindex_entry_stage_get(ie)) {
1560 case GOT_FILEIDX_STAGE_ADD:
1561 return GOT_STATUS_ADD;
1562 case GOT_FILEIDX_STAGE_DELETE:
1563 return GOT_STATUS_DELETE;
1564 case GOT_FILEIDX_STAGE_MODIFY:
1565 return GOT_STATUS_MODIFY;
1567 return GOT_STATUS_NO_CHANGE;
1571 static const struct got_error *
1572 get_symlink_modification_status(unsigned char *status,
1573 struct got_fileindex_entry *ie, const char *abspath,
1574 int dirfd, const char *de_name, struct got_blob_object *blob)
1576 const struct got_error *err = NULL;
1577 char target_path[PATH_MAX];
1578 char etarget[PATH_MAX];
1580 size_t len, target_len = 0;
1581 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1584 *status = GOT_STATUS_NO_CHANGE;
1586 /* Blob object content specifies the target path of the link. */
1588 err = got_object_blob_read_block(&len, blob);
1591 if (len + target_len >= sizeof(target_path)) {
1593 * Should not happen. The blob contents were OK
1594 * when this symlink was installed.
1596 return got_error(GOT_ERR_NO_SPACE);
1599 /* Skip blob object header first time around. */
1600 memcpy(target_path + target_len, buf + hdrlen,
1602 target_len += len - hdrlen;
1606 target_path[target_len] = '\0';
1609 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1611 return got_error_from_errno2("readlinkat", abspath);
1613 elen = readlink(abspath, etarget, sizeof(etarget));
1615 return got_error_from_errno2("readlink", abspath);
1618 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 *status = GOT_STATUS_MODIFY;
1624 static const struct got_error *
1625 get_file_status(unsigned char *status, struct stat *sb,
1626 struct got_fileindex_entry *ie, const char *abspath,
1627 int dirfd, const char *de_name, struct got_repository *repo)
1629 const struct got_error *err = NULL;
1630 struct got_object_id id;
1632 int fd = -1, fd1 = -1;
1635 struct got_blob_object *blob = NULL;
1637 unsigned char staged_status = get_staged_status(ie);
1639 *status = GOT_STATUS_NO_CHANGE;
1640 memset(sb, 0, sizeof(*sb));
1643 * Whenever the caller provides a directory descriptor and a
1644 * directory entry name for the file, use them! This prevents
1645 * race conditions if filesystem paths change beneath our feet.
1648 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 if (errno == ENOENT) {
1650 if (got_fileindex_entry_has_file_on_disk(ie))
1651 *status = GOT_STATUS_MISSING;
1653 *status = GOT_STATUS_DELETE;
1656 err = got_error_from_errno2("fstatat", abspath);
1660 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 if (fd == -1 && errno != ENOENT &&
1662 !got_err_open_nofollow_on_symlink())
1663 return got_error_from_errno2("open", abspath);
1664 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 if (lstat(abspath, sb) == -1)
1666 return got_error_from_errno2("lstat", abspath);
1667 } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 if (errno == ENOENT) {
1669 if (got_fileindex_entry_has_file_on_disk(ie))
1670 *status = GOT_STATUS_MISSING;
1672 *status = GOT_STATUS_DELETE;
1675 err = got_error_from_errno2("fstat", abspath);
1680 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 *status = GOT_STATUS_OBSTRUCTED;
1685 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 *status = GOT_STATUS_DELETE;
1688 } else if (!got_fileindex_entry_has_blob(ie) &&
1689 staged_status != GOT_STATUS_ADD) {
1690 *status = GOT_STATUS_ADD;
1694 if (!stat_info_differs(ie, sb))
1697 if (S_ISLNK(sb->st_mode) &&
1698 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 *status = GOT_STATUS_MODIFY;
1703 if (staged_status == GOT_STATUS_MODIFY ||
1704 staged_status == GOT_STATUS_ADD)
1705 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1707 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1709 fd1 = got_opentempfd();
1711 err = got_error_from_errno("got_opentempfd");
1714 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1718 if (S_ISLNK(sb->st_mode)) {
1719 err = get_symlink_modification_status(status, ie,
1720 abspath, dirfd, de_name, blob);
1725 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1727 err = got_error_from_errno2("openat", abspath);
1732 f = fdopen(fd, "r");
1734 err = got_error_from_errno2("fdopen", abspath);
1738 hdrlen = got_object_blob_get_hdrlen(blob);
1740 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1741 err = got_object_blob_read_block(&blen, blob);
1744 /* Skip length of blob object header first time around. */
1745 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1746 if (flen == 0 && ferror(f)) {
1747 err = got_error_from_errno("fread");
1750 if (blen - hdrlen == 0) {
1752 *status = GOT_STATUS_MODIFY;
1754 } else if (flen == 0) {
1755 if (blen - hdrlen != 0)
1756 *status = GOT_STATUS_MODIFY;
1758 } else if (blen - hdrlen == flen) {
1759 /* Skip blob object header first time around. */
1760 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1761 *status = GOT_STATUS_MODIFY;
1765 *status = GOT_STATUS_MODIFY;
1771 if (*status == GOT_STATUS_MODIFY) {
1773 err = get_modified_file_content_status(status, f);
1774 } else if (xbit_differs(ie, sb->st_mode))
1775 *status = GOT_STATUS_MODE_CHANGE;
1777 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1778 err = got_error_from_errno("close");
1780 got_object_blob_close(blob);
1781 if (f != NULL && fclose(f) == EOF && err == NULL)
1782 err = got_error_from_errno2("fclose", abspath);
1783 if (fd != -1 && close(fd) == -1 && err == NULL)
1784 err = got_error_from_errno2("close", abspath);
1789 * Update timestamps in the file index if a file is unmodified and
1790 * we had to run a full content comparison to find out.
1792 static const struct got_error *
1793 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1794 struct got_fileindex_entry *ie, struct stat *sb)
1796 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1797 return got_fileindex_entry_update(ie, wt_fd, path,
1798 ie->blob_sha1, ie->commit_sha1, 1);
1803 static const struct got_error *
1804 update_blob(struct got_worktree *worktree,
1805 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1806 struct got_tree_entry *te, const char *path,
1807 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1810 const struct got_error *err = NULL;
1811 struct got_blob_object *blob = NULL;
1812 char *ondisk_path = NULL;
1813 unsigned char status = GOT_STATUS_NO_CHANGE;
1815 int fd1 = -1, fd2 = -1;
1817 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1818 return got_error_from_errno("asprintf");
1821 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1822 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1825 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1829 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1830 sb.st_mode = got_fileindex_perms_to_st(ie);
1832 if (stat(ondisk_path, &sb) == -1) {
1833 if (errno != ENOENT) {
1834 err = got_error_from_errno2("stat",
1838 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1839 status = GOT_STATUS_UNVERSIONED;
1841 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1842 status = GOT_STATUS_UNVERSIONED;
1844 status = GOT_STATUS_OBSTRUCTED;
1848 if (status == GOT_STATUS_OBSTRUCTED) {
1850 got_fileindex_entry_mark_skipped(ie);
1851 err = (*progress_cb)(progress_arg, status, path);
1854 if (status == GOT_STATUS_CONFLICT) {
1856 got_fileindex_entry_mark_skipped(ie);
1857 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1862 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1863 (S_ISLNK(te->mode) ||
1864 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1866 * This is a regular file or an installed bad symlink.
1867 * If the file index indicates that this file is already
1868 * up-to-date with respect to the repository we can skip
1869 * updating contents of this file.
1871 if (got_fileindex_entry_has_commit(ie) &&
1872 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1873 SHA1_DIGEST_LENGTH) == 0) {
1875 err = sync_timestamps(worktree->root_fd,
1876 path, status, ie, &sb);
1879 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1883 if (got_fileindex_entry_has_blob(ie) &&
1884 memcmp(ie->blob_sha1, te->id.sha1,
1885 SHA1_DIGEST_LENGTH) == 0) {
1886 /* Different commit but the same blob. */
1887 err = sync_timestamps(worktree->root_fd,
1888 path, status, ie, &sb);
1891 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1897 fd1 = got_opentempfd();
1899 err = got_error_from_errno("got_opentempfd");
1902 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1906 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1907 int update_timestamps;
1908 struct got_blob_object *blob2 = NULL;
1909 char *label_orig = NULL;
1910 if (got_fileindex_entry_has_blob(ie)) {
1911 fd2 = got_opentempfd();
1913 err = got_error_from_errno("got_opentempfd");
1916 struct got_object_id id2;
1917 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1918 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1923 if (got_fileindex_entry_has_commit(ie)) {
1924 char id_str[SHA1_DIGEST_STRING_LENGTH];
1925 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1926 sizeof(id_str)) == NULL) {
1927 err = got_error_path(id_str,
1928 GOT_ERR_BAD_OBJ_ID_STR);
1931 if (asprintf(&label_orig, "%s: commit %s",
1932 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1933 err = got_error_from_errno("asprintf");
1937 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1939 err = got_object_blob_read_to_str(&link_target, blob);
1942 err = merge_symlink(worktree, blob2, ondisk_path, path,
1943 label_orig, link_target, worktree->base_commit_id,
1944 repo, progress_cb, progress_arg);
1947 err = merge_blob(&update_timestamps, worktree, blob2,
1948 ondisk_path, path, sb.st_mode, label_orig, blob,
1949 worktree->base_commit_id, repo,
1950 progress_cb, progress_arg);
1953 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1954 err = got_error_from_errno("close");
1958 got_object_blob_close(blob2);
1962 * Do not update timestamps of files with local changes.
1963 * Otherwise, a future status walk would treat them as
1964 * unmodified files again.
1966 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1967 blob->id.sha1, worktree->base_commit_id->sha1,
1969 } else if (status == GOT_STATUS_MODE_CHANGE) {
1970 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1971 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1972 } else if (status == GOT_STATUS_DELETE) {
1973 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1976 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1977 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1981 int is_bad_symlink = 0;
1982 if (S_ISLNK(te->mode)) {
1983 err = install_symlink(&is_bad_symlink, worktree,
1984 ondisk_path, path, blob,
1985 status == GOT_STATUS_MISSING, 0,
1986 status == GOT_STATUS_UNVERSIONED, 0,
1987 repo, progress_cb, progress_arg);
1989 err = install_blob(worktree, ondisk_path, path,
1990 te->mode, sb.st_mode, blob,
1991 status == GOT_STATUS_MISSING, 0, 0,
1992 status == GOT_STATUS_UNVERSIONED, repo,
1993 progress_cb, progress_arg);
1999 err = got_fileindex_entry_update(ie,
2000 worktree->root_fd, path, blob->id.sha1,
2001 worktree->base_commit_id->sha1, 1);
2003 err = create_fileindex_entry(&ie, fileindex,
2004 worktree->base_commit_id, worktree->root_fd, path,
2010 if (is_bad_symlink) {
2011 got_fileindex_entry_filetype_set(ie,
2012 GOT_FILEIDX_MODE_BAD_SYMLINK);
2016 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2017 err = got_error_from_errno("close");
2020 got_object_blob_close(blob);
2026 static const struct got_error *
2027 remove_ondisk_file(const char *root_path, const char *path)
2029 const struct got_error *err = NULL;
2030 char *ondisk_path = NULL, *parent = NULL;
2032 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2033 return got_error_from_errno("asprintf");
2035 if (unlink(ondisk_path) == -1) {
2036 if (errno != ENOENT)
2037 err = got_error_from_errno2("unlink", ondisk_path);
2039 size_t root_len = strlen(root_path);
2040 err = got_path_dirname(&parent, ondisk_path);
2043 while (got_path_cmp(parent, root_path,
2044 strlen(parent), root_len) != 0) {
2046 ondisk_path = parent;
2048 if (rmdir(ondisk_path) == -1) {
2049 if (errno != ENOTEMPTY)
2050 err = got_error_from_errno2("rmdir",
2054 err = got_path_dirname(&parent, ondisk_path);
2065 static const struct got_error *
2066 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2067 struct got_fileindex_entry *ie, struct got_repository *repo,
2068 got_worktree_checkout_cb progress_cb, void *progress_arg)
2070 const struct got_error *err = NULL;
2071 unsigned char status;
2075 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2076 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2078 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2080 return got_error_from_errno("asprintf");
2082 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2086 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2087 char ondisk_target[PATH_MAX];
2088 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2089 sizeof(ondisk_target));
2090 if (ondisk_len == -1) {
2091 err = got_error_from_errno2("readlink", ondisk_path);
2094 ondisk_target[ondisk_len] = '\0';
2095 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2096 NULL, NULL, /* XXX pass common ancestor info? */
2097 ondisk_target, ondisk_path);
2100 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2105 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2106 status == GOT_STATUS_ADD) {
2107 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2111 * Preserve the working file and change the deleted blob's
2112 * entry into a schedule-add entry.
2114 err = got_fileindex_entry_update(ie, worktree->root_fd,
2115 ie->path, NULL, NULL, 0);
2117 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2120 if (status == GOT_STATUS_NO_CHANGE) {
2121 err = remove_ondisk_file(worktree->root_path, ie->path);
2125 got_fileindex_entry_remove(fileindex, ie);
2132 struct diff_cb_arg {
2133 struct got_fileindex *fileindex;
2134 struct got_worktree *worktree;
2135 struct got_repository *repo;
2136 got_worktree_checkout_cb progress_cb;
2138 got_cancel_cb cancel_cb;
2142 static const struct got_error *
2143 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2144 struct got_tree_entry *te, const char *parent_path)
2146 struct diff_cb_arg *a = arg;
2148 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2149 return got_error(GOT_ERR_CANCELLED);
2151 return update_blob(a->worktree, a->fileindex, ie, te,
2152 ie->path, a->repo, a->progress_cb, a->progress_arg);
2155 static const struct got_error *
2156 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2158 struct diff_cb_arg *a = arg;
2160 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2161 return got_error(GOT_ERR_CANCELLED);
2163 return delete_blob(a->worktree, a->fileindex, ie,
2164 a->repo, a->progress_cb, a->progress_arg);
2167 static const struct got_error *
2168 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2170 struct diff_cb_arg *a = arg;
2171 const struct got_error *err;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 if (got_object_tree_entry_is_submodule(te))
2180 if (asprintf(&path, "%s%s%s", parent_path,
2181 parent_path[0] ? "/" : "", te->name)
2183 return got_error_from_errno("asprintf");
2185 if (S_ISDIR(te->mode))
2186 err = add_dir_on_disk(a->worktree, path);
2188 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2189 a->repo, a->progress_cb, a->progress_arg);
2195 const struct got_error *
2196 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2198 uint32_t uuid_status;
2200 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2201 if (uuid_status != uuid_s_ok) {
2203 return got_error_uuid(uuid_status, "uuid_to_string");
2209 static const struct got_error *
2210 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2212 const struct got_error *err = NULL;
2213 char *uuidstr = NULL;
2217 err = got_worktree_get_uuid(&uuidstr, worktree);
2221 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2222 err = got_error_from_errno("asprintf");
2229 const struct got_error *
2230 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2232 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2235 static const struct got_error *
2236 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2238 return get_ref_name(refname, worktree,
2239 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2242 static const struct got_error *
2243 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2245 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2248 static const struct got_error *
2249 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2255 static const struct got_error *
2256 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2262 static const struct got_error *
2263 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2269 static const struct got_error *
2270 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2290 const struct got_error *
2291 got_worktree_get_histedit_script_path(char **path,
2292 struct got_worktree *worktree)
2294 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2295 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2297 return got_error_from_errno("asprintf");
2302 static const struct got_error *
2303 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2305 return get_ref_name(refname, worktree,
2306 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2309 static const struct got_error *
2310 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2312 return get_ref_name(refname, worktree,
2313 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2317 * Prevent Git's garbage collector from deleting our base commit by
2318 * setting a reference to our base commit's ID.
2320 static const struct got_error *
2321 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2323 const struct got_error *err = NULL;
2324 struct got_reference *ref = NULL;
2327 err = got_worktree_get_base_ref_name(&refname, worktree);
2331 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2335 err = got_ref_write(ref, repo);
2343 static const struct got_error *
2344 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2346 const struct got_error *err = NULL;
2348 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2349 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2350 err = got_error_from_errno("asprintf");
2351 *fileindex_path = NULL;
2357 static const struct got_error *
2358 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2359 struct got_worktree *worktree)
2361 const struct got_error *err = NULL;
2364 *fileindex_path = NULL;
2365 *fileindex = got_fileindex_alloc();
2366 if (*fileindex == NULL)
2367 return got_error_from_errno("got_fileindex_alloc");
2369 err = get_fileindex_path(fileindex_path, worktree);
2373 index = fopen(*fileindex_path, "rbe");
2374 if (index == NULL) {
2375 if (errno != ENOENT)
2376 err = got_error_from_errno2("fopen", *fileindex_path);
2378 err = got_fileindex_read(*fileindex, index);
2379 if (fclose(index) == EOF && err == NULL)
2380 err = got_error_from_errno("fclose");
2384 free(*fileindex_path);
2385 *fileindex_path = NULL;
2386 got_fileindex_free(*fileindex);
2392 struct bump_base_commit_id_arg {
2393 struct got_object_id *base_commit_id;
2396 const char *entry_name;
2397 got_worktree_checkout_cb progress_cb;
2401 /* Bump base commit ID of all files within an updated part of the work tree. */
2402 static const struct got_error *
2403 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2405 const struct got_error *err;
2406 struct bump_base_commit_id_arg *a = arg;
2408 if (a->entry_name) {
2409 if (strcmp(ie->path, a->path) != 0)
2411 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2414 if (got_fileindex_entry_was_skipped(ie))
2417 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2418 SHA1_DIGEST_LENGTH) == 0)
2421 if (a->progress_cb) {
2422 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2427 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2431 static const struct got_error *
2432 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2433 struct got_fileindex *fileindex,
2434 got_worktree_checkout_cb progress_cb, void *progress_arg)
2436 struct bump_base_commit_id_arg bbc_arg;
2438 bbc_arg.base_commit_id = worktree->base_commit_id;
2439 bbc_arg.entry_name = NULL;
2441 bbc_arg.path_len = 0;
2442 bbc_arg.progress_cb = progress_cb;
2443 bbc_arg.progress_arg = progress_arg;
2445 return got_fileindex_for_each_entry_safe(fileindex,
2446 bump_base_commit_id, &bbc_arg);
2449 static const struct got_error *
2450 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2452 const struct got_error *err = NULL;
2453 char *new_fileindex_path = NULL;
2454 FILE *new_index = NULL;
2455 struct timespec timeout;
2457 err = got_opentemp_named(&new_fileindex_path, &new_index,
2462 err = got_fileindex_write(fileindex, new_index);
2466 if (rename(new_fileindex_path, fileindex_path) != 0) {
2467 err = got_error_from_errno3("rename", new_fileindex_path,
2469 unlink(new_fileindex_path);
2473 * Sleep for a short amount of time to ensure that files modified after
2474 * this program exits have a different time stamp from the one which
2475 * was recorded in the file index.
2478 timeout.tv_nsec = 1;
2479 nanosleep(&timeout, NULL);
2483 free(new_fileindex_path);
2487 static const struct got_error *
2488 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2489 struct got_object_id **tree_id, const char *wt_relpath,
2490 struct got_commit_object *base_commit, struct got_worktree *worktree,
2491 struct got_repository *repo)
2493 const struct got_error *err = NULL;
2494 struct got_object_id *id = NULL;
2495 char *in_repo_path = NULL;
2496 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2498 *entry_type = GOT_OBJ_TYPE_ANY;
2499 *tree_relpath = NULL;
2502 if (wt_relpath[0] == '\0') {
2503 /* Check out all files within the work tree. */
2504 *entry_type = GOT_OBJ_TYPE_TREE;
2505 *tree_relpath = strdup("");
2506 if (*tree_relpath == NULL) {
2507 err = got_error_from_errno("strdup");
2510 err = got_object_id_by_path(tree_id, repo, base_commit,
2511 worktree->path_prefix);
2517 /* Check out a subset of files in the work tree. */
2519 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2520 is_root_wt ? "" : "/", wt_relpath) == -1) {
2521 err = got_error_from_errno("asprintf");
2525 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2530 in_repo_path = NULL;
2532 err = got_object_get_type(entry_type, repo, id);
2536 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2537 /* Check out a single file. */
2538 if (strchr(wt_relpath, '/') == NULL) {
2539 /* Check out a single file in work tree's root dir. */
2540 in_repo_path = strdup(worktree->path_prefix);
2541 if (in_repo_path == NULL) {
2542 err = got_error_from_errno("strdup");
2545 *tree_relpath = strdup("");
2546 if (*tree_relpath == NULL) {
2547 err = got_error_from_errno("strdup");
2551 /* Check out a single file in a subdirectory. */
2552 err = got_path_dirname(tree_relpath, wt_relpath);
2555 if (asprintf(&in_repo_path, "%s%s%s",
2556 worktree->path_prefix, is_root_wt ? "" : "/",
2557 *tree_relpath) == -1) {
2558 err = got_error_from_errno("asprintf");
2562 err = got_object_id_by_path(tree_id, repo,
2563 base_commit, in_repo_path);
2565 /* Check out all files within a subdirectory. */
2566 *tree_id = got_object_id_dup(id);
2567 if (*tree_id == NULL) {
2568 err = got_error_from_errno("got_object_id_dup");
2571 *tree_relpath = strdup(wt_relpath);
2572 if (*tree_relpath == NULL) {
2573 err = got_error_from_errno("strdup");
2581 *entry_type = GOT_OBJ_TYPE_ANY;
2582 free(*tree_relpath);
2583 *tree_relpath = NULL;
2590 static const struct got_error *
2591 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2592 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2593 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2594 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2596 const struct got_error *err = NULL;
2597 struct got_commit_object *commit = NULL;
2598 struct got_tree_object *tree = NULL;
2599 struct got_fileindex_diff_tree_cb diff_cb;
2600 struct diff_cb_arg arg;
2602 err = ref_base_commit(worktree, repo);
2604 if (!(err->code == GOT_ERR_ERRNO &&
2605 (errno == EACCES || errno == EROFS)))
2607 err = (*progress_cb)(progress_arg,
2608 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2613 err = got_object_open_as_commit(&commit, repo,
2614 worktree->base_commit_id);
2618 err = got_object_open_as_tree(&tree, repo, tree_id);
2623 got_object_tree_find_entry(tree, entry_name) == NULL) {
2624 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2628 diff_cb.diff_old_new = diff_old_new;
2629 diff_cb.diff_old = diff_old;
2630 diff_cb.diff_new = diff_new;
2631 arg.fileindex = fileindex;
2632 arg.worktree = worktree;
2634 arg.progress_cb = progress_cb;
2635 arg.progress_arg = progress_arg;
2636 arg.cancel_cb = cancel_cb;
2637 arg.cancel_arg = cancel_arg;
2638 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2639 entry_name, repo, &diff_cb, &arg);
2642 got_object_tree_close(tree);
2644 got_object_commit_close(commit);
2648 const struct got_error *
2649 got_worktree_checkout_files(struct got_worktree *worktree,
2650 struct got_pathlist_head *paths, struct got_repository *repo,
2651 got_worktree_checkout_cb progress_cb, void *progress_arg,
2652 got_cancel_cb cancel_cb, void *cancel_arg)
2654 const struct got_error *err = NULL, *sync_err, *unlockerr;
2655 struct got_commit_object *commit = NULL;
2656 struct got_tree_object *tree = NULL;
2657 struct got_fileindex *fileindex = NULL;
2658 char *fileindex_path = NULL;
2659 struct got_pathlist_entry *pe;
2660 struct tree_path_data {
2661 STAILQ_ENTRY(tree_path_data) entry;
2662 struct got_object_id *tree_id;
2667 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2669 STAILQ_INIT(&tree_paths);
2671 err = lock_worktree(worktree, LOCK_EX);
2675 err = got_object_open_as_commit(&commit, repo,
2676 worktree->base_commit_id);
2680 /* Map all specified paths to in-repository trees. */
2681 TAILQ_FOREACH(pe, paths, entry) {
2682 tpd = malloc(sizeof(*tpd));
2684 err = got_error_from_errno("malloc");
2688 err = find_tree_entry_for_checkout(&tpd->entry_type,
2689 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2696 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2697 err = got_path_basename(&tpd->entry_name, pe->path);
2705 tpd->entry_name = NULL;
2707 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2711 * Read the file index.
2712 * Checking out files is supposed to be an idempotent operation.
2713 * If the on-disk file index is incomplete we will try to complete it.
2715 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2719 tpd = STAILQ_FIRST(&tree_paths);
2720 TAILQ_FOREACH(pe, paths, entry) {
2721 struct bump_base_commit_id_arg bbc_arg;
2723 err = checkout_files(worktree, fileindex, tpd->relpath,
2724 tpd->tree_id, tpd->entry_name, repo,
2725 progress_cb, progress_arg, cancel_cb, cancel_arg);
2729 bbc_arg.base_commit_id = worktree->base_commit_id;
2730 bbc_arg.entry_name = tpd->entry_name;
2731 bbc_arg.path = pe->path;
2732 bbc_arg.path_len = pe->path_len;
2733 bbc_arg.progress_cb = progress_cb;
2734 bbc_arg.progress_arg = progress_arg;
2735 err = got_fileindex_for_each_entry_safe(fileindex,
2736 bump_base_commit_id, &bbc_arg);
2740 tpd = STAILQ_NEXT(tpd, entry);
2742 sync_err = sync_fileindex(fileindex, fileindex_path);
2743 if (sync_err && err == NULL)
2746 free(fileindex_path);
2748 got_object_tree_close(tree);
2750 got_object_commit_close(commit);
2752 got_fileindex_free(fileindex);
2753 while (!STAILQ_EMPTY(&tree_paths)) {
2754 tpd = STAILQ_FIRST(&tree_paths);
2755 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2760 unlockerr = lock_worktree(worktree, LOCK_SH);
2761 if (unlockerr && err == NULL)
2766 struct merge_file_cb_arg {
2767 struct got_worktree *worktree;
2768 struct got_fileindex *fileindex;
2769 got_worktree_checkout_cb progress_cb;
2771 got_cancel_cb cancel_cb;
2773 const char *label_orig;
2774 struct got_object_id *commit_id2;
2775 int allow_bad_symlinks;
2778 static const struct got_error *
2779 merge_file_cb(void *arg, struct got_blob_object *blob1,
2780 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2781 struct got_object_id *id1, struct got_object_id *id2,
2782 const char *path1, const char *path2,
2783 mode_t mode1, mode_t mode2, struct got_repository *repo)
2785 static const struct got_error *err = NULL;
2786 struct merge_file_cb_arg *a = arg;
2787 struct got_fileindex_entry *ie;
2788 char *ondisk_path = NULL;
2790 unsigned char status;
2791 int local_changes_subsumed;
2792 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2793 char *id_str = NULL, *label_deriv2 = NULL;
2795 if (blob1 && blob2) {
2796 ie = got_fileindex_entry_get(a->fileindex, path2,
2799 return (*a->progress_cb)(a->progress_arg,
2800 GOT_STATUS_MISSING, path2);
2802 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2804 return got_error_from_errno("asprintf");
2806 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2811 if (status == GOT_STATUS_DELETE) {
2812 err = (*a->progress_cb)(a->progress_arg,
2813 GOT_STATUS_MERGE, path2);
2816 if (status != GOT_STATUS_NO_CHANGE &&
2817 status != GOT_STATUS_MODIFY &&
2818 status != GOT_STATUS_CONFLICT &&
2819 status != GOT_STATUS_ADD) {
2820 err = (*a->progress_cb)(a->progress_arg, status, path2);
2824 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2826 err = got_object_blob_read_to_str(&link_target2, blob2);
2829 err = merge_symlink(a->worktree, blob1, ondisk_path,
2830 path2, a->label_orig, link_target2, a->commit_id2,
2831 repo, a->progress_cb, a->progress_arg);
2836 f_orig = got_opentemp();
2837 if (f_orig == NULL) {
2838 err = got_error_from_errno("got_opentemp");
2841 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2846 f_deriv2 = got_opentemp();
2847 if (f_deriv2 == NULL)
2849 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2854 fd = open(ondisk_path,
2855 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2857 err = got_error_from_errno2("open",
2861 f_deriv = fdopen(fd, "r");
2862 if (f_deriv == NULL) {
2863 err = got_error_from_errno2("fdopen",
2868 err = got_object_id_str(&id_str, a->commit_id2);
2871 if (asprintf(&label_deriv2, "%s: commit %s",
2872 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2873 err = got_error_from_errno("asprintf");
2876 err = merge_file(&local_changes_subsumed, a->worktree,
2877 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2878 sb.st_mode, a->label_orig, NULL, label_deriv2,
2879 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2880 a->progress_cb, a->progress_arg);
2883 ie = got_fileindex_entry_get(a->fileindex, path1,
2886 return (*a->progress_cb)(a->progress_arg,
2887 GOT_STATUS_MISSING, path1);
2889 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2891 return got_error_from_errno("asprintf");
2893 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2899 case GOT_STATUS_NO_CHANGE:
2900 err = (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_DELETE, path1);
2904 err = remove_ondisk_file(a->worktree->root_path, path1);
2908 got_fileindex_entry_mark_deleted_from_disk(ie);
2910 case GOT_STATUS_DELETE:
2911 case GOT_STATUS_MISSING:
2912 err = (*a->progress_cb)(a->progress_arg,
2913 GOT_STATUS_DELETE, path1);
2917 got_fileindex_entry_mark_deleted_from_disk(ie);
2919 case GOT_STATUS_ADD: {
2920 struct got_object_id *id;
2924 * Delete the added file only if its content already
2925 * exists in the repository.
2927 err = got_object_blob_file_create(&id, &blob1_f,
2928 &blob1_size, path1);
2931 if (got_object_id_cmp(id, id1) == 0) {
2932 err = (*a->progress_cb)(a->progress_arg,
2933 GOT_STATUS_DELETE, path1);
2936 err = remove_ondisk_file(a->worktree->root_path,
2941 got_fileindex_entry_remove(a->fileindex,
2944 err = (*a->progress_cb)(a->progress_arg,
2945 GOT_STATUS_CANNOT_DELETE, path1);
2947 if (fclose(blob1_f) == EOF && err == NULL)
2948 err = got_error_from_errno("fclose");
2954 case GOT_STATUS_MODIFY:
2955 case GOT_STATUS_CONFLICT:
2956 err = (*a->progress_cb)(a->progress_arg,
2957 GOT_STATUS_CANNOT_DELETE, path1);
2961 case GOT_STATUS_OBSTRUCTED:
2962 err = (*a->progress_cb)(a->progress_arg, status, path1);
2970 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2972 return got_error_from_errno("asprintf");
2973 ie = got_fileindex_entry_get(a->fileindex, path2,
2976 err = get_file_status(&status, &sb, ie, ondisk_path,
2980 if (status != GOT_STATUS_NO_CHANGE &&
2981 status != GOT_STATUS_MODIFY &&
2982 status != GOT_STATUS_CONFLICT &&
2983 status != GOT_STATUS_ADD) {
2984 err = (*a->progress_cb)(a->progress_arg,
2988 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2990 err = got_object_blob_read_to_str(&link_target2,
2994 err = merge_symlink(a->worktree, NULL,
2995 ondisk_path, path2, a->label_orig,
2996 link_target2, a->commit_id2, repo,
2997 a->progress_cb, a->progress_arg);
2999 } else if (S_ISREG(sb.st_mode)) {
3000 err = merge_blob(&local_changes_subsumed,
3001 a->worktree, NULL, ondisk_path, path2,
3002 sb.st_mode, a->label_orig, blob2,
3003 a->commit_id2, repo, a->progress_cb,
3006 err = got_error_path(ondisk_path,
3007 GOT_ERR_FILE_OBSTRUCTED);
3011 if (status == GOT_STATUS_DELETE) {
3012 err = got_fileindex_entry_update(ie,
3013 a->worktree->root_fd, path2, blob2->id.sha1,
3014 a->worktree->base_commit_id->sha1, 0);
3019 int is_bad_symlink = 0;
3020 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3021 if (S_ISLNK(mode2)) {
3022 err = install_symlink(&is_bad_symlink,
3023 a->worktree, ondisk_path, path2, blob2, 0,
3024 0, 1, a->allow_bad_symlinks, repo,
3025 a->progress_cb, a->progress_arg);
3027 err = install_blob(a->worktree, ondisk_path, path2,
3028 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3029 a->progress_cb, a->progress_arg);
3033 err = got_fileindex_entry_alloc(&ie, path2);
3036 err = got_fileindex_entry_update(ie,
3037 a->worktree->root_fd, path2, NULL, NULL, 1);
3039 got_fileindex_entry_free(ie);
3042 err = got_fileindex_entry_add(a->fileindex, ie);
3044 got_fileindex_entry_free(ie);
3047 if (is_bad_symlink) {
3048 got_fileindex_entry_filetype_set(ie,
3049 GOT_FILEIDX_MODE_BAD_SYMLINK);
3054 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3055 err = got_error_from_errno("fclose");
3056 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3057 err = got_error_from_errno("fclose");
3058 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3059 err = got_error_from_errno("fclose");
3066 static const struct got_error *
3067 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3069 struct got_worktree *worktree = arg;
3071 /* Reject merges into a work tree with mixed base commits. */
3072 if (got_fileindex_entry_has_commit(ie) &&
3073 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3074 SHA1_DIGEST_LENGTH) != 0)
3075 return got_error(GOT_ERR_MIXED_COMMITS);
3080 struct check_merge_conflicts_arg {
3081 struct got_worktree *worktree;
3082 struct got_fileindex *fileindex;
3083 struct got_repository *repo;
3086 static const struct got_error *
3087 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3088 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3089 struct got_object_id *id1, struct got_object_id *id2,
3090 const char *path1, const char *path2,
3091 mode_t mode1, mode_t mode2, struct got_repository *repo)
3093 const struct got_error *err = NULL;
3094 struct check_merge_conflicts_arg *a = arg;
3095 unsigned char status;
3097 struct got_fileindex_entry *ie;
3098 const char *path = path2 ? path2 : path1;
3099 struct got_object_id *id = id2 ? id2 : id1;
3105 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3109 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3111 return got_error_from_errno("asprintf");
3113 /* Reject merges into a work tree with conflicted files. */
3114 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3118 if (status == GOT_STATUS_CONFLICT)
3119 return got_error(GOT_ERR_CONFLICTS);
3124 static const struct got_error *
3125 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3126 const char *fileindex_path, struct got_object_id *commit_id1,
3127 struct got_object_id *commit_id2, struct got_repository *repo,
3128 got_worktree_checkout_cb progress_cb, void *progress_arg,
3129 got_cancel_cb cancel_cb, void *cancel_arg)
3131 const struct got_error *err = NULL, *sync_err;
3132 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3133 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3134 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3135 struct check_merge_conflicts_arg cmc_arg;
3136 struct merge_file_cb_arg arg;
3137 char *label_orig = NULL;
3138 FILE *f1 = NULL, *f2 = NULL;
3139 int fd1 = -1, fd2 = -1;
3142 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3145 err = got_object_id_by_path(&tree_id1, repo, commit1,
3146 worktree->path_prefix);
3147 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3153 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3157 err = got_object_id_str(&id_str, commit_id1);
3161 if (asprintf(&label_orig, "%s: commit %s",
3162 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3163 err = got_error_from_errno("asprintf");
3169 f1 = got_opentemp();
3171 err = got_error_from_errno("got_opentemp");
3176 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3180 err = got_object_id_by_path(&tree_id2, repo, commit2,
3181 worktree->path_prefix);
3185 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3189 f2 = got_opentemp();
3191 err = got_error_from_errno("got_opentemp");
3195 fd1 = got_opentempfd();
3197 err = got_error_from_errno("got_opentempfd");
3201 fd2 = got_opentempfd();
3203 err = got_error_from_errno("got_opentempfd");
3207 cmc_arg.worktree = worktree;
3208 cmc_arg.fileindex = fileindex;
3209 cmc_arg.repo = repo;
3210 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3211 check_merge_conflicts, &cmc_arg, 0);
3215 arg.worktree = worktree;
3216 arg.fileindex = fileindex;
3217 arg.progress_cb = progress_cb;
3218 arg.progress_arg = progress_arg;
3219 arg.cancel_cb = cancel_cb;
3220 arg.cancel_arg = cancel_arg;
3221 arg.label_orig = label_orig;
3222 arg.commit_id2 = commit_id2;
3223 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3224 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3225 merge_file_cb, &arg, 1);
3226 sync_err = sync_fileindex(fileindex, fileindex_path);
3227 if (sync_err && err == NULL)
3231 got_object_commit_close(commit1);
3233 got_object_commit_close(commit2);
3235 got_object_tree_close(tree1);
3237 got_object_tree_close(tree2);
3238 if (f1 && fclose(f1) == EOF && err == NULL)
3239 err = got_error_from_errno("fclose");
3240 if (f2 && fclose(f2) == EOF && err == NULL)
3241 err = got_error_from_errno("fclose");
3242 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3243 err = got_error_from_errno("close");
3244 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3245 err = got_error_from_errno("close");
3250 const struct got_error *
3251 got_worktree_merge_files(struct got_worktree *worktree,
3252 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3253 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3254 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3256 const struct got_error *err, *unlockerr;
3257 char *fileindex_path = NULL;
3258 struct got_fileindex *fileindex = NULL;
3260 err = lock_worktree(worktree, LOCK_EX);
3264 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3268 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3273 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3274 commit_id2, repo, progress_cb, progress_arg,
3275 cancel_cb, cancel_arg);
3278 got_fileindex_free(fileindex);
3279 free(fileindex_path);
3280 unlockerr = lock_worktree(worktree, LOCK_SH);
3281 if (unlockerr && err == NULL)
3286 struct diff_dir_cb_arg {
3287 struct got_fileindex *fileindex;
3288 struct got_worktree *worktree;
3289 const char *status_path;
3290 size_t status_path_len;
3291 struct got_repository *repo;
3292 got_worktree_status_cb status_cb;
3294 got_cancel_cb cancel_cb;
3296 /* A pathlist containing per-directory pathlists of ignore patterns. */
3297 struct got_pathlist_head *ignores;
3298 int report_unchanged;
3302 static const struct got_error *
3303 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3304 int dirfd, const char *de_name,
3305 got_worktree_status_cb status_cb, void *status_arg,
3306 struct got_repository *repo, int report_unchanged)
3308 const struct got_error *err = NULL;
3309 unsigned char status = GOT_STATUS_NO_CHANGE;
3310 unsigned char staged_status = get_staged_status(ie);
3312 struct got_object_id blob_id, commit_id, staged_blob_id;
3313 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3314 struct got_object_id *staged_blob_idp = NULL;
3316 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3320 if (status == GOT_STATUS_NO_CHANGE &&
3321 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3324 if (got_fileindex_entry_has_blob(ie)) {
3325 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3326 blob_idp = &blob_id;
3328 if (got_fileindex_entry_has_commit(ie)) {
3329 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3330 commit_idp = &commit_id;
3332 if (staged_status == GOT_STATUS_ADD ||
3333 staged_status == GOT_STATUS_MODIFY) {
3334 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3335 SHA1_DIGEST_LENGTH);
3336 staged_blob_idp = &staged_blob_id;
3339 return (*status_cb)(status_arg, status, staged_status,
3340 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3343 static const struct got_error *
3344 status_old_new(void *arg, struct got_fileindex_entry *ie,
3345 struct dirent *de, const char *parent_path, int dirfd)
3347 const struct got_error *err = NULL;
3348 struct diff_dir_cb_arg *a = arg;
3351 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3352 return got_error(GOT_ERR_CANCELLED);
3354 if (got_path_cmp(parent_path, a->status_path,
3355 strlen(parent_path), a->status_path_len) != 0 &&
3356 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3359 if (parent_path[0]) {
3360 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3361 parent_path, de->d_name) == -1)
3362 return got_error_from_errno("asprintf");
3364 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3366 return got_error_from_errno("asprintf");
3369 err = report_file_status(ie, abspath, dirfd, de->d_name,
3370 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3375 static const struct got_error *
3376 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3378 struct diff_dir_cb_arg *a = arg;
3379 struct got_object_id blob_id, commit_id;
3380 unsigned char status;
3382 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3383 return got_error(GOT_ERR_CANCELLED);
3385 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3388 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3389 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3390 if (got_fileindex_entry_has_file_on_disk(ie))
3391 status = GOT_STATUS_MISSING;
3393 status = GOT_STATUS_DELETE;
3394 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3395 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3399 free_ignorelist(struct got_pathlist_head *ignorelist)
3401 struct got_pathlist_entry *pe;
3403 TAILQ_FOREACH(pe, ignorelist, entry)
3404 free((char *)pe->path);
3405 got_pathlist_free(ignorelist);
3409 free_ignores(struct got_pathlist_head *ignores)
3411 struct got_pathlist_entry *pe;
3413 TAILQ_FOREACH(pe, ignores, entry) {
3414 struct got_pathlist_head *ignorelist = pe->data;
3415 free_ignorelist(ignorelist);
3416 free((char *)pe->path);
3418 got_pathlist_free(ignores);
3421 static const struct got_error *
3422 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3424 const struct got_error *err = NULL;
3425 struct got_pathlist_entry *pe = NULL;
3426 struct got_pathlist_head *ignorelist;
3427 char *line = NULL, *pattern, *dirpath = NULL;
3428 size_t linesize = 0;
3431 ignorelist = calloc(1, sizeof(*ignorelist));
3432 if (ignorelist == NULL)
3433 return got_error_from_errno("calloc");
3434 TAILQ_INIT(ignorelist);
3436 while ((linelen = getline(&line, &linesize, f)) != -1) {
3437 if (linelen > 0 && line[linelen - 1] == '\n')
3438 line[linelen - 1] = '\0';
3440 /* Git's ignores may contain comments. */
3444 /* Git's negated patterns are not (yet?) supported. */
3448 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3450 err = got_error_from_errno("asprintf");
3453 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3458 err = got_error_from_errno("getline");
3462 dirpath = strdup(path);
3463 if (dirpath == NULL) {
3464 err = got_error_from_errno("strdup");
3467 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3470 if (err || pe == NULL) {
3472 free_ignorelist(ignorelist);
3478 match_ignores(struct got_pathlist_head *ignores, const char *path)
3480 struct got_pathlist_entry *pe;
3482 /* Handle patterns which match in all directories. */
3483 TAILQ_FOREACH(pe, ignores, entry) {
3484 struct got_pathlist_head *ignorelist = pe->data;
3485 struct got_pathlist_entry *pi;
3487 TAILQ_FOREACH(pi, ignorelist, entry) {
3488 const char *p, *pattern = pi->path;
3490 if (strncmp(pattern, "**/", 3) != 0)
3495 if (fnmatch(pattern, p,
3496 FNM_PATHNAME | FNM_LEADING_DIR)) {
3497 /* Retry in next directory. */
3498 while (*p && *p != '/')
3510 * The ignores pathlist contains ignore lists from children before
3511 * parents, so we can find the most specific ignorelist by walking
3512 * ignores backwards.
3514 pe = TAILQ_LAST(ignores, got_pathlist_head);
3516 if (got_path_is_child(path, pe->path, pe->path_len)) {
3517 struct got_pathlist_head *ignorelist = pe->data;
3518 struct got_pathlist_entry *pi;
3519 TAILQ_FOREACH(pi, ignorelist, entry) {
3520 const char *pattern = pi->path;
3521 int flags = FNM_LEADING_DIR;
3522 if (strstr(pattern, "/**/") == NULL)
3523 flags |= FNM_PATHNAME;
3524 if (fnmatch(pattern, path, flags))
3529 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3535 static const struct got_error *
3536 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3537 const char *path, int dirfd, const char *ignores_filename)
3539 const struct got_error *err = NULL;
3542 FILE *ignoresfile = NULL;
3544 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3545 path[0] ? "/" : "", ignores_filename) == -1)
3546 return got_error_from_errno("asprintf");
3549 fd = openat(dirfd, ignores_filename,
3550 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3552 if (errno != ENOENT && errno != EACCES)
3553 err = got_error_from_errno2("openat",
3556 ignoresfile = fdopen(fd, "r");
3557 if (ignoresfile == NULL)
3558 err = got_error_from_errno2("fdopen",
3562 err = read_ignores(ignores, path, ignoresfile);
3566 ignoresfile = fopen(ignorespath, "re");
3567 if (ignoresfile == NULL) {
3568 if (errno != ENOENT && errno != EACCES)
3569 err = got_error_from_errno2("fopen",
3572 err = read_ignores(ignores, path, ignoresfile);
3575 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3576 err = got_error_from_errno2("fclose", path);
3577 if (fd != -1 && close(fd) == -1 && err == NULL)
3578 err = got_error_from_errno2("close", path);
3583 static const struct got_error *
3584 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3587 const struct got_error *err = NULL;
3588 struct diff_dir_cb_arg *a = arg;
3594 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3595 return got_error(GOT_ERR_CANCELLED);
3597 if (parent_path[0]) {
3598 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3599 return got_error_from_errno("asprintf");
3604 if (de->d_type == DT_DIR) {
3605 if (!a->no_ignores && ignore != NULL &&
3606 match_ignores(a->ignores, path))
3608 } else if (!match_ignores(a->ignores, path) &&
3609 got_path_is_child(path, a->status_path, a->status_path_len))
3610 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3611 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3617 static const struct got_error *
3618 status_traverse(void *arg, const char *path, int dirfd)
3620 const struct got_error *err = NULL;
3621 struct diff_dir_cb_arg *a = arg;
3626 err = add_ignores(a->ignores, a->worktree->root_path,
3627 path, dirfd, ".cvsignore");
3631 err = add_ignores(a->ignores, a->worktree->root_path, path,
3632 dirfd, ".gitignore");
3637 static const struct got_error *
3638 report_single_file_status(const char *path, const char *ondisk_path,
3639 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3640 void *status_arg, struct got_repository *repo, int report_unchanged,
3641 struct got_pathlist_head *ignores, int no_ignores)
3643 struct got_fileindex_entry *ie;
3646 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3648 return report_file_status(ie, ondisk_path, -1, NULL,
3649 status_cb, status_arg, repo, report_unchanged);
3651 if (lstat(ondisk_path, &sb) == -1) {
3652 if (errno != ENOENT)
3653 return got_error_from_errno2("lstat", ondisk_path);
3654 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3655 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3658 if (!no_ignores && match_ignores(ignores, path))
3661 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3662 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3663 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3668 static const struct got_error *
3669 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3670 const char *root_path, const char *path)
3672 const struct got_error *err;
3673 char *parent_path, *next_parent_path = NULL;
3675 err = add_ignores(ignores, root_path, "", -1,
3680 err = add_ignores(ignores, root_path, "", -1,
3685 err = got_path_dirname(&parent_path, path);
3687 if (err->code == GOT_ERR_BAD_PATH)
3688 return NULL; /* cannot traverse parent */
3692 err = add_ignores(ignores, root_path, parent_path, -1,
3696 err = add_ignores(ignores, root_path, parent_path, -1,
3700 err = got_path_dirname(&next_parent_path, parent_path);
3702 if (err->code == GOT_ERR_BAD_PATH)
3703 err = NULL; /* traversed everything */
3706 if (got_path_is_root_dir(parent_path))
3709 parent_path = next_parent_path;
3710 next_parent_path = NULL;
3714 free(next_parent_path);
3718 static const struct got_error *
3719 worktree_status(struct got_worktree *worktree, const char *path,
3720 struct got_fileindex *fileindex, struct got_repository *repo,
3721 got_worktree_status_cb status_cb, void *status_arg,
3722 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3723 int report_unchanged)
3725 const struct got_error *err = NULL;
3727 struct got_fileindex_diff_dir_cb fdiff_cb;
3728 struct diff_dir_cb_arg arg;
3729 char *ondisk_path = NULL;
3730 struct got_pathlist_head ignores;
3731 struct got_fileindex_entry *ie;
3733 TAILQ_INIT(&ignores);
3735 if (asprintf(&ondisk_path, "%s%s%s",
3736 worktree->root_path, path[0] ? "/" : "", path) == -1)
3737 return got_error_from_errno("asprintf");
3739 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3741 err = report_single_file_status(path, ondisk_path,
3742 fileindex, status_cb, status_arg, repo,
3743 report_unchanged, &ignores, no_ignores);
3747 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3749 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3750 !got_err_open_nofollow_on_symlink())
3751 err = got_error_from_errno2("open", ondisk_path);
3754 err = add_ignores_from_parent_paths(&ignores,
3755 worktree->root_path, ondisk_path);
3759 err = report_single_file_status(path, ondisk_path,
3760 fileindex, status_cb, status_arg, repo,
3761 report_unchanged, &ignores, no_ignores);
3764 fdiff_cb.diff_old_new = status_old_new;
3765 fdiff_cb.diff_old = status_old;
3766 fdiff_cb.diff_new = status_new;
3767 fdiff_cb.diff_traverse = status_traverse;
3768 arg.fileindex = fileindex;
3769 arg.worktree = worktree;
3770 arg.status_path = path;
3771 arg.status_path_len = strlen(path);
3773 arg.status_cb = status_cb;
3774 arg.status_arg = status_arg;
3775 arg.cancel_cb = cancel_cb;
3776 arg.cancel_arg = cancel_arg;
3777 arg.report_unchanged = report_unchanged;
3778 arg.no_ignores = no_ignores;
3780 err = add_ignores_from_parent_paths(&ignores,
3781 worktree->root_path, path);
3785 arg.ignores = &ignores;
3786 err = got_fileindex_diff_dir(fileindex, fd,
3787 worktree->root_path, path, repo, &fdiff_cb, &arg);
3790 free_ignores(&ignores);
3791 if (fd != -1 && close(fd) == -1 && err == NULL)
3792 err = got_error_from_errno("close");
3797 const struct got_error *
3798 got_worktree_status(struct got_worktree *worktree,
3799 struct got_pathlist_head *paths, struct got_repository *repo,
3800 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3801 got_cancel_cb cancel_cb, void *cancel_arg)
3803 const struct got_error *err = NULL;
3804 char *fileindex_path = NULL;
3805 struct got_fileindex *fileindex = NULL;
3806 struct got_pathlist_entry *pe;
3808 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3812 TAILQ_FOREACH(pe, paths, entry) {
3813 err = worktree_status(worktree, pe->path, fileindex, repo,
3814 status_cb, status_arg, cancel_cb, cancel_arg,
3819 free(fileindex_path);
3820 got_fileindex_free(fileindex);
3824 const struct got_error *
3825 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3828 const struct got_error *err = NULL;
3829 char *resolved = NULL, *cwd = NULL, *path = NULL;
3832 char *abspath = NULL;
3833 char canonpath[PATH_MAX];
3837 cwd = getcwd(NULL, 0);
3839 return got_error_from_errno("getcwd");
3841 if (lstat(arg, &sb) == -1) {
3842 if (errno != ENOENT) {
3843 err = got_error_from_errno2("lstat", arg);
3848 if (S_ISLNK(sb.st_mode)) {
3850 * We cannot use realpath(3) with symlinks since we want to
3851 * operate on the symlink itself.
3852 * But we can make the path absolute, assuming it is relative
3853 * to the current working directory, and then canonicalize it.
3855 if (!got_path_is_absolute(arg)) {
3856 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3857 err = got_error_from_errno("asprintf");
3862 err = got_canonpath(abspath ? abspath : arg, canonpath,
3866 resolved = strdup(canonpath);
3867 if (resolved == NULL) {
3868 err = got_error_from_errno("strdup");
3872 resolved = realpath(arg, NULL);
3873 if (resolved == NULL) {
3874 if (errno != ENOENT) {
3875 err = got_error_from_errno2("realpath", arg);
3878 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3879 err = got_error_from_errno("asprintf");
3882 err = got_canonpath(abspath, canonpath,
3886 resolved = strdup(canonpath);
3887 if (resolved == NULL) {
3888 err = got_error_from_errno("strdup");
3894 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3895 strlen(got_worktree_get_root_path(worktree)))) {
3896 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3900 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3901 err = got_path_skip_common_ancestor(&path,
3902 got_worktree_get_root_path(worktree), resolved);
3908 err = got_error_from_errno("strdup");
3913 /* XXX status walk can't deal with trailing slash! */
3915 while (len > 0 && path[len - 1] == '/') {
3916 path[len - 1] = '\0';
3930 struct schedule_addition_args {
3931 struct got_worktree *worktree;
3932 struct got_fileindex *fileindex;
3933 got_worktree_checkout_cb progress_cb;
3935 struct got_repository *repo;
3938 static const struct got_error *
3939 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3940 const char *relpath, struct got_object_id *blob_id,
3941 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3942 int dirfd, const char *de_name)
3944 struct schedule_addition_args *a = arg;
3945 const struct got_error *err = NULL;
3946 struct got_fileindex_entry *ie;
3950 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3952 return got_error_from_errno("asprintf");
3954 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3956 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3960 /* Re-adding an existing entry is a no-op. */
3961 if (status == GOT_STATUS_ADD)
3963 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3968 if (status != GOT_STATUS_UNVERSIONED) {
3969 if (status == GOT_STATUS_NONEXISTENT)
3970 err = got_error_set_errno(ENOENT, ondisk_path);
3972 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3976 err = got_fileindex_entry_alloc(&ie, relpath);
3979 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3980 relpath, NULL, NULL, 1);
3982 got_fileindex_entry_free(ie);
3985 err = got_fileindex_entry_add(a->fileindex, ie);
3987 got_fileindex_entry_free(ie);
3994 if (status == GOT_STATUS_ADD)
3996 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3999 const struct got_error *
4000 got_worktree_schedule_add(struct got_worktree *worktree,
4001 struct got_pathlist_head *paths,
4002 got_worktree_checkout_cb progress_cb, void *progress_arg,
4003 struct got_repository *repo, int no_ignores)
4005 struct got_fileindex *fileindex = NULL;
4006 char *fileindex_path = NULL;
4007 const struct got_error *err = NULL, *sync_err, *unlockerr;
4008 struct got_pathlist_entry *pe;
4009 struct schedule_addition_args saa;
4011 err = lock_worktree(worktree, LOCK_EX);
4015 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4019 saa.worktree = worktree;
4020 saa.fileindex = fileindex;
4021 saa.progress_cb = progress_cb;
4022 saa.progress_arg = progress_arg;
4025 TAILQ_FOREACH(pe, paths, entry) {
4026 err = worktree_status(worktree, pe->path, fileindex, repo,
4027 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4031 sync_err = sync_fileindex(fileindex, fileindex_path);
4032 if (sync_err && err == NULL)
4035 free(fileindex_path);
4037 got_fileindex_free(fileindex);
4038 unlockerr = lock_worktree(worktree, LOCK_SH);
4039 if (unlockerr && err == NULL)
4044 struct schedule_deletion_args {
4045 struct got_worktree *worktree;
4046 struct got_fileindex *fileindex;
4047 got_worktree_delete_cb progress_cb;
4049 struct got_repository *repo;
4050 int delete_local_mods;
4052 int ignore_missing_paths;
4053 const char *status_codes;
4056 static const struct got_error *
4057 schedule_for_deletion(void *arg, unsigned char status,
4058 unsigned char staged_status, const char *relpath,
4059 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4060 struct got_object_id *commit_id, int dirfd, const char *de_name)
4062 struct schedule_deletion_args *a = arg;
4063 const struct got_error *err = NULL;
4064 struct got_fileindex_entry *ie = NULL;
4068 if (status == GOT_STATUS_NONEXISTENT) {
4069 if (a->ignore_missing_paths)
4071 return got_error_set_errno(ENOENT, relpath);
4074 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4076 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4078 staged_status = get_staged_status(ie);
4079 if (staged_status != GOT_STATUS_NO_CHANGE) {
4080 if (staged_status == GOT_STATUS_DELETE)
4082 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4085 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4087 return got_error_from_errno("asprintf");
4089 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4094 if (a->status_codes) {
4095 size_t ncodes = strlen(a->status_codes);
4097 for (i = 0; i < ncodes ; i++) {
4098 if (status == a->status_codes[i])
4102 /* Do not delete files in non-matching status. */
4106 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4107 a->status_codes[i] != GOT_STATUS_MISSING) {
4108 static char msg[64];
4109 snprintf(msg, sizeof(msg),
4110 "invalid status code '%c'", a->status_codes[i]);
4111 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4116 if (status != GOT_STATUS_NO_CHANGE) {
4117 if (status == GOT_STATUS_DELETE)
4119 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4120 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4123 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4124 err = got_error_set_errno(ENOENT, relpath);
4127 if (status != GOT_STATUS_MODIFY &&
4128 status != GOT_STATUS_MISSING) {
4129 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4134 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4138 if (unlinkat(dirfd, de_name, 0) != 0) {
4139 err = got_error_from_errno2("unlinkat",
4143 } else if (unlink(ondisk_path) != 0) {
4144 err = got_error_from_errno2("unlink", ondisk_path);
4148 root_len = strlen(a->worktree->root_path);
4151 err = got_path_dirname(&parent, ondisk_path);
4155 ondisk_path = parent;
4156 if (rmdir(ondisk_path) == -1) {
4157 if (errno != ENOTEMPTY)
4158 err = got_error_from_errno2("rmdir",
4162 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4163 strlen(ondisk_path), root_len) != 0);
4166 got_fileindex_entry_mark_deleted_from_disk(ie);
4171 if (status == GOT_STATUS_DELETE)
4173 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4174 staged_status, relpath);
4177 const struct got_error *
4178 got_worktree_schedule_delete(struct got_worktree *worktree,
4179 struct got_pathlist_head *paths, int delete_local_mods,
4180 const char *status_codes,
4181 got_worktree_delete_cb progress_cb, void *progress_arg,
4182 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4184 struct got_fileindex *fileindex = NULL;
4185 char *fileindex_path = NULL;
4186 const struct got_error *err = NULL, *sync_err, *unlockerr;
4187 struct got_pathlist_entry *pe;
4188 struct schedule_deletion_args sda;
4190 err = lock_worktree(worktree, LOCK_EX);
4194 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4198 sda.worktree = worktree;
4199 sda.fileindex = fileindex;
4200 sda.progress_cb = progress_cb;
4201 sda.progress_arg = progress_arg;
4203 sda.delete_local_mods = delete_local_mods;
4204 sda.keep_on_disk = keep_on_disk;
4205 sda.ignore_missing_paths = ignore_missing_paths;
4206 sda.status_codes = status_codes;
4208 TAILQ_FOREACH(pe, paths, entry) {
4209 err = worktree_status(worktree, pe->path, fileindex, repo,
4210 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4214 sync_err = sync_fileindex(fileindex, fileindex_path);
4215 if (sync_err && err == NULL)
4218 free(fileindex_path);
4220 got_fileindex_free(fileindex);
4221 unlockerr = lock_worktree(worktree, LOCK_SH);
4222 if (unlockerr && err == NULL)
4227 static const struct got_error *
4228 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4230 const struct got_error *err = NULL;
4232 size_t linesize = 0, n;
4235 linelen = getline(&line, &linesize, infile);
4236 if (linelen == -1) {
4237 if (ferror(infile)) {
4238 err = got_error_from_errno("getline");
4244 n = fwrite(line, 1, linelen, outfile);
4246 err = got_ferror(outfile, GOT_ERR_IO);
4251 n = fwrite(line, 1, linelen, rejectfile);
4253 err = got_ferror(outfile, GOT_ERR_IO);
4260 static const struct got_error *
4261 skip_one_line(FILE *f)
4264 size_t linesize = 0;
4267 linelen = getline(&line, &linesize, f);
4268 if (linelen == -1) {
4270 return got_error_from_errno("getline");
4277 static const struct got_error *
4278 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4279 int start_old, int end_old, int start_new, int end_new,
4280 FILE *outfile, FILE *rejectfile)
4282 const struct got_error *err;
4284 /* Copy old file's lines leading up to patch. */
4285 while (!feof(f1) && *line_cur1 < start_old) {
4286 err = copy_one_line(f1, outfile, NULL);
4291 /* Skip new file's lines leading up to patch. */
4292 while (!feof(f2) && *line_cur2 < start_new) {
4294 err = copy_one_line(f2, NULL, rejectfile);
4296 err = skip_one_line(f2);
4301 /* Copy patched lines. */
4302 while (!feof(f2) && *line_cur2 <= end_new) {
4303 err = copy_one_line(f2, outfile, NULL);
4308 /* Skip over old file's replaced lines. */
4309 while (!feof(f1) && *line_cur1 <= end_old) {
4311 err = copy_one_line(f1, NULL, rejectfile);
4313 err = skip_one_line(f1);
4322 static const struct got_error *
4323 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4324 FILE *outfile, FILE *rejectfile)
4326 const struct got_error *err;
4329 /* Copy old file's lines until EOF. */
4331 err = copy_one_line(f1, outfile, NULL);
4338 /* Copy new file's lines until EOF. */
4340 err = copy_one_line(f2, NULL, rejectfile);
4350 static const struct got_error *
4351 apply_or_reject_change(int *choice, int *nchunks_used,
4352 struct diff_result *diff_result, int n,
4353 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4354 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4355 got_worktree_patch_cb patch_cb, void *patch_arg)
4357 const struct got_error *err = NULL;
4358 struct diff_chunk_context cc = {};
4359 int start_old, end_old, start_new, end_new;
4361 struct diff_output_unidiff_state *diff_state;
4362 struct diff_input_info diff_info;
4365 *choice = GOT_PATCH_CHOICE_NONE;
4367 /* Get changed line numbers without context lines for copy_change(). */
4368 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4369 start_old = cc.left.start;
4370 end_old = cc.left.end;
4371 start_new = cc.right.start;
4372 end_new = cc.right.end;
4374 /* Get the same change with context lines for display. */
4375 memset(&cc, 0, sizeof(cc));
4376 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4378 memset(&diff_info, 0, sizeof(diff_info));
4379 diff_info.left_path = relpath;
4380 diff_info.right_path = relpath;
4382 diff_state = diff_output_unidiff_state_alloc();
4383 if (diff_state == NULL)
4384 return got_error_set_errno(ENOMEM,
4385 "diff_output_unidiff_state_alloc");
4387 hunkfile = got_opentemp();
4388 if (hunkfile == NULL) {
4389 err = got_error_from_errno("got_opentemp");
4393 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4395 if (rc != DIFF_RC_OK) {
4396 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4400 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4401 err = got_ferror(hunkfile, GOT_ERR_IO);
4405 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4406 hunkfile, changeno, nchanges);
4411 case GOT_PATCH_CHOICE_YES:
4412 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4413 end_old, start_new, end_new, outfile, rejectfile);
4415 case GOT_PATCH_CHOICE_NO:
4416 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4417 end_old, start_new, end_new, rejectfile, outfile);
4419 case GOT_PATCH_CHOICE_QUIT:
4422 err = got_error(GOT_ERR_PATCH_CHOICE);
4426 diff_output_unidiff_state_free(diff_state);
4427 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4428 err = got_error_from_errno("fclose");
4432 struct revert_file_args {
4433 struct got_worktree *worktree;
4434 struct got_fileindex *fileindex;
4435 got_worktree_checkout_cb progress_cb;
4437 got_worktree_patch_cb patch_cb;
4439 struct got_repository *repo;
4440 int unlink_added_files;
4443 static const struct got_error *
4444 create_patched_content(char **path_outfile, int reverse_patch,
4445 struct got_object_id *blob_id, const char *path2,
4446 int dirfd2, const char *de_name2,
4447 const char *relpath, struct got_repository *repo,
4448 got_worktree_patch_cb patch_cb, void *patch_arg)
4450 const struct got_error *err, *free_err;
4451 struct got_blob_object *blob = NULL;
4452 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4453 int fd = -1, fd2 = -1;
4454 char link_target[PATH_MAX];
4455 ssize_t link_len = 0;
4456 char *path1 = NULL, *id_str = NULL;
4458 struct got_diffreg_result *diffreg_result = NULL;
4459 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4460 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4462 *path_outfile = NULL;
4464 err = got_object_id_str(&id_str, blob_id);
4469 fd2 = openat(dirfd2, de_name2,
4470 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4472 if (!got_err_open_nofollow_on_symlink()) {
4473 err = got_error_from_errno2("openat", path2);
4476 link_len = readlinkat(dirfd2, de_name2,
4477 link_target, sizeof(link_target));
4478 if (link_len == -1) {
4479 return got_error_from_errno2("readlinkat",
4482 sb2.st_mode = S_IFLNK;
4483 sb2.st_size = link_len;
4486 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4488 if (!got_err_open_nofollow_on_symlink()) {
4489 err = got_error_from_errno2("open", path2);
4492 link_len = readlink(path2, link_target,
4493 sizeof(link_target));
4495 return got_error_from_errno2("readlink", path2);
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4501 if (fstat(fd2, &sb2) == -1) {
4502 err = got_error_from_errno2("fstat", path2);
4506 f2 = fdopen(fd2, "r");
4508 err = got_error_from_errno2("fdopen", path2);
4514 f2 = got_opentemp();
4516 err = got_error_from_errno2("got_opentemp", path2);
4519 n = fwrite(link_target, 1, link_len, f2);
4520 if (n != link_len) {
4521 err = got_ferror(f2, GOT_ERR_IO);
4524 if (fflush(f2) == EOF) {
4525 err = got_error_from_errno("fflush");
4531 fd = got_opentempfd();
4533 err = got_error_from_errno("got_opentempfd");
4537 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4541 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4545 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4549 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4550 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4554 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4558 if (fseek(f1, 0L, SEEK_SET) == -1)
4559 return got_ferror(f1, GOT_ERR_IO);
4560 if (fseek(f2, 0L, SEEK_SET) == -1)
4561 return got_ferror(f2, GOT_ERR_IO);
4563 /* Count the number of actual changes in the diff result. */
4564 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4565 struct diff_chunk_context cc = {};
4566 diff_chunk_context_load_change(&cc, &nchunks_used,
4567 diffreg_result->result, n, 0);
4570 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4572 err = apply_or_reject_change(&choice, &nchunks_used,
4573 diffreg_result->result, n, relpath, f1, f2,
4574 &line_cur1, &line_cur2,
4575 reverse_patch ? NULL : outfile,
4576 reverse_patch ? outfile : NULL,
4577 ++i, nchanges, patch_cb, patch_arg);
4580 if (choice == GOT_PATCH_CHOICE_YES)
4582 else if (choice == GOT_PATCH_CHOICE_QUIT)
4586 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4587 reverse_patch ? NULL : outfile,
4588 reverse_patch ? outfile : NULL);
4592 if (!S_ISLNK(sb2.st_mode)) {
4593 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4594 err = got_error_from_errno2("fchmod", path2);
4601 if (fd != -1 && close(fd) == -1 && err == NULL)
4602 err = got_error_from_errno("close");
4604 got_object_blob_close(blob);
4605 free_err = got_diffreg_result_free(diffreg_result);
4608 if (f1 && fclose(f1) == EOF && err == NULL)
4609 err = got_error_from_errno2("fclose", path1);
4610 if (f2 && fclose(f2) == EOF && err == NULL)
4611 err = got_error_from_errno2("fclose", path2);
4612 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4613 err = got_error_from_errno2("close", path2);
4614 if (outfile && fclose(outfile) == EOF && err == NULL)
4615 err = got_error_from_errno2("fclose", *path_outfile);
4616 if (path1 && unlink(path1) == -1 && err == NULL)
4617 err = got_error_from_errno2("unlink", path1);
4618 if (err || !have_content) {
4619 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4620 err = got_error_from_errno2("unlink", *path_outfile);
4621 free(*path_outfile);
4622 *path_outfile = NULL;
4628 static const struct got_error *
4629 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4630 const char *relpath, struct got_object_id *blob_id,
4631 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4632 int dirfd, const char *de_name)
4634 struct revert_file_args *a = arg;
4635 const struct got_error *err = NULL;
4636 char *parent_path = NULL;
4637 struct got_fileindex_entry *ie;
4638 struct got_commit_object *base_commit = NULL;
4639 struct got_tree_object *tree = NULL;
4640 struct got_object_id *tree_id = NULL;
4641 const struct got_tree_entry *te = NULL;
4642 char *tree_path = NULL, *te_name;
4643 char *ondisk_path = NULL, *path_content = NULL;
4644 struct got_blob_object *blob = NULL;
4647 /* Reverting a staged deletion is a no-op. */
4648 if (status == GOT_STATUS_DELETE &&
4649 staged_status != GOT_STATUS_NO_CHANGE)
4652 if (status == GOT_STATUS_UNVERSIONED)
4653 return (*a->progress_cb)(a->progress_arg,
4654 GOT_STATUS_UNVERSIONED, relpath);
4656 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4658 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4660 /* Construct in-repository path of tree which contains this blob. */
4661 err = got_path_dirname(&parent_path, ie->path);
4663 if (err->code != GOT_ERR_BAD_PATH)
4665 parent_path = strdup("/");
4666 if (parent_path == NULL) {
4667 err = got_error_from_errno("strdup");
4671 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4672 tree_path = strdup(parent_path);
4673 if (tree_path == NULL) {
4674 err = got_error_from_errno("strdup");
4678 if (got_path_is_root_dir(parent_path)) {
4679 tree_path = strdup(a->worktree->path_prefix);
4680 if (tree_path == NULL) {
4681 err = got_error_from_errno("strdup");
4685 if (asprintf(&tree_path, "%s/%s",
4686 a->worktree->path_prefix, parent_path) == -1) {
4687 err = got_error_from_errno("asprintf");
4693 err = got_object_open_as_commit(&base_commit, a->repo,
4694 a->worktree->base_commit_id);
4698 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4700 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4701 (status == GOT_STATUS_ADD ||
4702 staged_status == GOT_STATUS_ADD)))
4705 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4709 err = got_path_basename(&te_name, ie->path);
4713 te = got_object_tree_find_entry(tree, te_name);
4715 if (te == NULL && status != GOT_STATUS_ADD &&
4716 staged_status != GOT_STATUS_ADD) {
4717 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4723 case GOT_STATUS_ADD:
4725 int choice = GOT_PATCH_CHOICE_NONE;
4726 err = (*a->patch_cb)(&choice, a->patch_arg,
4727 status, ie->path, NULL, 1, 1);
4730 if (choice != GOT_PATCH_CHOICE_YES)
4733 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4737 got_fileindex_entry_remove(a->fileindex, ie);
4738 if (a->unlink_added_files) {
4739 if (asprintf(&ondisk_path, "%s/%s",
4740 got_worktree_get_root_path(a->worktree),
4742 err = got_error_from_errno("asprintf");
4745 if (unlink(ondisk_path) == -1) {
4746 err = got_error_from_errno2("unlink",
4752 case GOT_STATUS_DELETE:
4754 int choice = GOT_PATCH_CHOICE_NONE;
4755 err = (*a->patch_cb)(&choice, a->patch_arg,
4756 status, ie->path, NULL, 1, 1);
4759 if (choice != GOT_PATCH_CHOICE_YES)
4763 case GOT_STATUS_MODIFY:
4764 case GOT_STATUS_MODE_CHANGE:
4765 case GOT_STATUS_CONFLICT:
4766 case GOT_STATUS_MISSING: {
4767 struct got_object_id id;
4768 if (staged_status == GOT_STATUS_ADD ||
4769 staged_status == GOT_STATUS_MODIFY) {
4770 memcpy(id.sha1, ie->staged_blob_sha1,
4771 SHA1_DIGEST_LENGTH);
4773 memcpy(id.sha1, ie->blob_sha1,
4774 SHA1_DIGEST_LENGTH);
4775 fd = got_opentempfd();
4777 err = got_error_from_errno("got_opentempfd");
4781 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4785 if (asprintf(&ondisk_path, "%s/%s",
4786 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4787 err = got_error_from_errno("asprintf");
4791 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4792 status == GOT_STATUS_CONFLICT)) {
4793 int is_bad_symlink = 0;
4794 err = create_patched_content(&path_content, 1, &id,
4795 ondisk_path, dirfd, de_name, ie->path, a->repo,
4796 a->patch_cb, a->patch_arg);
4797 if (err || path_content == NULL)
4799 if (te && S_ISLNK(te->mode)) {
4800 if (unlink(path_content) == -1) {
4801 err = got_error_from_errno2("unlink",
4805 err = install_symlink(&is_bad_symlink,
4806 a->worktree, ondisk_path, ie->path,
4807 blob, 0, 1, 0, 0, a->repo,
4808 a->progress_cb, a->progress_arg);
4810 if (rename(path_content, ondisk_path) == -1) {
4811 err = got_error_from_errno3("rename",
4812 path_content, ondisk_path);
4817 int is_bad_symlink = 0;
4818 if (te && S_ISLNK(te->mode)) {
4819 err = install_symlink(&is_bad_symlink,
4820 a->worktree, ondisk_path, ie->path,
4821 blob, 0, 1, 0, 0, a->repo,
4822 a->progress_cb, a->progress_arg);
4824 err = install_blob(a->worktree, ondisk_path,
4826 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4827 got_fileindex_perms_to_st(ie), blob,
4828 0, 1, 0, 0, a->repo,
4829 a->progress_cb, a->progress_arg);
4833 if (status == GOT_STATUS_DELETE ||
4834 status == GOT_STATUS_MODE_CHANGE) {
4835 err = got_fileindex_entry_update(ie,
4836 a->worktree->root_fd, relpath,
4838 a->worktree->base_commit_id->sha1, 1);
4842 if (is_bad_symlink) {
4843 got_fileindex_entry_filetype_set(ie,
4844 GOT_FILEIDX_MODE_BAD_SYMLINK);
4857 if (fd != -1 && close(fd) == -1 && err == NULL)
4858 err = got_error_from_errno("close");
4860 got_object_blob_close(blob);
4862 got_object_tree_close(tree);
4865 got_object_commit_close(base_commit);
4869 const struct got_error *
4870 got_worktree_revert(struct got_worktree *worktree,
4871 struct got_pathlist_head *paths,
4872 got_worktree_checkout_cb progress_cb, void *progress_arg,
4873 got_worktree_patch_cb patch_cb, void *patch_arg,
4874 struct got_repository *repo)
4876 struct got_fileindex *fileindex = NULL;
4877 char *fileindex_path = NULL;
4878 const struct got_error *err = NULL, *unlockerr = NULL;
4879 const struct got_error *sync_err = NULL;
4880 struct got_pathlist_entry *pe;
4881 struct revert_file_args rfa;
4883 err = lock_worktree(worktree, LOCK_EX);
4887 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4891 rfa.worktree = worktree;
4892 rfa.fileindex = fileindex;
4893 rfa.progress_cb = progress_cb;
4894 rfa.progress_arg = progress_arg;
4895 rfa.patch_cb = patch_cb;
4896 rfa.patch_arg = patch_arg;
4898 rfa.unlink_added_files = 0;
4899 TAILQ_FOREACH(pe, paths, entry) {
4900 err = worktree_status(worktree, pe->path, fileindex, repo,
4901 revert_file, &rfa, NULL, NULL, 1, 0);
4905 sync_err = sync_fileindex(fileindex, fileindex_path);
4906 if (sync_err && err == NULL)
4909 free(fileindex_path);
4911 got_fileindex_free(fileindex);
4912 unlockerr = lock_worktree(worktree, LOCK_SH);
4913 if (unlockerr && err == NULL)
4919 free_commitable(struct got_commitable *ct)
4922 free(ct->in_repo_path);
4923 free(ct->ondisk_path);
4925 free(ct->base_blob_id);
4926 free(ct->staged_blob_id);
4927 free(ct->base_commit_id);
4931 struct collect_commitables_arg {
4932 struct got_pathlist_head *commitable_paths;
4933 struct got_repository *repo;
4934 struct got_worktree *worktree;
4935 struct got_fileindex *fileindex;
4936 int have_staged_files;
4937 int allow_bad_symlinks;
4940 static const struct got_error *
4941 collect_commitables(void *arg, unsigned char status,
4942 unsigned char staged_status, const char *relpath,
4943 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4944 struct got_object_id *commit_id, int dirfd, const char *de_name)
4946 struct collect_commitables_arg *a = arg;
4947 const struct got_error *err = NULL;
4948 struct got_commitable *ct = NULL;
4949 struct got_pathlist_entry *new = NULL;
4950 char *parent_path = NULL, *path = NULL;
4953 if (a->have_staged_files) {
4954 if (staged_status != GOT_STATUS_MODIFY &&
4955 staged_status != GOT_STATUS_ADD &&
4956 staged_status != GOT_STATUS_DELETE)
4959 if (status == GOT_STATUS_CONFLICT)
4960 return got_error(GOT_ERR_COMMIT_CONFLICT);
4962 if (status != GOT_STATUS_MODIFY &&
4963 status != GOT_STATUS_MODE_CHANGE &&
4964 status != GOT_STATUS_ADD &&
4965 status != GOT_STATUS_DELETE)
4969 if (asprintf(&path, "/%s", relpath) == -1) {
4970 err = got_error_from_errno("asprintf");
4973 if (strcmp(path, "/") == 0) {
4974 parent_path = strdup("");
4975 if (parent_path == NULL)
4976 return got_error_from_errno("strdup");
4978 err = got_path_dirname(&parent_path, path);
4983 ct = calloc(1, sizeof(*ct));
4985 err = got_error_from_errno("calloc");
4989 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4991 err = got_error_from_errno("asprintf");
4995 if (staged_status == GOT_STATUS_ADD ||
4996 staged_status == GOT_STATUS_MODIFY) {
4997 struct got_fileindex_entry *ie;
4998 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4999 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5000 case GOT_FILEIDX_MODE_REGULAR_FILE:
5001 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5004 case GOT_FILEIDX_MODE_SYMLINK:
5008 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5011 ct->mode |= got_fileindex_entry_perms_get(ie);
5012 } else if (status != GOT_STATUS_DELETE &&
5013 staged_status != GOT_STATUS_DELETE) {
5015 if (fstatat(dirfd, de_name, &sb,
5016 AT_SYMLINK_NOFOLLOW) == -1) {
5017 err = got_error_from_errno2("fstatat",
5021 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5022 err = got_error_from_errno2("lstat", ct->ondisk_path);
5025 ct->mode = sb.st_mode;
5028 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5029 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5031 err = got_error_from_errno("asprintf");
5035 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5036 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5038 char target_path[PATH_MAX];
5040 target_len = readlink(ct->ondisk_path, target_path,
5041 sizeof(target_path));
5042 if (target_len == -1) {
5043 err = got_error_from_errno2("readlink",
5047 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5048 target_len, ct->ondisk_path, a->worktree->root_path);
5051 if (is_bad_symlink) {
5052 err = got_error_path(ct->ondisk_path,
5053 GOT_ERR_BAD_SYMLINK);
5059 ct->status = status;
5060 ct->staged_status = staged_status;
5061 ct->blob_id = NULL; /* will be filled in when blob gets created */
5062 if (ct->status != GOT_STATUS_ADD &&
5063 ct->staged_status != GOT_STATUS_ADD) {
5064 ct->base_blob_id = got_object_id_dup(blob_id);
5065 if (ct->base_blob_id == NULL) {
5066 err = got_error_from_errno("got_object_id_dup");
5069 ct->base_commit_id = got_object_id_dup(commit_id);
5070 if (ct->base_commit_id == NULL) {
5071 err = got_error_from_errno("got_object_id_dup");
5075 if (ct->staged_status == GOT_STATUS_ADD ||
5076 ct->staged_status == GOT_STATUS_MODIFY) {
5077 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5078 if (ct->staged_blob_id == NULL) {
5079 err = got_error_from_errno("got_object_id_dup");
5083 ct->path = strdup(path);
5084 if (ct->path == NULL) {
5085 err = got_error_from_errno("strdup");
5088 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5090 if (ct && (err || new == NULL))
5091 free_commitable(ct);
5097 static const struct got_error *write_tree(struct got_object_id **, int *,
5098 struct got_tree_object *, const char *, struct got_pathlist_head *,
5099 got_worktree_status_cb status_cb, void *status_arg,
5100 struct got_repository *);
5102 static const struct got_error *
5103 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5104 struct got_tree_entry *te, const char *parent_path,
5105 struct got_pathlist_head *commitable_paths,
5106 got_worktree_status_cb status_cb, void *status_arg,
5107 struct got_repository *repo)
5109 const struct got_error *err = NULL;
5110 struct got_tree_object *subtree;
5113 if (asprintf(&subpath, "%s%s%s", parent_path,
5114 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5115 return got_error_from_errno("asprintf");
5117 err = got_object_open_as_tree(&subtree, repo, &te->id);
5121 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5122 commitable_paths, status_cb, status_arg, repo);
5123 got_object_tree_close(subtree);
5128 static const struct got_error *
5129 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5131 const struct got_error *err = NULL;
5132 char *ct_parent_path = NULL;
5136 if (strchr(ct->in_repo_path, '/') == NULL) {
5137 *match = got_path_is_root_dir(path);
5141 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5144 *match = (strcmp(path, ct_parent_path) == 0);
5145 free(ct_parent_path);
5150 get_ct_file_mode(struct got_commitable *ct)
5152 if (S_ISLNK(ct->mode))
5155 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5158 static const struct got_error *
5159 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5160 struct got_tree_entry *te, struct got_commitable *ct)
5162 const struct got_error *err = NULL;
5166 err = got_object_tree_entry_dup(new_te, te);
5170 (*new_te)->mode = get_ct_file_mode(ct);
5172 if (ct->staged_status == GOT_STATUS_MODIFY)
5173 memcpy(&(*new_te)->id, ct->staged_blob_id,
5174 sizeof((*new_te)->id));
5176 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5178 if (err && *new_te) {
5185 static const struct got_error *
5186 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5187 struct got_commitable *ct)
5189 const struct got_error *err = NULL;
5190 char *ct_name = NULL;
5194 *new_te = calloc(1, sizeof(**new_te));
5195 if (*new_te == NULL)
5196 return got_error_from_errno("calloc");
5198 err = got_path_basename(&ct_name, ct->path);
5201 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5202 sizeof((*new_te)->name)) {
5203 err = got_error(GOT_ERR_NO_SPACE);
5207 (*new_te)->mode = get_ct_file_mode(ct);
5209 if (ct->staged_status == GOT_STATUS_ADD)
5210 memcpy(&(*new_te)->id, ct->staged_blob_id,
5211 sizeof((*new_te)->id));
5213 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5216 if (err && *new_te) {
5223 static const struct got_error *
5224 insert_tree_entry(struct got_tree_entry *new_te,
5225 struct got_pathlist_head *paths)
5227 const struct got_error *err = NULL;
5228 struct got_pathlist_entry *new_pe;
5230 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5234 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5238 static const struct got_error *
5239 report_ct_status(struct got_commitable *ct,
5240 got_worktree_status_cb status_cb, void *status_arg)
5242 const char *ct_path = ct->path;
5243 unsigned char status;
5245 if (status_cb == NULL) /* no commit progress output desired */
5248 while (ct_path[0] == '/')
5251 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5252 status = ct->staged_status;
5254 status = ct->status;
5256 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5257 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5260 static const struct got_error *
5261 match_modified_subtree(int *modified, struct got_tree_entry *te,
5262 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5264 const struct got_error *err = NULL;
5265 struct got_pathlist_entry *pe;
5270 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5271 got_path_is_root_dir(base_tree_path) ? "" : "/",
5273 return got_error_from_errno("asprintf");
5275 TAILQ_FOREACH(pe, commitable_paths, entry) {
5276 struct got_commitable *ct = pe->data;
5277 *modified = got_path_is_child(ct->in_repo_path, te_path,
5287 static const struct got_error *
5288 match_deleted_or_modified_ct(struct got_commitable **ctp,
5289 struct got_tree_entry *te, const char *base_tree_path,
5290 struct got_pathlist_head *commitable_paths)
5292 const struct got_error *err = NULL;
5293 struct got_pathlist_entry *pe;
5297 TAILQ_FOREACH(pe, commitable_paths, entry) {
5298 struct got_commitable *ct = pe->data;
5299 char *ct_name = NULL;
5302 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5303 if (ct->status != GOT_STATUS_MODIFY &&
5304 ct->status != GOT_STATUS_MODE_CHANGE &&
5305 ct->status != GOT_STATUS_DELETE)
5308 if (ct->staged_status != GOT_STATUS_MODIFY &&
5309 ct->staged_status != GOT_STATUS_DELETE)
5313 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5316 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5322 err = got_path_basename(&ct_name, pe->path);
5326 if (strcmp(te->name, ct_name) != 0) {
5339 static const struct got_error *
5340 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5341 const char *child_path, const char *path_base_tree,
5342 struct got_pathlist_head *commitable_paths,
5343 got_worktree_status_cb status_cb, void *status_arg,
5344 struct got_repository *repo)
5346 const struct got_error *err = NULL;
5347 struct got_tree_entry *new_te;
5349 struct got_object_id *id = NULL;
5354 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5355 got_path_is_root_dir(path_base_tree) ? "" : "/",
5357 return got_error_from_errno("asprintf");
5359 new_te = calloc(1, sizeof(*new_te));
5361 return got_error_from_errno("calloc");
5362 new_te->mode = S_IFDIR;
5364 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5365 sizeof(new_te->name)) {
5366 err = got_error(GOT_ERR_NO_SPACE);
5369 err = write_tree(&id, &nentries, NULL, subtree_path,
5370 commitable_paths, status_cb, status_arg, repo);
5375 memcpy(&new_te->id, id, sizeof(new_te->id));
5384 static const struct got_error *
5385 write_tree(struct got_object_id **new_tree_id, int *nentries,
5386 struct got_tree_object *base_tree, const char *path_base_tree,
5387 struct got_pathlist_head *commitable_paths,
5388 got_worktree_status_cb status_cb, void *status_arg,
5389 struct got_repository *repo)
5391 const struct got_error *err = NULL;
5392 struct got_pathlist_head paths;
5393 struct got_tree_entry *te, *new_te = NULL;
5394 struct got_pathlist_entry *pe;
5399 /* Insert, and recurse into, newly added entries first. */
5400 TAILQ_FOREACH(pe, commitable_paths, entry) {
5401 struct got_commitable *ct = pe->data;
5402 char *child_path = NULL, *slash;
5404 if ((ct->status != GOT_STATUS_ADD &&
5405 ct->staged_status != GOT_STATUS_ADD) ||
5406 (ct->flags & GOT_COMMITABLE_ADDED))
5409 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5410 strlen(path_base_tree)))
5413 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5418 slash = strchr(child_path, '/');
5419 if (slash == NULL) {
5420 err = alloc_added_blob_tree_entry(&new_te, ct);
5423 err = report_ct_status(ct, status_cb, status_arg);
5426 ct->flags |= GOT_COMMITABLE_ADDED;
5427 err = insert_tree_entry(new_te, &paths);
5432 *slash = '\0'; /* trim trailing path components */
5433 if (base_tree == NULL ||
5434 got_object_tree_find_entry(base_tree, child_path)
5436 err = make_subtree_for_added_blob(&new_te,
5437 child_path, path_base_tree,
5438 commitable_paths, status_cb, status_arg,
5442 err = insert_tree_entry(new_te, &paths);
5451 int i, nbase_entries;
5452 /* Handle modified and deleted entries. */
5453 nbase_entries = got_object_tree_get_nentries(base_tree);
5454 for (i = 0; i < nbase_entries; i++) {
5455 struct got_commitable *ct = NULL;
5457 te = got_object_tree_get_entry(base_tree, i);
5458 if (got_object_tree_entry_is_submodule(te)) {
5459 /* Entry is a submodule; just copy it. */
5460 err = got_object_tree_entry_dup(&new_te, te);
5463 err = insert_tree_entry(new_te, &paths);
5470 if (S_ISDIR(te->mode)) {
5472 err = got_object_tree_entry_dup(&new_te, te);
5475 err = match_modified_subtree(&modified, te,
5476 path_base_tree, commitable_paths);
5479 /* Avoid recursion into unmodified subtrees. */
5481 struct got_object_id *new_id;
5483 err = write_subtree(&new_id,
5485 path_base_tree, commitable_paths,
5486 status_cb, status_arg, repo);
5489 if (nsubentries == 0) {
5490 /* All entries were deleted. */
5494 memcpy(&new_te->id, new_id,
5495 sizeof(new_te->id));
5498 err = insert_tree_entry(new_te, &paths);
5505 err = match_deleted_or_modified_ct(&ct, te,
5506 path_base_tree, commitable_paths);
5510 /* NB: Deleted entries get dropped here. */
5511 if (ct->status == GOT_STATUS_MODIFY ||
5512 ct->status == GOT_STATUS_MODE_CHANGE ||
5513 ct->staged_status == GOT_STATUS_MODIFY) {
5514 err = alloc_modified_blob_tree_entry(
5518 err = insert_tree_entry(new_te, &paths);
5523 err = report_ct_status(ct, status_cb,
5528 /* Entry is unchanged; just copy it. */
5529 err = got_object_tree_entry_dup(&new_te, te);
5532 err = insert_tree_entry(new_te, &paths);
5540 /* Write new list of entries; deleted entries have been dropped. */
5541 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5543 got_pathlist_free(&paths);
5547 static const struct got_error *
5548 update_fileindex_after_commit(struct got_worktree *worktree,
5549 struct got_pathlist_head *commitable_paths,
5550 struct got_object_id *new_base_commit_id,
5551 struct got_fileindex *fileindex, int have_staged_files)
5553 const struct got_error *err = NULL;
5554 struct got_pathlist_entry *pe;
5555 char *relpath = NULL;
5557 TAILQ_FOREACH(pe, commitable_paths, entry) {
5558 struct got_fileindex_entry *ie;
5559 struct got_commitable *ct = pe->data;
5561 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5563 err = got_path_skip_common_ancestor(&relpath,
5564 worktree->root_path, ct->ondisk_path);
5569 if (ct->status == GOT_STATUS_DELETE ||
5570 ct->staged_status == GOT_STATUS_DELETE) {
5571 got_fileindex_entry_remove(fileindex, ie);
5572 } else if (ct->staged_status == GOT_STATUS_ADD ||
5573 ct->staged_status == GOT_STATUS_MODIFY) {
5574 got_fileindex_entry_stage_set(ie,
5575 GOT_FILEIDX_STAGE_NONE);
5576 got_fileindex_entry_staged_filetype_set(ie, 0);
5578 err = got_fileindex_entry_update(ie,
5579 worktree->root_fd, relpath,
5580 ct->staged_blob_id->sha1,
5581 new_base_commit_id->sha1,
5582 !have_staged_files);
5584 err = got_fileindex_entry_update(ie,
5585 worktree->root_fd, relpath,
5587 new_base_commit_id->sha1,
5588 !have_staged_files);
5590 err = got_fileindex_entry_alloc(&ie, pe->path);
5593 err = got_fileindex_entry_update(ie,
5594 worktree->root_fd, relpath, ct->blob_id->sha1,
5595 new_base_commit_id->sha1, 1);
5597 got_fileindex_entry_free(ie);
5600 err = got_fileindex_entry_add(fileindex, ie);
5602 got_fileindex_entry_free(ie);
5615 static const struct got_error *
5616 check_out_of_date(const char *in_repo_path, unsigned char status,
5617 unsigned char staged_status, struct got_object_id *base_blob_id,
5618 struct got_object_id *base_commit_id,
5619 struct got_object_id *head_commit_id, struct got_repository *repo,
5622 const struct got_error *err = NULL;
5623 struct got_commit_object *commit = NULL;
5624 struct got_object_id *id = NULL;
5626 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5627 /* Trivial case: base commit == head commit */
5628 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5631 * Ensure file content which local changes were based
5632 * on matches file content in the branch head.
5634 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5637 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5639 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5640 err = got_error(ood_errcode);
5642 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5643 err = got_error(ood_errcode);
5645 /* Require that added files don't exist in the branch head. */
5646 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5649 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5650 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5652 err = id ? got_error(ood_errcode) : NULL;
5657 got_object_commit_close(commit);
5661 static const struct got_error *
5662 commit_worktree(struct got_object_id **new_commit_id,
5663 struct got_pathlist_head *commitable_paths,
5664 struct got_object_id *head_commit_id,
5665 struct got_object_id *parent_id2,
5666 struct got_worktree *worktree,
5667 const char *author, const char *committer,
5668 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5669 got_worktree_status_cb status_cb, void *status_arg,
5670 struct got_repository *repo)
5672 const struct got_error *err = NULL, *unlockerr = NULL;
5673 struct got_pathlist_entry *pe;
5674 const char *head_ref_name = NULL;
5675 struct got_commit_object *head_commit = NULL;
5676 struct got_reference *head_ref2 = NULL;
5677 struct got_object_id *head_commit_id2 = NULL;
5678 struct got_tree_object *head_tree = NULL;
5679 struct got_object_id *new_tree_id = NULL;
5680 int nentries, nparents = 0;
5681 struct got_object_id_queue parent_ids;
5682 struct got_object_qid *pid = NULL;
5683 char *logmsg = NULL;
5685 *new_commit_id = NULL;
5687 STAILQ_INIT(&parent_ids);
5689 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5693 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5697 if (commit_msg_cb != NULL) {
5698 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5703 if (logmsg == NULL || strlen(logmsg) == 0) {
5704 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5708 /* Create blobs from added and modified files and record their IDs. */
5709 TAILQ_FOREACH(pe, commitable_paths, entry) {
5710 struct got_commitable *ct = pe->data;
5713 /* Blobs for staged files already exist. */
5714 if (ct->staged_status == GOT_STATUS_ADD ||
5715 ct->staged_status == GOT_STATUS_MODIFY)
5718 if (ct->status != GOT_STATUS_ADD &&
5719 ct->status != GOT_STATUS_MODIFY &&
5720 ct->status != GOT_STATUS_MODE_CHANGE)
5723 if (asprintf(&ondisk_path, "%s/%s",
5724 worktree->root_path, pe->path) == -1) {
5725 err = got_error_from_errno("asprintf");
5728 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5734 /* Recursively write new tree objects. */
5735 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5736 commitable_paths, status_cb, status_arg, repo);
5740 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5743 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5746 err = got_object_qid_alloc(&pid, parent_id2);
5749 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5752 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5753 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5759 /* Check if a concurrent commit to our branch has occurred. */
5760 head_ref_name = got_worktree_get_head_ref_name(worktree);
5761 if (head_ref_name == NULL) {
5762 err = got_error_from_errno("got_worktree_get_head_ref_name");
5765 /* Lock the reference here to prevent concurrent modification. */
5766 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5769 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5772 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5773 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5776 /* Update branch head in repository. */
5777 err = got_ref_change_ref(head_ref2, *new_commit_id);
5780 err = got_ref_write(head_ref2, repo);
5784 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5788 err = ref_base_commit(worktree, repo);
5792 got_object_id_queue_free(&parent_ids);
5794 got_object_tree_close(head_tree);
5796 got_object_commit_close(head_commit);
5797 free(head_commit_id2);
5799 unlockerr = got_ref_unlock(head_ref2);
5800 if (unlockerr && err == NULL)
5802 got_ref_close(head_ref2);
5807 static const struct got_error *
5808 check_path_is_commitable(const char *path,
5809 struct got_pathlist_head *commitable_paths)
5811 struct got_pathlist_entry *cpe = NULL;
5812 size_t path_len = strlen(path);
5814 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5815 struct got_commitable *ct = cpe->data;
5816 const char *ct_path = ct->path;
5818 while (ct_path[0] == '/')
5821 if (strcmp(path, ct_path) == 0 ||
5822 got_path_is_child(ct_path, path, path_len))
5827 return got_error_path(path, GOT_ERR_BAD_PATH);
5832 static const struct got_error *
5833 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5835 int *have_staged_files = arg;
5837 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5838 *have_staged_files = 1;
5839 return got_error(GOT_ERR_CANCELLED);
5845 static const struct got_error *
5846 check_non_staged_files(struct got_fileindex *fileindex,
5847 struct got_pathlist_head *paths)
5849 struct got_pathlist_entry *pe;
5850 struct got_fileindex_entry *ie;
5852 TAILQ_FOREACH(pe, paths, entry) {
5853 if (pe->path[0] == '\0')
5855 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5857 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5858 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5859 return got_error_path(pe->path,
5860 GOT_ERR_FILE_NOT_STAGED);
5866 const struct got_error *
5867 got_worktree_commit(struct got_object_id **new_commit_id,
5868 struct got_worktree *worktree, struct got_pathlist_head *paths,
5869 const char *author, const char *committer, int allow_bad_symlinks,
5870 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5871 got_worktree_status_cb status_cb, void *status_arg,
5872 struct got_repository *repo)
5874 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5875 struct got_fileindex *fileindex = NULL;
5876 char *fileindex_path = NULL;
5877 struct got_pathlist_head commitable_paths;
5878 struct collect_commitables_arg cc_arg;
5879 struct got_pathlist_entry *pe;
5880 struct got_reference *head_ref = NULL;
5881 struct got_object_id *head_commit_id = NULL;
5882 int have_staged_files = 0;
5884 *new_commit_id = NULL;
5886 TAILQ_INIT(&commitable_paths);
5888 err = lock_worktree(worktree, LOCK_EX);
5892 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5896 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5900 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5904 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5905 &have_staged_files);
5906 if (err && err->code != GOT_ERR_CANCELLED)
5908 if (have_staged_files) {
5909 err = check_non_staged_files(fileindex, paths);
5914 cc_arg.commitable_paths = &commitable_paths;
5915 cc_arg.worktree = worktree;
5916 cc_arg.fileindex = fileindex;
5918 cc_arg.have_staged_files = have_staged_files;
5919 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5920 TAILQ_FOREACH(pe, paths, entry) {
5921 err = worktree_status(worktree, pe->path, fileindex, repo,
5922 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5927 if (TAILQ_EMPTY(&commitable_paths)) {
5928 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5932 TAILQ_FOREACH(pe, paths, entry) {
5933 err = check_path_is_commitable(pe->path, &commitable_paths);
5938 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5939 struct got_commitable *ct = pe->data;
5940 const char *ct_path = ct->in_repo_path;
5942 while (ct_path[0] == '/')
5944 err = check_out_of_date(ct_path, ct->status,
5945 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5946 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5952 err = commit_worktree(new_commit_id, &commitable_paths,
5953 head_commit_id, NULL, worktree, author, committer,
5954 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5958 err = update_fileindex_after_commit(worktree, &commitable_paths,
5959 *new_commit_id, fileindex, have_staged_files);
5960 sync_err = sync_fileindex(fileindex, fileindex_path);
5961 if (sync_err && err == NULL)
5965 got_fileindex_free(fileindex);
5966 free(fileindex_path);
5967 unlockerr = lock_worktree(worktree, LOCK_SH);
5968 if (unlockerr && err == NULL)
5970 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5971 struct got_commitable *ct = pe->data;
5972 free_commitable(ct);
5974 got_pathlist_free(&commitable_paths);
5979 got_commitable_get_path(struct got_commitable *ct)
5985 got_commitable_get_status(struct got_commitable *ct)
5990 struct check_rebase_ok_arg {
5991 struct got_worktree *worktree;
5992 struct got_repository *repo;
5995 static const struct got_error *
5996 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5998 const struct got_error *err = NULL;
5999 struct check_rebase_ok_arg *a = arg;
6000 unsigned char status;
6004 /* Reject rebase of a work tree with mixed base commits. */
6005 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6006 SHA1_DIGEST_LENGTH))
6007 return got_error(GOT_ERR_MIXED_COMMITS);
6009 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6011 return got_error_from_errno("asprintf");
6013 /* Reject rebase of a work tree with modified or staged files. */
6014 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6019 if (status != GOT_STATUS_NO_CHANGE)
6020 return got_error(GOT_ERR_MODIFIED);
6021 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6022 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6027 const struct got_error *
6028 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6029 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6030 struct got_worktree *worktree, struct got_reference *branch,
6031 struct got_repository *repo)
6033 const struct got_error *err = NULL;
6034 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6035 char *branch_ref_name = NULL;
6036 char *fileindex_path = NULL;
6037 struct check_rebase_ok_arg ok_arg;
6038 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6039 struct got_object_id *wt_branch_tip = NULL;
6041 *new_base_branch_ref = NULL;
6045 err = lock_worktree(worktree, LOCK_EX);
6049 err = open_fileindex(fileindex, &fileindex_path, worktree);
6053 ok_arg.worktree = worktree;
6055 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6060 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6064 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6068 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6072 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6077 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6080 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6081 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6085 err = got_ref_alloc_symref(new_base_branch_ref,
6086 new_base_branch_ref_name, wt_branch);
6089 err = got_ref_write(*new_base_branch_ref, repo);
6093 /* TODO Lock original branch's ref while rebasing? */
6095 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6099 err = got_ref_write(branch_ref, repo);
6103 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6104 worktree->base_commit_id);
6107 err = got_ref_write(*tmp_branch, repo);
6111 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6115 free(fileindex_path);
6116 free(tmp_branch_name);
6117 free(new_base_branch_ref_name);
6118 free(branch_ref_name);
6120 got_ref_close(branch_ref);
6122 got_ref_close(wt_branch);
6123 free(wt_branch_tip);
6125 if (*new_base_branch_ref) {
6126 got_ref_close(*new_base_branch_ref);
6127 *new_base_branch_ref = NULL;
6130 got_ref_close(*tmp_branch);
6134 got_fileindex_free(*fileindex);
6137 lock_worktree(worktree, LOCK_SH);
6142 const struct got_error *
6143 got_worktree_rebase_continue(struct got_object_id **commit_id,
6144 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6145 struct got_reference **branch, struct got_fileindex **fileindex,
6146 struct got_worktree *worktree, struct got_repository *repo)
6148 const struct got_error *err;
6149 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6150 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6151 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6152 char *fileindex_path = NULL;
6153 int have_staged_files = 0;
6156 *new_base_branch = NULL;
6161 err = lock_worktree(worktree, LOCK_EX);
6165 err = open_fileindex(fileindex, &fileindex_path, worktree);
6169 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6170 &have_staged_files);
6171 if (err && err->code != GOT_ERR_CANCELLED)
6173 if (have_staged_files) {
6174 err = got_error(GOT_ERR_STAGED_PATHS);
6178 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6182 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6186 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6190 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6194 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6198 err = got_ref_open(branch, repo,
6199 got_ref_get_symref_target(branch_ref), 0);
6203 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6207 err = got_ref_resolve(commit_id, repo, commit_ref);
6211 err = got_ref_open(new_base_branch, repo,
6212 new_base_branch_ref_name, 0);
6216 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6220 free(commit_ref_name);
6221 free(branch_ref_name);
6222 free(fileindex_path);
6224 got_ref_close(commit_ref);
6226 got_ref_close(branch_ref);
6231 got_ref_close(*tmp_branch);
6234 if (*new_base_branch) {
6235 got_ref_close(*new_base_branch);
6236 *new_base_branch = NULL;
6239 got_ref_close(*branch);
6243 got_fileindex_free(*fileindex);
6246 lock_worktree(worktree, LOCK_SH);
6251 const struct got_error *
6252 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6254 const struct got_error *err;
6255 char *tmp_branch_name = NULL;
6257 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6261 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6262 free(tmp_branch_name);
6266 static const struct got_error *
6267 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6268 char **logmsg, void *arg)
6274 static const struct got_error *
6275 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6276 const char *path, struct got_object_id *blob_id,
6277 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6278 int dirfd, const char *de_name)
6283 struct collect_merged_paths_arg {
6284 got_worktree_checkout_cb progress_cb;
6286 struct got_pathlist_head *merged_paths;
6289 static const struct got_error *
6290 collect_merged_paths(void *arg, unsigned char status, const char *path)
6292 const struct got_error *err;
6293 struct collect_merged_paths_arg *a = arg;
6295 struct got_pathlist_entry *new;
6297 err = (*a->progress_cb)(a->progress_arg, status, path);
6301 if (status != GOT_STATUS_MERGE &&
6302 status != GOT_STATUS_ADD &&
6303 status != GOT_STATUS_DELETE &&
6304 status != GOT_STATUS_CONFLICT)
6309 return got_error_from_errno("strdup");
6311 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6312 if (err || new == NULL)
6318 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6320 struct got_pathlist_entry *pe;
6322 TAILQ_FOREACH(pe, merged_paths, entry)
6323 free((char *)pe->path);
6325 got_pathlist_free(merged_paths);
6328 static const struct got_error *
6329 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6330 int is_rebase, struct got_repository *repo)
6332 const struct got_error *err;
6333 struct got_reference *commit_ref = NULL;
6335 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6337 if (err->code != GOT_ERR_NOT_REF)
6339 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6342 err = got_ref_write(commit_ref, repo);
6345 } else if (is_rebase) {
6346 struct got_object_id *stored_id;
6349 err = got_ref_resolve(&stored_id, repo, commit_ref);
6352 cmp = got_object_id_cmp(commit_id, stored_id);
6355 err = got_error(GOT_ERR_REBASE_COMMITID);
6361 got_ref_close(commit_ref);
6365 static const struct got_error *
6366 rebase_merge_files(struct got_pathlist_head *merged_paths,
6367 const char *commit_ref_name, struct got_worktree *worktree,
6368 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6369 struct got_object_id *commit_id, struct got_repository *repo,
6370 got_worktree_checkout_cb progress_cb, void *progress_arg,
6371 got_cancel_cb cancel_cb, void *cancel_arg)
6373 const struct got_error *err;
6374 struct got_reference *commit_ref = NULL;
6375 struct collect_merged_paths_arg cmp_arg;
6376 char *fileindex_path;
6378 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6380 err = get_fileindex_path(&fileindex_path, worktree);
6384 cmp_arg.progress_cb = progress_cb;
6385 cmp_arg.progress_arg = progress_arg;
6386 cmp_arg.merged_paths = merged_paths;
6387 err = merge_files(worktree, fileindex, fileindex_path,
6388 parent_commit_id, commit_id, repo, collect_merged_paths,
6389 &cmp_arg, cancel_cb, cancel_arg);
6391 got_ref_close(commit_ref);
6395 const struct got_error *
6396 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6397 struct got_worktree *worktree, struct got_fileindex *fileindex,
6398 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6399 struct got_repository *repo,
6400 got_worktree_checkout_cb progress_cb, void *progress_arg,
6401 got_cancel_cb cancel_cb, void *cancel_arg)
6403 const struct got_error *err;
6404 char *commit_ref_name;
6406 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6410 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6414 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6415 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6416 progress_arg, cancel_cb, cancel_arg);
6418 free(commit_ref_name);
6422 const struct got_error *
6423 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6424 struct got_worktree *worktree, struct got_fileindex *fileindex,
6425 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6426 struct got_repository *repo,
6427 got_worktree_checkout_cb progress_cb, void *progress_arg,
6428 got_cancel_cb cancel_cb, void *cancel_arg)
6430 const struct got_error *err;
6431 char *commit_ref_name;
6433 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6437 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6441 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6442 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6443 progress_arg, cancel_cb, cancel_arg);
6445 free(commit_ref_name);
6449 static const struct got_error *
6450 rebase_commit(struct got_object_id **new_commit_id,
6451 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6452 struct got_worktree *worktree, struct got_fileindex *fileindex,
6453 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6454 const char *new_logmsg, struct got_repository *repo)
6456 const struct got_error *err, *sync_err;
6457 struct got_pathlist_head commitable_paths;
6458 struct collect_commitables_arg cc_arg;
6459 char *fileindex_path = NULL;
6460 struct got_reference *head_ref = NULL;
6461 struct got_object_id *head_commit_id = NULL;
6462 char *logmsg = NULL;
6464 TAILQ_INIT(&commitable_paths);
6465 *new_commit_id = NULL;
6467 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6469 err = get_fileindex_path(&fileindex_path, worktree);
6473 cc_arg.commitable_paths = &commitable_paths;
6474 cc_arg.worktree = worktree;
6476 cc_arg.have_staged_files = 0;
6478 * If possible get the status of individual files directly to
6479 * avoid crawling the entire work tree once per rebased commit.
6481 * Ideally, merged_paths would contain a list of commitables
6482 * we could use so we could skip worktree_status() entirely.
6483 * However, we would then need carefully keep track of cumulative
6484 * effects of operations such as file additions and deletions
6485 * in 'got histedit -f' (folding multiple commits into one),
6486 * and this extra complexity is not really worth it.
6489 struct got_pathlist_entry *pe;
6490 TAILQ_FOREACH(pe, merged_paths, entry) {
6491 err = worktree_status(worktree, pe->path, fileindex,
6492 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6498 err = worktree_status(worktree, "", fileindex, repo,
6499 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6504 if (TAILQ_EMPTY(&commitable_paths)) {
6505 /* No-op change; commit will be elided. */
6506 err = got_ref_delete(commit_ref, repo);
6509 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6513 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6517 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6522 logmsg = strdup(new_logmsg);
6523 if (logmsg == NULL) {
6524 err = got_error_from_errno("strdup");
6528 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6533 /* NB: commit_worktree will call free(logmsg) */
6534 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6535 NULL, worktree, got_object_commit_get_author(orig_commit),
6536 got_object_commit_get_committer(orig_commit),
6537 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6541 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6545 err = got_ref_delete(commit_ref, repo);
6549 err = update_fileindex_after_commit(worktree, &commitable_paths,
6550 *new_commit_id, fileindex, 0);
6551 sync_err = sync_fileindex(fileindex, fileindex_path);
6552 if (sync_err && err == NULL)
6555 free(fileindex_path);
6556 free(head_commit_id);
6558 got_ref_close(head_ref);
6560 free(*new_commit_id);
6561 *new_commit_id = NULL;
6566 const struct got_error *
6567 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6568 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6569 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6570 struct got_commit_object *orig_commit,
6571 struct got_object_id *orig_commit_id, struct got_repository *repo)
6573 const struct got_error *err;
6574 char *commit_ref_name;
6575 struct got_reference *commit_ref = NULL;
6576 struct got_object_id *commit_id = NULL;
6578 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6582 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6585 err = got_ref_resolve(&commit_id, repo, commit_ref);
6588 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6589 err = got_error(GOT_ERR_REBASE_COMMITID);
6593 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6594 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6597 got_ref_close(commit_ref);
6598 free(commit_ref_name);
6603 const struct got_error *
6604 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6605 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6606 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6607 struct got_commit_object *orig_commit,
6608 struct got_object_id *orig_commit_id, const char *new_logmsg,
6609 struct got_repository *repo)
6611 const struct got_error *err;
6612 char *commit_ref_name;
6613 struct got_reference *commit_ref = NULL;
6615 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6619 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6623 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6624 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6627 got_ref_close(commit_ref);
6628 free(commit_ref_name);
6632 const struct got_error *
6633 got_worktree_rebase_postpone(struct got_worktree *worktree,
6634 struct got_fileindex *fileindex)
6637 got_fileindex_free(fileindex);
6638 return lock_worktree(worktree, LOCK_SH);
6641 static const struct got_error *
6642 delete_ref(const char *name, struct got_repository *repo)
6644 const struct got_error *err;
6645 struct got_reference *ref;
6647 err = got_ref_open(&ref, repo, name, 0);
6649 if (err->code == GOT_ERR_NOT_REF)
6654 err = got_ref_delete(ref, repo);
6659 static const struct got_error *
6660 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6662 const struct got_error *err;
6663 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6664 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6666 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6669 err = delete_ref(tmp_branch_name, repo);
6673 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6676 err = delete_ref(new_base_branch_ref_name, repo);
6680 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6683 err = delete_ref(branch_ref_name, repo);
6687 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6690 err = delete_ref(commit_ref_name, repo);
6695 free(tmp_branch_name);
6696 free(new_base_branch_ref_name);
6697 free(branch_ref_name);
6698 free(commit_ref_name);
6702 static const struct got_error *
6703 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6704 struct got_object_id *new_commit_id, struct got_repository *repo)
6706 const struct got_error *err;
6707 struct got_reference *ref = NULL;
6708 struct got_object_id *old_commit_id = NULL;
6709 const char *branch_name = NULL;
6710 char *new_id_str = NULL;
6711 char *refname = NULL;
6713 branch_name = got_ref_get_name(branch);
6714 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6715 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6718 err = got_object_id_str(&new_id_str, new_commit_id);
6722 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6723 new_id_str) == -1) {
6724 err = got_error_from_errno("asprintf");
6728 err = got_ref_resolve(&old_commit_id, repo, branch);
6732 err = got_ref_alloc(&ref, refname, old_commit_id);
6736 err = got_ref_write(ref, repo);
6740 free(old_commit_id);
6746 const struct got_error *
6747 got_worktree_rebase_complete(struct got_worktree *worktree,
6748 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6749 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6750 struct got_repository *repo, int create_backup)
6752 const struct got_error *err, *unlockerr, *sync_err;
6753 struct got_object_id *new_head_commit_id = NULL;
6754 char *fileindex_path = NULL;
6756 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6760 if (create_backup) {
6761 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6762 rebased_branch, new_head_commit_id, repo);
6767 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6771 err = got_ref_write(rebased_branch, repo);
6775 err = got_worktree_set_head_ref(worktree, rebased_branch);
6779 err = delete_rebase_refs(worktree, repo);
6783 err = get_fileindex_path(&fileindex_path, worktree);
6786 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6787 sync_err = sync_fileindex(fileindex, fileindex_path);
6788 if (sync_err && err == NULL)
6791 got_fileindex_free(fileindex);
6792 free(fileindex_path);
6793 free(new_head_commit_id);
6794 unlockerr = lock_worktree(worktree, LOCK_SH);
6795 if (unlockerr && err == NULL)
6800 const struct got_error *
6801 got_worktree_rebase_abort(struct got_worktree *worktree,
6802 struct got_fileindex *fileindex, struct got_repository *repo,
6803 struct got_reference *new_base_branch,
6804 got_worktree_checkout_cb progress_cb, void *progress_arg)
6806 const struct got_error *err, *unlockerr, *sync_err;
6807 struct got_reference *resolved = NULL;
6808 struct got_object_id *commit_id = NULL;
6809 struct got_commit_object *commit = NULL;
6810 char *fileindex_path = NULL;
6811 struct revert_file_args rfa;
6812 struct got_object_id *tree_id = NULL;
6814 err = lock_worktree(worktree, LOCK_EX);
6818 err = got_object_open_as_commit(&commit, repo,
6819 worktree->base_commit_id);
6823 err = got_ref_open(&resolved, repo,
6824 got_ref_get_symref_target(new_base_branch), 0);
6828 err = got_worktree_set_head_ref(worktree, resolved);
6833 * XXX commits to the base branch could have happened while
6834 * we were busy rebasing; should we store the original commit ID
6835 * when rebase begins and read it back here?
6837 err = got_ref_resolve(&commit_id, repo, resolved);
6841 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6845 err = got_object_id_by_path(&tree_id, repo, commit,
6846 worktree->path_prefix);
6850 err = delete_rebase_refs(worktree, repo);
6854 err = get_fileindex_path(&fileindex_path, worktree);
6858 rfa.worktree = worktree;
6859 rfa.fileindex = fileindex;
6860 rfa.progress_cb = progress_cb;
6861 rfa.progress_arg = progress_arg;
6862 rfa.patch_cb = NULL;
6863 rfa.patch_arg = NULL;
6865 rfa.unlink_added_files = 0;
6866 err = worktree_status(worktree, "", fileindex, repo,
6867 revert_file, &rfa, NULL, NULL, 1, 0);
6871 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6872 repo, progress_cb, progress_arg, NULL, NULL);
6874 sync_err = sync_fileindex(fileindex, fileindex_path);
6875 if (sync_err && err == NULL)
6878 got_ref_close(resolved);
6882 got_object_commit_close(commit);
6884 got_fileindex_free(fileindex);
6885 free(fileindex_path);
6887 unlockerr = lock_worktree(worktree, LOCK_SH);
6888 if (unlockerr && err == NULL)
6893 const struct got_error *
6894 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6895 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6896 struct got_fileindex **fileindex, struct got_worktree *worktree,
6897 struct got_repository *repo)
6899 const struct got_error *err = NULL;
6900 char *tmp_branch_name = NULL;
6901 char *branch_ref_name = NULL;
6902 char *base_commit_ref_name = NULL;
6903 char *fileindex_path = NULL;
6904 struct check_rebase_ok_arg ok_arg;
6905 struct got_reference *wt_branch = NULL;
6906 struct got_reference *base_commit_ref = NULL;
6910 *base_commit_id = NULL;
6913 err = lock_worktree(worktree, LOCK_EX);
6917 err = open_fileindex(fileindex, &fileindex_path, worktree);
6921 ok_arg.worktree = worktree;
6923 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6928 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6932 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6936 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6941 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6946 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6950 err = got_ref_write(*branch_ref, repo);
6954 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6955 worktree->base_commit_id);
6958 err = got_ref_write(base_commit_ref, repo);
6961 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6962 if (*base_commit_id == NULL) {
6963 err = got_error_from_errno("got_object_id_dup");
6967 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6968 worktree->base_commit_id);
6971 err = got_ref_write(*tmp_branch, repo);
6975 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6979 free(fileindex_path);
6980 free(tmp_branch_name);
6981 free(branch_ref_name);
6982 free(base_commit_ref_name);
6984 got_ref_close(wt_branch);
6987 got_ref_close(*branch_ref);
6991 got_ref_close(*tmp_branch);
6994 free(*base_commit_id);
6996 got_fileindex_free(*fileindex);
6999 lock_worktree(worktree, LOCK_SH);
7004 const struct got_error *
7005 got_worktree_histedit_postpone(struct got_worktree *worktree,
7006 struct got_fileindex *fileindex)
7009 got_fileindex_free(fileindex);
7010 return lock_worktree(worktree, LOCK_SH);
7013 const struct got_error *
7014 got_worktree_histedit_in_progress(int *in_progress,
7015 struct got_worktree *worktree)
7017 const struct got_error *err;
7018 char *tmp_branch_name = NULL;
7020 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7024 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7025 free(tmp_branch_name);
7029 const struct got_error *
7030 got_worktree_histedit_continue(struct got_object_id **commit_id,
7031 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7032 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7033 struct got_worktree *worktree, struct got_repository *repo)
7035 const struct got_error *err;
7036 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7037 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7038 struct got_reference *commit_ref = NULL;
7039 struct got_reference *base_commit_ref = NULL;
7040 char *fileindex_path = NULL;
7041 int have_staged_files = 0;
7045 *base_commit_id = NULL;
7048 err = lock_worktree(worktree, LOCK_EX);
7052 err = open_fileindex(fileindex, &fileindex_path, worktree);
7056 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7057 &have_staged_files);
7058 if (err && err->code != GOT_ERR_CANCELLED)
7060 if (have_staged_files) {
7061 err = got_error(GOT_ERR_STAGED_PATHS);
7065 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7069 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7073 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7077 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7082 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7086 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7089 err = got_ref_resolve(commit_id, repo, commit_ref);
7093 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7096 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7100 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7104 free(commit_ref_name);
7105 free(branch_ref_name);
7106 free(fileindex_path);
7108 got_ref_close(commit_ref);
7109 if (base_commit_ref)
7110 got_ref_close(base_commit_ref);
7114 free(*base_commit_id);
7115 *base_commit_id = NULL;
7117 got_ref_close(*tmp_branch);
7121 got_fileindex_free(*fileindex);
7124 lock_worktree(worktree, LOCK_EX);
7129 static const struct got_error *
7130 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7132 const struct got_error *err;
7133 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7134 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7136 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7139 err = delete_ref(tmp_branch_name, repo);
7143 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7147 err = delete_ref(base_commit_ref_name, repo);
7151 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7154 err = delete_ref(branch_ref_name, repo);
7158 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7161 err = delete_ref(commit_ref_name, repo);
7165 free(tmp_branch_name);
7166 free(base_commit_ref_name);
7167 free(branch_ref_name);
7168 free(commit_ref_name);
7172 const struct got_error *
7173 got_worktree_histedit_abort(struct got_worktree *worktree,
7174 struct got_fileindex *fileindex, struct got_repository *repo,
7175 struct got_reference *branch, struct got_object_id *base_commit_id,
7176 got_worktree_checkout_cb progress_cb, void *progress_arg)
7178 const struct got_error *err, *unlockerr, *sync_err;
7179 struct got_reference *resolved = NULL;
7180 char *fileindex_path = NULL;
7181 struct got_commit_object *commit = NULL;
7182 struct got_object_id *tree_id = NULL;
7183 struct revert_file_args rfa;
7185 err = lock_worktree(worktree, LOCK_EX);
7189 err = got_object_open_as_commit(&commit, repo,
7190 worktree->base_commit_id);
7194 err = got_ref_open(&resolved, repo,
7195 got_ref_get_symref_target(branch), 0);
7199 err = got_worktree_set_head_ref(worktree, resolved);
7203 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7207 err = got_object_id_by_path(&tree_id, repo, commit,
7208 worktree->path_prefix);
7212 err = delete_histedit_refs(worktree, repo);
7216 err = get_fileindex_path(&fileindex_path, worktree);
7220 rfa.worktree = worktree;
7221 rfa.fileindex = fileindex;
7222 rfa.progress_cb = progress_cb;
7223 rfa.progress_arg = progress_arg;
7224 rfa.patch_cb = NULL;
7225 rfa.patch_arg = NULL;
7227 rfa.unlink_added_files = 0;
7228 err = worktree_status(worktree, "", fileindex, repo,
7229 revert_file, &rfa, NULL, NULL, 1, 0);
7233 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7234 repo, progress_cb, progress_arg, NULL, NULL);
7236 sync_err = sync_fileindex(fileindex, fileindex_path);
7237 if (sync_err && err == NULL)
7240 got_ref_close(resolved);
7242 free(fileindex_path);
7244 unlockerr = lock_worktree(worktree, LOCK_SH);
7245 if (unlockerr && err == NULL)
7250 const struct got_error *
7251 got_worktree_histedit_complete(struct got_worktree *worktree,
7252 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7253 struct got_reference *edited_branch, struct got_repository *repo)
7255 const struct got_error *err, *unlockerr, *sync_err;
7256 struct got_object_id *new_head_commit_id = NULL;
7257 struct got_reference *resolved = NULL;
7258 char *fileindex_path = NULL;
7260 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7264 err = got_ref_open(&resolved, repo,
7265 got_ref_get_symref_target(edited_branch), 0);
7269 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7270 resolved, new_head_commit_id, repo);
7274 err = got_ref_change_ref(resolved, new_head_commit_id);
7278 err = got_ref_write(resolved, repo);
7282 err = got_worktree_set_head_ref(worktree, resolved);
7286 err = delete_histedit_refs(worktree, repo);
7290 err = get_fileindex_path(&fileindex_path, worktree);
7293 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7294 sync_err = sync_fileindex(fileindex, fileindex_path);
7295 if (sync_err && err == NULL)
7298 got_fileindex_free(fileindex);
7299 free(fileindex_path);
7300 free(new_head_commit_id);
7301 unlockerr = lock_worktree(worktree, LOCK_SH);
7302 if (unlockerr && err == NULL)
7307 const struct got_error *
7308 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7309 struct got_object_id *commit_id, struct got_repository *repo)
7311 const struct got_error *err;
7312 char *commit_ref_name;
7314 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7318 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7322 err = delete_ref(commit_ref_name, repo);
7324 free(commit_ref_name);
7328 const struct got_error *
7329 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7330 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7331 struct got_worktree *worktree, const char *refname,
7332 struct got_repository *repo)
7334 const struct got_error *err = NULL;
7335 char *fileindex_path = NULL;
7336 struct check_rebase_ok_arg ok_arg;
7340 *base_branch_ref = NULL;
7342 err = lock_worktree(worktree, LOCK_EX);
7346 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7347 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7348 "cannot integrate a branch into itself; "
7349 "update -b or different branch name required");
7353 err = open_fileindex(fileindex, &fileindex_path, worktree);
7357 /* Preconditions are the same as for rebase. */
7358 ok_arg.worktree = worktree;
7360 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7365 err = got_ref_open(branch_ref, repo, refname, 1);
7369 err = got_ref_open(base_branch_ref, repo,
7370 got_worktree_get_head_ref_name(worktree), 1);
7374 got_ref_close(*branch_ref);
7377 if (*base_branch_ref) {
7378 got_ref_close(*base_branch_ref);
7379 *base_branch_ref = NULL;
7382 got_fileindex_free(*fileindex);
7385 lock_worktree(worktree, LOCK_SH);
7390 const struct got_error *
7391 got_worktree_integrate_continue(struct got_worktree *worktree,
7392 struct got_fileindex *fileindex, struct got_repository *repo,
7393 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7394 got_worktree_checkout_cb progress_cb, void *progress_arg,
7395 got_cancel_cb cancel_cb, void *cancel_arg)
7397 const struct got_error *err = NULL, *sync_err, *unlockerr;
7398 char *fileindex_path = NULL;
7399 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7400 struct got_commit_object *commit = NULL;
7402 err = get_fileindex_path(&fileindex_path, worktree);
7406 err = got_ref_resolve(&commit_id, repo, branch_ref);
7410 err = got_object_open_as_commit(&commit, repo, commit_id);
7414 err = got_object_id_by_path(&tree_id, repo, commit,
7415 worktree->path_prefix);
7419 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7423 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7424 progress_cb, progress_arg, cancel_cb, cancel_arg);
7428 err = got_ref_change_ref(base_branch_ref, commit_id);
7432 err = got_ref_write(base_branch_ref, repo);
7436 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7438 sync_err = sync_fileindex(fileindex, fileindex_path);
7439 if (sync_err && err == NULL)
7443 unlockerr = got_ref_unlock(branch_ref);
7444 if (unlockerr && err == NULL)
7446 got_ref_close(branch_ref);
7448 unlockerr = got_ref_unlock(base_branch_ref);
7449 if (unlockerr && err == NULL)
7451 got_ref_close(base_branch_ref);
7453 got_fileindex_free(fileindex);
7454 free(fileindex_path);
7457 got_object_commit_close(commit);
7459 unlockerr = lock_worktree(worktree, LOCK_SH);
7460 if (unlockerr && err == NULL)
7465 const struct got_error *
7466 got_worktree_integrate_abort(struct got_worktree *worktree,
7467 struct got_fileindex *fileindex, struct got_repository *repo,
7468 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7470 const struct got_error *err = NULL, *unlockerr = NULL;
7472 got_fileindex_free(fileindex);
7474 err = lock_worktree(worktree, LOCK_SH);
7476 unlockerr = got_ref_unlock(branch_ref);
7477 if (unlockerr && err == NULL)
7479 got_ref_close(branch_ref);
7481 unlockerr = got_ref_unlock(base_branch_ref);
7482 if (unlockerr && err == NULL)
7484 got_ref_close(base_branch_ref);
7489 const struct got_error *
7490 got_worktree_merge_postpone(struct got_worktree *worktree,
7491 struct got_fileindex *fileindex)
7493 const struct got_error *err, *sync_err;
7494 char *fileindex_path = NULL;
7496 err = get_fileindex_path(&fileindex_path, worktree);
7500 sync_err = sync_fileindex(fileindex, fileindex_path);
7502 err = lock_worktree(worktree, LOCK_SH);
7503 if (sync_err && err == NULL)
7506 got_fileindex_free(fileindex);
7507 free(fileindex_path);
7511 static const struct got_error *
7512 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7514 const struct got_error *err;
7515 char *branch_refname = NULL, *commit_refname = NULL;
7517 err = get_merge_branch_ref_name(&branch_refname, worktree);
7520 err = delete_ref(branch_refname, repo);
7524 err = get_merge_commit_ref_name(&commit_refname, worktree);
7527 err = delete_ref(commit_refname, repo);
7532 free(branch_refname);
7533 free(commit_refname);
7537 struct merge_commit_msg_arg {
7538 struct got_worktree *worktree;
7539 const char *branch_name;
7542 static const struct got_error *
7543 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7546 struct merge_commit_msg_arg *a = arg;
7548 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7549 got_worktree_get_head_ref_name(a->worktree)) == -1)
7550 return got_error_from_errno("asprintf");
7556 const struct got_error *
7557 got_worktree_merge_branch(struct got_worktree *worktree,
7558 struct got_fileindex *fileindex,
7559 struct got_object_id *yca_commit_id,
7560 struct got_object_id *branch_tip,
7561 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7562 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7564 const struct got_error *err;
7565 char *fileindex_path = NULL;
7567 err = get_fileindex_path(&fileindex_path, worktree);
7571 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7576 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7577 branch_tip, repo, progress_cb, progress_arg,
7578 cancel_cb, cancel_arg);
7580 free(fileindex_path);
7584 const struct got_error *
7585 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7586 struct got_worktree *worktree, struct got_fileindex *fileindex,
7587 const char *author, const char *committer, int allow_bad_symlinks,
7588 struct got_object_id *branch_tip, const char *branch_name,
7589 struct got_repository *repo,
7590 got_worktree_status_cb status_cb, void *status_arg)
7593 const struct got_error *err = NULL, *sync_err;
7594 struct got_pathlist_head commitable_paths;
7595 struct collect_commitables_arg cc_arg;
7596 struct got_pathlist_entry *pe;
7597 struct got_reference *head_ref = NULL;
7598 struct got_object_id *head_commit_id = NULL;
7599 int have_staged_files = 0;
7600 struct merge_commit_msg_arg mcm_arg;
7601 char *fileindex_path = NULL;
7603 *new_commit_id = NULL;
7605 TAILQ_INIT(&commitable_paths);
7607 err = get_fileindex_path(&fileindex_path, worktree);
7611 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7615 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7619 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7620 &have_staged_files);
7621 if (err && err->code != GOT_ERR_CANCELLED)
7623 if (have_staged_files) {
7624 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7628 cc_arg.commitable_paths = &commitable_paths;
7629 cc_arg.worktree = worktree;
7630 cc_arg.fileindex = fileindex;
7632 cc_arg.have_staged_files = have_staged_files;
7633 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7634 err = worktree_status(worktree, "", fileindex, repo,
7635 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7639 if (TAILQ_EMPTY(&commitable_paths)) {
7640 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7641 "merge of %s cannot proceed", branch_name);
7645 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7646 struct got_commitable *ct = pe->data;
7647 const char *ct_path = ct->in_repo_path;
7649 while (ct_path[0] == '/')
7651 err = check_out_of_date(ct_path, ct->status,
7652 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7653 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7659 mcm_arg.worktree = worktree;
7660 mcm_arg.branch_name = branch_name;
7661 err = commit_worktree(new_commit_id, &commitable_paths,
7662 head_commit_id, branch_tip, worktree, author, committer,
7663 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7667 err = update_fileindex_after_commit(worktree, &commitable_paths,
7668 *new_commit_id, fileindex, have_staged_files);
7669 sync_err = sync_fileindex(fileindex, fileindex_path);
7670 if (sync_err && err == NULL)
7673 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7674 struct got_commitable *ct = pe->data;
7675 free_commitable(ct);
7677 got_pathlist_free(&commitable_paths);
7678 free(fileindex_path);
7682 const struct got_error *
7683 got_worktree_merge_complete(struct got_worktree *worktree,
7684 struct got_fileindex *fileindex, struct got_repository *repo)
7686 const struct got_error *err, *unlockerr, *sync_err;
7687 char *fileindex_path = NULL;
7689 err = delete_merge_refs(worktree, repo);
7693 err = get_fileindex_path(&fileindex_path, worktree);
7696 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7697 sync_err = sync_fileindex(fileindex, fileindex_path);
7698 if (sync_err && err == NULL)
7701 got_fileindex_free(fileindex);
7702 free(fileindex_path);
7703 unlockerr = lock_worktree(worktree, LOCK_SH);
7704 if (unlockerr && err == NULL)
7709 const struct got_error *
7710 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7711 struct got_repository *repo)
7713 const struct got_error *err;
7714 char *branch_refname = NULL;
7715 struct got_reference *branch_ref = NULL;
7719 err = get_merge_branch_ref_name(&branch_refname, worktree);
7722 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7723 free(branch_refname);
7725 if (err->code != GOT_ERR_NOT_REF)
7733 const struct got_error *got_worktree_merge_prepare(
7734 struct got_fileindex **fileindex, struct got_worktree *worktree,
7735 struct got_reference *branch, struct got_repository *repo)
7737 const struct got_error *err = NULL;
7738 char *fileindex_path = NULL;
7739 char *branch_refname = NULL, *commit_refname = NULL;
7740 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7741 struct got_reference *commit_ref = NULL;
7742 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7743 struct check_rebase_ok_arg ok_arg;
7747 err = lock_worktree(worktree, LOCK_EX);
7751 err = open_fileindex(fileindex, &fileindex_path, worktree);
7755 /* Preconditions are the same as for rebase. */
7756 ok_arg.worktree = worktree;
7758 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7763 err = get_merge_branch_ref_name(&branch_refname, worktree);
7767 err = get_merge_commit_ref_name(&commit_refname, worktree);
7771 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7776 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7780 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7781 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7785 err = got_ref_resolve(&branch_tip, repo, branch);
7789 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7792 err = got_ref_write(branch_ref, repo);
7796 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7799 err = got_ref_write(commit_ref, repo);
7804 free(branch_refname);
7805 free(commit_refname);
7806 free(fileindex_path);
7808 got_ref_close(branch_ref);
7810 got_ref_close(commit_ref);
7812 got_ref_close(wt_branch);
7813 free(wt_branch_tip);
7816 got_fileindex_free(*fileindex);
7819 lock_worktree(worktree, LOCK_SH);
7824 const struct got_error *
7825 got_worktree_merge_continue(char **branch_name,
7826 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7827 struct got_worktree *worktree, struct got_repository *repo)
7829 const struct got_error *err;
7830 char *commit_refname = NULL, *branch_refname = NULL;
7831 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7832 char *fileindex_path = NULL;
7833 int have_staged_files = 0;
7835 *branch_name = NULL;
7839 err = lock_worktree(worktree, LOCK_EX);
7843 err = open_fileindex(fileindex, &fileindex_path, worktree);
7847 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7848 &have_staged_files);
7849 if (err && err->code != GOT_ERR_CANCELLED)
7851 if (have_staged_files) {
7852 err = got_error(GOT_ERR_STAGED_PATHS);
7856 err = get_merge_branch_ref_name(&branch_refname, worktree);
7860 err = get_merge_commit_ref_name(&commit_refname, worktree);
7864 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7868 if (!got_ref_is_symbolic(branch_ref)) {
7869 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7870 "%s is not a symbolic reference",
7871 got_ref_get_name(branch_ref));
7874 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7875 if (*branch_name == NULL) {
7876 err = got_error_from_errno("strdup");
7880 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7884 err = got_ref_resolve(branch_tip, repo, commit_ref);
7888 free(commit_refname);
7889 free(branch_refname);
7890 free(fileindex_path);
7892 got_ref_close(commit_ref);
7894 got_ref_close(branch_ref);
7898 *branch_name = NULL;
7903 got_fileindex_free(*fileindex);
7906 lock_worktree(worktree, LOCK_SH);
7911 const struct got_error *
7912 got_worktree_merge_abort(struct got_worktree *worktree,
7913 struct got_fileindex *fileindex, struct got_repository *repo,
7914 got_worktree_checkout_cb progress_cb, void *progress_arg)
7916 const struct got_error *err, *unlockerr, *sync_err;
7917 struct got_object_id *commit_id = NULL;
7918 struct got_commit_object *commit = NULL;
7919 char *fileindex_path = NULL;
7920 struct revert_file_args rfa;
7921 struct got_object_id *tree_id = NULL;
7923 err = got_object_open_as_commit(&commit, repo,
7924 worktree->base_commit_id);
7928 err = got_object_id_by_path(&tree_id, repo, commit,
7929 worktree->path_prefix);
7933 err = delete_merge_refs(worktree, repo);
7937 err = get_fileindex_path(&fileindex_path, worktree);
7941 rfa.worktree = worktree;
7942 rfa.fileindex = fileindex;
7943 rfa.progress_cb = progress_cb;
7944 rfa.progress_arg = progress_arg;
7945 rfa.patch_cb = NULL;
7946 rfa.patch_arg = NULL;
7948 rfa.unlink_added_files = 1;
7949 err = worktree_status(worktree, "", fileindex, repo,
7950 revert_file, &rfa, NULL, NULL, 1, 0);
7954 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7955 repo, progress_cb, progress_arg, NULL, NULL);
7957 sync_err = sync_fileindex(fileindex, fileindex_path);
7958 if (sync_err && err == NULL)
7964 got_object_commit_close(commit);
7966 got_fileindex_free(fileindex);
7967 free(fileindex_path);
7969 unlockerr = lock_worktree(worktree, LOCK_SH);
7970 if (unlockerr && err == NULL)
7975 struct check_stage_ok_arg {
7976 struct got_object_id *head_commit_id;
7977 struct got_worktree *worktree;
7978 struct got_fileindex *fileindex;
7979 struct got_repository *repo;
7983 static const struct got_error *
7984 check_stage_ok(void *arg, unsigned char status,
7985 unsigned char staged_status, const char *relpath,
7986 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7987 struct got_object_id *commit_id, int dirfd, const char *de_name)
7989 struct check_stage_ok_arg *a = arg;
7990 const struct got_error *err = NULL;
7991 struct got_fileindex_entry *ie;
7992 struct got_object_id base_commit_id;
7993 struct got_object_id *base_commit_idp = NULL;
7994 char *in_repo_path = NULL, *p;
7996 if (status == GOT_STATUS_UNVERSIONED ||
7997 status == GOT_STATUS_NO_CHANGE)
7999 if (status == GOT_STATUS_NONEXISTENT)
8000 return got_error_set_errno(ENOENT, relpath);
8002 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8004 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8006 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8007 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8009 return got_error_from_errno("asprintf");
8011 if (got_fileindex_entry_has_commit(ie)) {
8012 memcpy(base_commit_id.sha1, ie->commit_sha1,
8013 SHA1_DIGEST_LENGTH);
8014 base_commit_idp = &base_commit_id;
8017 if (status == GOT_STATUS_CONFLICT) {
8018 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8020 } else if (status != GOT_STATUS_ADD &&
8021 status != GOT_STATUS_MODIFY &&
8022 status != GOT_STATUS_DELETE) {
8023 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8027 a->have_changes = 1;
8032 err = check_out_of_date(p, status, staged_status,
8033 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8034 GOT_ERR_STAGE_OUT_OF_DATE);
8040 struct stage_path_arg {
8041 struct got_worktree *worktree;
8042 struct got_fileindex *fileindex;
8043 struct got_repository *repo;
8044 got_worktree_status_cb status_cb;
8046 got_worktree_patch_cb patch_cb;
8048 int staged_something;
8049 int allow_bad_symlinks;
8052 static const struct got_error *
8053 stage_path(void *arg, unsigned char status,
8054 unsigned char staged_status, const char *relpath,
8055 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8056 struct got_object_id *commit_id, int dirfd, const char *de_name)
8058 struct stage_path_arg *a = arg;
8059 const struct got_error *err = NULL;
8060 struct got_fileindex_entry *ie;
8061 char *ondisk_path = NULL, *path_content = NULL;
8063 struct got_object_id *new_staged_blob_id = NULL;
8066 if (status == GOT_STATUS_UNVERSIONED)
8069 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8071 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8073 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8075 return got_error_from_errno("asprintf");
8078 case GOT_STATUS_ADD:
8079 case GOT_STATUS_MODIFY:
8080 /* XXX could sb.st_mode be passed in by our caller? */
8081 if (lstat(ondisk_path, &sb) == -1) {
8082 err = got_error_from_errno2("lstat", ondisk_path);
8086 if (status == GOT_STATUS_ADD) {
8087 int choice = GOT_PATCH_CHOICE_NONE;
8088 err = (*a->patch_cb)(&choice, a->patch_arg,
8089 status, ie->path, NULL, 1, 1);
8092 if (choice != GOT_PATCH_CHOICE_YES)
8095 err = create_patched_content(&path_content, 0,
8096 staged_blob_id ? staged_blob_id : blob_id,
8097 ondisk_path, dirfd, de_name, ie->path,
8098 a->repo, a->patch_cb, a->patch_arg);
8099 if (err || path_content == NULL)
8103 err = got_object_blob_create(&new_staged_blob_id,
8104 path_content ? path_content : ondisk_path, a->repo);
8107 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8108 SHA1_DIGEST_LENGTH);
8109 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8110 stage = GOT_FILEIDX_STAGE_ADD;
8112 stage = GOT_FILEIDX_STAGE_MODIFY;
8113 got_fileindex_entry_stage_set(ie, stage);
8114 if (S_ISLNK(sb.st_mode)) {
8115 int is_bad_symlink = 0;
8116 if (!a->allow_bad_symlinks) {
8117 char target_path[PATH_MAX];
8119 target_len = readlink(ondisk_path, target_path,
8120 sizeof(target_path));
8121 if (target_len == -1) {
8122 err = got_error_from_errno2("readlink",
8126 err = is_bad_symlink_target(&is_bad_symlink,
8127 target_path, target_len, ondisk_path,
8128 a->worktree->root_path);
8131 if (is_bad_symlink) {
8132 err = got_error_path(ondisk_path,
8133 GOT_ERR_BAD_SYMLINK);
8138 got_fileindex_entry_staged_filetype_set(ie,
8139 GOT_FILEIDX_MODE_BAD_SYMLINK);
8141 got_fileindex_entry_staged_filetype_set(ie,
8142 GOT_FILEIDX_MODE_SYMLINK);
8144 got_fileindex_entry_staged_filetype_set(ie,
8145 GOT_FILEIDX_MODE_REGULAR_FILE);
8147 a->staged_something = 1;
8148 if (a->status_cb == NULL)
8150 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8151 get_staged_status(ie), relpath, blob_id,
8152 new_staged_blob_id, NULL, dirfd, de_name);
8156 * When staging the reverse of the staged diff,
8157 * implicitly unstage the file.
8159 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8160 sizeof(ie->blob_sha1)) == 0) {
8161 got_fileindex_entry_stage_set(ie,
8162 GOT_FILEIDX_STAGE_NONE);
8165 case GOT_STATUS_DELETE:
8166 if (staged_status == GOT_STATUS_DELETE)
8169 int choice = GOT_PATCH_CHOICE_NONE;
8170 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8171 ie->path, NULL, 1, 1);
8174 if (choice == GOT_PATCH_CHOICE_NO)
8176 if (choice != GOT_PATCH_CHOICE_YES) {
8177 err = got_error(GOT_ERR_PATCH_CHOICE);
8181 stage = GOT_FILEIDX_STAGE_DELETE;
8182 got_fileindex_entry_stage_set(ie, stage);
8183 a->staged_something = 1;
8184 if (a->status_cb == NULL)
8186 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8187 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8190 case GOT_STATUS_NO_CHANGE:
8192 case GOT_STATUS_CONFLICT:
8193 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8195 case GOT_STATUS_NONEXISTENT:
8196 err = got_error_set_errno(ENOENT, relpath);
8199 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8203 if (path_content && unlink(path_content) == -1 && err == NULL)
8204 err = got_error_from_errno2("unlink", path_content);
8207 free(new_staged_blob_id);
8211 const struct got_error *
8212 got_worktree_stage(struct got_worktree *worktree,
8213 struct got_pathlist_head *paths,
8214 got_worktree_status_cb status_cb, void *status_arg,
8215 got_worktree_patch_cb patch_cb, void *patch_arg,
8216 int allow_bad_symlinks, struct got_repository *repo)
8218 const struct got_error *err = NULL, *sync_err, *unlockerr;
8219 struct got_pathlist_entry *pe;
8220 struct got_fileindex *fileindex = NULL;
8221 char *fileindex_path = NULL;
8222 struct got_reference *head_ref = NULL;
8223 struct got_object_id *head_commit_id = NULL;
8224 struct check_stage_ok_arg oka;
8225 struct stage_path_arg spa;
8227 err = lock_worktree(worktree, LOCK_EX);
8231 err = got_ref_open(&head_ref, repo,
8232 got_worktree_get_head_ref_name(worktree), 0);
8235 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8238 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8242 /* Check pre-conditions before staging anything. */
8243 oka.head_commit_id = head_commit_id;
8244 oka.worktree = worktree;
8245 oka.fileindex = fileindex;
8247 oka.have_changes = 0;
8248 TAILQ_FOREACH(pe, paths, entry) {
8249 err = worktree_status(worktree, pe->path, fileindex, repo,
8250 check_stage_ok, &oka, NULL, NULL, 1, 0);
8254 if (!oka.have_changes) {
8255 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8259 spa.worktree = worktree;
8260 spa.fileindex = fileindex;
8262 spa.patch_cb = patch_cb;
8263 spa.patch_arg = patch_arg;
8264 spa.status_cb = status_cb;
8265 spa.status_arg = status_arg;
8266 spa.staged_something = 0;
8267 spa.allow_bad_symlinks = allow_bad_symlinks;
8268 TAILQ_FOREACH(pe, paths, entry) {
8269 err = worktree_status(worktree, pe->path, fileindex, repo,
8270 stage_path, &spa, NULL, NULL, 1, 0);
8274 if (!spa.staged_something) {
8275 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8279 sync_err = sync_fileindex(fileindex, fileindex_path);
8280 if (sync_err && err == NULL)
8284 got_ref_close(head_ref);
8285 free(head_commit_id);
8286 free(fileindex_path);
8288 got_fileindex_free(fileindex);
8289 unlockerr = lock_worktree(worktree, LOCK_SH);
8290 if (unlockerr && err == NULL)
8295 struct unstage_path_arg {
8296 struct got_worktree *worktree;
8297 struct got_fileindex *fileindex;
8298 struct got_repository *repo;
8299 got_worktree_checkout_cb progress_cb;
8301 got_worktree_patch_cb patch_cb;
8305 static const struct got_error *
8306 create_unstaged_content(char **path_unstaged_content,
8307 char **path_new_staged_content, struct got_object_id *blob_id,
8308 struct got_object_id *staged_blob_id, const char *relpath,
8309 struct got_repository *repo,
8310 got_worktree_patch_cb patch_cb, void *patch_arg)
8312 const struct got_error *err, *free_err;
8313 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8314 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8315 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8316 struct got_diffreg_result *diffreg_result = NULL;
8317 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8318 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8319 int fd1 = -1, fd2 = -1;
8321 *path_unstaged_content = NULL;
8322 *path_new_staged_content = NULL;
8324 err = got_object_id_str(&label1, blob_id);
8328 fd1 = got_opentempfd();
8330 err = got_error_from_errno("got_opentempfd");
8333 fd2 = got_opentempfd();
8335 err = got_error_from_errno("got_opentempfd");
8339 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8343 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8347 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8351 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8356 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8360 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8364 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8365 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8369 err = got_opentemp_named(path_unstaged_content, &outfile,
8370 "got-unstaged-content");
8373 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8374 "got-new-staged-content");
8378 if (fseek(f1, 0L, SEEK_SET) == -1) {
8379 err = got_ferror(f1, GOT_ERR_IO);
8382 if (fseek(f2, 0L, SEEK_SET) == -1) {
8383 err = got_ferror(f2, GOT_ERR_IO);
8386 /* Count the number of actual changes in the diff result. */
8387 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8388 struct diff_chunk_context cc = {};
8389 diff_chunk_context_load_change(&cc, &nchunks_used,
8390 diffreg_result->result, n, 0);
8393 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8395 err = apply_or_reject_change(&choice, &nchunks_used,
8396 diffreg_result->result, n, relpath, f1, f2,
8397 &line_cur1, &line_cur2,
8398 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8401 if (choice == GOT_PATCH_CHOICE_YES)
8404 have_rejected_content = 1;
8405 if (choice == GOT_PATCH_CHOICE_QUIT)
8408 if (have_content || have_rejected_content)
8409 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8410 outfile, rejectfile);
8413 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8414 err = got_error_from_errno("close");
8416 got_object_blob_close(blob);
8417 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8418 err = got_error_from_errno("close");
8420 got_object_blob_close(staged_blob);
8421 free_err = got_diffreg_result_free(diffreg_result);
8422 if (free_err && err == NULL)
8424 if (f1 && fclose(f1) == EOF && err == NULL)
8425 err = got_error_from_errno2("fclose", path1);
8426 if (f2 && fclose(f2) == EOF && err == NULL)
8427 err = got_error_from_errno2("fclose", path2);
8428 if (outfile && fclose(outfile) == EOF && err == NULL)
8429 err = got_error_from_errno2("fclose", *path_unstaged_content);
8430 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8431 err = got_error_from_errno2("fclose", *path_new_staged_content);
8432 if (path1 && unlink(path1) == -1 && err == NULL)
8433 err = got_error_from_errno2("unlink", path1);
8434 if (path2 && unlink(path2) == -1 && err == NULL)
8435 err = got_error_from_errno2("unlink", path2);
8436 if (err || !have_content) {
8437 if (*path_unstaged_content &&
8438 unlink(*path_unstaged_content) == -1 && err == NULL)
8439 err = got_error_from_errno2("unlink",
8440 *path_unstaged_content);
8441 free(*path_unstaged_content);
8442 *path_unstaged_content = NULL;
8444 if (err || !have_content || !have_rejected_content) {
8445 if (*path_new_staged_content &&
8446 unlink(*path_new_staged_content) == -1 && err == NULL)
8447 err = got_error_from_errno2("unlink",
8448 *path_new_staged_content);
8449 free(*path_new_staged_content);
8450 *path_new_staged_content = NULL;
8457 static const struct got_error *
8458 unstage_hunks(struct got_object_id *staged_blob_id,
8459 struct got_blob_object *blob_base,
8460 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8461 const char *ondisk_path, const char *label_orig,
8462 struct got_worktree *worktree, struct got_repository *repo,
8463 got_worktree_patch_cb patch_cb, void *patch_arg,
8464 got_worktree_checkout_cb progress_cb, void *progress_arg)
8466 const struct got_error *err = NULL;
8467 char *path_unstaged_content = NULL;
8468 char *path_new_staged_content = NULL;
8469 char *parent = NULL, *base_path = NULL;
8470 char *blob_base_path = NULL;
8471 struct got_object_id *new_staged_blob_id = NULL;
8472 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8475 err = create_unstaged_content(&path_unstaged_content,
8476 &path_new_staged_content, blob_id, staged_blob_id,
8477 ie->path, repo, patch_cb, patch_arg);
8481 if (path_unstaged_content == NULL)
8484 if (path_new_staged_content) {
8485 err = got_object_blob_create(&new_staged_blob_id,
8486 path_new_staged_content, repo);
8491 f = fopen(path_unstaged_content, "re");
8493 err = got_error_from_errno2("fopen",
8494 path_unstaged_content);
8497 if (fstat(fileno(f), &sb) == -1) {
8498 err = got_error_from_errno2("fstat", path_unstaged_content);
8501 if (got_fileindex_entry_staged_filetype_get(ie) ==
8502 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8503 char link_target[PATH_MAX];
8505 r = fread(link_target, 1, sizeof(link_target), f);
8506 if (r == 0 && ferror(f)) {
8507 err = got_error_from_errno("fread");
8510 if (r >= sizeof(link_target)) { /* should not happen */
8511 err = got_error(GOT_ERR_NO_SPACE);
8514 link_target[r] = '\0';
8515 err = merge_symlink(worktree, blob_base,
8516 ondisk_path, ie->path, label_orig, link_target,
8517 worktree->base_commit_id, repo, progress_cb,
8520 int local_changes_subsumed;
8522 err = got_path_dirname(&parent, ondisk_path);
8526 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8528 err = got_error_from_errno("asprintf");
8533 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8536 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8542 * In order the run a 3-way merge with a symlink we copy the symlink's
8543 * target path into a temporary file and use that file with diff3.
8545 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8546 err = dump_symlink_target_path_to_file(&f_deriv2,
8552 fd = open(ondisk_path,
8553 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8555 err = got_error_from_errno2("open", ondisk_path);
8558 f_deriv2 = fdopen(fd, "r");
8559 if (f_deriv2 == NULL) {
8560 err = got_error_from_errno2("fdopen", ondisk_path);
8566 err = merge_file(&local_changes_subsumed, worktree,
8567 f_base, f, f_deriv2, ondisk_path, ie->path,
8568 got_fileindex_perms_to_st(ie),
8569 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8570 repo, progress_cb, progress_arg);
8575 if (new_staged_blob_id) {
8576 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8577 SHA1_DIGEST_LENGTH);
8579 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8580 got_fileindex_entry_staged_filetype_set(ie, 0);
8583 free(new_staged_blob_id);
8584 if (path_unstaged_content &&
8585 unlink(path_unstaged_content) == -1 && err == NULL)
8586 err = got_error_from_errno2("unlink", path_unstaged_content);
8587 if (path_new_staged_content &&
8588 unlink(path_new_staged_content) == -1 && err == NULL)
8589 err = got_error_from_errno2("unlink", path_new_staged_content);
8590 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8591 err = got_error_from_errno2("unlink", blob_base_path);
8592 if (f_base && fclose(f_base) == EOF && err == NULL)
8593 err = got_error_from_errno2("fclose", path_unstaged_content);
8594 if (f && fclose(f) == EOF && err == NULL)
8595 err = got_error_from_errno2("fclose", path_unstaged_content);
8596 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8597 err = got_error_from_errno2("fclose", ondisk_path);
8598 free(path_unstaged_content);
8599 free(path_new_staged_content);
8600 free(blob_base_path);
8606 static const struct got_error *
8607 unstage_path(void *arg, unsigned char status,
8608 unsigned char staged_status, const char *relpath,
8609 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8610 struct got_object_id *commit_id, int dirfd, const char *de_name)
8612 const struct got_error *err = NULL;
8613 struct unstage_path_arg *a = arg;
8614 struct got_fileindex_entry *ie;
8615 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8616 char *ondisk_path = NULL;
8617 char *id_str = NULL, *label_orig = NULL;
8618 int local_changes_subsumed;
8620 int fd1 = -1, fd2 = -1;
8622 if (staged_status != GOT_STATUS_ADD &&
8623 staged_status != GOT_STATUS_MODIFY &&
8624 staged_status != GOT_STATUS_DELETE)
8627 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8629 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8631 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8633 return got_error_from_errno("asprintf");
8635 err = got_object_id_str(&id_str,
8636 commit_id ? commit_id : a->worktree->base_commit_id);
8639 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8641 err = got_error_from_errno("asprintf");
8645 fd1 = got_opentempfd();
8647 err = got_error_from_errno("got_opentempfd");
8650 fd2 = got_opentempfd();
8652 err = got_error_from_errno("got_opentempfd");
8656 switch (staged_status) {
8657 case GOT_STATUS_MODIFY:
8658 err = got_object_open_as_blob(&blob_base, a->repo,
8659 blob_id, 8192, fd1);
8663 case GOT_STATUS_ADD:
8665 if (staged_status == GOT_STATUS_ADD) {
8666 int choice = GOT_PATCH_CHOICE_NONE;
8667 err = (*a->patch_cb)(&choice, a->patch_arg,
8668 staged_status, ie->path, NULL, 1, 1);
8671 if (choice != GOT_PATCH_CHOICE_YES)
8674 err = unstage_hunks(staged_blob_id,
8675 blob_base, blob_id, ie, ondisk_path,
8676 label_orig, a->worktree, a->repo,
8677 a->patch_cb, a->patch_arg,
8678 a->progress_cb, a->progress_arg);
8679 break; /* Done with this file. */
8682 err = got_object_open_as_blob(&blob_staged, a->repo,
8683 staged_blob_id, 8192, fd2);
8686 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8687 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8688 case GOT_FILEIDX_MODE_REGULAR_FILE:
8689 err = merge_blob(&local_changes_subsumed, a->worktree,
8690 blob_base, ondisk_path, relpath,
8691 got_fileindex_perms_to_st(ie), label_orig,
8692 blob_staged, commit_id ? commit_id :
8693 a->worktree->base_commit_id, a->repo,
8694 a->progress_cb, a->progress_arg);
8696 case GOT_FILEIDX_MODE_SYMLINK:
8697 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8698 char *staged_target;
8699 err = got_object_blob_read_to_str(
8700 &staged_target, blob_staged);
8703 err = merge_symlink(a->worktree, blob_base,
8704 ondisk_path, relpath, label_orig,
8705 staged_target, commit_id ? commit_id :
8706 a->worktree->base_commit_id,
8707 a->repo, a->progress_cb, a->progress_arg);
8708 free(staged_target);
8710 err = merge_blob(&local_changes_subsumed,
8711 a->worktree, blob_base, ondisk_path,
8712 relpath, got_fileindex_perms_to_st(ie),
8713 label_orig, blob_staged,
8714 commit_id ? commit_id :
8715 a->worktree->base_commit_id, a->repo,
8716 a->progress_cb, a->progress_arg);
8720 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8724 got_fileindex_entry_stage_set(ie,
8725 GOT_FILEIDX_STAGE_NONE);
8726 got_fileindex_entry_staged_filetype_set(ie, 0);
8729 case GOT_STATUS_DELETE:
8731 int choice = GOT_PATCH_CHOICE_NONE;
8732 err = (*a->patch_cb)(&choice, a->patch_arg,
8733 staged_status, ie->path, NULL, 1, 1);
8736 if (choice == GOT_PATCH_CHOICE_NO)
8738 if (choice != GOT_PATCH_CHOICE_YES) {
8739 err = got_error(GOT_ERR_PATCH_CHOICE);
8743 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8744 got_fileindex_entry_staged_filetype_set(ie, 0);
8745 err = get_file_status(&status, &sb, ie, ondisk_path,
8746 dirfd, de_name, a->repo);
8749 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8754 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8755 err = got_error_from_errno("close");
8757 got_object_blob_close(blob_base);
8758 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8759 err = got_error_from_errno("close");
8761 got_object_blob_close(blob_staged);
8767 const struct got_error *
8768 got_worktree_unstage(struct got_worktree *worktree,
8769 struct got_pathlist_head *paths,
8770 got_worktree_checkout_cb progress_cb, void *progress_arg,
8771 got_worktree_patch_cb patch_cb, void *patch_arg,
8772 struct got_repository *repo)
8774 const struct got_error *err = NULL, *sync_err, *unlockerr;
8775 struct got_pathlist_entry *pe;
8776 struct got_fileindex *fileindex = NULL;
8777 char *fileindex_path = NULL;
8778 struct unstage_path_arg upa;
8780 err = lock_worktree(worktree, LOCK_EX);
8784 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8788 upa.worktree = worktree;
8789 upa.fileindex = fileindex;
8791 upa.progress_cb = progress_cb;
8792 upa.progress_arg = progress_arg;
8793 upa.patch_cb = patch_cb;
8794 upa.patch_arg = patch_arg;
8795 TAILQ_FOREACH(pe, paths, entry) {
8796 err = worktree_status(worktree, pe->path, fileindex, repo,
8797 unstage_path, &upa, NULL, NULL, 1, 0);
8802 sync_err = sync_fileindex(fileindex, fileindex_path);
8803 if (sync_err && err == NULL)
8806 free(fileindex_path);
8808 got_fileindex_free(fileindex);
8809 unlockerr = lock_worktree(worktree, LOCK_SH);
8810 if (unlockerr && err == NULL)
8815 struct report_file_info_arg {
8816 struct got_worktree *worktree;
8817 got_worktree_path_info_cb info_cb;
8819 struct got_pathlist_head *paths;
8820 got_cancel_cb cancel_cb;
8824 static const struct got_error *
8825 report_file_info(void *arg, struct got_fileindex_entry *ie)
8827 struct report_file_info_arg *a = arg;
8828 struct got_pathlist_entry *pe;
8829 struct got_object_id blob_id, staged_blob_id, commit_id;
8830 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8831 struct got_object_id *commit_idp = NULL;
8834 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8835 return got_error(GOT_ERR_CANCELLED);
8837 TAILQ_FOREACH(pe, a->paths, entry) {
8838 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8839 got_path_is_child(ie->path, pe->path, pe->path_len))
8842 if (pe == NULL) /* not found */
8845 if (got_fileindex_entry_has_blob(ie)) {
8846 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8847 blob_idp = &blob_id;
8849 stage = got_fileindex_entry_stage_get(ie);
8850 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8851 stage == GOT_FILEIDX_STAGE_ADD) {
8852 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8853 SHA1_DIGEST_LENGTH);
8854 staged_blob_idp = &staged_blob_id;
8857 if (got_fileindex_entry_has_commit(ie)) {
8858 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8859 commit_idp = &commit_id;
8862 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8863 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8866 const struct got_error *
8867 got_worktree_path_info(struct got_worktree *worktree,
8868 struct got_pathlist_head *paths,
8869 got_worktree_path_info_cb info_cb, void *info_arg,
8870 got_cancel_cb cancel_cb, void *cancel_arg)
8873 const struct got_error *err = NULL, *unlockerr;
8874 struct got_fileindex *fileindex = NULL;
8875 char *fileindex_path = NULL;
8876 struct report_file_info_arg arg;
8878 err = lock_worktree(worktree, LOCK_SH);
8882 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8886 arg.worktree = worktree;
8887 arg.info_cb = info_cb;
8888 arg.info_arg = info_arg;
8890 arg.cancel_cb = cancel_cb;
8891 arg.cancel_arg = cancel_arg;
8892 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8895 free(fileindex_path);
8897 got_fileindex_free(fileindex);
8898 unlockerr = lock_worktree(worktree, LOCK_UN);
8899 if (unlockerr && err == NULL)
8904 static const struct got_error *
8905 patch_check_path(const char *p, char **path, unsigned char *status,
8906 unsigned char *staged_status, struct got_fileindex *fileindex,
8907 struct got_worktree *worktree, struct got_repository *repo)
8909 const struct got_error *err;
8910 struct got_fileindex_entry *ie;
8912 char *ondisk_path = NULL;
8914 err = got_worktree_resolve_path(path, worktree, p);
8918 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8919 *path[0] ? "/" : "", *path) == -1)
8920 return got_error_from_errno("asprintf");
8922 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8924 *staged_status = get_staged_status(ie);
8925 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8930 *staged_status = GOT_STATUS_NO_CHANGE;
8931 *status = GOT_STATUS_UNVERSIONED;
8932 if (lstat(ondisk_path, &sb) == -1) {
8933 if (errno != ENOENT) {
8934 err = got_error_from_errno2("lstat",
8938 *status = GOT_STATUS_NONEXISTENT;
8947 static const struct got_error *
8948 patch_can_rm(const char *path, unsigned char status,
8949 unsigned char staged_status)
8951 if (status == GOT_STATUS_NONEXISTENT)
8952 return got_error_set_errno(ENOENT, path);
8953 if (status != GOT_STATUS_NO_CHANGE &&
8954 status != GOT_STATUS_ADD &&
8955 status != GOT_STATUS_MODIFY &&
8956 status != GOT_STATUS_MODE_CHANGE)
8957 return got_error_path(path, GOT_ERR_FILE_STATUS);
8958 if (staged_status == GOT_STATUS_DELETE)
8959 return got_error_path(path, GOT_ERR_FILE_STATUS);
8963 static const struct got_error *
8964 patch_can_add(const char *path, unsigned char status)
8966 if (status != GOT_STATUS_NONEXISTENT)
8967 return got_error_path(path, GOT_ERR_FILE_STATUS);
8971 static const struct got_error *
8972 patch_can_edit(const char *path, unsigned char status,
8973 unsigned char staged_status)
8975 if (status == GOT_STATUS_NONEXISTENT)
8976 return got_error_set_errno(ENOENT, path);
8977 if (status != GOT_STATUS_NO_CHANGE &&
8978 status != GOT_STATUS_ADD &&
8979 status != GOT_STATUS_MODIFY)
8980 return got_error_path(path, GOT_ERR_FILE_STATUS);
8981 if (staged_status == GOT_STATUS_DELETE)
8982 return got_error_path(path, GOT_ERR_FILE_STATUS);
8986 const struct got_error *
8987 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8988 char **fileindex_path, struct got_worktree *worktree)
8990 return open_fileindex(fileindex, fileindex_path, worktree);
8993 const struct got_error *
8994 got_worktree_patch_check_path(const char *old, const char *new,
8995 char **oldpath, char **newpath, struct got_worktree *worktree,
8996 struct got_repository *repo, struct got_fileindex *fileindex)
8998 const struct got_error *err = NULL;
8999 int file_renamed = 0;
9000 unsigned char status_old, staged_status_old;
9001 unsigned char status_new, staged_status_new;
9006 err = patch_check_path(old != NULL ? old : new, oldpath,
9007 &status_old, &staged_status_old, fileindex, worktree, repo);
9011 err = patch_check_path(new != NULL ? new : old, newpath,
9012 &status_new, &staged_status_new, fileindex, worktree, repo);
9016 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9019 if (old != NULL && new == NULL)
9020 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9021 else if (file_renamed) {
9022 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9024 err = patch_can_add(*newpath, status_new);
9025 } else if (old == NULL)
9026 err = patch_can_add(*newpath, status_new);
9028 err = patch_can_edit(*newpath, status_new, staged_status_new);
9040 const struct got_error *
9041 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9042 struct got_worktree *worktree, struct got_fileindex *fileindex,
9043 got_worktree_checkout_cb progress_cb, void *progress_arg)
9045 struct schedule_addition_args saa;
9047 memset(&saa, 0, sizeof(saa));
9048 saa.worktree = worktree;
9049 saa.fileindex = fileindex;
9050 saa.progress_cb = progress_cb;
9051 saa.progress_arg = progress_arg;
9054 return worktree_status(worktree, path, fileindex, repo,
9055 schedule_addition, &saa, NULL, NULL, 1, 0);
9058 const struct got_error *
9059 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9060 struct got_worktree *worktree, struct got_fileindex *fileindex,
9061 got_worktree_delete_cb progress_cb, void *progress_arg)
9063 struct schedule_deletion_args sda;
9065 memset(&sda, 0, sizeof(sda));
9066 sda.worktree = worktree;
9067 sda.fileindex = fileindex;
9068 sda.progress_cb = progress_cb;
9069 sda.progress_arg = progress_arg;
9071 sda.delete_local_mods = 0;
9072 sda.keep_on_disk = 0;
9073 sda.ignore_missing_paths = 0;
9074 sda.status_codes = NULL;
9076 return worktree_status(worktree, path, fileindex, repo,
9077 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9080 const struct got_error *
9081 got_worktree_patch_complete(struct got_fileindex *fileindex,
9082 const char *fileindex_path)
9084 const struct got_error *err = NULL;
9086 err = sync_fileindex(fileindex, fileindex_path);
9087 got_fileindex_free(fileindex);