2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 #define WORKTREE_ALGO(wt) ((wt)->base_commit_id->algo)
70 static mode_t apply_umask(mode_t);
72 static const struct got_error *
73 create_meta_file(const char *path_got, const char *name, const char *content)
75 const struct got_error *err = NULL;
78 if (asprintf(&path, "%s/%s", path_got, name) == -1)
79 return got_error_from_errno("asprintf");
81 err = got_path_create_file(path, content);
86 static const struct got_error *
87 update_meta_file(const char *path_got, const char *name, const char *content)
89 const struct got_error *err = NULL;
94 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
95 err = got_error_from_errno("asprintf");
100 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
105 int len = fprintf(tmpfile, "%s\n", content);
106 if (len != strlen(content) + 1) {
107 err = got_error_from_errno2("fprintf", tmppath);
112 if (rename(tmppath, path) != 0) {
113 err = got_error_from_errno3("rename", tmppath, path);
119 if (fclose(tmpfile) == EOF && err == NULL)
120 err = got_error_from_errno2("fclose", tmppath);
125 static const struct got_error *
126 write_head_ref(const char *path_got, struct got_reference *head_ref)
128 const struct got_error *err = NULL;
131 if (got_ref_is_symbolic(head_ref)) {
132 refstr = got_ref_to_str(head_ref);
134 return got_error_from_errno("got_ref_to_str");
136 refstr = strdup(got_ref_get_name(head_ref));
138 return got_error_from_errno("strdup");
140 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
145 const struct got_error *
146 got_worktree_init(const char *path, struct got_reference *head_ref,
147 const char *prefix, struct got_repository *repo)
149 const struct got_error *err = NULL;
150 struct got_object_id *commit_id = NULL;
152 uint32_t uuid_status;
154 char *path_got = NULL;
155 char *formatstr = NULL;
156 char *absprefix = NULL;
157 char *basestr = NULL;
158 char *uuidstr = NULL;
160 if (strcmp(path, got_repo_get_path(repo)) == 0) {
161 err = got_error(GOT_ERR_WORKTREE_REPO);
165 err = got_ref_resolve(&commit_id, repo, head_ref);
168 err = got_object_get_type(&obj_type, repo, commit_id);
171 if (obj_type != GOT_OBJ_TYPE_COMMIT)
172 return got_error(GOT_ERR_OBJ_TYPE);
174 if (!got_path_is_absolute(prefix)) {
175 if (asprintf(&absprefix, "/%s", prefix) == -1)
176 return got_error_from_errno("asprintf");
179 /* Create top-level directory (may already exist). */
180 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
181 err = got_error_from_errno2("mkdir", path);
185 /* Create .got directory (may already exist). */
186 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
187 err = got_error_from_errno("asprintf");
190 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
191 err = got_error_from_errno2("mkdir", path_got);
195 /* Create an empty lock file. */
196 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
200 /* Create an empty file index. */
201 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
205 /* Write the HEAD reference. */
206 err = write_head_ref(path_got, head_ref);
210 /* Record our base commit. */
211 err = got_object_id_str(&basestr, commit_id);
214 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
218 /* Store path to repository. */
219 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
220 got_repo_get_path(repo));
224 /* Store in-repository path prefix. */
225 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
226 absprefix ? absprefix : prefix);
231 uuid_create(&uuid, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_create");
236 uuid_to_string(&uuid, &uuidstr, &uuid_status);
237 if (uuid_status != uuid_s_ok) {
238 err = got_error_uuid(uuid_status, "uuid_to_string");
241 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
245 /* Stamp work tree with format file. */
246 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
247 err = got_error_from_errno("asprintf");
250 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
264 const struct got_error *
265 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
266 const char *path_prefix)
268 char *absprefix = NULL;
270 if (!got_path_is_absolute(path_prefix)) {
271 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
272 return got_error_from_errno("asprintf");
274 *match = (strcmp(absprefix ? absprefix : path_prefix,
275 worktree->path_prefix) == 0);
281 got_worktree_get_head_ref_name(struct got_worktree *worktree)
283 return worktree->head_ref_name;
286 const struct got_error *
287 got_worktree_set_head_ref(struct got_worktree *worktree,
288 struct got_reference *head_ref)
290 const struct got_error *err = NULL;
291 char *path_got = NULL, *head_ref_name = NULL;
293 if (asprintf(&path_got, "%s/%s", worktree->root_path,
294 GOT_WORKTREE_GOT_DIR) == -1) {
295 err = got_error_from_errno("asprintf");
300 head_ref_name = strdup(got_ref_get_name(head_ref));
301 if (head_ref_name == NULL) {
302 err = got_error_from_errno("strdup");
306 err = write_head_ref(path_got, head_ref);
310 free(worktree->head_ref_name);
311 worktree->head_ref_name = head_ref_name;
319 struct got_object_id *
320 got_worktree_get_base_commit_id(struct got_worktree *worktree)
322 return worktree->base_commit_id;
325 const struct got_error *
326 got_worktree_set_base_commit_id(struct got_worktree *worktree,
327 struct got_repository *repo, struct got_object_id *commit_id)
329 const struct got_error *err;
330 struct got_object *obj = NULL;
332 char *path_got = NULL;
334 if (asprintf(&path_got, "%s/%s", worktree->root_path,
335 GOT_WORKTREE_GOT_DIR) == -1) {
336 err = got_error_from_errno("asprintf");
341 err = got_object_open(&obj, repo, commit_id);
345 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
346 err = got_error(GOT_ERR_OBJ_TYPE);
350 /* Record our base commit. */
351 err = got_object_id_str(&id_str, commit_id);
354 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
358 free(worktree->base_commit_id);
359 worktree->base_commit_id = got_object_id_dup(commit_id);
360 if (worktree->base_commit_id == NULL) {
361 err = got_error_from_errno("got_object_id_dup");
366 got_object_close(obj);
372 const struct got_gotconfig *
373 got_worktree_get_gotconfig(struct got_worktree *worktree)
375 return worktree->gotconfig;
378 static const struct got_error *
379 lock_worktree(struct got_worktree *worktree, int operation)
381 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
382 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
383 : got_error_from_errno2("flock",
384 got_worktree_get_root_path(worktree)));
388 static const struct got_error *
389 add_dir_on_disk(struct got_worktree *worktree, const char *path)
391 const struct got_error *err = NULL;
394 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
395 return got_error_from_errno("asprintf");
397 err = got_path_mkdir(abspath);
398 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
401 if (lstat(abspath, &sb) == -1) {
402 err = got_error_from_errno2("lstat", abspath);
403 } else if (!S_ISDIR(sb.st_mode)) {
404 /* TODO directory is obstructed; do something */
405 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
412 static const struct got_error *
413 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
415 const struct got_error *err = NULL;
418 size_t flen1 = 0, flen2 = 0;
423 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
424 if (flen1 == 0 && ferror(f1)) {
425 err = got_error_from_errno("fread");
428 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
429 if (flen2 == 0 && ferror(f2)) {
430 err = got_error_from_errno("fread");
437 } else if (flen2 == 0) {
441 } else if (flen1 == flen2) {
442 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
455 static const struct got_error *
456 check_files_equal(int *same, FILE *f1, FILE *f2)
463 if (fstat(fileno(f1), &sb) != 0)
464 return got_error_from_errno("fstat");
467 if (fstat(fileno(f2), &sb) != 0)
468 return got_error_from_errno("fstat");
471 if (size1 != size2) {
476 if (fseek(f1, 0L, SEEK_SET) == -1)
477 return got_ferror(f1, GOT_ERR_IO);
478 if (fseek(f2, 0L, SEEK_SET) == -1)
479 return got_ferror(f2, GOT_ERR_IO);
481 return check_file_contents_equal(same, f1, f2);
484 static const struct got_error *
485 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
493 if (fseek(f, 0L, SEEK_SET) == -1)
494 return got_ferror(f, GOT_ERR_IO);
497 flen = fread(fbuf, 1, sizeof(fbuf), f);
500 return got_error_from_errno("fread");
504 outlen = write(outfd, fbuf, flen);
506 return got_error_from_errno("write");
508 return got_error(GOT_ERR_IO);
515 static const struct got_error *
516 merge_binary_file(int *overlapcnt, int merged_fd,
517 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
518 const char *label_deriv, const char *label_orig, const char *label_deriv2,
519 const char *ondisk_path)
521 const struct got_error *err = NULL;
522 int same_content, changed_deriv, changed_deriv2;
523 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
524 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
525 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
526 char *base_path_orig = NULL, *base_path_deriv = NULL;
527 char *base_path_deriv2 = NULL;
531 err = check_files_equal(&same_content, f_deriv, f_deriv2);
536 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
538 err = check_files_equal(&same_content, f_deriv, f_orig);
541 changed_deriv = !same_content;
542 err = check_files_equal(&same_content, f_deriv2, f_orig);
545 changed_deriv2 = !same_content;
547 if (changed_deriv && changed_deriv2) {
549 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
550 err = got_error_from_errno("asprintf");
553 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
554 err = got_error_from_errno("asprintf");
557 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
558 err = got_error_from_errno("asprintf");
561 err = got_opentemp_named_fd(&path_orig, &fd_orig,
565 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
566 base_path_deriv, "");
569 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
570 base_path_deriv2, "");
573 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
576 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
579 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
582 if (dprintf(merged_fd, "Binary files differ and cannot be "
583 "merged automatically:\n") < 0) {
584 err = got_error_from_errno("dprintf");
587 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
588 GOT_DIFF_CONFLICT_MARKER_BEGIN,
589 label_deriv ? " " : "",
590 label_deriv ? label_deriv : "",
592 err = got_error_from_errno("dprintf");
596 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
597 GOT_DIFF_CONFLICT_MARKER_ORIG,
598 label_orig ? " " : "",
599 label_orig ? label_orig : "",
601 err = got_error_from_errno("dprintf");
605 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
606 GOT_DIFF_CONFLICT_MARKER_SEP,
608 GOT_DIFF_CONFLICT_MARKER_END,
609 label_deriv2 ? " " : "",
610 label_deriv2 ? label_deriv2 : "") < 0) {
611 err = got_error_from_errno("dprintf");
614 } else if (changed_deriv)
615 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
616 else if (changed_deriv2)
617 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
619 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
621 err = got_error_from_errno2("unlink", path_orig);
622 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
623 err = got_error_from_errno2("close", path_orig);
624 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
625 err = got_error_from_errno2("close", path_deriv);
626 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
627 err = got_error_from_errno2("close", path_deriv2);
631 free(base_path_orig);
632 free(base_path_deriv);
633 free(base_path_deriv2);
638 * Perform a 3-way merge where the file f_orig acts as the common
639 * ancestor, the file f_deriv acts as the first derived version,
640 * and the file f_deriv2 acts as the second derived version.
641 * The merge result will be written to a new file at ondisk_path; any
642 * existing file at this path will be replaced.
644 static const struct got_error *
645 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
646 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
647 const char *path, uint16_t st_mode,
648 const char *label_orig, const char *label_deriv, const char *label_deriv2,
649 enum got_diff_algorithm diff_algo, struct got_repository *repo,
650 got_worktree_checkout_cb progress_cb, void *progress_arg)
652 const struct got_error *err = NULL;
654 FILE *f_merged = NULL;
655 char *merged_path = NULL, *base_path = NULL;
659 *local_changes_subsumed = 0;
661 err = got_path_dirname(&parent, ondisk_path);
665 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
666 err = got_error_from_errno("asprintf");
670 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
674 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
675 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
677 if (err->code != GOT_ERR_FILE_BINARY)
679 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
680 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
686 err = (*progress_cb)(progress_arg,
687 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
691 if (fsync(merged_fd) != 0) {
692 err = got_error_from_errno("fsync");
696 f_merged = fdopen(merged_fd, "r");
697 if (f_merged == NULL) {
698 err = got_error_from_errno("fdopen");
703 /* Check if a clean merge has subsumed all local changes. */
704 if (overlapcnt == 0) {
705 err = check_files_equal(local_changes_subsumed, f_deriv,
711 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
712 err = got_error_from_errno2("fchmod", merged_path);
716 if (rename(merged_path, ondisk_path) != 0) {
717 err = got_error_from_errno3("rename", merged_path,
726 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
727 err = got_error_from_errno("close");
728 if (f_merged && fclose(f_merged) == EOF && err == NULL)
729 err = got_error_from_errno("fclose");
736 static const struct got_error *
737 update_symlink(const char *ondisk_path, const char *target_path,
740 /* This is not atomic but matches what 'ln -sf' does. */
741 if (unlink(ondisk_path) == -1)
742 return got_error_from_errno2("unlink", ondisk_path);
743 if (symlink(target_path, ondisk_path) == -1)
744 return got_error_from_errno3("symlink", target_path,
750 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
751 * in the work tree with a file that contains conflict markers and the
752 * conflicting target paths of the original version, a "derived version"
753 * of a symlink from an incoming change, and a local version of the symlink.
755 * The original versions's target path can be NULL if it is not available,
756 * such as if both derived versions added a new symlink at the same path.
758 * The incoming derived symlink target is NULL in case the incoming change
759 * has deleted this symlink.
761 static const struct got_error *
762 install_symlink_conflict(const char *deriv_target,
763 struct got_object_id *deriv_base_commit_id, const char *orig_target,
764 const char *label_orig, const char *local_target, const char *ondisk_path)
766 const struct got_error *err;
767 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
770 err = got_object_id_str(&id_str, deriv_base_commit_id);
772 return got_error_from_errno("asprintf");
774 if (asprintf(&label_deriv, "%s: commit %s",
775 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
776 err = got_error_from_errno("asprintf");
780 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
784 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
785 err = got_error_from_errno2("fchmod", path);
789 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
790 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
791 deriv_target ? deriv_target : "(symlink was deleted)",
792 orig_target ? label_orig : "",
793 orig_target ? "\n" : "",
794 orig_target ? orig_target : "",
795 orig_target ? "\n" : "",
796 GOT_DIFF_CONFLICT_MARKER_SEP,
797 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
798 err = got_error_from_errno2("fprintf", path);
802 if (unlink(ondisk_path) == -1) {
803 err = got_error_from_errno2("unlink", ondisk_path);
806 if (rename(path, ondisk_path) == -1) {
807 err = got_error_from_errno3("rename", path, ondisk_path);
811 if (f != NULL && fclose(f) == EOF && err == NULL)
812 err = got_error_from_errno2("fclose", path);
819 /* forward declaration */
820 static const struct got_error *
821 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
822 const char *, const char *, uint16_t, const char *,
823 struct got_blob_object *, struct got_object_id *,
824 struct got_repository *, got_worktree_checkout_cb, void *);
827 * Merge a symlink into the work tree, where blob_orig acts as the common
828 * ancestor, deriv_target is the link target of the first derived version,
829 * and the symlink on disk acts as the second derived version.
830 * Assume that contents of both blobs represent symlinks.
832 static const struct got_error *
833 merge_symlink(struct got_worktree *worktree,
834 struct got_blob_object *blob_orig, const char *ondisk_path,
835 const char *path, const char *label_orig, const char *deriv_target,
836 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
837 got_worktree_checkout_cb progress_cb, void *progress_arg)
839 const struct got_error *err = NULL;
840 char *ancestor_target = NULL;
842 ssize_t ondisk_len, deriv_len;
843 char ondisk_target[PATH_MAX];
844 int have_local_change = 0;
845 int have_incoming_change = 0;
847 if (lstat(ondisk_path, &sb) == -1)
848 return got_error_from_errno2("lstat", ondisk_path);
850 ondisk_len = readlink(ondisk_path, ondisk_target,
851 sizeof(ondisk_target));
852 if (ondisk_len == -1) {
853 err = got_error_from_errno2("readlink",
857 ondisk_target[ondisk_len] = '\0';
860 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
865 if (ancestor_target == NULL ||
866 (ondisk_len != strlen(ancestor_target) ||
867 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
868 have_local_change = 1;
870 deriv_len = strlen(deriv_target);
871 if (ancestor_target == NULL ||
872 (deriv_len != strlen(ancestor_target) ||
873 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
874 have_incoming_change = 1;
876 if (!have_local_change && !have_incoming_change) {
877 if (ancestor_target) {
878 /* Both sides made the same change. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
881 } else if (deriv_len == ondisk_len &&
882 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
883 /* Both sides added the same symlink. */
884 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
887 /* Both sides added symlinks which don't match. */
888 err = install_symlink_conflict(deriv_target,
889 deriv_base_commit_id, ancestor_target,
890 label_orig, ondisk_target, ondisk_path);
893 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
896 } else if (!have_local_change && have_incoming_change) {
897 /* Apply the incoming change. */
898 err = update_symlink(ondisk_path, deriv_target,
899 strlen(deriv_target));
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
903 } else if (have_local_change && have_incoming_change) {
904 if (deriv_len == ondisk_len &&
905 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
906 /* Both sides made the same change. */
907 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
910 err = install_symlink_conflict(deriv_target,
911 deriv_base_commit_id, ancestor_target, label_orig,
912 ondisk_target, ondisk_path);
915 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
921 free(ancestor_target);
925 static const struct got_error *
926 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
928 const struct got_error *err = NULL;
929 char target_path[PATH_MAX];
938 return got_error_from_errno("got_opentemp");
939 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
940 if (target_len == -1) {
941 err = got_error_from_errno2("readlink", ondisk_path);
944 n = fwrite(target_path, 1, target_len, f);
945 if (n != target_len) {
946 err = got_ferror(f, GOT_ERR_IO);
949 if (fflush(f) == EOF) {
950 err = got_error_from_errno("fflush");
953 if (fseek(f, 0L, SEEK_SET) == -1) {
954 err = got_ferror(f, GOT_ERR_IO);
966 * Perform a 3-way merge where blob_orig acts as the common ancestor,
967 * blob_deriv acts as the first derived version, and the file on disk
968 * acts as the second derived version.
970 static const struct got_error *
971 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
972 struct got_blob_object *blob_orig, const char *ondisk_path,
973 const char *path, uint16_t st_mode, const char *label_orig,
974 struct got_blob_object *blob_deriv,
975 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
976 got_worktree_checkout_cb progress_cb, void *progress_arg)
978 const struct got_error *err = NULL;
979 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
980 char *blob_orig_path = NULL;
981 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
982 char *label_deriv = NULL, *parent = NULL;
984 *local_changes_subsumed = 0;
986 err = got_path_dirname(&parent, ondisk_path);
991 if (asprintf(&base_path, "%s/got-merge-blob-orig",
993 err = got_error_from_errno("asprintf");
998 err = got_opentemp_named(&blob_orig_path, &f_orig,
1002 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1009 * No common ancestor exists. This is an "add vs add" conflict
1010 * and we simply use an empty ancestor file to make both files
1011 * appear in the merged result in their entirety.
1013 f_orig = got_opentemp();
1014 if (f_orig == NULL) {
1015 err = got_error_from_errno("got_opentemp");
1020 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1021 err = got_error_from_errno("asprintf");
1026 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1029 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1034 err = got_object_id_str(&id_str, deriv_base_commit_id);
1037 if (asprintf(&label_deriv, "%s: commit %s",
1038 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1039 err = got_error_from_errno("asprintf");
1044 * In order the run a 3-way merge with a symlink we copy the symlink's
1045 * target path into a temporary file and use that file with diff3.
1047 if (S_ISLNK(st_mode)) {
1048 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1053 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1055 err = got_error_from_errno2("open", ondisk_path);
1058 f_deriv2 = fdopen(fd, "r");
1059 if (f_deriv2 == NULL) {
1060 err = got_error_from_errno2("fdopen", ondisk_path);
1066 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1067 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1068 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1070 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1072 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1073 err = got_error_from_errno("fclose");
1074 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1075 err = got_error_from_errno("fclose");
1077 if (blob_orig_path) {
1078 unlink(blob_orig_path);
1079 free(blob_orig_path);
1081 if (blob_deriv_path) {
1082 unlink(blob_deriv_path);
1083 free(blob_deriv_path);
1091 static const struct got_error *
1092 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1093 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1094 int wt_fd, const char *path, struct got_object_id *blob_id)
1096 const struct got_error *err = NULL;
1097 struct got_fileindex_entry *new_ie;
1101 err = got_fileindex_entry_alloc(&new_ie, path);
1105 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1106 blob_id->hash, base_commit_id->hash, 1);
1110 err = got_fileindex_entry_add(fileindex, new_ie);
1113 got_fileindex_entry_free(new_ie);
1120 get_ondisk_perms(int executable, mode_t st_mode)
1122 mode_t xbits = S_IXUSR;
1125 /* Map read bits to execute bits. */
1126 if (st_mode & S_IRGRP)
1128 if (st_mode & S_IROTH)
1130 return st_mode | xbits;
1137 apply_umask(mode_t mode)
1146 /* forward declaration */
1147 static const struct got_error *
1148 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1149 const char *path, mode_t te_mode, mode_t st_mode,
1150 struct got_blob_object *blob, int restoring_missing_file,
1151 int reverting_versioned_file, int installing_bad_symlink,
1152 int path_is_unversioned, struct got_repository *repo,
1153 got_worktree_checkout_cb progress_cb, void *progress_arg);
1156 * This function assumes that the provided symlink target points at a
1157 * safe location in the work tree!
1159 static const struct got_error *
1160 replace_existing_symlink(int *did_something, const char *ondisk_path,
1161 const char *target_path, size_t target_len)
1163 const struct got_error *err = NULL;
1165 char etarget[PATH_MAX];
1171 * "Bad" symlinks (those pointing outside the work tree or into the
1172 * .got directory) are installed in the work tree as a regular file
1173 * which contains the bad symlink target path.
1174 * The new symlink target has already been checked for safety by our
1175 * caller. If we can successfully open a regular file then we simply
1176 * replace this file with a symlink below.
1178 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1180 if (!got_err_open_nofollow_on_symlink())
1181 return got_error_from_errno2("open", ondisk_path);
1183 /* We are updating an existing on-disk symlink. */
1184 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1186 return got_error_from_errno2("readlink", ondisk_path);
1188 if (elen == target_len &&
1189 memcmp(etarget, target_path, target_len) == 0)
1190 return NULL; /* nothing to do */
1194 err = update_symlink(ondisk_path, target_path, target_len);
1195 if (fd != -1 && close(fd) == -1 && err == NULL)
1196 err = got_error_from_errno2("close", ondisk_path);
1200 static const struct got_error *
1201 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1202 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1204 const struct got_error *err = NULL;
1205 char canonpath[PATH_MAX];
1206 char *path_got = NULL;
1208 *is_bad_symlink = 0;
1210 if (target_len >= sizeof(canonpath)) {
1211 *is_bad_symlink = 1;
1216 * We do not use realpath(3) to resolve the symlink's target
1217 * path because we don't want to resolve symlinks recursively.
1218 * Instead we make the path absolute and then canonicalize it.
1219 * Relative symlink target lookup should begin at the directory
1220 * in which the blob object is being installed.
1222 if (!got_path_is_absolute(target_path)) {
1223 char *abspath, *parent;
1224 err = got_path_dirname(&parent, ondisk_path);
1227 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1229 return got_error_from_errno("asprintf");
1232 if (strlen(abspath) >= sizeof(canonpath)) {
1233 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1237 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1242 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1247 /* Only allow symlinks pointing at paths within the work tree. */
1248 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1249 *is_bad_symlink = 1;
1253 /* Do not allow symlinks pointing into the .got directory. */
1254 if (asprintf(&path_got, "%s/%s", wtroot_path,
1255 GOT_WORKTREE_GOT_DIR) == -1)
1256 return got_error_from_errno("asprintf");
1257 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1258 *is_bad_symlink = 1;
1264 static const struct got_error *
1265 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1266 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1267 int restoring_missing_file, int reverting_versioned_file,
1268 int path_is_unversioned, int allow_bad_symlinks,
1269 struct got_repository *repo,
1270 got_worktree_checkout_cb progress_cb, void *progress_arg)
1272 const struct got_error *err = NULL;
1273 char target_path[PATH_MAX];
1274 size_t len, target_len = 0;
1275 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1276 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1278 *is_bad_symlink = 0;
1281 * Blob object content specifies the target path of the link.
1282 * If a symbolic link cannot be installed we instead create
1283 * a regular file which contains the link target path stored
1284 * in the blob object.
1287 err = got_object_blob_read_block(&len, blob);
1291 if (len + target_len >= sizeof(target_path)) {
1292 /* Path too long; install as a regular file. */
1293 *is_bad_symlink = 1;
1294 got_object_blob_rewind(blob);
1295 return install_blob(worktree, ondisk_path, path,
1296 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1297 restoring_missing_file, reverting_versioned_file,
1298 1, path_is_unversioned, repo, progress_cb,
1302 /* Skip blob object header first time around. */
1303 memcpy(target_path + target_len, buf + hdrlen,
1305 target_len += len - hdrlen;
1309 target_path[target_len] = '\0';
1311 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1312 ondisk_path, worktree->root_path);
1316 if (*is_bad_symlink && !allow_bad_symlinks) {
1317 /* install as a regular file */
1318 got_object_blob_rewind(blob);
1319 err = install_blob(worktree, ondisk_path, path,
1320 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1321 restoring_missing_file, reverting_versioned_file, 1,
1322 path_is_unversioned, repo, progress_cb, progress_arg);
1326 if (symlink(target_path, ondisk_path) == -1) {
1327 if (errno == EEXIST) {
1328 int symlink_replaced;
1329 if (path_is_unversioned) {
1330 err = (*progress_cb)(progress_arg,
1331 GOT_STATUS_UNVERSIONED, path);
1334 err = replace_existing_symlink(&symlink_replaced,
1335 ondisk_path, target_path, target_len);
1339 if (symlink_replaced) {
1340 err = (*progress_cb)(progress_arg,
1341 reverting_versioned_file ?
1343 GOT_STATUS_UPDATE, path);
1345 err = (*progress_cb)(progress_arg,
1346 GOT_STATUS_EXISTS, path);
1349 return err; /* Nothing else to do. */
1352 if (errno == ENOENT) {
1354 err = got_path_dirname(&parent, ondisk_path);
1357 err = add_dir_on_disk(worktree, parent);
1362 * Retry, and fall through to error handling
1363 * below if this second attempt fails.
1365 if (symlink(target_path, ondisk_path) != -1) {
1366 err = NULL; /* success */
1371 /* Handle errors from first or second creation attempt. */
1372 if (errno == ENAMETOOLONG) {
1373 /* bad target path; install as a regular file */
1374 *is_bad_symlink = 1;
1375 got_object_blob_rewind(blob);
1376 err = install_blob(worktree, ondisk_path, path,
1377 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1378 restoring_missing_file, reverting_versioned_file, 1,
1379 path_is_unversioned, repo,
1380 progress_cb, progress_arg);
1381 } else if (errno == ENOTDIR) {
1382 err = got_error_path(ondisk_path,
1383 GOT_ERR_FILE_OBSTRUCTED);
1385 err = got_error_from_errno3("symlink",
1386 target_path, ondisk_path);
1388 } else if (progress_cb)
1389 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1390 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1394 static const struct got_error *
1395 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1396 const char *path, mode_t te_mode, mode_t st_mode,
1397 struct got_blob_object *blob, int restoring_missing_file,
1398 int reverting_versioned_file, int installing_bad_symlink,
1399 int path_is_unversioned, struct got_repository *repo,
1400 got_worktree_checkout_cb progress_cb, void *progress_arg)
1402 const struct got_error *err = NULL;
1406 char *tmppath = NULL;
1409 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1410 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1413 if (errno == ENOENT) {
1415 err = got_path_dirname(&parent, path);
1418 err = add_dir_on_disk(worktree, parent);
1422 fd = open(ondisk_path,
1423 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1426 return got_error_from_errno2("open",
1428 } else if (errno == EEXIST) {
1429 if (path_is_unversioned) {
1430 err = (*progress_cb)(progress_arg,
1431 GOT_STATUS_UNVERSIONED, path);
1434 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1435 !S_ISREG(st_mode) && !installing_bad_symlink) {
1436 /* TODO file is obstructed; do something */
1437 err = got_error_path(ondisk_path,
1438 GOT_ERR_FILE_OBSTRUCTED);
1441 err = got_opentemp_named_fd(&tmppath, &fd,
1447 if (fchmod(fd, apply_umask(mode)) == -1) {
1448 err = got_error_from_errno2("fchmod",
1454 return got_error_from_errno2("open", ondisk_path);
1458 if (restoring_missing_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1461 else if (reverting_versioned_file)
1462 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1465 err = (*progress_cb)(progress_arg,
1466 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1471 hdrlen = got_object_blob_get_hdrlen(blob);
1473 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1474 err = got_object_blob_read_block(&len, blob);
1478 /* Skip blob object header first time around. */
1479 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1481 err = got_error_from_errno("write");
1483 } else if (outlen != len - hdrlen) {
1484 err = got_error(GOT_ERR_IO);
1491 if (fsync(fd) != 0) {
1492 err = got_error_from_errno("fsync");
1497 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1498 err = got_error_from_errno2("unlink", ondisk_path);
1501 if (rename(tmppath, ondisk_path) != 0) {
1502 err = got_error_from_errno3("rename", tmppath,
1511 if (fd != -1 && close(fd) == -1 && err == NULL)
1512 err = got_error_from_errno("close");
1513 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1514 err = got_error_from_errno2("unlink", tmppath);
1519 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1520 static const struct got_error *
1521 get_modified_file_content_status(unsigned char *status, FILE *f)
1523 const struct got_error *err = NULL;
1524 const char *markers[3] = {
1525 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 GOT_DIFF_CONFLICT_MARKER_SEP,
1527 GOT_DIFF_CONFLICT_MARKER_END
1531 size_t linesize = 0;
1534 while (*status == GOT_STATUS_MODIFY) {
1535 linelen = getline(&line, &linesize, f);
1536 if (linelen == -1) {
1539 err = got_ferror(f, GOT_ERR_IO);
1543 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1544 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1546 *status = GOT_STATUS_CONFLICT;
1557 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1559 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1560 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1564 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1566 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1567 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1568 ie->mtime_sec == sb->st_mtim.tv_sec &&
1569 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1570 ie->size == (sb->st_size & 0xffffffff) &&
1571 !xbit_differs(ie, sb->st_mode));
1574 static unsigned char
1575 get_staged_status(struct got_fileindex_entry *ie)
1577 switch (got_fileindex_entry_stage_get(ie)) {
1578 case GOT_FILEIDX_STAGE_ADD:
1579 return GOT_STATUS_ADD;
1580 case GOT_FILEIDX_STAGE_DELETE:
1581 return GOT_STATUS_DELETE;
1582 case GOT_FILEIDX_STAGE_MODIFY:
1583 return GOT_STATUS_MODIFY;
1585 return GOT_STATUS_NO_CHANGE;
1589 static const struct got_error *
1590 get_symlink_modification_status(unsigned char *status,
1591 struct got_fileindex_entry *ie, const char *abspath,
1592 int dirfd, const char *de_name, struct got_blob_object *blob)
1594 const struct got_error *err = NULL;
1595 char target_path[PATH_MAX];
1596 char etarget[PATH_MAX];
1598 size_t len, target_len = 0;
1599 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1600 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1602 *status = GOT_STATUS_NO_CHANGE;
1604 /* Blob object content specifies the target path of the link. */
1606 err = got_object_blob_read_block(&len, blob);
1609 if (len + target_len >= sizeof(target_path)) {
1611 * Should not happen. The blob contents were OK
1612 * when this symlink was installed.
1614 return got_error(GOT_ERR_NO_SPACE);
1617 /* Skip blob object header first time around. */
1618 memcpy(target_path + target_len, buf + hdrlen,
1620 target_len += len - hdrlen;
1624 target_path[target_len] = '\0';
1627 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1629 return got_error_from_errno2("readlinkat", abspath);
1631 elen = readlink(abspath, etarget, sizeof(etarget));
1633 return got_error_from_errno2("readlink", abspath);
1636 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1637 *status = GOT_STATUS_MODIFY;
1642 static const struct got_error *
1643 get_file_status(unsigned char *status, struct stat *sb,
1644 struct got_fileindex_entry *ie, const char *abspath,
1645 int dirfd, const char *de_name, struct got_repository *repo)
1647 const struct got_error *err = NULL;
1648 struct got_object_id id;
1650 int fd = -1, fd1 = -1;
1653 struct got_blob_object *blob = NULL;
1655 unsigned char staged_status = get_staged_status(ie);
1657 *status = GOT_STATUS_NO_CHANGE;
1658 memset(sb, 0, sizeof(*sb));
1661 * Whenever the caller provides a directory descriptor and a
1662 * directory entry name for the file, use them! This prevents
1663 * race conditions if filesystem paths change beneath our feet.
1666 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1667 if (errno == ENOENT) {
1668 if (got_fileindex_entry_has_file_on_disk(ie))
1669 *status = GOT_STATUS_MISSING;
1671 *status = GOT_STATUS_DELETE;
1674 err = got_error_from_errno2("fstatat", abspath);
1678 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1679 if (fd == -1 && errno != ENOENT &&
1680 !got_err_open_nofollow_on_symlink())
1681 return got_error_from_errno2("open", abspath);
1682 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1683 if (lstat(abspath, sb) == -1)
1684 return got_error_from_errno2("lstat", abspath);
1685 } else if (fd == -1 || fstat(fd, sb) == -1) {
1686 if (errno == ENOENT) {
1687 if (got_fileindex_entry_has_file_on_disk(ie))
1688 *status = GOT_STATUS_MISSING;
1690 *status = GOT_STATUS_DELETE;
1693 err = got_error_from_errno2("fstat", abspath);
1698 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1699 *status = GOT_STATUS_OBSTRUCTED;
1703 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1704 *status = GOT_STATUS_DELETE;
1706 } else if (!got_fileindex_entry_has_blob(ie) &&
1707 staged_status != GOT_STATUS_ADD) {
1708 *status = GOT_STATUS_ADD;
1712 if (!stat_info_differs(ie, sb))
1715 if (S_ISLNK(sb->st_mode) &&
1716 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1717 *status = GOT_STATUS_MODIFY;
1721 if (staged_status == GOT_STATUS_MODIFY ||
1722 staged_status == GOT_STATUS_ADD)
1723 memcpy(id.hash, ie->staged_blob_hash, sizeof(id.hash));
1725 memcpy(id.hash, ie->blob_hash, sizeof(id.hash));
1726 id.algo = got_repo_get_hash_algorithm(repo);
1728 fd1 = got_opentempfd();
1730 err = got_error_from_errno("got_opentempfd");
1733 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1737 if (S_ISLNK(sb->st_mode)) {
1738 err = get_symlink_modification_status(status, ie,
1739 abspath, dirfd, de_name, blob);
1744 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1746 err = got_error_from_errno2("openat", abspath);
1751 f = fdopen(fd, "r");
1753 err = got_error_from_errno2("fdopen", abspath);
1757 hdrlen = got_object_blob_get_hdrlen(blob);
1759 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1760 err = got_object_blob_read_block(&blen, blob);
1763 /* Skip length of blob object header first time around. */
1764 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1765 if (flen == 0 && ferror(f)) {
1766 err = got_error_from_errno("fread");
1769 if (blen - hdrlen == 0) {
1771 *status = GOT_STATUS_MODIFY;
1773 } else if (flen == 0) {
1774 if (blen - hdrlen != 0)
1775 *status = GOT_STATUS_MODIFY;
1777 } else if (blen - hdrlen == flen) {
1778 /* Skip blob object header first time around. */
1779 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1780 *status = GOT_STATUS_MODIFY;
1784 *status = GOT_STATUS_MODIFY;
1790 if (*status == GOT_STATUS_MODIFY) {
1792 err = get_modified_file_content_status(status, f);
1793 } else if (xbit_differs(ie, sb->st_mode))
1794 *status = GOT_STATUS_MODE_CHANGE;
1796 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1797 err = got_error_from_errno("close");
1799 got_object_blob_close(blob);
1800 if (f != NULL && fclose(f) == EOF && err == NULL)
1801 err = got_error_from_errno2("fclose", abspath);
1802 if (fd != -1 && close(fd) == -1 && err == NULL)
1803 err = got_error_from_errno2("close", abspath);
1808 * Update timestamps in the file index if a file is unmodified and
1809 * we had to run a full content comparison to find out.
1811 static const struct got_error *
1812 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1813 struct got_fileindex_entry *ie, struct stat *sb)
1815 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1816 return got_fileindex_entry_update(ie, wt_fd, path,
1817 ie->blob_hash, ie->commit_hash, 1);
1822 static const struct got_error *
1823 update_blob(struct got_worktree *worktree,
1824 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1825 struct got_tree_entry *te, const char *path,
1826 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1829 const struct got_error *err = NULL;
1830 struct got_blob_object *blob = NULL;
1831 char *ondisk_path = NULL;
1832 unsigned char status = GOT_STATUS_NO_CHANGE;
1834 int fd1 = -1, fd2 = -1;
1836 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1837 return got_error_from_errno("asprintf");
1840 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1841 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1844 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1848 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1849 sb.st_mode = got_fileindex_perms_to_st(ie);
1851 if (stat(ondisk_path, &sb) == -1) {
1852 if (errno != ENOENT) {
1853 err = got_error_from_errno2("stat",
1857 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1858 status = GOT_STATUS_UNVERSIONED;
1860 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1861 status = GOT_STATUS_UNVERSIONED;
1863 status = GOT_STATUS_OBSTRUCTED;
1867 if (status == GOT_STATUS_OBSTRUCTED) {
1869 got_fileindex_entry_mark_skipped(ie);
1870 err = (*progress_cb)(progress_arg, status, path);
1873 if (status == GOT_STATUS_CONFLICT) {
1875 got_fileindex_entry_mark_skipped(ie);
1876 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1881 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1882 (S_ISLNK(te->mode) ||
1883 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1885 * This is a regular file or an installed bad symlink.
1886 * If the file index indicates that this file is already
1887 * up-to-date with respect to the repository we can skip
1888 * updating contents of this file.
1890 if (got_fileindex_entry_has_commit(ie) &&
1891 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
1892 SHA1_DIGEST_LENGTH) == 0) {
1894 err = sync_timestamps(worktree->root_fd,
1895 path, status, ie, &sb);
1898 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1902 if (got_fileindex_entry_has_blob(ie) &&
1903 memcmp(ie->blob_hash, te->id.hash,
1904 SHA1_DIGEST_LENGTH) == 0) {
1905 /* Different commit but the same blob. */
1906 err = sync_timestamps(worktree->root_fd,
1907 path, status, ie, &sb);
1910 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1916 fd1 = got_opentempfd();
1918 err = got_error_from_errno("got_opentempfd");
1921 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1925 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1926 int update_timestamps;
1927 struct got_blob_object *blob2 = NULL;
1928 char *label_orig = NULL;
1929 if (got_fileindex_entry_has_blob(ie)) {
1930 fd2 = got_opentempfd();
1932 err = got_error_from_errno("got_opentempfd");
1935 struct got_object_id id2;
1936 memcpy(id2.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
1937 id2.algo = got_repo_get_hash_algorithm(repo);
1938 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1943 if (got_fileindex_entry_has_commit(ie)) {
1944 char id_str[SHA1_DIGEST_STRING_LENGTH];
1945 if (got_sha1_digest_to_str(ie->commit_hash, id_str,
1946 sizeof(id_str)) == NULL) {
1947 err = got_error_path(id_str,
1948 GOT_ERR_BAD_OBJ_ID_STR);
1951 if (asprintf(&label_orig, "%s: commit %s",
1952 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1953 err = got_error_from_errno("asprintf");
1957 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1959 err = got_object_blob_read_to_str(&link_target, blob);
1962 err = merge_symlink(worktree, blob2, ondisk_path, path,
1963 label_orig, link_target, worktree->base_commit_id,
1964 repo, progress_cb, progress_arg);
1967 err = merge_blob(&update_timestamps, worktree, blob2,
1968 ondisk_path, path, sb.st_mode, label_orig, blob,
1969 worktree->base_commit_id, repo,
1970 progress_cb, progress_arg);
1973 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1974 err = got_error_from_errno("close");
1978 got_object_blob_close(blob2);
1982 * Do not update timestamps of files with local changes.
1983 * Otherwise, a future status walk would treat them as
1984 * unmodified files again.
1986 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1987 blob->id.hash, worktree->base_commit_id->hash,
1989 } else if (status == GOT_STATUS_MODE_CHANGE) {
1990 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1991 blob->id.hash, worktree->base_commit_id->hash, 0);
1992 } else if (status == GOT_STATUS_DELETE) {
1993 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1996 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1997 blob->id.hash, worktree->base_commit_id->hash, 0);
2001 int is_bad_symlink = 0;
2002 if (S_ISLNK(te->mode)) {
2003 err = install_symlink(&is_bad_symlink, worktree,
2004 ondisk_path, path, blob,
2005 status == GOT_STATUS_MISSING, 0,
2006 status == GOT_STATUS_UNVERSIONED, 0,
2007 repo, progress_cb, progress_arg);
2009 err = install_blob(worktree, ondisk_path, path,
2010 te->mode, sb.st_mode, blob,
2011 status == GOT_STATUS_MISSING, 0, 0,
2012 status == GOT_STATUS_UNVERSIONED, repo,
2013 progress_cb, progress_arg);
2019 err = got_fileindex_entry_update(ie,
2020 worktree->root_fd, path, blob->id.hash,
2021 worktree->base_commit_id->hash, 1);
2023 err = create_fileindex_entry(&ie, fileindex,
2024 worktree->base_commit_id, worktree->root_fd, path,
2030 if (is_bad_symlink) {
2031 got_fileindex_entry_filetype_set(ie,
2032 GOT_FILEIDX_MODE_BAD_SYMLINK);
2036 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2037 err = got_error_from_errno("close");
2040 got_object_blob_close(blob);
2046 static const struct got_error *
2047 remove_ondisk_file(const char *root_path, const char *path)
2049 const struct got_error *err = NULL;
2050 char *ondisk_path = NULL, *parent = NULL;
2052 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2053 return got_error_from_errno("asprintf");
2055 if (unlink(ondisk_path) == -1) {
2056 if (errno != ENOENT)
2057 err = got_error_from_errno2("unlink", ondisk_path);
2059 size_t root_len = strlen(root_path);
2060 err = got_path_dirname(&parent, ondisk_path);
2063 while (got_path_cmp(parent, root_path,
2064 strlen(parent), root_len) != 0) {
2066 ondisk_path = parent;
2068 if (rmdir(ondisk_path) == -1) {
2069 if (errno != ENOTEMPTY)
2070 err = got_error_from_errno2("rmdir",
2074 err = got_path_dirname(&parent, ondisk_path);
2085 static const struct got_error *
2086 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2087 struct got_fileindex_entry *ie, struct got_repository *repo,
2088 got_worktree_checkout_cb progress_cb, void *progress_arg)
2090 const struct got_error *err = NULL;
2091 unsigned char status;
2095 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2096 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2098 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2100 return got_error_from_errno("asprintf");
2102 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2106 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2107 char ondisk_target[PATH_MAX];
2108 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2109 sizeof(ondisk_target));
2110 if (ondisk_len == -1) {
2111 err = got_error_from_errno2("readlink", ondisk_path);
2114 ondisk_target[ondisk_len] = '\0';
2115 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2116 NULL, NULL, /* XXX pass common ancestor info? */
2117 ondisk_target, ondisk_path);
2120 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2125 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2126 status == GOT_STATUS_ADD) {
2127 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2131 * Preserve the working file and change the deleted blob's
2132 * entry into a schedule-add entry.
2134 err = got_fileindex_entry_update(ie, worktree->root_fd,
2135 ie->path, NULL, NULL, 0);
2137 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2140 if (status == GOT_STATUS_NO_CHANGE) {
2141 err = remove_ondisk_file(worktree->root_path, ie->path);
2145 got_fileindex_entry_remove(fileindex, ie);
2152 struct diff_cb_arg {
2153 struct got_fileindex *fileindex;
2154 struct got_worktree *worktree;
2155 struct got_repository *repo;
2156 got_worktree_checkout_cb progress_cb;
2158 got_cancel_cb cancel_cb;
2162 static const struct got_error *
2163 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2164 struct got_tree_entry *te, const char *parent_path)
2166 struct diff_cb_arg *a = arg;
2168 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2169 return got_error(GOT_ERR_CANCELLED);
2171 return update_blob(a->worktree, a->fileindex, ie, te,
2172 ie->path, a->repo, a->progress_cb, a->progress_arg);
2175 static const struct got_error *
2176 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2178 struct diff_cb_arg *a = arg;
2180 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2181 return got_error(GOT_ERR_CANCELLED);
2183 return delete_blob(a->worktree, a->fileindex, ie,
2184 a->repo, a->progress_cb, a->progress_arg);
2187 static const struct got_error *
2188 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2190 struct diff_cb_arg *a = arg;
2191 const struct got_error *err;
2194 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2195 return got_error(GOT_ERR_CANCELLED);
2197 if (got_object_tree_entry_is_submodule(te))
2200 if (asprintf(&path, "%s%s%s", parent_path,
2201 parent_path[0] ? "/" : "", te->name)
2203 return got_error_from_errno("asprintf");
2205 if (S_ISDIR(te->mode))
2206 err = add_dir_on_disk(a->worktree, path);
2208 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2209 a->repo, a->progress_cb, a->progress_arg);
2215 const struct got_error *
2216 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2218 uint32_t uuid_status;
2220 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2221 if (uuid_status != uuid_s_ok) {
2223 return got_error_uuid(uuid_status, "uuid_to_string");
2229 static const struct got_error *
2230 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2232 const struct got_error *err = NULL;
2233 char *uuidstr = NULL;
2237 err = got_worktree_get_uuid(&uuidstr, worktree);
2241 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2242 err = got_error_from_errno("asprintf");
2249 const struct got_error *
2250 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2253 return get_ref_name(refname, worktree, prefix);
2256 const struct got_error *
2257 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2259 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2262 static const struct got_error *
2263 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2269 static const struct got_error *
2270 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2275 static const struct got_error *
2276 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2278 return get_ref_name(refname, worktree,
2279 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2282 static const struct got_error *
2283 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2285 return get_ref_name(refname, worktree,
2286 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2289 static const struct got_error *
2290 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2292 return get_ref_name(refname, worktree,
2293 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2296 static const struct got_error *
2297 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2299 return get_ref_name(refname, worktree,
2300 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2303 static const struct got_error *
2304 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2306 return get_ref_name(refname, worktree,
2307 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2310 static const struct got_error *
2311 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2313 return get_ref_name(refname, worktree,
2314 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2317 const struct got_error *
2318 got_worktree_get_histedit_script_path(char **path,
2319 struct got_worktree *worktree)
2321 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2322 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2324 return got_error_from_errno("asprintf");
2329 static const struct got_error *
2330 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree,
2333 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2336 static const struct got_error *
2337 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2339 return get_ref_name(refname, worktree,
2340 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2344 * Prevent Git's garbage collector from deleting our base commit by
2345 * setting a reference to our base commit's ID.
2347 static const struct got_error *
2348 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2350 const struct got_error *err = NULL;
2351 struct got_reference *ref = NULL;
2354 err = got_worktree_get_base_ref_name(&refname, worktree);
2358 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2362 err = got_ref_write(ref, repo);
2370 static const struct got_error *
2371 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2373 const struct got_error *err = NULL;
2375 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2376 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2377 err = got_error_from_errno("asprintf");
2378 *fileindex_path = NULL;
2384 static const struct got_error *
2385 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2386 struct got_worktree *worktree)
2388 const struct got_error *err = NULL;
2391 *fileindex_path = NULL;
2392 *fileindex = got_fileindex_alloc();
2393 if (*fileindex == NULL)
2394 return got_error_from_errno("got_fileindex_alloc");
2396 err = get_fileindex_path(fileindex_path, worktree);
2400 index = fopen(*fileindex_path, "rbe");
2401 if (index == NULL) {
2402 if (errno != ENOENT)
2403 err = got_error_from_errno2("fopen", *fileindex_path);
2405 err = got_fileindex_read(*fileindex, index);
2406 if (fclose(index) == EOF && err == NULL)
2407 err = got_error_from_errno("fclose");
2411 free(*fileindex_path);
2412 *fileindex_path = NULL;
2413 got_fileindex_free(*fileindex);
2419 struct bump_base_commit_id_arg {
2420 struct got_object_id *base_commit_id;
2423 const char *entry_name;
2424 got_worktree_checkout_cb progress_cb;
2428 /* Bump base commit ID of all files within an updated part of the work tree. */
2429 static const struct got_error *
2430 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2432 const struct got_error *err;
2433 struct bump_base_commit_id_arg *a = arg;
2435 if (a->entry_name) {
2436 if (strcmp(ie->path, a->path) != 0)
2438 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2441 if (got_fileindex_entry_was_skipped(ie))
2444 if (memcmp(ie->commit_hash, a->base_commit_id->hash,
2445 SHA1_DIGEST_LENGTH) == 0)
2448 if (a->progress_cb) {
2449 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2454 memcpy(ie->commit_hash, a->base_commit_id->hash, SHA1_DIGEST_LENGTH);
2458 static const struct got_error *
2459 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2460 struct got_fileindex *fileindex,
2461 got_worktree_checkout_cb progress_cb, void *progress_arg)
2463 struct bump_base_commit_id_arg bbc_arg;
2465 bbc_arg.base_commit_id = worktree->base_commit_id;
2466 bbc_arg.entry_name = NULL;
2468 bbc_arg.path_len = 0;
2469 bbc_arg.progress_cb = progress_cb;
2470 bbc_arg.progress_arg = progress_arg;
2472 return got_fileindex_for_each_entry_safe(fileindex,
2473 bump_base_commit_id, &bbc_arg);
2476 static const struct got_error *
2477 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2479 const struct got_error *err = NULL;
2480 char *new_fileindex_path = NULL;
2481 FILE *new_index = NULL;
2482 struct timespec timeout;
2484 err = got_opentemp_named(&new_fileindex_path, &new_index,
2485 fileindex_path, "");
2489 err = got_fileindex_write(fileindex, new_index);
2493 if (rename(new_fileindex_path, fileindex_path) != 0) {
2494 err = got_error_from_errno3("rename", new_fileindex_path,
2496 unlink(new_fileindex_path);
2500 * Sleep for a short amount of time to ensure that files modified after
2501 * this program exits have a different time stamp from the one which
2502 * was recorded in the file index.
2505 timeout.tv_nsec = 1;
2506 nanosleep(&timeout, NULL);
2510 free(new_fileindex_path);
2514 static const struct got_error *
2515 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2516 struct got_object_id **tree_id, const char *wt_relpath,
2517 struct got_commit_object *base_commit, struct got_worktree *worktree,
2518 struct got_repository *repo)
2520 const struct got_error *err = NULL;
2521 struct got_object_id *id = NULL;
2522 char *in_repo_path = NULL;
2523 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2525 *entry_type = GOT_OBJ_TYPE_ANY;
2526 *tree_relpath = NULL;
2529 if (wt_relpath[0] == '\0') {
2530 /* Check out all files within the work tree. */
2531 *entry_type = GOT_OBJ_TYPE_TREE;
2532 *tree_relpath = strdup("");
2533 if (*tree_relpath == NULL) {
2534 err = got_error_from_errno("strdup");
2537 err = got_object_id_by_path(tree_id, repo, base_commit,
2538 worktree->path_prefix);
2544 /* Check out a subset of files in the work tree. */
2546 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2547 is_root_wt ? "" : "/", wt_relpath) == -1) {
2548 err = got_error_from_errno("asprintf");
2552 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2557 in_repo_path = NULL;
2559 err = got_object_get_type(entry_type, repo, id);
2563 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2564 /* Check out a single file. */
2565 if (strchr(wt_relpath, '/') == NULL) {
2566 /* Check out a single file in work tree's root dir. */
2567 in_repo_path = strdup(worktree->path_prefix);
2568 if (in_repo_path == NULL) {
2569 err = got_error_from_errno("strdup");
2572 *tree_relpath = strdup("");
2573 if (*tree_relpath == NULL) {
2574 err = got_error_from_errno("strdup");
2578 /* Check out a single file in a subdirectory. */
2579 err = got_path_dirname(tree_relpath, wt_relpath);
2582 if (asprintf(&in_repo_path, "%s%s%s",
2583 worktree->path_prefix, is_root_wt ? "" : "/",
2584 *tree_relpath) == -1) {
2585 err = got_error_from_errno("asprintf");
2589 err = got_object_id_by_path(tree_id, repo,
2590 base_commit, in_repo_path);
2592 /* Check out all files within a subdirectory. */
2593 *tree_id = got_object_id_dup(id);
2594 if (*tree_id == NULL) {
2595 err = got_error_from_errno("got_object_id_dup");
2598 *tree_relpath = strdup(wt_relpath);
2599 if (*tree_relpath == NULL) {
2600 err = got_error_from_errno("strdup");
2608 *entry_type = GOT_OBJ_TYPE_ANY;
2609 free(*tree_relpath);
2610 *tree_relpath = NULL;
2617 static const struct got_error *
2618 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2619 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2620 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2621 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2623 const struct got_error *err = NULL;
2624 struct got_commit_object *commit = NULL;
2625 struct got_tree_object *tree = NULL;
2626 struct got_fileindex_diff_tree_cb diff_cb;
2627 struct diff_cb_arg arg;
2629 err = ref_base_commit(worktree, repo);
2631 if (!(err->code == GOT_ERR_ERRNO &&
2632 (errno == EACCES || errno == EROFS)))
2634 err = (*progress_cb)(progress_arg,
2635 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2640 err = got_object_open_as_commit(&commit, repo,
2641 worktree->base_commit_id);
2645 err = got_object_open_as_tree(&tree, repo, tree_id);
2650 got_object_tree_find_entry(tree, entry_name) == NULL) {
2651 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2655 diff_cb.diff_old_new = diff_old_new;
2656 diff_cb.diff_old = diff_old;
2657 diff_cb.diff_new = diff_new;
2658 arg.fileindex = fileindex;
2659 arg.worktree = worktree;
2661 arg.progress_cb = progress_cb;
2662 arg.progress_arg = progress_arg;
2663 arg.cancel_cb = cancel_cb;
2664 arg.cancel_arg = cancel_arg;
2665 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2666 entry_name, repo, &diff_cb, &arg);
2669 got_object_tree_close(tree);
2671 got_object_commit_close(commit);
2675 const struct got_error *
2676 got_worktree_checkout_files(struct got_worktree *worktree,
2677 struct got_pathlist_head *paths, struct got_repository *repo,
2678 got_worktree_checkout_cb progress_cb, void *progress_arg,
2679 got_cancel_cb cancel_cb, void *cancel_arg)
2681 const struct got_error *err = NULL, *sync_err, *unlockerr;
2682 struct got_commit_object *commit = NULL;
2683 struct got_tree_object *tree = NULL;
2684 struct got_fileindex *fileindex = NULL;
2685 char *fileindex_path = NULL;
2686 struct got_pathlist_entry *pe;
2687 struct tree_path_data {
2688 STAILQ_ENTRY(tree_path_data) entry;
2689 struct got_object_id *tree_id;
2694 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2696 STAILQ_INIT(&tree_paths);
2698 err = lock_worktree(worktree, LOCK_EX);
2702 err = got_object_open_as_commit(&commit, repo,
2703 worktree->base_commit_id);
2707 /* Map all specified paths to in-repository trees. */
2708 TAILQ_FOREACH(pe, paths, entry) {
2709 tpd = malloc(sizeof(*tpd));
2711 err = got_error_from_errno("malloc");
2715 err = find_tree_entry_for_checkout(&tpd->entry_type,
2716 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2723 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2724 err = got_path_basename(&tpd->entry_name, pe->path);
2732 tpd->entry_name = NULL;
2734 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2738 * Read the file index.
2739 * Checking out files is supposed to be an idempotent operation.
2740 * If the on-disk file index is incomplete we will try to complete it.
2742 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2746 tpd = STAILQ_FIRST(&tree_paths);
2747 TAILQ_FOREACH(pe, paths, entry) {
2748 struct bump_base_commit_id_arg bbc_arg;
2750 err = checkout_files(worktree, fileindex, tpd->relpath,
2751 tpd->tree_id, tpd->entry_name, repo,
2752 progress_cb, progress_arg, cancel_cb, cancel_arg);
2756 bbc_arg.base_commit_id = worktree->base_commit_id;
2757 bbc_arg.entry_name = tpd->entry_name;
2758 bbc_arg.path = pe->path;
2759 bbc_arg.path_len = pe->path_len;
2760 bbc_arg.progress_cb = progress_cb;
2761 bbc_arg.progress_arg = progress_arg;
2762 err = got_fileindex_for_each_entry_safe(fileindex,
2763 bump_base_commit_id, &bbc_arg);
2767 tpd = STAILQ_NEXT(tpd, entry);
2769 sync_err = sync_fileindex(fileindex, fileindex_path);
2770 if (sync_err && err == NULL)
2773 free(fileindex_path);
2775 got_object_tree_close(tree);
2777 got_object_commit_close(commit);
2779 got_fileindex_free(fileindex);
2780 while (!STAILQ_EMPTY(&tree_paths)) {
2781 tpd = STAILQ_FIRST(&tree_paths);
2782 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2787 unlockerr = lock_worktree(worktree, LOCK_SH);
2788 if (unlockerr && err == NULL)
2793 struct merge_file_cb_arg {
2794 struct got_worktree *worktree;
2795 struct got_fileindex *fileindex;
2796 got_worktree_checkout_cb progress_cb;
2798 got_cancel_cb cancel_cb;
2800 const char *label_orig;
2801 struct got_object_id *commit_id2;
2802 int allow_bad_symlinks;
2805 static const struct got_error *
2806 merge_file_cb(void *arg, struct got_blob_object *blob1,
2807 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2808 struct got_object_id *id1, struct got_object_id *id2,
2809 const char *path1, const char *path2,
2810 mode_t mode1, mode_t mode2, struct got_repository *repo)
2812 static const struct got_error *err = NULL;
2813 struct merge_file_cb_arg *a = arg;
2814 struct got_fileindex_entry *ie;
2815 char *ondisk_path = NULL;
2817 unsigned char status;
2818 int local_changes_subsumed;
2819 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2820 char *id_str = NULL, *label_deriv2 = NULL;
2822 if (blob1 && blob2) {
2823 ie = got_fileindex_entry_get(a->fileindex, path2,
2826 return (*a->progress_cb)(a->progress_arg,
2827 GOT_STATUS_MISSING, path2);
2829 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2831 return got_error_from_errno("asprintf");
2833 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2838 if (status == GOT_STATUS_DELETE) {
2839 err = (*a->progress_cb)(a->progress_arg,
2840 GOT_STATUS_MERGE, path2);
2843 if (status != GOT_STATUS_NO_CHANGE &&
2844 status != GOT_STATUS_MODIFY &&
2845 status != GOT_STATUS_CONFLICT &&
2846 status != GOT_STATUS_ADD) {
2847 err = (*a->progress_cb)(a->progress_arg, status, path2);
2851 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2853 err = got_object_blob_read_to_str(&link_target2, blob2);
2856 err = merge_symlink(a->worktree, blob1, ondisk_path,
2857 path2, a->label_orig, link_target2, a->commit_id2,
2858 repo, a->progress_cb, a->progress_arg);
2863 f_orig = got_opentemp();
2864 if (f_orig == NULL) {
2865 err = got_error_from_errno("got_opentemp");
2868 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2873 f_deriv2 = got_opentemp();
2874 if (f_deriv2 == NULL)
2876 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2881 fd = open(ondisk_path,
2882 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2884 err = got_error_from_errno2("open",
2888 f_deriv = fdopen(fd, "r");
2889 if (f_deriv == NULL) {
2890 err = got_error_from_errno2("fdopen",
2895 err = got_object_id_str(&id_str, a->commit_id2);
2898 if (asprintf(&label_deriv2, "%s: commit %s",
2899 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2900 err = got_error_from_errno("asprintf");
2903 err = merge_file(&local_changes_subsumed, a->worktree,
2904 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2905 mode2, a->label_orig, NULL, label_deriv2,
2906 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2907 a->progress_cb, a->progress_arg);
2910 ie = got_fileindex_entry_get(a->fileindex, path1,
2913 return (*a->progress_cb)(a->progress_arg,
2914 GOT_STATUS_MISSING, path1);
2916 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2918 return got_error_from_errno("asprintf");
2920 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2926 case GOT_STATUS_NO_CHANGE:
2927 err = (*a->progress_cb)(a->progress_arg,
2928 GOT_STATUS_DELETE, path1);
2931 err = remove_ondisk_file(a->worktree->root_path, path1);
2935 got_fileindex_entry_mark_deleted_from_disk(ie);
2937 case GOT_STATUS_DELETE:
2938 case GOT_STATUS_MISSING:
2939 err = (*a->progress_cb)(a->progress_arg,
2940 GOT_STATUS_DELETE, path1);
2944 got_fileindex_entry_mark_deleted_from_disk(ie);
2946 case GOT_STATUS_ADD: {
2947 struct got_object_id *id;
2951 * Delete the added file only if its content already
2952 * exists in the repository.
2954 err = got_object_blob_file_create(&id, &blob1_f,
2955 &blob1_size, path1);
2958 if (got_object_id_cmp(id, id1) == 0) {
2959 err = (*a->progress_cb)(a->progress_arg,
2960 GOT_STATUS_DELETE, path1);
2963 err = remove_ondisk_file(a->worktree->root_path,
2968 got_fileindex_entry_remove(a->fileindex,
2971 err = (*a->progress_cb)(a->progress_arg,
2972 GOT_STATUS_CANNOT_DELETE, path1);
2974 if (fclose(blob1_f) == EOF && err == NULL)
2975 err = got_error_from_errno("fclose");
2981 case GOT_STATUS_MODIFY:
2982 case GOT_STATUS_CONFLICT:
2983 err = (*a->progress_cb)(a->progress_arg,
2984 GOT_STATUS_CANNOT_DELETE, path1);
2988 case GOT_STATUS_OBSTRUCTED:
2989 err = (*a->progress_cb)(a->progress_arg, status, path1);
2997 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2999 return got_error_from_errno("asprintf");
3000 ie = got_fileindex_entry_get(a->fileindex, path2,
3003 err = get_file_status(&status, &sb, ie, ondisk_path,
3007 if (status != GOT_STATUS_NO_CHANGE &&
3008 status != GOT_STATUS_MODIFY &&
3009 status != GOT_STATUS_CONFLICT &&
3010 status != GOT_STATUS_ADD) {
3011 err = (*a->progress_cb)(a->progress_arg,
3015 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3017 err = got_object_blob_read_to_str(&link_target2,
3021 err = merge_symlink(a->worktree, NULL,
3022 ondisk_path, path2, a->label_orig,
3023 link_target2, a->commit_id2, repo,
3024 a->progress_cb, a->progress_arg);
3026 } else if (S_ISREG(sb.st_mode)) {
3027 err = merge_blob(&local_changes_subsumed,
3028 a->worktree, NULL, ondisk_path, path2,
3029 sb.st_mode, a->label_orig, blob2,
3030 a->commit_id2, repo, a->progress_cb,
3033 err = got_error_path(ondisk_path,
3034 GOT_ERR_FILE_OBSTRUCTED);
3038 if (status == GOT_STATUS_DELETE) {
3039 err = got_fileindex_entry_update(ie,
3040 a->worktree->root_fd, path2, blob2->id.hash,
3041 a->worktree->base_commit_id->hash, 0);
3046 int is_bad_symlink = 0;
3047 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3048 if (S_ISLNK(mode2)) {
3049 err = install_symlink(&is_bad_symlink,
3050 a->worktree, ondisk_path, path2, blob2, 0,
3051 0, 1, a->allow_bad_symlinks, repo,
3052 a->progress_cb, a->progress_arg);
3054 err = install_blob(a->worktree, ondisk_path, path2,
3055 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3056 a->progress_cb, a->progress_arg);
3060 err = got_fileindex_entry_alloc(&ie, path2);
3063 err = got_fileindex_entry_update(ie,
3064 a->worktree->root_fd, path2, NULL, NULL, 1);
3066 got_fileindex_entry_free(ie);
3069 err = got_fileindex_entry_add(a->fileindex, ie);
3071 got_fileindex_entry_free(ie);
3074 if (is_bad_symlink) {
3075 got_fileindex_entry_filetype_set(ie,
3076 GOT_FILEIDX_MODE_BAD_SYMLINK);
3081 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3082 err = got_error_from_errno("fclose");
3083 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3084 err = got_error_from_errno("fclose");
3085 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3086 err = got_error_from_errno("fclose");
3093 static const struct got_error *
3094 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3096 struct got_worktree *worktree = arg;
3098 /* Reject merges into a work tree with mixed base commits. */
3099 if (got_fileindex_entry_has_commit(ie) &&
3100 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
3101 SHA1_DIGEST_LENGTH) != 0)
3102 return got_error(GOT_ERR_MIXED_COMMITS);
3107 struct check_merge_conflicts_arg {
3108 struct got_worktree *worktree;
3109 struct got_fileindex *fileindex;
3110 struct got_repository *repo;
3113 static const struct got_error *
3114 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3115 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3116 struct got_object_id *id1, struct got_object_id *id2,
3117 const char *path1, const char *path2,
3118 mode_t mode1, mode_t mode2, struct got_repository *repo)
3120 const struct got_error *err = NULL;
3121 struct check_merge_conflicts_arg *a = arg;
3122 unsigned char status;
3124 struct got_fileindex_entry *ie;
3125 const char *path = path2 ? path2 : path1;
3126 struct got_object_id *id = id2 ? id2 : id1;
3132 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3136 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3138 return got_error_from_errno("asprintf");
3140 /* Reject merges into a work tree with conflicted files. */
3141 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3145 if (status == GOT_STATUS_CONFLICT)
3146 return got_error(GOT_ERR_CONFLICTS);
3151 static const struct got_error *
3152 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3153 const char *fileindex_path, struct got_object_id *commit_id1,
3154 struct got_object_id *commit_id2, struct got_repository *repo,
3155 got_worktree_checkout_cb progress_cb, void *progress_arg,
3156 got_cancel_cb cancel_cb, void *cancel_arg)
3158 const struct got_error *err = NULL, *sync_err;
3159 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3160 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3161 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3162 struct check_merge_conflicts_arg cmc_arg;
3163 struct merge_file_cb_arg arg;
3164 char *label_orig = NULL;
3165 FILE *f1 = NULL, *f2 = NULL;
3166 int fd1 = -1, fd2 = -1;
3169 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3172 err = got_object_id_by_path(&tree_id1, repo, commit1,
3173 worktree->path_prefix);
3174 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3180 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3184 err = got_object_id_str(&id_str, commit_id1);
3188 if (asprintf(&label_orig, "%s: commit %s",
3189 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3190 err = got_error_from_errno("asprintf");
3196 f1 = got_opentemp();
3198 err = got_error_from_errno("got_opentemp");
3203 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3207 err = got_object_id_by_path(&tree_id2, repo, commit2,
3208 worktree->path_prefix);
3212 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3216 f2 = got_opentemp();
3218 err = got_error_from_errno("got_opentemp");
3222 fd1 = got_opentempfd();
3224 err = got_error_from_errno("got_opentempfd");
3228 fd2 = got_opentempfd();
3230 err = got_error_from_errno("got_opentempfd");
3234 cmc_arg.worktree = worktree;
3235 cmc_arg.fileindex = fileindex;
3236 cmc_arg.repo = repo;
3237 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3238 check_merge_conflicts, &cmc_arg, 0);
3242 arg.worktree = worktree;
3243 arg.fileindex = fileindex;
3244 arg.progress_cb = progress_cb;
3245 arg.progress_arg = progress_arg;
3246 arg.cancel_cb = cancel_cb;
3247 arg.cancel_arg = cancel_arg;
3248 arg.label_orig = label_orig;
3249 arg.commit_id2 = commit_id2;
3250 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3251 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3252 merge_file_cb, &arg, 1);
3253 sync_err = sync_fileindex(fileindex, fileindex_path);
3254 if (sync_err && err == NULL)
3258 got_object_commit_close(commit1);
3260 got_object_commit_close(commit2);
3262 got_object_tree_close(tree1);
3264 got_object_tree_close(tree2);
3265 if (f1 && fclose(f1) == EOF && err == NULL)
3266 err = got_error_from_errno("fclose");
3267 if (f2 && fclose(f2) == EOF && err == NULL)
3268 err = got_error_from_errno("fclose");
3269 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3270 err = got_error_from_errno("close");
3271 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3272 err = got_error_from_errno("close");
3277 const struct got_error *
3278 got_worktree_merge_files(struct got_worktree *worktree,
3279 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3280 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3281 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3283 const struct got_error *err, *unlockerr;
3284 char *fileindex_path = NULL;
3285 struct got_fileindex *fileindex = NULL;
3287 err = lock_worktree(worktree, LOCK_EX);
3291 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3295 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3300 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3301 commit_id2, repo, progress_cb, progress_arg,
3302 cancel_cb, cancel_arg);
3305 got_fileindex_free(fileindex);
3306 free(fileindex_path);
3307 unlockerr = lock_worktree(worktree, LOCK_SH);
3308 if (unlockerr && err == NULL)
3313 struct diff_dir_cb_arg {
3314 struct got_fileindex *fileindex;
3315 struct got_worktree *worktree;
3316 const char *status_path;
3317 size_t status_path_len;
3318 struct got_repository *repo;
3319 got_worktree_status_cb status_cb;
3321 got_cancel_cb cancel_cb;
3323 /* A pathlist containing per-directory pathlists of ignore patterns. */
3324 struct got_pathlist_head *ignores;
3325 int report_unchanged;
3329 static const struct got_error *
3330 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3331 int dirfd, const char *de_name,
3332 got_worktree_status_cb status_cb, void *status_arg,
3333 struct got_repository *repo, int report_unchanged)
3335 const struct got_error *err = NULL;
3336 unsigned char status = GOT_STATUS_NO_CHANGE;
3337 unsigned char staged_status = get_staged_status(ie);
3339 struct got_object_id blob_id, commit_id, staged_blob_id;
3340 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3341 struct got_object_id *staged_blob_idp = NULL;
3343 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3347 if (status == GOT_STATUS_NO_CHANGE &&
3348 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3351 if (got_fileindex_entry_has_blob(ie)) {
3352 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3353 blob_id.algo = got_repo_get_hash_algorithm(repo);
3354 blob_idp = &blob_id;
3356 if (got_fileindex_entry_has_commit(ie)) {
3357 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3358 commit_id.algo = got_repo_get_hash_algorithm(repo);
3359 commit_idp = &commit_id;
3361 if (staged_status == GOT_STATUS_ADD ||
3362 staged_status == GOT_STATUS_MODIFY) {
3363 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
3364 SHA1_DIGEST_LENGTH);
3365 staged_blob_id.algo = got_repo_get_hash_algorithm(repo);
3366 staged_blob_idp = &staged_blob_id;
3369 return (*status_cb)(status_arg, status, staged_status,
3370 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3373 static const struct got_error *
3374 status_old_new(void *arg, struct got_fileindex_entry *ie,
3375 struct dirent *de, const char *parent_path, int dirfd)
3377 const struct got_error *err = NULL;
3378 struct diff_dir_cb_arg *a = arg;
3381 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3382 return got_error(GOT_ERR_CANCELLED);
3384 if (got_path_cmp(parent_path, a->status_path,
3385 strlen(parent_path), a->status_path_len) != 0 &&
3386 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3389 if (parent_path[0]) {
3390 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3391 parent_path, de->d_name) == -1)
3392 return got_error_from_errno("asprintf");
3394 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3396 return got_error_from_errno("asprintf");
3399 err = report_file_status(ie, abspath, dirfd, de->d_name,
3400 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3405 static const struct got_error *
3406 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3408 struct diff_dir_cb_arg *a = arg;
3409 struct got_object_id blob_id, commit_id;
3410 unsigned char status;
3412 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3413 return got_error(GOT_ERR_CANCELLED);
3415 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3418 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3419 blob_id.algo = got_repo_get_hash_algorithm(a->repo);
3420 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3421 commit_id.algo = got_repo_get_hash_algorithm(a->repo);
3422 if (got_fileindex_entry_has_file_on_disk(ie))
3423 status = GOT_STATUS_MISSING;
3425 status = GOT_STATUS_DELETE;
3426 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3427 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3431 free_ignores(struct got_pathlist_head *ignores)
3433 struct got_pathlist_entry *pe;
3435 TAILQ_FOREACH(pe, ignores, entry) {
3436 struct got_pathlist_head *ignorelist = pe->data;
3438 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3440 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3443 static const struct got_error *
3444 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3446 const struct got_error *err = NULL;
3447 struct got_pathlist_entry *pe = NULL;
3448 struct got_pathlist_head *ignorelist;
3449 char *line = NULL, *pattern, *dirpath = NULL;
3450 size_t linesize = 0;
3453 ignorelist = calloc(1, sizeof(*ignorelist));
3454 if (ignorelist == NULL)
3455 return got_error_from_errno("calloc");
3456 TAILQ_INIT(ignorelist);
3458 while ((linelen = getline(&line, &linesize, f)) != -1) {
3459 if (linelen > 0 && line[linelen - 1] == '\n')
3460 line[linelen - 1] = '\0';
3462 /* Git's ignores may contain comments. */
3466 /* Git's negated patterns are not (yet?) supported. */
3470 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3472 err = got_error_from_errno("asprintf");
3475 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3480 err = got_error_from_errno("getline");
3484 dirpath = strdup(path);
3485 if (dirpath == NULL) {
3486 err = got_error_from_errno("strdup");
3489 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3492 if (err || pe == NULL) {
3494 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3500 match_ignores(struct got_pathlist_head *ignores, const char *path)
3502 struct got_pathlist_entry *pe;
3504 /* Handle patterns which match in all directories. */
3505 TAILQ_FOREACH(pe, ignores, entry) {
3506 struct got_pathlist_head *ignorelist = pe->data;
3507 struct got_pathlist_entry *pi;
3509 TAILQ_FOREACH(pi, ignorelist, entry) {
3510 const char *p, *pattern = pi->path;
3512 if (strncmp(pattern, "**/", 3) != 0)
3517 if (fnmatch(pattern, p,
3518 FNM_PATHNAME | FNM_LEADING_DIR)) {
3519 /* Retry in next directory. */
3520 while (*p && *p != '/')
3532 * The ignores pathlist contains ignore lists from children before
3533 * parents, so we can find the most specific ignorelist by walking
3534 * ignores backwards.
3536 pe = TAILQ_LAST(ignores, got_pathlist_head);
3538 if (got_path_is_child(path, pe->path, pe->path_len)) {
3539 struct got_pathlist_head *ignorelist = pe->data;
3540 struct got_pathlist_entry *pi;
3541 TAILQ_FOREACH(pi, ignorelist, entry) {
3542 const char *pattern = pi->path;
3543 int flags = FNM_LEADING_DIR;
3544 if (strstr(pattern, "/**/") == NULL)
3545 flags |= FNM_PATHNAME;
3546 if (fnmatch(pattern, path, flags))
3551 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3557 static const struct got_error *
3558 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3559 const char *path, int dirfd, const char *ignores_filename)
3561 const struct got_error *err = NULL;
3564 FILE *ignoresfile = NULL;
3566 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3567 path[0] ? "/" : "", ignores_filename) == -1)
3568 return got_error_from_errno("asprintf");
3571 fd = openat(dirfd, ignores_filename,
3572 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3574 if (errno != ENOENT && errno != EACCES)
3575 err = got_error_from_errno2("openat",
3578 ignoresfile = fdopen(fd, "r");
3579 if (ignoresfile == NULL)
3580 err = got_error_from_errno2("fdopen",
3584 err = read_ignores(ignores, path, ignoresfile);
3588 ignoresfile = fopen(ignorespath, "re");
3589 if (ignoresfile == NULL) {
3590 if (errno != ENOENT && errno != EACCES)
3591 err = got_error_from_errno2("fopen",
3594 err = read_ignores(ignores, path, ignoresfile);
3597 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3598 err = got_error_from_errno2("fclose", path);
3599 if (fd != -1 && close(fd) == -1 && err == NULL)
3600 err = got_error_from_errno2("close", path);
3605 static const struct got_error *
3606 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3609 const struct got_error *err = NULL;
3610 struct diff_dir_cb_arg *a = arg;
3616 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3617 return got_error(GOT_ERR_CANCELLED);
3619 if (parent_path[0]) {
3620 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3621 return got_error_from_errno("asprintf");
3626 if (de->d_type == DT_DIR) {
3627 if (!a->no_ignores && ignore != NULL &&
3628 match_ignores(a->ignores, path))
3630 } else if (!match_ignores(a->ignores, path) &&
3631 got_path_is_child(path, a->status_path, a->status_path_len))
3632 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3633 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3639 static const struct got_error *
3640 status_traverse(void *arg, const char *path, int dirfd)
3642 const struct got_error *err = NULL;
3643 struct diff_dir_cb_arg *a = arg;
3648 err = add_ignores(a->ignores, a->worktree->root_path,
3649 path, dirfd, ".cvsignore");
3653 err = add_ignores(a->ignores, a->worktree->root_path, path,
3654 dirfd, ".gitignore");
3659 static const struct got_error *
3660 report_single_file_status(const char *path, const char *ondisk_path,
3661 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3662 void *status_arg, struct got_repository *repo, int report_unchanged,
3663 struct got_pathlist_head *ignores, int no_ignores)
3665 struct got_fileindex_entry *ie;
3668 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3670 return report_file_status(ie, ondisk_path, -1, NULL,
3671 status_cb, status_arg, repo, report_unchanged);
3673 if (lstat(ondisk_path, &sb) == -1) {
3674 if (errno != ENOENT)
3675 return got_error_from_errno2("lstat", ondisk_path);
3676 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3677 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3680 if (!no_ignores && match_ignores(ignores, path))
3683 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3684 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3685 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3690 static const struct got_error *
3691 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3692 const char *root_path, const char *path)
3694 const struct got_error *err;
3695 char *parent_path, *next_parent_path = NULL;
3697 err = add_ignores(ignores, root_path, "", -1,
3702 err = add_ignores(ignores, root_path, "", -1,
3707 err = got_path_dirname(&parent_path, path);
3709 if (err->code == GOT_ERR_BAD_PATH)
3710 return NULL; /* cannot traverse parent */
3714 err = add_ignores(ignores, root_path, parent_path, -1,
3718 err = add_ignores(ignores, root_path, parent_path, -1,
3722 err = got_path_dirname(&next_parent_path, parent_path);
3724 if (err->code == GOT_ERR_BAD_PATH)
3725 err = NULL; /* traversed everything */
3728 if (got_path_is_root_dir(parent_path))
3731 parent_path = next_parent_path;
3732 next_parent_path = NULL;
3736 free(next_parent_path);
3740 static const struct got_error *
3741 worktree_status(struct got_worktree *worktree, const char *path,
3742 struct got_fileindex *fileindex, struct got_repository *repo,
3743 got_worktree_status_cb status_cb, void *status_arg,
3744 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3745 int report_unchanged)
3747 const struct got_error *err = NULL;
3749 struct got_fileindex_diff_dir_cb fdiff_cb;
3750 struct diff_dir_cb_arg arg;
3751 char *ondisk_path = NULL;
3752 struct got_pathlist_head ignores;
3753 struct got_fileindex_entry *ie;
3755 TAILQ_INIT(&ignores);
3757 if (asprintf(&ondisk_path, "%s%s%s",
3758 worktree->root_path, path[0] ? "/" : "", path) == -1)
3759 return got_error_from_errno("asprintf");
3761 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3763 err = report_single_file_status(path, ondisk_path,
3764 fileindex, status_cb, status_arg, repo,
3765 report_unchanged, &ignores, no_ignores);
3769 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3771 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3772 !got_err_open_nofollow_on_symlink())
3773 err = got_error_from_errno2("open", ondisk_path);
3776 err = add_ignores_from_parent_paths(&ignores,
3777 worktree->root_path, ondisk_path);
3781 err = report_single_file_status(path, ondisk_path,
3782 fileindex, status_cb, status_arg, repo,
3783 report_unchanged, &ignores, no_ignores);
3786 fdiff_cb.diff_old_new = status_old_new;
3787 fdiff_cb.diff_old = status_old;
3788 fdiff_cb.diff_new = status_new;
3789 fdiff_cb.diff_traverse = status_traverse;
3790 arg.fileindex = fileindex;
3791 arg.worktree = worktree;
3792 arg.status_path = path;
3793 arg.status_path_len = strlen(path);
3795 arg.status_cb = status_cb;
3796 arg.status_arg = status_arg;
3797 arg.cancel_cb = cancel_cb;
3798 arg.cancel_arg = cancel_arg;
3799 arg.report_unchanged = report_unchanged;
3800 arg.no_ignores = no_ignores;
3802 err = add_ignores_from_parent_paths(&ignores,
3803 worktree->root_path, path);
3807 arg.ignores = &ignores;
3808 err = got_fileindex_diff_dir(fileindex, fd,
3809 worktree->root_path, path, repo, &fdiff_cb, &arg);
3812 free_ignores(&ignores);
3813 if (fd != -1 && close(fd) == -1 && err == NULL)
3814 err = got_error_from_errno("close");
3819 const struct got_error *
3820 got_worktree_status(struct got_worktree *worktree,
3821 struct got_pathlist_head *paths, struct got_repository *repo,
3822 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3823 got_cancel_cb cancel_cb, void *cancel_arg)
3825 const struct got_error *err = NULL;
3826 char *fileindex_path = NULL;
3827 struct got_fileindex *fileindex = NULL;
3828 struct got_pathlist_entry *pe;
3830 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3834 TAILQ_FOREACH(pe, paths, entry) {
3835 err = worktree_status(worktree, pe->path, fileindex, repo,
3836 status_cb, status_arg, cancel_cb, cancel_arg,
3841 free(fileindex_path);
3842 got_fileindex_free(fileindex);
3846 const struct got_error *
3847 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3850 const struct got_error *err = NULL;
3851 char *resolved = NULL, *cwd = NULL, *path = NULL;
3854 char *abspath = NULL;
3855 char canonpath[PATH_MAX];
3859 cwd = getcwd(NULL, 0);
3861 return got_error_from_errno("getcwd");
3863 if (lstat(arg, &sb) == -1) {
3864 if (errno != ENOENT) {
3865 err = got_error_from_errno2("lstat", arg);
3870 if (S_ISLNK(sb.st_mode)) {
3872 * We cannot use realpath(3) with symlinks since we want to
3873 * operate on the symlink itself.
3874 * But we can make the path absolute, assuming it is relative
3875 * to the current working directory, and then canonicalize it.
3877 if (!got_path_is_absolute(arg)) {
3878 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3879 err = got_error_from_errno("asprintf");
3884 err = got_canonpath(abspath ? abspath : arg, canonpath,
3888 resolved = strdup(canonpath);
3889 if (resolved == NULL) {
3890 err = got_error_from_errno("strdup");
3894 resolved = realpath(arg, NULL);
3895 if (resolved == NULL) {
3896 if (errno != ENOENT) {
3897 err = got_error_from_errno2("realpath", arg);
3900 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3901 err = got_error_from_errno("asprintf");
3904 err = got_canonpath(abspath, canonpath,
3908 resolved = strdup(canonpath);
3909 if (resolved == NULL) {
3910 err = got_error_from_errno("strdup");
3916 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3917 strlen(got_worktree_get_root_path(worktree)))) {
3918 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3922 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3923 err = got_path_skip_common_ancestor(&path,
3924 got_worktree_get_root_path(worktree), resolved);
3930 err = got_error_from_errno("strdup");
3935 /* XXX status walk can't deal with trailing slash! */
3937 while (len > 0 && path[len - 1] == '/') {
3938 path[len - 1] = '\0';
3952 struct schedule_addition_args {
3953 struct got_worktree *worktree;
3954 struct got_fileindex *fileindex;
3955 got_worktree_checkout_cb progress_cb;
3957 struct got_repository *repo;
3960 static const struct got_error *
3961 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3962 const char *relpath, struct got_object_id *blob_id,
3963 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3964 int dirfd, const char *de_name)
3966 struct schedule_addition_args *a = arg;
3967 const struct got_error *err = NULL;
3968 struct got_fileindex_entry *ie;
3972 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3974 return got_error_from_errno("asprintf");
3976 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3978 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3982 /* Re-adding an existing entry is a no-op. */
3983 if (status == GOT_STATUS_ADD)
3985 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3990 if (status != GOT_STATUS_UNVERSIONED) {
3991 if (status == GOT_STATUS_NONEXISTENT)
3992 err = got_error_set_errno(ENOENT, ondisk_path);
3994 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3998 err = got_fileindex_entry_alloc(&ie, relpath);
4001 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4002 relpath, NULL, NULL, 1);
4004 got_fileindex_entry_free(ie);
4007 err = got_fileindex_entry_add(a->fileindex, ie);
4009 got_fileindex_entry_free(ie);
4016 if (status == GOT_STATUS_ADD)
4018 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4021 const struct got_error *
4022 got_worktree_schedule_add(struct got_worktree *worktree,
4023 struct got_pathlist_head *paths,
4024 got_worktree_checkout_cb progress_cb, void *progress_arg,
4025 struct got_repository *repo, int no_ignores)
4027 struct got_fileindex *fileindex = NULL;
4028 char *fileindex_path = NULL;
4029 const struct got_error *err = NULL, *sync_err, *unlockerr;
4030 struct got_pathlist_entry *pe;
4031 struct schedule_addition_args saa;
4033 err = lock_worktree(worktree, LOCK_EX);
4037 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4041 saa.worktree = worktree;
4042 saa.fileindex = fileindex;
4043 saa.progress_cb = progress_cb;
4044 saa.progress_arg = progress_arg;
4047 TAILQ_FOREACH(pe, paths, entry) {
4048 err = worktree_status(worktree, pe->path, fileindex, repo,
4049 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4053 sync_err = sync_fileindex(fileindex, fileindex_path);
4054 if (sync_err && err == NULL)
4057 free(fileindex_path);
4059 got_fileindex_free(fileindex);
4060 unlockerr = lock_worktree(worktree, LOCK_SH);
4061 if (unlockerr && err == NULL)
4066 struct schedule_deletion_args {
4067 struct got_worktree *worktree;
4068 struct got_fileindex *fileindex;
4069 got_worktree_delete_cb progress_cb;
4071 struct got_repository *repo;
4072 int delete_local_mods;
4074 int ignore_missing_paths;
4075 const char *status_codes;
4078 static const struct got_error *
4079 schedule_for_deletion(void *arg, unsigned char status,
4080 unsigned char staged_status, const char *relpath,
4081 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4082 struct got_object_id *commit_id, int dirfd, const char *de_name)
4084 struct schedule_deletion_args *a = arg;
4085 const struct got_error *err = NULL;
4086 struct got_fileindex_entry *ie = NULL;
4090 if (status == GOT_STATUS_NONEXISTENT) {
4091 if (a->ignore_missing_paths)
4093 return got_error_set_errno(ENOENT, relpath);
4096 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4098 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4100 staged_status = get_staged_status(ie);
4101 if (staged_status != GOT_STATUS_NO_CHANGE) {
4102 if (staged_status == GOT_STATUS_DELETE)
4104 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4107 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4109 return got_error_from_errno("asprintf");
4111 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4116 if (a->status_codes) {
4117 size_t ncodes = strlen(a->status_codes);
4119 for (i = 0; i < ncodes ; i++) {
4120 if (status == a->status_codes[i])
4124 /* Do not delete files in non-matching status. */
4128 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4129 a->status_codes[i] != GOT_STATUS_MISSING) {
4130 static char msg[64];
4131 snprintf(msg, sizeof(msg),
4132 "invalid status code '%c'", a->status_codes[i]);
4133 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4138 if (status != GOT_STATUS_NO_CHANGE) {
4139 if (status == GOT_STATUS_DELETE)
4141 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4142 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4145 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4146 err = got_error_set_errno(ENOENT, relpath);
4149 if (status != GOT_STATUS_MODIFY &&
4150 status != GOT_STATUS_MISSING) {
4151 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4156 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4160 if (unlinkat(dirfd, de_name, 0) == -1) {
4161 err = got_error_from_errno2("unlinkat",
4165 } else if (unlink(ondisk_path) == -1) {
4166 err = got_error_from_errno2("unlink", ondisk_path);
4170 root_len = strlen(a->worktree->root_path);
4173 err = got_path_dirname(&parent, ondisk_path);
4177 ondisk_path = parent;
4178 if (rmdir(ondisk_path) == -1) {
4179 if (errno != ENOTEMPTY)
4180 err = got_error_from_errno2("rmdir",
4184 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4185 strlen(ondisk_path), root_len) != 0);
4188 got_fileindex_entry_mark_deleted_from_disk(ie);
4193 if (status == GOT_STATUS_DELETE)
4195 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4196 staged_status, relpath);
4199 const struct got_error *
4200 got_worktree_schedule_delete(struct got_worktree *worktree,
4201 struct got_pathlist_head *paths, int delete_local_mods,
4202 const char *status_codes,
4203 got_worktree_delete_cb progress_cb, void *progress_arg,
4204 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4206 struct got_fileindex *fileindex = NULL;
4207 char *fileindex_path = NULL;
4208 const struct got_error *err = NULL, *sync_err, *unlockerr;
4209 struct got_pathlist_entry *pe;
4210 struct schedule_deletion_args sda;
4212 err = lock_worktree(worktree, LOCK_EX);
4216 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4220 sda.worktree = worktree;
4221 sda.fileindex = fileindex;
4222 sda.progress_cb = progress_cb;
4223 sda.progress_arg = progress_arg;
4225 sda.delete_local_mods = delete_local_mods;
4226 sda.keep_on_disk = keep_on_disk;
4227 sda.ignore_missing_paths = ignore_missing_paths;
4228 sda.status_codes = status_codes;
4230 TAILQ_FOREACH(pe, paths, entry) {
4231 err = worktree_status(worktree, pe->path, fileindex, repo,
4232 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4236 sync_err = sync_fileindex(fileindex, fileindex_path);
4237 if (sync_err && err == NULL)
4240 free(fileindex_path);
4242 got_fileindex_free(fileindex);
4243 unlockerr = lock_worktree(worktree, LOCK_SH);
4244 if (unlockerr && err == NULL)
4249 static const struct got_error *
4250 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4252 const struct got_error *err = NULL;
4254 size_t linesize = 0, n;
4257 linelen = getline(&line, &linesize, infile);
4258 if (linelen == -1) {
4259 if (ferror(infile)) {
4260 err = got_error_from_errno("getline");
4266 n = fwrite(line, 1, linelen, outfile);
4268 err = got_ferror(outfile, GOT_ERR_IO);
4273 n = fwrite(line, 1, linelen, rejectfile);
4275 err = got_ferror(rejectfile, GOT_ERR_IO);
4282 static const struct got_error *
4283 skip_one_line(FILE *f)
4286 size_t linesize = 0;
4289 linelen = getline(&line, &linesize, f);
4290 if (linelen == -1) {
4292 return got_error_from_errno("getline");
4299 static const struct got_error *
4300 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4301 int start_old, int end_old, int start_new, int end_new,
4302 FILE *outfile, FILE *rejectfile)
4304 const struct got_error *err;
4306 /* Copy old file's lines leading up to patch. */
4307 while (!feof(f1) && *line_cur1 < start_old) {
4308 err = copy_one_line(f1, outfile, NULL);
4313 /* Skip new file's lines leading up to patch. */
4314 while (!feof(f2) && *line_cur2 < start_new) {
4316 err = copy_one_line(f2, NULL, rejectfile);
4318 err = skip_one_line(f2);
4323 /* Copy patched lines. */
4324 while (!feof(f2) && *line_cur2 <= end_new) {
4325 err = copy_one_line(f2, outfile, NULL);
4330 /* Skip over old file's replaced lines. */
4331 while (!feof(f1) && *line_cur1 <= end_old) {
4333 err = copy_one_line(f1, NULL, rejectfile);
4335 err = skip_one_line(f1);
4344 static const struct got_error *
4345 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4346 FILE *outfile, FILE *rejectfile)
4348 const struct got_error *err;
4351 /* Copy old file's lines until EOF. */
4353 err = copy_one_line(f1, outfile, NULL);
4360 /* Copy new file's lines until EOF. */
4362 err = copy_one_line(f2, NULL, rejectfile);
4372 static const struct got_error *
4373 apply_or_reject_change(int *choice, int *nchunks_used,
4374 struct diff_result *diff_result, int n,
4375 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4376 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4377 got_worktree_patch_cb patch_cb, void *patch_arg)
4379 const struct got_error *err = NULL;
4380 struct diff_chunk_context cc = {};
4381 int start_old, end_old, start_new, end_new;
4383 struct diff_output_unidiff_state *diff_state;
4384 struct diff_input_info diff_info;
4387 *choice = GOT_PATCH_CHOICE_NONE;
4389 /* Get changed line numbers without context lines for copy_change(). */
4390 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4391 start_old = cc.left.start;
4392 end_old = cc.left.end;
4393 start_new = cc.right.start;
4394 end_new = cc.right.end;
4396 /* Get the same change with context lines for display. */
4397 memset(&cc, 0, sizeof(cc));
4398 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4400 memset(&diff_info, 0, sizeof(diff_info));
4401 diff_info.left_path = relpath;
4402 diff_info.right_path = relpath;
4404 diff_state = diff_output_unidiff_state_alloc();
4405 if (diff_state == NULL)
4406 return got_error_set_errno(ENOMEM,
4407 "diff_output_unidiff_state_alloc");
4409 hunkfile = got_opentemp();
4410 if (hunkfile == NULL) {
4411 err = got_error_from_errno("got_opentemp");
4415 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4417 if (rc != DIFF_RC_OK) {
4418 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4422 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4423 err = got_ferror(hunkfile, GOT_ERR_IO);
4427 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4428 hunkfile, changeno, nchanges);
4433 case GOT_PATCH_CHOICE_YES:
4434 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4435 end_old, start_new, end_new, outfile, rejectfile);
4437 case GOT_PATCH_CHOICE_NO:
4438 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4439 end_old, start_new, end_new, rejectfile, outfile);
4441 case GOT_PATCH_CHOICE_QUIT:
4444 err = got_error(GOT_ERR_PATCH_CHOICE);
4448 diff_output_unidiff_state_free(diff_state);
4449 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4450 err = got_error_from_errno("fclose");
4454 struct revert_file_args {
4455 struct got_worktree *worktree;
4456 struct got_fileindex *fileindex;
4457 got_worktree_checkout_cb progress_cb;
4459 got_worktree_patch_cb patch_cb;
4461 struct got_repository *repo;
4462 int unlink_added_files;
4465 static const struct got_error *
4466 create_patched_content(char **path_outfile, int reverse_patch,
4467 struct got_object_id *blob_id, const char *path2,
4468 int dirfd2, const char *de_name2,
4469 const char *relpath, struct got_repository *repo,
4470 got_worktree_patch_cb patch_cb, void *patch_arg)
4472 const struct got_error *err, *free_err;
4473 struct got_blob_object *blob = NULL;
4474 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4475 int fd = -1, fd2 = -1;
4476 char link_target[PATH_MAX];
4477 ssize_t link_len = 0;
4478 char *path1 = NULL, *id_str = NULL;
4480 struct got_diffreg_result *diffreg_result = NULL;
4481 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4482 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4484 *path_outfile = NULL;
4486 err = got_object_id_str(&id_str, blob_id);
4491 fd2 = openat(dirfd2, de_name2,
4492 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4494 if (!got_err_open_nofollow_on_symlink()) {
4495 err = got_error_from_errno2("openat", path2);
4498 link_len = readlinkat(dirfd2, de_name2,
4499 link_target, sizeof(link_target));
4500 if (link_len == -1) {
4501 return got_error_from_errno2("readlinkat",
4504 sb2.st_mode = S_IFLNK;
4505 sb2.st_size = link_len;
4508 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4510 if (!got_err_open_nofollow_on_symlink()) {
4511 err = got_error_from_errno2("open", path2);
4514 link_len = readlink(path2, link_target,
4515 sizeof(link_target));
4517 return got_error_from_errno2("readlink", path2);
4518 sb2.st_mode = S_IFLNK;
4519 sb2.st_size = link_len;
4523 if (fstat(fd2, &sb2) == -1) {
4524 err = got_error_from_errno2("fstat", path2);
4528 f2 = fdopen(fd2, "r");
4530 err = got_error_from_errno2("fdopen", path2);
4536 f2 = got_opentemp();
4538 err = got_error_from_errno2("got_opentemp", path2);
4541 n = fwrite(link_target, 1, link_len, f2);
4542 if (n != link_len) {
4543 err = got_ferror(f2, GOT_ERR_IO);
4546 if (fflush(f2) == EOF) {
4547 err = got_error_from_errno("fflush");
4553 fd = got_opentempfd();
4555 err = got_error_from_errno("got_opentempfd");
4559 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4563 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4567 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4571 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4572 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4576 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4581 if (fseek(f1, 0L, SEEK_SET) == -1)
4582 return got_ferror(f1, GOT_ERR_IO);
4583 if (fseek(f2, 0L, SEEK_SET) == -1)
4584 return got_ferror(f2, GOT_ERR_IO);
4586 /* Count the number of actual changes in the diff result. */
4587 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4588 struct diff_chunk_context cc = {};
4589 diff_chunk_context_load_change(&cc, &nchunks_used,
4590 diffreg_result->result, n, 0);
4593 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4595 err = apply_or_reject_change(&choice, &nchunks_used,
4596 diffreg_result->result, n, relpath, f1, f2,
4597 &line_cur1, &line_cur2,
4598 reverse_patch ? NULL : outfile,
4599 reverse_patch ? outfile : NULL,
4600 ++i, nchanges, patch_cb, patch_arg);
4603 if (choice == GOT_PATCH_CHOICE_YES)
4605 else if (choice == GOT_PATCH_CHOICE_QUIT)
4609 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4610 reverse_patch ? NULL : outfile,
4611 reverse_patch ? outfile : NULL);
4615 if (!S_ISLNK(sb2.st_mode)) {
4618 mode = apply_umask(sb2.st_mode);
4619 if (fchmod(fileno(outfile), mode) == -1) {
4620 err = got_error_from_errno2("fchmod", path2);
4627 if (fd != -1 && close(fd) == -1 && err == NULL)
4628 err = got_error_from_errno("close");
4630 got_object_blob_close(blob);
4631 free_err = got_diffreg_result_free(diffreg_result);
4634 if (f1 && fclose(f1) == EOF && err == NULL)
4635 err = got_error_from_errno2("fclose", path1);
4636 if (f2 && fclose(f2) == EOF && err == NULL)
4637 err = got_error_from_errno2("fclose", path2);
4638 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4639 err = got_error_from_errno2("close", path2);
4640 if (outfile && fclose(outfile) == EOF && err == NULL)
4641 err = got_error_from_errno2("fclose", *path_outfile);
4642 if (path1 && unlink(path1) == -1 && err == NULL)
4643 err = got_error_from_errno2("unlink", path1);
4644 if (err || !have_content) {
4645 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4646 err = got_error_from_errno2("unlink", *path_outfile);
4647 free(*path_outfile);
4648 *path_outfile = NULL;
4654 static const struct got_error *
4655 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4656 const char *relpath, struct got_object_id *blob_id,
4657 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4658 int dirfd, const char *de_name)
4660 struct revert_file_args *a = arg;
4661 const struct got_error *err = NULL;
4662 char *parent_path = NULL;
4663 struct got_fileindex_entry *ie;
4664 struct got_commit_object *base_commit = NULL;
4665 struct got_tree_object *tree = NULL;
4666 struct got_object_id *tree_id = NULL;
4667 const struct got_tree_entry *te = NULL;
4668 char *tree_path = NULL, *te_name;
4669 char *ondisk_path = NULL, *path_content = NULL;
4670 struct got_blob_object *blob = NULL;
4673 /* Reverting a staged deletion is a no-op. */
4674 if (status == GOT_STATUS_DELETE &&
4675 staged_status != GOT_STATUS_NO_CHANGE)
4678 if (status == GOT_STATUS_UNVERSIONED)
4679 return (*a->progress_cb)(a->progress_arg,
4680 GOT_STATUS_UNVERSIONED, relpath);
4682 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4684 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4686 /* Construct in-repository path of tree which contains this blob. */
4687 err = got_path_dirname(&parent_path, ie->path);
4689 if (err->code != GOT_ERR_BAD_PATH)
4691 parent_path = strdup("/");
4692 if (parent_path == NULL) {
4693 err = got_error_from_errno("strdup");
4697 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4698 tree_path = strdup(parent_path);
4699 if (tree_path == NULL) {
4700 err = got_error_from_errno("strdup");
4704 if (got_path_is_root_dir(parent_path)) {
4705 tree_path = strdup(a->worktree->path_prefix);
4706 if (tree_path == NULL) {
4707 err = got_error_from_errno("strdup");
4711 if (asprintf(&tree_path, "%s/%s",
4712 a->worktree->path_prefix, parent_path) == -1) {
4713 err = got_error_from_errno("asprintf");
4719 err = got_object_open_as_commit(&base_commit, a->repo,
4720 a->worktree->base_commit_id);
4724 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4726 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4727 (status == GOT_STATUS_ADD ||
4728 staged_status == GOT_STATUS_ADD)))
4731 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4735 err = got_path_basename(&te_name, ie->path);
4739 te = got_object_tree_find_entry(tree, te_name);
4741 if (te == NULL && status != GOT_STATUS_ADD &&
4742 staged_status != GOT_STATUS_ADD) {
4743 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4749 case GOT_STATUS_ADD:
4751 int choice = GOT_PATCH_CHOICE_NONE;
4752 err = (*a->patch_cb)(&choice, a->patch_arg,
4753 status, ie->path, NULL, 1, 1);
4756 if (choice != GOT_PATCH_CHOICE_YES)
4759 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4763 got_fileindex_entry_remove(a->fileindex, ie);
4764 if (a->unlink_added_files) {
4765 if (asprintf(&ondisk_path, "%s/%s",
4766 got_worktree_get_root_path(a->worktree),
4768 err = got_error_from_errno("asprintf");
4771 if (unlink(ondisk_path) == -1) {
4772 err = got_error_from_errno2("unlink",
4778 case GOT_STATUS_DELETE:
4780 int choice = GOT_PATCH_CHOICE_NONE;
4781 err = (*a->patch_cb)(&choice, a->patch_arg,
4782 status, ie->path, NULL, 1, 1);
4785 if (choice != GOT_PATCH_CHOICE_YES)
4789 case GOT_STATUS_MODIFY:
4790 case GOT_STATUS_MODE_CHANGE:
4791 case GOT_STATUS_CONFLICT:
4792 case GOT_STATUS_MISSING: {
4793 struct got_object_id id;
4794 if (staged_status == GOT_STATUS_ADD ||
4795 staged_status == GOT_STATUS_MODIFY) {
4796 memcpy(id.hash, ie->staged_blob_hash,
4797 SHA1_DIGEST_LENGTH);
4799 memcpy(id.hash, ie->blob_hash,
4800 SHA1_DIGEST_LENGTH);
4801 id.algo = got_repo_get_hash_algorithm(a->repo);
4803 fd = got_opentempfd();
4805 err = got_error_from_errno("got_opentempfd");
4809 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4813 if (asprintf(&ondisk_path, "%s/%s",
4814 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4815 err = got_error_from_errno("asprintf");
4819 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4820 status == GOT_STATUS_CONFLICT)) {
4821 int is_bad_symlink = 0;
4822 err = create_patched_content(&path_content, 1, &id,
4823 ondisk_path, dirfd, de_name, ie->path, a->repo,
4824 a->patch_cb, a->patch_arg);
4825 if (err || path_content == NULL)
4827 if (te && S_ISLNK(te->mode)) {
4828 if (unlink(path_content) == -1) {
4829 err = got_error_from_errno2("unlink",
4833 err = install_symlink(&is_bad_symlink,
4834 a->worktree, ondisk_path, ie->path,
4835 blob, 0, 1, 0, 0, a->repo,
4836 a->progress_cb, a->progress_arg);
4838 if (rename(path_content, ondisk_path) == -1) {
4839 err = got_error_from_errno3("rename",
4840 path_content, ondisk_path);
4845 int is_bad_symlink = 0;
4846 if (te && S_ISLNK(te->mode)) {
4847 err = install_symlink(&is_bad_symlink,
4848 a->worktree, ondisk_path, ie->path,
4849 blob, 0, 1, 0, 0, a->repo,
4850 a->progress_cb, a->progress_arg);
4852 err = install_blob(a->worktree, ondisk_path,
4854 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4855 got_fileindex_perms_to_st(ie), blob,
4856 0, 1, 0, 0, a->repo,
4857 a->progress_cb, a->progress_arg);
4861 if (status == GOT_STATUS_DELETE ||
4862 status == GOT_STATUS_MODE_CHANGE) {
4863 err = got_fileindex_entry_update(ie,
4864 a->worktree->root_fd, relpath,
4866 a->worktree->base_commit_id->hash, 1);
4870 if (is_bad_symlink) {
4871 got_fileindex_entry_filetype_set(ie,
4872 GOT_FILEIDX_MODE_BAD_SYMLINK);
4885 if (fd != -1 && close(fd) == -1 && err == NULL)
4886 err = got_error_from_errno("close");
4888 got_object_blob_close(blob);
4890 got_object_tree_close(tree);
4893 got_object_commit_close(base_commit);
4897 const struct got_error *
4898 got_worktree_revert(struct got_worktree *worktree,
4899 struct got_pathlist_head *paths,
4900 got_worktree_checkout_cb progress_cb, void *progress_arg,
4901 got_worktree_patch_cb patch_cb, void *patch_arg,
4902 struct got_repository *repo)
4904 struct got_fileindex *fileindex = NULL;
4905 char *fileindex_path = NULL;
4906 const struct got_error *err = NULL, *unlockerr = NULL;
4907 const struct got_error *sync_err = NULL;
4908 struct got_pathlist_entry *pe;
4909 struct revert_file_args rfa;
4911 err = lock_worktree(worktree, LOCK_EX);
4915 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4919 rfa.worktree = worktree;
4920 rfa.fileindex = fileindex;
4921 rfa.progress_cb = progress_cb;
4922 rfa.progress_arg = progress_arg;
4923 rfa.patch_cb = patch_cb;
4924 rfa.patch_arg = patch_arg;
4926 rfa.unlink_added_files = 0;
4927 TAILQ_FOREACH(pe, paths, entry) {
4928 err = worktree_status(worktree, pe->path, fileindex, repo,
4929 revert_file, &rfa, NULL, NULL, 1, 0);
4933 sync_err = sync_fileindex(fileindex, fileindex_path);
4934 if (sync_err && err == NULL)
4937 free(fileindex_path);
4939 got_fileindex_free(fileindex);
4940 unlockerr = lock_worktree(worktree, LOCK_SH);
4941 if (unlockerr && err == NULL)
4947 free_commitable(struct got_commitable *ct)
4950 free(ct->in_repo_path);
4951 free(ct->ondisk_path);
4953 free(ct->base_blob_id);
4954 free(ct->staged_blob_id);
4955 free(ct->base_commit_id);
4959 struct collect_commitables_arg {
4960 struct got_pathlist_head *commitable_paths;
4961 struct got_repository *repo;
4962 struct got_worktree *worktree;
4963 struct got_fileindex *fileindex;
4964 int have_staged_files;
4965 int allow_bad_symlinks;
4966 int diff_header_shown;
4973 * Create a file which contains the target path of a symlink so we can feed
4974 * it as content to the diff engine.
4976 static const struct got_error *
4977 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4978 const char *abspath)
4980 const struct got_error *err = NULL;
4981 char target_path[PATH_MAX];
4982 ssize_t target_len, outlen;
4987 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4988 if (target_len == -1)
4989 return got_error_from_errno2("readlinkat", abspath);
4991 target_len = readlink(abspath, target_path, PATH_MAX);
4992 if (target_len == -1)
4993 return got_error_from_errno2("readlink", abspath);
4996 *fd = got_opentempfd();
4998 return got_error_from_errno("got_opentempfd");
5000 outlen = write(*fd, target_path, target_len);
5002 err = got_error_from_errno("got_opentempfd");
5006 if (lseek(*fd, 0, SEEK_SET) == -1) {
5007 err = got_error_from_errno2("lseek", abspath);
5018 static const struct got_error *
5019 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5020 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5021 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5023 const struct got_error *err = NULL;
5024 struct got_blob_object *blob1 = NULL;
5025 int fd = -1, fd1 = -1, fd2 = -1;
5026 FILE *ondisk_file = NULL;
5027 char *label1 = NULL;
5031 char *id_str = NULL;
5033 memset(&sb, 0, sizeof(sb));
5036 if (ct->staged_status != GOT_STATUS_MODIFY &&
5037 ct->staged_status != GOT_STATUS_ADD &&
5038 ct->staged_status != GOT_STATUS_DELETE)
5041 if (ct->status != GOT_STATUS_MODIFY &&
5042 ct->status != GOT_STATUS_ADD &&
5043 ct->status != GOT_STATUS_DELETE)
5047 err = got_opentemp_truncate(f1);
5049 return got_error_from_errno("got_opentemp_truncate");
5050 err = got_opentemp_truncate(f2);
5052 return got_error_from_errno("got_opentemp_truncate");
5054 if (!*diff_header_shown) {
5055 err = got_object_id_str(&id_str, worktree->base_commit_id);
5058 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5059 got_worktree_get_root_path(worktree));
5060 fprintf(diff_outfile, "commit - %s\n", id_str);
5061 fprintf(diff_outfile, "path + %s%s\n",
5062 got_worktree_get_root_path(worktree),
5063 diff_staged ? " (staged changes)" : "");
5064 *diff_header_shown = 1;
5068 const char *label1 = NULL, *label2 = NULL;
5069 switch (ct->staged_status) {
5070 case GOT_STATUS_MODIFY:
5074 case GOT_STATUS_ADD:
5077 case GOT_STATUS_DELETE:
5081 return got_error(GOT_ERR_FILE_STATUS);
5083 fd1 = got_opentempfd();
5085 err = got_error_from_errno("got_opentempfd");
5088 fd2 = got_opentempfd();
5090 err = got_error_from_errno("got_opentempfd");
5093 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5094 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5095 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5096 NULL, repo, diff_outfile);
5100 fd1 = got_opentempfd();
5102 err = got_error_from_errno("got_opentempfd");
5106 if (ct->status != GOT_STATUS_ADD) {
5107 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5113 if (ct->status != GOT_STATUS_DELETE) {
5115 fd = openat(dirfd, de_name,
5116 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5118 if (!got_err_open_nofollow_on_symlink()) {
5119 err = got_error_from_errno2("openat",
5123 err = get_symlink_target_file(&fd, dirfd,
5124 de_name, ct->ondisk_path);
5129 fd = open(ct->ondisk_path,
5130 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5132 if (!got_err_open_nofollow_on_symlink()) {
5133 err = got_error_from_errno2("open",
5137 err = get_symlink_target_file(&fd, dirfd,
5138 de_name, ct->ondisk_path);
5143 if (fstatat(fd, ct->ondisk_path, &sb,
5144 AT_SYMLINK_NOFOLLOW) == -1) {
5145 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5148 ondisk_file = fdopen(fd, "r");
5149 if (ondisk_file == NULL) {
5150 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5158 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5164 err = got_diff_blob_file(blob1, f1, size1, label1,
5165 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5166 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5168 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5169 err = got_error_from_errno("close");
5170 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5171 err = got_error_from_errno("close");
5173 got_object_blob_close(blob1);
5174 if (fd != -1 && close(fd) == -1 && err == NULL)
5175 err = got_error_from_errno("close");
5176 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5177 err = got_error_from_errno("fclose");
5181 static const struct got_error *
5182 collect_commitables(void *arg, unsigned char status,
5183 unsigned char staged_status, const char *relpath,
5184 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5185 struct got_object_id *commit_id, int dirfd, const char *de_name)
5187 struct collect_commitables_arg *a = arg;
5188 const struct got_error *err = NULL;
5189 struct got_commitable *ct = NULL;
5190 struct got_pathlist_entry *new = NULL;
5191 char *parent_path = NULL, *path = NULL;
5194 if (a->have_staged_files) {
5195 if (staged_status != GOT_STATUS_MODIFY &&
5196 staged_status != GOT_STATUS_ADD &&
5197 staged_status != GOT_STATUS_DELETE)
5200 if (status == GOT_STATUS_CONFLICT)
5201 return got_error(GOT_ERR_COMMIT_CONFLICT);
5203 if (status != GOT_STATUS_MODIFY &&
5204 status != GOT_STATUS_MODE_CHANGE &&
5205 status != GOT_STATUS_ADD &&
5206 status != GOT_STATUS_DELETE)
5210 if (asprintf(&path, "/%s", relpath) == -1) {
5211 err = got_error_from_errno("asprintf");
5214 if (strcmp(path, "/") == 0) {
5215 parent_path = strdup("");
5216 if (parent_path == NULL)
5217 return got_error_from_errno("strdup");
5219 err = got_path_dirname(&parent_path, path);
5224 ct = calloc(1, sizeof(*ct));
5226 err = got_error_from_errno("calloc");
5230 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5232 err = got_error_from_errno("asprintf");
5236 if (staged_status == GOT_STATUS_ADD ||
5237 staged_status == GOT_STATUS_MODIFY) {
5238 struct got_fileindex_entry *ie;
5239 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5240 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5241 case GOT_FILEIDX_MODE_REGULAR_FILE:
5242 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5245 case GOT_FILEIDX_MODE_SYMLINK:
5249 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5252 ct->mode |= got_fileindex_entry_perms_get(ie);
5253 } else if (status != GOT_STATUS_DELETE &&
5254 staged_status != GOT_STATUS_DELETE) {
5256 if (fstatat(dirfd, de_name, &sb,
5257 AT_SYMLINK_NOFOLLOW) == -1) {
5258 err = got_error_from_errno2("fstatat",
5262 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5263 err = got_error_from_errno2("lstat", ct->ondisk_path);
5266 ct->mode = sb.st_mode;
5269 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5270 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5272 err = got_error_from_errno("asprintf");
5276 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5277 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5279 char target_path[PATH_MAX];
5281 target_len = readlink(ct->ondisk_path, target_path,
5282 sizeof(target_path));
5283 if (target_len == -1) {
5284 err = got_error_from_errno2("readlink",
5288 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5289 target_len, ct->ondisk_path, a->worktree->root_path);
5292 if (is_bad_symlink) {
5293 err = got_error_path(ct->ondisk_path,
5294 GOT_ERR_BAD_SYMLINK);
5300 ct->status = status;
5301 ct->staged_status = staged_status;
5302 ct->blob_id = NULL; /* will be filled in when blob gets created */
5303 if (ct->status != GOT_STATUS_ADD &&
5304 ct->staged_status != GOT_STATUS_ADD) {
5305 ct->base_blob_id = got_object_id_dup(blob_id);
5306 if (ct->base_blob_id == NULL) {
5307 err = got_error_from_errno("got_object_id_dup");
5310 ct->base_commit_id = got_object_id_dup(commit_id);
5311 if (ct->base_commit_id == NULL) {
5312 err = got_error_from_errno("got_object_id_dup");
5316 if (ct->staged_status == GOT_STATUS_ADD ||
5317 ct->staged_status == GOT_STATUS_MODIFY) {
5318 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5319 if (ct->staged_blob_id == NULL) {
5320 err = got_error_from_errno("got_object_id_dup");
5324 ct->path = strdup(path);
5325 if (ct->path == NULL) {
5326 err = got_error_from_errno("strdup");
5329 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5333 if (a->diff_outfile && ct && new != NULL) {
5334 err = append_ct_diff(ct, &a->diff_header_shown,
5335 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5336 a->have_staged_files, a->repo, a->worktree);
5341 if (ct && (err || new == NULL))
5342 free_commitable(ct);
5348 static const struct got_error *write_tree(struct got_object_id **, int *,
5349 struct got_tree_object *, const char *, struct got_pathlist_head *,
5350 got_worktree_status_cb status_cb, void *status_arg,
5351 struct got_repository *);
5353 static const struct got_error *
5354 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5355 struct got_tree_entry *te, const char *parent_path,
5356 struct got_pathlist_head *commitable_paths,
5357 got_worktree_status_cb status_cb, void *status_arg,
5358 struct got_repository *repo)
5360 const struct got_error *err = NULL;
5361 struct got_tree_object *subtree;
5364 if (asprintf(&subpath, "%s%s%s", parent_path,
5365 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5366 return got_error_from_errno("asprintf");
5368 err = got_object_open_as_tree(&subtree, repo, &te->id);
5372 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5373 commitable_paths, status_cb, status_arg, repo);
5374 got_object_tree_close(subtree);
5379 static const struct got_error *
5380 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5382 const struct got_error *err = NULL;
5383 char *ct_parent_path = NULL;
5387 if (strchr(ct->in_repo_path, '/') == NULL) {
5388 *match = got_path_is_root_dir(path);
5392 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5395 *match = (strcmp(path, ct_parent_path) == 0);
5396 free(ct_parent_path);
5401 get_ct_file_mode(struct got_commitable *ct)
5403 if (S_ISLNK(ct->mode))
5406 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5409 static const struct got_error *
5410 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5411 struct got_tree_entry *te, struct got_commitable *ct)
5413 const struct got_error *err = NULL;
5417 err = got_object_tree_entry_dup(new_te, te);
5421 (*new_te)->mode = get_ct_file_mode(ct);
5423 if (ct->staged_status == GOT_STATUS_MODIFY)
5424 memcpy(&(*new_te)->id, ct->staged_blob_id,
5425 sizeof((*new_te)->id));
5427 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5429 if (err && *new_te) {
5436 static const struct got_error *
5437 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5438 struct got_commitable *ct)
5440 const struct got_error *err = NULL;
5441 char *ct_name = NULL;
5445 *new_te = calloc(1, sizeof(**new_te));
5446 if (*new_te == NULL)
5447 return got_error_from_errno("calloc");
5449 err = got_path_basename(&ct_name, ct->path);
5452 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5453 sizeof((*new_te)->name)) {
5454 err = got_error(GOT_ERR_NO_SPACE);
5458 (*new_te)->mode = get_ct_file_mode(ct);
5460 if (ct->staged_status == GOT_STATUS_ADD)
5461 memcpy(&(*new_te)->id, ct->staged_blob_id,
5462 sizeof((*new_te)->id));
5464 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5467 if (err && *new_te) {
5474 static const struct got_error *
5475 insert_tree_entry(struct got_tree_entry *new_te,
5476 struct got_pathlist_head *paths)
5478 const struct got_error *err = NULL;
5479 struct got_pathlist_entry *new_pe;
5481 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5485 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5489 static const struct got_error *
5490 report_ct_status(struct got_commitable *ct,
5491 got_worktree_status_cb status_cb, void *status_arg)
5493 const char *ct_path = ct->path;
5494 unsigned char status;
5496 if (status_cb == NULL) /* no commit progress output desired */
5499 while (ct_path[0] == '/')
5502 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5503 status = ct->staged_status;
5505 status = ct->status;
5507 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5508 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5511 static const struct got_error *
5512 match_modified_subtree(int *modified, struct got_tree_entry *te,
5513 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5515 const struct got_error *err = NULL;
5516 struct got_pathlist_entry *pe;
5521 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5522 got_path_is_root_dir(base_tree_path) ? "" : "/",
5524 return got_error_from_errno("asprintf");
5526 TAILQ_FOREACH(pe, commitable_paths, entry) {
5527 struct got_commitable *ct = pe->data;
5528 *modified = got_path_is_child(ct->in_repo_path, te_path,
5538 static const struct got_error *
5539 match_deleted_or_modified_ct(struct got_commitable **ctp,
5540 struct got_tree_entry *te, const char *base_tree_path,
5541 struct got_pathlist_head *commitable_paths)
5543 const struct got_error *err = NULL;
5544 struct got_pathlist_entry *pe;
5548 TAILQ_FOREACH(pe, commitable_paths, entry) {
5549 struct got_commitable *ct = pe->data;
5550 char *ct_name = NULL;
5553 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5554 if (ct->status != GOT_STATUS_MODIFY &&
5555 ct->status != GOT_STATUS_MODE_CHANGE &&
5556 ct->status != GOT_STATUS_DELETE)
5559 if (ct->staged_status != GOT_STATUS_MODIFY &&
5560 ct->staged_status != GOT_STATUS_DELETE)
5564 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5567 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5573 err = got_path_basename(&ct_name, pe->path);
5577 if (strcmp(te->name, ct_name) != 0) {
5590 static const struct got_error *
5591 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5592 const char *child_path, const char *path_base_tree,
5593 struct got_pathlist_head *commitable_paths,
5594 got_worktree_status_cb status_cb, void *status_arg,
5595 struct got_repository *repo)
5597 const struct got_error *err = NULL;
5598 struct got_tree_entry *new_te;
5600 struct got_object_id *id = NULL;
5605 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5606 got_path_is_root_dir(path_base_tree) ? "" : "/",
5608 return got_error_from_errno("asprintf");
5610 new_te = calloc(1, sizeof(*new_te));
5612 return got_error_from_errno("calloc");
5613 new_te->mode = S_IFDIR;
5615 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5616 sizeof(new_te->name)) {
5617 err = got_error(GOT_ERR_NO_SPACE);
5620 err = write_tree(&id, &nentries, NULL, subtree_path,
5621 commitable_paths, status_cb, status_arg, repo);
5626 memcpy(&new_te->id, id, sizeof(new_te->id));
5635 static const struct got_error *
5636 write_tree(struct got_object_id **new_tree_id, int *nentries,
5637 struct got_tree_object *base_tree, const char *path_base_tree,
5638 struct got_pathlist_head *commitable_paths,
5639 got_worktree_status_cb status_cb, void *status_arg,
5640 struct got_repository *repo)
5642 const struct got_error *err = NULL;
5643 struct got_pathlist_head paths;
5644 struct got_tree_entry *te, *new_te = NULL;
5645 struct got_pathlist_entry *pe;
5650 /* Insert, and recurse into, newly added entries first. */
5651 TAILQ_FOREACH(pe, commitable_paths, entry) {
5652 struct got_commitable *ct = pe->data;
5653 char *child_path = NULL, *slash;
5655 if ((ct->status != GOT_STATUS_ADD &&
5656 ct->staged_status != GOT_STATUS_ADD) ||
5657 (ct->flags & GOT_COMMITABLE_ADDED))
5660 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5661 strlen(path_base_tree)))
5664 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5669 slash = strchr(child_path, '/');
5670 if (slash == NULL) {
5671 err = alloc_added_blob_tree_entry(&new_te, ct);
5674 err = report_ct_status(ct, status_cb, status_arg);
5677 ct->flags |= GOT_COMMITABLE_ADDED;
5678 err = insert_tree_entry(new_te, &paths);
5683 *slash = '\0'; /* trim trailing path components */
5684 if (base_tree == NULL ||
5685 got_object_tree_find_entry(base_tree, child_path)
5687 err = make_subtree_for_added_blob(&new_te,
5688 child_path, path_base_tree,
5689 commitable_paths, status_cb, status_arg,
5693 err = insert_tree_entry(new_te, &paths);
5702 int i, nbase_entries;
5703 /* Handle modified and deleted entries. */
5704 nbase_entries = got_object_tree_get_nentries(base_tree);
5705 for (i = 0; i < nbase_entries; i++) {
5706 struct got_commitable *ct = NULL;
5708 te = got_object_tree_get_entry(base_tree, i);
5709 if (got_object_tree_entry_is_submodule(te)) {
5710 /* Entry is a submodule; just copy it. */
5711 err = got_object_tree_entry_dup(&new_te, te);
5714 err = insert_tree_entry(new_te, &paths);
5721 if (S_ISDIR(te->mode)) {
5723 err = got_object_tree_entry_dup(&new_te, te);
5726 err = match_modified_subtree(&modified, te,
5727 path_base_tree, commitable_paths);
5730 /* Avoid recursion into unmodified subtrees. */
5732 struct got_object_id *new_id;
5734 err = write_subtree(&new_id,
5736 path_base_tree, commitable_paths,
5737 status_cb, status_arg, repo);
5740 if (nsubentries == 0) {
5741 /* All entries were deleted. */
5745 memcpy(&new_te->id, new_id,
5746 sizeof(new_te->id));
5749 err = insert_tree_entry(new_te, &paths);
5756 err = match_deleted_or_modified_ct(&ct, te,
5757 path_base_tree, commitable_paths);
5761 /* NB: Deleted entries get dropped here. */
5762 if (ct->status == GOT_STATUS_MODIFY ||
5763 ct->status == GOT_STATUS_MODE_CHANGE ||
5764 ct->staged_status == GOT_STATUS_MODIFY) {
5765 err = alloc_modified_blob_tree_entry(
5769 err = insert_tree_entry(new_te, &paths);
5774 err = report_ct_status(ct, status_cb,
5779 /* Entry is unchanged; just copy it. */
5780 err = got_object_tree_entry_dup(&new_te, te);
5783 err = insert_tree_entry(new_te, &paths);
5791 /* Write new list of entries; deleted entries have been dropped. */
5792 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5794 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5798 static const struct got_error *
5799 update_fileindex_after_commit(struct got_worktree *worktree,
5800 struct got_pathlist_head *commitable_paths,
5801 struct got_object_id *new_base_commit_id,
5802 struct got_fileindex *fileindex, int have_staged_files)
5804 const struct got_error *err = NULL;
5805 struct got_pathlist_entry *pe;
5806 char *relpath = NULL;
5808 TAILQ_FOREACH(pe, commitable_paths, entry) {
5809 struct got_fileindex_entry *ie;
5810 struct got_commitable *ct = pe->data;
5812 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5814 err = got_path_skip_common_ancestor(&relpath,
5815 worktree->root_path, ct->ondisk_path);
5820 if (ct->status == GOT_STATUS_DELETE ||
5821 ct->staged_status == GOT_STATUS_DELETE) {
5822 got_fileindex_entry_remove(fileindex, ie);
5823 } else if (ct->staged_status == GOT_STATUS_ADD ||
5824 ct->staged_status == GOT_STATUS_MODIFY) {
5825 got_fileindex_entry_stage_set(ie,
5826 GOT_FILEIDX_STAGE_NONE);
5827 got_fileindex_entry_staged_filetype_set(ie, 0);
5829 err = got_fileindex_entry_update(ie,
5830 worktree->root_fd, relpath,
5831 ct->staged_blob_id->hash,
5832 new_base_commit_id->hash,
5833 !have_staged_files);
5835 err = got_fileindex_entry_update(ie,
5836 worktree->root_fd, relpath,
5838 new_base_commit_id->hash,
5839 !have_staged_files);
5841 err = got_fileindex_entry_alloc(&ie, pe->path);
5844 err = got_fileindex_entry_update(ie,
5845 worktree->root_fd, relpath, ct->blob_id->hash,
5846 new_base_commit_id->hash, 1);
5848 got_fileindex_entry_free(ie);
5851 err = got_fileindex_entry_add(fileindex, ie);
5853 got_fileindex_entry_free(ie);
5866 static const struct got_error *
5867 check_out_of_date(const char *in_repo_path, unsigned char status,
5868 unsigned char staged_status, struct got_object_id *base_blob_id,
5869 struct got_object_id *base_commit_id,
5870 struct got_object_id *head_commit_id, struct got_repository *repo,
5873 const struct got_error *err = NULL;
5874 struct got_commit_object *commit = NULL;
5875 struct got_object_id *id = NULL;
5877 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5878 /* Trivial case: base commit == head commit */
5879 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5882 * Ensure file content which local changes were based
5883 * on matches file content in the branch head.
5885 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5888 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5890 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5891 err = got_error(ood_errcode);
5893 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5894 err = got_error(ood_errcode);
5896 /* Require that added files don't exist in the branch head. */
5897 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5900 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5901 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5903 err = id ? got_error(ood_errcode) : NULL;
5908 got_object_commit_close(commit);
5912 static const struct got_error *
5913 commit_worktree(struct got_object_id **new_commit_id,
5914 struct got_pathlist_head *commitable_paths,
5915 struct got_object_id *head_commit_id,
5916 struct got_object_id *parent_id2,
5917 struct got_worktree *worktree,
5918 const char *author, const char *committer, char *diff_path,
5919 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5920 got_worktree_status_cb status_cb, void *status_arg,
5921 struct got_repository *repo)
5923 const struct got_error *err = NULL, *unlockerr = NULL;
5924 struct got_pathlist_entry *pe;
5925 const char *head_ref_name = NULL;
5926 struct got_commit_object *head_commit = NULL;
5927 struct got_reference *head_ref2 = NULL;
5928 struct got_object_id *head_commit_id2 = NULL;
5929 struct got_tree_object *head_tree = NULL;
5930 struct got_object_id *new_tree_id = NULL;
5931 int nentries, nparents = 0;
5932 struct got_object_id_queue parent_ids;
5933 struct got_object_qid *pid = NULL;
5934 char *logmsg = NULL;
5937 *new_commit_id = NULL;
5939 STAILQ_INIT(&parent_ids);
5941 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5945 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5949 if (commit_msg_cb != NULL) {
5950 err = commit_msg_cb(commitable_paths, diff_path,
5951 &logmsg, commit_arg);
5956 if (logmsg == NULL || strlen(logmsg) == 0) {
5957 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5961 /* Create blobs from added and modified files and record their IDs. */
5962 TAILQ_FOREACH(pe, commitable_paths, entry) {
5963 struct got_commitable *ct = pe->data;
5966 /* Blobs for staged files already exist. */
5967 if (ct->staged_status == GOT_STATUS_ADD ||
5968 ct->staged_status == GOT_STATUS_MODIFY)
5971 if (ct->status != GOT_STATUS_ADD &&
5972 ct->status != GOT_STATUS_MODIFY &&
5973 ct->status != GOT_STATUS_MODE_CHANGE)
5976 if (asprintf(&ondisk_path, "%s/%s",
5977 worktree->root_path, pe->path) == -1) {
5978 err = got_error_from_errno("asprintf");
5981 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5987 /* Recursively write new tree objects. */
5988 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5989 commitable_paths, status_cb, status_arg, repo);
5993 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5996 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5999 err = got_object_qid_alloc(&pid, parent_id2);
6002 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6005 timestamp = time(NULL);
6006 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6007 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6013 /* Check if a concurrent commit to our branch has occurred. */
6014 head_ref_name = got_worktree_get_head_ref_name(worktree);
6015 if (head_ref_name == NULL) {
6016 err = got_error_from_errno("got_worktree_get_head_ref_name");
6019 /* Lock the reference here to prevent concurrent modification. */
6020 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6023 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6026 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6027 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6030 /* Update branch head in repository. */
6031 err = got_ref_change_ref(head_ref2, *new_commit_id);
6034 err = got_ref_write(head_ref2, repo);
6038 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6042 err = ref_base_commit(worktree, repo);
6046 got_object_id_queue_free(&parent_ids);
6048 got_object_tree_close(head_tree);
6050 got_object_commit_close(head_commit);
6051 free(head_commit_id2);
6053 unlockerr = got_ref_unlock(head_ref2);
6054 if (unlockerr && err == NULL)
6056 got_ref_close(head_ref2);
6061 static const struct got_error *
6062 check_path_is_commitable(const char *path,
6063 struct got_pathlist_head *commitable_paths)
6065 struct got_pathlist_entry *cpe = NULL;
6066 size_t path_len = strlen(path);
6068 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6069 struct got_commitable *ct = cpe->data;
6070 const char *ct_path = ct->path;
6072 while (ct_path[0] == '/')
6075 if (strcmp(path, ct_path) == 0 ||
6076 got_path_is_child(ct_path, path, path_len))
6081 return got_error_path(path, GOT_ERR_BAD_PATH);
6086 static const struct got_error *
6087 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6089 int *have_staged_files = arg;
6091 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6092 *have_staged_files = 1;
6093 return got_error(GOT_ERR_CANCELLED);
6099 static const struct got_error *
6100 check_non_staged_files(struct got_fileindex *fileindex,
6101 struct got_pathlist_head *paths)
6103 struct got_pathlist_entry *pe;
6104 struct got_fileindex_entry *ie;
6106 TAILQ_FOREACH(pe, paths, entry) {
6107 if (pe->path[0] == '\0')
6109 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6111 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6112 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6113 return got_error_path(pe->path,
6114 GOT_ERR_FILE_NOT_STAGED);
6120 const struct got_error *
6121 got_worktree_commit(struct got_object_id **new_commit_id,
6122 struct got_worktree *worktree, struct got_pathlist_head *paths,
6123 const char *author, const char *committer, int allow_bad_symlinks,
6124 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6125 got_worktree_status_cb status_cb, void *status_arg,
6126 struct got_repository *repo)
6128 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6129 struct got_fileindex *fileindex = NULL;
6130 char *fileindex_path = NULL;
6131 struct got_pathlist_head commitable_paths;
6132 struct collect_commitables_arg cc_arg;
6133 struct got_pathlist_entry *pe;
6134 struct got_reference *head_ref = NULL;
6135 struct got_object_id *head_commit_id = NULL;
6136 char *diff_path = NULL;
6137 int have_staged_files = 0;
6139 *new_commit_id = NULL;
6141 memset(&cc_arg, 0, sizeof(cc_arg));
6142 TAILQ_INIT(&commitable_paths);
6144 err = lock_worktree(worktree, LOCK_EX);
6148 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6152 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6156 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6160 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6161 &have_staged_files);
6162 if (err && err->code != GOT_ERR_CANCELLED)
6164 if (have_staged_files) {
6165 err = check_non_staged_files(fileindex, paths);
6170 cc_arg.commitable_paths = &commitable_paths;
6171 cc_arg.worktree = worktree;
6172 cc_arg.fileindex = fileindex;
6174 cc_arg.have_staged_files = have_staged_files;
6175 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6176 cc_arg.diff_header_shown = 0;
6178 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6179 GOT_TMPDIR_STR "/got", ".diff");
6182 cc_arg.f1 = got_opentemp();
6183 if (cc_arg.f1 == NULL) {
6184 err = got_error_from_errno("got_opentemp");
6187 cc_arg.f2 = got_opentemp();
6188 if (cc_arg.f2 == NULL) {
6189 err = got_error_from_errno("got_opentemp");
6194 TAILQ_FOREACH(pe, paths, entry) {
6195 err = worktree_status(worktree, pe->path, fileindex, repo,
6196 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6202 if (fflush(cc_arg.diff_outfile) == EOF) {
6203 err = got_error_from_errno("fflush");
6208 if (TAILQ_EMPTY(&commitable_paths)) {
6209 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6213 TAILQ_FOREACH(pe, paths, entry) {
6214 err = check_path_is_commitable(pe->path, &commitable_paths);
6219 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6220 struct got_commitable *ct = pe->data;
6221 const char *ct_path = ct->in_repo_path;
6223 while (ct_path[0] == '/')
6225 err = check_out_of_date(ct_path, ct->status,
6226 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6227 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6233 err = commit_worktree(new_commit_id, &commitable_paths,
6234 head_commit_id, NULL, worktree, author, committer,
6235 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6236 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6240 err = update_fileindex_after_commit(worktree, &commitable_paths,
6241 *new_commit_id, fileindex, have_staged_files);
6242 sync_err = sync_fileindex(fileindex, fileindex_path);
6243 if (sync_err && err == NULL)
6247 got_fileindex_free(fileindex);
6248 free(fileindex_path);
6249 unlockerr = lock_worktree(worktree, LOCK_SH);
6250 if (unlockerr && err == NULL)
6252 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6253 struct got_commitable *ct = pe->data;
6255 free_commitable(ct);
6257 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6258 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6259 err = got_error_from_errno2("unlink", diff_path);
6261 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6263 err = got_error_from_errno("fclose");
6268 got_commitable_get_path(struct got_commitable *ct)
6274 got_commitable_get_status(struct got_commitable *ct)
6279 struct check_rebase_ok_arg {
6280 struct got_worktree *worktree;
6281 struct got_repository *repo;
6284 static const struct got_error *
6285 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6287 const struct got_error *err = NULL;
6288 struct check_rebase_ok_arg *a = arg;
6289 unsigned char status;
6293 /* Reject rebase of a work tree with mixed base commits. */
6294 if (memcmp(ie->commit_hash, a->worktree->base_commit_id->hash,
6295 SHA1_DIGEST_LENGTH))
6296 return got_error(GOT_ERR_MIXED_COMMITS);
6298 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6300 return got_error_from_errno("asprintf");
6302 /* Reject rebase of a work tree with modified or staged files. */
6303 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6308 if (status != GOT_STATUS_NO_CHANGE)
6309 return got_error(GOT_ERR_MODIFIED);
6310 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6311 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6316 const struct got_error *
6317 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6318 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6319 struct got_worktree *worktree, struct got_reference *branch,
6320 struct got_repository *repo)
6322 const struct got_error *err = NULL;
6323 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6324 char *branch_ref_name = NULL;
6325 char *fileindex_path = NULL;
6326 struct check_rebase_ok_arg ok_arg;
6327 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6328 struct got_object_id *wt_branch_tip = NULL;
6330 *new_base_branch_ref = NULL;
6334 err = lock_worktree(worktree, LOCK_EX);
6338 err = open_fileindex(fileindex, &fileindex_path, worktree);
6342 ok_arg.worktree = worktree;
6344 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6349 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6353 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6357 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6361 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6366 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6369 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6370 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6374 err = got_ref_alloc_symref(new_base_branch_ref,
6375 new_base_branch_ref_name, wt_branch);
6378 err = got_ref_write(*new_base_branch_ref, repo);
6382 /* TODO Lock original branch's ref while rebasing? */
6384 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6388 err = got_ref_write(branch_ref, repo);
6392 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6393 worktree->base_commit_id);
6396 err = got_ref_write(*tmp_branch, repo);
6400 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6404 free(fileindex_path);
6405 free(tmp_branch_name);
6406 free(new_base_branch_ref_name);
6407 free(branch_ref_name);
6409 got_ref_close(branch_ref);
6411 got_ref_close(wt_branch);
6412 free(wt_branch_tip);
6414 if (*new_base_branch_ref) {
6415 got_ref_close(*new_base_branch_ref);
6416 *new_base_branch_ref = NULL;
6419 got_ref_close(*tmp_branch);
6423 got_fileindex_free(*fileindex);
6426 lock_worktree(worktree, LOCK_SH);
6431 const struct got_error *
6432 got_worktree_rebase_continue(struct got_object_id **commit_id,
6433 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6434 struct got_reference **branch, struct got_fileindex **fileindex,
6435 struct got_worktree *worktree, struct got_repository *repo)
6437 const struct got_error *err;
6438 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6439 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6440 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6441 char *fileindex_path = NULL;
6442 int have_staged_files = 0;
6445 *new_base_branch = NULL;
6450 err = lock_worktree(worktree, LOCK_EX);
6454 err = open_fileindex(fileindex, &fileindex_path, worktree);
6458 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6459 &have_staged_files);
6460 if (err && err->code != GOT_ERR_CANCELLED)
6462 if (have_staged_files) {
6463 err = got_error(GOT_ERR_STAGED_PATHS);
6467 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6471 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6475 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6479 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6483 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6487 err = got_ref_open(branch, repo,
6488 got_ref_get_symref_target(branch_ref), 0);
6492 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6496 err = got_ref_resolve(commit_id, repo, commit_ref);
6500 err = got_ref_open(new_base_branch, repo,
6501 new_base_branch_ref_name, 0);
6505 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6509 free(commit_ref_name);
6510 free(branch_ref_name);
6511 free(fileindex_path);
6513 got_ref_close(commit_ref);
6515 got_ref_close(branch_ref);
6520 got_ref_close(*tmp_branch);
6523 if (*new_base_branch) {
6524 got_ref_close(*new_base_branch);
6525 *new_base_branch = NULL;
6528 got_ref_close(*branch);
6532 got_fileindex_free(*fileindex);
6535 lock_worktree(worktree, LOCK_SH);
6540 const struct got_error *
6541 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6543 const struct got_error *err;
6544 char *tmp_branch_name = NULL;
6546 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6550 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6551 free(tmp_branch_name);
6555 static const struct got_error *
6556 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6557 const char *diff_path, char **logmsg, void *arg)
6563 static const struct got_error *
6564 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6565 const char *path, struct got_object_id *blob_id,
6566 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6567 int dirfd, const char *de_name)
6572 struct collect_merged_paths_arg {
6573 got_worktree_checkout_cb progress_cb;
6575 struct got_pathlist_head *merged_paths;
6578 static const struct got_error *
6579 collect_merged_paths(void *arg, unsigned char status, const char *path)
6581 const struct got_error *err;
6582 struct collect_merged_paths_arg *a = arg;
6584 struct got_pathlist_entry *new;
6586 err = (*a->progress_cb)(a->progress_arg, status, path);
6590 if (status != GOT_STATUS_MERGE &&
6591 status != GOT_STATUS_ADD &&
6592 status != GOT_STATUS_DELETE &&
6593 status != GOT_STATUS_CONFLICT)
6598 return got_error_from_errno("strdup");
6600 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6601 if (err || new == NULL)
6606 static const struct got_error *
6607 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6608 int is_rebase, struct got_repository *repo)
6610 const struct got_error *err;
6611 struct got_reference *commit_ref = NULL;
6613 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6615 if (err->code != GOT_ERR_NOT_REF)
6617 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6620 err = got_ref_write(commit_ref, repo);
6623 } else if (is_rebase) {
6624 struct got_object_id *stored_id;
6627 err = got_ref_resolve(&stored_id, repo, commit_ref);
6630 cmp = got_object_id_cmp(commit_id, stored_id);
6633 err = got_error(GOT_ERR_REBASE_COMMITID);
6639 got_ref_close(commit_ref);
6643 static const struct got_error *
6644 rebase_merge_files(struct got_pathlist_head *merged_paths,
6645 const char *commit_ref_name, struct got_worktree *worktree,
6646 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6647 struct got_object_id *commit_id, struct got_repository *repo,
6648 got_worktree_checkout_cb progress_cb, void *progress_arg,
6649 got_cancel_cb cancel_cb, void *cancel_arg)
6651 const struct got_error *err;
6652 struct got_reference *commit_ref = NULL;
6653 struct collect_merged_paths_arg cmp_arg;
6654 char *fileindex_path;
6656 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6658 err = get_fileindex_path(&fileindex_path, worktree);
6662 cmp_arg.progress_cb = progress_cb;
6663 cmp_arg.progress_arg = progress_arg;
6664 cmp_arg.merged_paths = merged_paths;
6665 err = merge_files(worktree, fileindex, fileindex_path,
6666 parent_commit_id, commit_id, repo, collect_merged_paths,
6667 &cmp_arg, cancel_cb, cancel_arg);
6669 got_ref_close(commit_ref);
6673 const struct got_error *
6674 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6675 struct got_worktree *worktree, struct got_fileindex *fileindex,
6676 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6677 struct got_repository *repo,
6678 got_worktree_checkout_cb progress_cb, void *progress_arg,
6679 got_cancel_cb cancel_cb, void *cancel_arg)
6681 const struct got_error *err;
6682 char *commit_ref_name;
6684 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6688 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6692 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6693 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6694 progress_arg, cancel_cb, cancel_arg);
6696 free(commit_ref_name);
6700 const struct got_error *
6701 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6702 struct got_worktree *worktree, struct got_fileindex *fileindex,
6703 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6704 struct got_repository *repo,
6705 got_worktree_checkout_cb progress_cb, void *progress_arg,
6706 got_cancel_cb cancel_cb, void *cancel_arg)
6708 const struct got_error *err;
6709 char *commit_ref_name;
6711 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6715 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6719 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6720 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6721 progress_arg, cancel_cb, cancel_arg);
6723 free(commit_ref_name);
6727 static const struct got_error *
6728 rebase_commit(struct got_object_id **new_commit_id,
6729 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6730 struct got_worktree *worktree, struct got_fileindex *fileindex,
6731 struct got_reference *tmp_branch, const char *committer,
6732 struct got_commit_object *orig_commit, const char *new_logmsg,
6733 struct got_repository *repo)
6735 const struct got_error *err, *sync_err;
6736 struct got_pathlist_head commitable_paths;
6737 struct collect_commitables_arg cc_arg;
6738 char *fileindex_path = NULL;
6739 struct got_reference *head_ref = NULL;
6740 struct got_object_id *head_commit_id = NULL;
6741 char *logmsg = NULL;
6743 memset(&cc_arg, 0, sizeof(cc_arg));
6744 TAILQ_INIT(&commitable_paths);
6745 *new_commit_id = NULL;
6747 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6749 err = get_fileindex_path(&fileindex_path, worktree);
6753 cc_arg.commitable_paths = &commitable_paths;
6754 cc_arg.worktree = worktree;
6756 cc_arg.have_staged_files = 0;
6758 * If possible get the status of individual files directly to
6759 * avoid crawling the entire work tree once per rebased commit.
6761 * Ideally, merged_paths would contain a list of commitables
6762 * we could use so we could skip worktree_status() entirely.
6763 * However, we would then need carefully keep track of cumulative
6764 * effects of operations such as file additions and deletions
6765 * in 'got histedit -f' (folding multiple commits into one),
6766 * and this extra complexity is not really worth it.
6769 struct got_pathlist_entry *pe;
6770 TAILQ_FOREACH(pe, merged_paths, entry) {
6771 err = worktree_status(worktree, pe->path, fileindex,
6772 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6778 err = worktree_status(worktree, "", fileindex, repo,
6779 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6784 if (TAILQ_EMPTY(&commitable_paths)) {
6785 /* No-op change; commit will be elided. */
6786 err = got_ref_delete(commit_ref, repo);
6789 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6793 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6797 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6802 logmsg = strdup(new_logmsg);
6803 if (logmsg == NULL) {
6804 err = got_error_from_errno("strdup");
6808 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6813 /* NB: commit_worktree will call free(logmsg) */
6814 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6815 NULL, worktree, got_object_commit_get_author(orig_commit),
6816 committer ? committer :
6817 got_object_commit_get_committer(orig_commit), NULL,
6818 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6822 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6826 err = got_ref_delete(commit_ref, repo);
6830 err = update_fileindex_after_commit(worktree, &commitable_paths,
6831 *new_commit_id, fileindex, 0);
6832 sync_err = sync_fileindex(fileindex, fileindex_path);
6833 if (sync_err && err == NULL)
6836 free(fileindex_path);
6837 free(head_commit_id);
6839 got_ref_close(head_ref);
6841 free(*new_commit_id);
6842 *new_commit_id = NULL;
6847 const struct got_error *
6848 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6849 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6850 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6851 const char *committer, struct got_commit_object *orig_commit,
6852 struct got_object_id *orig_commit_id, struct got_repository *repo)
6854 const struct got_error *err;
6855 char *commit_ref_name;
6856 struct got_reference *commit_ref = NULL;
6857 struct got_object_id *commit_id = NULL;
6859 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6863 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6866 err = got_ref_resolve(&commit_id, repo, commit_ref);
6869 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6870 err = got_error(GOT_ERR_REBASE_COMMITID);
6874 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6875 worktree, fileindex, tmp_branch, committer, orig_commit,
6879 got_ref_close(commit_ref);
6880 free(commit_ref_name);
6885 const struct got_error *
6886 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6887 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6888 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6889 const char *committer, struct got_commit_object *orig_commit,
6890 struct got_object_id *orig_commit_id, const char *new_logmsg,
6891 struct got_repository *repo)
6893 const struct got_error *err;
6894 char *commit_ref_name;
6895 struct got_reference *commit_ref = NULL;
6897 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6901 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6905 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6906 worktree, fileindex, tmp_branch, committer, orig_commit,
6910 got_ref_close(commit_ref);
6911 free(commit_ref_name);
6915 const struct got_error *
6916 got_worktree_rebase_postpone(struct got_worktree *worktree,
6917 struct got_fileindex *fileindex)
6920 got_fileindex_free(fileindex);
6921 return lock_worktree(worktree, LOCK_SH);
6924 static const struct got_error *
6925 delete_ref(const char *name, struct got_repository *repo)
6927 const struct got_error *err;
6928 struct got_reference *ref;
6930 err = got_ref_open(&ref, repo, name, 0);
6932 if (err->code == GOT_ERR_NOT_REF)
6937 err = got_ref_delete(ref, repo);
6942 static const struct got_error *
6943 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6945 const struct got_error *err;
6946 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6947 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6949 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6952 err = delete_ref(tmp_branch_name, repo);
6956 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6959 err = delete_ref(new_base_branch_ref_name, repo);
6963 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6966 err = delete_ref(branch_ref_name, repo);
6970 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6973 err = delete_ref(commit_ref_name, repo);
6978 free(tmp_branch_name);
6979 free(new_base_branch_ref_name);
6980 free(branch_ref_name);
6981 free(commit_ref_name);
6985 static const struct got_error *
6986 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6987 struct got_object_id *new_commit_id, struct got_repository *repo)
6989 const struct got_error *err;
6990 struct got_reference *ref = NULL;
6991 struct got_object_id *old_commit_id = NULL;
6992 const char *branch_name = NULL;
6993 char *new_id_str = NULL;
6994 char *refname = NULL;
6996 branch_name = got_ref_get_name(branch);
6997 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6998 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7001 err = got_object_id_str(&new_id_str, new_commit_id);
7005 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7006 new_id_str) == -1) {
7007 err = got_error_from_errno("asprintf");
7011 err = got_ref_resolve(&old_commit_id, repo, branch);
7015 err = got_ref_alloc(&ref, refname, old_commit_id);
7019 err = got_ref_write(ref, repo);
7023 free(old_commit_id);
7029 const struct got_error *
7030 got_worktree_rebase_complete(struct got_worktree *worktree,
7031 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7032 struct got_reference *rebased_branch, struct got_repository *repo,
7035 const struct got_error *err, *unlockerr, *sync_err;
7036 struct got_object_id *new_head_commit_id = NULL;
7037 char *fileindex_path = NULL;
7039 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7043 if (create_backup) {
7044 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7045 rebased_branch, new_head_commit_id, repo);
7050 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7054 err = got_ref_write(rebased_branch, repo);
7058 err = got_worktree_set_head_ref(worktree, rebased_branch);
7062 err = delete_rebase_refs(worktree, repo);
7066 err = get_fileindex_path(&fileindex_path, worktree);
7069 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7070 sync_err = sync_fileindex(fileindex, fileindex_path);
7071 if (sync_err && err == NULL)
7074 got_fileindex_free(fileindex);
7075 free(fileindex_path);
7076 free(new_head_commit_id);
7077 unlockerr = lock_worktree(worktree, LOCK_SH);
7078 if (unlockerr && err == NULL)
7083 const struct got_error *
7084 got_worktree_rebase_abort(struct got_worktree *worktree,
7085 struct got_fileindex *fileindex, struct got_repository *repo,
7086 struct got_reference *new_base_branch,
7087 got_worktree_checkout_cb progress_cb, void *progress_arg)
7089 const struct got_error *err, *unlockerr, *sync_err;
7090 struct got_reference *resolved = NULL;
7091 struct got_object_id *commit_id = NULL;
7092 struct got_commit_object *commit = NULL;
7093 char *fileindex_path = NULL;
7094 struct revert_file_args rfa;
7095 struct got_object_id *tree_id = NULL;
7097 err = lock_worktree(worktree, LOCK_EX);
7101 err = got_object_open_as_commit(&commit, repo,
7102 worktree->base_commit_id);
7106 err = got_ref_open(&resolved, repo,
7107 got_ref_get_symref_target(new_base_branch), 0);
7111 err = got_worktree_set_head_ref(worktree, resolved);
7116 * XXX commits to the base branch could have happened while
7117 * we were busy rebasing; should we store the original commit ID
7118 * when rebase begins and read it back here?
7120 err = got_ref_resolve(&commit_id, repo, resolved);
7124 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7128 err = got_object_id_by_path(&tree_id, repo, commit,
7129 worktree->path_prefix);
7133 err = delete_rebase_refs(worktree, repo);
7137 err = get_fileindex_path(&fileindex_path, worktree);
7141 rfa.worktree = worktree;
7142 rfa.fileindex = fileindex;
7143 rfa.progress_cb = progress_cb;
7144 rfa.progress_arg = progress_arg;
7145 rfa.patch_cb = NULL;
7146 rfa.patch_arg = NULL;
7148 rfa.unlink_added_files = 0;
7149 err = worktree_status(worktree, "", fileindex, repo,
7150 revert_file, &rfa, NULL, NULL, 1, 0);
7154 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7155 repo, progress_cb, progress_arg, NULL, NULL);
7157 sync_err = sync_fileindex(fileindex, fileindex_path);
7158 if (sync_err && err == NULL)
7161 got_ref_close(resolved);
7165 got_object_commit_close(commit);
7167 got_fileindex_free(fileindex);
7168 free(fileindex_path);
7170 unlockerr = lock_worktree(worktree, LOCK_SH);
7171 if (unlockerr && err == NULL)
7176 const struct got_error *
7177 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7178 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7179 struct got_fileindex **fileindex, struct got_worktree *worktree,
7180 struct got_repository *repo)
7182 const struct got_error *err = NULL;
7183 char *tmp_branch_name = NULL;
7184 char *branch_ref_name = NULL;
7185 char *base_commit_ref_name = NULL;
7186 char *fileindex_path = NULL;
7187 struct check_rebase_ok_arg ok_arg;
7188 struct got_reference *wt_branch = NULL;
7189 struct got_reference *base_commit_ref = NULL;
7193 *base_commit_id = NULL;
7196 err = lock_worktree(worktree, LOCK_EX);
7200 err = open_fileindex(fileindex, &fileindex_path, worktree);
7204 ok_arg.worktree = worktree;
7206 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7211 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7215 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7219 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7224 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7229 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7233 err = got_ref_write(*branch_ref, repo);
7237 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7238 worktree->base_commit_id);
7241 err = got_ref_write(base_commit_ref, repo);
7244 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7245 if (*base_commit_id == NULL) {
7246 err = got_error_from_errno("got_object_id_dup");
7250 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7251 worktree->base_commit_id);
7254 err = got_ref_write(*tmp_branch, repo);
7258 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7262 free(fileindex_path);
7263 free(tmp_branch_name);
7264 free(branch_ref_name);
7265 free(base_commit_ref_name);
7267 got_ref_close(wt_branch);
7270 got_ref_close(*branch_ref);
7274 got_ref_close(*tmp_branch);
7277 free(*base_commit_id);
7279 got_fileindex_free(*fileindex);
7282 lock_worktree(worktree, LOCK_SH);
7287 const struct got_error *
7288 got_worktree_histedit_postpone(struct got_worktree *worktree,
7289 struct got_fileindex *fileindex)
7292 got_fileindex_free(fileindex);
7293 return lock_worktree(worktree, LOCK_SH);
7296 const struct got_error *
7297 got_worktree_histedit_in_progress(int *in_progress,
7298 struct got_worktree *worktree)
7300 const struct got_error *err;
7301 char *tmp_branch_name = NULL;
7303 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7307 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7308 free(tmp_branch_name);
7312 const struct got_error *
7313 got_worktree_histedit_continue(struct got_object_id **commit_id,
7314 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7315 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7316 struct got_worktree *worktree, struct got_repository *repo)
7318 const struct got_error *err;
7319 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7320 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7321 struct got_reference *commit_ref = NULL;
7322 struct got_reference *base_commit_ref = NULL;
7323 char *fileindex_path = NULL;
7324 int have_staged_files = 0;
7328 *base_commit_id = NULL;
7331 err = lock_worktree(worktree, LOCK_EX);
7335 err = open_fileindex(fileindex, &fileindex_path, worktree);
7339 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7340 &have_staged_files);
7341 if (err && err->code != GOT_ERR_CANCELLED)
7343 if (have_staged_files) {
7344 err = got_error(GOT_ERR_STAGED_PATHS);
7348 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7352 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7356 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7360 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7365 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7369 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7372 err = got_ref_resolve(commit_id, repo, commit_ref);
7376 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7379 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7383 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7387 free(commit_ref_name);
7388 free(branch_ref_name);
7389 free(fileindex_path);
7391 got_ref_close(commit_ref);
7392 if (base_commit_ref)
7393 got_ref_close(base_commit_ref);
7397 free(*base_commit_id);
7398 *base_commit_id = NULL;
7400 got_ref_close(*tmp_branch);
7404 got_fileindex_free(*fileindex);
7407 lock_worktree(worktree, LOCK_EX);
7412 static const struct got_error *
7413 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7415 const struct got_error *err;
7416 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7417 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7419 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7422 err = delete_ref(tmp_branch_name, repo);
7426 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7430 err = delete_ref(base_commit_ref_name, repo);
7434 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7437 err = delete_ref(branch_ref_name, repo);
7441 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7444 err = delete_ref(commit_ref_name, repo);
7448 free(tmp_branch_name);
7449 free(base_commit_ref_name);
7450 free(branch_ref_name);
7451 free(commit_ref_name);
7455 const struct got_error *
7456 got_worktree_histedit_abort(struct got_worktree *worktree,
7457 struct got_fileindex *fileindex, struct got_repository *repo,
7458 struct got_reference *branch, struct got_object_id *base_commit_id,
7459 got_worktree_checkout_cb progress_cb, void *progress_arg)
7461 const struct got_error *err, *unlockerr, *sync_err;
7462 struct got_reference *resolved = NULL;
7463 char *fileindex_path = NULL;
7464 struct got_commit_object *commit = NULL;
7465 struct got_object_id *tree_id = NULL;
7466 struct revert_file_args rfa;
7468 err = lock_worktree(worktree, LOCK_EX);
7472 err = got_object_open_as_commit(&commit, repo,
7473 worktree->base_commit_id);
7477 err = got_ref_open(&resolved, repo,
7478 got_ref_get_symref_target(branch), 0);
7482 err = got_worktree_set_head_ref(worktree, resolved);
7486 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7490 err = got_object_id_by_path(&tree_id, repo, commit,
7491 worktree->path_prefix);
7495 err = delete_histedit_refs(worktree, repo);
7499 err = get_fileindex_path(&fileindex_path, worktree);
7503 rfa.worktree = worktree;
7504 rfa.fileindex = fileindex;
7505 rfa.progress_cb = progress_cb;
7506 rfa.progress_arg = progress_arg;
7507 rfa.patch_cb = NULL;
7508 rfa.patch_arg = NULL;
7510 rfa.unlink_added_files = 0;
7511 err = worktree_status(worktree, "", fileindex, repo,
7512 revert_file, &rfa, NULL, NULL, 1, 0);
7516 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7517 repo, progress_cb, progress_arg, NULL, NULL);
7519 sync_err = sync_fileindex(fileindex, fileindex_path);
7520 if (sync_err && err == NULL)
7523 got_ref_close(resolved);
7525 free(fileindex_path);
7527 unlockerr = lock_worktree(worktree, LOCK_SH);
7528 if (unlockerr && err == NULL)
7533 const struct got_error *
7534 got_worktree_histedit_complete(struct got_worktree *worktree,
7535 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7536 struct got_reference *edited_branch, struct got_repository *repo)
7538 const struct got_error *err, *unlockerr, *sync_err;
7539 struct got_object_id *new_head_commit_id = NULL;
7540 struct got_reference *resolved = NULL;
7541 char *fileindex_path = NULL;
7543 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7547 err = got_ref_open(&resolved, repo,
7548 got_ref_get_symref_target(edited_branch), 0);
7552 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7553 resolved, new_head_commit_id, repo);
7557 err = got_ref_change_ref(resolved, new_head_commit_id);
7561 err = got_ref_write(resolved, repo);
7565 err = got_worktree_set_head_ref(worktree, resolved);
7569 err = delete_histedit_refs(worktree, repo);
7573 err = get_fileindex_path(&fileindex_path, worktree);
7576 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7577 sync_err = sync_fileindex(fileindex, fileindex_path);
7578 if (sync_err && err == NULL)
7581 got_fileindex_free(fileindex);
7582 free(fileindex_path);
7583 free(new_head_commit_id);
7584 unlockerr = lock_worktree(worktree, LOCK_SH);
7585 if (unlockerr && err == NULL)
7590 const struct got_error *
7591 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7592 struct got_object_id *commit_id, struct got_repository *repo)
7594 const struct got_error *err;
7595 char *commit_ref_name;
7597 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7601 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7605 err = delete_ref(commit_ref_name, repo);
7607 free(commit_ref_name);
7611 const struct got_error *
7612 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7613 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7614 struct got_worktree *worktree, const char *refname,
7615 struct got_repository *repo)
7617 const struct got_error *err = NULL;
7618 char *fileindex_path = NULL;
7619 struct check_rebase_ok_arg ok_arg;
7623 *base_branch_ref = NULL;
7625 err = lock_worktree(worktree, LOCK_EX);
7629 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7630 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7631 "cannot integrate a branch into itself; "
7632 "update -b or different branch name required");
7636 err = open_fileindex(fileindex, &fileindex_path, worktree);
7640 /* Preconditions are the same as for rebase. */
7641 ok_arg.worktree = worktree;
7643 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7648 err = got_ref_open(branch_ref, repo, refname, 1);
7652 err = got_ref_open(base_branch_ref, repo,
7653 got_worktree_get_head_ref_name(worktree), 1);
7657 got_ref_close(*branch_ref);
7660 if (*base_branch_ref) {
7661 got_ref_close(*base_branch_ref);
7662 *base_branch_ref = NULL;
7665 got_fileindex_free(*fileindex);
7668 lock_worktree(worktree, LOCK_SH);
7673 const struct got_error *
7674 got_worktree_integrate_continue(struct got_worktree *worktree,
7675 struct got_fileindex *fileindex, struct got_repository *repo,
7676 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7677 got_worktree_checkout_cb progress_cb, void *progress_arg,
7678 got_cancel_cb cancel_cb, void *cancel_arg)
7680 const struct got_error *err = NULL, *sync_err, *unlockerr;
7681 char *fileindex_path = NULL;
7682 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7683 struct got_commit_object *commit = NULL;
7685 err = get_fileindex_path(&fileindex_path, worktree);
7689 err = got_ref_resolve(&commit_id, repo, branch_ref);
7693 err = got_object_open_as_commit(&commit, repo, commit_id);
7697 err = got_object_id_by_path(&tree_id, repo, commit,
7698 worktree->path_prefix);
7702 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7706 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7707 progress_cb, progress_arg, cancel_cb, cancel_arg);
7711 err = got_ref_change_ref(base_branch_ref, commit_id);
7715 err = got_ref_write(base_branch_ref, repo);
7719 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7721 sync_err = sync_fileindex(fileindex, fileindex_path);
7722 if (sync_err && err == NULL)
7726 unlockerr = got_ref_unlock(branch_ref);
7727 if (unlockerr && err == NULL)
7729 got_ref_close(branch_ref);
7731 unlockerr = got_ref_unlock(base_branch_ref);
7732 if (unlockerr && err == NULL)
7734 got_ref_close(base_branch_ref);
7736 got_fileindex_free(fileindex);
7737 free(fileindex_path);
7740 got_object_commit_close(commit);
7742 unlockerr = lock_worktree(worktree, LOCK_SH);
7743 if (unlockerr && err == NULL)
7748 const struct got_error *
7749 got_worktree_integrate_abort(struct got_worktree *worktree,
7750 struct got_fileindex *fileindex, struct got_repository *repo,
7751 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7753 const struct got_error *err = NULL, *unlockerr = NULL;
7755 got_fileindex_free(fileindex);
7757 err = lock_worktree(worktree, LOCK_SH);
7759 unlockerr = got_ref_unlock(branch_ref);
7760 if (unlockerr && err == NULL)
7762 got_ref_close(branch_ref);
7764 unlockerr = got_ref_unlock(base_branch_ref);
7765 if (unlockerr && err == NULL)
7767 got_ref_close(base_branch_ref);
7772 const struct got_error *
7773 got_worktree_merge_postpone(struct got_worktree *worktree,
7774 struct got_fileindex *fileindex)
7776 const struct got_error *err, *sync_err;
7777 char *fileindex_path = NULL;
7779 err = get_fileindex_path(&fileindex_path, worktree);
7783 sync_err = sync_fileindex(fileindex, fileindex_path);
7785 err = lock_worktree(worktree, LOCK_SH);
7786 if (sync_err && err == NULL)
7789 got_fileindex_free(fileindex);
7790 free(fileindex_path);
7794 static const struct got_error *
7795 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7797 const struct got_error *err;
7798 char *branch_refname = NULL, *commit_refname = NULL;
7800 err = get_merge_branch_ref_name(&branch_refname, worktree);
7803 err = delete_ref(branch_refname, repo);
7807 err = get_merge_commit_ref_name(&commit_refname, worktree);
7810 err = delete_ref(commit_refname, repo);
7815 free(branch_refname);
7816 free(commit_refname);
7820 struct merge_commit_msg_arg {
7821 struct got_worktree *worktree;
7822 const char *branch_name;
7825 static const struct got_error *
7826 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7827 const char *diff_path, char **logmsg, void *arg)
7829 struct merge_commit_msg_arg *a = arg;
7831 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7832 got_worktree_get_head_ref_name(a->worktree)) == -1)
7833 return got_error_from_errno("asprintf");
7839 const struct got_error *
7840 got_worktree_merge_branch(struct got_worktree *worktree,
7841 struct got_fileindex *fileindex,
7842 struct got_object_id *yca_commit_id,
7843 struct got_object_id *branch_tip,
7844 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7845 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7847 const struct got_error *err;
7848 char *fileindex_path = NULL;
7850 err = get_fileindex_path(&fileindex_path, worktree);
7854 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7859 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7860 branch_tip, repo, progress_cb, progress_arg,
7861 cancel_cb, cancel_arg);
7863 free(fileindex_path);
7867 const struct got_error *
7868 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7869 struct got_worktree *worktree, struct got_fileindex *fileindex,
7870 const char *author, const char *committer, int allow_bad_symlinks,
7871 struct got_object_id *branch_tip, const char *branch_name,
7872 struct got_repository *repo,
7873 got_worktree_status_cb status_cb, void *status_arg)
7876 const struct got_error *err = NULL, *sync_err;
7877 struct got_pathlist_head commitable_paths;
7878 struct collect_commitables_arg cc_arg;
7879 struct got_pathlist_entry *pe;
7880 struct got_reference *head_ref = NULL;
7881 struct got_object_id *head_commit_id = NULL;
7882 int have_staged_files = 0;
7883 struct merge_commit_msg_arg mcm_arg;
7884 char *fileindex_path = NULL;
7886 memset(&cc_arg, 0, sizeof(cc_arg));
7887 *new_commit_id = NULL;
7889 TAILQ_INIT(&commitable_paths);
7891 err = get_fileindex_path(&fileindex_path, worktree);
7895 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7899 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7903 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7904 &have_staged_files);
7905 if (err && err->code != GOT_ERR_CANCELLED)
7907 if (have_staged_files) {
7908 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7912 cc_arg.commitable_paths = &commitable_paths;
7913 cc_arg.worktree = worktree;
7914 cc_arg.fileindex = fileindex;
7916 cc_arg.have_staged_files = have_staged_files;
7917 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7918 err = worktree_status(worktree, "", fileindex, repo,
7919 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7923 if (TAILQ_EMPTY(&commitable_paths)) {
7924 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7925 "merge of %s cannot proceed", branch_name);
7929 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7930 struct got_commitable *ct = pe->data;
7931 const char *ct_path = ct->in_repo_path;
7933 while (ct_path[0] == '/')
7935 err = check_out_of_date(ct_path, ct->status,
7936 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7937 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7943 mcm_arg.worktree = worktree;
7944 mcm_arg.branch_name = branch_name;
7945 err = commit_worktree(new_commit_id, &commitable_paths,
7946 head_commit_id, branch_tip, worktree, author, committer, NULL,
7947 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7951 err = update_fileindex_after_commit(worktree, &commitable_paths,
7952 *new_commit_id, fileindex, have_staged_files);
7953 sync_err = sync_fileindex(fileindex, fileindex_path);
7954 if (sync_err && err == NULL)
7957 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7958 struct got_commitable *ct = pe->data;
7960 free_commitable(ct);
7962 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7963 free(fileindex_path);
7967 const struct got_error *
7968 got_worktree_merge_complete(struct got_worktree *worktree,
7969 struct got_fileindex *fileindex, struct got_repository *repo)
7971 const struct got_error *err, *unlockerr, *sync_err;
7972 char *fileindex_path = NULL;
7974 err = delete_merge_refs(worktree, repo);
7978 err = get_fileindex_path(&fileindex_path, worktree);
7981 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7982 sync_err = sync_fileindex(fileindex, fileindex_path);
7983 if (sync_err && err == NULL)
7986 got_fileindex_free(fileindex);
7987 free(fileindex_path);
7988 unlockerr = lock_worktree(worktree, LOCK_SH);
7989 if (unlockerr && err == NULL)
7994 const struct got_error *
7995 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7996 struct got_repository *repo)
7998 const struct got_error *err;
7999 char *branch_refname = NULL;
8000 struct got_reference *branch_ref = NULL;
8004 err = get_merge_branch_ref_name(&branch_refname, worktree);
8007 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8008 free(branch_refname);
8010 if (err->code != GOT_ERR_NOT_REF)
8018 const struct got_error *got_worktree_merge_prepare(
8019 struct got_fileindex **fileindex, struct got_worktree *worktree,
8020 struct got_reference *branch, struct got_repository *repo)
8022 const struct got_error *err = NULL;
8023 char *fileindex_path = NULL;
8024 char *branch_refname = NULL, *commit_refname = NULL;
8025 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8026 struct got_reference *commit_ref = NULL;
8027 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8028 struct check_rebase_ok_arg ok_arg;
8032 err = lock_worktree(worktree, LOCK_EX);
8036 err = open_fileindex(fileindex, &fileindex_path, worktree);
8040 /* Preconditions are the same as for rebase. */
8041 ok_arg.worktree = worktree;
8043 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8048 err = get_merge_branch_ref_name(&branch_refname, worktree);
8052 err = get_merge_commit_ref_name(&commit_refname, worktree);
8056 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8061 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8065 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8066 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8070 err = got_ref_resolve(&branch_tip, repo, branch);
8074 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8077 err = got_ref_write(branch_ref, repo);
8081 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8084 err = got_ref_write(commit_ref, repo);
8089 free(branch_refname);
8090 free(commit_refname);
8091 free(fileindex_path);
8093 got_ref_close(branch_ref);
8095 got_ref_close(commit_ref);
8097 got_ref_close(wt_branch);
8098 free(wt_branch_tip);
8101 got_fileindex_free(*fileindex);
8104 lock_worktree(worktree, LOCK_SH);
8109 const struct got_error *
8110 got_worktree_merge_continue(char **branch_name,
8111 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8112 struct got_worktree *worktree, struct got_repository *repo)
8114 const struct got_error *err;
8115 char *commit_refname = NULL, *branch_refname = NULL;
8116 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8117 char *fileindex_path = NULL;
8118 int have_staged_files = 0;
8120 *branch_name = NULL;
8124 err = lock_worktree(worktree, LOCK_EX);
8128 err = open_fileindex(fileindex, &fileindex_path, worktree);
8132 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8133 &have_staged_files);
8134 if (err && err->code != GOT_ERR_CANCELLED)
8136 if (have_staged_files) {
8137 err = got_error(GOT_ERR_STAGED_PATHS);
8141 err = get_merge_branch_ref_name(&branch_refname, worktree);
8145 err = get_merge_commit_ref_name(&commit_refname, worktree);
8149 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8153 if (!got_ref_is_symbolic(branch_ref)) {
8154 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8155 "%s is not a symbolic reference",
8156 got_ref_get_name(branch_ref));
8159 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8160 if (*branch_name == NULL) {
8161 err = got_error_from_errno("strdup");
8165 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8169 err = got_ref_resolve(branch_tip, repo, commit_ref);
8173 free(commit_refname);
8174 free(branch_refname);
8175 free(fileindex_path);
8177 got_ref_close(commit_ref);
8179 got_ref_close(branch_ref);
8183 *branch_name = NULL;
8188 got_fileindex_free(*fileindex);
8191 lock_worktree(worktree, LOCK_SH);
8196 const struct got_error *
8197 got_worktree_merge_abort(struct got_worktree *worktree,
8198 struct got_fileindex *fileindex, struct got_repository *repo,
8199 got_worktree_checkout_cb progress_cb, void *progress_arg)
8201 const struct got_error *err, *unlockerr, *sync_err;
8202 struct got_object_id *commit_id = NULL;
8203 struct got_commit_object *commit = NULL;
8204 char *fileindex_path = NULL;
8205 struct revert_file_args rfa;
8206 struct got_object_id *tree_id = NULL;
8208 err = got_object_open_as_commit(&commit, repo,
8209 worktree->base_commit_id);
8213 err = got_object_id_by_path(&tree_id, repo, commit,
8214 worktree->path_prefix);
8218 err = delete_merge_refs(worktree, repo);
8222 err = get_fileindex_path(&fileindex_path, worktree);
8226 rfa.worktree = worktree;
8227 rfa.fileindex = fileindex;
8228 rfa.progress_cb = progress_cb;
8229 rfa.progress_arg = progress_arg;
8230 rfa.patch_cb = NULL;
8231 rfa.patch_arg = NULL;
8233 rfa.unlink_added_files = 1;
8234 err = worktree_status(worktree, "", fileindex, repo,
8235 revert_file, &rfa, NULL, NULL, 1, 0);
8239 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8240 repo, progress_cb, progress_arg, NULL, NULL);
8242 sync_err = sync_fileindex(fileindex, fileindex_path);
8243 if (sync_err && err == NULL)
8249 got_object_commit_close(commit);
8251 got_fileindex_free(fileindex);
8252 free(fileindex_path);
8254 unlockerr = lock_worktree(worktree, LOCK_SH);
8255 if (unlockerr && err == NULL)
8260 struct check_stage_ok_arg {
8261 struct got_object_id *head_commit_id;
8262 struct got_worktree *worktree;
8263 struct got_fileindex *fileindex;
8264 struct got_repository *repo;
8268 static const struct got_error *
8269 check_stage_ok(void *arg, unsigned char status,
8270 unsigned char staged_status, const char *relpath,
8271 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8272 struct got_object_id *commit_id, int dirfd, const char *de_name)
8274 struct check_stage_ok_arg *a = arg;
8275 const struct got_error *err = NULL;
8276 struct got_fileindex_entry *ie;
8277 struct got_object_id base_commit_id;
8278 struct got_object_id *base_commit_idp = NULL;
8279 char *in_repo_path = NULL, *p;
8281 if (status == GOT_STATUS_UNVERSIONED ||
8282 status == GOT_STATUS_NO_CHANGE)
8284 if (status == GOT_STATUS_NONEXISTENT)
8285 return got_error_set_errno(ENOENT, relpath);
8287 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8289 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8291 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8292 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8294 return got_error_from_errno("asprintf");
8296 if (got_fileindex_entry_has_commit(ie)) {
8297 memcpy(base_commit_id.hash, ie->commit_hash,
8298 SHA1_DIGEST_LENGTH);
8299 base_commit_id.algo = got_repo_get_hash_algorithm(a->repo);
8300 base_commit_idp = &base_commit_id;
8303 if (status == GOT_STATUS_CONFLICT) {
8304 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8306 } else if (status != GOT_STATUS_ADD &&
8307 status != GOT_STATUS_MODIFY &&
8308 status != GOT_STATUS_DELETE) {
8309 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8313 a->have_changes = 1;
8318 err = check_out_of_date(p, status, staged_status,
8319 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8320 GOT_ERR_STAGE_OUT_OF_DATE);
8326 struct stage_path_arg {
8327 struct got_worktree *worktree;
8328 struct got_fileindex *fileindex;
8329 struct got_repository *repo;
8330 got_worktree_status_cb status_cb;
8332 got_worktree_patch_cb patch_cb;
8334 int staged_something;
8335 int allow_bad_symlinks;
8338 static const struct got_error *
8339 stage_path(void *arg, unsigned char status,
8340 unsigned char staged_status, const char *relpath,
8341 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8342 struct got_object_id *commit_id, int dirfd, const char *de_name)
8344 struct stage_path_arg *a = arg;
8345 const struct got_error *err = NULL;
8346 struct got_fileindex_entry *ie;
8347 char *ondisk_path = NULL, *path_content = NULL;
8349 struct got_object_id *new_staged_blob_id = NULL;
8352 if (status == GOT_STATUS_UNVERSIONED)
8355 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8357 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8359 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8361 return got_error_from_errno("asprintf");
8364 case GOT_STATUS_ADD:
8365 case GOT_STATUS_MODIFY:
8366 /* XXX could sb.st_mode be passed in by our caller? */
8367 if (lstat(ondisk_path, &sb) == -1) {
8368 err = got_error_from_errno2("lstat", ondisk_path);
8372 if (status == GOT_STATUS_ADD) {
8373 int choice = GOT_PATCH_CHOICE_NONE;
8374 err = (*a->patch_cb)(&choice, a->patch_arg,
8375 status, ie->path, NULL, 1, 1);
8378 if (choice != GOT_PATCH_CHOICE_YES)
8381 err = create_patched_content(&path_content, 0,
8382 staged_blob_id ? staged_blob_id : blob_id,
8383 ondisk_path, dirfd, de_name, ie->path,
8384 a->repo, a->patch_cb, a->patch_arg);
8385 if (err || path_content == NULL)
8389 err = got_object_blob_create(&new_staged_blob_id,
8390 path_content ? path_content : ondisk_path, a->repo);
8393 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8394 SHA1_DIGEST_LENGTH);
8395 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8396 stage = GOT_FILEIDX_STAGE_ADD;
8398 stage = GOT_FILEIDX_STAGE_MODIFY;
8399 got_fileindex_entry_stage_set(ie, stage);
8400 if (S_ISLNK(sb.st_mode)) {
8401 int is_bad_symlink = 0;
8402 if (!a->allow_bad_symlinks) {
8403 char target_path[PATH_MAX];
8405 target_len = readlink(ondisk_path, target_path,
8406 sizeof(target_path));
8407 if (target_len == -1) {
8408 err = got_error_from_errno2("readlink",
8412 err = is_bad_symlink_target(&is_bad_symlink,
8413 target_path, target_len, ondisk_path,
8414 a->worktree->root_path);
8417 if (is_bad_symlink) {
8418 err = got_error_path(ondisk_path,
8419 GOT_ERR_BAD_SYMLINK);
8424 got_fileindex_entry_staged_filetype_set(ie,
8425 GOT_FILEIDX_MODE_BAD_SYMLINK);
8427 got_fileindex_entry_staged_filetype_set(ie,
8428 GOT_FILEIDX_MODE_SYMLINK);
8430 got_fileindex_entry_staged_filetype_set(ie,
8431 GOT_FILEIDX_MODE_REGULAR_FILE);
8433 a->staged_something = 1;
8434 if (a->status_cb == NULL)
8436 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8437 get_staged_status(ie), relpath, blob_id,
8438 new_staged_blob_id, NULL, dirfd, de_name);
8442 * When staging the reverse of the staged diff,
8443 * implicitly unstage the file.
8445 if (memcmp(ie->staged_blob_hash, ie->blob_hash,
8446 sizeof(ie->blob_hash)) == 0) {
8447 got_fileindex_entry_stage_set(ie,
8448 GOT_FILEIDX_STAGE_NONE);
8451 case GOT_STATUS_DELETE:
8452 if (staged_status == GOT_STATUS_DELETE)
8455 int choice = GOT_PATCH_CHOICE_NONE;
8456 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8457 ie->path, NULL, 1, 1);
8460 if (choice == GOT_PATCH_CHOICE_NO)
8462 if (choice != GOT_PATCH_CHOICE_YES) {
8463 err = got_error(GOT_ERR_PATCH_CHOICE);
8467 stage = GOT_FILEIDX_STAGE_DELETE;
8468 got_fileindex_entry_stage_set(ie, stage);
8469 a->staged_something = 1;
8470 if (a->status_cb == NULL)
8472 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8473 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8476 case GOT_STATUS_NO_CHANGE:
8478 case GOT_STATUS_CONFLICT:
8479 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8481 case GOT_STATUS_NONEXISTENT:
8482 err = got_error_set_errno(ENOENT, relpath);
8485 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8489 if (path_content && unlink(path_content) == -1 && err == NULL)
8490 err = got_error_from_errno2("unlink", path_content);
8493 free(new_staged_blob_id);
8497 const struct got_error *
8498 got_worktree_stage(struct got_worktree *worktree,
8499 struct got_pathlist_head *paths,
8500 got_worktree_status_cb status_cb, void *status_arg,
8501 got_worktree_patch_cb patch_cb, void *patch_arg,
8502 int allow_bad_symlinks, struct got_repository *repo)
8504 const struct got_error *err = NULL, *sync_err, *unlockerr;
8505 struct got_pathlist_entry *pe;
8506 struct got_fileindex *fileindex = NULL;
8507 char *fileindex_path = NULL;
8508 struct got_reference *head_ref = NULL;
8509 struct got_object_id *head_commit_id = NULL;
8510 struct check_stage_ok_arg oka;
8511 struct stage_path_arg spa;
8513 err = lock_worktree(worktree, LOCK_EX);
8517 err = got_ref_open(&head_ref, repo,
8518 got_worktree_get_head_ref_name(worktree), 0);
8521 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8524 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8528 /* Check pre-conditions before staging anything. */
8529 oka.head_commit_id = head_commit_id;
8530 oka.worktree = worktree;
8531 oka.fileindex = fileindex;
8533 oka.have_changes = 0;
8534 TAILQ_FOREACH(pe, paths, entry) {
8535 err = worktree_status(worktree, pe->path, fileindex, repo,
8536 check_stage_ok, &oka, NULL, NULL, 1, 0);
8540 if (!oka.have_changes) {
8541 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8545 spa.worktree = worktree;
8546 spa.fileindex = fileindex;
8548 spa.patch_cb = patch_cb;
8549 spa.patch_arg = patch_arg;
8550 spa.status_cb = status_cb;
8551 spa.status_arg = status_arg;
8552 spa.staged_something = 0;
8553 spa.allow_bad_symlinks = allow_bad_symlinks;
8554 TAILQ_FOREACH(pe, paths, entry) {
8555 err = worktree_status(worktree, pe->path, fileindex, repo,
8556 stage_path, &spa, NULL, NULL, 1, 0);
8560 if (!spa.staged_something) {
8561 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8565 sync_err = sync_fileindex(fileindex, fileindex_path);
8566 if (sync_err && err == NULL)
8570 got_ref_close(head_ref);
8571 free(head_commit_id);
8572 free(fileindex_path);
8574 got_fileindex_free(fileindex);
8575 unlockerr = lock_worktree(worktree, LOCK_SH);
8576 if (unlockerr && err == NULL)
8581 struct unstage_path_arg {
8582 struct got_worktree *worktree;
8583 struct got_fileindex *fileindex;
8584 struct got_repository *repo;
8585 got_worktree_checkout_cb progress_cb;
8587 got_worktree_patch_cb patch_cb;
8591 static const struct got_error *
8592 create_unstaged_content(char **path_unstaged_content,
8593 char **path_new_staged_content, struct got_object_id *blob_id,
8594 struct got_object_id *staged_blob_id, const char *relpath,
8595 struct got_repository *repo,
8596 got_worktree_patch_cb patch_cb, void *patch_arg)
8598 const struct got_error *err, *free_err;
8599 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8600 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8601 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8602 struct got_diffreg_result *diffreg_result = NULL;
8603 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8604 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8605 int fd1 = -1, fd2 = -1;
8607 *path_unstaged_content = NULL;
8608 *path_new_staged_content = NULL;
8610 err = got_object_id_str(&label1, blob_id);
8614 fd1 = got_opentempfd();
8616 err = got_error_from_errno("got_opentempfd");
8619 fd2 = got_opentempfd();
8621 err = got_error_from_errno("got_opentempfd");
8625 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8629 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8633 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8637 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8642 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8646 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8650 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8651 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8655 err = got_opentemp_named(path_unstaged_content, &outfile,
8656 "got-unstaged-content", "");
8659 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8660 "got-new-staged-content", "");
8664 if (fseek(f1, 0L, SEEK_SET) == -1) {
8665 err = got_ferror(f1, GOT_ERR_IO);
8668 if (fseek(f2, 0L, SEEK_SET) == -1) {
8669 err = got_ferror(f2, GOT_ERR_IO);
8672 /* Count the number of actual changes in the diff result. */
8673 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8674 struct diff_chunk_context cc = {};
8675 diff_chunk_context_load_change(&cc, &nchunks_used,
8676 diffreg_result->result, n, 0);
8679 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8681 err = apply_or_reject_change(&choice, &nchunks_used,
8682 diffreg_result->result, n, relpath, f1, f2,
8683 &line_cur1, &line_cur2,
8684 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8687 if (choice == GOT_PATCH_CHOICE_YES)
8690 have_rejected_content = 1;
8691 if (choice == GOT_PATCH_CHOICE_QUIT)
8694 if (have_content || have_rejected_content)
8695 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8696 outfile, rejectfile);
8699 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8700 err = got_error_from_errno("close");
8702 got_object_blob_close(blob);
8703 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8704 err = got_error_from_errno("close");
8706 got_object_blob_close(staged_blob);
8707 free_err = got_diffreg_result_free(diffreg_result);
8708 if (free_err && err == NULL)
8710 if (f1 && fclose(f1) == EOF && err == NULL)
8711 err = got_error_from_errno2("fclose", path1);
8712 if (f2 && fclose(f2) == EOF && err == NULL)
8713 err = got_error_from_errno2("fclose", path2);
8714 if (outfile && fclose(outfile) == EOF && err == NULL)
8715 err = got_error_from_errno2("fclose", *path_unstaged_content);
8716 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8717 err = got_error_from_errno2("fclose", *path_new_staged_content);
8718 if (path1 && unlink(path1) == -1 && err == NULL)
8719 err = got_error_from_errno2("unlink", path1);
8720 if (path2 && unlink(path2) == -1 && err == NULL)
8721 err = got_error_from_errno2("unlink", path2);
8722 if (err || !have_content) {
8723 if (*path_unstaged_content &&
8724 unlink(*path_unstaged_content) == -1 && err == NULL)
8725 err = got_error_from_errno2("unlink",
8726 *path_unstaged_content);
8727 free(*path_unstaged_content);
8728 *path_unstaged_content = NULL;
8730 if (err || !have_content || !have_rejected_content) {
8731 if (*path_new_staged_content &&
8732 unlink(*path_new_staged_content) == -1 && err == NULL)
8733 err = got_error_from_errno2("unlink",
8734 *path_new_staged_content);
8735 free(*path_new_staged_content);
8736 *path_new_staged_content = NULL;
8743 static const struct got_error *
8744 unstage_hunks(struct got_object_id *staged_blob_id,
8745 struct got_blob_object *blob_base,
8746 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8747 const char *ondisk_path, const char *label_orig,
8748 struct got_worktree *worktree, struct got_repository *repo,
8749 got_worktree_patch_cb patch_cb, void *patch_arg,
8750 got_worktree_checkout_cb progress_cb, void *progress_arg)
8752 const struct got_error *err = NULL;
8753 char *path_unstaged_content = NULL;
8754 char *path_new_staged_content = NULL;
8755 char *parent = NULL, *base_path = NULL;
8756 char *blob_base_path = NULL;
8757 struct got_object_id *new_staged_blob_id = NULL;
8758 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8761 err = create_unstaged_content(&path_unstaged_content,
8762 &path_new_staged_content, blob_id, staged_blob_id,
8763 ie->path, repo, patch_cb, patch_arg);
8767 if (path_unstaged_content == NULL)
8770 if (path_new_staged_content) {
8771 err = got_object_blob_create(&new_staged_blob_id,
8772 path_new_staged_content, repo);
8777 f = fopen(path_unstaged_content, "re");
8779 err = got_error_from_errno2("fopen",
8780 path_unstaged_content);
8783 if (fstat(fileno(f), &sb) == -1) {
8784 err = got_error_from_errno2("fstat", path_unstaged_content);
8787 if (got_fileindex_entry_staged_filetype_get(ie) ==
8788 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8789 char link_target[PATH_MAX];
8791 r = fread(link_target, 1, sizeof(link_target), f);
8792 if (r == 0 && ferror(f)) {
8793 err = got_error_from_errno("fread");
8796 if (r >= sizeof(link_target)) { /* should not happen */
8797 err = got_error(GOT_ERR_NO_SPACE);
8800 link_target[r] = '\0';
8801 err = merge_symlink(worktree, blob_base,
8802 ondisk_path, ie->path, label_orig, link_target,
8803 worktree->base_commit_id, repo, progress_cb,
8806 int local_changes_subsumed;
8808 err = got_path_dirname(&parent, ondisk_path);
8812 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8814 err = got_error_from_errno("asprintf");
8819 err = got_opentemp_named(&blob_base_path, &f_base,
8823 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8829 * In order the run a 3-way merge with a symlink we copy the symlink's
8830 * target path into a temporary file and use that file with diff3.
8832 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8833 err = dump_symlink_target_path_to_file(&f_deriv2,
8839 fd = open(ondisk_path,
8840 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8842 err = got_error_from_errno2("open", ondisk_path);
8845 f_deriv2 = fdopen(fd, "r");
8846 if (f_deriv2 == NULL) {
8847 err = got_error_from_errno2("fdopen", ondisk_path);
8853 err = merge_file(&local_changes_subsumed, worktree,
8854 f_base, f, f_deriv2, ondisk_path, ie->path,
8855 got_fileindex_perms_to_st(ie),
8856 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8857 repo, progress_cb, progress_arg);
8862 if (new_staged_blob_id) {
8863 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8864 SHA1_DIGEST_LENGTH);
8866 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8867 got_fileindex_entry_staged_filetype_set(ie, 0);
8870 free(new_staged_blob_id);
8871 if (path_unstaged_content &&
8872 unlink(path_unstaged_content) == -1 && err == NULL)
8873 err = got_error_from_errno2("unlink", path_unstaged_content);
8874 if (path_new_staged_content &&
8875 unlink(path_new_staged_content) == -1 && err == NULL)
8876 err = got_error_from_errno2("unlink", path_new_staged_content);
8877 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8878 err = got_error_from_errno2("unlink", blob_base_path);
8879 if (f_base && fclose(f_base) == EOF && err == NULL)
8880 err = got_error_from_errno2("fclose", path_unstaged_content);
8881 if (f && fclose(f) == EOF && err == NULL)
8882 err = got_error_from_errno2("fclose", path_unstaged_content);
8883 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8884 err = got_error_from_errno2("fclose", ondisk_path);
8885 free(path_unstaged_content);
8886 free(path_new_staged_content);
8887 free(blob_base_path);
8893 static const struct got_error *
8894 unstage_path(void *arg, unsigned char status,
8895 unsigned char staged_status, const char *relpath,
8896 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8897 struct got_object_id *commit_id, int dirfd, const char *de_name)
8899 const struct got_error *err = NULL;
8900 struct unstage_path_arg *a = arg;
8901 struct got_fileindex_entry *ie;
8902 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8903 char *ondisk_path = NULL;
8904 char *id_str = NULL, *label_orig = NULL;
8905 int local_changes_subsumed;
8907 int fd1 = -1, fd2 = -1;
8909 if (staged_status != GOT_STATUS_ADD &&
8910 staged_status != GOT_STATUS_MODIFY &&
8911 staged_status != GOT_STATUS_DELETE)
8914 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8916 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8918 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8920 return got_error_from_errno("asprintf");
8922 err = got_object_id_str(&id_str,
8923 commit_id ? commit_id : a->worktree->base_commit_id);
8926 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8928 err = got_error_from_errno("asprintf");
8932 fd1 = got_opentempfd();
8934 err = got_error_from_errno("got_opentempfd");
8937 fd2 = got_opentempfd();
8939 err = got_error_from_errno("got_opentempfd");
8943 switch (staged_status) {
8944 case GOT_STATUS_MODIFY:
8945 err = got_object_open_as_blob(&blob_base, a->repo,
8946 blob_id, 8192, fd1);
8950 case GOT_STATUS_ADD:
8952 if (staged_status == GOT_STATUS_ADD) {
8953 int choice = GOT_PATCH_CHOICE_NONE;
8954 err = (*a->patch_cb)(&choice, a->patch_arg,
8955 staged_status, ie->path, NULL, 1, 1);
8958 if (choice != GOT_PATCH_CHOICE_YES)
8961 err = unstage_hunks(staged_blob_id,
8962 blob_base, blob_id, ie, ondisk_path,
8963 label_orig, a->worktree, a->repo,
8964 a->patch_cb, a->patch_arg,
8965 a->progress_cb, a->progress_arg);
8966 break; /* Done with this file. */
8969 err = got_object_open_as_blob(&blob_staged, a->repo,
8970 staged_blob_id, 8192, fd2);
8973 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8974 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8975 case GOT_FILEIDX_MODE_REGULAR_FILE:
8976 err = merge_blob(&local_changes_subsumed, a->worktree,
8977 blob_base, ondisk_path, relpath,
8978 got_fileindex_perms_to_st(ie), label_orig,
8979 blob_staged, commit_id ? commit_id :
8980 a->worktree->base_commit_id, a->repo,
8981 a->progress_cb, a->progress_arg);
8983 case GOT_FILEIDX_MODE_SYMLINK:
8984 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8985 char *staged_target;
8986 err = got_object_blob_read_to_str(
8987 &staged_target, blob_staged);
8990 err = merge_symlink(a->worktree, blob_base,
8991 ondisk_path, relpath, label_orig,
8992 staged_target, commit_id ? commit_id :
8993 a->worktree->base_commit_id,
8994 a->repo, a->progress_cb, a->progress_arg);
8995 free(staged_target);
8997 err = merge_blob(&local_changes_subsumed,
8998 a->worktree, blob_base, ondisk_path,
8999 relpath, got_fileindex_perms_to_st(ie),
9000 label_orig, blob_staged,
9001 commit_id ? commit_id :
9002 a->worktree->base_commit_id, a->repo,
9003 a->progress_cb, a->progress_arg);
9007 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9011 got_fileindex_entry_stage_set(ie,
9012 GOT_FILEIDX_STAGE_NONE);
9013 got_fileindex_entry_staged_filetype_set(ie, 0);
9016 case GOT_STATUS_DELETE:
9018 int choice = GOT_PATCH_CHOICE_NONE;
9019 err = (*a->patch_cb)(&choice, a->patch_arg,
9020 staged_status, ie->path, NULL, 1, 1);
9023 if (choice == GOT_PATCH_CHOICE_NO)
9025 if (choice != GOT_PATCH_CHOICE_YES) {
9026 err = got_error(GOT_ERR_PATCH_CHOICE);
9030 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9031 got_fileindex_entry_staged_filetype_set(ie, 0);
9032 err = get_file_status(&status, &sb, ie, ondisk_path,
9033 dirfd, de_name, a->repo);
9036 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9041 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9042 err = got_error_from_errno("close");
9044 got_object_blob_close(blob_base);
9045 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9046 err = got_error_from_errno("close");
9048 got_object_blob_close(blob_staged);
9054 const struct got_error *
9055 got_worktree_unstage(struct got_worktree *worktree,
9056 struct got_pathlist_head *paths,
9057 got_worktree_checkout_cb progress_cb, void *progress_arg,
9058 got_worktree_patch_cb patch_cb, void *patch_arg,
9059 struct got_repository *repo)
9061 const struct got_error *err = NULL, *sync_err, *unlockerr;
9062 struct got_pathlist_entry *pe;
9063 struct got_fileindex *fileindex = NULL;
9064 char *fileindex_path = NULL;
9065 struct unstage_path_arg upa;
9067 err = lock_worktree(worktree, LOCK_EX);
9071 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9075 upa.worktree = worktree;
9076 upa.fileindex = fileindex;
9078 upa.progress_cb = progress_cb;
9079 upa.progress_arg = progress_arg;
9080 upa.patch_cb = patch_cb;
9081 upa.patch_arg = patch_arg;
9082 TAILQ_FOREACH(pe, paths, entry) {
9083 err = worktree_status(worktree, pe->path, fileindex, repo,
9084 unstage_path, &upa, NULL, NULL, 1, 0);
9089 sync_err = sync_fileindex(fileindex, fileindex_path);
9090 if (sync_err && err == NULL)
9093 free(fileindex_path);
9095 got_fileindex_free(fileindex);
9096 unlockerr = lock_worktree(worktree, LOCK_SH);
9097 if (unlockerr && err == NULL)
9102 struct report_file_info_arg {
9103 struct got_worktree *worktree;
9104 got_worktree_path_info_cb info_cb;
9106 struct got_pathlist_head *paths;
9107 got_cancel_cb cancel_cb;
9111 static const struct got_error *
9112 report_file_info(void *arg, struct got_fileindex_entry *ie)
9114 struct report_file_info_arg *a = arg;
9115 struct got_pathlist_entry *pe;
9116 struct got_object_id blob_id, staged_blob_id, commit_id;
9117 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9118 struct got_object_id *commit_idp = NULL;
9121 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9122 return got_error(GOT_ERR_CANCELLED);
9124 TAILQ_FOREACH(pe, a->paths, entry) {
9125 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9126 got_path_is_child(ie->path, pe->path, pe->path_len))
9129 if (pe == NULL) /* not found */
9132 if (got_fileindex_entry_has_blob(ie)) {
9133 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
9134 blob_id.algo = WORKTREE_ALGO(a->worktree);
9135 blob_idp = &blob_id;
9137 stage = got_fileindex_entry_stage_get(ie);
9138 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9139 stage == GOT_FILEIDX_STAGE_ADD) {
9140 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
9141 SHA1_DIGEST_LENGTH);
9142 staged_blob_id.algo = WORKTREE_ALGO(a->worktree);
9143 staged_blob_idp = &staged_blob_id;
9146 if (got_fileindex_entry_has_commit(ie)) {
9147 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
9148 commit_id.algo = WORKTREE_ALGO(a->worktree);
9149 commit_idp = &commit_id;
9152 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9153 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9156 const struct got_error *
9157 got_worktree_path_info(struct got_worktree *worktree,
9158 struct got_pathlist_head *paths,
9159 got_worktree_path_info_cb info_cb, void *info_arg,
9160 got_cancel_cb cancel_cb, void *cancel_arg)
9163 const struct got_error *err = NULL, *unlockerr;
9164 struct got_fileindex *fileindex = NULL;
9165 char *fileindex_path = NULL;
9166 struct report_file_info_arg arg;
9168 err = lock_worktree(worktree, LOCK_SH);
9172 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9176 arg.worktree = worktree;
9177 arg.info_cb = info_cb;
9178 arg.info_arg = info_arg;
9180 arg.cancel_cb = cancel_cb;
9181 arg.cancel_arg = cancel_arg;
9182 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9185 free(fileindex_path);
9187 got_fileindex_free(fileindex);
9188 unlockerr = lock_worktree(worktree, LOCK_UN);
9189 if (unlockerr && err == NULL)
9194 static const struct got_error *
9195 patch_check_path(const char *p, char **path, unsigned char *status,
9196 unsigned char *staged_status, struct got_fileindex *fileindex,
9197 struct got_worktree *worktree, struct got_repository *repo)
9199 const struct got_error *err;
9200 struct got_fileindex_entry *ie;
9202 char *ondisk_path = NULL;
9204 err = got_worktree_resolve_path(path, worktree, p);
9208 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9209 *path[0] ? "/" : "", *path) == -1)
9210 return got_error_from_errno("asprintf");
9212 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9214 *staged_status = get_staged_status(ie);
9215 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9220 *staged_status = GOT_STATUS_NO_CHANGE;
9221 *status = GOT_STATUS_UNVERSIONED;
9222 if (lstat(ondisk_path, &sb) == -1) {
9223 if (errno != ENOENT) {
9224 err = got_error_from_errno2("lstat",
9228 *status = GOT_STATUS_NONEXISTENT;
9237 static const struct got_error *
9238 patch_can_rm(const char *path, unsigned char status,
9239 unsigned char staged_status)
9241 if (status == GOT_STATUS_NONEXISTENT)
9242 return got_error_set_errno(ENOENT, path);
9243 if (status != GOT_STATUS_NO_CHANGE &&
9244 status != GOT_STATUS_ADD &&
9245 status != GOT_STATUS_MODIFY &&
9246 status != GOT_STATUS_MODE_CHANGE)
9247 return got_error_path(path, GOT_ERR_FILE_STATUS);
9248 if (staged_status == GOT_STATUS_DELETE)
9249 return got_error_path(path, GOT_ERR_FILE_STATUS);
9253 static const struct got_error *
9254 patch_can_add(const char *path, unsigned char status)
9256 if (status != GOT_STATUS_NONEXISTENT)
9257 return got_error_path(path, GOT_ERR_FILE_STATUS);
9261 static const struct got_error *
9262 patch_can_edit(const char *path, unsigned char status,
9263 unsigned char staged_status)
9265 if (status == GOT_STATUS_NONEXISTENT)
9266 return got_error_set_errno(ENOENT, path);
9267 if (status != GOT_STATUS_NO_CHANGE &&
9268 status != GOT_STATUS_ADD &&
9269 status != GOT_STATUS_MODIFY)
9270 return got_error_path(path, GOT_ERR_FILE_STATUS);
9271 if (staged_status == GOT_STATUS_DELETE)
9272 return got_error_path(path, GOT_ERR_FILE_STATUS);
9276 const struct got_error *
9277 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9278 char **fileindex_path, struct got_worktree *worktree)
9280 return open_fileindex(fileindex, fileindex_path, worktree);
9283 const struct got_error *
9284 got_worktree_patch_check_path(const char *old, const char *new,
9285 char **oldpath, char **newpath, struct got_worktree *worktree,
9286 struct got_repository *repo, struct got_fileindex *fileindex)
9288 const struct got_error *err = NULL;
9289 int file_renamed = 0;
9290 unsigned char status_old, staged_status_old;
9291 unsigned char status_new, staged_status_new;
9296 err = patch_check_path(old != NULL ? old : new, oldpath,
9297 &status_old, &staged_status_old, fileindex, worktree, repo);
9301 err = patch_check_path(new != NULL ? new : old, newpath,
9302 &status_new, &staged_status_new, fileindex, worktree, repo);
9306 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9309 if (old != NULL && new == NULL)
9310 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9311 else if (file_renamed) {
9312 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9314 err = patch_can_add(*newpath, status_new);
9315 } else if (old == NULL)
9316 err = patch_can_add(*newpath, status_new);
9318 err = patch_can_edit(*newpath, status_new, staged_status_new);
9330 const struct got_error *
9331 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9332 struct got_worktree *worktree, struct got_fileindex *fileindex,
9333 got_worktree_checkout_cb progress_cb, void *progress_arg)
9335 struct schedule_addition_args saa;
9337 memset(&saa, 0, sizeof(saa));
9338 saa.worktree = worktree;
9339 saa.fileindex = fileindex;
9340 saa.progress_cb = progress_cb;
9341 saa.progress_arg = progress_arg;
9344 return worktree_status(worktree, path, fileindex, repo,
9345 schedule_addition, &saa, NULL, NULL, 1, 0);
9348 const struct got_error *
9349 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9350 struct got_worktree *worktree, struct got_fileindex *fileindex,
9351 got_worktree_delete_cb progress_cb, void *progress_arg)
9353 struct schedule_deletion_args sda;
9355 memset(&sda, 0, sizeof(sda));
9356 sda.worktree = worktree;
9357 sda.fileindex = fileindex;
9358 sda.progress_cb = progress_cb;
9359 sda.progress_arg = progress_arg;
9361 sda.delete_local_mods = 0;
9362 sda.keep_on_disk = 0;
9363 sda.ignore_missing_paths = 0;
9364 sda.status_codes = NULL;
9366 return worktree_status(worktree, path, fileindex, repo,
9367 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9370 const struct got_error *
9371 got_worktree_patch_complete(struct got_fileindex *fileindex,
9372 const char *fileindex_path)
9374 const struct got_error *err = NULL;
9376 err = sync_fileindex(fileindex, fileindex_path);
9377 got_fileindex_free(fileindex);