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.
17 #include "got_compat.h"
20 #include <sys/queue.h>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t apply_umask(mode_t);
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
129 return got_error_from_errno("got_ref_to_str");
131 refstr = strdup(got_ref_get_name(head_ref));
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
147 uint32_t uuid_status;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
160 err = got_ref_resolve(&commit_id, repo, head_ref);
163 err = got_object_get_type(&obj_type, repo, commit_id);
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
301 err = write_head_ref(path_got, head_ref);
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
336 err = got_object_open(&obj, repo, commit_id);
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
361 got_object_close(obj);
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
413 size_t flen1 = 0, flen2 = 0;
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
432 } else if (flen2 == 0) {
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
466 if (size1 != size2) {
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
495 return got_error_from_errno("fread");
499 outlen = write(outfd, fbuf, flen);
501 return got_error_from_errno("write");
503 return got_error(GOT_ERR_IO);
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 base_path_deriv, "");
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 base_path_deriv2, "");
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
587 err = got_error_from_errno("dprintf");
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
596 err = got_error_from_errno("dprintf");
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
672 if (err->code != GOT_ERR_FILE_BINARY)
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
706 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
779 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 err = got_error_from_errno2("fchmod", path);
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
852 ondisk_target[ondisk_len] = '\0';
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
916 free(ancestor_target);
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
988 err = got_error_from_errno("asprintf");
993 err = got_opentemp_named(&blob_orig_path, &f_orig,
997 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1004 * No common ancestor exists. This is an "add vs add" conflict
1005 * and we simply use an empty ancestor file to make both files
1006 * appear in the merged result in their entirety.
1008 f_orig = got_opentemp();
1009 if (f_orig == NULL) {
1010 err = got_error_from_errno("got_opentemp");
1015 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 err = got_error_from_errno("asprintf");
1021 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1024 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1029 err = got_object_id_str(&id_str, deriv_base_commit_id);
1032 if (asprintf(&label_deriv, "%s: commit %s",
1033 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 err = got_error_from_errno("asprintf");
1039 * In order the run a 3-way merge with a symlink we copy the symlink's
1040 * target path into a temporary file and use that file with diff3.
1042 if (S_ISLNK(st_mode)) {
1043 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1048 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1050 err = got_error_from_errno2("open", ondisk_path);
1053 f_deriv2 = fdopen(fd, "r");
1054 if (f_deriv2 == NULL) {
1055 err = got_error_from_errno2("fdopen", ondisk_path);
1061 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1065 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1067 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1072 if (blob_orig_path) {
1073 unlink(blob_orig_path);
1074 free(blob_orig_path);
1076 if (blob_deriv_path) {
1077 unlink(blob_deriv_path);
1078 free(blob_deriv_path);
1086 static const struct got_error *
1087 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 int wt_fd, const char *path, struct got_object_id *blob_id)
1091 const struct got_error *err = NULL;
1092 struct got_fileindex_entry *new_ie;
1096 err = got_fileindex_entry_alloc(&new_ie, path);
1100 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 blob_id->sha1, base_commit_id->sha1, 1);
1105 err = got_fileindex_entry_add(fileindex, new_ie);
1108 got_fileindex_entry_free(new_ie);
1115 get_ondisk_perms(int executable, mode_t st_mode)
1117 mode_t xbits = S_IXUSR;
1120 /* Map read bits to execute bits. */
1121 if (st_mode & S_IRGRP)
1123 if (st_mode & S_IROTH)
1125 return st_mode | xbits;
1132 apply_umask(mode_t mode)
1141 /* forward declaration */
1142 static const struct got_error *
1143 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 const char *path, mode_t te_mode, mode_t st_mode,
1145 struct got_blob_object *blob, int restoring_missing_file,
1146 int reverting_versioned_file, int installing_bad_symlink,
1147 int path_is_unversioned, struct got_repository *repo,
1148 got_worktree_checkout_cb progress_cb, void *progress_arg);
1151 * This function assumes that the provided symlink target points at a
1152 * safe location in the work tree!
1154 static const struct got_error *
1155 replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 const char *target_path, size_t target_len)
1158 const struct got_error *err = NULL;
1160 char etarget[PATH_MAX];
1166 * "Bad" symlinks (those pointing outside the work tree or into the
1167 * .got directory) are installed in the work tree as a regular file
1168 * which contains the bad symlink target path.
1169 * The new symlink target has already been checked for safety by our
1170 * caller. If we can successfully open a regular file then we simply
1171 * replace this file with a symlink below.
1173 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1175 if (!got_err_open_nofollow_on_symlink())
1176 return got_error_from_errno2("open", ondisk_path);
1178 /* We are updating an existing on-disk symlink. */
1179 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1181 return got_error_from_errno2("readlink", ondisk_path);
1183 if (elen == target_len &&
1184 memcmp(etarget, target_path, target_len) == 0)
1185 return NULL; /* nothing to do */
1189 err = update_symlink(ondisk_path, target_path, target_len);
1190 if (fd != -1 && close(fd) == -1 && err == NULL)
1191 err = got_error_from_errno2("close", ondisk_path);
1195 static const struct got_error *
1196 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1199 const struct got_error *err = NULL;
1200 char canonpath[PATH_MAX];
1201 char *path_got = NULL;
1203 *is_bad_symlink = 0;
1205 if (target_len >= sizeof(canonpath)) {
1206 *is_bad_symlink = 1;
1211 * We do not use realpath(3) to resolve the symlink's target
1212 * path because we don't want to resolve symlinks recursively.
1213 * Instead we make the path absolute and then canonicalize it.
1214 * Relative symlink target lookup should begin at the directory
1215 * in which the blob object is being installed.
1217 if (!got_path_is_absolute(target_path)) {
1218 char *abspath, *parent;
1219 err = got_path_dirname(&parent, ondisk_path);
1222 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 return got_error_from_errno("asprintf");
1227 if (strlen(abspath) >= sizeof(canonpath)) {
1228 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1232 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1237 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1242 /* Only allow symlinks pointing at paths within the work tree. */
1243 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 *is_bad_symlink = 1;
1248 /* Do not allow symlinks pointing into the .got directory. */
1249 if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 GOT_WORKTREE_GOT_DIR) == -1)
1251 return got_error_from_errno("asprintf");
1252 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 *is_bad_symlink = 1;
1259 static const struct got_error *
1260 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 int restoring_missing_file, int reverting_versioned_file,
1263 int path_is_unversioned, int allow_bad_symlinks,
1264 struct got_repository *repo,
1265 got_worktree_checkout_cb progress_cb, void *progress_arg)
1267 const struct got_error *err = NULL;
1268 char target_path[PATH_MAX];
1269 size_t len, target_len = 0;
1270 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1273 *is_bad_symlink = 0;
1276 * Blob object content specifies the target path of the link.
1277 * If a symbolic link cannot be installed we instead create
1278 * a regular file which contains the link target path stored
1279 * in the blob object.
1282 err = got_object_blob_read_block(&len, blob);
1286 if (len + target_len >= sizeof(target_path)) {
1287 /* Path too long; install as a regular file. */
1288 *is_bad_symlink = 1;
1289 got_object_blob_rewind(blob);
1290 return install_blob(worktree, ondisk_path, path,
1291 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 restoring_missing_file, reverting_versioned_file,
1293 1, path_is_unversioned, repo, progress_cb,
1297 /* Skip blob object header first time around. */
1298 memcpy(target_path + target_len, buf + hdrlen,
1300 target_len += len - hdrlen;
1304 target_path[target_len] = '\0';
1306 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 ondisk_path, worktree->root_path);
1311 if (*is_bad_symlink && !allow_bad_symlinks) {
1312 /* install as a regular file */
1313 got_object_blob_rewind(blob);
1314 err = install_blob(worktree, ondisk_path, path,
1315 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 restoring_missing_file, reverting_versioned_file, 1,
1317 path_is_unversioned, repo, progress_cb, progress_arg);
1321 if (symlink(target_path, ondisk_path) == -1) {
1322 if (errno == EEXIST) {
1323 int symlink_replaced;
1324 if (path_is_unversioned) {
1325 err = (*progress_cb)(progress_arg,
1326 GOT_STATUS_UNVERSIONED, path);
1329 err = replace_existing_symlink(&symlink_replaced,
1330 ondisk_path, target_path, target_len);
1334 if (symlink_replaced) {
1335 err = (*progress_cb)(progress_arg,
1336 reverting_versioned_file ?
1338 GOT_STATUS_UPDATE, path);
1340 err = (*progress_cb)(progress_arg,
1341 GOT_STATUS_EXISTS, path);
1344 return err; /* Nothing else to do. */
1347 if (errno == ENOENT) {
1349 err = got_path_dirname(&parent, ondisk_path);
1352 err = add_dir_on_disk(worktree, parent);
1357 * Retry, and fall through to error handling
1358 * below if this second attempt fails.
1360 if (symlink(target_path, ondisk_path) != -1) {
1361 err = NULL; /* success */
1366 /* Handle errors from first or second creation attempt. */
1367 if (errno == ENAMETOOLONG) {
1368 /* bad target path; install as a regular file */
1369 *is_bad_symlink = 1;
1370 got_object_blob_rewind(blob);
1371 err = install_blob(worktree, ondisk_path, path,
1372 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 restoring_missing_file, reverting_versioned_file, 1,
1374 path_is_unversioned, repo,
1375 progress_cb, progress_arg);
1376 } else if (errno == ENOTDIR) {
1377 err = got_error_path(ondisk_path,
1378 GOT_ERR_FILE_OBSTRUCTED);
1380 err = got_error_from_errno3("symlink",
1381 target_path, ondisk_path);
1383 } else if (progress_cb)
1384 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1389 static const struct got_error *
1390 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 const char *path, mode_t te_mode, mode_t st_mode,
1392 struct got_blob_object *blob, int restoring_missing_file,
1393 int reverting_versioned_file, int installing_bad_symlink,
1394 int path_is_unversioned, struct got_repository *repo,
1395 got_worktree_checkout_cb progress_cb, void *progress_arg)
1397 const struct got_error *err = NULL;
1401 char *tmppath = NULL;
1404 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1408 if (errno == ENOENT || errno == ENOTDIR) {
1410 err = got_path_dirname(&parent, path);
1413 err = add_dir_on_disk(worktree, parent);
1414 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1415 err = got_error_path(path, err->code);
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1423 return got_error_from_errno2("open",
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1451 return got_error_from_errno2("open", ondisk_path);
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1478 err = got_error_from_errno("write");
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1517 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 * conflict marker is found in newly added lines only.
1520 static const struct got_error *
1521 get_modified_file_content_status(unsigned char *status,
1522 struct got_blob_object *blob, const char *path, struct stat *sb,
1525 const struct got_error *err, *free_err;
1526 const char *markers[3] = {
1527 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 GOT_DIFF_CONFLICT_MARKER_SEP,
1529 GOT_DIFF_CONFLICT_MARKER_END
1532 struct got_diffreg_result *diffreg_result = NULL;
1533 struct diff_result *r;
1534 int nchunks_parsed, n, i = 0, ln = 0;
1536 size_t linesize = 0;
1539 if (*status != GOT_STATUS_MODIFY)
1542 f1 = got_opentemp();
1544 return got_error_from_errno("got_opentemp");
1547 got_object_blob_rewind(blob);
1548 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1553 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1558 r = diffreg_result->result;
1560 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 struct diff_chunk *c;
1562 struct diff_chunk_context cc = {};
1566 * We can optimise a little by advancing straight
1567 * to the next chunk if this one has no added lines.
1569 c = diff_chunk_get(r, n);
1571 if (diff_chunk_type(c) != CHUNK_PLUS) {
1573 continue; /* removed or unchanged lines */
1576 pos = diff_chunk_get_right_start_pos(c);
1577 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1578 err = got_ferror(ondisk_file, GOT_ERR_IO);
1582 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1583 ln = cc.right.start;
1585 while (ln < cc.right.end) {
1586 linelen = getline(&line, &linesize, ondisk_file);
1587 if (linelen == -1) {
1588 if (feof(ondisk_file))
1590 err = got_ferror(ondisk_file, GOT_ERR_IO);
1594 if (line && strncmp(line, markers[i],
1595 strlen(markers[i])) == 0) {
1596 if (strcmp(markers[i],
1597 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1598 *status = GOT_STATUS_CONFLICT;
1609 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1610 err = got_error_from_errno("fclose");
1611 free_err = got_diffreg_result_free(diffreg_result);
1619 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1621 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1622 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1626 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1628 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1629 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1630 ie->mtime_sec == sb->st_mtim.tv_sec &&
1631 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1632 ie->size == (sb->st_size & 0xffffffff) &&
1633 !xbit_differs(ie, sb->st_mode));
1636 static unsigned char
1637 get_staged_status(struct got_fileindex_entry *ie)
1639 switch (got_fileindex_entry_stage_get(ie)) {
1640 case GOT_FILEIDX_STAGE_ADD:
1641 return GOT_STATUS_ADD;
1642 case GOT_FILEIDX_STAGE_DELETE:
1643 return GOT_STATUS_DELETE;
1644 case GOT_FILEIDX_STAGE_MODIFY:
1645 return GOT_STATUS_MODIFY;
1647 return GOT_STATUS_NO_CHANGE;
1651 static const struct got_error *
1652 get_symlink_modification_status(unsigned char *status,
1653 struct got_fileindex_entry *ie, const char *abspath,
1654 int dirfd, const char *de_name, struct got_blob_object *blob)
1656 const struct got_error *err = NULL;
1657 char target_path[PATH_MAX];
1658 char etarget[PATH_MAX];
1660 size_t len, target_len = 0;
1661 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1662 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1664 *status = GOT_STATUS_NO_CHANGE;
1666 /* Blob object content specifies the target path of the link. */
1668 err = got_object_blob_read_block(&len, blob);
1671 if (len + target_len >= sizeof(target_path)) {
1673 * Should not happen. The blob contents were OK
1674 * when this symlink was installed.
1676 return got_error(GOT_ERR_NO_SPACE);
1679 /* Skip blob object header first time around. */
1680 memcpy(target_path + target_len, buf + hdrlen,
1682 target_len += len - hdrlen;
1686 target_path[target_len] = '\0';
1689 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1691 return got_error_from_errno2("readlinkat", abspath);
1693 elen = readlink(abspath, etarget, sizeof(etarget));
1695 return got_error_from_errno2("readlink", abspath);
1698 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1699 *status = GOT_STATUS_MODIFY;
1704 static const struct got_error *
1705 get_file_status(unsigned char *status, struct stat *sb,
1706 struct got_fileindex_entry *ie, const char *abspath,
1707 int dirfd, const char *de_name, struct got_repository *repo)
1709 const struct got_error *err = NULL;
1710 struct got_object_id id;
1712 int fd = -1, fd1 = -1;
1715 struct got_blob_object *blob = NULL;
1717 unsigned char staged_status;
1719 staged_status = get_staged_status(ie);
1720 *status = GOT_STATUS_NO_CHANGE;
1721 memset(sb, 0, sizeof(*sb));
1724 * Whenever the caller provides a directory descriptor and a
1725 * directory entry name for the file, use them! This prevents
1726 * race conditions if filesystem paths change beneath our feet.
1729 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 if (errno == ENOENT) {
1731 if (got_fileindex_entry_has_file_on_disk(ie))
1732 *status = GOT_STATUS_MISSING;
1734 *status = GOT_STATUS_DELETE;
1737 err = got_error_from_errno2("fstatat", abspath);
1741 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 if (fd == -1 && errno != ENOENT &&
1743 !got_err_open_nofollow_on_symlink())
1744 return got_error_from_errno2("open", abspath);
1745 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1746 if (lstat(abspath, sb) == -1)
1747 return got_error_from_errno2("lstat", abspath);
1748 } else if (fd == -1 || fstat(fd, sb) == -1) {
1749 if (errno == ENOENT) {
1750 if (got_fileindex_entry_has_file_on_disk(ie))
1751 *status = GOT_STATUS_MISSING;
1753 *status = GOT_STATUS_DELETE;
1756 err = got_error_from_errno2("fstat", abspath);
1761 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1762 *status = GOT_STATUS_OBSTRUCTED;
1766 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1767 *status = GOT_STATUS_DELETE;
1769 } else if (!got_fileindex_entry_has_blob(ie) &&
1770 staged_status != GOT_STATUS_ADD) {
1771 *status = GOT_STATUS_ADD;
1775 if (!stat_info_differs(ie, sb))
1778 if (S_ISLNK(sb->st_mode) &&
1779 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1780 *status = GOT_STATUS_MODIFY;
1784 if (staged_status == GOT_STATUS_MODIFY ||
1785 staged_status == GOT_STATUS_ADD)
1786 got_fileindex_entry_get_staged_blob_id(&id, ie);
1788 got_fileindex_entry_get_blob_id(&id, ie);
1790 fd1 = got_opentempfd();
1792 err = got_error_from_errno("got_opentempfd");
1795 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1799 if (S_ISLNK(sb->st_mode)) {
1800 err = get_symlink_modification_status(status, ie,
1801 abspath, dirfd, de_name, blob);
1806 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1808 err = got_error_from_errno2("openat", abspath);
1813 f = fdopen(fd, "r");
1815 err = got_error_from_errno2("fdopen", abspath);
1819 hdrlen = got_object_blob_get_hdrlen(blob);
1821 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1822 err = got_object_blob_read_block(&blen, blob);
1825 /* Skip length of blob object header first time around. */
1826 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1827 if (flen == 0 && ferror(f)) {
1828 err = got_error_from_errno("fread");
1831 if (blen - hdrlen == 0) {
1833 *status = GOT_STATUS_MODIFY;
1835 } else if (flen == 0) {
1836 if (blen - hdrlen != 0)
1837 *status = GOT_STATUS_MODIFY;
1839 } else if (blen - hdrlen == flen) {
1840 /* Skip blob object header first time around. */
1841 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1842 *status = GOT_STATUS_MODIFY;
1846 *status = GOT_STATUS_MODIFY;
1852 if (*status == GOT_STATUS_MODIFY) {
1854 err = get_modified_file_content_status(status, blob, ie->path,
1856 } else if (xbit_differs(ie, sb->st_mode))
1857 *status = GOT_STATUS_MODE_CHANGE;
1859 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1860 err = got_error_from_errno("close");
1862 got_object_blob_close(blob);
1863 if (f != NULL && fclose(f) == EOF && err == NULL)
1864 err = got_error_from_errno2("fclose", abspath);
1865 if (fd != -1 && close(fd) == -1 && err == NULL)
1866 err = got_error_from_errno2("close", abspath);
1871 * Update timestamps in the file index if a file is unmodified and
1872 * we had to run a full content comparison to find out.
1874 static const struct got_error *
1875 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 struct got_fileindex_entry *ie, struct stat *sb)
1878 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 return got_fileindex_entry_update(ie, wt_fd, path,
1880 ie->blob_sha1, ie->commit_sha1, 1);
1885 static const struct got_error *remove_ondisk_file(const char *, const char *);
1887 static const struct got_error *
1888 update_blob(struct got_worktree *worktree,
1889 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1890 struct got_tree_entry *te, const char *path,
1891 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1894 const struct got_error *err = NULL;
1895 struct got_blob_object *blob = NULL;
1896 char *ondisk_path = NULL;
1897 unsigned char status = GOT_STATUS_NO_CHANGE;
1899 int fd1 = -1, fd2 = -1;
1901 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1902 return got_error_from_errno("asprintf");
1905 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1906 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1909 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1913 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1914 sb.st_mode = got_fileindex_perms_to_st(ie);
1916 if (stat(ondisk_path, &sb) == -1) {
1917 if (errno != ENOENT && errno != ENOTDIR) {
1918 err = got_error_from_errno2("stat",
1922 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1923 status = GOT_STATUS_UNVERSIONED;
1925 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1926 status = GOT_STATUS_UNVERSIONED;
1928 status = GOT_STATUS_OBSTRUCTED;
1932 if (status == GOT_STATUS_OBSTRUCTED) {
1934 got_fileindex_entry_mark_skipped(ie);
1935 err = (*progress_cb)(progress_arg, status, path);
1938 if (status == GOT_STATUS_CONFLICT) {
1940 got_fileindex_entry_mark_skipped(ie);
1941 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1946 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1947 if (status == GOT_STATUS_UNVERSIONED) {
1948 err = (*progress_cb)(progress_arg, status, path);
1949 } else if (status != GOT_STATUS_NO_CHANGE &&
1950 status != GOT_STATUS_DELETE &&
1951 status != GOT_STATUS_NONEXISTENT &&
1952 status != GOT_STATUS_MISSING) {
1953 err = (*progress_cb)(progress_arg,
1954 GOT_STATUS_CANNOT_DELETE, path);
1956 if (status != GOT_STATUS_DELETE &&
1957 status != GOT_STATUS_NONEXISTENT &&
1958 status != GOT_STATUS_MISSING) {
1959 err = remove_ondisk_file(worktree->root_path,
1961 if (err && !(err->code == GOT_ERR_ERRNO &&
1965 got_fileindex_entry_remove(fileindex, ie);
1966 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1969 goto done; /* nothing else to do */
1972 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1973 (S_ISLNK(te->mode) ||
1974 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1976 * This is a regular file or an installed bad symlink.
1977 * If the file index indicates that this file is already
1978 * up-to-date with respect to the repository we can skip
1979 * updating contents of this file.
1981 if (got_fileindex_entry_has_commit(ie) &&
1982 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1983 SHA1_DIGEST_LENGTH) == 0) {
1985 err = sync_timestamps(worktree->root_fd,
1986 path, status, ie, &sb);
1989 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1993 if (got_fileindex_entry_has_blob(ie) &&
1994 memcmp(ie->blob_sha1, te->id.sha1,
1995 SHA1_DIGEST_LENGTH) == 0) {
1996 /* Different commit but the same blob. */
1997 if (got_fileindex_entry_has_commit(ie)) {
1998 /* Update the base commit ID of this file. */
1999 memcpy(ie->commit_sha1,
2000 worktree->base_commit_id->sha1,
2001 sizeof(ie->commit_sha1));
2003 err = sync_timestamps(worktree->root_fd,
2004 path, status, ie, &sb);
2007 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2013 fd1 = got_opentempfd();
2015 err = got_error_from_errno("got_opentempfd");
2018 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2022 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2023 int update_timestamps;
2024 struct got_blob_object *blob2 = NULL;
2025 char *label_orig = NULL;
2026 if (got_fileindex_entry_has_blob(ie)) {
2027 fd2 = got_opentempfd();
2029 err = got_error_from_errno("got_opentempfd");
2032 struct got_object_id id2;
2033 got_fileindex_entry_get_blob_id(&id2, ie);
2034 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2039 if (got_fileindex_entry_has_commit(ie)) {
2040 char id_str[SHA1_DIGEST_STRING_LENGTH];
2041 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2042 sizeof(id_str)) == NULL) {
2043 err = got_error_path(id_str,
2044 GOT_ERR_BAD_OBJ_ID_STR);
2047 if (asprintf(&label_orig, "%s: commit %s",
2048 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2049 err = got_error_from_errno("asprintf");
2053 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2055 err = got_object_blob_read_to_str(&link_target, blob);
2058 err = merge_symlink(worktree, blob2, ondisk_path, path,
2059 label_orig, link_target, worktree->base_commit_id,
2060 repo, progress_cb, progress_arg);
2063 err = merge_blob(&update_timestamps, worktree, blob2,
2064 ondisk_path, path, sb.st_mode, label_orig, blob,
2065 worktree->base_commit_id, repo,
2066 progress_cb, progress_arg);
2069 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2070 err = got_error_from_errno("close");
2074 got_object_blob_close(blob2);
2078 * Do not update timestamps of files with local changes.
2079 * Otherwise, a future status walk would treat them as
2080 * unmodified files again.
2082 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2083 blob->id.sha1, worktree->base_commit_id->sha1,
2085 } else if (status == GOT_STATUS_MODE_CHANGE) {
2086 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2087 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2088 } else if (status == GOT_STATUS_DELETE) {
2089 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2092 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2093 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2097 int is_bad_symlink = 0;
2098 if (S_ISLNK(te->mode)) {
2099 err = install_symlink(&is_bad_symlink, worktree,
2100 ondisk_path, path, blob,
2101 status == GOT_STATUS_MISSING, 0,
2102 status == GOT_STATUS_UNVERSIONED, 0,
2103 repo, progress_cb, progress_arg);
2105 err = install_blob(worktree, ondisk_path, path,
2106 te->mode, sb.st_mode, blob,
2107 status == GOT_STATUS_MISSING, 0, 0,
2108 status == GOT_STATUS_UNVERSIONED, repo,
2109 progress_cb, progress_arg);
2115 err = got_fileindex_entry_update(ie,
2116 worktree->root_fd, path, blob->id.sha1,
2117 worktree->base_commit_id->sha1, 1);
2119 err = create_fileindex_entry(&ie, fileindex,
2120 worktree->base_commit_id, worktree->root_fd, path,
2126 if (is_bad_symlink) {
2127 got_fileindex_entry_filetype_set(ie,
2128 GOT_FILEIDX_MODE_BAD_SYMLINK);
2132 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2133 err = got_error_from_errno("close");
2136 got_object_blob_close(blob);
2142 static const struct got_error *
2143 remove_ondisk_file(const char *root_path, const char *path)
2145 const struct got_error *err = NULL;
2146 char *ondisk_path = NULL, *parent = NULL;
2148 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2149 return got_error_from_errno("asprintf");
2151 if (unlink(ondisk_path) == -1) {
2152 if (errno != ENOENT)
2153 err = got_error_from_errno2("unlink", ondisk_path);
2155 size_t root_len = strlen(root_path);
2156 err = got_path_dirname(&parent, ondisk_path);
2159 while (got_path_cmp(parent, root_path,
2160 strlen(parent), root_len) != 0) {
2162 ondisk_path = parent;
2164 if (rmdir(ondisk_path) == -1) {
2165 if (errno != ENOTEMPTY)
2166 err = got_error_from_errno2("rmdir",
2170 err = got_path_dirname(&parent, ondisk_path);
2181 static const struct got_error *
2182 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2183 struct got_fileindex_entry *ie, struct got_repository *repo,
2184 got_worktree_checkout_cb progress_cb, void *progress_arg)
2186 const struct got_error *err = NULL;
2187 unsigned char status;
2191 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2192 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2194 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2196 return got_error_from_errno("asprintf");
2198 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2202 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2203 char ondisk_target[PATH_MAX];
2204 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2205 sizeof(ondisk_target));
2206 if (ondisk_len == -1) {
2207 err = got_error_from_errno2("readlink", ondisk_path);
2210 ondisk_target[ondisk_len] = '\0';
2211 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2212 NULL, NULL, /* XXX pass common ancestor info? */
2213 ondisk_target, ondisk_path);
2216 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2221 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2222 status == GOT_STATUS_ADD) {
2223 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2227 * Preserve the working file and change the deleted blob's
2228 * entry into a schedule-add entry.
2230 err = got_fileindex_entry_update(ie, worktree->root_fd,
2231 ie->path, NULL, NULL, 0);
2233 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2236 if (status == GOT_STATUS_NO_CHANGE) {
2237 err = remove_ondisk_file(worktree->root_path, ie->path);
2241 got_fileindex_entry_remove(fileindex, ie);
2248 struct diff_cb_arg {
2249 struct got_fileindex *fileindex;
2250 struct got_worktree *worktree;
2251 struct got_repository *repo;
2252 got_worktree_checkout_cb progress_cb;
2254 got_cancel_cb cancel_cb;
2258 static const struct got_error *
2259 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2260 struct got_tree_entry *te, const char *parent_path)
2262 struct diff_cb_arg *a = arg;
2264 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2265 return got_error(GOT_ERR_CANCELLED);
2267 return update_blob(a->worktree, a->fileindex, ie, te,
2268 ie->path, a->repo, a->progress_cb, a->progress_arg);
2271 static const struct got_error *
2272 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2274 struct diff_cb_arg *a = arg;
2276 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2277 return got_error(GOT_ERR_CANCELLED);
2279 return delete_blob(a->worktree, a->fileindex, ie,
2280 a->repo, a->progress_cb, a->progress_arg);
2283 static const struct got_error *
2284 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2286 struct diff_cb_arg *a = arg;
2287 const struct got_error *err;
2290 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 return got_error(GOT_ERR_CANCELLED);
2293 if (got_object_tree_entry_is_submodule(te))
2296 if (asprintf(&path, "%s%s%s", parent_path,
2297 parent_path[0] ? "/" : "", te->name)
2299 return got_error_from_errno("asprintf");
2301 if (S_ISDIR(te->mode))
2302 err = add_dir_on_disk(a->worktree, path);
2304 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2305 a->repo, a->progress_cb, a->progress_arg);
2311 const struct got_error *
2312 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2314 uint32_t uuid_status;
2316 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2317 if (uuid_status != uuid_s_ok) {
2319 return got_error_uuid(uuid_status, "uuid_to_string");
2325 static const struct got_error *
2326 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2328 const struct got_error *err = NULL;
2329 char *uuidstr = NULL;
2333 err = got_worktree_get_uuid(&uuidstr, worktree);
2337 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2338 err = got_error_from_errno("asprintf");
2345 const struct got_error *
2346 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2349 return get_ref_name(refname, worktree, prefix);
2352 const struct got_error *
2353 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2355 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2358 static const struct got_error *
2359 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2361 return get_ref_name(refname, worktree,
2362 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2365 static const struct got_error *
2366 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2368 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2371 static const struct got_error *
2372 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2374 return get_ref_name(refname, worktree,
2375 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2378 static const struct got_error *
2379 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2381 return get_ref_name(refname, worktree,
2382 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2385 static const struct got_error *
2386 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2388 return get_ref_name(refname, worktree,
2389 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2392 static const struct got_error *
2393 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2395 return get_ref_name(refname, worktree,
2396 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2399 static const struct got_error *
2400 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2402 return get_ref_name(refname, worktree,
2403 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2406 static const struct got_error *
2407 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2409 return get_ref_name(refname, worktree,
2410 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2413 const struct got_error *
2414 got_worktree_get_histedit_script_path(char **path,
2415 struct got_worktree *worktree)
2417 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2418 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2420 return got_error_from_errno("asprintf");
2425 static const struct got_error *
2426 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2428 return get_ref_name(refname, worktree,
2429 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2432 static const struct got_error *
2433 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2435 return get_ref_name(refname, worktree,
2436 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2440 * Prevent Git's garbage collector from deleting our base commit by
2441 * setting a reference to our base commit's ID.
2443 static const struct got_error *
2444 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2446 const struct got_error *err = NULL;
2447 struct got_reference *ref = NULL;
2450 err = got_worktree_get_base_ref_name(&refname, worktree);
2454 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2458 err = got_ref_write(ref, repo);
2466 static const struct got_error *
2467 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2469 const struct got_error *err = NULL;
2471 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2472 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2473 err = got_error_from_errno("asprintf");
2474 *fileindex_path = NULL;
2480 static const struct got_error *
2481 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2482 struct got_worktree *worktree)
2484 const struct got_error *err = NULL;
2487 *fileindex_path = NULL;
2488 *fileindex = got_fileindex_alloc();
2489 if (*fileindex == NULL)
2490 return got_error_from_errno("got_fileindex_alloc");
2492 err = get_fileindex_path(fileindex_path, worktree);
2496 index = fopen(*fileindex_path, "rbe");
2497 if (index == NULL) {
2498 if (errno != ENOENT)
2499 err = got_error_from_errno2("fopen", *fileindex_path);
2501 err = got_fileindex_read(*fileindex, index);
2502 if (fclose(index) == EOF && err == NULL)
2503 err = got_error_from_errno("fclose");
2507 free(*fileindex_path);
2508 *fileindex_path = NULL;
2509 got_fileindex_free(*fileindex);
2515 struct bump_base_commit_id_arg {
2516 struct got_object_id *base_commit_id;
2519 const char *entry_name;
2520 got_worktree_checkout_cb progress_cb;
2524 static const struct got_error *
2525 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2527 const struct got_error *err;
2528 struct bump_base_commit_id_arg *a = arg;
2530 if (a->entry_name) {
2531 if (strcmp(ie->path, a->path) != 0)
2533 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2536 if (got_fileindex_entry_was_skipped(ie))
2539 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2540 SHA1_DIGEST_LENGTH) == 0)
2543 if (a->progress_cb) {
2544 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2549 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2553 /* Bump base commit ID of all files within an updated part of the work tree. */
2554 static const struct got_error *
2555 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2556 struct got_fileindex *fileindex,
2557 got_worktree_checkout_cb progress_cb, void *progress_arg)
2559 struct bump_base_commit_id_arg bbc_arg;
2561 bbc_arg.base_commit_id = worktree->base_commit_id;
2562 bbc_arg.entry_name = NULL;
2564 bbc_arg.path_len = 0;
2565 bbc_arg.progress_cb = progress_cb;
2566 bbc_arg.progress_arg = progress_arg;
2568 return got_fileindex_for_each_entry_safe(fileindex,
2569 bump_base_commit_id, &bbc_arg);
2572 static const struct got_error *
2573 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2575 const struct got_error *err = NULL;
2576 char *new_fileindex_path = NULL;
2577 FILE *new_index = NULL;
2578 struct timespec timeout;
2580 err = got_opentemp_named(&new_fileindex_path, &new_index,
2581 fileindex_path, "");
2585 err = got_fileindex_write(fileindex, new_index);
2589 if (rename(new_fileindex_path, fileindex_path) != 0) {
2590 err = got_error_from_errno3("rename", new_fileindex_path,
2592 unlink(new_fileindex_path);
2596 * Sleep for a short amount of time to ensure that files modified after
2597 * this program exits have a different time stamp from the one which
2598 * was recorded in the file index.
2601 timeout.tv_nsec = 1;
2602 nanosleep(&timeout, NULL);
2606 free(new_fileindex_path);
2610 static const struct got_error *
2611 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2612 struct got_object_id **tree_id, const char *wt_relpath,
2613 struct got_commit_object *base_commit, struct got_worktree *worktree,
2614 struct got_repository *repo)
2616 const struct got_error *err = NULL;
2617 struct got_object_id *id = NULL;
2618 char *in_repo_path = NULL;
2619 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2621 *entry_type = GOT_OBJ_TYPE_ANY;
2622 *tree_relpath = NULL;
2625 if (wt_relpath[0] == '\0') {
2626 /* Check out all files within the work tree. */
2627 *entry_type = GOT_OBJ_TYPE_TREE;
2628 *tree_relpath = strdup("");
2629 if (*tree_relpath == NULL) {
2630 err = got_error_from_errno("strdup");
2633 err = got_object_id_by_path(tree_id, repo, base_commit,
2634 worktree->path_prefix);
2640 /* Check out a subset of files in the work tree. */
2642 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2643 is_root_wt ? "" : "/", wt_relpath) == -1) {
2644 err = got_error_from_errno("asprintf");
2648 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2653 in_repo_path = NULL;
2655 err = got_object_get_type(entry_type, repo, id);
2659 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2660 /* Check out a single file. */
2661 if (strchr(wt_relpath, '/') == NULL) {
2662 /* Check out a single file in work tree's root dir. */
2663 in_repo_path = strdup(worktree->path_prefix);
2664 if (in_repo_path == NULL) {
2665 err = got_error_from_errno("strdup");
2668 *tree_relpath = strdup("");
2669 if (*tree_relpath == NULL) {
2670 err = got_error_from_errno("strdup");
2674 /* Check out a single file in a subdirectory. */
2675 err = got_path_dirname(tree_relpath, wt_relpath);
2678 if (asprintf(&in_repo_path, "%s%s%s",
2679 worktree->path_prefix, is_root_wt ? "" : "/",
2680 *tree_relpath) == -1) {
2681 err = got_error_from_errno("asprintf");
2685 err = got_object_id_by_path(tree_id, repo,
2686 base_commit, in_repo_path);
2688 /* Check out all files within a subdirectory. */
2689 *tree_id = got_object_id_dup(id);
2690 if (*tree_id == NULL) {
2691 err = got_error_from_errno("got_object_id_dup");
2694 *tree_relpath = strdup(wt_relpath);
2695 if (*tree_relpath == NULL) {
2696 err = got_error_from_errno("strdup");
2704 *entry_type = GOT_OBJ_TYPE_ANY;
2705 free(*tree_relpath);
2706 *tree_relpath = NULL;
2713 static const struct got_error *
2714 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2715 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2716 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2717 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2719 const struct got_error *err = NULL;
2720 struct got_commit_object *commit = NULL;
2721 struct got_tree_object *tree = NULL;
2722 struct got_fileindex_diff_tree_cb diff_cb;
2723 struct diff_cb_arg arg;
2725 err = ref_base_commit(worktree, repo);
2727 if (!(err->code == GOT_ERR_ERRNO &&
2728 (errno == EACCES || errno == EROFS)))
2730 err = (*progress_cb)(progress_arg,
2731 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2736 err = got_object_open_as_commit(&commit, repo,
2737 worktree->base_commit_id);
2741 err = got_object_open_as_tree(&tree, repo, tree_id);
2746 got_object_tree_find_entry(tree, entry_name) == NULL) {
2747 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2751 diff_cb.diff_old_new = diff_old_new;
2752 diff_cb.diff_old = diff_old;
2753 diff_cb.diff_new = diff_new;
2754 arg.fileindex = fileindex;
2755 arg.worktree = worktree;
2757 arg.progress_cb = progress_cb;
2758 arg.progress_arg = progress_arg;
2759 arg.cancel_cb = cancel_cb;
2760 arg.cancel_arg = cancel_arg;
2761 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2762 entry_name, repo, &diff_cb, &arg);
2765 got_object_tree_close(tree);
2767 got_object_commit_close(commit);
2771 const struct got_error *
2772 got_worktree_checkout_files(struct got_worktree *worktree,
2773 struct got_pathlist_head *paths, struct got_repository *repo,
2774 got_worktree_checkout_cb progress_cb, void *progress_arg,
2775 got_cancel_cb cancel_cb, void *cancel_arg)
2777 const struct got_error *err = NULL, *sync_err, *unlockerr;
2778 struct got_commit_object *commit = NULL;
2779 struct got_tree_object *tree = NULL;
2780 struct got_fileindex *fileindex = NULL;
2781 char *fileindex_path = NULL;
2782 struct got_pathlist_entry *pe;
2783 struct tree_path_data {
2784 STAILQ_ENTRY(tree_path_data) entry;
2785 struct got_object_id *tree_id;
2790 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2792 STAILQ_INIT(&tree_paths);
2794 err = lock_worktree(worktree, LOCK_EX);
2798 err = got_object_open_as_commit(&commit, repo,
2799 worktree->base_commit_id);
2803 /* Map all specified paths to in-repository trees. */
2804 TAILQ_FOREACH(pe, paths, entry) {
2805 tpd = malloc(sizeof(*tpd));
2807 err = got_error_from_errno("malloc");
2811 err = find_tree_entry_for_checkout(&tpd->entry_type,
2812 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2819 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2820 err = got_path_basename(&tpd->entry_name, pe->path);
2828 tpd->entry_name = NULL;
2830 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2834 * Read the file index.
2835 * Checking out files is supposed to be an idempotent operation.
2836 * If the on-disk file index is incomplete we will try to complete it.
2838 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2842 tpd = STAILQ_FIRST(&tree_paths);
2843 TAILQ_FOREACH(pe, paths, entry) {
2844 struct bump_base_commit_id_arg bbc_arg;
2846 err = checkout_files(worktree, fileindex, tpd->relpath,
2847 tpd->tree_id, tpd->entry_name, repo,
2848 progress_cb, progress_arg, cancel_cb, cancel_arg);
2852 bbc_arg.base_commit_id = worktree->base_commit_id;
2853 bbc_arg.entry_name = tpd->entry_name;
2854 bbc_arg.path = pe->path;
2855 bbc_arg.path_len = pe->path_len;
2856 bbc_arg.progress_cb = progress_cb;
2857 bbc_arg.progress_arg = progress_arg;
2858 err = got_fileindex_for_each_entry_safe(fileindex,
2859 bump_base_commit_id, &bbc_arg);
2863 tpd = STAILQ_NEXT(tpd, entry);
2865 sync_err = sync_fileindex(fileindex, fileindex_path);
2866 if (sync_err && err == NULL)
2869 free(fileindex_path);
2871 got_object_tree_close(tree);
2873 got_object_commit_close(commit);
2875 got_fileindex_free(fileindex);
2876 while (!STAILQ_EMPTY(&tree_paths)) {
2877 tpd = STAILQ_FIRST(&tree_paths);
2878 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2883 unlockerr = lock_worktree(worktree, LOCK_SH);
2884 if (unlockerr && err == NULL)
2889 static const struct got_error *
2890 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2891 struct got_fileindex_entry *ie, const char *ondisk_path,
2892 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2893 int restoring_missing_file, int reverting_versioned_file,
2894 int path_is_unversioned, int allow_bad_symlinks,
2895 struct got_repository *repo,
2896 got_worktree_checkout_cb progress_cb, void *progress_arg)
2898 const struct got_error *err = NULL;
2899 int is_bad_symlink = 0;
2901 if (S_ISLNK(mode2)) {
2902 err = install_symlink(&is_bad_symlink,
2903 worktree, ondisk_path, path2, blob2,
2904 restoring_missing_file,
2905 reverting_versioned_file,
2906 path_is_unversioned, allow_bad_symlinks,
2907 repo, progress_cb, progress_arg);
2909 err = install_blob(worktree, ondisk_path, path2,
2910 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2911 restoring_missing_file, reverting_versioned_file, 0,
2912 path_is_unversioned, repo, progress_cb, progress_arg);
2917 /* Adding an unversioned file. */
2918 err = got_fileindex_entry_alloc(&ie, path2);
2921 err = got_fileindex_entry_update(ie,
2922 worktree->root_fd, path2, NULL, NULL, 1);
2924 got_fileindex_entry_free(ie);
2927 err = got_fileindex_entry_add(fileindex, ie);
2929 got_fileindex_entry_free(ie);
2933 /* Re-adding a locally deleted file. */
2934 err = got_fileindex_entry_update(ie,
2935 worktree->root_fd, path2, ie->blob_sha1,
2936 worktree->base_commit_id->sha1, 0);
2941 if (is_bad_symlink) {
2942 got_fileindex_entry_filetype_set(ie,
2943 GOT_FILEIDX_MODE_BAD_SYMLINK);
2949 struct merge_file_cb_arg {
2950 struct got_worktree *worktree;
2951 struct got_fileindex *fileindex;
2952 got_worktree_checkout_cb progress_cb;
2954 got_cancel_cb cancel_cb;
2956 const char *label_orig;
2957 struct got_object_id *commit_id2;
2958 int allow_bad_symlinks;
2961 static const struct got_error *
2962 merge_file_cb(void *arg, struct got_blob_object *blob1,
2963 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2964 struct got_object_id *id1, struct got_object_id *id2,
2965 const char *path1, const char *path2,
2966 mode_t mode1, mode_t mode2, struct got_repository *repo)
2968 static const struct got_error *err = NULL;
2969 struct merge_file_cb_arg *a = arg;
2970 struct got_fileindex_entry *ie;
2971 char *ondisk_path = NULL;
2973 unsigned char status;
2974 int local_changes_subsumed;
2975 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2976 char *id_str = NULL, *label_deriv2 = NULL;
2978 if (blob1 && blob2) {
2979 ie = got_fileindex_entry_get(a->fileindex, path2,
2982 return (*a->progress_cb)(a->progress_arg,
2983 GOT_STATUS_MISSING, path2);
2985 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2987 return got_error_from_errno("asprintf");
2989 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2994 if (status == GOT_STATUS_DELETE) {
2995 err = (*a->progress_cb)(a->progress_arg,
2996 GOT_STATUS_MERGE, path2);
2999 if (status != GOT_STATUS_NO_CHANGE &&
3000 status != GOT_STATUS_MODIFY &&
3001 status != GOT_STATUS_CONFLICT &&
3002 status != GOT_STATUS_ADD) {
3003 err = (*a->progress_cb)(a->progress_arg, status, path2);
3007 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3009 err = got_object_blob_read_to_str(&link_target2, blob2);
3012 err = merge_symlink(a->worktree, blob1, ondisk_path,
3013 path2, a->label_orig, link_target2, a->commit_id2,
3014 repo, a->progress_cb, a->progress_arg);
3019 f_orig = got_opentemp();
3020 if (f_orig == NULL) {
3021 err = got_error_from_errno("got_opentemp");
3024 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3029 f_deriv2 = got_opentemp();
3030 if (f_deriv2 == NULL)
3032 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3037 fd = open(ondisk_path,
3038 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3040 err = got_error_from_errno2("open",
3044 f_deriv = fdopen(fd, "r");
3045 if (f_deriv == NULL) {
3046 err = got_error_from_errno2("fdopen",
3051 err = got_object_id_str(&id_str, a->commit_id2);
3054 if (asprintf(&label_deriv2, "%s: commit %s",
3055 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3056 err = got_error_from_errno("asprintf");
3059 err = merge_file(&local_changes_subsumed, a->worktree,
3060 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3061 mode2, a->label_orig, NULL, label_deriv2,
3062 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3063 a->progress_cb, a->progress_arg);
3066 ie = got_fileindex_entry_get(a->fileindex, path1,
3069 return (*a->progress_cb)(a->progress_arg,
3070 GOT_STATUS_MISSING, path1);
3072 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3074 return got_error_from_errno("asprintf");
3076 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3082 case GOT_STATUS_NO_CHANGE:
3083 err = (*a->progress_cb)(a->progress_arg,
3084 GOT_STATUS_DELETE, path1);
3087 err = remove_ondisk_file(a->worktree->root_path, path1);
3091 got_fileindex_entry_mark_deleted_from_disk(ie);
3093 case GOT_STATUS_DELETE:
3094 case GOT_STATUS_MISSING:
3095 err = (*a->progress_cb)(a->progress_arg,
3096 GOT_STATUS_DELETE, path1);
3100 got_fileindex_entry_mark_deleted_from_disk(ie);
3102 case GOT_STATUS_ADD: {
3103 struct got_object_id *id;
3107 * Delete the added file only if its content already
3108 * exists in the repository.
3110 err = got_object_blob_file_create(&id, &blob1_f,
3111 &blob1_size, path1);
3114 if (got_object_id_cmp(id, id1) == 0) {
3115 err = (*a->progress_cb)(a->progress_arg,
3116 GOT_STATUS_DELETE, path1);
3119 err = remove_ondisk_file(a->worktree->root_path,
3124 got_fileindex_entry_remove(a->fileindex,
3127 err = (*a->progress_cb)(a->progress_arg,
3128 GOT_STATUS_CANNOT_DELETE, path1);
3130 if (fclose(blob1_f) == EOF && err == NULL)
3131 err = got_error_from_errno("fclose");
3137 case GOT_STATUS_MODIFY:
3138 case GOT_STATUS_CONFLICT:
3139 err = (*a->progress_cb)(a->progress_arg,
3140 GOT_STATUS_CANNOT_DELETE, path1);
3144 case GOT_STATUS_OBSTRUCTED:
3145 err = (*a->progress_cb)(a->progress_arg, status, path1);
3153 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3155 return got_error_from_errno("asprintf");
3156 ie = got_fileindex_entry_get(a->fileindex, path2,
3159 err = get_file_status(&status, &sb, ie, ondisk_path,
3163 if (status != GOT_STATUS_NO_CHANGE &&
3164 status != GOT_STATUS_MODIFY &&
3165 status != GOT_STATUS_CONFLICT &&
3166 status != GOT_STATUS_ADD &&
3167 status != GOT_STATUS_DELETE) {
3168 err = (*a->progress_cb)(a->progress_arg,
3172 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3174 err = got_object_blob_read_to_str(&link_target2,
3178 err = merge_symlink(a->worktree, NULL,
3179 ondisk_path, path2, a->label_orig,
3180 link_target2, a->commit_id2, repo,
3181 a->progress_cb, a->progress_arg);
3183 } else if (S_ISREG(sb.st_mode)) {
3184 err = merge_blob(&local_changes_subsumed,
3185 a->worktree, NULL, ondisk_path, path2,
3186 sb.st_mode, a->label_orig, blob2,
3187 a->commit_id2, repo, a->progress_cb,
3189 } else if (status != GOT_STATUS_DELETE) {
3190 err = got_error_path(ondisk_path,
3191 GOT_ERR_FILE_OBSTRUCTED);
3195 if (status == GOT_STATUS_DELETE) {
3196 /* Re-add file with content from new blob. */
3197 err = add_file(a->worktree, a->fileindex, ie,
3198 ondisk_path, path2, blob2, mode2,
3199 0, 0, 0, a->allow_bad_symlinks,
3200 repo, a->progress_cb, a->progress_arg);
3205 err = add_file(a->worktree, a->fileindex, NULL,
3206 ondisk_path, path2, blob2, mode2,
3207 0, 0, 1, a->allow_bad_symlinks,
3208 repo, a->progress_cb, a->progress_arg);
3214 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3215 err = got_error_from_errno("fclose");
3216 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3217 err = got_error_from_errno("fclose");
3218 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3219 err = got_error_from_errno("fclose");
3226 static const struct got_error *
3227 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3229 struct got_worktree *worktree = arg;
3231 /* Reject merges into a work tree with mixed base commits. */
3232 if (got_fileindex_entry_has_commit(ie) &&
3233 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3234 SHA1_DIGEST_LENGTH) != 0)
3235 return got_error(GOT_ERR_MIXED_COMMITS);
3240 struct check_merge_conflicts_arg {
3241 struct got_worktree *worktree;
3242 struct got_fileindex *fileindex;
3243 struct got_repository *repo;
3246 static const struct got_error *
3247 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3248 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3249 struct got_object_id *id1, struct got_object_id *id2,
3250 const char *path1, const char *path2,
3251 mode_t mode1, mode_t mode2, struct got_repository *repo)
3253 const struct got_error *err = NULL;
3254 struct check_merge_conflicts_arg *a = arg;
3255 unsigned char status;
3257 struct got_fileindex_entry *ie;
3258 const char *path = path2 ? path2 : path1;
3259 struct got_object_id *id = id2 ? id2 : id1;
3265 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3269 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3271 return got_error_from_errno("asprintf");
3273 /* Reject merges into a work tree with conflicted files. */
3274 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3278 if (status == GOT_STATUS_CONFLICT)
3279 return got_error(GOT_ERR_CONFLICTS);
3284 static const struct got_error *
3285 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3286 const char *fileindex_path, struct got_object_id *commit_id1,
3287 struct got_object_id *commit_id2, struct got_repository *repo,
3288 got_worktree_checkout_cb progress_cb, void *progress_arg,
3289 got_cancel_cb cancel_cb, void *cancel_arg)
3291 const struct got_error *err = NULL, *sync_err;
3292 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3293 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3294 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3295 struct check_merge_conflicts_arg cmc_arg;
3296 struct merge_file_cb_arg arg;
3297 char *label_orig = NULL;
3298 FILE *f1 = NULL, *f2 = NULL;
3299 int fd1 = -1, fd2 = -1;
3302 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3305 err = got_object_id_by_path(&tree_id1, repo, commit1,
3306 worktree->path_prefix);
3307 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3313 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3317 err = got_object_id_str(&id_str, commit_id1);
3321 if (asprintf(&label_orig, "%s: commit %s",
3322 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3323 err = got_error_from_errno("asprintf");
3329 f1 = got_opentemp();
3331 err = got_error_from_errno("got_opentemp");
3336 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3340 err = got_object_id_by_path(&tree_id2, repo, commit2,
3341 worktree->path_prefix);
3345 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3349 f2 = got_opentemp();
3351 err = got_error_from_errno("got_opentemp");
3355 fd1 = got_opentempfd();
3357 err = got_error_from_errno("got_opentempfd");
3361 fd2 = got_opentempfd();
3363 err = got_error_from_errno("got_opentempfd");
3367 cmc_arg.worktree = worktree;
3368 cmc_arg.fileindex = fileindex;
3369 cmc_arg.repo = repo;
3370 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3371 check_merge_conflicts, &cmc_arg, 0);
3375 arg.worktree = worktree;
3376 arg.fileindex = fileindex;
3377 arg.progress_cb = progress_cb;
3378 arg.progress_arg = progress_arg;
3379 arg.cancel_cb = cancel_cb;
3380 arg.cancel_arg = cancel_arg;
3381 arg.label_orig = label_orig;
3382 arg.commit_id2 = commit_id2;
3383 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3384 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3385 merge_file_cb, &arg, 1);
3386 sync_err = sync_fileindex(fileindex, fileindex_path);
3387 if (sync_err && err == NULL)
3391 got_object_commit_close(commit1);
3393 got_object_commit_close(commit2);
3395 got_object_tree_close(tree1);
3397 got_object_tree_close(tree2);
3398 if (f1 && fclose(f1) == EOF && err == NULL)
3399 err = got_error_from_errno("fclose");
3400 if (f2 && fclose(f2) == EOF && err == NULL)
3401 err = got_error_from_errno("fclose");
3402 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3403 err = got_error_from_errno("close");
3404 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3405 err = got_error_from_errno("close");
3410 const struct got_error *
3411 got_worktree_merge_files(struct got_worktree *worktree,
3412 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3413 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3414 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3416 const struct got_error *err, *unlockerr;
3417 char *fileindex_path = NULL;
3418 struct got_fileindex *fileindex = NULL;
3420 err = lock_worktree(worktree, LOCK_EX);
3424 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3428 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3433 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3434 commit_id2, repo, progress_cb, progress_arg,
3435 cancel_cb, cancel_arg);
3438 got_fileindex_free(fileindex);
3439 free(fileindex_path);
3440 unlockerr = lock_worktree(worktree, LOCK_SH);
3441 if (unlockerr && err == NULL)
3446 struct diff_dir_cb_arg {
3447 struct got_fileindex *fileindex;
3448 struct got_worktree *worktree;
3449 const char *status_path;
3450 size_t status_path_len;
3451 struct got_repository *repo;
3452 got_worktree_status_cb status_cb;
3454 got_cancel_cb cancel_cb;
3456 /* A pathlist containing per-directory pathlists of ignore patterns. */
3457 struct got_pathlist_head *ignores;
3458 int report_unchanged;
3462 static const struct got_error *
3463 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3464 int dirfd, const char *de_name,
3465 got_worktree_status_cb status_cb, void *status_arg,
3466 struct got_repository *repo, int report_unchanged)
3468 const struct got_error *err = NULL;
3469 unsigned char status = GOT_STATUS_NO_CHANGE;
3470 unsigned char staged_status;
3472 struct got_object_id blob_id, commit_id, staged_blob_id;
3473 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3474 struct got_object_id *staged_blob_idp = NULL;
3476 staged_status = get_staged_status(ie);
3477 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3481 if (status == GOT_STATUS_NO_CHANGE &&
3482 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3485 if (got_fileindex_entry_has_blob(ie))
3486 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3487 if (got_fileindex_entry_has_commit(ie))
3488 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3489 if (staged_status == GOT_STATUS_ADD ||
3490 staged_status == GOT_STATUS_MODIFY) {
3491 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3492 &staged_blob_id, ie);
3495 return (*status_cb)(status_arg, status, staged_status,
3496 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3499 static const struct got_error *
3500 status_old_new(void *arg, struct got_fileindex_entry *ie,
3501 struct dirent *de, const char *parent_path, int dirfd)
3503 const struct got_error *err = NULL;
3504 struct diff_dir_cb_arg *a = arg;
3507 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3508 return got_error(GOT_ERR_CANCELLED);
3510 if (got_path_cmp(parent_path, a->status_path,
3511 strlen(parent_path), a->status_path_len) != 0 &&
3512 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3515 if (parent_path[0]) {
3516 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3517 parent_path, de->d_name) == -1)
3518 return got_error_from_errno("asprintf");
3520 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3522 return got_error_from_errno("asprintf");
3525 err = report_file_status(ie, abspath, dirfd, de->d_name,
3526 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3531 static const struct got_error *
3532 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3534 struct diff_dir_cb_arg *a = arg;
3535 struct got_object_id blob_id, commit_id;
3536 unsigned char status;
3538 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3539 return got_error(GOT_ERR_CANCELLED);
3541 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3544 got_fileindex_entry_get_blob_id(&blob_id, ie);
3545 got_fileindex_entry_get_commit_id(&commit_id, ie);
3546 if (got_fileindex_entry_has_file_on_disk(ie))
3547 status = GOT_STATUS_MISSING;
3549 status = GOT_STATUS_DELETE;
3550 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3551 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3555 free_ignores(struct got_pathlist_head *ignores)
3557 struct got_pathlist_entry *pe;
3559 TAILQ_FOREACH(pe, ignores, entry) {
3560 struct got_pathlist_head *ignorelist = pe->data;
3562 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3564 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3567 static const struct got_error *
3568 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3570 const struct got_error *err = NULL;
3571 struct got_pathlist_entry *pe = NULL;
3572 struct got_pathlist_head *ignorelist;
3573 char *line = NULL, *pattern, *dirpath = NULL;
3574 size_t linesize = 0;
3577 ignorelist = calloc(1, sizeof(*ignorelist));
3578 if (ignorelist == NULL)
3579 return got_error_from_errno("calloc");
3580 TAILQ_INIT(ignorelist);
3582 while ((linelen = getline(&line, &linesize, f)) != -1) {
3583 if (linelen > 0 && line[linelen - 1] == '\n')
3584 line[linelen - 1] = '\0';
3586 /* Git's ignores may contain comments. */
3590 /* Git's negated patterns are not (yet?) supported. */
3594 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3596 err = got_error_from_errno("asprintf");
3599 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3604 err = got_error_from_errno("getline");
3608 dirpath = strdup(path);
3609 if (dirpath == NULL) {
3610 err = got_error_from_errno("strdup");
3613 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3616 if (err || pe == NULL) {
3618 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3624 match_path(const char *pattern, size_t pattern_len, const char *path,
3630 * Trailing slashes signify directories.
3631 * Append a * to make such patterns conform to fnmatch rules.
3633 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3634 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3635 return FNM_NOMATCH; /* XXX */
3637 return fnmatch(buf, path, flags);
3640 return fnmatch(pattern, path, flags);
3644 match_ignores(struct got_pathlist_head *ignores, const char *path)
3646 struct got_pathlist_entry *pe;
3648 /* Handle patterns which match in all directories. */
3649 TAILQ_FOREACH(pe, ignores, entry) {
3650 struct got_pathlist_head *ignorelist = pe->data;
3651 struct got_pathlist_entry *pi;
3653 TAILQ_FOREACH(pi, ignorelist, entry) {
3656 if (pi->path_len < 3 ||
3657 strncmp(pi->path, "**/", 3) != 0)
3661 if (match_path(pi->path + 3,
3662 pi->path_len - 3, p,
3663 FNM_PATHNAME | FNM_LEADING_DIR)) {
3664 /* Retry in next directory. */
3665 while (*p && *p != '/')
3677 * The ignores pathlist contains ignore lists from children before
3678 * parents, so we can find the most specific ignorelist by walking
3679 * ignores backwards.
3681 pe = TAILQ_LAST(ignores, got_pathlist_head);
3683 if (got_path_is_child(path, pe->path, pe->path_len)) {
3684 struct got_pathlist_head *ignorelist = pe->data;
3685 struct got_pathlist_entry *pi;
3686 TAILQ_FOREACH(pi, ignorelist, entry) {
3687 int flags = FNM_LEADING_DIR;
3688 if (strstr(pi->path, "/**/") == NULL)
3689 flags |= FNM_PATHNAME;
3690 if (match_path(pi->path, pi->path_len,
3696 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3702 static const struct got_error *
3703 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3704 const char *path, int dirfd, const char *ignores_filename)
3706 const struct got_error *err = NULL;
3709 FILE *ignoresfile = NULL;
3711 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3712 path[0] ? "/" : "", ignores_filename) == -1)
3713 return got_error_from_errno("asprintf");
3716 fd = openat(dirfd, ignores_filename,
3717 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3719 if (errno != ENOENT && errno != EACCES)
3720 err = got_error_from_errno2("openat",
3723 ignoresfile = fdopen(fd, "r");
3724 if (ignoresfile == NULL)
3725 err = got_error_from_errno2("fdopen",
3729 err = read_ignores(ignores, path, ignoresfile);
3733 ignoresfile = fopen(ignorespath, "re");
3734 if (ignoresfile == NULL) {
3735 if (errno != ENOENT && errno != EACCES)
3736 err = got_error_from_errno2("fopen",
3739 err = read_ignores(ignores, path, ignoresfile);
3742 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3743 err = got_error_from_errno2("fclose", path);
3744 if (fd != -1 && close(fd) == -1 && err == NULL)
3745 err = got_error_from_errno2("close", path);
3750 static const struct got_error *
3751 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3754 const struct got_error *err = NULL;
3755 struct diff_dir_cb_arg *a = arg;
3761 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3762 return got_error(GOT_ERR_CANCELLED);
3764 if (parent_path[0]) {
3765 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3766 return got_error_from_errno("asprintf");
3771 if (de->d_type == DT_DIR) {
3772 if (!a->no_ignores && ignore != NULL &&
3773 match_ignores(a->ignores, path))
3775 } else if (!match_ignores(a->ignores, path) &&
3776 got_path_is_child(path, a->status_path, a->status_path_len))
3777 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3778 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3784 static const struct got_error *
3785 status_traverse(void *arg, const char *path, int dirfd)
3787 const struct got_error *err = NULL;
3788 struct diff_dir_cb_arg *a = arg;
3793 err = add_ignores(a->ignores, a->worktree->root_path,
3794 path, dirfd, ".cvsignore");
3798 err = add_ignores(a->ignores, a->worktree->root_path, path,
3799 dirfd, ".gitignore");
3804 static const struct got_error *
3805 report_single_file_status(const char *path, const char *ondisk_path,
3806 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3807 void *status_arg, struct got_repository *repo, int report_unchanged,
3808 struct got_pathlist_head *ignores, int no_ignores)
3810 struct got_fileindex_entry *ie;
3813 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3815 return report_file_status(ie, ondisk_path, -1, NULL,
3816 status_cb, status_arg, repo, report_unchanged);
3818 if (lstat(ondisk_path, &sb) == -1) {
3819 if (errno != ENOENT)
3820 return got_error_from_errno2("lstat", ondisk_path);
3821 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3822 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3825 if (!no_ignores && match_ignores(ignores, path))
3828 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3829 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3830 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3835 static const struct got_error *
3836 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3837 const char *root_path, const char *path)
3839 const struct got_error *err;
3840 char *parent_path, *next_parent_path = NULL;
3842 err = add_ignores(ignores, root_path, "", -1,
3847 err = add_ignores(ignores, root_path, "", -1,
3852 err = got_path_dirname(&parent_path, path);
3854 if (err->code == GOT_ERR_BAD_PATH)
3855 return NULL; /* cannot traverse parent */
3859 err = add_ignores(ignores, root_path, parent_path, -1,
3863 err = add_ignores(ignores, root_path, parent_path, -1,
3867 err = got_path_dirname(&next_parent_path, parent_path);
3869 if (err->code == GOT_ERR_BAD_PATH)
3870 err = NULL; /* traversed everything */
3873 if (got_path_is_root_dir(parent_path))
3876 parent_path = next_parent_path;
3877 next_parent_path = NULL;
3881 free(next_parent_path);
3885 struct find_missing_children_args {
3886 const char *parent_path;
3888 struct got_pathlist_head *children;
3889 got_cancel_cb cancel_cb;
3893 static const struct got_error *
3894 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3896 const struct got_error *err = NULL;
3897 struct find_missing_children_args *a = arg;
3900 err = a->cancel_cb(a->cancel_arg);
3905 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3906 err = got_pathlist_append(a->children, ie->path, NULL);
3911 static const struct got_error *
3912 report_children(struct got_pathlist_head *children,
3913 struct got_worktree *worktree, struct got_fileindex *fileindex,
3914 struct got_repository *repo, int is_root_dir, int report_unchanged,
3915 struct got_pathlist_head *ignores, int no_ignores,
3916 got_worktree_status_cb status_cb, void *status_arg,
3917 got_cancel_cb cancel_cb, void *cancel_arg)
3919 const struct got_error *err = NULL;
3920 struct got_pathlist_entry *pe;
3921 char *ondisk_path = NULL;
3923 TAILQ_FOREACH(pe, children, entry) {
3925 err = cancel_cb(cancel_arg);
3930 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3931 !is_root_dir ? "/" : "", pe->path) == -1) {
3932 err = got_error_from_errno("asprintf");
3937 err = report_single_file_status(pe->path, ondisk_path,
3938 fileindex, status_cb, status_arg, repo, report_unchanged,
3939 ignores, no_ignores);
3951 static const struct got_error *
3952 worktree_status(struct got_worktree *worktree, const char *path,
3953 struct got_fileindex *fileindex, struct got_repository *repo,
3954 got_worktree_status_cb status_cb, void *status_arg,
3955 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3956 int report_unchanged)
3958 const struct got_error *err = NULL;
3960 struct got_fileindex_diff_dir_cb fdiff_cb;
3961 struct diff_dir_cb_arg arg;
3962 char *ondisk_path = NULL;
3963 struct got_pathlist_head ignores, missing_children;
3964 struct got_fileindex_entry *ie;
3966 TAILQ_INIT(&ignores);
3967 TAILQ_INIT(&missing_children);
3969 if (asprintf(&ondisk_path, "%s%s%s",
3970 worktree->root_path, path[0] ? "/" : "", path) == -1)
3971 return got_error_from_errno("asprintf");
3973 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3975 err = report_single_file_status(path, ondisk_path,
3976 fileindex, status_cb, status_arg, repo,
3977 report_unchanged, &ignores, no_ignores);
3980 struct find_missing_children_args fmca;
3981 fmca.parent_path = path;
3982 fmca.parent_len = strlen(path);
3983 fmca.children = &missing_children;
3984 fmca.cancel_cb = cancel_cb;
3985 fmca.cancel_arg = cancel_arg;
3986 err = got_fileindex_for_each_entry_safe(fileindex,
3987 find_missing_children, &fmca);
3992 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3994 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3995 !got_err_open_nofollow_on_symlink())
3996 err = got_error_from_errno2("open", ondisk_path);
3999 err = add_ignores_from_parent_paths(&ignores,
4000 worktree->root_path, ondisk_path);
4004 if (TAILQ_EMPTY(&missing_children)) {
4005 err = report_single_file_status(path,
4006 ondisk_path, fileindex,
4007 status_cb, status_arg, repo,
4008 report_unchanged, &ignores, no_ignores);
4012 err = report_children(&missing_children,
4013 worktree, fileindex, repo,
4014 (path[0] == '\0'), report_unchanged,
4015 &ignores, no_ignores,
4016 status_cb, status_arg,
4017 cancel_cb, cancel_arg);
4023 fdiff_cb.diff_old_new = status_old_new;
4024 fdiff_cb.diff_old = status_old;
4025 fdiff_cb.diff_new = status_new;
4026 fdiff_cb.diff_traverse = status_traverse;
4027 arg.fileindex = fileindex;
4028 arg.worktree = worktree;
4029 arg.status_path = path;
4030 arg.status_path_len = strlen(path);
4032 arg.status_cb = status_cb;
4033 arg.status_arg = status_arg;
4034 arg.cancel_cb = cancel_cb;
4035 arg.cancel_arg = cancel_arg;
4036 arg.report_unchanged = report_unchanged;
4037 arg.no_ignores = no_ignores;
4039 err = add_ignores_from_parent_paths(&ignores,
4040 worktree->root_path, path);
4044 arg.ignores = &ignores;
4045 err = got_fileindex_diff_dir(fileindex, fd,
4046 worktree->root_path, path, repo, &fdiff_cb, &arg);
4049 free_ignores(&ignores);
4050 if (fd != -1 && close(fd) == -1 && err == NULL)
4051 err = got_error_from_errno("close");
4056 const struct got_error *
4057 got_worktree_status(struct got_worktree *worktree,
4058 struct got_pathlist_head *paths, struct got_repository *repo,
4059 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4060 got_cancel_cb cancel_cb, void *cancel_arg)
4062 const struct got_error *err = NULL;
4063 char *fileindex_path = NULL;
4064 struct got_fileindex *fileindex = NULL;
4065 struct got_pathlist_entry *pe;
4067 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4071 TAILQ_FOREACH(pe, paths, entry) {
4072 err = worktree_status(worktree, pe->path, fileindex, repo,
4073 status_cb, status_arg, cancel_cb, cancel_arg,
4078 free(fileindex_path);
4079 got_fileindex_free(fileindex);
4083 const struct got_error *
4084 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4087 const struct got_error *err = NULL;
4088 char *resolved = NULL, *cwd = NULL, *path = NULL;
4091 char *abspath = NULL;
4092 char canonpath[PATH_MAX];
4096 cwd = getcwd(NULL, 0);
4098 return got_error_from_errno("getcwd");
4100 if (lstat(arg, &sb) == -1) {
4101 if (errno != ENOENT) {
4102 err = got_error_from_errno2("lstat", arg);
4107 if (S_ISLNK(sb.st_mode)) {
4109 * We cannot use realpath(3) with symlinks since we want to
4110 * operate on the symlink itself.
4111 * But we can make the path absolute, assuming it is relative
4112 * to the current working directory, and then canonicalize it.
4114 if (!got_path_is_absolute(arg)) {
4115 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4116 err = got_error_from_errno("asprintf");
4121 err = got_canonpath(abspath ? abspath : arg, canonpath,
4125 resolved = strdup(canonpath);
4126 if (resolved == NULL) {
4127 err = got_error_from_errno("strdup");
4131 resolved = realpath(arg, NULL);
4132 if (resolved == NULL) {
4133 if (errno != ENOENT) {
4134 err = got_error_from_errno2("realpath", arg);
4137 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4138 err = got_error_from_errno("asprintf");
4141 err = got_canonpath(abspath, canonpath,
4145 resolved = strdup(canonpath);
4146 if (resolved == NULL) {
4147 err = got_error_from_errno("strdup");
4153 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4154 strlen(got_worktree_get_root_path(worktree)))) {
4155 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4159 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4160 err = got_path_skip_common_ancestor(&path,
4161 got_worktree_get_root_path(worktree), resolved);
4167 err = got_error_from_errno("strdup");
4172 /* XXX status walk can't deal with trailing slash! */
4174 while (len > 0 && path[len - 1] == '/') {
4175 path[len - 1] = '\0';
4189 struct schedule_addition_args {
4190 struct got_worktree *worktree;
4191 struct got_fileindex *fileindex;
4192 got_worktree_checkout_cb progress_cb;
4194 struct got_repository *repo;
4197 static const struct got_error *
4198 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4199 const char *relpath, struct got_object_id *blob_id,
4200 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4201 int dirfd, const char *de_name)
4203 struct schedule_addition_args *a = arg;
4204 const struct got_error *err = NULL;
4205 struct got_fileindex_entry *ie;
4209 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4211 return got_error_from_errno("asprintf");
4213 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4215 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4219 /* Re-adding an existing entry is a no-op. */
4220 if (status == GOT_STATUS_ADD)
4222 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4227 if (status != GOT_STATUS_UNVERSIONED) {
4228 if (status == GOT_STATUS_NONEXISTENT)
4229 err = got_error_set_errno(ENOENT, ondisk_path);
4231 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4235 err = got_fileindex_entry_alloc(&ie, relpath);
4238 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4239 relpath, NULL, NULL, 1);
4241 got_fileindex_entry_free(ie);
4244 err = got_fileindex_entry_add(a->fileindex, ie);
4246 got_fileindex_entry_free(ie);
4253 if (status == GOT_STATUS_ADD)
4255 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4258 const struct got_error *
4259 got_worktree_schedule_add(struct got_worktree *worktree,
4260 struct got_pathlist_head *paths,
4261 got_worktree_checkout_cb progress_cb, void *progress_arg,
4262 struct got_repository *repo, int no_ignores)
4264 struct got_fileindex *fileindex = NULL;
4265 char *fileindex_path = NULL;
4266 const struct got_error *err = NULL, *sync_err, *unlockerr;
4267 struct got_pathlist_entry *pe;
4268 struct schedule_addition_args saa;
4270 err = lock_worktree(worktree, LOCK_EX);
4274 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4278 saa.worktree = worktree;
4279 saa.fileindex = fileindex;
4280 saa.progress_cb = progress_cb;
4281 saa.progress_arg = progress_arg;
4284 TAILQ_FOREACH(pe, paths, entry) {
4285 err = worktree_status(worktree, pe->path, fileindex, repo,
4286 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4290 sync_err = sync_fileindex(fileindex, fileindex_path);
4291 if (sync_err && err == NULL)
4294 free(fileindex_path);
4296 got_fileindex_free(fileindex);
4297 unlockerr = lock_worktree(worktree, LOCK_SH);
4298 if (unlockerr && err == NULL)
4303 struct schedule_deletion_args {
4304 struct got_worktree *worktree;
4305 struct got_fileindex *fileindex;
4306 got_worktree_delete_cb progress_cb;
4308 struct got_repository *repo;
4309 int delete_local_mods;
4311 int ignore_missing_paths;
4312 const char *status_codes;
4315 static const struct got_error *
4316 schedule_for_deletion(void *arg, unsigned char status,
4317 unsigned char staged_status, const char *relpath,
4318 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4319 struct got_object_id *commit_id, int dirfd, const char *de_name)
4321 struct schedule_deletion_args *a = arg;
4322 const struct got_error *err = NULL;
4323 struct got_fileindex_entry *ie = NULL;
4327 if (status == GOT_STATUS_NONEXISTENT) {
4328 if (a->ignore_missing_paths)
4330 return got_error_set_errno(ENOENT, relpath);
4333 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4335 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4337 staged_status = get_staged_status(ie);
4338 if (staged_status != GOT_STATUS_NO_CHANGE) {
4339 if (staged_status == GOT_STATUS_DELETE)
4341 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4344 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4346 return got_error_from_errno("asprintf");
4348 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4353 if (a->status_codes) {
4354 size_t ncodes = strlen(a->status_codes);
4356 for (i = 0; i < ncodes ; i++) {
4357 if (status == a->status_codes[i])
4361 /* Do not delete files in non-matching status. */
4365 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4366 a->status_codes[i] != GOT_STATUS_MISSING) {
4367 static char msg[64];
4368 snprintf(msg, sizeof(msg),
4369 "invalid status code '%c'", a->status_codes[i]);
4370 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4375 if (status != GOT_STATUS_NO_CHANGE) {
4376 if (status == GOT_STATUS_DELETE)
4378 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4379 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4382 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4383 err = got_error_set_errno(ENOENT, relpath);
4386 if (status != GOT_STATUS_MODIFY &&
4387 status != GOT_STATUS_MISSING) {
4388 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4393 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4397 if (unlinkat(dirfd, de_name, 0) == -1) {
4398 err = got_error_from_errno2("unlinkat",
4402 } else if (unlink(ondisk_path) == -1) {
4403 err = got_error_from_errno2("unlink", ondisk_path);
4407 root_len = strlen(a->worktree->root_path);
4410 err = got_path_dirname(&parent, ondisk_path);
4414 ondisk_path = parent;
4415 if (rmdir(ondisk_path) == -1) {
4416 if (errno != ENOTEMPTY)
4417 err = got_error_from_errno2("rmdir",
4421 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4422 strlen(ondisk_path), root_len) != 0);
4425 got_fileindex_entry_mark_deleted_from_disk(ie);
4430 if (status == GOT_STATUS_DELETE)
4432 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4433 staged_status, relpath);
4436 const struct got_error *
4437 got_worktree_schedule_delete(struct got_worktree *worktree,
4438 struct got_pathlist_head *paths, int delete_local_mods,
4439 const char *status_codes,
4440 got_worktree_delete_cb progress_cb, void *progress_arg,
4441 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4443 struct got_fileindex *fileindex = NULL;
4444 char *fileindex_path = NULL;
4445 const struct got_error *err = NULL, *sync_err, *unlockerr;
4446 struct got_pathlist_entry *pe;
4447 struct schedule_deletion_args sda;
4449 err = lock_worktree(worktree, LOCK_EX);
4453 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4457 sda.worktree = worktree;
4458 sda.fileindex = fileindex;
4459 sda.progress_cb = progress_cb;
4460 sda.progress_arg = progress_arg;
4462 sda.delete_local_mods = delete_local_mods;
4463 sda.keep_on_disk = keep_on_disk;
4464 sda.ignore_missing_paths = ignore_missing_paths;
4465 sda.status_codes = status_codes;
4467 TAILQ_FOREACH(pe, paths, entry) {
4468 err = worktree_status(worktree, pe->path, fileindex, repo,
4469 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4473 sync_err = sync_fileindex(fileindex, fileindex_path);
4474 if (sync_err && err == NULL)
4477 free(fileindex_path);
4479 got_fileindex_free(fileindex);
4480 unlockerr = lock_worktree(worktree, LOCK_SH);
4481 if (unlockerr && err == NULL)
4486 static const struct got_error *
4487 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4489 const struct got_error *err = NULL;
4491 size_t linesize = 0, n;
4494 linelen = getline(&line, &linesize, infile);
4495 if (linelen == -1) {
4496 if (ferror(infile)) {
4497 err = got_error_from_errno("getline");
4503 n = fwrite(line, 1, linelen, outfile);
4505 err = got_ferror(outfile, GOT_ERR_IO);
4510 n = fwrite(line, 1, linelen, rejectfile);
4512 err = got_ferror(rejectfile, GOT_ERR_IO);
4519 static const struct got_error *
4520 skip_one_line(FILE *f)
4523 size_t linesize = 0;
4526 linelen = getline(&line, &linesize, f);
4527 if (linelen == -1) {
4529 return got_error_from_errno("getline");
4536 static const struct got_error *
4537 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4538 int start_old, int end_old, int start_new, int end_new,
4539 FILE *outfile, FILE *rejectfile)
4541 const struct got_error *err;
4543 /* Copy old file's lines leading up to patch. */
4544 while (!feof(f1) && *line_cur1 < start_old) {
4545 err = copy_one_line(f1, outfile, NULL);
4550 /* Skip new file's lines leading up to patch. */
4551 while (!feof(f2) && *line_cur2 < start_new) {
4553 err = copy_one_line(f2, NULL, rejectfile);
4555 err = skip_one_line(f2);
4560 /* Copy patched lines. */
4561 while (!feof(f2) && *line_cur2 <= end_new) {
4562 err = copy_one_line(f2, outfile, NULL);
4567 /* Skip over old file's replaced lines. */
4568 while (!feof(f1) && *line_cur1 <= end_old) {
4570 err = copy_one_line(f1, NULL, rejectfile);
4572 err = skip_one_line(f1);
4581 static const struct got_error *
4582 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4583 FILE *outfile, FILE *rejectfile)
4585 const struct got_error *err;
4588 /* Copy old file's lines until EOF. */
4590 err = copy_one_line(f1, outfile, NULL);
4597 /* Copy new file's lines until EOF. */
4599 err = copy_one_line(f2, NULL, rejectfile);
4609 static const struct got_error *
4610 apply_or_reject_change(int *choice, int *nchunks_used,
4611 struct diff_result *diff_result, int n,
4612 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4613 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4614 got_worktree_patch_cb patch_cb, void *patch_arg)
4616 const struct got_error *err = NULL;
4617 struct diff_chunk_context cc = {};
4618 int start_old, end_old, start_new, end_new;
4620 struct diff_output_unidiff_state *diff_state;
4621 struct diff_input_info diff_info;
4624 *choice = GOT_PATCH_CHOICE_NONE;
4626 /* Get changed line numbers without context lines for copy_change(). */
4627 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4628 start_old = cc.left.start;
4629 end_old = cc.left.end;
4630 start_new = cc.right.start;
4631 end_new = cc.right.end;
4633 /* Get the same change with context lines for display. */
4634 memset(&cc, 0, sizeof(cc));
4635 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4637 memset(&diff_info, 0, sizeof(diff_info));
4638 diff_info.left_path = relpath;
4639 diff_info.right_path = relpath;
4641 diff_state = diff_output_unidiff_state_alloc();
4642 if (diff_state == NULL)
4643 return got_error_set_errno(ENOMEM,
4644 "diff_output_unidiff_state_alloc");
4646 hunkfile = got_opentemp();
4647 if (hunkfile == NULL) {
4648 err = got_error_from_errno("got_opentemp");
4652 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4654 if (rc != DIFF_RC_OK) {
4655 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4659 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4660 err = got_ferror(hunkfile, GOT_ERR_IO);
4664 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4665 hunkfile, changeno, nchanges);
4670 case GOT_PATCH_CHOICE_YES:
4671 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4672 end_old, start_new, end_new, outfile, rejectfile);
4674 case GOT_PATCH_CHOICE_NO:
4675 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4676 end_old, start_new, end_new, rejectfile, outfile);
4678 case GOT_PATCH_CHOICE_QUIT:
4681 err = got_error(GOT_ERR_PATCH_CHOICE);
4685 diff_output_unidiff_state_free(diff_state);
4686 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4687 err = got_error_from_errno("fclose");
4691 struct revert_file_args {
4692 struct got_worktree *worktree;
4693 struct got_fileindex *fileindex;
4694 got_worktree_checkout_cb progress_cb;
4696 got_worktree_patch_cb patch_cb;
4698 struct got_repository *repo;
4699 int unlink_added_files;
4700 struct got_pathlist_head *added_files_to_unlink;
4703 static const struct got_error *
4704 create_patched_content(char **path_outfile, int reverse_patch,
4705 struct got_object_id *blob_id, const char *path2,
4706 int dirfd2, const char *de_name2,
4707 const char *relpath, struct got_repository *repo,
4708 got_worktree_patch_cb patch_cb, void *patch_arg)
4710 const struct got_error *err, *free_err;
4711 struct got_blob_object *blob = NULL;
4712 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4713 int fd = -1, fd2 = -1;
4714 char link_target[PATH_MAX];
4715 ssize_t link_len = 0;
4716 char *path1 = NULL, *id_str = NULL;
4718 struct got_diffreg_result *diffreg_result = NULL;
4719 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4720 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4722 *path_outfile = NULL;
4724 err = got_object_id_str(&id_str, blob_id);
4729 fd2 = openat(dirfd2, de_name2,
4730 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4732 if (!got_err_open_nofollow_on_symlink()) {
4733 err = got_error_from_errno2("openat", path2);
4736 link_len = readlinkat(dirfd2, de_name2,
4737 link_target, sizeof(link_target));
4738 if (link_len == -1) {
4739 return got_error_from_errno2("readlinkat",
4742 sb2.st_mode = S_IFLNK;
4743 sb2.st_size = link_len;
4746 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4748 if (!got_err_open_nofollow_on_symlink()) {
4749 err = got_error_from_errno2("open", path2);
4752 link_len = readlink(path2, link_target,
4753 sizeof(link_target));
4755 return got_error_from_errno2("readlink", path2);
4756 sb2.st_mode = S_IFLNK;
4757 sb2.st_size = link_len;
4761 if (fstat(fd2, &sb2) == -1) {
4762 err = got_error_from_errno2("fstat", path2);
4766 f2 = fdopen(fd2, "r");
4768 err = got_error_from_errno2("fdopen", path2);
4774 f2 = got_opentemp();
4776 err = got_error_from_errno2("got_opentemp", path2);
4779 n = fwrite(link_target, 1, link_len, f2);
4780 if (n != link_len) {
4781 err = got_ferror(f2, GOT_ERR_IO);
4784 if (fflush(f2) == EOF) {
4785 err = got_error_from_errno("fflush");
4791 fd = got_opentempfd();
4793 err = got_error_from_errno("got_opentempfd");
4797 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4801 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4805 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4809 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4810 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4814 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4819 if (fseek(f1, 0L, SEEK_SET) == -1)
4820 return got_ferror(f1, GOT_ERR_IO);
4821 if (fseek(f2, 0L, SEEK_SET) == -1)
4822 return got_ferror(f2, GOT_ERR_IO);
4824 /* Count the number of actual changes in the diff result. */
4825 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4826 struct diff_chunk_context cc = {};
4827 diff_chunk_context_load_change(&cc, &nchunks_used,
4828 diffreg_result->result, n, 0);
4831 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4833 err = apply_or_reject_change(&choice, &nchunks_used,
4834 diffreg_result->result, n, relpath, f1, f2,
4835 &line_cur1, &line_cur2,
4836 reverse_patch ? NULL : outfile,
4837 reverse_patch ? outfile : NULL,
4838 ++i, nchanges, patch_cb, patch_arg);
4841 if (choice == GOT_PATCH_CHOICE_YES)
4843 else if (choice == GOT_PATCH_CHOICE_QUIT)
4847 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4848 reverse_patch ? NULL : outfile,
4849 reverse_patch ? outfile : NULL);
4853 if (!S_ISLNK(sb2.st_mode)) {
4856 mode = apply_umask(sb2.st_mode);
4857 if (fchmod(fileno(outfile), mode) == -1) {
4858 err = got_error_from_errno2("fchmod", path2);
4865 if (fd != -1 && close(fd) == -1 && err == NULL)
4866 err = got_error_from_errno("close");
4868 got_object_blob_close(blob);
4869 free_err = got_diffreg_result_free(diffreg_result);
4872 if (f1 && fclose(f1) == EOF && err == NULL)
4873 err = got_error_from_errno2("fclose", path1);
4874 if (f2 && fclose(f2) == EOF && err == NULL)
4875 err = got_error_from_errno2("fclose", path2);
4876 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4877 err = got_error_from_errno2("close", path2);
4878 if (outfile && fclose(outfile) == EOF && err == NULL)
4879 err = got_error_from_errno2("fclose", *path_outfile);
4880 if (path1 && unlink(path1) == -1 && err == NULL)
4881 err = got_error_from_errno2("unlink", path1);
4882 if (err || !have_content) {
4883 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4884 err = got_error_from_errno2("unlink", *path_outfile);
4885 free(*path_outfile);
4886 *path_outfile = NULL;
4892 static const struct got_error *
4893 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4894 const char *relpath, struct got_object_id *blob_id,
4895 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4896 int dirfd, const char *de_name)
4898 struct revert_file_args *a = arg;
4899 const struct got_error *err = NULL;
4900 char *parent_path = NULL;
4901 struct got_fileindex_entry *ie;
4902 struct got_commit_object *base_commit = NULL;
4903 struct got_tree_object *tree = NULL;
4904 struct got_object_id *tree_id = NULL;
4905 const struct got_tree_entry *te = NULL;
4906 char *tree_path = NULL, *te_name;
4907 char *ondisk_path = NULL, *path_content = NULL;
4908 struct got_blob_object *blob = NULL;
4911 /* Reverting a staged deletion is a no-op. */
4912 if (status == GOT_STATUS_DELETE &&
4913 staged_status != GOT_STATUS_NO_CHANGE)
4916 if (status == GOT_STATUS_UNVERSIONED)
4917 return (*a->progress_cb)(a->progress_arg,
4918 GOT_STATUS_UNVERSIONED, relpath);
4920 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4922 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4924 /* Construct in-repository path of tree which contains this blob. */
4925 err = got_path_dirname(&parent_path, ie->path);
4927 if (err->code != GOT_ERR_BAD_PATH)
4929 parent_path = strdup("/");
4930 if (parent_path == NULL) {
4931 err = got_error_from_errno("strdup");
4935 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4936 tree_path = strdup(parent_path);
4937 if (tree_path == NULL) {
4938 err = got_error_from_errno("strdup");
4942 if (got_path_is_root_dir(parent_path)) {
4943 tree_path = strdup(a->worktree->path_prefix);
4944 if (tree_path == NULL) {
4945 err = got_error_from_errno("strdup");
4949 if (asprintf(&tree_path, "%s/%s",
4950 a->worktree->path_prefix, parent_path) == -1) {
4951 err = got_error_from_errno("asprintf");
4957 err = got_object_open_as_commit(&base_commit, a->repo,
4958 a->worktree->base_commit_id);
4962 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4964 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4965 (status == GOT_STATUS_ADD ||
4966 staged_status == GOT_STATUS_ADD)))
4969 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4973 err = got_path_basename(&te_name, ie->path);
4977 te = got_object_tree_find_entry(tree, te_name);
4979 if (te == NULL && status != GOT_STATUS_ADD &&
4980 staged_status != GOT_STATUS_ADD) {
4981 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4987 case GOT_STATUS_ADD:
4989 int choice = GOT_PATCH_CHOICE_NONE;
4990 err = (*a->patch_cb)(&choice, a->patch_arg,
4991 status, ie->path, NULL, 1, 1);
4994 if (choice != GOT_PATCH_CHOICE_YES)
4997 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5001 got_fileindex_entry_remove(a->fileindex, ie);
5002 if (a->unlink_added_files) {
5003 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5005 if (a->added_files_to_unlink) {
5006 struct got_pathlist_entry *pe;
5008 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5010 if (got_path_cmp(pe->path, relpath,
5011 pe->path_len, strlen(relpath)))
5019 if (asprintf(&ondisk_path, "%s/%s",
5020 got_worktree_get_root_path(a->worktree),
5022 err = got_error_from_errno("asprintf");
5025 if (unlink(ondisk_path) == -1) {
5026 err = got_error_from_errno2("unlink",
5033 case GOT_STATUS_DELETE:
5035 int choice = GOT_PATCH_CHOICE_NONE;
5036 err = (*a->patch_cb)(&choice, a->patch_arg,
5037 status, ie->path, NULL, 1, 1);
5040 if (choice != GOT_PATCH_CHOICE_YES)
5044 case GOT_STATUS_MODIFY:
5045 case GOT_STATUS_MODE_CHANGE:
5046 case GOT_STATUS_CONFLICT:
5047 case GOT_STATUS_MISSING: {
5048 struct got_object_id id;
5049 if (staged_status == GOT_STATUS_ADD ||
5050 staged_status == GOT_STATUS_MODIFY)
5051 got_fileindex_entry_get_staged_blob_id(&id, ie);
5053 got_fileindex_entry_get_blob_id(&id, ie);
5054 fd = got_opentempfd();
5056 err = got_error_from_errno("got_opentempfd");
5060 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5064 if (asprintf(&ondisk_path, "%s/%s",
5065 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5066 err = got_error_from_errno("asprintf");
5070 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5071 status == GOT_STATUS_CONFLICT)) {
5072 int is_bad_symlink = 0;
5073 err = create_patched_content(&path_content, 1, &id,
5074 ondisk_path, dirfd, de_name, ie->path, a->repo,
5075 a->patch_cb, a->patch_arg);
5076 if (err || path_content == NULL)
5078 if (te && S_ISLNK(te->mode)) {
5079 if (unlink(path_content) == -1) {
5080 err = got_error_from_errno2("unlink",
5084 err = install_symlink(&is_bad_symlink,
5085 a->worktree, ondisk_path, ie->path,
5086 blob, 0, 1, 0, 0, a->repo,
5087 a->progress_cb, a->progress_arg);
5089 if (rename(path_content, ondisk_path) == -1) {
5090 err = got_error_from_errno3("rename",
5091 path_content, ondisk_path);
5096 int is_bad_symlink = 0;
5097 if (te && S_ISLNK(te->mode)) {
5098 err = install_symlink(&is_bad_symlink,
5099 a->worktree, ondisk_path, ie->path,
5100 blob, 0, 1, 0, 0, a->repo,
5101 a->progress_cb, a->progress_arg);
5103 err = install_blob(a->worktree, ondisk_path,
5105 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5106 got_fileindex_perms_to_st(ie), blob,
5107 0, 1, 0, 0, a->repo,
5108 a->progress_cb, a->progress_arg);
5112 if (status == GOT_STATUS_DELETE ||
5113 status == GOT_STATUS_MODE_CHANGE) {
5114 err = got_fileindex_entry_update(ie,
5115 a->worktree->root_fd, relpath,
5117 a->worktree->base_commit_id->sha1, 1);
5121 if (is_bad_symlink) {
5122 got_fileindex_entry_filetype_set(ie,
5123 GOT_FILEIDX_MODE_BAD_SYMLINK);
5136 if (fd != -1 && close(fd) == -1 && err == NULL)
5137 err = got_error_from_errno("close");
5139 got_object_blob_close(blob);
5141 got_object_tree_close(tree);
5144 got_object_commit_close(base_commit);
5148 const struct got_error *
5149 got_worktree_revert(struct got_worktree *worktree,
5150 struct got_pathlist_head *paths,
5151 got_worktree_checkout_cb progress_cb, void *progress_arg,
5152 got_worktree_patch_cb patch_cb, void *patch_arg,
5153 struct got_repository *repo)
5155 struct got_fileindex *fileindex = NULL;
5156 char *fileindex_path = NULL;
5157 const struct got_error *err = NULL, *unlockerr = NULL;
5158 const struct got_error *sync_err = NULL;
5159 struct got_pathlist_entry *pe;
5160 struct revert_file_args rfa;
5162 err = lock_worktree(worktree, LOCK_EX);
5166 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5170 rfa.worktree = worktree;
5171 rfa.fileindex = fileindex;
5172 rfa.progress_cb = progress_cb;
5173 rfa.progress_arg = progress_arg;
5174 rfa.patch_cb = patch_cb;
5175 rfa.patch_arg = patch_arg;
5177 rfa.unlink_added_files = 0;
5178 TAILQ_FOREACH(pe, paths, entry) {
5179 err = worktree_status(worktree, pe->path, fileindex, repo,
5180 revert_file, &rfa, NULL, NULL, 1, 0);
5184 sync_err = sync_fileindex(fileindex, fileindex_path);
5185 if (sync_err && err == NULL)
5188 free(fileindex_path);
5190 got_fileindex_free(fileindex);
5191 unlockerr = lock_worktree(worktree, LOCK_SH);
5192 if (unlockerr && err == NULL)
5198 free_commitable(struct got_commitable *ct)
5201 free(ct->in_repo_path);
5202 free(ct->ondisk_path);
5204 free(ct->base_blob_id);
5205 free(ct->staged_blob_id);
5206 free(ct->base_commit_id);
5210 struct collect_commitables_arg {
5211 struct got_pathlist_head *commitable_paths;
5212 struct got_repository *repo;
5213 struct got_worktree *worktree;
5214 struct got_fileindex *fileindex;
5215 int have_staged_files;
5216 int allow_bad_symlinks;
5217 int diff_header_shown;
5218 int commit_conflicts;
5225 * Create a file which contains the target path of a symlink so we can feed
5226 * it as content to the diff engine.
5228 static const struct got_error *
5229 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5230 const char *abspath)
5232 const struct got_error *err = NULL;
5233 char target_path[PATH_MAX];
5234 ssize_t target_len, outlen;
5239 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5240 if (target_len == -1)
5241 return got_error_from_errno2("readlinkat", abspath);
5243 target_len = readlink(abspath, target_path, PATH_MAX);
5244 if (target_len == -1)
5245 return got_error_from_errno2("readlink", abspath);
5248 *fd = got_opentempfd();
5250 return got_error_from_errno("got_opentempfd");
5252 outlen = write(*fd, target_path, target_len);
5254 err = got_error_from_errno("got_opentempfd");
5258 if (lseek(*fd, 0, SEEK_SET) == -1) {
5259 err = got_error_from_errno2("lseek", abspath);
5270 static const struct got_error *
5271 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5272 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5273 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5275 const struct got_error *err = NULL;
5276 struct got_blob_object *blob1 = NULL;
5277 int fd = -1, fd1 = -1, fd2 = -1;
5278 FILE *ondisk_file = NULL;
5279 char *label1 = NULL;
5283 char *id_str = NULL;
5285 memset(&sb, 0, sizeof(sb));
5288 if (ct->staged_status != GOT_STATUS_MODIFY &&
5289 ct->staged_status != GOT_STATUS_ADD &&
5290 ct->staged_status != GOT_STATUS_DELETE)
5293 if (ct->status != GOT_STATUS_MODIFY &&
5294 ct->status != GOT_STATUS_ADD &&
5295 ct->status != GOT_STATUS_DELETE &&
5296 ct->status != GOT_STATUS_CONFLICT)
5300 err = got_opentemp_truncate(f1);
5302 return got_error_from_errno("got_opentemp_truncate");
5303 err = got_opentemp_truncate(f2);
5305 return got_error_from_errno("got_opentemp_truncate");
5307 if (!*diff_header_shown) {
5308 err = got_object_id_str(&id_str, worktree->base_commit_id);
5311 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5312 got_worktree_get_root_path(worktree));
5313 fprintf(diff_outfile, "commit - %s\n", id_str);
5314 fprintf(diff_outfile, "path + %s%s\n",
5315 got_worktree_get_root_path(worktree),
5316 diff_staged ? " (staged changes)" : "");
5317 *diff_header_shown = 1;
5321 const char *label1 = NULL, *label2 = NULL;
5322 switch (ct->staged_status) {
5323 case GOT_STATUS_MODIFY:
5327 case GOT_STATUS_ADD:
5330 case GOT_STATUS_DELETE:
5334 return got_error(GOT_ERR_FILE_STATUS);
5336 fd1 = got_opentempfd();
5338 err = got_error_from_errno("got_opentempfd");
5341 fd2 = got_opentempfd();
5343 err = got_error_from_errno("got_opentempfd");
5346 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5347 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5348 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5349 NULL, repo, diff_outfile);
5353 fd1 = got_opentempfd();
5355 err = got_error_from_errno("got_opentempfd");
5359 if (ct->status != GOT_STATUS_ADD) {
5360 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5366 if (ct->status != GOT_STATUS_DELETE) {
5368 fd = openat(dirfd, de_name,
5369 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5371 if (!got_err_open_nofollow_on_symlink()) {
5372 err = got_error_from_errno2("openat",
5376 err = get_symlink_target_file(&fd, dirfd,
5377 de_name, ct->ondisk_path);
5382 fd = open(ct->ondisk_path,
5383 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5385 if (!got_err_open_nofollow_on_symlink()) {
5386 err = got_error_from_errno2("open",
5390 err = get_symlink_target_file(&fd, dirfd,
5391 de_name, ct->ondisk_path);
5396 if (fstatat(fd, ct->ondisk_path, &sb,
5397 AT_SYMLINK_NOFOLLOW) == -1) {
5398 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5401 ondisk_file = fdopen(fd, "r");
5402 if (ondisk_file == NULL) {
5403 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5411 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5417 err = got_diff_blob_file(blob1, f1, size1, label1,
5418 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5419 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5421 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5422 err = got_error_from_errno("close");
5423 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5424 err = got_error_from_errno("close");
5426 got_object_blob_close(blob1);
5427 if (fd != -1 && close(fd) == -1 && err == NULL)
5428 err = got_error_from_errno("close");
5429 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5430 err = got_error_from_errno("fclose");
5434 static const struct got_error *
5435 collect_commitables(void *arg, unsigned char status,
5436 unsigned char staged_status, const char *relpath,
5437 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5438 struct got_object_id *commit_id, int dirfd, const char *de_name)
5440 struct collect_commitables_arg *a = arg;
5441 const struct got_error *err = NULL;
5442 struct got_commitable *ct = NULL;
5443 struct got_pathlist_entry *new = NULL;
5444 char *parent_path = NULL, *path = NULL;
5447 if (a->have_staged_files) {
5448 if (staged_status != GOT_STATUS_MODIFY &&
5449 staged_status != GOT_STATUS_ADD &&
5450 staged_status != GOT_STATUS_DELETE)
5453 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5454 printf("C %s\n", relpath);
5455 return got_error(GOT_ERR_COMMIT_CONFLICT);
5458 if (status != GOT_STATUS_MODIFY &&
5459 status != GOT_STATUS_MODE_CHANGE &&
5460 status != GOT_STATUS_ADD &&
5461 status != GOT_STATUS_DELETE &&
5462 status != GOT_STATUS_CONFLICT)
5466 if (asprintf(&path, "/%s", relpath) == -1) {
5467 err = got_error_from_errno("asprintf");
5470 if (strcmp(path, "/") == 0) {
5471 parent_path = strdup("");
5472 if (parent_path == NULL)
5473 return got_error_from_errno("strdup");
5475 err = got_path_dirname(&parent_path, path);
5480 ct = calloc(1, sizeof(*ct));
5482 err = got_error_from_errno("calloc");
5486 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5488 err = got_error_from_errno("asprintf");
5492 if (staged_status == GOT_STATUS_ADD ||
5493 staged_status == GOT_STATUS_MODIFY) {
5494 struct got_fileindex_entry *ie;
5495 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5496 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5497 case GOT_FILEIDX_MODE_REGULAR_FILE:
5498 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5501 case GOT_FILEIDX_MODE_SYMLINK:
5505 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5508 ct->mode |= got_fileindex_entry_perms_get(ie);
5509 } else if (status != GOT_STATUS_DELETE &&
5510 staged_status != GOT_STATUS_DELETE) {
5512 if (fstatat(dirfd, de_name, &sb,
5513 AT_SYMLINK_NOFOLLOW) == -1) {
5514 err = got_error_from_errno2("fstatat",
5518 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5519 err = got_error_from_errno2("lstat", ct->ondisk_path);
5522 ct->mode = sb.st_mode;
5525 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5526 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5528 err = got_error_from_errno("asprintf");
5532 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5533 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5535 char target_path[PATH_MAX];
5537 target_len = readlink(ct->ondisk_path, target_path,
5538 sizeof(target_path));
5539 if (target_len == -1) {
5540 err = got_error_from_errno2("readlink",
5544 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5545 target_len, ct->ondisk_path, a->worktree->root_path);
5548 if (is_bad_symlink) {
5549 err = got_error_path(ct->ondisk_path,
5550 GOT_ERR_BAD_SYMLINK);
5556 ct->status = status;
5557 ct->staged_status = staged_status;
5558 ct->blob_id = NULL; /* will be filled in when blob gets created */
5559 if (ct->status != GOT_STATUS_ADD &&
5560 ct->staged_status != GOT_STATUS_ADD) {
5561 ct->base_blob_id = got_object_id_dup(blob_id);
5562 if (ct->base_blob_id == NULL) {
5563 err = got_error_from_errno("got_object_id_dup");
5566 ct->base_commit_id = got_object_id_dup(commit_id);
5567 if (ct->base_commit_id == NULL) {
5568 err = got_error_from_errno("got_object_id_dup");
5572 if (ct->staged_status == GOT_STATUS_ADD ||
5573 ct->staged_status == GOT_STATUS_MODIFY) {
5574 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5575 if (ct->staged_blob_id == NULL) {
5576 err = got_error_from_errno("got_object_id_dup");
5580 ct->path = strdup(path);
5581 if (ct->path == NULL) {
5582 err = got_error_from_errno("strdup");
5585 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5589 if (a->diff_outfile && ct && new != NULL) {
5590 err = append_ct_diff(ct, &a->diff_header_shown,
5591 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5592 a->have_staged_files, a->repo, a->worktree);
5597 if (ct && (err || new == NULL))
5598 free_commitable(ct);
5604 static const struct got_error *write_tree(struct got_object_id **, int *,
5605 struct got_tree_object *, const char *, struct got_pathlist_head *,
5606 got_worktree_status_cb status_cb, void *status_arg,
5607 struct got_repository *);
5609 static const struct got_error *
5610 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5611 struct got_tree_entry *te, const char *parent_path,
5612 struct got_pathlist_head *commitable_paths,
5613 got_worktree_status_cb status_cb, void *status_arg,
5614 struct got_repository *repo)
5616 const struct got_error *err = NULL;
5617 struct got_tree_object *subtree;
5620 if (asprintf(&subpath, "%s%s%s", parent_path,
5621 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5622 return got_error_from_errno("asprintf");
5624 err = got_object_open_as_tree(&subtree, repo, &te->id);
5628 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5629 commitable_paths, status_cb, status_arg, repo);
5630 got_object_tree_close(subtree);
5635 static const struct got_error *
5636 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5638 const struct got_error *err = NULL;
5639 char *ct_parent_path = NULL;
5643 if (strchr(ct->in_repo_path, '/') == NULL) {
5644 *match = got_path_is_root_dir(path);
5648 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5651 *match = (strcmp(path, ct_parent_path) == 0);
5652 free(ct_parent_path);
5657 get_ct_file_mode(struct got_commitable *ct)
5659 if (S_ISLNK(ct->mode))
5662 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5665 static const struct got_error *
5666 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5667 struct got_tree_entry *te, struct got_commitable *ct)
5669 const struct got_error *err = NULL;
5673 err = got_object_tree_entry_dup(new_te, te);
5677 (*new_te)->mode = get_ct_file_mode(ct);
5679 if (ct->staged_status == GOT_STATUS_MODIFY)
5680 memcpy(&(*new_te)->id, ct->staged_blob_id,
5681 sizeof((*new_te)->id));
5683 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5685 if (err && *new_te) {
5692 static const struct got_error *
5693 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5694 struct got_commitable *ct)
5696 const struct got_error *err = NULL;
5697 char *ct_name = NULL;
5701 *new_te = calloc(1, sizeof(**new_te));
5702 if (*new_te == NULL)
5703 return got_error_from_errno("calloc");
5705 err = got_path_basename(&ct_name, ct->path);
5708 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5709 sizeof((*new_te)->name)) {
5710 err = got_error(GOT_ERR_NO_SPACE);
5714 (*new_te)->mode = get_ct_file_mode(ct);
5716 if (ct->staged_status == GOT_STATUS_ADD)
5717 memcpy(&(*new_te)->id, ct->staged_blob_id,
5718 sizeof((*new_te)->id));
5720 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5723 if (err && *new_te) {
5730 static const struct got_error *
5731 insert_tree_entry(struct got_tree_entry *new_te,
5732 struct got_pathlist_head *paths)
5734 const struct got_error *err = NULL;
5735 struct got_pathlist_entry *new_pe;
5737 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5741 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5745 static const struct got_error *
5746 report_ct_status(struct got_commitable *ct,
5747 got_worktree_status_cb status_cb, void *status_arg)
5749 const char *ct_path = ct->path;
5750 unsigned char status;
5752 if (status_cb == NULL) /* no commit progress output desired */
5755 while (ct_path[0] == '/')
5758 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5759 status = ct->staged_status;
5761 status = ct->status;
5763 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5764 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5767 static const struct got_error *
5768 match_modified_subtree(int *modified, struct got_tree_entry *te,
5769 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5771 const struct got_error *err = NULL;
5772 struct got_pathlist_entry *pe;
5777 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5778 got_path_is_root_dir(base_tree_path) ? "" : "/",
5780 return got_error_from_errno("asprintf");
5782 TAILQ_FOREACH(pe, commitable_paths, entry) {
5783 struct got_commitable *ct = pe->data;
5784 *modified = got_path_is_child(ct->in_repo_path, te_path,
5794 static const struct got_error *
5795 match_deleted_or_modified_ct(struct got_commitable **ctp,
5796 struct got_tree_entry *te, const char *base_tree_path,
5797 struct got_pathlist_head *commitable_paths)
5799 const struct got_error *err = NULL;
5800 struct got_pathlist_entry *pe;
5804 TAILQ_FOREACH(pe, commitable_paths, entry) {
5805 struct got_commitable *ct = pe->data;
5806 char *ct_name = NULL;
5809 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5810 if (ct->status != GOT_STATUS_MODIFY &&
5811 ct->status != GOT_STATUS_MODE_CHANGE &&
5812 ct->status != GOT_STATUS_DELETE &&
5813 ct->status != GOT_STATUS_CONFLICT)
5816 if (ct->staged_status != GOT_STATUS_MODIFY &&
5817 ct->staged_status != GOT_STATUS_DELETE)
5821 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5824 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5830 err = got_path_basename(&ct_name, pe->path);
5834 if (strcmp(te->name, ct_name) != 0) {
5847 static const struct got_error *
5848 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5849 const char *child_path, const char *path_base_tree,
5850 struct got_pathlist_head *commitable_paths,
5851 got_worktree_status_cb status_cb, void *status_arg,
5852 struct got_repository *repo)
5854 const struct got_error *err = NULL;
5855 struct got_tree_entry *new_te;
5857 struct got_object_id *id = NULL;
5862 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5863 got_path_is_root_dir(path_base_tree) ? "" : "/",
5865 return got_error_from_errno("asprintf");
5867 new_te = calloc(1, sizeof(*new_te));
5869 return got_error_from_errno("calloc");
5870 new_te->mode = S_IFDIR;
5872 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5873 sizeof(new_te->name)) {
5874 err = got_error(GOT_ERR_NO_SPACE);
5877 err = write_tree(&id, &nentries, NULL, subtree_path,
5878 commitable_paths, status_cb, status_arg, repo);
5883 memcpy(&new_te->id, id, sizeof(new_te->id));
5892 static const struct got_error *
5893 write_tree(struct got_object_id **new_tree_id, int *nentries,
5894 struct got_tree_object *base_tree, const char *path_base_tree,
5895 struct got_pathlist_head *commitable_paths,
5896 got_worktree_status_cb status_cb, void *status_arg,
5897 struct got_repository *repo)
5899 const struct got_error *err = NULL;
5900 struct got_pathlist_head paths;
5901 struct got_tree_entry *te, *new_te = NULL;
5902 struct got_pathlist_entry *pe;
5907 /* Insert, and recurse into, newly added entries first. */
5908 TAILQ_FOREACH(pe, commitable_paths, entry) {
5909 struct got_commitable *ct = pe->data;
5910 char *child_path = NULL, *slash;
5912 if ((ct->status != GOT_STATUS_ADD &&
5913 ct->staged_status != GOT_STATUS_ADD) ||
5914 (ct->flags & GOT_COMMITABLE_ADDED))
5917 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5918 strlen(path_base_tree)))
5921 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5926 slash = strchr(child_path, '/');
5927 if (slash == NULL) {
5928 err = alloc_added_blob_tree_entry(&new_te, ct);
5931 err = report_ct_status(ct, status_cb, status_arg);
5934 ct->flags |= GOT_COMMITABLE_ADDED;
5935 err = insert_tree_entry(new_te, &paths);
5940 *slash = '\0'; /* trim trailing path components */
5941 if (base_tree == NULL ||
5942 got_object_tree_find_entry(base_tree, child_path)
5944 err = make_subtree_for_added_blob(&new_te,
5945 child_path, path_base_tree,
5946 commitable_paths, status_cb, status_arg,
5950 err = insert_tree_entry(new_te, &paths);
5959 int i, nbase_entries;
5960 /* Handle modified and deleted entries. */
5961 nbase_entries = got_object_tree_get_nentries(base_tree);
5962 for (i = 0; i < nbase_entries; i++) {
5963 struct got_commitable *ct = NULL;
5965 te = got_object_tree_get_entry(base_tree, i);
5966 if (got_object_tree_entry_is_submodule(te)) {
5967 /* Entry is a submodule; just copy it. */
5968 err = got_object_tree_entry_dup(&new_te, te);
5971 err = insert_tree_entry(new_te, &paths);
5978 if (S_ISDIR(te->mode)) {
5980 err = got_object_tree_entry_dup(&new_te, te);
5983 err = match_modified_subtree(&modified, te,
5984 path_base_tree, commitable_paths);
5987 /* Avoid recursion into unmodified subtrees. */
5989 struct got_object_id *new_id;
5991 err = write_subtree(&new_id,
5993 path_base_tree, commitable_paths,
5994 status_cb, status_arg, repo);
5997 if (nsubentries == 0) {
5998 /* All entries were deleted. */
6002 memcpy(&new_te->id, new_id,
6003 sizeof(new_te->id));
6006 err = insert_tree_entry(new_te, &paths);
6013 err = match_deleted_or_modified_ct(&ct, te,
6014 path_base_tree, commitable_paths);
6018 /* NB: Deleted entries get dropped here. */
6019 if (ct->status == GOT_STATUS_MODIFY ||
6020 ct->status == GOT_STATUS_MODE_CHANGE ||
6021 ct->status == GOT_STATUS_CONFLICT ||
6022 ct->staged_status == GOT_STATUS_MODIFY) {
6023 err = alloc_modified_blob_tree_entry(
6027 err = insert_tree_entry(new_te, &paths);
6032 err = report_ct_status(ct, status_cb,
6037 /* Entry is unchanged; just copy it. */
6038 err = got_object_tree_entry_dup(&new_te, te);
6041 err = insert_tree_entry(new_te, &paths);
6049 /* Write new list of entries; deleted entries have been dropped. */
6050 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6052 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6056 static const struct got_error *
6057 update_fileindex_after_commit(struct got_worktree *worktree,
6058 struct got_pathlist_head *commitable_paths,
6059 struct got_object_id *new_base_commit_id,
6060 struct got_fileindex *fileindex, int have_staged_files)
6062 const struct got_error *err = NULL;
6063 struct got_pathlist_entry *pe;
6064 char *relpath = NULL;
6066 TAILQ_FOREACH(pe, commitable_paths, entry) {
6067 struct got_fileindex_entry *ie;
6068 struct got_commitable *ct = pe->data;
6070 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6072 err = got_path_skip_common_ancestor(&relpath,
6073 worktree->root_path, ct->ondisk_path);
6078 if (ct->status == GOT_STATUS_DELETE ||
6079 ct->staged_status == GOT_STATUS_DELETE) {
6080 got_fileindex_entry_remove(fileindex, ie);
6081 } else if (ct->staged_status == GOT_STATUS_ADD ||
6082 ct->staged_status == GOT_STATUS_MODIFY) {
6083 got_fileindex_entry_stage_set(ie,
6084 GOT_FILEIDX_STAGE_NONE);
6085 got_fileindex_entry_staged_filetype_set(ie, 0);
6087 err = got_fileindex_entry_update(ie,
6088 worktree->root_fd, relpath,
6089 ct->staged_blob_id->sha1,
6090 new_base_commit_id->sha1,
6091 !have_staged_files);
6093 err = got_fileindex_entry_update(ie,
6094 worktree->root_fd, relpath,
6096 new_base_commit_id->sha1,
6097 !have_staged_files);
6099 err = got_fileindex_entry_alloc(&ie, pe->path);
6102 err = got_fileindex_entry_update(ie,
6103 worktree->root_fd, relpath, ct->blob_id->sha1,
6104 new_base_commit_id->sha1, 1);
6106 got_fileindex_entry_free(ie);
6109 err = got_fileindex_entry_add(fileindex, ie);
6111 got_fileindex_entry_free(ie);
6124 static const struct got_error *
6125 check_out_of_date(const char *in_repo_path, unsigned char status,
6126 unsigned char staged_status, struct got_object_id *base_blob_id,
6127 struct got_object_id *base_commit_id,
6128 struct got_object_id *head_commit_id, struct got_repository *repo,
6131 const struct got_error *err = NULL;
6132 struct got_commit_object *commit = NULL;
6133 struct got_object_id *id = NULL;
6135 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6136 /* Trivial case: base commit == head commit */
6137 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6140 * Ensure file content which local changes were based
6141 * on matches file content in the branch head.
6143 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6146 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6148 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6149 err = got_error(ood_errcode);
6151 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6152 err = got_error(ood_errcode);
6154 /* Require that added files don't exist in the branch head. */
6155 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6158 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6159 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6161 err = id ? got_error(ood_errcode) : NULL;
6166 got_object_commit_close(commit);
6170 static const struct got_error *
6171 commit_worktree(struct got_object_id **new_commit_id,
6172 struct got_pathlist_head *commitable_paths,
6173 struct got_object_id *head_commit_id,
6174 struct got_object_id *parent_id2,
6175 struct got_worktree *worktree,
6176 const char *author, const char *committer, char *diff_path,
6177 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6178 got_worktree_status_cb status_cb, void *status_arg,
6179 struct got_repository *repo)
6181 const struct got_error *err = NULL, *unlockerr = NULL;
6182 struct got_pathlist_entry *pe;
6183 const char *head_ref_name = NULL;
6184 struct got_commit_object *head_commit = NULL;
6185 struct got_reference *head_ref2 = NULL;
6186 struct got_object_id *head_commit_id2 = NULL;
6187 struct got_tree_object *head_tree = NULL;
6188 struct got_object_id *new_tree_id = NULL;
6189 int nentries, nparents = 0;
6190 struct got_object_id_queue parent_ids;
6191 struct got_object_qid *pid = NULL;
6192 char *logmsg = NULL;
6195 *new_commit_id = NULL;
6197 STAILQ_INIT(&parent_ids);
6199 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6203 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6207 if (commit_msg_cb != NULL) {
6208 err = commit_msg_cb(commitable_paths, diff_path,
6209 &logmsg, commit_arg);
6214 if (logmsg == NULL || strlen(logmsg) == 0) {
6215 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6219 /* Create blobs from added and modified files and record their IDs. */
6220 TAILQ_FOREACH(pe, commitable_paths, entry) {
6221 struct got_commitable *ct = pe->data;
6224 /* Blobs for staged files already exist. */
6225 if (ct->staged_status == GOT_STATUS_ADD ||
6226 ct->staged_status == GOT_STATUS_MODIFY)
6229 if (ct->status != GOT_STATUS_ADD &&
6230 ct->status != GOT_STATUS_MODIFY &&
6231 ct->status != GOT_STATUS_MODE_CHANGE &&
6232 ct->status != GOT_STATUS_CONFLICT)
6235 if (asprintf(&ondisk_path, "%s/%s",
6236 worktree->root_path, pe->path) == -1) {
6237 err = got_error_from_errno("asprintf");
6240 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6246 /* Recursively write new tree objects. */
6247 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6248 commitable_paths, status_cb, status_arg, repo);
6252 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6255 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6258 err = got_object_qid_alloc(&pid, parent_id2);
6261 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6264 timestamp = time(NULL);
6265 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6266 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6272 /* Check if a concurrent commit to our branch has occurred. */
6273 head_ref_name = got_worktree_get_head_ref_name(worktree);
6274 if (head_ref_name == NULL) {
6275 err = got_error_from_errno("got_worktree_get_head_ref_name");
6278 /* Lock the reference here to prevent concurrent modification. */
6279 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6282 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6285 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6286 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6289 /* Update branch head in repository. */
6290 err = got_ref_change_ref(head_ref2, *new_commit_id);
6293 err = got_ref_write(head_ref2, repo);
6297 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6301 err = ref_base_commit(worktree, repo);
6305 got_object_id_queue_free(&parent_ids);
6307 got_object_tree_close(head_tree);
6309 got_object_commit_close(head_commit);
6310 free(head_commit_id2);
6312 unlockerr = got_ref_unlock(head_ref2);
6313 if (unlockerr && err == NULL)
6315 got_ref_close(head_ref2);
6320 static const struct got_error *
6321 check_path_is_commitable(const char *path,
6322 struct got_pathlist_head *commitable_paths)
6324 struct got_pathlist_entry *cpe = NULL;
6325 size_t path_len = strlen(path);
6327 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6328 struct got_commitable *ct = cpe->data;
6329 const char *ct_path = ct->path;
6331 while (ct_path[0] == '/')
6334 if (strcmp(path, ct_path) == 0 ||
6335 got_path_is_child(ct_path, path, path_len))
6340 return got_error_path(path, GOT_ERR_BAD_PATH);
6345 static const struct got_error *
6346 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6348 int *have_staged_files = arg;
6350 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6351 *have_staged_files = 1;
6352 return got_error(GOT_ERR_CANCELLED);
6358 static const struct got_error *
6359 check_non_staged_files(struct got_fileindex *fileindex,
6360 struct got_pathlist_head *paths)
6362 struct got_pathlist_entry *pe;
6363 struct got_fileindex_entry *ie;
6365 TAILQ_FOREACH(pe, paths, entry) {
6366 if (pe->path[0] == '\0')
6368 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6370 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6371 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6372 return got_error_path(pe->path,
6373 GOT_ERR_FILE_NOT_STAGED);
6379 const struct got_error *
6380 got_worktree_commit(struct got_object_id **new_commit_id,
6381 struct got_worktree *worktree, struct got_pathlist_head *paths,
6382 const char *author, const char *committer, int allow_bad_symlinks,
6383 int show_diff, int commit_conflicts,
6384 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6385 got_worktree_status_cb status_cb, void *status_arg,
6386 struct got_repository *repo)
6388 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6389 struct got_fileindex *fileindex = NULL;
6390 char *fileindex_path = NULL;
6391 struct got_pathlist_head commitable_paths;
6392 struct collect_commitables_arg cc_arg;
6393 struct got_pathlist_entry *pe;
6394 struct got_reference *head_ref = NULL;
6395 struct got_object_id *head_commit_id = NULL;
6396 char *diff_path = NULL;
6397 int have_staged_files = 0;
6399 *new_commit_id = NULL;
6401 memset(&cc_arg, 0, sizeof(cc_arg));
6402 TAILQ_INIT(&commitable_paths);
6404 err = lock_worktree(worktree, LOCK_EX);
6408 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6412 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6416 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6420 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6421 &have_staged_files);
6422 if (err && err->code != GOT_ERR_CANCELLED)
6424 if (have_staged_files) {
6425 err = check_non_staged_files(fileindex, paths);
6430 cc_arg.commitable_paths = &commitable_paths;
6431 cc_arg.worktree = worktree;
6432 cc_arg.fileindex = fileindex;
6434 cc_arg.have_staged_files = have_staged_files;
6435 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6436 cc_arg.diff_header_shown = 0;
6437 cc_arg.commit_conflicts = commit_conflicts;
6439 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6440 GOT_TMPDIR_STR "/got", ".diff");
6443 cc_arg.f1 = got_opentemp();
6444 if (cc_arg.f1 == NULL) {
6445 err = got_error_from_errno("got_opentemp");
6448 cc_arg.f2 = got_opentemp();
6449 if (cc_arg.f2 == NULL) {
6450 err = got_error_from_errno("got_opentemp");
6455 TAILQ_FOREACH(pe, paths, entry) {
6456 err = worktree_status(worktree, pe->path, fileindex, repo,
6457 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6463 if (fflush(cc_arg.diff_outfile) == EOF) {
6464 err = got_error_from_errno("fflush");
6469 if (TAILQ_EMPTY(&commitable_paths)) {
6470 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6474 TAILQ_FOREACH(pe, paths, entry) {
6475 err = check_path_is_commitable(pe->path, &commitable_paths);
6480 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6481 struct got_commitable *ct = pe->data;
6482 const char *ct_path = ct->in_repo_path;
6484 while (ct_path[0] == '/')
6486 err = check_out_of_date(ct_path, ct->status,
6487 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6488 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6494 err = commit_worktree(new_commit_id, &commitable_paths,
6495 head_commit_id, NULL, worktree, author, committer,
6496 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6497 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6501 err = update_fileindex_after_commit(worktree, &commitable_paths,
6502 *new_commit_id, fileindex, have_staged_files);
6503 sync_err = sync_fileindex(fileindex, fileindex_path);
6504 if (sync_err && err == NULL)
6508 got_fileindex_free(fileindex);
6509 free(fileindex_path);
6510 unlockerr = lock_worktree(worktree, LOCK_SH);
6511 if (unlockerr && err == NULL)
6513 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6514 struct got_commitable *ct = pe->data;
6516 free_commitable(ct);
6518 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6519 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6520 err = got_error_from_errno2("unlink", diff_path);
6522 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6524 err = got_error_from_errno("fclose");
6529 got_commitable_get_path(struct got_commitable *ct)
6535 got_commitable_get_status(struct got_commitable *ct)
6540 struct check_rebase_ok_arg {
6541 struct got_worktree *worktree;
6542 struct got_repository *repo;
6545 static const struct got_error *
6546 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6548 const struct got_error *err = NULL;
6549 struct check_rebase_ok_arg *a = arg;
6550 unsigned char status;
6554 /* Reject rebase of a work tree with mixed base commits. */
6555 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6556 SHA1_DIGEST_LENGTH))
6557 return got_error(GOT_ERR_MIXED_COMMITS);
6559 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6561 return got_error_from_errno("asprintf");
6563 /* Reject rebase of a work tree with modified or staged files. */
6564 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6569 if (status != GOT_STATUS_NO_CHANGE)
6570 return got_error(GOT_ERR_MODIFIED);
6571 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6572 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6577 const struct got_error *
6578 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6579 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6580 struct got_worktree *worktree, struct got_reference *branch,
6581 struct got_repository *repo)
6583 const struct got_error *err = NULL;
6584 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6585 char *branch_ref_name = NULL;
6586 char *fileindex_path = NULL;
6587 struct check_rebase_ok_arg ok_arg;
6588 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6589 struct got_object_id *wt_branch_tip = NULL;
6591 *new_base_branch_ref = NULL;
6595 err = lock_worktree(worktree, LOCK_EX);
6599 err = open_fileindex(fileindex, &fileindex_path, worktree);
6603 ok_arg.worktree = worktree;
6605 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6610 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6614 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6618 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6622 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6627 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6630 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6631 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6635 err = got_ref_alloc_symref(new_base_branch_ref,
6636 new_base_branch_ref_name, wt_branch);
6639 err = got_ref_write(*new_base_branch_ref, repo);
6643 /* TODO Lock original branch's ref while rebasing? */
6645 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6649 err = got_ref_write(branch_ref, repo);
6653 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6654 worktree->base_commit_id);
6657 err = got_ref_write(*tmp_branch, repo);
6661 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6665 free(fileindex_path);
6666 free(tmp_branch_name);
6667 free(new_base_branch_ref_name);
6668 free(branch_ref_name);
6670 got_ref_close(branch_ref);
6672 got_ref_close(wt_branch);
6673 free(wt_branch_tip);
6675 if (*new_base_branch_ref) {
6676 got_ref_close(*new_base_branch_ref);
6677 *new_base_branch_ref = NULL;
6680 got_ref_close(*tmp_branch);
6684 got_fileindex_free(*fileindex);
6687 lock_worktree(worktree, LOCK_SH);
6692 const struct got_error *
6693 got_worktree_rebase_continue(struct got_object_id **commit_id,
6694 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6695 struct got_reference **branch, struct got_fileindex **fileindex,
6696 struct got_worktree *worktree, struct got_repository *repo)
6698 const struct got_error *err;
6699 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6700 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6701 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6702 char *fileindex_path = NULL;
6703 int have_staged_files = 0;
6706 *new_base_branch = NULL;
6711 err = lock_worktree(worktree, LOCK_EX);
6715 err = open_fileindex(fileindex, &fileindex_path, worktree);
6719 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6720 &have_staged_files);
6721 if (err && err->code != GOT_ERR_CANCELLED)
6723 if (have_staged_files) {
6724 err = got_error(GOT_ERR_STAGED_PATHS);
6728 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6732 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6736 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6740 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6744 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6748 err = got_ref_open(branch, repo,
6749 got_ref_get_symref_target(branch_ref), 0);
6753 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6757 err = got_ref_resolve(commit_id, repo, commit_ref);
6761 err = got_ref_open(new_base_branch, repo,
6762 new_base_branch_ref_name, 0);
6766 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6770 free(commit_ref_name);
6771 free(branch_ref_name);
6772 free(fileindex_path);
6774 got_ref_close(commit_ref);
6776 got_ref_close(branch_ref);
6781 got_ref_close(*tmp_branch);
6784 if (*new_base_branch) {
6785 got_ref_close(*new_base_branch);
6786 *new_base_branch = NULL;
6789 got_ref_close(*branch);
6793 got_fileindex_free(*fileindex);
6796 lock_worktree(worktree, LOCK_SH);
6801 const struct got_error *
6802 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6804 const struct got_error *err;
6805 char *tmp_branch_name = NULL;
6807 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6811 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6812 free(tmp_branch_name);
6816 static const struct got_error *
6817 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6818 const char *diff_path, char **logmsg, void *arg)
6824 static const struct got_error *
6825 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6826 const char *path, struct got_object_id *blob_id,
6827 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6828 int dirfd, const char *de_name)
6833 struct collect_merged_paths_arg {
6834 got_worktree_checkout_cb progress_cb;
6836 struct got_pathlist_head *merged_paths;
6839 static const struct got_error *
6840 collect_merged_paths(void *arg, unsigned char status, const char *path)
6842 const struct got_error *err;
6843 struct collect_merged_paths_arg *a = arg;
6845 struct got_pathlist_entry *new;
6847 err = (*a->progress_cb)(a->progress_arg, status, path);
6851 if (status != GOT_STATUS_MERGE &&
6852 status != GOT_STATUS_ADD &&
6853 status != GOT_STATUS_DELETE &&
6854 status != GOT_STATUS_CONFLICT)
6859 return got_error_from_errno("strdup");
6861 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6862 if (err || new == NULL)
6867 static const struct got_error *
6868 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6869 int is_rebase, struct got_repository *repo)
6871 const struct got_error *err;
6872 struct got_reference *commit_ref = NULL;
6874 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6876 if (err->code != GOT_ERR_NOT_REF)
6878 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6881 err = got_ref_write(commit_ref, repo);
6884 } else if (is_rebase) {
6885 struct got_object_id *stored_id;
6888 err = got_ref_resolve(&stored_id, repo, commit_ref);
6891 cmp = got_object_id_cmp(commit_id, stored_id);
6894 err = got_error(GOT_ERR_REBASE_COMMITID);
6900 got_ref_close(commit_ref);
6904 static const struct got_error *
6905 rebase_merge_files(struct got_pathlist_head *merged_paths,
6906 const char *commit_ref_name, struct got_worktree *worktree,
6907 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6908 struct got_object_id *commit_id, struct got_repository *repo,
6909 got_worktree_checkout_cb progress_cb, void *progress_arg,
6910 got_cancel_cb cancel_cb, void *cancel_arg)
6912 const struct got_error *err;
6913 struct got_reference *commit_ref = NULL;
6914 struct collect_merged_paths_arg cmp_arg;
6915 char *fileindex_path;
6917 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6919 err = get_fileindex_path(&fileindex_path, worktree);
6923 cmp_arg.progress_cb = progress_cb;
6924 cmp_arg.progress_arg = progress_arg;
6925 cmp_arg.merged_paths = merged_paths;
6926 err = merge_files(worktree, fileindex, fileindex_path,
6927 parent_commit_id, commit_id, repo, collect_merged_paths,
6928 &cmp_arg, cancel_cb, cancel_arg);
6930 got_ref_close(commit_ref);
6934 const struct got_error *
6935 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6936 struct got_worktree *worktree, struct got_fileindex *fileindex,
6937 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6938 struct got_repository *repo,
6939 got_worktree_checkout_cb progress_cb, void *progress_arg,
6940 got_cancel_cb cancel_cb, void *cancel_arg)
6942 const struct got_error *err;
6943 char *commit_ref_name;
6945 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6949 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6953 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6954 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6955 progress_arg, cancel_cb, cancel_arg);
6957 free(commit_ref_name);
6961 const struct got_error *
6962 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6963 struct got_worktree *worktree, struct got_fileindex *fileindex,
6964 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6965 struct got_repository *repo,
6966 got_worktree_checkout_cb progress_cb, void *progress_arg,
6967 got_cancel_cb cancel_cb, void *cancel_arg)
6969 const struct got_error *err;
6970 char *commit_ref_name;
6972 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6976 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6980 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6981 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6982 progress_arg, cancel_cb, cancel_arg);
6984 free(commit_ref_name);
6988 static const struct got_error *
6989 rebase_commit(struct got_object_id **new_commit_id,
6990 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6991 struct got_worktree *worktree, struct got_fileindex *fileindex,
6992 struct got_reference *tmp_branch, const char *committer,
6993 struct got_commit_object *orig_commit, const char *new_logmsg,
6994 int allow_conflict, struct got_repository *repo)
6996 const struct got_error *err, *sync_err;
6997 struct got_pathlist_head commitable_paths;
6998 struct collect_commitables_arg cc_arg;
6999 char *fileindex_path = NULL;
7000 struct got_reference *head_ref = NULL;
7001 struct got_object_id *head_commit_id = NULL;
7002 char *logmsg = NULL;
7004 memset(&cc_arg, 0, sizeof(cc_arg));
7005 TAILQ_INIT(&commitable_paths);
7006 *new_commit_id = NULL;
7008 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7010 err = get_fileindex_path(&fileindex_path, worktree);
7014 cc_arg.commitable_paths = &commitable_paths;
7015 cc_arg.worktree = worktree;
7017 cc_arg.have_staged_files = 0;
7018 cc_arg.commit_conflicts = allow_conflict;
7020 * If possible get the status of individual files directly to
7021 * avoid crawling the entire work tree once per rebased commit.
7023 * Ideally, merged_paths would contain a list of commitables
7024 * we could use so we could skip worktree_status() entirely.
7025 * However, we would then need carefully keep track of cumulative
7026 * effects of operations such as file additions and deletions
7027 * in 'got histedit -f' (folding multiple commits into one),
7028 * and this extra complexity is not really worth it.
7031 struct got_pathlist_entry *pe;
7032 TAILQ_FOREACH(pe, merged_paths, entry) {
7033 err = worktree_status(worktree, pe->path, fileindex,
7034 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7040 err = worktree_status(worktree, "", fileindex, repo,
7041 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7046 if (TAILQ_EMPTY(&commitable_paths)) {
7047 /* No-op change; commit will be elided. */
7048 err = got_ref_delete(commit_ref, repo);
7051 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7055 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7059 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7064 logmsg = strdup(new_logmsg);
7065 if (logmsg == NULL) {
7066 err = got_error_from_errno("strdup");
7070 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7075 /* NB: commit_worktree will call free(logmsg) */
7076 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7077 NULL, worktree, got_object_commit_get_author(orig_commit),
7078 committer ? committer :
7079 got_object_commit_get_committer(orig_commit), NULL,
7080 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7084 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7088 err = got_ref_delete(commit_ref, repo);
7092 err = update_fileindex_after_commit(worktree, &commitable_paths,
7093 *new_commit_id, fileindex, 0);
7094 sync_err = sync_fileindex(fileindex, fileindex_path);
7095 if (sync_err && err == NULL)
7098 free(fileindex_path);
7099 free(head_commit_id);
7101 got_ref_close(head_ref);
7103 free(*new_commit_id);
7104 *new_commit_id = NULL;
7109 const struct got_error *
7110 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7111 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7112 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7113 const char *committer, struct got_commit_object *orig_commit,
7114 struct got_object_id *orig_commit_id, int allow_conflict,
7115 struct got_repository *repo)
7117 const struct got_error *err;
7118 char *commit_ref_name;
7119 struct got_reference *commit_ref = NULL;
7120 struct got_object_id *commit_id = NULL;
7122 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7126 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7129 err = got_ref_resolve(&commit_id, repo, commit_ref);
7132 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7133 err = got_error(GOT_ERR_REBASE_COMMITID);
7137 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7138 worktree, fileindex, tmp_branch, committer, orig_commit,
7139 NULL, allow_conflict, repo);
7142 got_ref_close(commit_ref);
7143 free(commit_ref_name);
7148 const struct got_error *
7149 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7150 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7151 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7152 const char *committer, struct got_commit_object *orig_commit,
7153 struct got_object_id *orig_commit_id, const char *new_logmsg,
7154 int allow_conflict, struct got_repository *repo)
7156 const struct got_error *err;
7157 char *commit_ref_name;
7158 struct got_reference *commit_ref = NULL;
7160 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7164 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7168 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7169 worktree, fileindex, tmp_branch, committer, orig_commit,
7170 new_logmsg, allow_conflict, repo);
7173 got_ref_close(commit_ref);
7174 free(commit_ref_name);
7178 const struct got_error *
7179 got_worktree_rebase_postpone(struct got_worktree *worktree,
7180 struct got_fileindex *fileindex)
7183 got_fileindex_free(fileindex);
7184 return lock_worktree(worktree, LOCK_SH);
7187 static const struct got_error *
7188 delete_ref(const char *name, struct got_repository *repo)
7190 const struct got_error *err;
7191 struct got_reference *ref;
7193 err = got_ref_open(&ref, repo, name, 0);
7195 if (err->code == GOT_ERR_NOT_REF)
7200 err = got_ref_delete(ref, repo);
7205 static const struct got_error *
7206 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7208 const struct got_error *err;
7209 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7210 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7212 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7215 err = delete_ref(tmp_branch_name, repo);
7219 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7222 err = delete_ref(new_base_branch_ref_name, repo);
7226 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7229 err = delete_ref(branch_ref_name, repo);
7233 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7236 err = delete_ref(commit_ref_name, repo);
7241 free(tmp_branch_name);
7242 free(new_base_branch_ref_name);
7243 free(branch_ref_name);
7244 free(commit_ref_name);
7248 static const struct got_error *
7249 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7250 struct got_object_id *new_commit_id, struct got_repository *repo)
7252 const struct got_error *err;
7253 struct got_reference *ref = NULL;
7254 struct got_object_id *old_commit_id = NULL;
7255 const char *branch_name = NULL;
7256 char *new_id_str = NULL;
7257 char *refname = NULL;
7259 branch_name = got_ref_get_name(branch);
7260 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7261 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7264 err = got_object_id_str(&new_id_str, new_commit_id);
7268 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7269 new_id_str) == -1) {
7270 err = got_error_from_errno("asprintf");
7274 err = got_ref_resolve(&old_commit_id, repo, branch);
7278 err = got_ref_alloc(&ref, refname, old_commit_id);
7282 err = got_ref_write(ref, repo);
7286 free(old_commit_id);
7292 const struct got_error *
7293 got_worktree_rebase_complete(struct got_worktree *worktree,
7294 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7295 struct got_reference *rebased_branch, struct got_repository *repo,
7298 const struct got_error *err, *unlockerr, *sync_err;
7299 struct got_object_id *new_head_commit_id = NULL;
7300 char *fileindex_path = NULL;
7302 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7306 if (create_backup) {
7307 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7308 rebased_branch, new_head_commit_id, repo);
7313 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7317 err = got_ref_write(rebased_branch, repo);
7321 err = got_worktree_set_head_ref(worktree, rebased_branch);
7325 err = delete_rebase_refs(worktree, repo);
7329 err = get_fileindex_path(&fileindex_path, worktree);
7332 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7333 sync_err = sync_fileindex(fileindex, fileindex_path);
7334 if (sync_err && err == NULL)
7337 got_fileindex_free(fileindex);
7338 free(fileindex_path);
7339 free(new_head_commit_id);
7340 unlockerr = lock_worktree(worktree, LOCK_SH);
7341 if (unlockerr && err == NULL)
7346 static const struct got_error *
7347 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7348 struct got_object_id *id1, struct got_object_id *id2,
7349 struct got_repository *repo)
7351 const struct got_error *err;
7352 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7353 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7356 err = got_object_open_as_commit(&commit1, repo, id1);
7360 err = got_object_open_as_tree(&tree1, repo,
7361 got_object_commit_get_tree_id(commit1));
7367 err = got_object_open_as_commit(&commit2, repo, id2);
7371 err = got_object_open_as_tree(&tree2, repo,
7372 got_object_commit_get_tree_id(commit2));
7377 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7378 got_diff_tree_collect_changed_paths, paths, 0);
7383 got_object_commit_close(commit1);
7385 got_object_commit_close(commit2);
7387 got_object_tree_close(tree1);
7389 got_object_tree_close(tree2);
7393 static const struct got_error *
7394 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7395 struct got_object_id *id1, struct got_object_id *id2,
7396 const char *path_prefix, struct got_repository *repo)
7398 const struct got_error *err;
7399 struct got_pathlist_head merged_paths;
7400 struct got_pathlist_entry *pe;
7401 char *abspath = NULL, *wt_path = NULL;
7403 TAILQ_INIT(&merged_paths);
7405 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7409 TAILQ_FOREACH(pe, &merged_paths, entry) {
7410 struct got_diff_changed_path *change = pe->data;
7412 if (change->status != GOT_STATUS_ADD)
7415 if (got_path_is_root_dir(path_prefix)) {
7416 wt_path = strdup(pe->path);
7417 if (wt_path == NULL) {
7418 err = got_error_from_errno("strdup");
7422 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7423 err = got_error_from_errno("asprintf");
7427 err = got_path_skip_common_ancestor(&wt_path,
7428 path_prefix, abspath);
7435 err = got_pathlist_append(added_paths, wt_path, NULL);
7442 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7448 static const struct got_error *
7449 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7450 struct got_object_id *id, const char *path_prefix,
7451 struct got_repository *repo)
7453 const struct got_error *err;
7454 struct got_commit_object *commit = NULL;
7455 struct got_object_qid *pid;
7457 err = got_object_open_as_commit(&commit, repo, id);
7461 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7463 err = get_paths_added_between_commits(added_paths,
7464 pid ? &pid->id : NULL, id, path_prefix, repo);
7469 got_object_commit_close(commit);
7473 const struct got_error *
7474 got_worktree_rebase_abort(struct got_worktree *worktree,
7475 struct got_fileindex *fileindex, struct got_repository *repo,
7476 struct got_reference *new_base_branch,
7477 got_worktree_checkout_cb progress_cb, void *progress_arg)
7479 const struct got_error *err, *unlockerr, *sync_err;
7480 struct got_reference *resolved = NULL;
7481 struct got_object_id *commit_id = NULL;
7482 struct got_object_id *merged_commit_id = NULL;
7483 struct got_commit_object *commit = NULL;
7484 char *fileindex_path = NULL;
7485 char *commit_ref_name = NULL;
7486 struct got_reference *commit_ref = NULL;
7487 struct revert_file_args rfa;
7488 struct got_object_id *tree_id = NULL;
7489 struct got_pathlist_head added_paths;
7491 TAILQ_INIT(&added_paths);
7493 err = lock_worktree(worktree, LOCK_EX);
7497 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7501 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7505 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7510 * Determine which files in added status can be safely removed
7511 * from disk while reverting changes in the work tree.
7512 * We want to avoid deleting unrelated files which were added by
7513 * the user for conflict resolution purposes.
7515 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7516 got_worktree_get_path_prefix(worktree), repo);
7520 err = got_ref_open(&resolved, repo,
7521 got_ref_get_symref_target(new_base_branch), 0);
7525 err = got_worktree_set_head_ref(worktree, resolved);
7530 * XXX commits to the base branch could have happened while
7531 * we were busy rebasing; should we store the original commit ID
7532 * when rebase begins and read it back here?
7534 err = got_ref_resolve(&commit_id, repo, resolved);
7538 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7542 err = got_object_open_as_commit(&commit, repo,
7543 worktree->base_commit_id);
7547 err = got_object_id_by_path(&tree_id, repo, commit,
7548 worktree->path_prefix);
7552 err = delete_rebase_refs(worktree, repo);
7556 err = get_fileindex_path(&fileindex_path, worktree);
7560 rfa.worktree = worktree;
7561 rfa.fileindex = fileindex;
7562 rfa.progress_cb = progress_cb;
7563 rfa.progress_arg = progress_arg;
7564 rfa.patch_cb = NULL;
7565 rfa.patch_arg = NULL;
7567 rfa.unlink_added_files = 1;
7568 rfa.added_files_to_unlink = &added_paths;
7569 err = worktree_status(worktree, "", fileindex, repo,
7570 revert_file, &rfa, NULL, NULL, 1, 0);
7574 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7575 repo, progress_cb, progress_arg, NULL, NULL);
7577 sync_err = sync_fileindex(fileindex, fileindex_path);
7578 if (sync_err && err == NULL)
7581 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7582 got_ref_close(resolved);
7585 free(merged_commit_id);
7587 got_object_commit_close(commit);
7589 got_fileindex_free(fileindex);
7590 free(fileindex_path);
7591 free(commit_ref_name);
7593 got_ref_close(commit_ref);
7595 unlockerr = lock_worktree(worktree, LOCK_SH);
7596 if (unlockerr && err == NULL)
7601 const struct got_error *
7602 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7603 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7604 struct got_fileindex **fileindex, struct got_worktree *worktree,
7605 struct got_repository *repo)
7607 const struct got_error *err = NULL;
7608 char *tmp_branch_name = NULL;
7609 char *branch_ref_name = NULL;
7610 char *base_commit_ref_name = NULL;
7611 char *fileindex_path = NULL;
7612 struct check_rebase_ok_arg ok_arg;
7613 struct got_reference *wt_branch = NULL;
7614 struct got_reference *base_commit_ref = NULL;
7618 *base_commit_id = NULL;
7621 err = lock_worktree(worktree, LOCK_EX);
7625 err = open_fileindex(fileindex, &fileindex_path, worktree);
7629 ok_arg.worktree = worktree;
7631 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7636 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7640 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7644 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7649 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7654 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7658 err = got_ref_write(*branch_ref, repo);
7662 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7663 worktree->base_commit_id);
7666 err = got_ref_write(base_commit_ref, repo);
7669 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7670 if (*base_commit_id == NULL) {
7671 err = got_error_from_errno("got_object_id_dup");
7675 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7676 worktree->base_commit_id);
7679 err = got_ref_write(*tmp_branch, repo);
7683 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7687 free(fileindex_path);
7688 free(tmp_branch_name);
7689 free(branch_ref_name);
7690 free(base_commit_ref_name);
7692 got_ref_close(wt_branch);
7695 got_ref_close(*branch_ref);
7699 got_ref_close(*tmp_branch);
7702 free(*base_commit_id);
7704 got_fileindex_free(*fileindex);
7707 lock_worktree(worktree, LOCK_SH);
7712 const struct got_error *
7713 got_worktree_histedit_postpone(struct got_worktree *worktree,
7714 struct got_fileindex *fileindex)
7717 got_fileindex_free(fileindex);
7718 return lock_worktree(worktree, LOCK_SH);
7721 const struct got_error *
7722 got_worktree_histedit_in_progress(int *in_progress,
7723 struct got_worktree *worktree)
7725 const struct got_error *err;
7726 char *tmp_branch_name = NULL;
7728 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7732 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7733 free(tmp_branch_name);
7737 const struct got_error *
7738 got_worktree_histedit_continue(struct got_object_id **commit_id,
7739 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7740 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7741 struct got_worktree *worktree, struct got_repository *repo)
7743 const struct got_error *err;
7744 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7745 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7746 struct got_reference *commit_ref = NULL;
7747 struct got_reference *base_commit_ref = NULL;
7748 char *fileindex_path = NULL;
7749 int have_staged_files = 0;
7753 *base_commit_id = NULL;
7756 err = lock_worktree(worktree, LOCK_EX);
7760 err = open_fileindex(fileindex, &fileindex_path, worktree);
7764 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7765 &have_staged_files);
7766 if (err && err->code != GOT_ERR_CANCELLED)
7768 if (have_staged_files) {
7769 err = got_error(GOT_ERR_STAGED_PATHS);
7773 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7777 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7781 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7785 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7790 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7794 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7797 err = got_ref_resolve(commit_id, repo, commit_ref);
7801 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7804 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7808 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7812 free(commit_ref_name);
7813 free(branch_ref_name);
7814 free(fileindex_path);
7816 got_ref_close(commit_ref);
7817 if (base_commit_ref)
7818 got_ref_close(base_commit_ref);
7822 free(*base_commit_id);
7823 *base_commit_id = NULL;
7825 got_ref_close(*tmp_branch);
7829 got_fileindex_free(*fileindex);
7832 lock_worktree(worktree, LOCK_EX);
7837 static const struct got_error *
7838 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7840 const struct got_error *err;
7841 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7842 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7844 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7847 err = delete_ref(tmp_branch_name, repo);
7851 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7855 err = delete_ref(base_commit_ref_name, repo);
7859 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7862 err = delete_ref(branch_ref_name, repo);
7866 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7869 err = delete_ref(commit_ref_name, repo);
7873 free(tmp_branch_name);
7874 free(base_commit_ref_name);
7875 free(branch_ref_name);
7876 free(commit_ref_name);
7880 const struct got_error *
7881 got_worktree_histedit_abort(struct got_worktree *worktree,
7882 struct got_fileindex *fileindex, struct got_repository *repo,
7883 struct got_reference *branch, struct got_object_id *base_commit_id,
7884 got_worktree_checkout_cb progress_cb, void *progress_arg)
7886 const struct got_error *err, *unlockerr, *sync_err;
7887 struct got_reference *resolved = NULL;
7888 char *fileindex_path = NULL;
7889 struct got_object_id *merged_commit_id = NULL;
7890 struct got_commit_object *commit = NULL;
7891 char *commit_ref_name = NULL;
7892 struct got_reference *commit_ref = NULL;
7893 struct got_object_id *tree_id = NULL;
7894 struct revert_file_args rfa;
7895 struct got_pathlist_head added_paths;
7897 TAILQ_INIT(&added_paths);
7899 err = lock_worktree(worktree, LOCK_EX);
7903 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7907 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7909 if (err->code != GOT_ERR_NOT_REF)
7911 /* Can happen on early abort due to invalid histedit script. */
7916 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7921 * Determine which files in added status can be safely removed
7922 * from disk while reverting changes in the work tree.
7923 * We want to avoid deleting unrelated files added by the
7924 * user during conflict resolution or during histedit -e.
7926 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7927 got_worktree_get_path_prefix(worktree), repo);
7932 err = got_ref_open(&resolved, repo,
7933 got_ref_get_symref_target(branch), 0);
7937 err = got_worktree_set_head_ref(worktree, resolved);
7941 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7945 err = got_object_open_as_commit(&commit, repo,
7946 worktree->base_commit_id);
7950 err = got_object_id_by_path(&tree_id, repo, commit,
7951 worktree->path_prefix);
7955 err = delete_histedit_refs(worktree, repo);
7959 err = get_fileindex_path(&fileindex_path, worktree);
7963 rfa.worktree = worktree;
7964 rfa.fileindex = fileindex;
7965 rfa.progress_cb = progress_cb;
7966 rfa.progress_arg = progress_arg;
7967 rfa.patch_cb = NULL;
7968 rfa.patch_arg = NULL;
7970 rfa.unlink_added_files = 1;
7971 rfa.added_files_to_unlink = &added_paths;
7972 err = worktree_status(worktree, "", fileindex, repo,
7973 revert_file, &rfa, NULL, NULL, 1, 0);
7977 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7978 repo, progress_cb, progress_arg, NULL, NULL);
7980 sync_err = sync_fileindex(fileindex, fileindex_path);
7981 if (sync_err && err == NULL)
7985 got_ref_close(resolved);
7987 got_ref_close(commit_ref);
7988 free(merged_commit_id);
7990 free(fileindex_path);
7991 free(commit_ref_name);
7993 unlockerr = lock_worktree(worktree, LOCK_SH);
7994 if (unlockerr && err == NULL)
7999 const struct got_error *
8000 got_worktree_histedit_complete(struct got_worktree *worktree,
8001 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8002 struct got_reference *edited_branch, struct got_repository *repo)
8004 const struct got_error *err, *unlockerr, *sync_err;
8005 struct got_object_id *new_head_commit_id = NULL;
8006 struct got_reference *resolved = NULL;
8007 char *fileindex_path = NULL;
8009 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8013 err = got_ref_open(&resolved, repo,
8014 got_ref_get_symref_target(edited_branch), 0);
8018 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8019 resolved, new_head_commit_id, repo);
8023 err = got_ref_change_ref(resolved, new_head_commit_id);
8027 err = got_ref_write(resolved, repo);
8031 err = got_worktree_set_head_ref(worktree, resolved);
8035 err = delete_histedit_refs(worktree, repo);
8039 err = get_fileindex_path(&fileindex_path, worktree);
8042 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8043 sync_err = sync_fileindex(fileindex, fileindex_path);
8044 if (sync_err && err == NULL)
8047 got_fileindex_free(fileindex);
8048 free(fileindex_path);
8049 free(new_head_commit_id);
8050 unlockerr = lock_worktree(worktree, LOCK_SH);
8051 if (unlockerr && err == NULL)
8056 const struct got_error *
8057 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8058 struct got_object_id *commit_id, struct got_repository *repo)
8060 const struct got_error *err;
8061 char *commit_ref_name;
8063 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8067 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8071 err = delete_ref(commit_ref_name, repo);
8073 free(commit_ref_name);
8077 const struct got_error *
8078 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8079 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8080 struct got_worktree *worktree, const char *refname,
8081 struct got_repository *repo)
8083 const struct got_error *err = NULL;
8084 char *fileindex_path = NULL;
8085 struct check_rebase_ok_arg ok_arg;
8089 *base_branch_ref = NULL;
8091 err = lock_worktree(worktree, LOCK_EX);
8095 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8096 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8097 "cannot integrate a branch into itself; "
8098 "update -b or different branch name required");
8102 err = open_fileindex(fileindex, &fileindex_path, worktree);
8106 /* Preconditions are the same as for rebase. */
8107 ok_arg.worktree = worktree;
8109 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8114 err = got_ref_open(branch_ref, repo, refname, 1);
8118 err = got_ref_open(base_branch_ref, repo,
8119 got_worktree_get_head_ref_name(worktree), 1);
8123 got_ref_close(*branch_ref);
8126 if (*base_branch_ref) {
8127 got_ref_close(*base_branch_ref);
8128 *base_branch_ref = NULL;
8131 got_fileindex_free(*fileindex);
8134 lock_worktree(worktree, LOCK_SH);
8139 const struct got_error *
8140 got_worktree_integrate_continue(struct got_worktree *worktree,
8141 struct got_fileindex *fileindex, struct got_repository *repo,
8142 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8143 got_worktree_checkout_cb progress_cb, void *progress_arg,
8144 got_cancel_cb cancel_cb, void *cancel_arg)
8146 const struct got_error *err = NULL, *sync_err, *unlockerr;
8147 char *fileindex_path = NULL;
8148 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8149 struct got_commit_object *commit = NULL;
8151 err = get_fileindex_path(&fileindex_path, worktree);
8155 err = got_ref_resolve(&commit_id, repo, branch_ref);
8159 err = got_object_open_as_commit(&commit, repo, commit_id);
8163 err = got_object_id_by_path(&tree_id, repo, commit,
8164 worktree->path_prefix);
8168 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8172 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8173 progress_cb, progress_arg, cancel_cb, cancel_arg);
8177 err = got_ref_change_ref(base_branch_ref, commit_id);
8181 err = got_ref_write(base_branch_ref, repo);
8185 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8187 sync_err = sync_fileindex(fileindex, fileindex_path);
8188 if (sync_err && err == NULL)
8192 unlockerr = got_ref_unlock(branch_ref);
8193 if (unlockerr && err == NULL)
8195 got_ref_close(branch_ref);
8197 unlockerr = got_ref_unlock(base_branch_ref);
8198 if (unlockerr && err == NULL)
8200 got_ref_close(base_branch_ref);
8202 got_fileindex_free(fileindex);
8203 free(fileindex_path);
8206 got_object_commit_close(commit);
8208 unlockerr = lock_worktree(worktree, LOCK_SH);
8209 if (unlockerr && err == NULL)
8214 const struct got_error *
8215 got_worktree_integrate_abort(struct got_worktree *worktree,
8216 struct got_fileindex *fileindex, struct got_repository *repo,
8217 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8219 const struct got_error *err = NULL, *unlockerr = NULL;
8221 got_fileindex_free(fileindex);
8223 err = lock_worktree(worktree, LOCK_SH);
8225 unlockerr = got_ref_unlock(branch_ref);
8226 if (unlockerr && err == NULL)
8228 got_ref_close(branch_ref);
8230 unlockerr = got_ref_unlock(base_branch_ref);
8231 if (unlockerr && err == NULL)
8233 got_ref_close(base_branch_ref);
8238 const struct got_error *
8239 got_worktree_merge_postpone(struct got_worktree *worktree,
8240 struct got_fileindex *fileindex)
8242 const struct got_error *err, *sync_err;
8243 char *fileindex_path = NULL;
8245 err = get_fileindex_path(&fileindex_path, worktree);
8249 sync_err = sync_fileindex(fileindex, fileindex_path);
8251 err = lock_worktree(worktree, LOCK_SH);
8252 if (sync_err && err == NULL)
8255 got_fileindex_free(fileindex);
8256 free(fileindex_path);
8260 static const struct got_error *
8261 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8263 const struct got_error *err;
8264 char *branch_refname = NULL, *commit_refname = NULL;
8266 err = get_merge_branch_ref_name(&branch_refname, worktree);
8269 err = delete_ref(branch_refname, repo);
8273 err = get_merge_commit_ref_name(&commit_refname, worktree);
8276 err = delete_ref(commit_refname, repo);
8281 free(branch_refname);
8282 free(commit_refname);
8286 struct merge_commit_msg_arg {
8287 struct got_worktree *worktree;
8288 const char *branch_name;
8291 static const struct got_error *
8292 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8293 const char *diff_path, char **logmsg, void *arg)
8295 struct merge_commit_msg_arg *a = arg;
8297 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8298 got_worktree_get_head_ref_name(a->worktree)) == -1)
8299 return got_error_from_errno("asprintf");
8305 const struct got_error *
8306 got_worktree_merge_branch(struct got_worktree *worktree,
8307 struct got_fileindex *fileindex,
8308 struct got_object_id *yca_commit_id,
8309 struct got_object_id *branch_tip,
8310 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8311 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8313 const struct got_error *err;
8314 char *fileindex_path = NULL;
8316 err = get_fileindex_path(&fileindex_path, worktree);
8320 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8325 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8326 branch_tip, repo, progress_cb, progress_arg,
8327 cancel_cb, cancel_arg);
8329 free(fileindex_path);
8333 const struct got_error *
8334 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8335 struct got_worktree *worktree, struct got_fileindex *fileindex,
8336 const char *author, const char *committer, int allow_bad_symlinks,
8337 struct got_object_id *branch_tip, const char *branch_name,
8338 int allow_conflict, struct got_repository *repo,
8339 got_worktree_status_cb status_cb, void *status_arg)
8342 const struct got_error *err = NULL, *sync_err;
8343 struct got_pathlist_head commitable_paths;
8344 struct collect_commitables_arg cc_arg;
8345 struct got_pathlist_entry *pe;
8346 struct got_reference *head_ref = NULL;
8347 struct got_object_id *head_commit_id = NULL;
8348 int have_staged_files = 0;
8349 struct merge_commit_msg_arg mcm_arg;
8350 char *fileindex_path = NULL;
8352 memset(&cc_arg, 0, sizeof(cc_arg));
8353 *new_commit_id = NULL;
8355 TAILQ_INIT(&commitable_paths);
8357 err = get_fileindex_path(&fileindex_path, worktree);
8361 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8365 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8369 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8370 &have_staged_files);
8371 if (err && err->code != GOT_ERR_CANCELLED)
8373 if (have_staged_files) {
8374 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8378 cc_arg.commitable_paths = &commitable_paths;
8379 cc_arg.worktree = worktree;
8380 cc_arg.fileindex = fileindex;
8382 cc_arg.have_staged_files = have_staged_files;
8383 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8384 cc_arg.commit_conflicts = allow_conflict;
8385 err = worktree_status(worktree, "", fileindex, repo,
8386 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8390 if (TAILQ_EMPTY(&commitable_paths)) {
8391 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8392 "merge of %s cannot proceed", branch_name);
8396 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8397 struct got_commitable *ct = pe->data;
8398 const char *ct_path = ct->in_repo_path;
8400 while (ct_path[0] == '/')
8402 err = check_out_of_date(ct_path, ct->status,
8403 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8404 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8410 mcm_arg.worktree = worktree;
8411 mcm_arg.branch_name = branch_name;
8412 err = commit_worktree(new_commit_id, &commitable_paths,
8413 head_commit_id, branch_tip, worktree, author, committer, NULL,
8414 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8418 err = update_fileindex_after_commit(worktree, &commitable_paths,
8419 *new_commit_id, fileindex, have_staged_files);
8420 sync_err = sync_fileindex(fileindex, fileindex_path);
8421 if (sync_err && err == NULL)
8424 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8425 struct got_commitable *ct = pe->data;
8427 free_commitable(ct);
8429 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8430 free(fileindex_path);
8434 const struct got_error *
8435 got_worktree_merge_complete(struct got_worktree *worktree,
8436 struct got_fileindex *fileindex, struct got_repository *repo)
8438 const struct got_error *err, *unlockerr, *sync_err;
8439 char *fileindex_path = NULL;
8441 err = delete_merge_refs(worktree, repo);
8445 err = get_fileindex_path(&fileindex_path, worktree);
8448 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8449 sync_err = sync_fileindex(fileindex, fileindex_path);
8450 if (sync_err && err == NULL)
8453 got_fileindex_free(fileindex);
8454 free(fileindex_path);
8455 unlockerr = lock_worktree(worktree, LOCK_SH);
8456 if (unlockerr && err == NULL)
8461 const struct got_error *
8462 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8463 struct got_repository *repo)
8465 const struct got_error *err;
8466 char *branch_refname = NULL;
8467 struct got_reference *branch_ref = NULL;
8471 err = get_merge_branch_ref_name(&branch_refname, worktree);
8474 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8475 free(branch_refname);
8477 if (err->code != GOT_ERR_NOT_REF)
8485 const struct got_error *got_worktree_merge_prepare(
8486 struct got_fileindex **fileindex, struct got_worktree *worktree,
8487 struct got_reference *branch, struct got_repository *repo)
8489 const struct got_error *err = NULL;
8490 char *fileindex_path = NULL;
8491 char *branch_refname = NULL, *commit_refname = NULL;
8492 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8493 struct got_reference *commit_ref = NULL;
8494 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8495 struct check_rebase_ok_arg ok_arg;
8499 err = lock_worktree(worktree, LOCK_EX);
8503 err = open_fileindex(fileindex, &fileindex_path, worktree);
8507 /* Preconditions are the same as for rebase. */
8508 ok_arg.worktree = worktree;
8510 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8515 err = get_merge_branch_ref_name(&branch_refname, worktree);
8519 err = get_merge_commit_ref_name(&commit_refname, worktree);
8523 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8528 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8532 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8533 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8537 err = got_ref_resolve(&branch_tip, repo, branch);
8541 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8544 err = got_ref_write(branch_ref, repo);
8548 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8551 err = got_ref_write(commit_ref, repo);
8556 free(branch_refname);
8557 free(commit_refname);
8558 free(fileindex_path);
8560 got_ref_close(branch_ref);
8562 got_ref_close(commit_ref);
8564 got_ref_close(wt_branch);
8565 free(wt_branch_tip);
8568 got_fileindex_free(*fileindex);
8571 lock_worktree(worktree, LOCK_SH);
8576 const struct got_error *
8577 got_worktree_merge_continue(char **branch_name,
8578 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8579 struct got_worktree *worktree, struct got_repository *repo)
8581 const struct got_error *err;
8582 char *commit_refname = NULL, *branch_refname = NULL;
8583 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8584 char *fileindex_path = NULL;
8585 int have_staged_files = 0;
8587 *branch_name = NULL;
8591 err = lock_worktree(worktree, LOCK_EX);
8595 err = open_fileindex(fileindex, &fileindex_path, worktree);
8599 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8600 &have_staged_files);
8601 if (err && err->code != GOT_ERR_CANCELLED)
8603 if (have_staged_files) {
8604 err = got_error(GOT_ERR_STAGED_PATHS);
8608 err = get_merge_branch_ref_name(&branch_refname, worktree);
8612 err = get_merge_commit_ref_name(&commit_refname, worktree);
8616 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8620 if (!got_ref_is_symbolic(branch_ref)) {
8621 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8622 "%s is not a symbolic reference",
8623 got_ref_get_name(branch_ref));
8626 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8627 if (*branch_name == NULL) {
8628 err = got_error_from_errno("strdup");
8632 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8636 err = got_ref_resolve(branch_tip, repo, commit_ref);
8640 free(commit_refname);
8641 free(branch_refname);
8642 free(fileindex_path);
8644 got_ref_close(commit_ref);
8646 got_ref_close(branch_ref);
8650 *branch_name = NULL;
8655 got_fileindex_free(*fileindex);
8658 lock_worktree(worktree, LOCK_SH);
8663 const struct got_error *
8664 got_worktree_merge_abort(struct got_worktree *worktree,
8665 struct got_fileindex *fileindex, struct got_repository *repo,
8666 got_worktree_checkout_cb progress_cb, void *progress_arg)
8668 const struct got_error *err, *unlockerr, *sync_err;
8669 struct got_commit_object *commit = NULL;
8670 char *fileindex_path = NULL;
8671 struct revert_file_args rfa;
8672 char *commit_ref_name = NULL;
8673 struct got_reference *commit_ref = NULL;
8674 struct got_object_id *merged_commit_id = NULL;
8675 struct got_object_id *tree_id = NULL;
8676 struct got_pathlist_head added_paths;
8678 TAILQ_INIT(&added_paths);
8680 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8684 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8688 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8693 * Determine which files in added status can be safely removed
8694 * from disk while reverting changes in the work tree.
8695 * We want to avoid deleting unrelated files which were added by
8696 * the user for conflict resolution purposes.
8698 err = get_paths_added_between_commits(&added_paths,
8699 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8700 got_worktree_get_path_prefix(worktree), repo);
8705 err = got_object_open_as_commit(&commit, repo,
8706 worktree->base_commit_id);
8710 err = got_object_id_by_path(&tree_id, repo, commit,
8711 worktree->path_prefix);
8715 err = delete_merge_refs(worktree, repo);
8719 err = get_fileindex_path(&fileindex_path, worktree);
8723 rfa.worktree = worktree;
8724 rfa.fileindex = fileindex;
8725 rfa.progress_cb = progress_cb;
8726 rfa.progress_arg = progress_arg;
8727 rfa.patch_cb = NULL;
8728 rfa.patch_arg = NULL;
8730 rfa.unlink_added_files = 1;
8731 rfa.added_files_to_unlink = &added_paths;
8732 err = worktree_status(worktree, "", fileindex, repo,
8733 revert_file, &rfa, NULL, NULL, 1, 0);
8737 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8738 repo, progress_cb, progress_arg, NULL, NULL);
8740 sync_err = sync_fileindex(fileindex, fileindex_path);
8741 if (sync_err && err == NULL)
8745 free(merged_commit_id);
8747 got_object_commit_close(commit);
8749 got_fileindex_free(fileindex);
8750 free(fileindex_path);
8752 got_ref_close(commit_ref);
8753 free(commit_ref_name);
8755 unlockerr = lock_worktree(worktree, LOCK_SH);
8756 if (unlockerr && err == NULL)
8761 struct check_stage_ok_arg {
8762 struct got_object_id *head_commit_id;
8763 struct got_worktree *worktree;
8764 struct got_fileindex *fileindex;
8765 struct got_repository *repo;
8769 static const struct got_error *
8770 check_stage_ok(void *arg, unsigned char status,
8771 unsigned char staged_status, const char *relpath,
8772 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8773 struct got_object_id *commit_id, int dirfd, const char *de_name)
8775 struct check_stage_ok_arg *a = arg;
8776 const struct got_error *err = NULL;
8777 struct got_fileindex_entry *ie;
8778 struct got_object_id base_commit_id;
8779 struct got_object_id *base_commit_idp = NULL;
8780 char *in_repo_path = NULL, *p;
8782 if (status == GOT_STATUS_UNVERSIONED ||
8783 status == GOT_STATUS_NO_CHANGE)
8785 if (status == GOT_STATUS_NONEXISTENT)
8786 return got_error_set_errno(ENOENT, relpath);
8788 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8790 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8792 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8793 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8795 return got_error_from_errno("asprintf");
8797 if (got_fileindex_entry_has_commit(ie)) {
8798 base_commit_idp = got_fileindex_entry_get_commit_id(
8799 &base_commit_id, ie);
8802 if (status == GOT_STATUS_CONFLICT) {
8803 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8805 } else if (status != GOT_STATUS_ADD &&
8806 status != GOT_STATUS_MODIFY &&
8807 status != GOT_STATUS_DELETE) {
8808 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8812 a->have_changes = 1;
8817 err = check_out_of_date(p, status, staged_status,
8818 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8819 GOT_ERR_STAGE_OUT_OF_DATE);
8825 struct stage_path_arg {
8826 struct got_worktree *worktree;
8827 struct got_fileindex *fileindex;
8828 struct got_repository *repo;
8829 got_worktree_status_cb status_cb;
8831 got_worktree_patch_cb patch_cb;
8833 int staged_something;
8834 int allow_bad_symlinks;
8837 static const struct got_error *
8838 stage_path(void *arg, unsigned char status,
8839 unsigned char staged_status, const char *relpath,
8840 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8841 struct got_object_id *commit_id, int dirfd, const char *de_name)
8843 struct stage_path_arg *a = arg;
8844 const struct got_error *err = NULL;
8845 struct got_fileindex_entry *ie;
8846 char *ondisk_path = NULL, *path_content = NULL;
8848 struct got_object_id *new_staged_blob_id = NULL;
8851 if (status == GOT_STATUS_UNVERSIONED)
8854 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8856 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8858 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8860 return got_error_from_errno("asprintf");
8863 case GOT_STATUS_ADD:
8864 case GOT_STATUS_MODIFY:
8865 /* XXX could sb.st_mode be passed in by our caller? */
8866 if (lstat(ondisk_path, &sb) == -1) {
8867 err = got_error_from_errno2("lstat", ondisk_path);
8871 if (status == GOT_STATUS_ADD) {
8872 int choice = GOT_PATCH_CHOICE_NONE;
8873 err = (*a->patch_cb)(&choice, a->patch_arg,
8874 status, ie->path, NULL, 1, 1);
8877 if (choice != GOT_PATCH_CHOICE_YES)
8880 err = create_patched_content(&path_content, 0,
8881 staged_blob_id ? staged_blob_id : blob_id,
8882 ondisk_path, dirfd, de_name, ie->path,
8883 a->repo, a->patch_cb, a->patch_arg);
8884 if (err || path_content == NULL)
8888 err = got_object_blob_create(&new_staged_blob_id,
8889 path_content ? path_content : ondisk_path, a->repo);
8892 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8893 SHA1_DIGEST_LENGTH);
8894 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8895 stage = GOT_FILEIDX_STAGE_ADD;
8897 stage = GOT_FILEIDX_STAGE_MODIFY;
8898 got_fileindex_entry_stage_set(ie, stage);
8899 if (S_ISLNK(sb.st_mode)) {
8900 int is_bad_symlink = 0;
8901 if (!a->allow_bad_symlinks) {
8902 char target_path[PATH_MAX];
8904 target_len = readlink(ondisk_path, target_path,
8905 sizeof(target_path));
8906 if (target_len == -1) {
8907 err = got_error_from_errno2("readlink",
8911 err = is_bad_symlink_target(&is_bad_symlink,
8912 target_path, target_len, ondisk_path,
8913 a->worktree->root_path);
8916 if (is_bad_symlink) {
8917 err = got_error_path(ondisk_path,
8918 GOT_ERR_BAD_SYMLINK);
8923 got_fileindex_entry_staged_filetype_set(ie,
8924 GOT_FILEIDX_MODE_BAD_SYMLINK);
8926 got_fileindex_entry_staged_filetype_set(ie,
8927 GOT_FILEIDX_MODE_SYMLINK);
8929 got_fileindex_entry_staged_filetype_set(ie,
8930 GOT_FILEIDX_MODE_REGULAR_FILE);
8932 a->staged_something = 1;
8933 if (a->status_cb == NULL)
8935 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8936 get_staged_status(ie), relpath, blob_id,
8937 new_staged_blob_id, NULL, dirfd, de_name);
8941 * When staging the reverse of the staged diff,
8942 * implicitly unstage the file.
8944 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8945 sizeof(ie->blob_sha1)) == 0) {
8946 got_fileindex_entry_stage_set(ie,
8947 GOT_FILEIDX_STAGE_NONE);
8950 case GOT_STATUS_DELETE:
8951 if (staged_status == GOT_STATUS_DELETE)
8954 int choice = GOT_PATCH_CHOICE_NONE;
8955 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8956 ie->path, NULL, 1, 1);
8959 if (choice == GOT_PATCH_CHOICE_NO)
8961 if (choice != GOT_PATCH_CHOICE_YES) {
8962 err = got_error(GOT_ERR_PATCH_CHOICE);
8966 stage = GOT_FILEIDX_STAGE_DELETE;
8967 got_fileindex_entry_stage_set(ie, stage);
8968 a->staged_something = 1;
8969 if (a->status_cb == NULL)
8971 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8972 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8975 case GOT_STATUS_NO_CHANGE:
8977 case GOT_STATUS_CONFLICT:
8978 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8980 case GOT_STATUS_NONEXISTENT:
8981 err = got_error_set_errno(ENOENT, relpath);
8984 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8988 if (path_content && unlink(path_content) == -1 && err == NULL)
8989 err = got_error_from_errno2("unlink", path_content);
8992 free(new_staged_blob_id);
8996 const struct got_error *
8997 got_worktree_stage(struct got_worktree *worktree,
8998 struct got_pathlist_head *paths,
8999 got_worktree_status_cb status_cb, void *status_arg,
9000 got_worktree_patch_cb patch_cb, void *patch_arg,
9001 int allow_bad_symlinks, struct got_repository *repo)
9003 const struct got_error *err = NULL, *sync_err, *unlockerr;
9004 struct got_pathlist_entry *pe;
9005 struct got_fileindex *fileindex = NULL;
9006 char *fileindex_path = NULL;
9007 struct got_reference *head_ref = NULL;
9008 struct got_object_id *head_commit_id = NULL;
9009 struct check_stage_ok_arg oka;
9010 struct stage_path_arg spa;
9012 err = lock_worktree(worktree, LOCK_EX);
9016 err = got_ref_open(&head_ref, repo,
9017 got_worktree_get_head_ref_name(worktree), 0);
9020 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9023 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9027 /* Check pre-conditions before staging anything. */
9028 oka.head_commit_id = head_commit_id;
9029 oka.worktree = worktree;
9030 oka.fileindex = fileindex;
9032 oka.have_changes = 0;
9033 TAILQ_FOREACH(pe, paths, entry) {
9034 err = worktree_status(worktree, pe->path, fileindex, repo,
9035 check_stage_ok, &oka, NULL, NULL, 1, 0);
9039 if (!oka.have_changes) {
9040 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9044 spa.worktree = worktree;
9045 spa.fileindex = fileindex;
9047 spa.patch_cb = patch_cb;
9048 spa.patch_arg = patch_arg;
9049 spa.status_cb = status_cb;
9050 spa.status_arg = status_arg;
9051 spa.staged_something = 0;
9052 spa.allow_bad_symlinks = allow_bad_symlinks;
9053 TAILQ_FOREACH(pe, paths, entry) {
9054 err = worktree_status(worktree, pe->path, fileindex, repo,
9055 stage_path, &spa, NULL, NULL, 1, 0);
9059 if (!spa.staged_something) {
9060 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9064 sync_err = sync_fileindex(fileindex, fileindex_path);
9065 if (sync_err && err == NULL)
9069 got_ref_close(head_ref);
9070 free(head_commit_id);
9071 free(fileindex_path);
9073 got_fileindex_free(fileindex);
9074 unlockerr = lock_worktree(worktree, LOCK_SH);
9075 if (unlockerr && err == NULL)
9080 struct unstage_path_arg {
9081 struct got_worktree *worktree;
9082 struct got_fileindex *fileindex;
9083 struct got_repository *repo;
9084 got_worktree_checkout_cb progress_cb;
9086 got_worktree_patch_cb patch_cb;
9090 static const struct got_error *
9091 create_unstaged_content(char **path_unstaged_content,
9092 char **path_new_staged_content, struct got_object_id *blob_id,
9093 struct got_object_id *staged_blob_id, const char *relpath,
9094 struct got_repository *repo,
9095 got_worktree_patch_cb patch_cb, void *patch_arg)
9097 const struct got_error *err, *free_err;
9098 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9099 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9100 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9101 struct got_diffreg_result *diffreg_result = NULL;
9102 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9103 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9104 int fd1 = -1, fd2 = -1;
9106 *path_unstaged_content = NULL;
9107 *path_new_staged_content = NULL;
9109 err = got_object_id_str(&label1, blob_id);
9113 fd1 = got_opentempfd();
9115 err = got_error_from_errno("got_opentempfd");
9118 fd2 = got_opentempfd();
9120 err = got_error_from_errno("got_opentempfd");
9124 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9128 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9132 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9136 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9141 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9145 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9149 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9150 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9154 err = got_opentemp_named(path_unstaged_content, &outfile,
9155 "got-unstaged-content", "");
9158 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9159 "got-new-staged-content", "");
9163 if (fseek(f1, 0L, SEEK_SET) == -1) {
9164 err = got_ferror(f1, GOT_ERR_IO);
9167 if (fseek(f2, 0L, SEEK_SET) == -1) {
9168 err = got_ferror(f2, GOT_ERR_IO);
9171 /* Count the number of actual changes in the diff result. */
9172 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9173 struct diff_chunk_context cc = {};
9174 diff_chunk_context_load_change(&cc, &nchunks_used,
9175 diffreg_result->result, n, 0);
9178 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9180 err = apply_or_reject_change(&choice, &nchunks_used,
9181 diffreg_result->result, n, relpath, f1, f2,
9182 &line_cur1, &line_cur2,
9183 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9186 if (choice == GOT_PATCH_CHOICE_YES)
9189 have_rejected_content = 1;
9190 if (choice == GOT_PATCH_CHOICE_QUIT)
9193 if (have_content || have_rejected_content)
9194 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9195 outfile, rejectfile);
9198 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9199 err = got_error_from_errno("close");
9201 got_object_blob_close(blob);
9202 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9203 err = got_error_from_errno("close");
9205 got_object_blob_close(staged_blob);
9206 free_err = got_diffreg_result_free(diffreg_result);
9207 if (free_err && err == NULL)
9209 if (f1 && fclose(f1) == EOF && err == NULL)
9210 err = got_error_from_errno2("fclose", path1);
9211 if (f2 && fclose(f2) == EOF && err == NULL)
9212 err = got_error_from_errno2("fclose", path2);
9213 if (outfile && fclose(outfile) == EOF && err == NULL)
9214 err = got_error_from_errno2("fclose", *path_unstaged_content);
9215 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9216 err = got_error_from_errno2("fclose", *path_new_staged_content);
9217 if (path1 && unlink(path1) == -1 && err == NULL)
9218 err = got_error_from_errno2("unlink", path1);
9219 if (path2 && unlink(path2) == -1 && err == NULL)
9220 err = got_error_from_errno2("unlink", path2);
9221 if (err || !have_content) {
9222 if (*path_unstaged_content &&
9223 unlink(*path_unstaged_content) == -1 && err == NULL)
9224 err = got_error_from_errno2("unlink",
9225 *path_unstaged_content);
9226 free(*path_unstaged_content);
9227 *path_unstaged_content = NULL;
9229 if (err || !have_content || !have_rejected_content) {
9230 if (*path_new_staged_content &&
9231 unlink(*path_new_staged_content) == -1 && err == NULL)
9232 err = got_error_from_errno2("unlink",
9233 *path_new_staged_content);
9234 free(*path_new_staged_content);
9235 *path_new_staged_content = NULL;
9242 static const struct got_error *
9243 unstage_hunks(struct got_object_id *staged_blob_id,
9244 struct got_blob_object *blob_base,
9245 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9246 const char *ondisk_path, const char *label_orig,
9247 struct got_worktree *worktree, struct got_repository *repo,
9248 got_worktree_patch_cb patch_cb, void *patch_arg,
9249 got_worktree_checkout_cb progress_cb, void *progress_arg)
9251 const struct got_error *err = NULL;
9252 char *path_unstaged_content = NULL;
9253 char *path_new_staged_content = NULL;
9254 char *parent = NULL, *base_path = NULL;
9255 char *blob_base_path = NULL;
9256 struct got_object_id *new_staged_blob_id = NULL;
9257 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9260 err = create_unstaged_content(&path_unstaged_content,
9261 &path_new_staged_content, blob_id, staged_blob_id,
9262 ie->path, repo, patch_cb, patch_arg);
9266 if (path_unstaged_content == NULL)
9269 if (path_new_staged_content) {
9270 err = got_object_blob_create(&new_staged_blob_id,
9271 path_new_staged_content, repo);
9276 f = fopen(path_unstaged_content, "re");
9278 err = got_error_from_errno2("fopen",
9279 path_unstaged_content);
9282 if (fstat(fileno(f), &sb) == -1) {
9283 err = got_error_from_errno2("fstat", path_unstaged_content);
9286 if (got_fileindex_entry_staged_filetype_get(ie) ==
9287 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9288 char link_target[PATH_MAX];
9290 r = fread(link_target, 1, sizeof(link_target), f);
9291 if (r == 0 && ferror(f)) {
9292 err = got_error_from_errno("fread");
9295 if (r >= sizeof(link_target)) { /* should not happen */
9296 err = got_error(GOT_ERR_NO_SPACE);
9299 link_target[r] = '\0';
9300 err = merge_symlink(worktree, blob_base,
9301 ondisk_path, ie->path, label_orig, link_target,
9302 worktree->base_commit_id, repo, progress_cb,
9305 int local_changes_subsumed;
9307 err = got_path_dirname(&parent, ondisk_path);
9311 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9313 err = got_error_from_errno("asprintf");
9318 err = got_opentemp_named(&blob_base_path, &f_base,
9322 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9328 * In order the run a 3-way merge with a symlink we copy the symlink's
9329 * target path into a temporary file and use that file with diff3.
9331 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9332 err = dump_symlink_target_path_to_file(&f_deriv2,
9338 fd = open(ondisk_path,
9339 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9341 err = got_error_from_errno2("open", ondisk_path);
9344 f_deriv2 = fdopen(fd, "r");
9345 if (f_deriv2 == NULL) {
9346 err = got_error_from_errno2("fdopen", ondisk_path);
9352 err = merge_file(&local_changes_subsumed, worktree,
9353 f_base, f, f_deriv2, ondisk_path, ie->path,
9354 got_fileindex_perms_to_st(ie),
9355 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9356 repo, progress_cb, progress_arg);
9361 if (new_staged_blob_id) {
9362 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9363 SHA1_DIGEST_LENGTH);
9365 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9366 got_fileindex_entry_staged_filetype_set(ie, 0);
9369 free(new_staged_blob_id);
9370 if (path_unstaged_content &&
9371 unlink(path_unstaged_content) == -1 && err == NULL)
9372 err = got_error_from_errno2("unlink", path_unstaged_content);
9373 if (path_new_staged_content &&
9374 unlink(path_new_staged_content) == -1 && err == NULL)
9375 err = got_error_from_errno2("unlink", path_new_staged_content);
9376 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9377 err = got_error_from_errno2("unlink", blob_base_path);
9378 if (f_base && fclose(f_base) == EOF && err == NULL)
9379 err = got_error_from_errno2("fclose", path_unstaged_content);
9380 if (f && fclose(f) == EOF && err == NULL)
9381 err = got_error_from_errno2("fclose", path_unstaged_content);
9382 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9383 err = got_error_from_errno2("fclose", ondisk_path);
9384 free(path_unstaged_content);
9385 free(path_new_staged_content);
9386 free(blob_base_path);
9392 static const struct got_error *
9393 unstage_path(void *arg, unsigned char status,
9394 unsigned char staged_status, const char *relpath,
9395 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9396 struct got_object_id *commit_id, int dirfd, const char *de_name)
9398 const struct got_error *err = NULL;
9399 struct unstage_path_arg *a = arg;
9400 struct got_fileindex_entry *ie;
9401 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9402 char *ondisk_path = NULL;
9403 char *id_str = NULL, *label_orig = NULL;
9404 int local_changes_subsumed;
9406 int fd1 = -1, fd2 = -1;
9408 if (staged_status != GOT_STATUS_ADD &&
9409 staged_status != GOT_STATUS_MODIFY &&
9410 staged_status != GOT_STATUS_DELETE)
9413 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9415 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9417 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9419 return got_error_from_errno("asprintf");
9421 err = got_object_id_str(&id_str,
9422 commit_id ? commit_id : a->worktree->base_commit_id);
9425 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9427 err = got_error_from_errno("asprintf");
9431 fd1 = got_opentempfd();
9433 err = got_error_from_errno("got_opentempfd");
9436 fd2 = got_opentempfd();
9438 err = got_error_from_errno("got_opentempfd");
9442 switch (staged_status) {
9443 case GOT_STATUS_MODIFY:
9444 err = got_object_open_as_blob(&blob_base, a->repo,
9445 blob_id, 8192, fd1);
9449 case GOT_STATUS_ADD:
9451 if (staged_status == GOT_STATUS_ADD) {
9452 int choice = GOT_PATCH_CHOICE_NONE;
9453 err = (*a->patch_cb)(&choice, a->patch_arg,
9454 staged_status, ie->path, NULL, 1, 1);
9457 if (choice != GOT_PATCH_CHOICE_YES)
9460 err = unstage_hunks(staged_blob_id,
9461 blob_base, blob_id, ie, ondisk_path,
9462 label_orig, a->worktree, a->repo,
9463 a->patch_cb, a->patch_arg,
9464 a->progress_cb, a->progress_arg);
9465 break; /* Done with this file. */
9468 err = got_object_open_as_blob(&blob_staged, a->repo,
9469 staged_blob_id, 8192, fd2);
9472 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9473 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9474 case GOT_FILEIDX_MODE_REGULAR_FILE:
9475 err = merge_blob(&local_changes_subsumed, a->worktree,
9476 blob_base, ondisk_path, relpath,
9477 got_fileindex_perms_to_st(ie), label_orig,
9478 blob_staged, commit_id ? commit_id :
9479 a->worktree->base_commit_id, a->repo,
9480 a->progress_cb, a->progress_arg);
9482 case GOT_FILEIDX_MODE_SYMLINK:
9483 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9484 char *staged_target;
9485 err = got_object_blob_read_to_str(
9486 &staged_target, blob_staged);
9489 err = merge_symlink(a->worktree, blob_base,
9490 ondisk_path, relpath, label_orig,
9491 staged_target, commit_id ? commit_id :
9492 a->worktree->base_commit_id,
9493 a->repo, a->progress_cb, a->progress_arg);
9494 free(staged_target);
9496 err = merge_blob(&local_changes_subsumed,
9497 a->worktree, blob_base, ondisk_path,
9498 relpath, got_fileindex_perms_to_st(ie),
9499 label_orig, blob_staged,
9500 commit_id ? commit_id :
9501 a->worktree->base_commit_id, a->repo,
9502 a->progress_cb, a->progress_arg);
9506 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9510 got_fileindex_entry_stage_set(ie,
9511 GOT_FILEIDX_STAGE_NONE);
9512 got_fileindex_entry_staged_filetype_set(ie, 0);
9515 case GOT_STATUS_DELETE:
9517 int choice = GOT_PATCH_CHOICE_NONE;
9518 err = (*a->patch_cb)(&choice, a->patch_arg,
9519 staged_status, ie->path, NULL, 1, 1);
9522 if (choice == GOT_PATCH_CHOICE_NO)
9524 if (choice != GOT_PATCH_CHOICE_YES) {
9525 err = got_error(GOT_ERR_PATCH_CHOICE);
9529 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9530 got_fileindex_entry_staged_filetype_set(ie, 0);
9531 err = get_file_status(&status, &sb, ie, ondisk_path,
9532 dirfd, de_name, a->repo);
9535 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9540 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9541 err = got_error_from_errno("close");
9543 got_object_blob_close(blob_base);
9544 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9545 err = got_error_from_errno("close");
9547 got_object_blob_close(blob_staged);
9553 const struct got_error *
9554 got_worktree_unstage(struct got_worktree *worktree,
9555 struct got_pathlist_head *paths,
9556 got_worktree_checkout_cb progress_cb, void *progress_arg,
9557 got_worktree_patch_cb patch_cb, void *patch_arg,
9558 struct got_repository *repo)
9560 const struct got_error *err = NULL, *sync_err, *unlockerr;
9561 struct got_pathlist_entry *pe;
9562 struct got_fileindex *fileindex = NULL;
9563 char *fileindex_path = NULL;
9564 struct unstage_path_arg upa;
9566 err = lock_worktree(worktree, LOCK_EX);
9570 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9574 upa.worktree = worktree;
9575 upa.fileindex = fileindex;
9577 upa.progress_cb = progress_cb;
9578 upa.progress_arg = progress_arg;
9579 upa.patch_cb = patch_cb;
9580 upa.patch_arg = patch_arg;
9581 TAILQ_FOREACH(pe, paths, entry) {
9582 err = worktree_status(worktree, pe->path, fileindex, repo,
9583 unstage_path, &upa, NULL, NULL, 1, 0);
9588 sync_err = sync_fileindex(fileindex, fileindex_path);
9589 if (sync_err && err == NULL)
9592 free(fileindex_path);
9594 got_fileindex_free(fileindex);
9595 unlockerr = lock_worktree(worktree, LOCK_SH);
9596 if (unlockerr && err == NULL)
9601 struct report_file_info_arg {
9602 struct got_worktree *worktree;
9603 got_worktree_path_info_cb info_cb;
9605 struct got_pathlist_head *paths;
9606 got_cancel_cb cancel_cb;
9610 static const struct got_error *
9611 report_file_info(void *arg, struct got_fileindex_entry *ie)
9613 struct report_file_info_arg *a = arg;
9614 struct got_pathlist_entry *pe;
9615 struct got_object_id blob_id, staged_blob_id, commit_id;
9616 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9617 struct got_object_id *commit_idp = NULL;
9620 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9621 return got_error(GOT_ERR_CANCELLED);
9623 TAILQ_FOREACH(pe, a->paths, entry) {
9624 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9625 got_path_is_child(ie->path, pe->path, pe->path_len))
9628 if (pe == NULL) /* not found */
9631 if (got_fileindex_entry_has_blob(ie))
9632 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9633 stage = got_fileindex_entry_stage_get(ie);
9634 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9635 stage == GOT_FILEIDX_STAGE_ADD) {
9636 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9637 &staged_blob_id, ie);
9640 if (got_fileindex_entry_has_commit(ie))
9641 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9643 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9644 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9647 const struct got_error *
9648 got_worktree_path_info(struct got_worktree *worktree,
9649 struct got_pathlist_head *paths,
9650 got_worktree_path_info_cb info_cb, void *info_arg,
9651 got_cancel_cb cancel_cb, void *cancel_arg)
9654 const struct got_error *err = NULL, *unlockerr;
9655 struct got_fileindex *fileindex = NULL;
9656 char *fileindex_path = NULL;
9657 struct report_file_info_arg arg;
9659 err = lock_worktree(worktree, LOCK_SH);
9663 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9667 arg.worktree = worktree;
9668 arg.info_cb = info_cb;
9669 arg.info_arg = info_arg;
9671 arg.cancel_cb = cancel_cb;
9672 arg.cancel_arg = cancel_arg;
9673 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9676 free(fileindex_path);
9678 got_fileindex_free(fileindex);
9679 unlockerr = lock_worktree(worktree, LOCK_UN);
9680 if (unlockerr && err == NULL)
9685 static const struct got_error *
9686 patch_check_path(const char *p, char **path, unsigned char *status,
9687 unsigned char *staged_status, struct got_fileindex *fileindex,
9688 struct got_worktree *worktree, struct got_repository *repo)
9690 const struct got_error *err;
9691 struct got_fileindex_entry *ie;
9693 char *ondisk_path = NULL;
9695 err = got_worktree_resolve_path(path, worktree, p);
9699 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9700 *path[0] ? "/" : "", *path) == -1)
9701 return got_error_from_errno("asprintf");
9703 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9705 *staged_status = get_staged_status(ie);
9706 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9711 *staged_status = GOT_STATUS_NO_CHANGE;
9712 *status = GOT_STATUS_UNVERSIONED;
9713 if (lstat(ondisk_path, &sb) == -1) {
9714 if (errno != ENOENT) {
9715 err = got_error_from_errno2("lstat",
9719 *status = GOT_STATUS_NONEXISTENT;
9728 static const struct got_error *
9729 patch_can_rm(const char *path, unsigned char status,
9730 unsigned char staged_status)
9732 if (status == GOT_STATUS_NONEXISTENT)
9733 return got_error_set_errno(ENOENT, path);
9734 if (status != GOT_STATUS_NO_CHANGE &&
9735 status != GOT_STATUS_ADD &&
9736 status != GOT_STATUS_MODIFY &&
9737 status != GOT_STATUS_MODE_CHANGE)
9738 return got_error_path(path, GOT_ERR_FILE_STATUS);
9739 if (staged_status == GOT_STATUS_DELETE)
9740 return got_error_path(path, GOT_ERR_FILE_STATUS);
9744 static const struct got_error *
9745 patch_can_add(const char *path, unsigned char status)
9747 if (status != GOT_STATUS_NONEXISTENT)
9748 return got_error_path(path, GOT_ERR_FILE_STATUS);
9752 static const struct got_error *
9753 patch_can_edit(const char *path, unsigned char status,
9754 unsigned char staged_status)
9756 if (status == GOT_STATUS_NONEXISTENT)
9757 return got_error_set_errno(ENOENT, path);
9758 if (status != GOT_STATUS_NO_CHANGE &&
9759 status != GOT_STATUS_ADD &&
9760 status != GOT_STATUS_MODIFY)
9761 return got_error_path(path, GOT_ERR_FILE_STATUS);
9762 if (staged_status == GOT_STATUS_DELETE)
9763 return got_error_path(path, GOT_ERR_FILE_STATUS);
9767 const struct got_error *
9768 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9769 char **fileindex_path, struct got_worktree *worktree)
9771 return open_fileindex(fileindex, fileindex_path, worktree);
9774 const struct got_error *
9775 got_worktree_patch_check_path(const char *old, const char *new,
9776 char **oldpath, char **newpath, struct got_worktree *worktree,
9777 struct got_repository *repo, struct got_fileindex *fileindex)
9779 const struct got_error *err = NULL;
9780 int file_renamed = 0;
9781 unsigned char status_old, staged_status_old;
9782 unsigned char status_new, staged_status_new;
9787 err = patch_check_path(old != NULL ? old : new, oldpath,
9788 &status_old, &staged_status_old, fileindex, worktree, repo);
9792 err = patch_check_path(new != NULL ? new : old, newpath,
9793 &status_new, &staged_status_new, fileindex, worktree, repo);
9797 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9800 if (old != NULL && new == NULL)
9801 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9802 else if (file_renamed) {
9803 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9805 err = patch_can_add(*newpath, status_new);
9806 } else if (old == NULL)
9807 err = patch_can_add(*newpath, status_new);
9809 err = patch_can_edit(*newpath, status_new, staged_status_new);
9821 const struct got_error *
9822 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9823 struct got_worktree *worktree, struct got_fileindex *fileindex,
9824 got_worktree_checkout_cb progress_cb, void *progress_arg)
9826 struct schedule_addition_args saa;
9828 memset(&saa, 0, sizeof(saa));
9829 saa.worktree = worktree;
9830 saa.fileindex = fileindex;
9831 saa.progress_cb = progress_cb;
9832 saa.progress_arg = progress_arg;
9835 return worktree_status(worktree, path, fileindex, repo,
9836 schedule_addition, &saa, NULL, NULL, 1, 0);
9839 const struct got_error *
9840 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9841 struct got_worktree *worktree, struct got_fileindex *fileindex,
9842 got_worktree_delete_cb progress_cb, void *progress_arg)
9844 struct schedule_deletion_args sda;
9846 memset(&sda, 0, sizeof(sda));
9847 sda.worktree = worktree;
9848 sda.fileindex = fileindex;
9849 sda.progress_cb = progress_cb;
9850 sda.progress_arg = progress_arg;
9852 sda.delete_local_mods = 0;
9853 sda.keep_on_disk = 0;
9854 sda.ignore_missing_paths = 0;
9855 sda.status_codes = NULL;
9857 return worktree_status(worktree, path, fileindex, repo,
9858 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9861 const struct got_error *
9862 got_worktree_patch_complete(struct got_fileindex *fileindex,
9863 const char *fileindex_path)
9865 const struct got_error *err = NULL;
9867 err = sync_fileindex(fileindex, fileindex_path);
9868 got_fileindex_free(fileindex);