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>
35 #include "got_compat.h"
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_opentemp.h"
47 #include "got_lib_worktree.h"
48 #include "got_lib_hash.h"
49 #include "got_lib_fileindex.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_object_idset.h"
56 #include "got_lib_diff.h"
57 #include "got_lib_gotconfig.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static mode_t apply_umask(mode_t);
68 static const struct got_error *
69 create_meta_file(const char *path_got, const char *name, const char *content)
71 const struct got_error *err = NULL;
74 if (asprintf(&path, "%s/%s", path_got, name) == -1)
75 return got_error_from_errno("asprintf");
77 err = got_path_create_file(path, content);
82 static const struct got_error *
83 update_meta_file(const char *path_got, const char *name, const char *content)
85 const struct got_error *err = NULL;
90 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
91 err = got_error_from_errno("asprintf");
96 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
101 int len = fprintf(tmpfile, "%s\n", content);
102 if (len != strlen(content) + 1) {
103 err = got_error_from_errno2("fprintf", tmppath);
108 if (rename(tmppath, path) != 0) {
109 err = got_error_from_errno3("rename", tmppath, path);
115 if (fclose(tmpfile) == EOF && err == NULL)
116 err = got_error_from_errno2("fclose", tmppath);
121 static const struct got_error *
122 write_head_ref(const char *path_got, struct got_reference *head_ref)
124 const struct got_error *err = NULL;
127 if (got_ref_is_symbolic(head_ref)) {
128 refstr = got_ref_to_str(head_ref);
130 return got_error_from_errno("got_ref_to_str");
132 refstr = strdup(got_ref_get_name(head_ref));
134 return got_error_from_errno("strdup");
136 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
141 const struct got_error *
142 got_worktree_init(const char *path, struct got_reference *head_ref,
143 const char *prefix, struct got_repository *repo)
145 const struct got_error *err = NULL;
146 struct got_object_id *commit_id = NULL;
148 uint32_t uuid_status;
150 char *path_got = NULL;
151 char *formatstr = NULL;
152 char *absprefix = NULL;
153 char *basestr = NULL;
154 char *uuidstr = NULL;
156 if (strcmp(path, got_repo_get_path(repo)) == 0) {
157 err = got_error(GOT_ERR_WORKTREE_REPO);
161 err = got_ref_resolve(&commit_id, repo, head_ref);
164 err = got_object_get_type(&obj_type, repo, commit_id);
167 if (obj_type != GOT_OBJ_TYPE_COMMIT)
168 return got_error(GOT_ERR_OBJ_TYPE);
170 if (!got_path_is_absolute(prefix)) {
171 if (asprintf(&absprefix, "/%s", prefix) == -1)
172 return got_error_from_errno("asprintf");
175 /* Create top-level directory (may already exist). */
176 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
177 err = got_error_from_errno2("mkdir", path);
181 /* Create .got directory (may already exist). */
182 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
183 err = got_error_from_errno("asprintf");
186 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 err = got_error_from_errno2("mkdir", path_got);
191 /* Create an empty lock file. */
192 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
196 /* Create an empty file index. */
197 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
201 /* Write the HEAD reference. */
202 err = write_head_ref(path_got, head_ref);
206 /* Record our base commit. */
207 err = got_object_id_str(&basestr, commit_id);
210 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
214 /* Store path to repository. */
215 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
216 got_repo_get_path(repo));
220 /* Store in-repository path prefix. */
221 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
222 absprefix ? absprefix : prefix);
227 uuid_create(&uuid, &uuid_status);
228 if (uuid_status != uuid_s_ok) {
229 err = got_error_uuid(uuid_status, "uuid_create");
232 uuid_to_string(&uuid, &uuidstr, &uuid_status);
233 if (uuid_status != uuid_s_ok) {
234 err = got_error_uuid(uuid_status, "uuid_to_string");
237 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
241 /* Stamp work tree with format file. */
242 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
243 err = got_error_from_errno("asprintf");
246 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
260 const struct got_error *
261 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
262 const char *path_prefix)
264 char *absprefix = NULL;
266 if (!got_path_is_absolute(path_prefix)) {
267 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
268 return got_error_from_errno("asprintf");
270 *match = (strcmp(absprefix ? absprefix : path_prefix,
271 worktree->path_prefix) == 0);
277 got_worktree_get_head_ref_name(struct got_worktree *worktree)
279 return worktree->head_ref_name;
282 const struct got_error *
283 got_worktree_set_head_ref(struct got_worktree *worktree,
284 struct got_reference *head_ref)
286 const struct got_error *err = NULL;
287 char *path_got = NULL, *head_ref_name = NULL;
289 if (asprintf(&path_got, "%s/%s", worktree->root_path,
290 GOT_WORKTREE_GOT_DIR) == -1) {
291 err = got_error_from_errno("asprintf");
296 head_ref_name = strdup(got_ref_get_name(head_ref));
297 if (head_ref_name == NULL) {
298 err = got_error_from_errno("strdup");
302 err = write_head_ref(path_got, head_ref);
306 free(worktree->head_ref_name);
307 worktree->head_ref_name = head_ref_name;
315 struct got_object_id *
316 got_worktree_get_base_commit_id(struct got_worktree *worktree)
318 return worktree->base_commit_id;
321 const struct got_error *
322 got_worktree_set_base_commit_id(struct got_worktree *worktree,
323 struct got_repository *repo, struct got_object_id *commit_id)
325 const struct got_error *err;
326 struct got_object *obj = NULL;
328 char *path_got = NULL;
330 if (asprintf(&path_got, "%s/%s", worktree->root_path,
331 GOT_WORKTREE_GOT_DIR) == -1) {
332 err = got_error_from_errno("asprintf");
337 err = got_object_open(&obj, repo, commit_id);
341 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
342 err = got_error(GOT_ERR_OBJ_TYPE);
346 /* Record our base commit. */
347 err = got_object_id_str(&id_str, commit_id);
350 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
354 free(worktree->base_commit_id);
355 worktree->base_commit_id = got_object_id_dup(commit_id);
356 if (worktree->base_commit_id == NULL) {
357 err = got_error_from_errno("got_object_id_dup");
362 got_object_close(obj);
368 const struct got_gotconfig *
369 got_worktree_get_gotconfig(struct got_worktree *worktree)
371 return worktree->gotconfig;
374 static const struct got_error *
375 lock_worktree(struct got_worktree *worktree, int operation)
377 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
378 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
379 : got_error_from_errno2("flock",
380 got_worktree_get_root_path(worktree)));
384 static const struct got_error *
385 add_dir_on_disk(struct got_worktree *worktree, const char *path)
387 const struct got_error *err = NULL;
390 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
391 return got_error_from_errno("asprintf");
393 err = got_path_mkdir(abspath);
394 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
397 if (lstat(abspath, &sb) == -1) {
398 err = got_error_from_errno2("lstat", abspath);
399 } else if (!S_ISDIR(sb.st_mode)) {
400 /* TODO directory is obstructed; do something */
401 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
408 static const struct got_error *
409 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
411 const struct got_error *err = NULL;
414 size_t flen1 = 0, flen2 = 0;
419 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
420 if (flen1 == 0 && ferror(f1)) {
421 err = got_error_from_errno("fread");
424 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
425 if (flen2 == 0 && ferror(f2)) {
426 err = got_error_from_errno("fread");
433 } else if (flen2 == 0) {
437 } else if (flen1 == flen2) {
438 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
451 static const struct got_error *
452 check_files_equal(int *same, FILE *f1, FILE *f2)
459 if (fstat(fileno(f1), &sb) != 0)
460 return got_error_from_errno("fstat");
463 if (fstat(fileno(f2), &sb) != 0)
464 return got_error_from_errno("fstat");
467 if (size1 != size2) {
472 if (fseek(f1, 0L, SEEK_SET) == -1)
473 return got_ferror(f1, GOT_ERR_IO);
474 if (fseek(f2, 0L, SEEK_SET) == -1)
475 return got_ferror(f2, GOT_ERR_IO);
477 return check_file_contents_equal(same, f1, f2);
480 static const struct got_error *
481 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
489 if (fseek(f, 0L, SEEK_SET) == -1)
490 return got_ferror(f, GOT_ERR_IO);
493 flen = fread(fbuf, 1, sizeof(fbuf), f);
496 return got_error_from_errno("fread");
500 outlen = write(outfd, fbuf, flen);
502 return got_error_from_errno("write");
504 return got_error(GOT_ERR_IO);
511 static const struct got_error *
512 merge_binary_file(int *overlapcnt, int merged_fd,
513 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
514 const char *label_deriv, const char *label_orig, const char *label_deriv2,
515 const char *ondisk_path)
517 const struct got_error *err = NULL;
518 int same_content, changed_deriv, changed_deriv2;
519 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
520 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
521 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
522 char *base_path_orig = NULL, *base_path_deriv = NULL;
523 char *base_path_deriv2 = NULL;
527 err = check_files_equal(&same_content, f_deriv, f_deriv2);
532 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
534 err = check_files_equal(&same_content, f_deriv, f_orig);
537 changed_deriv = !same_content;
538 err = check_files_equal(&same_content, f_deriv2, f_orig);
541 changed_deriv2 = !same_content;
543 if (changed_deriv && changed_deriv2) {
545 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
546 err = got_error_from_errno("asprintf");
549 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
550 err = got_error_from_errno("asprintf");
553 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
554 err = got_error_from_errno("asprintf");
557 err = got_opentemp_named_fd(&path_orig, &fd_orig,
561 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
562 base_path_deriv, "");
565 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
566 base_path_deriv2, "");
569 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
572 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
575 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
578 if (dprintf(merged_fd, "Binary files differ and cannot be "
579 "merged automatically:\n") < 0) {
580 err = got_error_from_errno("dprintf");
583 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
584 GOT_DIFF_CONFLICT_MARKER_BEGIN,
585 label_deriv ? " " : "",
586 label_deriv ? label_deriv : "",
588 err = got_error_from_errno("dprintf");
592 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
593 GOT_DIFF_CONFLICT_MARKER_ORIG,
594 label_orig ? " " : "",
595 label_orig ? label_orig : "",
597 err = got_error_from_errno("dprintf");
601 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
602 GOT_DIFF_CONFLICT_MARKER_SEP,
604 GOT_DIFF_CONFLICT_MARKER_END,
605 label_deriv2 ? " " : "",
606 label_deriv2 ? label_deriv2 : "") < 0) {
607 err = got_error_from_errno("dprintf");
610 } else if (changed_deriv)
611 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
612 else if (changed_deriv2)
613 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
615 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
617 err = got_error_from_errno2("unlink", path_orig);
618 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
619 err = got_error_from_errno2("close", path_orig);
620 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
621 err = got_error_from_errno2("close", path_deriv);
622 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
623 err = got_error_from_errno2("close", path_deriv2);
627 free(base_path_orig);
628 free(base_path_deriv);
629 free(base_path_deriv2);
634 * Perform a 3-way merge where the file f_orig acts as the common
635 * ancestor, the file f_deriv acts as the first derived version,
636 * and the file f_deriv2 acts as the second derived version.
637 * The merge result will be written to a new file at ondisk_path; any
638 * existing file at this path will be replaced.
640 static const struct got_error *
641 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
642 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
643 const char *path, uint16_t st_mode,
644 const char *label_orig, const char *label_deriv, const char *label_deriv2,
645 enum got_diff_algorithm diff_algo, struct got_repository *repo,
646 got_worktree_checkout_cb progress_cb, void *progress_arg)
648 const struct got_error *err = NULL;
650 FILE *f_merged = NULL;
651 char *merged_path = NULL, *base_path = NULL;
655 *local_changes_subsumed = 0;
657 err = got_path_dirname(&parent, ondisk_path);
661 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
662 err = got_error_from_errno("asprintf");
666 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
670 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
671 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
673 if (err->code != GOT_ERR_FILE_BINARY)
675 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
676 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
682 err = (*progress_cb)(progress_arg,
683 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
687 if (fsync(merged_fd) != 0) {
688 err = got_error_from_errno("fsync");
692 f_merged = fdopen(merged_fd, "r");
693 if (f_merged == NULL) {
694 err = got_error_from_errno("fdopen");
699 /* Check if a clean merge has subsumed all local changes. */
700 if (overlapcnt == 0) {
701 err = check_files_equal(local_changes_subsumed, f_deriv,
707 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
708 err = got_error_from_errno2("fchmod", merged_path);
712 if (rename(merged_path, ondisk_path) != 0) {
713 err = got_error_from_errno3("rename", merged_path,
722 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
723 err = got_error_from_errno("close");
724 if (f_merged && fclose(f_merged) == EOF && err == NULL)
725 err = got_error_from_errno("fclose");
732 static const struct got_error *
733 update_symlink(const char *ondisk_path, const char *target_path,
736 /* This is not atomic but matches what 'ln -sf' does. */
737 if (unlink(ondisk_path) == -1)
738 return got_error_from_errno2("unlink", ondisk_path);
739 if (symlink(target_path, ondisk_path) == -1)
740 return got_error_from_errno3("symlink", target_path,
746 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
747 * in the work tree with a file that contains conflict markers and the
748 * conflicting target paths of the original version, a "derived version"
749 * of a symlink from an incoming change, and a local version of the symlink.
751 * The original versions's target path can be NULL if it is not available,
752 * such as if both derived versions added a new symlink at the same path.
754 * The incoming derived symlink target is NULL in case the incoming change
755 * has deleted this symlink.
757 static const struct got_error *
758 install_symlink_conflict(const char *deriv_target,
759 struct got_object_id *deriv_base_commit_id, const char *orig_target,
760 const char *label_orig, const char *local_target, const char *ondisk_path)
762 const struct got_error *err;
763 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
766 err = got_object_id_str(&id_str, deriv_base_commit_id);
768 return got_error_from_errno("asprintf");
770 if (asprintf(&label_deriv, "%s: commit %s",
771 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
772 err = got_error_from_errno("asprintf");
776 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
780 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
781 err = got_error_from_errno2("fchmod", path);
785 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
786 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
787 deriv_target ? deriv_target : "(symlink was deleted)",
788 orig_target ? label_orig : "",
789 orig_target ? "\n" : "",
790 orig_target ? orig_target : "",
791 orig_target ? "\n" : "",
792 GOT_DIFF_CONFLICT_MARKER_SEP,
793 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
794 err = got_error_from_errno2("fprintf", path);
798 if (unlink(ondisk_path) == -1) {
799 err = got_error_from_errno2("unlink", ondisk_path);
802 if (rename(path, ondisk_path) == -1) {
803 err = got_error_from_errno3("rename", path, ondisk_path);
807 if (f != NULL && fclose(f) == EOF && err == NULL)
808 err = got_error_from_errno2("fclose", path);
815 /* forward declaration */
816 static const struct got_error *
817 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
818 const char *, const char *, uint16_t, const char *,
819 struct got_blob_object *, struct got_object_id *,
820 struct got_repository *, got_worktree_checkout_cb, void *);
823 * Merge a symlink into the work tree, where blob_orig acts as the common
824 * ancestor, deriv_target is the link target of the first derived version,
825 * and the symlink on disk acts as the second derived version.
826 * Assume that contents of both blobs represent symlinks.
828 static const struct got_error *
829 merge_symlink(struct got_worktree *worktree,
830 struct got_blob_object *blob_orig, const char *ondisk_path,
831 const char *path, const char *label_orig, const char *deriv_target,
832 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
833 got_worktree_checkout_cb progress_cb, void *progress_arg)
835 const struct got_error *err = NULL;
836 char *ancestor_target = NULL;
838 ssize_t ondisk_len, deriv_len;
839 char ondisk_target[PATH_MAX];
840 int have_local_change = 0;
841 int have_incoming_change = 0;
843 if (lstat(ondisk_path, &sb) == -1)
844 return got_error_from_errno2("lstat", ondisk_path);
846 ondisk_len = readlink(ondisk_path, ondisk_target,
847 sizeof(ondisk_target));
848 if (ondisk_len == -1) {
849 err = got_error_from_errno2("readlink",
853 ondisk_target[ondisk_len] = '\0';
856 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
861 if (ancestor_target == NULL ||
862 (ondisk_len != strlen(ancestor_target) ||
863 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
864 have_local_change = 1;
866 deriv_len = strlen(deriv_target);
867 if (ancestor_target == NULL ||
868 (deriv_len != strlen(ancestor_target) ||
869 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
870 have_incoming_change = 1;
872 if (!have_local_change && !have_incoming_change) {
873 if (ancestor_target) {
874 /* Both sides made the same change. */
875 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 } else if (deriv_len == ondisk_len &&
878 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
879 /* Both sides added the same symlink. */
880 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
883 /* Both sides added symlinks which don't match. */
884 err = install_symlink_conflict(deriv_target,
885 deriv_base_commit_id, ancestor_target,
886 label_orig, ondisk_target, ondisk_path);
889 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
892 } else if (!have_local_change && have_incoming_change) {
893 /* Apply the incoming change. */
894 err = update_symlink(ondisk_path, deriv_target,
895 strlen(deriv_target));
898 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
899 } else if (have_local_change && have_incoming_change) {
900 if (deriv_len == ondisk_len &&
901 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
902 /* Both sides made the same change. */
903 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
906 err = install_symlink_conflict(deriv_target,
907 deriv_base_commit_id, ancestor_target, label_orig,
908 ondisk_target, ondisk_path);
911 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
917 free(ancestor_target);
921 static const struct got_error *
922 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
924 const struct got_error *err = NULL;
925 char target_path[PATH_MAX];
934 return got_error_from_errno("got_opentemp");
935 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
936 if (target_len == -1) {
937 err = got_error_from_errno2("readlink", ondisk_path);
940 n = fwrite(target_path, 1, target_len, f);
941 if (n != target_len) {
942 err = got_ferror(f, GOT_ERR_IO);
945 if (fflush(f) == EOF) {
946 err = got_error_from_errno("fflush");
949 if (fseek(f, 0L, SEEK_SET) == -1) {
950 err = got_ferror(f, GOT_ERR_IO);
962 * Perform a 3-way merge where blob_orig acts as the common ancestor,
963 * blob_deriv acts as the first derived version, and the file on disk
964 * acts as the second derived version.
966 static const struct got_error *
967 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
968 struct got_blob_object *blob_orig, const char *ondisk_path,
969 const char *path, uint16_t st_mode, const char *label_orig,
970 struct got_blob_object *blob_deriv,
971 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
972 got_worktree_checkout_cb progress_cb, void *progress_arg)
974 const struct got_error *err = NULL;
975 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
976 char *blob_orig_path = NULL;
977 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
978 char *label_deriv = NULL, *parent = NULL;
980 *local_changes_subsumed = 0;
982 err = got_path_dirname(&parent, ondisk_path);
987 if (asprintf(&base_path, "%s/got-merge-blob-orig",
989 err = got_error_from_errno("asprintf");
994 err = got_opentemp_named(&blob_orig_path, &f_orig,
998 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1005 * No common ancestor exists. This is an "add vs add" conflict
1006 * and we simply use an empty ancestor file to make both files
1007 * appear in the merged result in their entirety.
1009 f_orig = got_opentemp();
1010 if (f_orig == NULL) {
1011 err = got_error_from_errno("got_opentemp");
1016 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1017 err = got_error_from_errno("asprintf");
1022 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1025 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1030 err = got_object_id_str(&id_str, deriv_base_commit_id);
1033 if (asprintf(&label_deriv, "%s: commit %s",
1034 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1035 err = got_error_from_errno("asprintf");
1040 * In order the run a 3-way merge with a symlink we copy the symlink's
1041 * target path into a temporary file and use that file with diff3.
1043 if (S_ISLNK(st_mode)) {
1044 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1049 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 err = got_error_from_errno2("open", ondisk_path);
1054 f_deriv2 = fdopen(fd, "r");
1055 if (f_deriv2 == NULL) {
1056 err = got_error_from_errno2("fdopen", ondisk_path);
1062 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1063 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1064 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1073 if (blob_orig_path) {
1074 unlink(blob_orig_path);
1075 free(blob_orig_path);
1077 if (blob_deriv_path) {
1078 unlink(blob_deriv_path);
1079 free(blob_deriv_path);
1087 static const struct got_error *
1088 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1089 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1090 int wt_fd, const char *path, struct got_object_id *blob_id)
1092 const struct got_error *err = NULL;
1093 struct got_fileindex_entry *new_ie;
1097 err = got_fileindex_entry_alloc(&new_ie, path);
1101 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1102 blob_id->sha1, base_commit_id->sha1, 1);
1106 err = got_fileindex_entry_add(fileindex, new_ie);
1109 got_fileindex_entry_free(new_ie);
1116 get_ondisk_perms(int executable, mode_t st_mode)
1118 mode_t xbits = S_IXUSR;
1121 /* Map read bits to execute bits. */
1122 if (st_mode & S_IRGRP)
1124 if (st_mode & S_IROTH)
1126 return st_mode | xbits;
1133 apply_umask(mode_t mode)
1142 /* forward declaration */
1143 static const struct got_error *
1144 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1145 const char *path, mode_t te_mode, mode_t st_mode,
1146 struct got_blob_object *blob, int restoring_missing_file,
1147 int reverting_versioned_file, int installing_bad_symlink,
1148 int path_is_unversioned, struct got_repository *repo,
1149 got_worktree_checkout_cb progress_cb, void *progress_arg);
1152 * This function assumes that the provided symlink target points at a
1153 * safe location in the work tree!
1155 static const struct got_error *
1156 replace_existing_symlink(int *did_something, const char *ondisk_path,
1157 const char *target_path, size_t target_len)
1159 const struct got_error *err = NULL;
1161 char etarget[PATH_MAX];
1167 * "Bad" symlinks (those pointing outside the work tree or into the
1168 * .got directory) are installed in the work tree as a regular file
1169 * which contains the bad symlink target path.
1170 * The new symlink target has already been checked for safety by our
1171 * caller. If we can successfully open a regular file then we simply
1172 * replace this file with a symlink below.
1174 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 if (!got_err_open_nofollow_on_symlink())
1177 return got_error_from_errno2("open", ondisk_path);
1179 /* We are updating an existing on-disk symlink. */
1180 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 return got_error_from_errno2("readlink", ondisk_path);
1184 if (elen == target_len &&
1185 memcmp(etarget, target_path, target_len) == 0)
1186 return NULL; /* nothing to do */
1190 err = update_symlink(ondisk_path, target_path, target_len);
1191 if (fd != -1 && close(fd) == -1 && err == NULL)
1192 err = got_error_from_errno2("close", ondisk_path);
1196 static const struct got_error *
1197 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1198 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1200 const struct got_error *err = NULL;
1201 char canonpath[PATH_MAX];
1202 char *path_got = NULL;
1204 *is_bad_symlink = 0;
1206 if (target_len >= sizeof(canonpath)) {
1207 *is_bad_symlink = 1;
1212 * We do not use realpath(3) to resolve the symlink's target
1213 * path because we don't want to resolve symlinks recursively.
1214 * Instead we make the path absolute and then canonicalize it.
1215 * Relative symlink target lookup should begin at the directory
1216 * in which the blob object is being installed.
1218 if (!got_path_is_absolute(target_path)) {
1219 char *abspath, *parent;
1220 err = got_path_dirname(&parent, ondisk_path);
1223 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 return got_error_from_errno("asprintf");
1228 if (strlen(abspath) >= sizeof(canonpath)) {
1229 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1233 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1238 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1243 /* Only allow symlinks pointing at paths within the work tree. */
1244 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1245 *is_bad_symlink = 1;
1249 /* Do not allow symlinks pointing into the .got directory. */
1250 if (asprintf(&path_got, "%s/%s", wtroot_path,
1251 GOT_WORKTREE_GOT_DIR) == -1)
1252 return got_error_from_errno("asprintf");
1253 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1254 *is_bad_symlink = 1;
1260 static const struct got_error *
1261 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1262 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1263 int restoring_missing_file, int reverting_versioned_file,
1264 int path_is_unversioned, int allow_bad_symlinks,
1265 struct got_repository *repo,
1266 got_worktree_checkout_cb progress_cb, void *progress_arg)
1268 const struct got_error *err = NULL;
1269 char target_path[PATH_MAX];
1270 size_t len, target_len = 0;
1271 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1272 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1274 *is_bad_symlink = 0;
1277 * Blob object content specifies the target path of the link.
1278 * If a symbolic link cannot be installed we instead create
1279 * a regular file which contains the link target path stored
1280 * in the blob object.
1283 err = got_object_blob_read_block(&len, blob);
1287 if (len + target_len >= sizeof(target_path)) {
1288 /* Path too long; install as a regular file. */
1289 *is_bad_symlink = 1;
1290 got_object_blob_rewind(blob);
1291 return install_blob(worktree, ondisk_path, path,
1292 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1293 restoring_missing_file, reverting_versioned_file,
1294 1, path_is_unversioned, repo, progress_cb,
1298 /* Skip blob object header first time around. */
1299 memcpy(target_path + target_len, buf + hdrlen,
1301 target_len += len - hdrlen;
1305 target_path[target_len] = '\0';
1307 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1308 ondisk_path, worktree->root_path);
1312 if (*is_bad_symlink && !allow_bad_symlinks) {
1313 /* install as a regular file */
1314 got_object_blob_rewind(blob);
1315 err = install_blob(worktree, ondisk_path, path,
1316 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1317 restoring_missing_file, reverting_versioned_file, 1,
1318 path_is_unversioned, repo, progress_cb, progress_arg);
1322 if (symlink(target_path, ondisk_path) == -1) {
1323 if (errno == EEXIST) {
1324 int symlink_replaced;
1325 if (path_is_unversioned) {
1326 err = (*progress_cb)(progress_arg,
1327 GOT_STATUS_UNVERSIONED, path);
1330 err = replace_existing_symlink(&symlink_replaced,
1331 ondisk_path, target_path, target_len);
1335 if (symlink_replaced) {
1336 err = (*progress_cb)(progress_arg,
1337 reverting_versioned_file ?
1339 GOT_STATUS_UPDATE, path);
1341 err = (*progress_cb)(progress_arg,
1342 GOT_STATUS_EXISTS, path);
1345 return err; /* Nothing else to do. */
1348 if (errno == ENOENT) {
1350 err = got_path_dirname(&parent, ondisk_path);
1353 err = add_dir_on_disk(worktree, parent);
1358 * Retry, and fall through to error handling
1359 * below if this second attempt fails.
1361 if (symlink(target_path, ondisk_path) != -1) {
1362 err = NULL; /* success */
1367 /* Handle errors from first or second creation attempt. */
1368 if (errno == ENAMETOOLONG) {
1369 /* bad target path; install as a regular file */
1370 *is_bad_symlink = 1;
1371 got_object_blob_rewind(blob);
1372 err = install_blob(worktree, ondisk_path, path,
1373 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 restoring_missing_file, reverting_versioned_file, 1,
1375 path_is_unversioned, repo,
1376 progress_cb, progress_arg);
1377 } else if (errno == ENOTDIR) {
1378 err = got_error_path(ondisk_path,
1379 GOT_ERR_FILE_OBSTRUCTED);
1381 err = got_error_from_errno3("symlink",
1382 target_path, ondisk_path);
1384 } else if (progress_cb)
1385 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1386 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1390 static const struct got_error *
1391 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1392 const char *path, mode_t te_mode, mode_t st_mode,
1393 struct got_blob_object *blob, int restoring_missing_file,
1394 int reverting_versioned_file, int installing_bad_symlink,
1395 int path_is_unversioned, struct got_repository *repo,
1396 got_worktree_checkout_cb progress_cb, void *progress_arg)
1398 const struct got_error *err = NULL;
1402 char *tmppath = NULL;
1405 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1406 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1409 if (errno == ENOENT) {
1411 err = got_path_dirname(&parent, path);
1414 err = add_dir_on_disk(worktree, parent);
1418 fd = open(ondisk_path,
1419 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1422 return got_error_from_errno2("open",
1424 } else if (errno == EEXIST) {
1425 if (path_is_unversioned) {
1426 err = (*progress_cb)(progress_arg,
1427 GOT_STATUS_UNVERSIONED, path);
1430 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1431 !S_ISREG(st_mode) && !installing_bad_symlink) {
1432 /* TODO file is obstructed; do something */
1433 err = got_error_path(ondisk_path,
1434 GOT_ERR_FILE_OBSTRUCTED);
1437 err = got_opentemp_named_fd(&tmppath, &fd,
1443 if (fchmod(fd, apply_umask(mode)) == -1) {
1444 err = got_error_from_errno2("fchmod",
1450 return got_error_from_errno2("open", ondisk_path);
1454 if (restoring_missing_file)
1455 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 else if (reverting_versioned_file)
1458 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1461 err = (*progress_cb)(progress_arg,
1462 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1467 hdrlen = got_object_blob_get_hdrlen(blob);
1469 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1470 err = got_object_blob_read_block(&len, blob);
1474 /* Skip blob object header first time around. */
1475 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 err = got_error_from_errno("write");
1479 } else if (outlen != len - hdrlen) {
1480 err = got_error(GOT_ERR_IO);
1487 if (fsync(fd) != 0) {
1488 err = got_error_from_errno("fsync");
1493 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1494 err = got_error_from_errno2("unlink", ondisk_path);
1497 if (rename(tmppath, ondisk_path) != 0) {
1498 err = got_error_from_errno3("rename", tmppath,
1507 if (fd != -1 && close(fd) == -1 && err == NULL)
1508 err = got_error_from_errno("close");
1509 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1510 err = got_error_from_errno2("unlink", tmppath);
1516 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1517 * conflict marker is found in newly added lines only.
1519 static const struct got_error *
1520 get_modified_file_content_status(unsigned char *status,
1521 struct got_blob_object *blob, const char *path, struct stat *sb,
1524 const struct got_error *err, *free_err;
1525 const char *markers[3] = {
1526 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1527 GOT_DIFF_CONFLICT_MARKER_SEP,
1528 GOT_DIFF_CONFLICT_MARKER_END
1531 struct got_diffreg_result *diffreg_result = NULL;
1532 struct diff_result *r;
1533 int nchunks_parsed, n, i = 0, ln = 0;
1535 size_t linesize = 0;
1538 if (*status != GOT_STATUS_MODIFY)
1541 f1 = got_opentemp();
1543 return got_error_from_errno("got_opentemp");
1546 got_object_blob_rewind(blob);
1547 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1552 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1553 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1557 r = diffreg_result->result;
1559 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1560 struct diff_chunk *c;
1561 struct diff_chunk_context cc = {};
1565 * We can optimise a little by advancing straight
1566 * to the next chunk if this one has no added lines.
1568 c = diff_chunk_get(r, n);
1570 if (diff_chunk_type(c) != CHUNK_PLUS) {
1572 continue; /* removed or unchanged lines */
1575 pos = diff_chunk_get_right_start_pos(c);
1576 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1577 err = got_ferror(ondisk_file, GOT_ERR_IO);
1581 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1582 ln = cc.right.start;
1584 while (ln < cc.right.end) {
1585 linelen = getline(&line, &linesize, ondisk_file);
1586 if (linelen == -1) {
1587 if (feof(ondisk_file))
1589 err = got_ferror(ondisk_file, GOT_ERR_IO);
1593 if (line && strncmp(line, markers[i],
1594 strlen(markers[i])) == 0) {
1595 if (strcmp(markers[i],
1596 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1597 *status = GOT_STATUS_CONFLICT;
1608 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1609 err = got_error_from_errno("fclose");
1610 free_err = got_diffreg_result_free(diffreg_result);
1618 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1620 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1621 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1625 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1627 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1628 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1629 ie->mtime_sec == sb->st_mtim.tv_sec &&
1630 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1631 ie->size == (sb->st_size & 0xffffffff) &&
1632 !xbit_differs(ie, sb->st_mode));
1635 static unsigned char
1636 get_staged_status(struct got_fileindex_entry *ie)
1638 switch (got_fileindex_entry_stage_get(ie)) {
1639 case GOT_FILEIDX_STAGE_ADD:
1640 return GOT_STATUS_ADD;
1641 case GOT_FILEIDX_STAGE_DELETE:
1642 return GOT_STATUS_DELETE;
1643 case GOT_FILEIDX_STAGE_MODIFY:
1644 return GOT_STATUS_MODIFY;
1646 return GOT_STATUS_NO_CHANGE;
1650 static const struct got_error *
1651 get_symlink_modification_status(unsigned char *status,
1652 struct got_fileindex_entry *ie, const char *abspath,
1653 int dirfd, const char *de_name, struct got_blob_object *blob)
1655 const struct got_error *err = NULL;
1656 char target_path[PATH_MAX];
1657 char etarget[PATH_MAX];
1659 size_t len, target_len = 0;
1660 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1661 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1663 *status = GOT_STATUS_NO_CHANGE;
1665 /* Blob object content specifies the target path of the link. */
1667 err = got_object_blob_read_block(&len, blob);
1670 if (len + target_len >= sizeof(target_path)) {
1672 * Should not happen. The blob contents were OK
1673 * when this symlink was installed.
1675 return got_error(GOT_ERR_NO_SPACE);
1678 /* Skip blob object header first time around. */
1679 memcpy(target_path + target_len, buf + hdrlen,
1681 target_len += len - hdrlen;
1685 target_path[target_len] = '\0';
1688 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1690 return got_error_from_errno2("readlinkat", abspath);
1692 elen = readlink(abspath, etarget, sizeof(etarget));
1694 return got_error_from_errno2("readlink", abspath);
1697 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1698 *status = GOT_STATUS_MODIFY;
1703 static const struct got_error *
1704 get_file_status(unsigned char *status, struct stat *sb,
1705 struct got_fileindex_entry *ie, const char *abspath,
1706 int dirfd, const char *de_name, struct got_repository *repo)
1708 const struct got_error *err = NULL;
1709 struct got_object_id id;
1711 int fd = -1, fd1 = -1;
1714 struct got_blob_object *blob = NULL;
1716 unsigned char staged_status;
1718 staged_status = get_staged_status(ie);
1719 *status = GOT_STATUS_NO_CHANGE;
1720 memset(sb, 0, sizeof(*sb));
1723 * Whenever the caller provides a directory descriptor and a
1724 * directory entry name for the file, use them! This prevents
1725 * race conditions if filesystem paths change beneath our feet.
1728 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1729 if (errno == ENOENT) {
1730 if (got_fileindex_entry_has_file_on_disk(ie))
1731 *status = GOT_STATUS_MISSING;
1733 *status = GOT_STATUS_DELETE;
1736 err = got_error_from_errno2("fstatat", abspath);
1740 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1741 if (fd == -1 && errno != ENOENT &&
1742 !got_err_open_nofollow_on_symlink())
1743 return got_error_from_errno2("open", abspath);
1744 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1745 if (lstat(abspath, sb) == -1)
1746 return got_error_from_errno2("lstat", abspath);
1747 } else if (fd == -1 || fstat(fd, sb) == -1) {
1748 if (errno == ENOENT) {
1749 if (got_fileindex_entry_has_file_on_disk(ie))
1750 *status = GOT_STATUS_MISSING;
1752 *status = GOT_STATUS_DELETE;
1755 err = got_error_from_errno2("fstat", abspath);
1760 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1761 *status = GOT_STATUS_OBSTRUCTED;
1765 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1766 *status = GOT_STATUS_DELETE;
1768 } else if (!got_fileindex_entry_has_blob(ie) &&
1769 staged_status != GOT_STATUS_ADD) {
1770 *status = GOT_STATUS_ADD;
1774 if (!stat_info_differs(ie, sb))
1777 if (S_ISLNK(sb->st_mode) &&
1778 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1779 *status = GOT_STATUS_MODIFY;
1783 if (staged_status == GOT_STATUS_MODIFY ||
1784 staged_status == GOT_STATUS_ADD)
1785 got_fileindex_entry_get_staged_blob_id(&id, ie);
1787 got_fileindex_entry_get_blob_id(&id, ie);
1789 fd1 = got_opentempfd();
1791 err = got_error_from_errno("got_opentempfd");
1794 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1798 if (S_ISLNK(sb->st_mode)) {
1799 err = get_symlink_modification_status(status, ie,
1800 abspath, dirfd, de_name, blob);
1805 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1807 err = got_error_from_errno2("openat", abspath);
1812 f = fdopen(fd, "r");
1814 err = got_error_from_errno2("fdopen", abspath);
1818 hdrlen = got_object_blob_get_hdrlen(blob);
1820 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1821 err = got_object_blob_read_block(&blen, blob);
1824 /* Skip length of blob object header first time around. */
1825 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1826 if (flen == 0 && ferror(f)) {
1827 err = got_error_from_errno("fread");
1830 if (blen - hdrlen == 0) {
1832 *status = GOT_STATUS_MODIFY;
1834 } else if (flen == 0) {
1835 if (blen - hdrlen != 0)
1836 *status = GOT_STATUS_MODIFY;
1838 } else if (blen - hdrlen == flen) {
1839 /* Skip blob object header first time around. */
1840 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1841 *status = GOT_STATUS_MODIFY;
1845 *status = GOT_STATUS_MODIFY;
1851 if (*status == GOT_STATUS_MODIFY) {
1853 err = get_modified_file_content_status(status, blob, ie->path,
1855 } else if (xbit_differs(ie, sb->st_mode))
1856 *status = GOT_STATUS_MODE_CHANGE;
1858 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1859 err = got_error_from_errno("close");
1861 got_object_blob_close(blob);
1862 if (f != NULL && fclose(f) == EOF && err == NULL)
1863 err = got_error_from_errno2("fclose", abspath);
1864 if (fd != -1 && close(fd) == -1 && err == NULL)
1865 err = got_error_from_errno2("close", abspath);
1870 * Update timestamps in the file index if a file is unmodified and
1871 * we had to run a full content comparison to find out.
1873 static const struct got_error *
1874 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1875 struct got_fileindex_entry *ie, struct stat *sb)
1877 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1878 return got_fileindex_entry_update(ie, wt_fd, path,
1879 ie->blob_sha1, ie->commit_sha1, 1);
1884 static const struct got_error *
1885 update_blob(struct got_worktree *worktree,
1886 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1887 struct got_tree_entry *te, const char *path,
1888 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1891 const struct got_error *err = NULL;
1892 struct got_blob_object *blob = NULL;
1893 char *ondisk_path = NULL;
1894 unsigned char status = GOT_STATUS_NO_CHANGE;
1896 int fd1 = -1, fd2 = -1;
1898 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1899 return got_error_from_errno("asprintf");
1902 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1903 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1906 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1910 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1911 sb.st_mode = got_fileindex_perms_to_st(ie);
1913 if (stat(ondisk_path, &sb) == -1) {
1914 if (errno != ENOENT) {
1915 err = got_error_from_errno2("stat",
1919 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1920 status = GOT_STATUS_UNVERSIONED;
1922 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1923 status = GOT_STATUS_UNVERSIONED;
1925 status = GOT_STATUS_OBSTRUCTED;
1929 if (status == GOT_STATUS_OBSTRUCTED) {
1931 got_fileindex_entry_mark_skipped(ie);
1932 err = (*progress_cb)(progress_arg, status, path);
1935 if (status == GOT_STATUS_CONFLICT) {
1937 got_fileindex_entry_mark_skipped(ie);
1938 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1943 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1944 (S_ISLNK(te->mode) ||
1945 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1947 * This is a regular file or an installed bad symlink.
1948 * If the file index indicates that this file is already
1949 * up-to-date with respect to the repository we can skip
1950 * updating contents of this file.
1952 if (got_fileindex_entry_has_commit(ie) &&
1953 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1954 SHA1_DIGEST_LENGTH) == 0) {
1956 err = sync_timestamps(worktree->root_fd,
1957 path, status, ie, &sb);
1960 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1964 if (got_fileindex_entry_has_blob(ie) &&
1965 memcmp(ie->blob_sha1, te->id.sha1,
1966 SHA1_DIGEST_LENGTH) == 0) {
1967 /* Different commit but the same blob. */
1968 err = sync_timestamps(worktree->root_fd,
1969 path, status, ie, &sb);
1972 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1978 fd1 = got_opentempfd();
1980 err = got_error_from_errno("got_opentempfd");
1983 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1987 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1988 int update_timestamps;
1989 struct got_blob_object *blob2 = NULL;
1990 char *label_orig = NULL;
1991 if (got_fileindex_entry_has_blob(ie)) {
1992 fd2 = got_opentempfd();
1994 err = got_error_from_errno("got_opentempfd");
1997 struct got_object_id id2;
1998 got_fileindex_entry_get_blob_id(&id2, ie);
1999 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2004 if (got_fileindex_entry_has_commit(ie)) {
2005 char id_str[SHA1_DIGEST_STRING_LENGTH];
2006 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2007 sizeof(id_str)) == NULL) {
2008 err = got_error_path(id_str,
2009 GOT_ERR_BAD_OBJ_ID_STR);
2012 if (asprintf(&label_orig, "%s: commit %s",
2013 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2014 err = got_error_from_errno("asprintf");
2018 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2020 err = got_object_blob_read_to_str(&link_target, blob);
2023 err = merge_symlink(worktree, blob2, ondisk_path, path,
2024 label_orig, link_target, worktree->base_commit_id,
2025 repo, progress_cb, progress_arg);
2028 err = merge_blob(&update_timestamps, worktree, blob2,
2029 ondisk_path, path, sb.st_mode, label_orig, blob,
2030 worktree->base_commit_id, repo,
2031 progress_cb, progress_arg);
2034 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2035 err = got_error_from_errno("close");
2039 got_object_blob_close(blob2);
2043 * Do not update timestamps of files with local changes.
2044 * Otherwise, a future status walk would treat them as
2045 * unmodified files again.
2047 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2048 blob->id.sha1, worktree->base_commit_id->sha1,
2050 } else if (status == GOT_STATUS_MODE_CHANGE) {
2051 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2052 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2053 } else if (status == GOT_STATUS_DELETE) {
2054 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2057 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2058 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2062 int is_bad_symlink = 0;
2063 if (S_ISLNK(te->mode)) {
2064 err = install_symlink(&is_bad_symlink, worktree,
2065 ondisk_path, path, blob,
2066 status == GOT_STATUS_MISSING, 0,
2067 status == GOT_STATUS_UNVERSIONED, 0,
2068 repo, progress_cb, progress_arg);
2070 err = install_blob(worktree, ondisk_path, path,
2071 te->mode, sb.st_mode, blob,
2072 status == GOT_STATUS_MISSING, 0, 0,
2073 status == GOT_STATUS_UNVERSIONED, repo,
2074 progress_cb, progress_arg);
2080 err = got_fileindex_entry_update(ie,
2081 worktree->root_fd, path, blob->id.sha1,
2082 worktree->base_commit_id->sha1, 1);
2084 err = create_fileindex_entry(&ie, fileindex,
2085 worktree->base_commit_id, worktree->root_fd, path,
2091 if (is_bad_symlink) {
2092 got_fileindex_entry_filetype_set(ie,
2093 GOT_FILEIDX_MODE_BAD_SYMLINK);
2097 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2098 err = got_error_from_errno("close");
2101 got_object_blob_close(blob);
2107 static const struct got_error *
2108 remove_ondisk_file(const char *root_path, const char *path)
2110 const struct got_error *err = NULL;
2111 char *ondisk_path = NULL, *parent = NULL;
2113 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2114 return got_error_from_errno("asprintf");
2116 if (unlink(ondisk_path) == -1) {
2117 if (errno != ENOENT)
2118 err = got_error_from_errno2("unlink", ondisk_path);
2120 size_t root_len = strlen(root_path);
2121 err = got_path_dirname(&parent, ondisk_path);
2124 while (got_path_cmp(parent, root_path,
2125 strlen(parent), root_len) != 0) {
2127 ondisk_path = parent;
2129 if (rmdir(ondisk_path) == -1) {
2130 if (errno != ENOTEMPTY)
2131 err = got_error_from_errno2("rmdir",
2135 err = got_path_dirname(&parent, ondisk_path);
2146 static const struct got_error *
2147 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 struct got_fileindex_entry *ie, struct got_repository *repo,
2149 got_worktree_checkout_cb progress_cb, void *progress_arg)
2151 const struct got_error *err = NULL;
2152 unsigned char status;
2156 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2157 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2159 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2161 return got_error_from_errno("asprintf");
2163 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2167 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2168 char ondisk_target[PATH_MAX];
2169 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2170 sizeof(ondisk_target));
2171 if (ondisk_len == -1) {
2172 err = got_error_from_errno2("readlink", ondisk_path);
2175 ondisk_target[ondisk_len] = '\0';
2176 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2177 NULL, NULL, /* XXX pass common ancestor info? */
2178 ondisk_target, ondisk_path);
2181 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2186 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2187 status == GOT_STATUS_ADD) {
2188 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2192 * Preserve the working file and change the deleted blob's
2193 * entry into a schedule-add entry.
2195 err = got_fileindex_entry_update(ie, worktree->root_fd,
2196 ie->path, NULL, NULL, 0);
2198 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2201 if (status == GOT_STATUS_NO_CHANGE) {
2202 err = remove_ondisk_file(worktree->root_path, ie->path);
2206 got_fileindex_entry_remove(fileindex, ie);
2213 struct diff_cb_arg {
2214 struct got_fileindex *fileindex;
2215 struct got_worktree *worktree;
2216 struct got_repository *repo;
2217 got_worktree_checkout_cb progress_cb;
2219 got_cancel_cb cancel_cb;
2223 static const struct got_error *
2224 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2225 struct got_tree_entry *te, const char *parent_path)
2227 struct diff_cb_arg *a = arg;
2229 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2230 return got_error(GOT_ERR_CANCELLED);
2232 return update_blob(a->worktree, a->fileindex, ie, te,
2233 ie->path, a->repo, a->progress_cb, a->progress_arg);
2236 static const struct got_error *
2237 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2239 struct diff_cb_arg *a = arg;
2241 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 return got_error(GOT_ERR_CANCELLED);
2244 return delete_blob(a->worktree, a->fileindex, ie,
2245 a->repo, a->progress_cb, a->progress_arg);
2248 static const struct got_error *
2249 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2251 struct diff_cb_arg *a = arg;
2252 const struct got_error *err;
2255 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 return got_error(GOT_ERR_CANCELLED);
2258 if (got_object_tree_entry_is_submodule(te))
2261 if (asprintf(&path, "%s%s%s", parent_path,
2262 parent_path[0] ? "/" : "", te->name)
2264 return got_error_from_errno("asprintf");
2266 if (S_ISDIR(te->mode))
2267 err = add_dir_on_disk(a->worktree, path);
2269 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2270 a->repo, a->progress_cb, a->progress_arg);
2276 const struct got_error *
2277 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2279 uint32_t uuid_status;
2281 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2282 if (uuid_status != uuid_s_ok) {
2284 return got_error_uuid(uuid_status, "uuid_to_string");
2290 static const struct got_error *
2291 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2293 const struct got_error *err = NULL;
2294 char *uuidstr = NULL;
2298 err = got_worktree_get_uuid(&uuidstr, worktree);
2302 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2303 err = got_error_from_errno("asprintf");
2310 const struct got_error *
2311 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2314 return get_ref_name(refname, worktree, prefix);
2317 const struct got_error *
2318 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2320 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2323 static const struct got_error *
2324 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree,
2327 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2330 static const struct got_error *
2331 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2333 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2336 static const struct got_error *
2337 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2339 return get_ref_name(refname, worktree,
2340 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2343 static const struct got_error *
2344 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2346 return get_ref_name(refname, worktree,
2347 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2350 static const struct got_error *
2351 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree,
2354 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2357 static const struct got_error *
2358 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2360 return get_ref_name(refname, worktree,
2361 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2364 static const struct got_error *
2365 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2367 return get_ref_name(refname, worktree,
2368 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2371 static const struct got_error *
2372 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2374 return get_ref_name(refname, worktree,
2375 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2378 const struct got_error *
2379 got_worktree_get_histedit_script_path(char **path,
2380 struct got_worktree *worktree)
2382 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2383 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2385 return got_error_from_errno("asprintf");
2390 static const struct got_error *
2391 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2393 return get_ref_name(refname, worktree,
2394 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2397 static const struct got_error *
2398 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2400 return get_ref_name(refname, worktree,
2401 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2405 * Prevent Git's garbage collector from deleting our base commit by
2406 * setting a reference to our base commit's ID.
2408 static const struct got_error *
2409 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2411 const struct got_error *err = NULL;
2412 struct got_reference *ref = NULL;
2415 err = got_worktree_get_base_ref_name(&refname, worktree);
2419 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2423 err = got_ref_write(ref, repo);
2431 static const struct got_error *
2432 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2434 const struct got_error *err = NULL;
2436 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2437 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2438 err = got_error_from_errno("asprintf");
2439 *fileindex_path = NULL;
2445 static const struct got_error *
2446 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2447 struct got_worktree *worktree)
2449 const struct got_error *err = NULL;
2452 *fileindex_path = NULL;
2453 *fileindex = got_fileindex_alloc();
2454 if (*fileindex == NULL)
2455 return got_error_from_errno("got_fileindex_alloc");
2457 err = get_fileindex_path(fileindex_path, worktree);
2461 index = fopen(*fileindex_path, "rbe");
2462 if (index == NULL) {
2463 if (errno != ENOENT)
2464 err = got_error_from_errno2("fopen", *fileindex_path);
2466 err = got_fileindex_read(*fileindex, index);
2467 if (fclose(index) == EOF && err == NULL)
2468 err = got_error_from_errno("fclose");
2472 free(*fileindex_path);
2473 *fileindex_path = NULL;
2474 got_fileindex_free(*fileindex);
2480 struct bump_base_commit_id_arg {
2481 struct got_object_id *base_commit_id;
2484 const char *entry_name;
2485 got_worktree_checkout_cb progress_cb;
2489 static const struct got_error *
2490 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2492 const struct got_error *err;
2493 struct bump_base_commit_id_arg *a = arg;
2495 if (a->entry_name) {
2496 if (strcmp(ie->path, a->path) != 0)
2498 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2501 if (got_fileindex_entry_was_skipped(ie))
2504 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2505 SHA1_DIGEST_LENGTH) == 0)
2508 if (a->progress_cb) {
2509 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2514 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2518 /* Bump base commit ID of all files within an updated part of the work tree. */
2519 static const struct got_error *
2520 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2521 struct got_fileindex *fileindex,
2522 got_worktree_checkout_cb progress_cb, void *progress_arg)
2524 struct bump_base_commit_id_arg bbc_arg;
2526 bbc_arg.base_commit_id = worktree->base_commit_id;
2527 bbc_arg.entry_name = NULL;
2529 bbc_arg.path_len = 0;
2530 bbc_arg.progress_cb = progress_cb;
2531 bbc_arg.progress_arg = progress_arg;
2533 return got_fileindex_for_each_entry_safe(fileindex,
2534 bump_base_commit_id, &bbc_arg);
2537 static const struct got_error *
2538 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2540 const struct got_error *err = NULL;
2541 char *new_fileindex_path = NULL;
2542 FILE *new_index = NULL;
2543 struct timespec timeout;
2545 err = got_opentemp_named(&new_fileindex_path, &new_index,
2546 fileindex_path, "");
2550 err = got_fileindex_write(fileindex, new_index);
2554 if (rename(new_fileindex_path, fileindex_path) != 0) {
2555 err = got_error_from_errno3("rename", new_fileindex_path,
2557 unlink(new_fileindex_path);
2561 * Sleep for a short amount of time to ensure that files modified after
2562 * this program exits have a different time stamp from the one which
2563 * was recorded in the file index.
2566 timeout.tv_nsec = 1;
2567 nanosleep(&timeout, NULL);
2571 free(new_fileindex_path);
2575 static const struct got_error *
2576 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2577 struct got_object_id **tree_id, const char *wt_relpath,
2578 struct got_commit_object *base_commit, struct got_worktree *worktree,
2579 struct got_repository *repo)
2581 const struct got_error *err = NULL;
2582 struct got_object_id *id = NULL;
2583 char *in_repo_path = NULL;
2584 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2586 *entry_type = GOT_OBJ_TYPE_ANY;
2587 *tree_relpath = NULL;
2590 if (wt_relpath[0] == '\0') {
2591 /* Check out all files within the work tree. */
2592 *entry_type = GOT_OBJ_TYPE_TREE;
2593 *tree_relpath = strdup("");
2594 if (*tree_relpath == NULL) {
2595 err = got_error_from_errno("strdup");
2598 err = got_object_id_by_path(tree_id, repo, base_commit,
2599 worktree->path_prefix);
2605 /* Check out a subset of files in the work tree. */
2607 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2608 is_root_wt ? "" : "/", wt_relpath) == -1) {
2609 err = got_error_from_errno("asprintf");
2613 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2618 in_repo_path = NULL;
2620 err = got_object_get_type(entry_type, repo, id);
2624 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2625 /* Check out a single file. */
2626 if (strchr(wt_relpath, '/') == NULL) {
2627 /* Check out a single file in work tree's root dir. */
2628 in_repo_path = strdup(worktree->path_prefix);
2629 if (in_repo_path == NULL) {
2630 err = got_error_from_errno("strdup");
2633 *tree_relpath = strdup("");
2634 if (*tree_relpath == NULL) {
2635 err = got_error_from_errno("strdup");
2639 /* Check out a single file in a subdirectory. */
2640 err = got_path_dirname(tree_relpath, wt_relpath);
2643 if (asprintf(&in_repo_path, "%s%s%s",
2644 worktree->path_prefix, is_root_wt ? "" : "/",
2645 *tree_relpath) == -1) {
2646 err = got_error_from_errno("asprintf");
2650 err = got_object_id_by_path(tree_id, repo,
2651 base_commit, in_repo_path);
2653 /* Check out all files within a subdirectory. */
2654 *tree_id = got_object_id_dup(id);
2655 if (*tree_id == NULL) {
2656 err = got_error_from_errno("got_object_id_dup");
2659 *tree_relpath = strdup(wt_relpath);
2660 if (*tree_relpath == NULL) {
2661 err = got_error_from_errno("strdup");
2669 *entry_type = GOT_OBJ_TYPE_ANY;
2670 free(*tree_relpath);
2671 *tree_relpath = NULL;
2678 static const struct got_error *
2679 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2680 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2681 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2682 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2684 const struct got_error *err = NULL;
2685 struct got_commit_object *commit = NULL;
2686 struct got_tree_object *tree = NULL;
2687 struct got_fileindex_diff_tree_cb diff_cb;
2688 struct diff_cb_arg arg;
2690 err = ref_base_commit(worktree, repo);
2692 if (!(err->code == GOT_ERR_ERRNO &&
2693 (errno == EACCES || errno == EROFS)))
2695 err = (*progress_cb)(progress_arg,
2696 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2701 err = got_object_open_as_commit(&commit, repo,
2702 worktree->base_commit_id);
2706 err = got_object_open_as_tree(&tree, repo, tree_id);
2711 got_object_tree_find_entry(tree, entry_name) == NULL) {
2712 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2716 diff_cb.diff_old_new = diff_old_new;
2717 diff_cb.diff_old = diff_old;
2718 diff_cb.diff_new = diff_new;
2719 arg.fileindex = fileindex;
2720 arg.worktree = worktree;
2722 arg.progress_cb = progress_cb;
2723 arg.progress_arg = progress_arg;
2724 arg.cancel_cb = cancel_cb;
2725 arg.cancel_arg = cancel_arg;
2726 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2727 entry_name, repo, &diff_cb, &arg);
2730 got_object_tree_close(tree);
2732 got_object_commit_close(commit);
2736 const struct got_error *
2737 got_worktree_checkout_files(struct got_worktree *worktree,
2738 struct got_pathlist_head *paths, struct got_repository *repo,
2739 got_worktree_checkout_cb progress_cb, void *progress_arg,
2740 got_cancel_cb cancel_cb, void *cancel_arg)
2742 const struct got_error *err = NULL, *sync_err, *unlockerr;
2743 struct got_commit_object *commit = NULL;
2744 struct got_tree_object *tree = NULL;
2745 struct got_fileindex *fileindex = NULL;
2746 char *fileindex_path = NULL;
2747 struct got_pathlist_entry *pe;
2748 struct tree_path_data {
2749 STAILQ_ENTRY(tree_path_data) entry;
2750 struct got_object_id *tree_id;
2755 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2757 STAILQ_INIT(&tree_paths);
2759 err = lock_worktree(worktree, LOCK_EX);
2763 err = got_object_open_as_commit(&commit, repo,
2764 worktree->base_commit_id);
2768 /* Map all specified paths to in-repository trees. */
2769 TAILQ_FOREACH(pe, paths, entry) {
2770 tpd = malloc(sizeof(*tpd));
2772 err = got_error_from_errno("malloc");
2776 err = find_tree_entry_for_checkout(&tpd->entry_type,
2777 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2784 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2785 err = got_path_basename(&tpd->entry_name, pe->path);
2793 tpd->entry_name = NULL;
2795 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2799 * Read the file index.
2800 * Checking out files is supposed to be an idempotent operation.
2801 * If the on-disk file index is incomplete we will try to complete it.
2803 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2807 tpd = STAILQ_FIRST(&tree_paths);
2808 TAILQ_FOREACH(pe, paths, entry) {
2809 struct bump_base_commit_id_arg bbc_arg;
2811 err = checkout_files(worktree, fileindex, tpd->relpath,
2812 tpd->tree_id, tpd->entry_name, repo,
2813 progress_cb, progress_arg, cancel_cb, cancel_arg);
2817 bbc_arg.base_commit_id = worktree->base_commit_id;
2818 bbc_arg.entry_name = tpd->entry_name;
2819 bbc_arg.path = pe->path;
2820 bbc_arg.path_len = pe->path_len;
2821 bbc_arg.progress_cb = progress_cb;
2822 bbc_arg.progress_arg = progress_arg;
2823 err = got_fileindex_for_each_entry_safe(fileindex,
2824 bump_base_commit_id, &bbc_arg);
2828 tpd = STAILQ_NEXT(tpd, entry);
2830 sync_err = sync_fileindex(fileindex, fileindex_path);
2831 if (sync_err && err == NULL)
2834 free(fileindex_path);
2836 got_object_tree_close(tree);
2838 got_object_commit_close(commit);
2840 got_fileindex_free(fileindex);
2841 while (!STAILQ_EMPTY(&tree_paths)) {
2842 tpd = STAILQ_FIRST(&tree_paths);
2843 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2848 unlockerr = lock_worktree(worktree, LOCK_SH);
2849 if (unlockerr && err == NULL)
2854 struct merge_file_cb_arg {
2855 struct got_worktree *worktree;
2856 struct got_fileindex *fileindex;
2857 got_worktree_checkout_cb progress_cb;
2859 got_cancel_cb cancel_cb;
2861 const char *label_orig;
2862 struct got_object_id *commit_id2;
2863 int allow_bad_symlinks;
2866 static const struct got_error *
2867 merge_file_cb(void *arg, struct got_blob_object *blob1,
2868 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2869 struct got_object_id *id1, struct got_object_id *id2,
2870 const char *path1, const char *path2,
2871 mode_t mode1, mode_t mode2, struct got_repository *repo)
2873 static const struct got_error *err = NULL;
2874 struct merge_file_cb_arg *a = arg;
2875 struct got_fileindex_entry *ie;
2876 char *ondisk_path = NULL;
2878 unsigned char status;
2879 int local_changes_subsumed;
2880 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2881 char *id_str = NULL, *label_deriv2 = NULL;
2883 if (blob1 && blob2) {
2884 ie = got_fileindex_entry_get(a->fileindex, path2,
2887 return (*a->progress_cb)(a->progress_arg,
2888 GOT_STATUS_MISSING, path2);
2890 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2892 return got_error_from_errno("asprintf");
2894 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2899 if (status == GOT_STATUS_DELETE) {
2900 err = (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_MERGE, path2);
2904 if (status != GOT_STATUS_NO_CHANGE &&
2905 status != GOT_STATUS_MODIFY &&
2906 status != GOT_STATUS_CONFLICT &&
2907 status != GOT_STATUS_ADD) {
2908 err = (*a->progress_cb)(a->progress_arg, status, path2);
2912 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2914 err = got_object_blob_read_to_str(&link_target2, blob2);
2917 err = merge_symlink(a->worktree, blob1, ondisk_path,
2918 path2, a->label_orig, link_target2, a->commit_id2,
2919 repo, a->progress_cb, a->progress_arg);
2924 f_orig = got_opentemp();
2925 if (f_orig == NULL) {
2926 err = got_error_from_errno("got_opentemp");
2929 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2934 f_deriv2 = got_opentemp();
2935 if (f_deriv2 == NULL)
2937 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2942 fd = open(ondisk_path,
2943 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2945 err = got_error_from_errno2("open",
2949 f_deriv = fdopen(fd, "r");
2950 if (f_deriv == NULL) {
2951 err = got_error_from_errno2("fdopen",
2956 err = got_object_id_str(&id_str, a->commit_id2);
2959 if (asprintf(&label_deriv2, "%s: commit %s",
2960 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2961 err = got_error_from_errno("asprintf");
2964 err = merge_file(&local_changes_subsumed, a->worktree,
2965 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2966 mode2, a->label_orig, NULL, label_deriv2,
2967 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2968 a->progress_cb, a->progress_arg);
2971 ie = got_fileindex_entry_get(a->fileindex, path1,
2974 return (*a->progress_cb)(a->progress_arg,
2975 GOT_STATUS_MISSING, path1);
2977 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2979 return got_error_from_errno("asprintf");
2981 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2987 case GOT_STATUS_NO_CHANGE:
2988 err = (*a->progress_cb)(a->progress_arg,
2989 GOT_STATUS_DELETE, path1);
2992 err = remove_ondisk_file(a->worktree->root_path, path1);
2996 got_fileindex_entry_mark_deleted_from_disk(ie);
2998 case GOT_STATUS_DELETE:
2999 case GOT_STATUS_MISSING:
3000 err = (*a->progress_cb)(a->progress_arg,
3001 GOT_STATUS_DELETE, path1);
3005 got_fileindex_entry_mark_deleted_from_disk(ie);
3007 case GOT_STATUS_ADD: {
3008 struct got_object_id *id;
3012 * Delete the added file only if its content already
3013 * exists in the repository.
3015 err = got_object_blob_file_create(&id, &blob1_f,
3016 &blob1_size, path1);
3019 if (got_object_id_cmp(id, id1) == 0) {
3020 err = (*a->progress_cb)(a->progress_arg,
3021 GOT_STATUS_DELETE, path1);
3024 err = remove_ondisk_file(a->worktree->root_path,
3029 got_fileindex_entry_remove(a->fileindex,
3032 err = (*a->progress_cb)(a->progress_arg,
3033 GOT_STATUS_CANNOT_DELETE, path1);
3035 if (fclose(blob1_f) == EOF && err == NULL)
3036 err = got_error_from_errno("fclose");
3042 case GOT_STATUS_MODIFY:
3043 case GOT_STATUS_CONFLICT:
3044 err = (*a->progress_cb)(a->progress_arg,
3045 GOT_STATUS_CANNOT_DELETE, path1);
3049 case GOT_STATUS_OBSTRUCTED:
3050 err = (*a->progress_cb)(a->progress_arg, status, path1);
3058 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3060 return got_error_from_errno("asprintf");
3061 ie = got_fileindex_entry_get(a->fileindex, path2,
3064 err = get_file_status(&status, &sb, ie, ondisk_path,
3068 if (status != GOT_STATUS_NO_CHANGE &&
3069 status != GOT_STATUS_MODIFY &&
3070 status != GOT_STATUS_CONFLICT &&
3071 status != GOT_STATUS_ADD) {
3072 err = (*a->progress_cb)(a->progress_arg,
3076 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3078 err = got_object_blob_read_to_str(&link_target2,
3082 err = merge_symlink(a->worktree, NULL,
3083 ondisk_path, path2, a->label_orig,
3084 link_target2, a->commit_id2, repo,
3085 a->progress_cb, a->progress_arg);
3087 } else if (S_ISREG(sb.st_mode)) {
3088 err = merge_blob(&local_changes_subsumed,
3089 a->worktree, NULL, ondisk_path, path2,
3090 sb.st_mode, a->label_orig, blob2,
3091 a->commit_id2, repo, a->progress_cb,
3094 err = got_error_path(ondisk_path,
3095 GOT_ERR_FILE_OBSTRUCTED);
3099 if (status == GOT_STATUS_DELETE) {
3100 err = got_fileindex_entry_update(ie,
3101 a->worktree->root_fd, path2, blob2->id.sha1,
3102 a->worktree->base_commit_id->sha1, 0);
3107 int is_bad_symlink = 0;
3108 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3109 if (S_ISLNK(mode2)) {
3110 err = install_symlink(&is_bad_symlink,
3111 a->worktree, ondisk_path, path2, blob2, 0,
3112 0, 1, a->allow_bad_symlinks, repo,
3113 a->progress_cb, a->progress_arg);
3115 err = install_blob(a->worktree, ondisk_path, path2,
3116 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3117 a->progress_cb, a->progress_arg);
3121 err = got_fileindex_entry_alloc(&ie, path2);
3124 err = got_fileindex_entry_update(ie,
3125 a->worktree->root_fd, path2, NULL, NULL, 1);
3127 got_fileindex_entry_free(ie);
3130 err = got_fileindex_entry_add(a->fileindex, ie);
3132 got_fileindex_entry_free(ie);
3135 if (is_bad_symlink) {
3136 got_fileindex_entry_filetype_set(ie,
3137 GOT_FILEIDX_MODE_BAD_SYMLINK);
3142 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3143 err = got_error_from_errno("fclose");
3144 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3145 err = got_error_from_errno("fclose");
3146 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3147 err = got_error_from_errno("fclose");
3154 static const struct got_error *
3155 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3157 struct got_worktree *worktree = arg;
3159 /* Reject merges into a work tree with mixed base commits. */
3160 if (got_fileindex_entry_has_commit(ie) &&
3161 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3162 SHA1_DIGEST_LENGTH) != 0)
3163 return got_error(GOT_ERR_MIXED_COMMITS);
3168 struct check_merge_conflicts_arg {
3169 struct got_worktree *worktree;
3170 struct got_fileindex *fileindex;
3171 struct got_repository *repo;
3174 static const struct got_error *
3175 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3176 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3177 struct got_object_id *id1, struct got_object_id *id2,
3178 const char *path1, const char *path2,
3179 mode_t mode1, mode_t mode2, struct got_repository *repo)
3181 const struct got_error *err = NULL;
3182 struct check_merge_conflicts_arg *a = arg;
3183 unsigned char status;
3185 struct got_fileindex_entry *ie;
3186 const char *path = path2 ? path2 : path1;
3187 struct got_object_id *id = id2 ? id2 : id1;
3193 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3197 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3199 return got_error_from_errno("asprintf");
3201 /* Reject merges into a work tree with conflicted files. */
3202 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3206 if (status == GOT_STATUS_CONFLICT)
3207 return got_error(GOT_ERR_CONFLICTS);
3212 static const struct got_error *
3213 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3214 const char *fileindex_path, struct got_object_id *commit_id1,
3215 struct got_object_id *commit_id2, struct got_repository *repo,
3216 got_worktree_checkout_cb progress_cb, void *progress_arg,
3217 got_cancel_cb cancel_cb, void *cancel_arg)
3219 const struct got_error *err = NULL, *sync_err;
3220 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3221 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3222 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3223 struct check_merge_conflicts_arg cmc_arg;
3224 struct merge_file_cb_arg arg;
3225 char *label_orig = NULL;
3226 FILE *f1 = NULL, *f2 = NULL;
3227 int fd1 = -1, fd2 = -1;
3230 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3233 err = got_object_id_by_path(&tree_id1, repo, commit1,
3234 worktree->path_prefix);
3235 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3241 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3245 err = got_object_id_str(&id_str, commit_id1);
3249 if (asprintf(&label_orig, "%s: commit %s",
3250 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3251 err = got_error_from_errno("asprintf");
3257 f1 = got_opentemp();
3259 err = got_error_from_errno("got_opentemp");
3264 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3268 err = got_object_id_by_path(&tree_id2, repo, commit2,
3269 worktree->path_prefix);
3273 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3277 f2 = got_opentemp();
3279 err = got_error_from_errno("got_opentemp");
3283 fd1 = got_opentempfd();
3285 err = got_error_from_errno("got_opentempfd");
3289 fd2 = got_opentempfd();
3291 err = got_error_from_errno("got_opentempfd");
3295 cmc_arg.worktree = worktree;
3296 cmc_arg.fileindex = fileindex;
3297 cmc_arg.repo = repo;
3298 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3299 check_merge_conflicts, &cmc_arg, 0);
3303 arg.worktree = worktree;
3304 arg.fileindex = fileindex;
3305 arg.progress_cb = progress_cb;
3306 arg.progress_arg = progress_arg;
3307 arg.cancel_cb = cancel_cb;
3308 arg.cancel_arg = cancel_arg;
3309 arg.label_orig = label_orig;
3310 arg.commit_id2 = commit_id2;
3311 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3312 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3313 merge_file_cb, &arg, 1);
3314 sync_err = sync_fileindex(fileindex, fileindex_path);
3315 if (sync_err && err == NULL)
3319 got_object_commit_close(commit1);
3321 got_object_commit_close(commit2);
3323 got_object_tree_close(tree1);
3325 got_object_tree_close(tree2);
3326 if (f1 && fclose(f1) == EOF && err == NULL)
3327 err = got_error_from_errno("fclose");
3328 if (f2 && fclose(f2) == EOF && err == NULL)
3329 err = got_error_from_errno("fclose");
3330 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3331 err = got_error_from_errno("close");
3332 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3333 err = got_error_from_errno("close");
3338 const struct got_error *
3339 got_worktree_merge_files(struct got_worktree *worktree,
3340 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3341 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3342 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3344 const struct got_error *err, *unlockerr;
3345 char *fileindex_path = NULL;
3346 struct got_fileindex *fileindex = NULL;
3348 err = lock_worktree(worktree, LOCK_EX);
3352 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3356 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3361 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3362 commit_id2, repo, progress_cb, progress_arg,
3363 cancel_cb, cancel_arg);
3366 got_fileindex_free(fileindex);
3367 free(fileindex_path);
3368 unlockerr = lock_worktree(worktree, LOCK_SH);
3369 if (unlockerr && err == NULL)
3374 struct diff_dir_cb_arg {
3375 struct got_fileindex *fileindex;
3376 struct got_worktree *worktree;
3377 const char *status_path;
3378 size_t status_path_len;
3379 struct got_repository *repo;
3380 got_worktree_status_cb status_cb;
3382 got_cancel_cb cancel_cb;
3384 /* A pathlist containing per-directory pathlists of ignore patterns. */
3385 struct got_pathlist_head *ignores;
3386 int report_unchanged;
3390 static const struct got_error *
3391 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3392 int dirfd, const char *de_name,
3393 got_worktree_status_cb status_cb, void *status_arg,
3394 struct got_repository *repo, int report_unchanged)
3396 const struct got_error *err = NULL;
3397 unsigned char status = GOT_STATUS_NO_CHANGE;
3398 unsigned char staged_status;
3400 struct got_object_id blob_id, commit_id, staged_blob_id;
3401 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3402 struct got_object_id *staged_blob_idp = NULL;
3404 staged_status = get_staged_status(ie);
3405 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3409 if (status == GOT_STATUS_NO_CHANGE &&
3410 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3413 if (got_fileindex_entry_has_blob(ie))
3414 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3415 if (got_fileindex_entry_has_commit(ie))
3416 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3417 if (staged_status == GOT_STATUS_ADD ||
3418 staged_status == GOT_STATUS_MODIFY) {
3419 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3420 &staged_blob_id, ie);
3423 return (*status_cb)(status_arg, status, staged_status,
3424 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3427 static const struct got_error *
3428 status_old_new(void *arg, struct got_fileindex_entry *ie,
3429 struct dirent *de, const char *parent_path, int dirfd)
3431 const struct got_error *err = NULL;
3432 struct diff_dir_cb_arg *a = arg;
3435 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3436 return got_error(GOT_ERR_CANCELLED);
3438 if (got_path_cmp(parent_path, a->status_path,
3439 strlen(parent_path), a->status_path_len) != 0 &&
3440 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3443 if (parent_path[0]) {
3444 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3445 parent_path, de->d_name) == -1)
3446 return got_error_from_errno("asprintf");
3448 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3450 return got_error_from_errno("asprintf");
3453 err = report_file_status(ie, abspath, dirfd, de->d_name,
3454 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3459 static const struct got_error *
3460 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3462 struct diff_dir_cb_arg *a = arg;
3463 struct got_object_id blob_id, commit_id;
3464 unsigned char status;
3466 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3467 return got_error(GOT_ERR_CANCELLED);
3469 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3472 got_fileindex_entry_get_blob_id(&blob_id, ie);
3473 got_fileindex_entry_get_commit_id(&commit_id, ie);
3474 if (got_fileindex_entry_has_file_on_disk(ie))
3475 status = GOT_STATUS_MISSING;
3477 status = GOT_STATUS_DELETE;
3478 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3479 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3483 free_ignores(struct got_pathlist_head *ignores)
3485 struct got_pathlist_entry *pe;
3487 TAILQ_FOREACH(pe, ignores, entry) {
3488 struct got_pathlist_head *ignorelist = pe->data;
3490 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3492 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3495 static const struct got_error *
3496 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3498 const struct got_error *err = NULL;
3499 struct got_pathlist_entry *pe = NULL;
3500 struct got_pathlist_head *ignorelist;
3501 char *line = NULL, *pattern, *dirpath = NULL;
3502 size_t linesize = 0;
3505 ignorelist = calloc(1, sizeof(*ignorelist));
3506 if (ignorelist == NULL)
3507 return got_error_from_errno("calloc");
3508 TAILQ_INIT(ignorelist);
3510 while ((linelen = getline(&line, &linesize, f)) != -1) {
3511 if (linelen > 0 && line[linelen - 1] == '\n')
3512 line[linelen - 1] = '\0';
3514 /* Git's ignores may contain comments. */
3518 /* Git's negated patterns are not (yet?) supported. */
3522 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3524 err = got_error_from_errno("asprintf");
3527 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3532 err = got_error_from_errno("getline");
3536 dirpath = strdup(path);
3537 if (dirpath == NULL) {
3538 err = got_error_from_errno("strdup");
3541 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3544 if (err || pe == NULL) {
3546 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3552 match_path(const char *pattern, size_t pattern_len, const char *path,
3558 * Trailing slashes signify directories.
3559 * Append a * to make such patterns conform to fnmatch rules.
3561 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3562 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3563 return FNM_NOMATCH; /* XXX */
3565 return fnmatch(buf, path, flags);
3568 return fnmatch(pattern, path, flags);
3572 match_ignores(struct got_pathlist_head *ignores, const char *path)
3574 struct got_pathlist_entry *pe;
3576 /* Handle patterns which match in all directories. */
3577 TAILQ_FOREACH(pe, ignores, entry) {
3578 struct got_pathlist_head *ignorelist = pe->data;
3579 struct got_pathlist_entry *pi;
3581 TAILQ_FOREACH(pi, ignorelist, entry) {
3584 if (pi->path_len < 3 ||
3585 strncmp(pi->path, "**/", 3) != 0)
3589 if (match_path(pi->path + 3,
3590 pi->path_len - 3, p,
3591 FNM_PATHNAME | FNM_LEADING_DIR)) {
3592 /* Retry in next directory. */
3593 while (*p && *p != '/')
3605 * The ignores pathlist contains ignore lists from children before
3606 * parents, so we can find the most specific ignorelist by walking
3607 * ignores backwards.
3609 pe = TAILQ_LAST(ignores, got_pathlist_head);
3611 if (got_path_is_child(path, pe->path, pe->path_len)) {
3612 struct got_pathlist_head *ignorelist = pe->data;
3613 struct got_pathlist_entry *pi;
3614 TAILQ_FOREACH(pi, ignorelist, entry) {
3615 int flags = FNM_LEADING_DIR;
3616 if (strstr(pi->path, "/**/") == NULL)
3617 flags |= FNM_PATHNAME;
3618 if (match_path(pi->path, pi->path_len,
3624 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3630 static const struct got_error *
3631 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3632 const char *path, int dirfd, const char *ignores_filename)
3634 const struct got_error *err = NULL;
3637 FILE *ignoresfile = NULL;
3639 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3640 path[0] ? "/" : "", ignores_filename) == -1)
3641 return got_error_from_errno("asprintf");
3644 fd = openat(dirfd, ignores_filename,
3645 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3647 if (errno != ENOENT && errno != EACCES)
3648 err = got_error_from_errno2("openat",
3651 ignoresfile = fdopen(fd, "r");
3652 if (ignoresfile == NULL)
3653 err = got_error_from_errno2("fdopen",
3657 err = read_ignores(ignores, path, ignoresfile);
3661 ignoresfile = fopen(ignorespath, "re");
3662 if (ignoresfile == NULL) {
3663 if (errno != ENOENT && errno != EACCES)
3664 err = got_error_from_errno2("fopen",
3667 err = read_ignores(ignores, path, ignoresfile);
3670 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3671 err = got_error_from_errno2("fclose", path);
3672 if (fd != -1 && close(fd) == -1 && err == NULL)
3673 err = got_error_from_errno2("close", path);
3678 static const struct got_error *
3679 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3682 const struct got_error *err = NULL;
3683 struct diff_dir_cb_arg *a = arg;
3689 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3690 return got_error(GOT_ERR_CANCELLED);
3692 if (parent_path[0]) {
3693 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3694 return got_error_from_errno("asprintf");
3699 if (de->d_type == DT_DIR) {
3700 if (!a->no_ignores && ignore != NULL &&
3701 match_ignores(a->ignores, path))
3703 } else if (!match_ignores(a->ignores, path) &&
3704 got_path_is_child(path, a->status_path, a->status_path_len))
3705 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3706 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3712 static const struct got_error *
3713 status_traverse(void *arg, const char *path, int dirfd)
3715 const struct got_error *err = NULL;
3716 struct diff_dir_cb_arg *a = arg;
3721 err = add_ignores(a->ignores, a->worktree->root_path,
3722 path, dirfd, ".cvsignore");
3726 err = add_ignores(a->ignores, a->worktree->root_path, path,
3727 dirfd, ".gitignore");
3732 static const struct got_error *
3733 report_single_file_status(const char *path, const char *ondisk_path,
3734 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3735 void *status_arg, struct got_repository *repo, int report_unchanged,
3736 struct got_pathlist_head *ignores, int no_ignores)
3738 struct got_fileindex_entry *ie;
3741 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3743 return report_file_status(ie, ondisk_path, -1, NULL,
3744 status_cb, status_arg, repo, report_unchanged);
3746 if (lstat(ondisk_path, &sb) == -1) {
3747 if (errno != ENOENT)
3748 return got_error_from_errno2("lstat", ondisk_path);
3749 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3750 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3753 if (!no_ignores && match_ignores(ignores, path))
3756 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3757 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3758 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3763 static const struct got_error *
3764 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3765 const char *root_path, const char *path)
3767 const struct got_error *err;
3768 char *parent_path, *next_parent_path = NULL;
3770 err = add_ignores(ignores, root_path, "", -1,
3775 err = add_ignores(ignores, root_path, "", -1,
3780 err = got_path_dirname(&parent_path, path);
3782 if (err->code == GOT_ERR_BAD_PATH)
3783 return NULL; /* cannot traverse parent */
3787 err = add_ignores(ignores, root_path, parent_path, -1,
3791 err = add_ignores(ignores, root_path, parent_path, -1,
3795 err = got_path_dirname(&next_parent_path, parent_path);
3797 if (err->code == GOT_ERR_BAD_PATH)
3798 err = NULL; /* traversed everything */
3801 if (got_path_is_root_dir(parent_path))
3804 parent_path = next_parent_path;
3805 next_parent_path = NULL;
3809 free(next_parent_path);
3813 static const struct got_error *
3814 worktree_status(struct got_worktree *worktree, const char *path,
3815 struct got_fileindex *fileindex, struct got_repository *repo,
3816 got_worktree_status_cb status_cb, void *status_arg,
3817 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3818 int report_unchanged)
3820 const struct got_error *err = NULL;
3822 struct got_fileindex_diff_dir_cb fdiff_cb;
3823 struct diff_dir_cb_arg arg;
3824 char *ondisk_path = NULL;
3825 struct got_pathlist_head ignores;
3826 struct got_fileindex_entry *ie;
3828 TAILQ_INIT(&ignores);
3830 if (asprintf(&ondisk_path, "%s%s%s",
3831 worktree->root_path, path[0] ? "/" : "", path) == -1)
3832 return got_error_from_errno("asprintf");
3834 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3836 err = report_single_file_status(path, ondisk_path,
3837 fileindex, status_cb, status_arg, repo,
3838 report_unchanged, &ignores, no_ignores);
3842 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3844 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3845 !got_err_open_nofollow_on_symlink())
3846 err = got_error_from_errno2("open", ondisk_path);
3849 err = add_ignores_from_parent_paths(&ignores,
3850 worktree->root_path, ondisk_path);
3854 err = report_single_file_status(path, ondisk_path,
3855 fileindex, status_cb, status_arg, repo,
3856 report_unchanged, &ignores, no_ignores);
3859 fdiff_cb.diff_old_new = status_old_new;
3860 fdiff_cb.diff_old = status_old;
3861 fdiff_cb.diff_new = status_new;
3862 fdiff_cb.diff_traverse = status_traverse;
3863 arg.fileindex = fileindex;
3864 arg.worktree = worktree;
3865 arg.status_path = path;
3866 arg.status_path_len = strlen(path);
3868 arg.status_cb = status_cb;
3869 arg.status_arg = status_arg;
3870 arg.cancel_cb = cancel_cb;
3871 arg.cancel_arg = cancel_arg;
3872 arg.report_unchanged = report_unchanged;
3873 arg.no_ignores = no_ignores;
3875 err = add_ignores_from_parent_paths(&ignores,
3876 worktree->root_path, path);
3880 arg.ignores = &ignores;
3881 err = got_fileindex_diff_dir(fileindex, fd,
3882 worktree->root_path, path, repo, &fdiff_cb, &arg);
3885 free_ignores(&ignores);
3886 if (fd != -1 && close(fd) == -1 && err == NULL)
3887 err = got_error_from_errno("close");
3892 const struct got_error *
3893 got_worktree_status(struct got_worktree *worktree,
3894 struct got_pathlist_head *paths, struct got_repository *repo,
3895 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3896 got_cancel_cb cancel_cb, void *cancel_arg)
3898 const struct got_error *err = NULL;
3899 char *fileindex_path = NULL;
3900 struct got_fileindex *fileindex = NULL;
3901 struct got_pathlist_entry *pe;
3903 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3907 TAILQ_FOREACH(pe, paths, entry) {
3908 err = worktree_status(worktree, pe->path, fileindex, repo,
3909 status_cb, status_arg, cancel_cb, cancel_arg,
3914 free(fileindex_path);
3915 got_fileindex_free(fileindex);
3919 const struct got_error *
3920 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3923 const struct got_error *err = NULL;
3924 char *resolved = NULL, *cwd = NULL, *path = NULL;
3927 char *abspath = NULL;
3928 char canonpath[PATH_MAX];
3932 cwd = getcwd(NULL, 0);
3934 return got_error_from_errno("getcwd");
3936 if (lstat(arg, &sb) == -1) {
3937 if (errno != ENOENT) {
3938 err = got_error_from_errno2("lstat", arg);
3943 if (S_ISLNK(sb.st_mode)) {
3945 * We cannot use realpath(3) with symlinks since we want to
3946 * operate on the symlink itself.
3947 * But we can make the path absolute, assuming it is relative
3948 * to the current working directory, and then canonicalize it.
3950 if (!got_path_is_absolute(arg)) {
3951 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3952 err = got_error_from_errno("asprintf");
3957 err = got_canonpath(abspath ? abspath : arg, canonpath,
3961 resolved = strdup(canonpath);
3962 if (resolved == NULL) {
3963 err = got_error_from_errno("strdup");
3967 resolved = realpath(arg, NULL);
3968 if (resolved == NULL) {
3969 if (errno != ENOENT) {
3970 err = got_error_from_errno2("realpath", arg);
3973 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3974 err = got_error_from_errno("asprintf");
3977 err = got_canonpath(abspath, canonpath,
3981 resolved = strdup(canonpath);
3982 if (resolved == NULL) {
3983 err = got_error_from_errno("strdup");
3989 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3990 strlen(got_worktree_get_root_path(worktree)))) {
3991 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3995 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3996 err = got_path_skip_common_ancestor(&path,
3997 got_worktree_get_root_path(worktree), resolved);
4003 err = got_error_from_errno("strdup");
4008 /* XXX status walk can't deal with trailing slash! */
4010 while (len > 0 && path[len - 1] == '/') {
4011 path[len - 1] = '\0';
4025 struct schedule_addition_args {
4026 struct got_worktree *worktree;
4027 struct got_fileindex *fileindex;
4028 got_worktree_checkout_cb progress_cb;
4030 struct got_repository *repo;
4033 static const struct got_error *
4034 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4035 const char *relpath, struct got_object_id *blob_id,
4036 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4037 int dirfd, const char *de_name)
4039 struct schedule_addition_args *a = arg;
4040 const struct got_error *err = NULL;
4041 struct got_fileindex_entry *ie;
4045 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4047 return got_error_from_errno("asprintf");
4049 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4051 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4055 /* Re-adding an existing entry is a no-op. */
4056 if (status == GOT_STATUS_ADD)
4058 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4063 if (status != GOT_STATUS_UNVERSIONED) {
4064 if (status == GOT_STATUS_NONEXISTENT)
4065 err = got_error_set_errno(ENOENT, ondisk_path);
4067 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4071 err = got_fileindex_entry_alloc(&ie, relpath);
4074 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4075 relpath, NULL, NULL, 1);
4077 got_fileindex_entry_free(ie);
4080 err = got_fileindex_entry_add(a->fileindex, ie);
4082 got_fileindex_entry_free(ie);
4089 if (status == GOT_STATUS_ADD)
4091 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4094 const struct got_error *
4095 got_worktree_schedule_add(struct got_worktree *worktree,
4096 struct got_pathlist_head *paths,
4097 got_worktree_checkout_cb progress_cb, void *progress_arg,
4098 struct got_repository *repo, int no_ignores)
4100 struct got_fileindex *fileindex = NULL;
4101 char *fileindex_path = NULL;
4102 const struct got_error *err = NULL, *sync_err, *unlockerr;
4103 struct got_pathlist_entry *pe;
4104 struct schedule_addition_args saa;
4106 err = lock_worktree(worktree, LOCK_EX);
4110 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4114 saa.worktree = worktree;
4115 saa.fileindex = fileindex;
4116 saa.progress_cb = progress_cb;
4117 saa.progress_arg = progress_arg;
4120 TAILQ_FOREACH(pe, paths, entry) {
4121 err = worktree_status(worktree, pe->path, fileindex, repo,
4122 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4126 sync_err = sync_fileindex(fileindex, fileindex_path);
4127 if (sync_err && err == NULL)
4130 free(fileindex_path);
4132 got_fileindex_free(fileindex);
4133 unlockerr = lock_worktree(worktree, LOCK_SH);
4134 if (unlockerr && err == NULL)
4139 struct schedule_deletion_args {
4140 struct got_worktree *worktree;
4141 struct got_fileindex *fileindex;
4142 got_worktree_delete_cb progress_cb;
4144 struct got_repository *repo;
4145 int delete_local_mods;
4147 int ignore_missing_paths;
4148 const char *status_codes;
4151 static const struct got_error *
4152 schedule_for_deletion(void *arg, unsigned char status,
4153 unsigned char staged_status, const char *relpath,
4154 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4155 struct got_object_id *commit_id, int dirfd, const char *de_name)
4157 struct schedule_deletion_args *a = arg;
4158 const struct got_error *err = NULL;
4159 struct got_fileindex_entry *ie = NULL;
4163 if (status == GOT_STATUS_NONEXISTENT) {
4164 if (a->ignore_missing_paths)
4166 return got_error_set_errno(ENOENT, relpath);
4169 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4171 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4173 staged_status = get_staged_status(ie);
4174 if (staged_status != GOT_STATUS_NO_CHANGE) {
4175 if (staged_status == GOT_STATUS_DELETE)
4177 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4180 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4182 return got_error_from_errno("asprintf");
4184 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4189 if (a->status_codes) {
4190 size_t ncodes = strlen(a->status_codes);
4192 for (i = 0; i < ncodes ; i++) {
4193 if (status == a->status_codes[i])
4197 /* Do not delete files in non-matching status. */
4201 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4202 a->status_codes[i] != GOT_STATUS_MISSING) {
4203 static char msg[64];
4204 snprintf(msg, sizeof(msg),
4205 "invalid status code '%c'", a->status_codes[i]);
4206 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4211 if (status != GOT_STATUS_NO_CHANGE) {
4212 if (status == GOT_STATUS_DELETE)
4214 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4215 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4218 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4219 err = got_error_set_errno(ENOENT, relpath);
4222 if (status != GOT_STATUS_MODIFY &&
4223 status != GOT_STATUS_MISSING) {
4224 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4229 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4233 if (unlinkat(dirfd, de_name, 0) == -1) {
4234 err = got_error_from_errno2("unlinkat",
4238 } else if (unlink(ondisk_path) == -1) {
4239 err = got_error_from_errno2("unlink", ondisk_path);
4243 root_len = strlen(a->worktree->root_path);
4246 err = got_path_dirname(&parent, ondisk_path);
4250 ondisk_path = parent;
4251 if (rmdir(ondisk_path) == -1) {
4252 if (errno != ENOTEMPTY)
4253 err = got_error_from_errno2("rmdir",
4257 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4258 strlen(ondisk_path), root_len) != 0);
4261 got_fileindex_entry_mark_deleted_from_disk(ie);
4266 if (status == GOT_STATUS_DELETE)
4268 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4269 staged_status, relpath);
4272 const struct got_error *
4273 got_worktree_schedule_delete(struct got_worktree *worktree,
4274 struct got_pathlist_head *paths, int delete_local_mods,
4275 const char *status_codes,
4276 got_worktree_delete_cb progress_cb, void *progress_arg,
4277 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4279 struct got_fileindex *fileindex = NULL;
4280 char *fileindex_path = NULL;
4281 const struct got_error *err = NULL, *sync_err, *unlockerr;
4282 struct got_pathlist_entry *pe;
4283 struct schedule_deletion_args sda;
4285 err = lock_worktree(worktree, LOCK_EX);
4289 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4293 sda.worktree = worktree;
4294 sda.fileindex = fileindex;
4295 sda.progress_cb = progress_cb;
4296 sda.progress_arg = progress_arg;
4298 sda.delete_local_mods = delete_local_mods;
4299 sda.keep_on_disk = keep_on_disk;
4300 sda.ignore_missing_paths = ignore_missing_paths;
4301 sda.status_codes = status_codes;
4303 TAILQ_FOREACH(pe, paths, entry) {
4304 err = worktree_status(worktree, pe->path, fileindex, repo,
4305 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4309 sync_err = sync_fileindex(fileindex, fileindex_path);
4310 if (sync_err && err == NULL)
4313 free(fileindex_path);
4315 got_fileindex_free(fileindex);
4316 unlockerr = lock_worktree(worktree, LOCK_SH);
4317 if (unlockerr && err == NULL)
4322 static const struct got_error *
4323 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4325 const struct got_error *err = NULL;
4327 size_t linesize = 0, n;
4330 linelen = getline(&line, &linesize, infile);
4331 if (linelen == -1) {
4332 if (ferror(infile)) {
4333 err = got_error_from_errno("getline");
4339 n = fwrite(line, 1, linelen, outfile);
4341 err = got_ferror(outfile, GOT_ERR_IO);
4346 n = fwrite(line, 1, linelen, rejectfile);
4348 err = got_ferror(rejectfile, GOT_ERR_IO);
4355 static const struct got_error *
4356 skip_one_line(FILE *f)
4359 size_t linesize = 0;
4362 linelen = getline(&line, &linesize, f);
4363 if (linelen == -1) {
4365 return got_error_from_errno("getline");
4372 static const struct got_error *
4373 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4374 int start_old, int end_old, int start_new, int end_new,
4375 FILE *outfile, FILE *rejectfile)
4377 const struct got_error *err;
4379 /* Copy old file's lines leading up to patch. */
4380 while (!feof(f1) && *line_cur1 < start_old) {
4381 err = copy_one_line(f1, outfile, NULL);
4386 /* Skip new file's lines leading up to patch. */
4387 while (!feof(f2) && *line_cur2 < start_new) {
4389 err = copy_one_line(f2, NULL, rejectfile);
4391 err = skip_one_line(f2);
4396 /* Copy patched lines. */
4397 while (!feof(f2) && *line_cur2 <= end_new) {
4398 err = copy_one_line(f2, outfile, NULL);
4403 /* Skip over old file's replaced lines. */
4404 while (!feof(f1) && *line_cur1 <= end_old) {
4406 err = copy_one_line(f1, NULL, rejectfile);
4408 err = skip_one_line(f1);
4417 static const struct got_error *
4418 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4419 FILE *outfile, FILE *rejectfile)
4421 const struct got_error *err;
4424 /* Copy old file's lines until EOF. */
4426 err = copy_one_line(f1, outfile, NULL);
4433 /* Copy new file's lines until EOF. */
4435 err = copy_one_line(f2, NULL, rejectfile);
4445 static const struct got_error *
4446 apply_or_reject_change(int *choice, int *nchunks_used,
4447 struct diff_result *diff_result, int n,
4448 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4449 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4450 got_worktree_patch_cb patch_cb, void *patch_arg)
4452 const struct got_error *err = NULL;
4453 struct diff_chunk_context cc = {};
4454 int start_old, end_old, start_new, end_new;
4456 struct diff_output_unidiff_state *diff_state;
4457 struct diff_input_info diff_info;
4460 *choice = GOT_PATCH_CHOICE_NONE;
4462 /* Get changed line numbers without context lines for copy_change(). */
4463 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4464 start_old = cc.left.start;
4465 end_old = cc.left.end;
4466 start_new = cc.right.start;
4467 end_new = cc.right.end;
4469 /* Get the same change with context lines for display. */
4470 memset(&cc, 0, sizeof(cc));
4471 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4473 memset(&diff_info, 0, sizeof(diff_info));
4474 diff_info.left_path = relpath;
4475 diff_info.right_path = relpath;
4477 diff_state = diff_output_unidiff_state_alloc();
4478 if (diff_state == NULL)
4479 return got_error_set_errno(ENOMEM,
4480 "diff_output_unidiff_state_alloc");
4482 hunkfile = got_opentemp();
4483 if (hunkfile == NULL) {
4484 err = got_error_from_errno("got_opentemp");
4488 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4490 if (rc != DIFF_RC_OK) {
4491 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4495 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4496 err = got_ferror(hunkfile, GOT_ERR_IO);
4500 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4501 hunkfile, changeno, nchanges);
4506 case GOT_PATCH_CHOICE_YES:
4507 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4508 end_old, start_new, end_new, outfile, rejectfile);
4510 case GOT_PATCH_CHOICE_NO:
4511 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4512 end_old, start_new, end_new, rejectfile, outfile);
4514 case GOT_PATCH_CHOICE_QUIT:
4517 err = got_error(GOT_ERR_PATCH_CHOICE);
4521 diff_output_unidiff_state_free(diff_state);
4522 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4523 err = got_error_from_errno("fclose");
4527 struct revert_file_args {
4528 struct got_worktree *worktree;
4529 struct got_fileindex *fileindex;
4530 got_worktree_checkout_cb progress_cb;
4532 got_worktree_patch_cb patch_cb;
4534 struct got_repository *repo;
4535 int unlink_added_files;
4538 static const struct got_error *
4539 create_patched_content(char **path_outfile, int reverse_patch,
4540 struct got_object_id *blob_id, const char *path2,
4541 int dirfd2, const char *de_name2,
4542 const char *relpath, struct got_repository *repo,
4543 got_worktree_patch_cb patch_cb, void *patch_arg)
4545 const struct got_error *err, *free_err;
4546 struct got_blob_object *blob = NULL;
4547 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4548 int fd = -1, fd2 = -1;
4549 char link_target[PATH_MAX];
4550 ssize_t link_len = 0;
4551 char *path1 = NULL, *id_str = NULL;
4553 struct got_diffreg_result *diffreg_result = NULL;
4554 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4555 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4557 *path_outfile = NULL;
4559 err = got_object_id_str(&id_str, blob_id);
4564 fd2 = openat(dirfd2, de_name2,
4565 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4567 if (!got_err_open_nofollow_on_symlink()) {
4568 err = got_error_from_errno2("openat", path2);
4571 link_len = readlinkat(dirfd2, de_name2,
4572 link_target, sizeof(link_target));
4573 if (link_len == -1) {
4574 return got_error_from_errno2("readlinkat",
4577 sb2.st_mode = S_IFLNK;
4578 sb2.st_size = link_len;
4581 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4583 if (!got_err_open_nofollow_on_symlink()) {
4584 err = got_error_from_errno2("open", path2);
4587 link_len = readlink(path2, link_target,
4588 sizeof(link_target));
4590 return got_error_from_errno2("readlink", path2);
4591 sb2.st_mode = S_IFLNK;
4592 sb2.st_size = link_len;
4596 if (fstat(fd2, &sb2) == -1) {
4597 err = got_error_from_errno2("fstat", path2);
4601 f2 = fdopen(fd2, "r");
4603 err = got_error_from_errno2("fdopen", path2);
4609 f2 = got_opentemp();
4611 err = got_error_from_errno2("got_opentemp", path2);
4614 n = fwrite(link_target, 1, link_len, f2);
4615 if (n != link_len) {
4616 err = got_ferror(f2, GOT_ERR_IO);
4619 if (fflush(f2) == EOF) {
4620 err = got_error_from_errno("fflush");
4626 fd = got_opentempfd();
4628 err = got_error_from_errno("got_opentempfd");
4632 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4636 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4640 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4644 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4645 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4649 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4654 if (fseek(f1, 0L, SEEK_SET) == -1)
4655 return got_ferror(f1, GOT_ERR_IO);
4656 if (fseek(f2, 0L, SEEK_SET) == -1)
4657 return got_ferror(f2, GOT_ERR_IO);
4659 /* Count the number of actual changes in the diff result. */
4660 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4661 struct diff_chunk_context cc = {};
4662 diff_chunk_context_load_change(&cc, &nchunks_used,
4663 diffreg_result->result, n, 0);
4666 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4668 err = apply_or_reject_change(&choice, &nchunks_used,
4669 diffreg_result->result, n, relpath, f1, f2,
4670 &line_cur1, &line_cur2,
4671 reverse_patch ? NULL : outfile,
4672 reverse_patch ? outfile : NULL,
4673 ++i, nchanges, patch_cb, patch_arg);
4676 if (choice == GOT_PATCH_CHOICE_YES)
4678 else if (choice == GOT_PATCH_CHOICE_QUIT)
4682 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4683 reverse_patch ? NULL : outfile,
4684 reverse_patch ? outfile : NULL);
4688 if (!S_ISLNK(sb2.st_mode)) {
4691 mode = apply_umask(sb2.st_mode);
4692 if (fchmod(fileno(outfile), mode) == -1) {
4693 err = got_error_from_errno2("fchmod", path2);
4700 if (fd != -1 && close(fd) == -1 && err == NULL)
4701 err = got_error_from_errno("close");
4703 got_object_blob_close(blob);
4704 free_err = got_diffreg_result_free(diffreg_result);
4707 if (f1 && fclose(f1) == EOF && err == NULL)
4708 err = got_error_from_errno2("fclose", path1);
4709 if (f2 && fclose(f2) == EOF && err == NULL)
4710 err = got_error_from_errno2("fclose", path2);
4711 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4712 err = got_error_from_errno2("close", path2);
4713 if (outfile && fclose(outfile) == EOF && err == NULL)
4714 err = got_error_from_errno2("fclose", *path_outfile);
4715 if (path1 && unlink(path1) == -1 && err == NULL)
4716 err = got_error_from_errno2("unlink", path1);
4717 if (err || !have_content) {
4718 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4719 err = got_error_from_errno2("unlink", *path_outfile);
4720 free(*path_outfile);
4721 *path_outfile = NULL;
4727 static const struct got_error *
4728 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4729 const char *relpath, struct got_object_id *blob_id,
4730 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4731 int dirfd, const char *de_name)
4733 struct revert_file_args *a = arg;
4734 const struct got_error *err = NULL;
4735 char *parent_path = NULL;
4736 struct got_fileindex_entry *ie;
4737 struct got_commit_object *base_commit = NULL;
4738 struct got_tree_object *tree = NULL;
4739 struct got_object_id *tree_id = NULL;
4740 const struct got_tree_entry *te = NULL;
4741 char *tree_path = NULL, *te_name;
4742 char *ondisk_path = NULL, *path_content = NULL;
4743 struct got_blob_object *blob = NULL;
4746 /* Reverting a staged deletion is a no-op. */
4747 if (status == GOT_STATUS_DELETE &&
4748 staged_status != GOT_STATUS_NO_CHANGE)
4751 if (status == GOT_STATUS_UNVERSIONED)
4752 return (*a->progress_cb)(a->progress_arg,
4753 GOT_STATUS_UNVERSIONED, relpath);
4755 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4757 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4759 /* Construct in-repository path of tree which contains this blob. */
4760 err = got_path_dirname(&parent_path, ie->path);
4762 if (err->code != GOT_ERR_BAD_PATH)
4764 parent_path = strdup("/");
4765 if (parent_path == NULL) {
4766 err = got_error_from_errno("strdup");
4770 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4771 tree_path = strdup(parent_path);
4772 if (tree_path == NULL) {
4773 err = got_error_from_errno("strdup");
4777 if (got_path_is_root_dir(parent_path)) {
4778 tree_path = strdup(a->worktree->path_prefix);
4779 if (tree_path == NULL) {
4780 err = got_error_from_errno("strdup");
4784 if (asprintf(&tree_path, "%s/%s",
4785 a->worktree->path_prefix, parent_path) == -1) {
4786 err = got_error_from_errno("asprintf");
4792 err = got_object_open_as_commit(&base_commit, a->repo,
4793 a->worktree->base_commit_id);
4797 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4799 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4800 (status == GOT_STATUS_ADD ||
4801 staged_status == GOT_STATUS_ADD)))
4804 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4808 err = got_path_basename(&te_name, ie->path);
4812 te = got_object_tree_find_entry(tree, te_name);
4814 if (te == NULL && status != GOT_STATUS_ADD &&
4815 staged_status != GOT_STATUS_ADD) {
4816 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4822 case GOT_STATUS_ADD:
4824 int choice = GOT_PATCH_CHOICE_NONE;
4825 err = (*a->patch_cb)(&choice, a->patch_arg,
4826 status, ie->path, NULL, 1, 1);
4829 if (choice != GOT_PATCH_CHOICE_YES)
4832 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4836 got_fileindex_entry_remove(a->fileindex, ie);
4837 if (a->unlink_added_files) {
4838 if (asprintf(&ondisk_path, "%s/%s",
4839 got_worktree_get_root_path(a->worktree),
4841 err = got_error_from_errno("asprintf");
4844 if (unlink(ondisk_path) == -1) {
4845 err = got_error_from_errno2("unlink",
4851 case GOT_STATUS_DELETE:
4853 int choice = GOT_PATCH_CHOICE_NONE;
4854 err = (*a->patch_cb)(&choice, a->patch_arg,
4855 status, ie->path, NULL, 1, 1);
4858 if (choice != GOT_PATCH_CHOICE_YES)
4862 case GOT_STATUS_MODIFY:
4863 case GOT_STATUS_MODE_CHANGE:
4864 case GOT_STATUS_CONFLICT:
4865 case GOT_STATUS_MISSING: {
4866 struct got_object_id id;
4867 if (staged_status == GOT_STATUS_ADD ||
4868 staged_status == GOT_STATUS_MODIFY)
4869 got_fileindex_entry_get_staged_blob_id(&id, ie);
4871 got_fileindex_entry_get_blob_id(&id, ie);
4872 fd = got_opentempfd();
4874 err = got_error_from_errno("got_opentempfd");
4878 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4882 if (asprintf(&ondisk_path, "%s/%s",
4883 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4884 err = got_error_from_errno("asprintf");
4888 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4889 status == GOT_STATUS_CONFLICT)) {
4890 int is_bad_symlink = 0;
4891 err = create_patched_content(&path_content, 1, &id,
4892 ondisk_path, dirfd, de_name, ie->path, a->repo,
4893 a->patch_cb, a->patch_arg);
4894 if (err || path_content == NULL)
4896 if (te && S_ISLNK(te->mode)) {
4897 if (unlink(path_content) == -1) {
4898 err = got_error_from_errno2("unlink",
4902 err = install_symlink(&is_bad_symlink,
4903 a->worktree, ondisk_path, ie->path,
4904 blob, 0, 1, 0, 0, a->repo,
4905 a->progress_cb, a->progress_arg);
4907 if (rename(path_content, ondisk_path) == -1) {
4908 err = got_error_from_errno3("rename",
4909 path_content, ondisk_path);
4914 int is_bad_symlink = 0;
4915 if (te && S_ISLNK(te->mode)) {
4916 err = install_symlink(&is_bad_symlink,
4917 a->worktree, ondisk_path, ie->path,
4918 blob, 0, 1, 0, 0, a->repo,
4919 a->progress_cb, a->progress_arg);
4921 err = install_blob(a->worktree, ondisk_path,
4923 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4924 got_fileindex_perms_to_st(ie), blob,
4925 0, 1, 0, 0, a->repo,
4926 a->progress_cb, a->progress_arg);
4930 if (status == GOT_STATUS_DELETE ||
4931 status == GOT_STATUS_MODE_CHANGE) {
4932 err = got_fileindex_entry_update(ie,
4933 a->worktree->root_fd, relpath,
4935 a->worktree->base_commit_id->sha1, 1);
4939 if (is_bad_symlink) {
4940 got_fileindex_entry_filetype_set(ie,
4941 GOT_FILEIDX_MODE_BAD_SYMLINK);
4954 if (fd != -1 && close(fd) == -1 && err == NULL)
4955 err = got_error_from_errno("close");
4957 got_object_blob_close(blob);
4959 got_object_tree_close(tree);
4962 got_object_commit_close(base_commit);
4966 const struct got_error *
4967 got_worktree_revert(struct got_worktree *worktree,
4968 struct got_pathlist_head *paths,
4969 got_worktree_checkout_cb progress_cb, void *progress_arg,
4970 got_worktree_patch_cb patch_cb, void *patch_arg,
4971 struct got_repository *repo)
4973 struct got_fileindex *fileindex = NULL;
4974 char *fileindex_path = NULL;
4975 const struct got_error *err = NULL, *unlockerr = NULL;
4976 const struct got_error *sync_err = NULL;
4977 struct got_pathlist_entry *pe;
4978 struct revert_file_args rfa;
4980 err = lock_worktree(worktree, LOCK_EX);
4984 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4988 rfa.worktree = worktree;
4989 rfa.fileindex = fileindex;
4990 rfa.progress_cb = progress_cb;
4991 rfa.progress_arg = progress_arg;
4992 rfa.patch_cb = patch_cb;
4993 rfa.patch_arg = patch_arg;
4995 rfa.unlink_added_files = 0;
4996 TAILQ_FOREACH(pe, paths, entry) {
4997 err = worktree_status(worktree, pe->path, fileindex, repo,
4998 revert_file, &rfa, NULL, NULL, 1, 0);
5002 sync_err = sync_fileindex(fileindex, fileindex_path);
5003 if (sync_err && err == NULL)
5006 free(fileindex_path);
5008 got_fileindex_free(fileindex);
5009 unlockerr = lock_worktree(worktree, LOCK_SH);
5010 if (unlockerr && err == NULL)
5016 free_commitable(struct got_commitable *ct)
5019 free(ct->in_repo_path);
5020 free(ct->ondisk_path);
5022 free(ct->base_blob_id);
5023 free(ct->staged_blob_id);
5024 free(ct->base_commit_id);
5028 struct collect_commitables_arg {
5029 struct got_pathlist_head *commitable_paths;
5030 struct got_repository *repo;
5031 struct got_worktree *worktree;
5032 struct got_fileindex *fileindex;
5033 int have_staged_files;
5034 int allow_bad_symlinks;
5035 int diff_header_shown;
5036 int commit_conflicts;
5043 * Create a file which contains the target path of a symlink so we can feed
5044 * it as content to the diff engine.
5046 static const struct got_error *
5047 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5048 const char *abspath)
5050 const struct got_error *err = NULL;
5051 char target_path[PATH_MAX];
5052 ssize_t target_len, outlen;
5057 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5058 if (target_len == -1)
5059 return got_error_from_errno2("readlinkat", abspath);
5061 target_len = readlink(abspath, target_path, PATH_MAX);
5062 if (target_len == -1)
5063 return got_error_from_errno2("readlink", abspath);
5066 *fd = got_opentempfd();
5068 return got_error_from_errno("got_opentempfd");
5070 outlen = write(*fd, target_path, target_len);
5072 err = got_error_from_errno("got_opentempfd");
5076 if (lseek(*fd, 0, SEEK_SET) == -1) {
5077 err = got_error_from_errno2("lseek", abspath);
5088 static const struct got_error *
5089 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5090 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5091 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5093 const struct got_error *err = NULL;
5094 struct got_blob_object *blob1 = NULL;
5095 int fd = -1, fd1 = -1, fd2 = -1;
5096 FILE *ondisk_file = NULL;
5097 char *label1 = NULL;
5101 char *id_str = NULL;
5103 memset(&sb, 0, sizeof(sb));
5106 if (ct->staged_status != GOT_STATUS_MODIFY &&
5107 ct->staged_status != GOT_STATUS_ADD &&
5108 ct->staged_status != GOT_STATUS_DELETE)
5111 if (ct->status != GOT_STATUS_MODIFY &&
5112 ct->status != GOT_STATUS_ADD &&
5113 ct->status != GOT_STATUS_DELETE &&
5114 ct->status != GOT_STATUS_CONFLICT)
5118 err = got_opentemp_truncate(f1);
5120 return got_error_from_errno("got_opentemp_truncate");
5121 err = got_opentemp_truncate(f2);
5123 return got_error_from_errno("got_opentemp_truncate");
5125 if (!*diff_header_shown) {
5126 err = got_object_id_str(&id_str, worktree->base_commit_id);
5129 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5130 got_worktree_get_root_path(worktree));
5131 fprintf(diff_outfile, "commit - %s\n", id_str);
5132 fprintf(diff_outfile, "path + %s%s\n",
5133 got_worktree_get_root_path(worktree),
5134 diff_staged ? " (staged changes)" : "");
5135 *diff_header_shown = 1;
5139 const char *label1 = NULL, *label2 = NULL;
5140 switch (ct->staged_status) {
5141 case GOT_STATUS_MODIFY:
5145 case GOT_STATUS_ADD:
5148 case GOT_STATUS_DELETE:
5152 return got_error(GOT_ERR_FILE_STATUS);
5154 fd1 = got_opentempfd();
5156 err = got_error_from_errno("got_opentempfd");
5159 fd2 = got_opentempfd();
5161 err = got_error_from_errno("got_opentempfd");
5164 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5165 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5166 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5167 NULL, repo, diff_outfile);
5171 fd1 = got_opentempfd();
5173 err = got_error_from_errno("got_opentempfd");
5177 if (ct->status != GOT_STATUS_ADD) {
5178 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5184 if (ct->status != GOT_STATUS_DELETE) {
5186 fd = openat(dirfd, de_name,
5187 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5189 if (!got_err_open_nofollow_on_symlink()) {
5190 err = got_error_from_errno2("openat",
5194 err = get_symlink_target_file(&fd, dirfd,
5195 de_name, ct->ondisk_path);
5200 fd = open(ct->ondisk_path,
5201 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5203 if (!got_err_open_nofollow_on_symlink()) {
5204 err = got_error_from_errno2("open",
5208 err = get_symlink_target_file(&fd, dirfd,
5209 de_name, ct->ondisk_path);
5214 if (fstatat(fd, ct->ondisk_path, &sb,
5215 AT_SYMLINK_NOFOLLOW) == -1) {
5216 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5219 ondisk_file = fdopen(fd, "r");
5220 if (ondisk_file == NULL) {
5221 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5229 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5235 err = got_diff_blob_file(blob1, f1, size1, label1,
5236 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5237 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5239 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5240 err = got_error_from_errno("close");
5241 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5242 err = got_error_from_errno("close");
5244 got_object_blob_close(blob1);
5245 if (fd != -1 && close(fd) == -1 && err == NULL)
5246 err = got_error_from_errno("close");
5247 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5248 err = got_error_from_errno("fclose");
5252 static const struct got_error *
5253 collect_commitables(void *arg, unsigned char status,
5254 unsigned char staged_status, const char *relpath,
5255 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5256 struct got_object_id *commit_id, int dirfd, const char *de_name)
5258 struct collect_commitables_arg *a = arg;
5259 const struct got_error *err = NULL;
5260 struct got_commitable *ct = NULL;
5261 struct got_pathlist_entry *new = NULL;
5262 char *parent_path = NULL, *path = NULL;
5265 if (a->have_staged_files) {
5266 if (staged_status != GOT_STATUS_MODIFY &&
5267 staged_status != GOT_STATUS_ADD &&
5268 staged_status != GOT_STATUS_DELETE)
5271 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5272 printf("C %s\n", relpath);
5273 return got_error(GOT_ERR_COMMIT_CONFLICT);
5276 if (status != GOT_STATUS_MODIFY &&
5277 status != GOT_STATUS_MODE_CHANGE &&
5278 status != GOT_STATUS_ADD &&
5279 status != GOT_STATUS_DELETE &&
5280 status != GOT_STATUS_CONFLICT)
5284 if (asprintf(&path, "/%s", relpath) == -1) {
5285 err = got_error_from_errno("asprintf");
5288 if (strcmp(path, "/") == 0) {
5289 parent_path = strdup("");
5290 if (parent_path == NULL)
5291 return got_error_from_errno("strdup");
5293 err = got_path_dirname(&parent_path, path);
5298 ct = calloc(1, sizeof(*ct));
5300 err = got_error_from_errno("calloc");
5304 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5306 err = got_error_from_errno("asprintf");
5310 if (staged_status == GOT_STATUS_ADD ||
5311 staged_status == GOT_STATUS_MODIFY) {
5312 struct got_fileindex_entry *ie;
5313 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5314 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5315 case GOT_FILEIDX_MODE_REGULAR_FILE:
5316 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5319 case GOT_FILEIDX_MODE_SYMLINK:
5323 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5326 ct->mode |= got_fileindex_entry_perms_get(ie);
5327 } else if (status != GOT_STATUS_DELETE &&
5328 staged_status != GOT_STATUS_DELETE) {
5330 if (fstatat(dirfd, de_name, &sb,
5331 AT_SYMLINK_NOFOLLOW) == -1) {
5332 err = got_error_from_errno2("fstatat",
5336 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5337 err = got_error_from_errno2("lstat", ct->ondisk_path);
5340 ct->mode = sb.st_mode;
5343 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5344 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5346 err = got_error_from_errno("asprintf");
5350 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5351 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5353 char target_path[PATH_MAX];
5355 target_len = readlink(ct->ondisk_path, target_path,
5356 sizeof(target_path));
5357 if (target_len == -1) {
5358 err = got_error_from_errno2("readlink",
5362 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5363 target_len, ct->ondisk_path, a->worktree->root_path);
5366 if (is_bad_symlink) {
5367 err = got_error_path(ct->ondisk_path,
5368 GOT_ERR_BAD_SYMLINK);
5374 ct->status = status;
5375 ct->staged_status = staged_status;
5376 ct->blob_id = NULL; /* will be filled in when blob gets created */
5377 if (ct->status != GOT_STATUS_ADD &&
5378 ct->staged_status != GOT_STATUS_ADD) {
5379 ct->base_blob_id = got_object_id_dup(blob_id);
5380 if (ct->base_blob_id == NULL) {
5381 err = got_error_from_errno("got_object_id_dup");
5384 ct->base_commit_id = got_object_id_dup(commit_id);
5385 if (ct->base_commit_id == NULL) {
5386 err = got_error_from_errno("got_object_id_dup");
5390 if (ct->staged_status == GOT_STATUS_ADD ||
5391 ct->staged_status == GOT_STATUS_MODIFY) {
5392 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5393 if (ct->staged_blob_id == NULL) {
5394 err = got_error_from_errno("got_object_id_dup");
5398 ct->path = strdup(path);
5399 if (ct->path == NULL) {
5400 err = got_error_from_errno("strdup");
5403 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5407 if (a->diff_outfile && ct && new != NULL) {
5408 err = append_ct_diff(ct, &a->diff_header_shown,
5409 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5410 a->have_staged_files, a->repo, a->worktree);
5415 if (ct && (err || new == NULL))
5416 free_commitable(ct);
5422 static const struct got_error *write_tree(struct got_object_id **, int *,
5423 struct got_tree_object *, const char *, struct got_pathlist_head *,
5424 got_worktree_status_cb status_cb, void *status_arg,
5425 struct got_repository *);
5427 static const struct got_error *
5428 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5429 struct got_tree_entry *te, const char *parent_path,
5430 struct got_pathlist_head *commitable_paths,
5431 got_worktree_status_cb status_cb, void *status_arg,
5432 struct got_repository *repo)
5434 const struct got_error *err = NULL;
5435 struct got_tree_object *subtree;
5438 if (asprintf(&subpath, "%s%s%s", parent_path,
5439 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5440 return got_error_from_errno("asprintf");
5442 err = got_object_open_as_tree(&subtree, repo, &te->id);
5446 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5447 commitable_paths, status_cb, status_arg, repo);
5448 got_object_tree_close(subtree);
5453 static const struct got_error *
5454 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5456 const struct got_error *err = NULL;
5457 char *ct_parent_path = NULL;
5461 if (strchr(ct->in_repo_path, '/') == NULL) {
5462 *match = got_path_is_root_dir(path);
5466 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5469 *match = (strcmp(path, ct_parent_path) == 0);
5470 free(ct_parent_path);
5475 get_ct_file_mode(struct got_commitable *ct)
5477 if (S_ISLNK(ct->mode))
5480 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5483 static const struct got_error *
5484 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5485 struct got_tree_entry *te, struct got_commitable *ct)
5487 const struct got_error *err = NULL;
5491 err = got_object_tree_entry_dup(new_te, te);
5495 (*new_te)->mode = get_ct_file_mode(ct);
5497 if (ct->staged_status == GOT_STATUS_MODIFY)
5498 memcpy(&(*new_te)->id, ct->staged_blob_id,
5499 sizeof((*new_te)->id));
5501 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5503 if (err && *new_te) {
5510 static const struct got_error *
5511 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5512 struct got_commitable *ct)
5514 const struct got_error *err = NULL;
5515 char *ct_name = NULL;
5519 *new_te = calloc(1, sizeof(**new_te));
5520 if (*new_te == NULL)
5521 return got_error_from_errno("calloc");
5523 err = got_path_basename(&ct_name, ct->path);
5526 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5527 sizeof((*new_te)->name)) {
5528 err = got_error(GOT_ERR_NO_SPACE);
5532 (*new_te)->mode = get_ct_file_mode(ct);
5534 if (ct->staged_status == GOT_STATUS_ADD)
5535 memcpy(&(*new_te)->id, ct->staged_blob_id,
5536 sizeof((*new_te)->id));
5538 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5541 if (err && *new_te) {
5548 static const struct got_error *
5549 insert_tree_entry(struct got_tree_entry *new_te,
5550 struct got_pathlist_head *paths)
5552 const struct got_error *err = NULL;
5553 struct got_pathlist_entry *new_pe;
5555 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5559 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5563 static const struct got_error *
5564 report_ct_status(struct got_commitable *ct,
5565 got_worktree_status_cb status_cb, void *status_arg)
5567 const char *ct_path = ct->path;
5568 unsigned char status;
5570 if (status_cb == NULL) /* no commit progress output desired */
5573 while (ct_path[0] == '/')
5576 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5577 status = ct->staged_status;
5579 status = ct->status;
5581 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5582 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5585 static const struct got_error *
5586 match_modified_subtree(int *modified, struct got_tree_entry *te,
5587 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5589 const struct got_error *err = NULL;
5590 struct got_pathlist_entry *pe;
5595 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5596 got_path_is_root_dir(base_tree_path) ? "" : "/",
5598 return got_error_from_errno("asprintf");
5600 TAILQ_FOREACH(pe, commitable_paths, entry) {
5601 struct got_commitable *ct = pe->data;
5602 *modified = got_path_is_child(ct->in_repo_path, te_path,
5612 static const struct got_error *
5613 match_deleted_or_modified_ct(struct got_commitable **ctp,
5614 struct got_tree_entry *te, const char *base_tree_path,
5615 struct got_pathlist_head *commitable_paths)
5617 const struct got_error *err = NULL;
5618 struct got_pathlist_entry *pe;
5622 TAILQ_FOREACH(pe, commitable_paths, entry) {
5623 struct got_commitable *ct = pe->data;
5624 char *ct_name = NULL;
5627 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5628 if (ct->status != GOT_STATUS_MODIFY &&
5629 ct->status != GOT_STATUS_MODE_CHANGE &&
5630 ct->status != GOT_STATUS_DELETE &&
5631 ct->status != GOT_STATUS_CONFLICT)
5634 if (ct->staged_status != GOT_STATUS_MODIFY &&
5635 ct->staged_status != GOT_STATUS_DELETE)
5639 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5642 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5648 err = got_path_basename(&ct_name, pe->path);
5652 if (strcmp(te->name, ct_name) != 0) {
5665 static const struct got_error *
5666 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5667 const char *child_path, const char *path_base_tree,
5668 struct got_pathlist_head *commitable_paths,
5669 got_worktree_status_cb status_cb, void *status_arg,
5670 struct got_repository *repo)
5672 const struct got_error *err = NULL;
5673 struct got_tree_entry *new_te;
5675 struct got_object_id *id = NULL;
5680 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5681 got_path_is_root_dir(path_base_tree) ? "" : "/",
5683 return got_error_from_errno("asprintf");
5685 new_te = calloc(1, sizeof(*new_te));
5687 return got_error_from_errno("calloc");
5688 new_te->mode = S_IFDIR;
5690 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5691 sizeof(new_te->name)) {
5692 err = got_error(GOT_ERR_NO_SPACE);
5695 err = write_tree(&id, &nentries, NULL, subtree_path,
5696 commitable_paths, status_cb, status_arg, repo);
5701 memcpy(&new_te->id, id, sizeof(new_te->id));
5710 static const struct got_error *
5711 write_tree(struct got_object_id **new_tree_id, int *nentries,
5712 struct got_tree_object *base_tree, const char *path_base_tree,
5713 struct got_pathlist_head *commitable_paths,
5714 got_worktree_status_cb status_cb, void *status_arg,
5715 struct got_repository *repo)
5717 const struct got_error *err = NULL;
5718 struct got_pathlist_head paths;
5719 struct got_tree_entry *te, *new_te = NULL;
5720 struct got_pathlist_entry *pe;
5725 /* Insert, and recurse into, newly added entries first. */
5726 TAILQ_FOREACH(pe, commitable_paths, entry) {
5727 struct got_commitable *ct = pe->data;
5728 char *child_path = NULL, *slash;
5730 if ((ct->status != GOT_STATUS_ADD &&
5731 ct->staged_status != GOT_STATUS_ADD) ||
5732 (ct->flags & GOT_COMMITABLE_ADDED))
5735 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5736 strlen(path_base_tree)))
5739 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5744 slash = strchr(child_path, '/');
5745 if (slash == NULL) {
5746 err = alloc_added_blob_tree_entry(&new_te, ct);
5749 err = report_ct_status(ct, status_cb, status_arg);
5752 ct->flags |= GOT_COMMITABLE_ADDED;
5753 err = insert_tree_entry(new_te, &paths);
5758 *slash = '\0'; /* trim trailing path components */
5759 if (base_tree == NULL ||
5760 got_object_tree_find_entry(base_tree, child_path)
5762 err = make_subtree_for_added_blob(&new_te,
5763 child_path, path_base_tree,
5764 commitable_paths, status_cb, status_arg,
5768 err = insert_tree_entry(new_te, &paths);
5777 int i, nbase_entries;
5778 /* Handle modified and deleted entries. */
5779 nbase_entries = got_object_tree_get_nentries(base_tree);
5780 for (i = 0; i < nbase_entries; i++) {
5781 struct got_commitable *ct = NULL;
5783 te = got_object_tree_get_entry(base_tree, i);
5784 if (got_object_tree_entry_is_submodule(te)) {
5785 /* Entry is a submodule; just copy it. */
5786 err = got_object_tree_entry_dup(&new_te, te);
5789 err = insert_tree_entry(new_te, &paths);
5796 if (S_ISDIR(te->mode)) {
5798 err = got_object_tree_entry_dup(&new_te, te);
5801 err = match_modified_subtree(&modified, te,
5802 path_base_tree, commitable_paths);
5805 /* Avoid recursion into unmodified subtrees. */
5807 struct got_object_id *new_id;
5809 err = write_subtree(&new_id,
5811 path_base_tree, commitable_paths,
5812 status_cb, status_arg, repo);
5815 if (nsubentries == 0) {
5816 /* All entries were deleted. */
5820 memcpy(&new_te->id, new_id,
5821 sizeof(new_te->id));
5824 err = insert_tree_entry(new_te, &paths);
5831 err = match_deleted_or_modified_ct(&ct, te,
5832 path_base_tree, commitable_paths);
5836 /* NB: Deleted entries get dropped here. */
5837 if (ct->status == GOT_STATUS_MODIFY ||
5838 ct->status == GOT_STATUS_MODE_CHANGE ||
5839 ct->status == GOT_STATUS_CONFLICT ||
5840 ct->staged_status == GOT_STATUS_MODIFY) {
5841 err = alloc_modified_blob_tree_entry(
5845 err = insert_tree_entry(new_te, &paths);
5850 err = report_ct_status(ct, status_cb,
5855 /* Entry is unchanged; just copy it. */
5856 err = got_object_tree_entry_dup(&new_te, te);
5859 err = insert_tree_entry(new_te, &paths);
5867 /* Write new list of entries; deleted entries have been dropped. */
5868 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5870 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5874 static const struct got_error *
5875 update_fileindex_after_commit(struct got_worktree *worktree,
5876 struct got_pathlist_head *commitable_paths,
5877 struct got_object_id *new_base_commit_id,
5878 struct got_fileindex *fileindex, int have_staged_files)
5880 const struct got_error *err = NULL;
5881 struct got_pathlist_entry *pe;
5882 char *relpath = NULL;
5884 TAILQ_FOREACH(pe, commitable_paths, entry) {
5885 struct got_fileindex_entry *ie;
5886 struct got_commitable *ct = pe->data;
5888 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5890 err = got_path_skip_common_ancestor(&relpath,
5891 worktree->root_path, ct->ondisk_path);
5896 if (ct->status == GOT_STATUS_DELETE ||
5897 ct->staged_status == GOT_STATUS_DELETE) {
5898 got_fileindex_entry_remove(fileindex, ie);
5899 } else if (ct->staged_status == GOT_STATUS_ADD ||
5900 ct->staged_status == GOT_STATUS_MODIFY) {
5901 got_fileindex_entry_stage_set(ie,
5902 GOT_FILEIDX_STAGE_NONE);
5903 got_fileindex_entry_staged_filetype_set(ie, 0);
5905 err = got_fileindex_entry_update(ie,
5906 worktree->root_fd, relpath,
5907 ct->staged_blob_id->sha1,
5908 new_base_commit_id->sha1,
5909 !have_staged_files);
5911 err = got_fileindex_entry_update(ie,
5912 worktree->root_fd, relpath,
5914 new_base_commit_id->sha1,
5915 !have_staged_files);
5917 err = got_fileindex_entry_alloc(&ie, pe->path);
5920 err = got_fileindex_entry_update(ie,
5921 worktree->root_fd, relpath, ct->blob_id->sha1,
5922 new_base_commit_id->sha1, 1);
5924 got_fileindex_entry_free(ie);
5927 err = got_fileindex_entry_add(fileindex, ie);
5929 got_fileindex_entry_free(ie);
5942 static const struct got_error *
5943 check_out_of_date(const char *in_repo_path, unsigned char status,
5944 unsigned char staged_status, struct got_object_id *base_blob_id,
5945 struct got_object_id *base_commit_id,
5946 struct got_object_id *head_commit_id, struct got_repository *repo,
5949 const struct got_error *err = NULL;
5950 struct got_commit_object *commit = NULL;
5951 struct got_object_id *id = NULL;
5953 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5954 /* Trivial case: base commit == head commit */
5955 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5958 * Ensure file content which local changes were based
5959 * on matches file content in the branch head.
5961 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5964 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5966 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5967 err = got_error(ood_errcode);
5969 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5970 err = got_error(ood_errcode);
5972 /* Require that added files don't exist in the branch head. */
5973 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5976 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5977 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5979 err = id ? got_error(ood_errcode) : NULL;
5984 got_object_commit_close(commit);
5988 static const struct got_error *
5989 commit_worktree(struct got_object_id **new_commit_id,
5990 struct got_pathlist_head *commitable_paths,
5991 struct got_object_id *head_commit_id,
5992 struct got_object_id *parent_id2,
5993 struct got_worktree *worktree,
5994 const char *author, const char *committer, char *diff_path,
5995 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5996 got_worktree_status_cb status_cb, void *status_arg,
5997 struct got_repository *repo)
5999 const struct got_error *err = NULL, *unlockerr = NULL;
6000 struct got_pathlist_entry *pe;
6001 const char *head_ref_name = NULL;
6002 struct got_commit_object *head_commit = NULL;
6003 struct got_reference *head_ref2 = NULL;
6004 struct got_object_id *head_commit_id2 = NULL;
6005 struct got_tree_object *head_tree = NULL;
6006 struct got_object_id *new_tree_id = NULL;
6007 int nentries, nparents = 0;
6008 struct got_object_id_queue parent_ids;
6009 struct got_object_qid *pid = NULL;
6010 char *logmsg = NULL;
6013 *new_commit_id = NULL;
6015 STAILQ_INIT(&parent_ids);
6017 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6021 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6025 if (commit_msg_cb != NULL) {
6026 err = commit_msg_cb(commitable_paths, diff_path,
6027 &logmsg, commit_arg);
6032 if (logmsg == NULL || strlen(logmsg) == 0) {
6033 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6037 /* Create blobs from added and modified files and record their IDs. */
6038 TAILQ_FOREACH(pe, commitable_paths, entry) {
6039 struct got_commitable *ct = pe->data;
6042 /* Blobs for staged files already exist. */
6043 if (ct->staged_status == GOT_STATUS_ADD ||
6044 ct->staged_status == GOT_STATUS_MODIFY)
6047 if (ct->status != GOT_STATUS_ADD &&
6048 ct->status != GOT_STATUS_MODIFY &&
6049 ct->status != GOT_STATUS_MODE_CHANGE &&
6050 ct->status != GOT_STATUS_CONFLICT)
6053 if (asprintf(&ondisk_path, "%s/%s",
6054 worktree->root_path, pe->path) == -1) {
6055 err = got_error_from_errno("asprintf");
6058 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6064 /* Recursively write new tree objects. */
6065 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6066 commitable_paths, status_cb, status_arg, repo);
6070 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6073 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6076 err = got_object_qid_alloc(&pid, parent_id2);
6079 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6082 timestamp = time(NULL);
6083 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6084 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6090 /* Check if a concurrent commit to our branch has occurred. */
6091 head_ref_name = got_worktree_get_head_ref_name(worktree);
6092 if (head_ref_name == NULL) {
6093 err = got_error_from_errno("got_worktree_get_head_ref_name");
6096 /* Lock the reference here to prevent concurrent modification. */
6097 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6100 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6103 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6104 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6107 /* Update branch head in repository. */
6108 err = got_ref_change_ref(head_ref2, *new_commit_id);
6111 err = got_ref_write(head_ref2, repo);
6115 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6119 err = ref_base_commit(worktree, repo);
6123 got_object_id_queue_free(&parent_ids);
6125 got_object_tree_close(head_tree);
6127 got_object_commit_close(head_commit);
6128 free(head_commit_id2);
6130 unlockerr = got_ref_unlock(head_ref2);
6131 if (unlockerr && err == NULL)
6133 got_ref_close(head_ref2);
6138 static const struct got_error *
6139 check_path_is_commitable(const char *path,
6140 struct got_pathlist_head *commitable_paths)
6142 struct got_pathlist_entry *cpe = NULL;
6143 size_t path_len = strlen(path);
6145 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6146 struct got_commitable *ct = cpe->data;
6147 const char *ct_path = ct->path;
6149 while (ct_path[0] == '/')
6152 if (strcmp(path, ct_path) == 0 ||
6153 got_path_is_child(ct_path, path, path_len))
6158 return got_error_path(path, GOT_ERR_BAD_PATH);
6163 static const struct got_error *
6164 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6166 int *have_staged_files = arg;
6168 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6169 *have_staged_files = 1;
6170 return got_error(GOT_ERR_CANCELLED);
6176 static const struct got_error *
6177 check_non_staged_files(struct got_fileindex *fileindex,
6178 struct got_pathlist_head *paths)
6180 struct got_pathlist_entry *pe;
6181 struct got_fileindex_entry *ie;
6183 TAILQ_FOREACH(pe, paths, entry) {
6184 if (pe->path[0] == '\0')
6186 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6188 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6189 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6190 return got_error_path(pe->path,
6191 GOT_ERR_FILE_NOT_STAGED);
6197 const struct got_error *
6198 got_worktree_commit(struct got_object_id **new_commit_id,
6199 struct got_worktree *worktree, struct got_pathlist_head *paths,
6200 const char *author, const char *committer, int allow_bad_symlinks,
6201 int show_diff, int commit_conflicts,
6202 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6203 got_worktree_status_cb status_cb, void *status_arg,
6204 struct got_repository *repo)
6206 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6207 struct got_fileindex *fileindex = NULL;
6208 char *fileindex_path = NULL;
6209 struct got_pathlist_head commitable_paths;
6210 struct collect_commitables_arg cc_arg;
6211 struct got_pathlist_entry *pe;
6212 struct got_reference *head_ref = NULL;
6213 struct got_object_id *head_commit_id = NULL;
6214 char *diff_path = NULL;
6215 int have_staged_files = 0;
6217 *new_commit_id = NULL;
6219 memset(&cc_arg, 0, sizeof(cc_arg));
6220 TAILQ_INIT(&commitable_paths);
6222 err = lock_worktree(worktree, LOCK_EX);
6226 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6230 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6234 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6238 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6239 &have_staged_files);
6240 if (err && err->code != GOT_ERR_CANCELLED)
6242 if (have_staged_files) {
6243 err = check_non_staged_files(fileindex, paths);
6248 cc_arg.commitable_paths = &commitable_paths;
6249 cc_arg.worktree = worktree;
6250 cc_arg.fileindex = fileindex;
6252 cc_arg.have_staged_files = have_staged_files;
6253 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6254 cc_arg.diff_header_shown = 0;
6255 cc_arg.commit_conflicts = commit_conflicts;
6257 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6258 GOT_TMPDIR_STR "/got", ".diff");
6261 cc_arg.f1 = got_opentemp();
6262 if (cc_arg.f1 == NULL) {
6263 err = got_error_from_errno("got_opentemp");
6266 cc_arg.f2 = got_opentemp();
6267 if (cc_arg.f2 == NULL) {
6268 err = got_error_from_errno("got_opentemp");
6273 TAILQ_FOREACH(pe, paths, entry) {
6274 err = worktree_status(worktree, pe->path, fileindex, repo,
6275 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6281 if (fflush(cc_arg.diff_outfile) == EOF) {
6282 err = got_error_from_errno("fflush");
6287 if (TAILQ_EMPTY(&commitable_paths)) {
6288 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6292 TAILQ_FOREACH(pe, paths, entry) {
6293 err = check_path_is_commitable(pe->path, &commitable_paths);
6298 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6299 struct got_commitable *ct = pe->data;
6300 const char *ct_path = ct->in_repo_path;
6302 while (ct_path[0] == '/')
6304 err = check_out_of_date(ct_path, ct->status,
6305 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6306 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6312 err = commit_worktree(new_commit_id, &commitable_paths,
6313 head_commit_id, NULL, worktree, author, committer,
6314 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6315 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6319 err = update_fileindex_after_commit(worktree, &commitable_paths,
6320 *new_commit_id, fileindex, have_staged_files);
6321 sync_err = sync_fileindex(fileindex, fileindex_path);
6322 if (sync_err && err == NULL)
6326 got_fileindex_free(fileindex);
6327 free(fileindex_path);
6328 unlockerr = lock_worktree(worktree, LOCK_SH);
6329 if (unlockerr && err == NULL)
6331 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6332 struct got_commitable *ct = pe->data;
6334 free_commitable(ct);
6336 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6337 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6338 err = got_error_from_errno2("unlink", diff_path);
6340 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6342 err = got_error_from_errno("fclose");
6347 got_commitable_get_path(struct got_commitable *ct)
6353 got_commitable_get_status(struct got_commitable *ct)
6358 struct check_rebase_ok_arg {
6359 struct got_worktree *worktree;
6360 struct got_repository *repo;
6363 static const struct got_error *
6364 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6366 const struct got_error *err = NULL;
6367 struct check_rebase_ok_arg *a = arg;
6368 unsigned char status;
6372 /* Reject rebase of a work tree with mixed base commits. */
6373 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6374 SHA1_DIGEST_LENGTH))
6375 return got_error(GOT_ERR_MIXED_COMMITS);
6377 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6379 return got_error_from_errno("asprintf");
6381 /* Reject rebase of a work tree with modified or staged files. */
6382 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6387 if (status != GOT_STATUS_NO_CHANGE)
6388 return got_error(GOT_ERR_MODIFIED);
6389 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6390 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6395 const struct got_error *
6396 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6397 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6398 struct got_worktree *worktree, struct got_reference *branch,
6399 struct got_repository *repo)
6401 const struct got_error *err = NULL;
6402 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6403 char *branch_ref_name = NULL;
6404 char *fileindex_path = NULL;
6405 struct check_rebase_ok_arg ok_arg;
6406 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6407 struct got_object_id *wt_branch_tip = NULL;
6409 *new_base_branch_ref = NULL;
6413 err = lock_worktree(worktree, LOCK_EX);
6417 err = open_fileindex(fileindex, &fileindex_path, worktree);
6421 ok_arg.worktree = worktree;
6423 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6428 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6432 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6436 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6440 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6445 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6448 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6449 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6453 err = got_ref_alloc_symref(new_base_branch_ref,
6454 new_base_branch_ref_name, wt_branch);
6457 err = got_ref_write(*new_base_branch_ref, repo);
6461 /* TODO Lock original branch's ref while rebasing? */
6463 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6467 err = got_ref_write(branch_ref, repo);
6471 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6472 worktree->base_commit_id);
6475 err = got_ref_write(*tmp_branch, repo);
6479 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6483 free(fileindex_path);
6484 free(tmp_branch_name);
6485 free(new_base_branch_ref_name);
6486 free(branch_ref_name);
6488 got_ref_close(branch_ref);
6490 got_ref_close(wt_branch);
6491 free(wt_branch_tip);
6493 if (*new_base_branch_ref) {
6494 got_ref_close(*new_base_branch_ref);
6495 *new_base_branch_ref = NULL;
6498 got_ref_close(*tmp_branch);
6502 got_fileindex_free(*fileindex);
6505 lock_worktree(worktree, LOCK_SH);
6510 const struct got_error *
6511 got_worktree_rebase_continue(struct got_object_id **commit_id,
6512 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6513 struct got_reference **branch, struct got_fileindex **fileindex,
6514 struct got_worktree *worktree, struct got_repository *repo)
6516 const struct got_error *err;
6517 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6518 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6519 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6520 char *fileindex_path = NULL;
6521 int have_staged_files = 0;
6524 *new_base_branch = NULL;
6529 err = lock_worktree(worktree, LOCK_EX);
6533 err = open_fileindex(fileindex, &fileindex_path, worktree);
6537 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6538 &have_staged_files);
6539 if (err && err->code != GOT_ERR_CANCELLED)
6541 if (have_staged_files) {
6542 err = got_error(GOT_ERR_STAGED_PATHS);
6546 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6550 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6554 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6558 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6562 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6566 err = got_ref_open(branch, repo,
6567 got_ref_get_symref_target(branch_ref), 0);
6571 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6575 err = got_ref_resolve(commit_id, repo, commit_ref);
6579 err = got_ref_open(new_base_branch, repo,
6580 new_base_branch_ref_name, 0);
6584 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6588 free(commit_ref_name);
6589 free(branch_ref_name);
6590 free(fileindex_path);
6592 got_ref_close(commit_ref);
6594 got_ref_close(branch_ref);
6599 got_ref_close(*tmp_branch);
6602 if (*new_base_branch) {
6603 got_ref_close(*new_base_branch);
6604 *new_base_branch = NULL;
6607 got_ref_close(*branch);
6611 got_fileindex_free(*fileindex);
6614 lock_worktree(worktree, LOCK_SH);
6619 const struct got_error *
6620 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6622 const struct got_error *err;
6623 char *tmp_branch_name = NULL;
6625 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6629 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6630 free(tmp_branch_name);
6634 static const struct got_error *
6635 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6636 const char *diff_path, char **logmsg, void *arg)
6642 static const struct got_error *
6643 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6644 const char *path, struct got_object_id *blob_id,
6645 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6646 int dirfd, const char *de_name)
6651 struct collect_merged_paths_arg {
6652 got_worktree_checkout_cb progress_cb;
6654 struct got_pathlist_head *merged_paths;
6657 static const struct got_error *
6658 collect_merged_paths(void *arg, unsigned char status, const char *path)
6660 const struct got_error *err;
6661 struct collect_merged_paths_arg *a = arg;
6663 struct got_pathlist_entry *new;
6665 err = (*a->progress_cb)(a->progress_arg, status, path);
6669 if (status != GOT_STATUS_MERGE &&
6670 status != GOT_STATUS_ADD &&
6671 status != GOT_STATUS_DELETE &&
6672 status != GOT_STATUS_CONFLICT)
6677 return got_error_from_errno("strdup");
6679 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6680 if (err || new == NULL)
6685 static const struct got_error *
6686 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6687 int is_rebase, struct got_repository *repo)
6689 const struct got_error *err;
6690 struct got_reference *commit_ref = NULL;
6692 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6694 if (err->code != GOT_ERR_NOT_REF)
6696 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6699 err = got_ref_write(commit_ref, repo);
6702 } else if (is_rebase) {
6703 struct got_object_id *stored_id;
6706 err = got_ref_resolve(&stored_id, repo, commit_ref);
6709 cmp = got_object_id_cmp(commit_id, stored_id);
6712 err = got_error(GOT_ERR_REBASE_COMMITID);
6718 got_ref_close(commit_ref);
6722 static const struct got_error *
6723 rebase_merge_files(struct got_pathlist_head *merged_paths,
6724 const char *commit_ref_name, struct got_worktree *worktree,
6725 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6726 struct got_object_id *commit_id, struct got_repository *repo,
6727 got_worktree_checkout_cb progress_cb, void *progress_arg,
6728 got_cancel_cb cancel_cb, void *cancel_arg)
6730 const struct got_error *err;
6731 struct got_reference *commit_ref = NULL;
6732 struct collect_merged_paths_arg cmp_arg;
6733 char *fileindex_path;
6735 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6737 err = get_fileindex_path(&fileindex_path, worktree);
6741 cmp_arg.progress_cb = progress_cb;
6742 cmp_arg.progress_arg = progress_arg;
6743 cmp_arg.merged_paths = merged_paths;
6744 err = merge_files(worktree, fileindex, fileindex_path,
6745 parent_commit_id, commit_id, repo, collect_merged_paths,
6746 &cmp_arg, cancel_cb, cancel_arg);
6748 got_ref_close(commit_ref);
6752 const struct got_error *
6753 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6754 struct got_worktree *worktree, struct got_fileindex *fileindex,
6755 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6756 struct got_repository *repo,
6757 got_worktree_checkout_cb progress_cb, void *progress_arg,
6758 got_cancel_cb cancel_cb, void *cancel_arg)
6760 const struct got_error *err;
6761 char *commit_ref_name;
6763 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6767 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6771 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6772 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6773 progress_arg, cancel_cb, cancel_arg);
6775 free(commit_ref_name);
6779 const struct got_error *
6780 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6781 struct got_worktree *worktree, struct got_fileindex *fileindex,
6782 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6783 struct got_repository *repo,
6784 got_worktree_checkout_cb progress_cb, void *progress_arg,
6785 got_cancel_cb cancel_cb, void *cancel_arg)
6787 const struct got_error *err;
6788 char *commit_ref_name;
6790 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6794 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6798 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6799 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6800 progress_arg, cancel_cb, cancel_arg);
6802 free(commit_ref_name);
6806 static const struct got_error *
6807 rebase_commit(struct got_object_id **new_commit_id,
6808 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6809 struct got_worktree *worktree, struct got_fileindex *fileindex,
6810 struct got_reference *tmp_branch, const char *committer,
6811 struct got_commit_object *orig_commit, const char *new_logmsg,
6812 int allow_conflict, struct got_repository *repo)
6814 const struct got_error *err, *sync_err;
6815 struct got_pathlist_head commitable_paths;
6816 struct collect_commitables_arg cc_arg;
6817 char *fileindex_path = NULL;
6818 struct got_reference *head_ref = NULL;
6819 struct got_object_id *head_commit_id = NULL;
6820 char *logmsg = NULL;
6822 memset(&cc_arg, 0, sizeof(cc_arg));
6823 TAILQ_INIT(&commitable_paths);
6824 *new_commit_id = NULL;
6826 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6828 err = get_fileindex_path(&fileindex_path, worktree);
6832 cc_arg.commitable_paths = &commitable_paths;
6833 cc_arg.worktree = worktree;
6835 cc_arg.have_staged_files = 0;
6836 cc_arg.commit_conflicts = allow_conflict;
6838 * If possible get the status of individual files directly to
6839 * avoid crawling the entire work tree once per rebased commit.
6841 * Ideally, merged_paths would contain a list of commitables
6842 * we could use so we could skip worktree_status() entirely.
6843 * However, we would then need carefully keep track of cumulative
6844 * effects of operations such as file additions and deletions
6845 * in 'got histedit -f' (folding multiple commits into one),
6846 * and this extra complexity is not really worth it.
6849 struct got_pathlist_entry *pe;
6850 TAILQ_FOREACH(pe, merged_paths, entry) {
6851 err = worktree_status(worktree, pe->path, fileindex,
6852 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6858 err = worktree_status(worktree, "", fileindex, repo,
6859 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6864 if (TAILQ_EMPTY(&commitable_paths)) {
6865 /* No-op change; commit will be elided. */
6866 err = got_ref_delete(commit_ref, repo);
6869 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6873 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6877 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6882 logmsg = strdup(new_logmsg);
6883 if (logmsg == NULL) {
6884 err = got_error_from_errno("strdup");
6888 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6893 /* NB: commit_worktree will call free(logmsg) */
6894 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6895 NULL, worktree, got_object_commit_get_author(orig_commit),
6896 committer ? committer :
6897 got_object_commit_get_committer(orig_commit), NULL,
6898 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6902 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6906 err = got_ref_delete(commit_ref, repo);
6910 err = update_fileindex_after_commit(worktree, &commitable_paths,
6911 *new_commit_id, fileindex, 0);
6912 sync_err = sync_fileindex(fileindex, fileindex_path);
6913 if (sync_err && err == NULL)
6916 free(fileindex_path);
6917 free(head_commit_id);
6919 got_ref_close(head_ref);
6921 free(*new_commit_id);
6922 *new_commit_id = NULL;
6927 const struct got_error *
6928 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6929 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6930 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6931 const char *committer, struct got_commit_object *orig_commit,
6932 struct got_object_id *orig_commit_id, int allow_conflict,
6933 struct got_repository *repo)
6935 const struct got_error *err;
6936 char *commit_ref_name;
6937 struct got_reference *commit_ref = NULL;
6938 struct got_object_id *commit_id = NULL;
6940 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6944 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6947 err = got_ref_resolve(&commit_id, repo, commit_ref);
6950 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6951 err = got_error(GOT_ERR_REBASE_COMMITID);
6955 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6956 worktree, fileindex, tmp_branch, committer, orig_commit,
6957 NULL, allow_conflict, repo);
6960 got_ref_close(commit_ref);
6961 free(commit_ref_name);
6966 const struct got_error *
6967 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6968 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6969 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6970 const char *committer, struct got_commit_object *orig_commit,
6971 struct got_object_id *orig_commit_id, const char *new_logmsg,
6972 int allow_conflict, struct got_repository *repo)
6974 const struct got_error *err;
6975 char *commit_ref_name;
6976 struct got_reference *commit_ref = NULL;
6978 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6982 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6986 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6987 worktree, fileindex, tmp_branch, committer, orig_commit,
6988 new_logmsg, allow_conflict, repo);
6991 got_ref_close(commit_ref);
6992 free(commit_ref_name);
6996 const struct got_error *
6997 got_worktree_rebase_postpone(struct got_worktree *worktree,
6998 struct got_fileindex *fileindex)
7001 got_fileindex_free(fileindex);
7002 return lock_worktree(worktree, LOCK_SH);
7005 static const struct got_error *
7006 delete_ref(const char *name, struct got_repository *repo)
7008 const struct got_error *err;
7009 struct got_reference *ref;
7011 err = got_ref_open(&ref, repo, name, 0);
7013 if (err->code == GOT_ERR_NOT_REF)
7018 err = got_ref_delete(ref, repo);
7023 static const struct got_error *
7024 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7026 const struct got_error *err;
7027 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7028 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7030 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7033 err = delete_ref(tmp_branch_name, repo);
7037 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7040 err = delete_ref(new_base_branch_ref_name, repo);
7044 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7047 err = delete_ref(branch_ref_name, repo);
7051 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7054 err = delete_ref(commit_ref_name, repo);
7059 free(tmp_branch_name);
7060 free(new_base_branch_ref_name);
7061 free(branch_ref_name);
7062 free(commit_ref_name);
7066 static const struct got_error *
7067 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7068 struct got_object_id *new_commit_id, struct got_repository *repo)
7070 const struct got_error *err;
7071 struct got_reference *ref = NULL;
7072 struct got_object_id *old_commit_id = NULL;
7073 const char *branch_name = NULL;
7074 char *new_id_str = NULL;
7075 char *refname = NULL;
7077 branch_name = got_ref_get_name(branch);
7078 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7079 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7082 err = got_object_id_str(&new_id_str, new_commit_id);
7086 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7087 new_id_str) == -1) {
7088 err = got_error_from_errno("asprintf");
7092 err = got_ref_resolve(&old_commit_id, repo, branch);
7096 err = got_ref_alloc(&ref, refname, old_commit_id);
7100 err = got_ref_write(ref, repo);
7104 free(old_commit_id);
7110 const struct got_error *
7111 got_worktree_rebase_complete(struct got_worktree *worktree,
7112 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7113 struct got_reference *rebased_branch, struct got_repository *repo,
7116 const struct got_error *err, *unlockerr, *sync_err;
7117 struct got_object_id *new_head_commit_id = NULL;
7118 char *fileindex_path = NULL;
7120 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7124 if (create_backup) {
7125 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7126 rebased_branch, new_head_commit_id, repo);
7131 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7135 err = got_ref_write(rebased_branch, repo);
7139 err = got_worktree_set_head_ref(worktree, rebased_branch);
7143 err = delete_rebase_refs(worktree, repo);
7147 err = get_fileindex_path(&fileindex_path, worktree);
7150 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7151 sync_err = sync_fileindex(fileindex, fileindex_path);
7152 if (sync_err && err == NULL)
7155 got_fileindex_free(fileindex);
7156 free(fileindex_path);
7157 free(new_head_commit_id);
7158 unlockerr = lock_worktree(worktree, LOCK_SH);
7159 if (unlockerr && err == NULL)
7164 const struct got_error *
7165 got_worktree_rebase_abort(struct got_worktree *worktree,
7166 struct got_fileindex *fileindex, struct got_repository *repo,
7167 struct got_reference *new_base_branch,
7168 got_worktree_checkout_cb progress_cb, void *progress_arg)
7170 const struct got_error *err, *unlockerr, *sync_err;
7171 struct got_reference *resolved = NULL;
7172 struct got_object_id *commit_id = NULL;
7173 struct got_commit_object *commit = NULL;
7174 char *fileindex_path = NULL;
7175 struct revert_file_args rfa;
7176 struct got_object_id *tree_id = NULL;
7178 err = lock_worktree(worktree, LOCK_EX);
7182 err = got_object_open_as_commit(&commit, repo,
7183 worktree->base_commit_id);
7187 err = got_ref_open(&resolved, repo,
7188 got_ref_get_symref_target(new_base_branch), 0);
7192 err = got_worktree_set_head_ref(worktree, resolved);
7197 * XXX commits to the base branch could have happened while
7198 * we were busy rebasing; should we store the original commit ID
7199 * when rebase begins and read it back here?
7201 err = got_ref_resolve(&commit_id, repo, resolved);
7205 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7209 err = got_object_id_by_path(&tree_id, repo, commit,
7210 worktree->path_prefix);
7214 err = delete_rebase_refs(worktree, repo);
7218 err = get_fileindex_path(&fileindex_path, worktree);
7222 rfa.worktree = worktree;
7223 rfa.fileindex = fileindex;
7224 rfa.progress_cb = progress_cb;
7225 rfa.progress_arg = progress_arg;
7226 rfa.patch_cb = NULL;
7227 rfa.patch_arg = NULL;
7229 rfa.unlink_added_files = 0;
7230 err = worktree_status(worktree, "", fileindex, repo,
7231 revert_file, &rfa, NULL, NULL, 1, 0);
7235 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7236 repo, progress_cb, progress_arg, NULL, NULL);
7238 sync_err = sync_fileindex(fileindex, fileindex_path);
7239 if (sync_err && err == NULL)
7242 got_ref_close(resolved);
7246 got_object_commit_close(commit);
7248 got_fileindex_free(fileindex);
7249 free(fileindex_path);
7251 unlockerr = lock_worktree(worktree, LOCK_SH);
7252 if (unlockerr && err == NULL)
7257 const struct got_error *
7258 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7259 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7260 struct got_fileindex **fileindex, struct got_worktree *worktree,
7261 struct got_repository *repo)
7263 const struct got_error *err = NULL;
7264 char *tmp_branch_name = NULL;
7265 char *branch_ref_name = NULL;
7266 char *base_commit_ref_name = NULL;
7267 char *fileindex_path = NULL;
7268 struct check_rebase_ok_arg ok_arg;
7269 struct got_reference *wt_branch = NULL;
7270 struct got_reference *base_commit_ref = NULL;
7274 *base_commit_id = NULL;
7277 err = lock_worktree(worktree, LOCK_EX);
7281 err = open_fileindex(fileindex, &fileindex_path, worktree);
7285 ok_arg.worktree = worktree;
7287 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7292 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7296 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7300 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7305 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7310 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7314 err = got_ref_write(*branch_ref, repo);
7318 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7319 worktree->base_commit_id);
7322 err = got_ref_write(base_commit_ref, repo);
7325 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7326 if (*base_commit_id == NULL) {
7327 err = got_error_from_errno("got_object_id_dup");
7331 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7332 worktree->base_commit_id);
7335 err = got_ref_write(*tmp_branch, repo);
7339 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7343 free(fileindex_path);
7344 free(tmp_branch_name);
7345 free(branch_ref_name);
7346 free(base_commit_ref_name);
7348 got_ref_close(wt_branch);
7351 got_ref_close(*branch_ref);
7355 got_ref_close(*tmp_branch);
7358 free(*base_commit_id);
7360 got_fileindex_free(*fileindex);
7363 lock_worktree(worktree, LOCK_SH);
7368 const struct got_error *
7369 got_worktree_histedit_postpone(struct got_worktree *worktree,
7370 struct got_fileindex *fileindex)
7373 got_fileindex_free(fileindex);
7374 return lock_worktree(worktree, LOCK_SH);
7377 const struct got_error *
7378 got_worktree_histedit_in_progress(int *in_progress,
7379 struct got_worktree *worktree)
7381 const struct got_error *err;
7382 char *tmp_branch_name = NULL;
7384 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7388 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7389 free(tmp_branch_name);
7393 const struct got_error *
7394 got_worktree_histedit_continue(struct got_object_id **commit_id,
7395 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7396 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7397 struct got_worktree *worktree, struct got_repository *repo)
7399 const struct got_error *err;
7400 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7401 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7402 struct got_reference *commit_ref = NULL;
7403 struct got_reference *base_commit_ref = NULL;
7404 char *fileindex_path = NULL;
7405 int have_staged_files = 0;
7409 *base_commit_id = NULL;
7412 err = lock_worktree(worktree, LOCK_EX);
7416 err = open_fileindex(fileindex, &fileindex_path, worktree);
7420 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7421 &have_staged_files);
7422 if (err && err->code != GOT_ERR_CANCELLED)
7424 if (have_staged_files) {
7425 err = got_error(GOT_ERR_STAGED_PATHS);
7429 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7433 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7437 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7441 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7446 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7450 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7453 err = got_ref_resolve(commit_id, repo, commit_ref);
7457 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7460 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7464 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7468 free(commit_ref_name);
7469 free(branch_ref_name);
7470 free(fileindex_path);
7472 got_ref_close(commit_ref);
7473 if (base_commit_ref)
7474 got_ref_close(base_commit_ref);
7478 free(*base_commit_id);
7479 *base_commit_id = NULL;
7481 got_ref_close(*tmp_branch);
7485 got_fileindex_free(*fileindex);
7488 lock_worktree(worktree, LOCK_EX);
7493 static const struct got_error *
7494 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7496 const struct got_error *err;
7497 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7498 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7500 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7503 err = delete_ref(tmp_branch_name, repo);
7507 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7511 err = delete_ref(base_commit_ref_name, repo);
7515 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7518 err = delete_ref(branch_ref_name, repo);
7522 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7525 err = delete_ref(commit_ref_name, repo);
7529 free(tmp_branch_name);
7530 free(base_commit_ref_name);
7531 free(branch_ref_name);
7532 free(commit_ref_name);
7536 const struct got_error *
7537 got_worktree_histedit_abort(struct got_worktree *worktree,
7538 struct got_fileindex *fileindex, struct got_repository *repo,
7539 struct got_reference *branch, struct got_object_id *base_commit_id,
7540 got_worktree_checkout_cb progress_cb, void *progress_arg)
7542 const struct got_error *err, *unlockerr, *sync_err;
7543 struct got_reference *resolved = NULL;
7544 char *fileindex_path = NULL;
7545 struct got_commit_object *commit = NULL;
7546 struct got_object_id *tree_id = NULL;
7547 struct revert_file_args rfa;
7549 err = lock_worktree(worktree, LOCK_EX);
7553 err = got_object_open_as_commit(&commit, repo,
7554 worktree->base_commit_id);
7558 err = got_ref_open(&resolved, repo,
7559 got_ref_get_symref_target(branch), 0);
7563 err = got_worktree_set_head_ref(worktree, resolved);
7567 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7571 err = got_object_id_by_path(&tree_id, repo, commit,
7572 worktree->path_prefix);
7576 err = delete_histedit_refs(worktree, repo);
7580 err = get_fileindex_path(&fileindex_path, worktree);
7584 rfa.worktree = worktree;
7585 rfa.fileindex = fileindex;
7586 rfa.progress_cb = progress_cb;
7587 rfa.progress_arg = progress_arg;
7588 rfa.patch_cb = NULL;
7589 rfa.patch_arg = NULL;
7591 rfa.unlink_added_files = 0;
7592 err = worktree_status(worktree, "", fileindex, repo,
7593 revert_file, &rfa, NULL, NULL, 1, 0);
7597 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7598 repo, progress_cb, progress_arg, NULL, NULL);
7600 sync_err = sync_fileindex(fileindex, fileindex_path);
7601 if (sync_err && err == NULL)
7604 got_ref_close(resolved);
7606 free(fileindex_path);
7608 unlockerr = lock_worktree(worktree, LOCK_SH);
7609 if (unlockerr && err == NULL)
7614 const struct got_error *
7615 got_worktree_histedit_complete(struct got_worktree *worktree,
7616 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7617 struct got_reference *edited_branch, struct got_repository *repo)
7619 const struct got_error *err, *unlockerr, *sync_err;
7620 struct got_object_id *new_head_commit_id = NULL;
7621 struct got_reference *resolved = NULL;
7622 char *fileindex_path = NULL;
7624 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7628 err = got_ref_open(&resolved, repo,
7629 got_ref_get_symref_target(edited_branch), 0);
7633 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7634 resolved, new_head_commit_id, repo);
7638 err = got_ref_change_ref(resolved, new_head_commit_id);
7642 err = got_ref_write(resolved, repo);
7646 err = got_worktree_set_head_ref(worktree, resolved);
7650 err = delete_histedit_refs(worktree, repo);
7654 err = get_fileindex_path(&fileindex_path, worktree);
7657 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7658 sync_err = sync_fileindex(fileindex, fileindex_path);
7659 if (sync_err && err == NULL)
7662 got_fileindex_free(fileindex);
7663 free(fileindex_path);
7664 free(new_head_commit_id);
7665 unlockerr = lock_worktree(worktree, LOCK_SH);
7666 if (unlockerr && err == NULL)
7671 const struct got_error *
7672 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7673 struct got_object_id *commit_id, struct got_repository *repo)
7675 const struct got_error *err;
7676 char *commit_ref_name;
7678 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7682 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7686 err = delete_ref(commit_ref_name, repo);
7688 free(commit_ref_name);
7692 const struct got_error *
7693 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7694 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7695 struct got_worktree *worktree, const char *refname,
7696 struct got_repository *repo)
7698 const struct got_error *err = NULL;
7699 char *fileindex_path = NULL;
7700 struct check_rebase_ok_arg ok_arg;
7704 *base_branch_ref = NULL;
7706 err = lock_worktree(worktree, LOCK_EX);
7710 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7711 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7712 "cannot integrate a branch into itself; "
7713 "update -b or different branch name required");
7717 err = open_fileindex(fileindex, &fileindex_path, worktree);
7721 /* Preconditions are the same as for rebase. */
7722 ok_arg.worktree = worktree;
7724 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7729 err = got_ref_open(branch_ref, repo, refname, 1);
7733 err = got_ref_open(base_branch_ref, repo,
7734 got_worktree_get_head_ref_name(worktree), 1);
7738 got_ref_close(*branch_ref);
7741 if (*base_branch_ref) {
7742 got_ref_close(*base_branch_ref);
7743 *base_branch_ref = NULL;
7746 got_fileindex_free(*fileindex);
7749 lock_worktree(worktree, LOCK_SH);
7754 const struct got_error *
7755 got_worktree_integrate_continue(struct got_worktree *worktree,
7756 struct got_fileindex *fileindex, struct got_repository *repo,
7757 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7758 got_worktree_checkout_cb progress_cb, void *progress_arg,
7759 got_cancel_cb cancel_cb, void *cancel_arg)
7761 const struct got_error *err = NULL, *sync_err, *unlockerr;
7762 char *fileindex_path = NULL;
7763 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7764 struct got_commit_object *commit = NULL;
7766 err = get_fileindex_path(&fileindex_path, worktree);
7770 err = got_ref_resolve(&commit_id, repo, branch_ref);
7774 err = got_object_open_as_commit(&commit, repo, commit_id);
7778 err = got_object_id_by_path(&tree_id, repo, commit,
7779 worktree->path_prefix);
7783 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7787 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7788 progress_cb, progress_arg, cancel_cb, cancel_arg);
7792 err = got_ref_change_ref(base_branch_ref, commit_id);
7796 err = got_ref_write(base_branch_ref, repo);
7800 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7802 sync_err = sync_fileindex(fileindex, fileindex_path);
7803 if (sync_err && err == NULL)
7807 unlockerr = got_ref_unlock(branch_ref);
7808 if (unlockerr && err == NULL)
7810 got_ref_close(branch_ref);
7812 unlockerr = got_ref_unlock(base_branch_ref);
7813 if (unlockerr && err == NULL)
7815 got_ref_close(base_branch_ref);
7817 got_fileindex_free(fileindex);
7818 free(fileindex_path);
7821 got_object_commit_close(commit);
7823 unlockerr = lock_worktree(worktree, LOCK_SH);
7824 if (unlockerr && err == NULL)
7829 const struct got_error *
7830 got_worktree_integrate_abort(struct got_worktree *worktree,
7831 struct got_fileindex *fileindex, struct got_repository *repo,
7832 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7834 const struct got_error *err = NULL, *unlockerr = NULL;
7836 got_fileindex_free(fileindex);
7838 err = lock_worktree(worktree, LOCK_SH);
7840 unlockerr = got_ref_unlock(branch_ref);
7841 if (unlockerr && err == NULL)
7843 got_ref_close(branch_ref);
7845 unlockerr = got_ref_unlock(base_branch_ref);
7846 if (unlockerr && err == NULL)
7848 got_ref_close(base_branch_ref);
7853 const struct got_error *
7854 got_worktree_merge_postpone(struct got_worktree *worktree,
7855 struct got_fileindex *fileindex)
7857 const struct got_error *err, *sync_err;
7858 char *fileindex_path = NULL;
7860 err = get_fileindex_path(&fileindex_path, worktree);
7864 sync_err = sync_fileindex(fileindex, fileindex_path);
7866 err = lock_worktree(worktree, LOCK_SH);
7867 if (sync_err && err == NULL)
7870 got_fileindex_free(fileindex);
7871 free(fileindex_path);
7875 static const struct got_error *
7876 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7878 const struct got_error *err;
7879 char *branch_refname = NULL, *commit_refname = NULL;
7881 err = get_merge_branch_ref_name(&branch_refname, worktree);
7884 err = delete_ref(branch_refname, repo);
7888 err = get_merge_commit_ref_name(&commit_refname, worktree);
7891 err = delete_ref(commit_refname, repo);
7896 free(branch_refname);
7897 free(commit_refname);
7901 struct merge_commit_msg_arg {
7902 struct got_worktree *worktree;
7903 const char *branch_name;
7906 static const struct got_error *
7907 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7908 const char *diff_path, char **logmsg, void *arg)
7910 struct merge_commit_msg_arg *a = arg;
7912 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7913 got_worktree_get_head_ref_name(a->worktree)) == -1)
7914 return got_error_from_errno("asprintf");
7920 const struct got_error *
7921 got_worktree_merge_branch(struct got_worktree *worktree,
7922 struct got_fileindex *fileindex,
7923 struct got_object_id *yca_commit_id,
7924 struct got_object_id *branch_tip,
7925 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7926 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7928 const struct got_error *err;
7929 char *fileindex_path = NULL;
7931 err = get_fileindex_path(&fileindex_path, worktree);
7935 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7940 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7941 branch_tip, repo, progress_cb, progress_arg,
7942 cancel_cb, cancel_arg);
7944 free(fileindex_path);
7948 const struct got_error *
7949 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7950 struct got_worktree *worktree, struct got_fileindex *fileindex,
7951 const char *author, const char *committer, int allow_bad_symlinks,
7952 struct got_object_id *branch_tip, const char *branch_name,
7953 int allow_conflict, struct got_repository *repo,
7954 got_worktree_status_cb status_cb, void *status_arg)
7957 const struct got_error *err = NULL, *sync_err;
7958 struct got_pathlist_head commitable_paths;
7959 struct collect_commitables_arg cc_arg;
7960 struct got_pathlist_entry *pe;
7961 struct got_reference *head_ref = NULL;
7962 struct got_object_id *head_commit_id = NULL;
7963 int have_staged_files = 0;
7964 struct merge_commit_msg_arg mcm_arg;
7965 char *fileindex_path = NULL;
7967 memset(&cc_arg, 0, sizeof(cc_arg));
7968 *new_commit_id = NULL;
7970 TAILQ_INIT(&commitable_paths);
7972 err = get_fileindex_path(&fileindex_path, worktree);
7976 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7980 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7984 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7985 &have_staged_files);
7986 if (err && err->code != GOT_ERR_CANCELLED)
7988 if (have_staged_files) {
7989 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7993 cc_arg.commitable_paths = &commitable_paths;
7994 cc_arg.worktree = worktree;
7995 cc_arg.fileindex = fileindex;
7997 cc_arg.have_staged_files = have_staged_files;
7998 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7999 cc_arg.commit_conflicts = allow_conflict;
8000 err = worktree_status(worktree, "", fileindex, repo,
8001 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8005 if (TAILQ_EMPTY(&commitable_paths)) {
8006 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8007 "merge of %s cannot proceed", branch_name);
8011 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8012 struct got_commitable *ct = pe->data;
8013 const char *ct_path = ct->in_repo_path;
8015 while (ct_path[0] == '/')
8017 err = check_out_of_date(ct_path, ct->status,
8018 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8019 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8025 mcm_arg.worktree = worktree;
8026 mcm_arg.branch_name = branch_name;
8027 err = commit_worktree(new_commit_id, &commitable_paths,
8028 head_commit_id, branch_tip, worktree, author, committer, NULL,
8029 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8033 err = update_fileindex_after_commit(worktree, &commitable_paths,
8034 *new_commit_id, fileindex, have_staged_files);
8035 sync_err = sync_fileindex(fileindex, fileindex_path);
8036 if (sync_err && err == NULL)
8039 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8040 struct got_commitable *ct = pe->data;
8042 free_commitable(ct);
8044 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8045 free(fileindex_path);
8049 const struct got_error *
8050 got_worktree_merge_complete(struct got_worktree *worktree,
8051 struct got_fileindex *fileindex, struct got_repository *repo)
8053 const struct got_error *err, *unlockerr, *sync_err;
8054 char *fileindex_path = NULL;
8056 err = delete_merge_refs(worktree, repo);
8060 err = get_fileindex_path(&fileindex_path, worktree);
8063 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8064 sync_err = sync_fileindex(fileindex, fileindex_path);
8065 if (sync_err && err == NULL)
8068 got_fileindex_free(fileindex);
8069 free(fileindex_path);
8070 unlockerr = lock_worktree(worktree, LOCK_SH);
8071 if (unlockerr && err == NULL)
8076 const struct got_error *
8077 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8078 struct got_repository *repo)
8080 const struct got_error *err;
8081 char *branch_refname = NULL;
8082 struct got_reference *branch_ref = NULL;
8086 err = get_merge_branch_ref_name(&branch_refname, worktree);
8089 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8090 free(branch_refname);
8092 if (err->code != GOT_ERR_NOT_REF)
8100 const struct got_error *got_worktree_merge_prepare(
8101 struct got_fileindex **fileindex, struct got_worktree *worktree,
8102 struct got_reference *branch, struct got_repository *repo)
8104 const struct got_error *err = NULL;
8105 char *fileindex_path = NULL;
8106 char *branch_refname = NULL, *commit_refname = NULL;
8107 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8108 struct got_reference *commit_ref = NULL;
8109 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8110 struct check_rebase_ok_arg ok_arg;
8114 err = lock_worktree(worktree, LOCK_EX);
8118 err = open_fileindex(fileindex, &fileindex_path, worktree);
8122 /* Preconditions are the same as for rebase. */
8123 ok_arg.worktree = worktree;
8125 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8130 err = get_merge_branch_ref_name(&branch_refname, worktree);
8134 err = get_merge_commit_ref_name(&commit_refname, worktree);
8138 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8143 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8147 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8148 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8152 err = got_ref_resolve(&branch_tip, repo, branch);
8156 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8159 err = got_ref_write(branch_ref, repo);
8163 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8166 err = got_ref_write(commit_ref, repo);
8171 free(branch_refname);
8172 free(commit_refname);
8173 free(fileindex_path);
8175 got_ref_close(branch_ref);
8177 got_ref_close(commit_ref);
8179 got_ref_close(wt_branch);
8180 free(wt_branch_tip);
8183 got_fileindex_free(*fileindex);
8186 lock_worktree(worktree, LOCK_SH);
8191 const struct got_error *
8192 got_worktree_merge_continue(char **branch_name,
8193 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8194 struct got_worktree *worktree, struct got_repository *repo)
8196 const struct got_error *err;
8197 char *commit_refname = NULL, *branch_refname = NULL;
8198 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8199 char *fileindex_path = NULL;
8200 int have_staged_files = 0;
8202 *branch_name = NULL;
8206 err = lock_worktree(worktree, LOCK_EX);
8210 err = open_fileindex(fileindex, &fileindex_path, worktree);
8214 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8215 &have_staged_files);
8216 if (err && err->code != GOT_ERR_CANCELLED)
8218 if (have_staged_files) {
8219 err = got_error(GOT_ERR_STAGED_PATHS);
8223 err = get_merge_branch_ref_name(&branch_refname, worktree);
8227 err = get_merge_commit_ref_name(&commit_refname, worktree);
8231 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8235 if (!got_ref_is_symbolic(branch_ref)) {
8236 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8237 "%s is not a symbolic reference",
8238 got_ref_get_name(branch_ref));
8241 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8242 if (*branch_name == NULL) {
8243 err = got_error_from_errno("strdup");
8247 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8251 err = got_ref_resolve(branch_tip, repo, commit_ref);
8255 free(commit_refname);
8256 free(branch_refname);
8257 free(fileindex_path);
8259 got_ref_close(commit_ref);
8261 got_ref_close(branch_ref);
8265 *branch_name = NULL;
8270 got_fileindex_free(*fileindex);
8273 lock_worktree(worktree, LOCK_SH);
8278 const struct got_error *
8279 got_worktree_merge_abort(struct got_worktree *worktree,
8280 struct got_fileindex *fileindex, struct got_repository *repo,
8281 got_worktree_checkout_cb progress_cb, void *progress_arg)
8283 const struct got_error *err, *unlockerr, *sync_err;
8284 struct got_object_id *commit_id = NULL;
8285 struct got_commit_object *commit = NULL;
8286 char *fileindex_path = NULL;
8287 struct revert_file_args rfa;
8288 struct got_object_id *tree_id = NULL;
8290 err = got_object_open_as_commit(&commit, repo,
8291 worktree->base_commit_id);
8295 err = got_object_id_by_path(&tree_id, repo, commit,
8296 worktree->path_prefix);
8300 err = delete_merge_refs(worktree, repo);
8304 err = get_fileindex_path(&fileindex_path, worktree);
8308 rfa.worktree = worktree;
8309 rfa.fileindex = fileindex;
8310 rfa.progress_cb = progress_cb;
8311 rfa.progress_arg = progress_arg;
8312 rfa.patch_cb = NULL;
8313 rfa.patch_arg = NULL;
8315 rfa.unlink_added_files = 1;
8316 err = worktree_status(worktree, "", fileindex, repo,
8317 revert_file, &rfa, NULL, NULL, 1, 0);
8321 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8322 repo, progress_cb, progress_arg, NULL, NULL);
8324 sync_err = sync_fileindex(fileindex, fileindex_path);
8325 if (sync_err && err == NULL)
8331 got_object_commit_close(commit);
8333 got_fileindex_free(fileindex);
8334 free(fileindex_path);
8336 unlockerr = lock_worktree(worktree, LOCK_SH);
8337 if (unlockerr && err == NULL)
8342 struct check_stage_ok_arg {
8343 struct got_object_id *head_commit_id;
8344 struct got_worktree *worktree;
8345 struct got_fileindex *fileindex;
8346 struct got_repository *repo;
8350 static const struct got_error *
8351 check_stage_ok(void *arg, unsigned char status,
8352 unsigned char staged_status, const char *relpath,
8353 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8354 struct got_object_id *commit_id, int dirfd, const char *de_name)
8356 struct check_stage_ok_arg *a = arg;
8357 const struct got_error *err = NULL;
8358 struct got_fileindex_entry *ie;
8359 struct got_object_id base_commit_id;
8360 struct got_object_id *base_commit_idp = NULL;
8361 char *in_repo_path = NULL, *p;
8363 if (status == GOT_STATUS_UNVERSIONED ||
8364 status == GOT_STATUS_NO_CHANGE)
8366 if (status == GOT_STATUS_NONEXISTENT)
8367 return got_error_set_errno(ENOENT, relpath);
8369 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8371 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8373 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8374 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8376 return got_error_from_errno("asprintf");
8378 if (got_fileindex_entry_has_commit(ie)) {
8379 base_commit_idp = got_fileindex_entry_get_commit_id(
8380 &base_commit_id, ie);
8383 if (status == GOT_STATUS_CONFLICT) {
8384 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8386 } else if (status != GOT_STATUS_ADD &&
8387 status != GOT_STATUS_MODIFY &&
8388 status != GOT_STATUS_DELETE) {
8389 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8393 a->have_changes = 1;
8398 err = check_out_of_date(p, status, staged_status,
8399 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8400 GOT_ERR_STAGE_OUT_OF_DATE);
8406 struct stage_path_arg {
8407 struct got_worktree *worktree;
8408 struct got_fileindex *fileindex;
8409 struct got_repository *repo;
8410 got_worktree_status_cb status_cb;
8412 got_worktree_patch_cb patch_cb;
8414 int staged_something;
8415 int allow_bad_symlinks;
8418 static const struct got_error *
8419 stage_path(void *arg, unsigned char status,
8420 unsigned char staged_status, const char *relpath,
8421 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8422 struct got_object_id *commit_id, int dirfd, const char *de_name)
8424 struct stage_path_arg *a = arg;
8425 const struct got_error *err = NULL;
8426 struct got_fileindex_entry *ie;
8427 char *ondisk_path = NULL, *path_content = NULL;
8429 struct got_object_id *new_staged_blob_id = NULL;
8432 if (status == GOT_STATUS_UNVERSIONED)
8435 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8437 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8439 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8441 return got_error_from_errno("asprintf");
8444 case GOT_STATUS_ADD:
8445 case GOT_STATUS_MODIFY:
8446 /* XXX could sb.st_mode be passed in by our caller? */
8447 if (lstat(ondisk_path, &sb) == -1) {
8448 err = got_error_from_errno2("lstat", ondisk_path);
8452 if (status == GOT_STATUS_ADD) {
8453 int choice = GOT_PATCH_CHOICE_NONE;
8454 err = (*a->patch_cb)(&choice, a->patch_arg,
8455 status, ie->path, NULL, 1, 1);
8458 if (choice != GOT_PATCH_CHOICE_YES)
8461 err = create_patched_content(&path_content, 0,
8462 staged_blob_id ? staged_blob_id : blob_id,
8463 ondisk_path, dirfd, de_name, ie->path,
8464 a->repo, a->patch_cb, a->patch_arg);
8465 if (err || path_content == NULL)
8469 err = got_object_blob_create(&new_staged_blob_id,
8470 path_content ? path_content : ondisk_path, a->repo);
8473 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8474 SHA1_DIGEST_LENGTH);
8475 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8476 stage = GOT_FILEIDX_STAGE_ADD;
8478 stage = GOT_FILEIDX_STAGE_MODIFY;
8479 got_fileindex_entry_stage_set(ie, stage);
8480 if (S_ISLNK(sb.st_mode)) {
8481 int is_bad_symlink = 0;
8482 if (!a->allow_bad_symlinks) {
8483 char target_path[PATH_MAX];
8485 target_len = readlink(ondisk_path, target_path,
8486 sizeof(target_path));
8487 if (target_len == -1) {
8488 err = got_error_from_errno2("readlink",
8492 err = is_bad_symlink_target(&is_bad_symlink,
8493 target_path, target_len, ondisk_path,
8494 a->worktree->root_path);
8497 if (is_bad_symlink) {
8498 err = got_error_path(ondisk_path,
8499 GOT_ERR_BAD_SYMLINK);
8504 got_fileindex_entry_staged_filetype_set(ie,
8505 GOT_FILEIDX_MODE_BAD_SYMLINK);
8507 got_fileindex_entry_staged_filetype_set(ie,
8508 GOT_FILEIDX_MODE_SYMLINK);
8510 got_fileindex_entry_staged_filetype_set(ie,
8511 GOT_FILEIDX_MODE_REGULAR_FILE);
8513 a->staged_something = 1;
8514 if (a->status_cb == NULL)
8516 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8517 get_staged_status(ie), relpath, blob_id,
8518 new_staged_blob_id, NULL, dirfd, de_name);
8522 * When staging the reverse of the staged diff,
8523 * implicitly unstage the file.
8525 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8526 sizeof(ie->blob_sha1)) == 0) {
8527 got_fileindex_entry_stage_set(ie,
8528 GOT_FILEIDX_STAGE_NONE);
8531 case GOT_STATUS_DELETE:
8532 if (staged_status == GOT_STATUS_DELETE)
8535 int choice = GOT_PATCH_CHOICE_NONE;
8536 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8537 ie->path, NULL, 1, 1);
8540 if (choice == GOT_PATCH_CHOICE_NO)
8542 if (choice != GOT_PATCH_CHOICE_YES) {
8543 err = got_error(GOT_ERR_PATCH_CHOICE);
8547 stage = GOT_FILEIDX_STAGE_DELETE;
8548 got_fileindex_entry_stage_set(ie, stage);
8549 a->staged_something = 1;
8550 if (a->status_cb == NULL)
8552 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8553 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8556 case GOT_STATUS_NO_CHANGE:
8558 case GOT_STATUS_CONFLICT:
8559 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8561 case GOT_STATUS_NONEXISTENT:
8562 err = got_error_set_errno(ENOENT, relpath);
8565 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8569 if (path_content && unlink(path_content) == -1 && err == NULL)
8570 err = got_error_from_errno2("unlink", path_content);
8573 free(new_staged_blob_id);
8577 const struct got_error *
8578 got_worktree_stage(struct got_worktree *worktree,
8579 struct got_pathlist_head *paths,
8580 got_worktree_status_cb status_cb, void *status_arg,
8581 got_worktree_patch_cb patch_cb, void *patch_arg,
8582 int allow_bad_symlinks, struct got_repository *repo)
8584 const struct got_error *err = NULL, *sync_err, *unlockerr;
8585 struct got_pathlist_entry *pe;
8586 struct got_fileindex *fileindex = NULL;
8587 char *fileindex_path = NULL;
8588 struct got_reference *head_ref = NULL;
8589 struct got_object_id *head_commit_id = NULL;
8590 struct check_stage_ok_arg oka;
8591 struct stage_path_arg spa;
8593 err = lock_worktree(worktree, LOCK_EX);
8597 err = got_ref_open(&head_ref, repo,
8598 got_worktree_get_head_ref_name(worktree), 0);
8601 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8604 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8608 /* Check pre-conditions before staging anything. */
8609 oka.head_commit_id = head_commit_id;
8610 oka.worktree = worktree;
8611 oka.fileindex = fileindex;
8613 oka.have_changes = 0;
8614 TAILQ_FOREACH(pe, paths, entry) {
8615 err = worktree_status(worktree, pe->path, fileindex, repo,
8616 check_stage_ok, &oka, NULL, NULL, 1, 0);
8620 if (!oka.have_changes) {
8621 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8625 spa.worktree = worktree;
8626 spa.fileindex = fileindex;
8628 spa.patch_cb = patch_cb;
8629 spa.patch_arg = patch_arg;
8630 spa.status_cb = status_cb;
8631 spa.status_arg = status_arg;
8632 spa.staged_something = 0;
8633 spa.allow_bad_symlinks = allow_bad_symlinks;
8634 TAILQ_FOREACH(pe, paths, entry) {
8635 err = worktree_status(worktree, pe->path, fileindex, repo,
8636 stage_path, &spa, NULL, NULL, 1, 0);
8640 if (!spa.staged_something) {
8641 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8645 sync_err = sync_fileindex(fileindex, fileindex_path);
8646 if (sync_err && err == NULL)
8650 got_ref_close(head_ref);
8651 free(head_commit_id);
8652 free(fileindex_path);
8654 got_fileindex_free(fileindex);
8655 unlockerr = lock_worktree(worktree, LOCK_SH);
8656 if (unlockerr && err == NULL)
8661 struct unstage_path_arg {
8662 struct got_worktree *worktree;
8663 struct got_fileindex *fileindex;
8664 struct got_repository *repo;
8665 got_worktree_checkout_cb progress_cb;
8667 got_worktree_patch_cb patch_cb;
8671 static const struct got_error *
8672 create_unstaged_content(char **path_unstaged_content,
8673 char **path_new_staged_content, struct got_object_id *blob_id,
8674 struct got_object_id *staged_blob_id, const char *relpath,
8675 struct got_repository *repo,
8676 got_worktree_patch_cb patch_cb, void *patch_arg)
8678 const struct got_error *err, *free_err;
8679 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8680 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8681 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8682 struct got_diffreg_result *diffreg_result = NULL;
8683 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8684 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8685 int fd1 = -1, fd2 = -1;
8687 *path_unstaged_content = NULL;
8688 *path_new_staged_content = NULL;
8690 err = got_object_id_str(&label1, blob_id);
8694 fd1 = got_opentempfd();
8696 err = got_error_from_errno("got_opentempfd");
8699 fd2 = got_opentempfd();
8701 err = got_error_from_errno("got_opentempfd");
8705 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8709 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8713 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8717 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8722 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8726 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8730 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8731 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8735 err = got_opentemp_named(path_unstaged_content, &outfile,
8736 "got-unstaged-content", "");
8739 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8740 "got-new-staged-content", "");
8744 if (fseek(f1, 0L, SEEK_SET) == -1) {
8745 err = got_ferror(f1, GOT_ERR_IO);
8748 if (fseek(f2, 0L, SEEK_SET) == -1) {
8749 err = got_ferror(f2, GOT_ERR_IO);
8752 /* Count the number of actual changes in the diff result. */
8753 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8754 struct diff_chunk_context cc = {};
8755 diff_chunk_context_load_change(&cc, &nchunks_used,
8756 diffreg_result->result, n, 0);
8759 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8761 err = apply_or_reject_change(&choice, &nchunks_used,
8762 diffreg_result->result, n, relpath, f1, f2,
8763 &line_cur1, &line_cur2,
8764 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8767 if (choice == GOT_PATCH_CHOICE_YES)
8770 have_rejected_content = 1;
8771 if (choice == GOT_PATCH_CHOICE_QUIT)
8774 if (have_content || have_rejected_content)
8775 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8776 outfile, rejectfile);
8779 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8780 err = got_error_from_errno("close");
8782 got_object_blob_close(blob);
8783 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8784 err = got_error_from_errno("close");
8786 got_object_blob_close(staged_blob);
8787 free_err = got_diffreg_result_free(diffreg_result);
8788 if (free_err && err == NULL)
8790 if (f1 && fclose(f1) == EOF && err == NULL)
8791 err = got_error_from_errno2("fclose", path1);
8792 if (f2 && fclose(f2) == EOF && err == NULL)
8793 err = got_error_from_errno2("fclose", path2);
8794 if (outfile && fclose(outfile) == EOF && err == NULL)
8795 err = got_error_from_errno2("fclose", *path_unstaged_content);
8796 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8797 err = got_error_from_errno2("fclose", *path_new_staged_content);
8798 if (path1 && unlink(path1) == -1 && err == NULL)
8799 err = got_error_from_errno2("unlink", path1);
8800 if (path2 && unlink(path2) == -1 && err == NULL)
8801 err = got_error_from_errno2("unlink", path2);
8802 if (err || !have_content) {
8803 if (*path_unstaged_content &&
8804 unlink(*path_unstaged_content) == -1 && err == NULL)
8805 err = got_error_from_errno2("unlink",
8806 *path_unstaged_content);
8807 free(*path_unstaged_content);
8808 *path_unstaged_content = NULL;
8810 if (err || !have_content || !have_rejected_content) {
8811 if (*path_new_staged_content &&
8812 unlink(*path_new_staged_content) == -1 && err == NULL)
8813 err = got_error_from_errno2("unlink",
8814 *path_new_staged_content);
8815 free(*path_new_staged_content);
8816 *path_new_staged_content = NULL;
8823 static const struct got_error *
8824 unstage_hunks(struct got_object_id *staged_blob_id,
8825 struct got_blob_object *blob_base,
8826 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8827 const char *ondisk_path, const char *label_orig,
8828 struct got_worktree *worktree, struct got_repository *repo,
8829 got_worktree_patch_cb patch_cb, void *patch_arg,
8830 got_worktree_checkout_cb progress_cb, void *progress_arg)
8832 const struct got_error *err = NULL;
8833 char *path_unstaged_content = NULL;
8834 char *path_new_staged_content = NULL;
8835 char *parent = NULL, *base_path = NULL;
8836 char *blob_base_path = NULL;
8837 struct got_object_id *new_staged_blob_id = NULL;
8838 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8841 err = create_unstaged_content(&path_unstaged_content,
8842 &path_new_staged_content, blob_id, staged_blob_id,
8843 ie->path, repo, patch_cb, patch_arg);
8847 if (path_unstaged_content == NULL)
8850 if (path_new_staged_content) {
8851 err = got_object_blob_create(&new_staged_blob_id,
8852 path_new_staged_content, repo);
8857 f = fopen(path_unstaged_content, "re");
8859 err = got_error_from_errno2("fopen",
8860 path_unstaged_content);
8863 if (fstat(fileno(f), &sb) == -1) {
8864 err = got_error_from_errno2("fstat", path_unstaged_content);
8867 if (got_fileindex_entry_staged_filetype_get(ie) ==
8868 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8869 char link_target[PATH_MAX];
8871 r = fread(link_target, 1, sizeof(link_target), f);
8872 if (r == 0 && ferror(f)) {
8873 err = got_error_from_errno("fread");
8876 if (r >= sizeof(link_target)) { /* should not happen */
8877 err = got_error(GOT_ERR_NO_SPACE);
8880 link_target[r] = '\0';
8881 err = merge_symlink(worktree, blob_base,
8882 ondisk_path, ie->path, label_orig, link_target,
8883 worktree->base_commit_id, repo, progress_cb,
8886 int local_changes_subsumed;
8888 err = got_path_dirname(&parent, ondisk_path);
8892 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8894 err = got_error_from_errno("asprintf");
8899 err = got_opentemp_named(&blob_base_path, &f_base,
8903 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8909 * In order the run a 3-way merge with a symlink we copy the symlink's
8910 * target path into a temporary file and use that file with diff3.
8912 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8913 err = dump_symlink_target_path_to_file(&f_deriv2,
8919 fd = open(ondisk_path,
8920 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8922 err = got_error_from_errno2("open", ondisk_path);
8925 f_deriv2 = fdopen(fd, "r");
8926 if (f_deriv2 == NULL) {
8927 err = got_error_from_errno2("fdopen", ondisk_path);
8933 err = merge_file(&local_changes_subsumed, worktree,
8934 f_base, f, f_deriv2, ondisk_path, ie->path,
8935 got_fileindex_perms_to_st(ie),
8936 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8937 repo, progress_cb, progress_arg);
8942 if (new_staged_blob_id) {
8943 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8944 SHA1_DIGEST_LENGTH);
8946 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8947 got_fileindex_entry_staged_filetype_set(ie, 0);
8950 free(new_staged_blob_id);
8951 if (path_unstaged_content &&
8952 unlink(path_unstaged_content) == -1 && err == NULL)
8953 err = got_error_from_errno2("unlink", path_unstaged_content);
8954 if (path_new_staged_content &&
8955 unlink(path_new_staged_content) == -1 && err == NULL)
8956 err = got_error_from_errno2("unlink", path_new_staged_content);
8957 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8958 err = got_error_from_errno2("unlink", blob_base_path);
8959 if (f_base && fclose(f_base) == EOF && err == NULL)
8960 err = got_error_from_errno2("fclose", path_unstaged_content);
8961 if (f && fclose(f) == EOF && err == NULL)
8962 err = got_error_from_errno2("fclose", path_unstaged_content);
8963 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8964 err = got_error_from_errno2("fclose", ondisk_path);
8965 free(path_unstaged_content);
8966 free(path_new_staged_content);
8967 free(blob_base_path);
8973 static const struct got_error *
8974 unstage_path(void *arg, unsigned char status,
8975 unsigned char staged_status, const char *relpath,
8976 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8977 struct got_object_id *commit_id, int dirfd, const char *de_name)
8979 const struct got_error *err = NULL;
8980 struct unstage_path_arg *a = arg;
8981 struct got_fileindex_entry *ie;
8982 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8983 char *ondisk_path = NULL;
8984 char *id_str = NULL, *label_orig = NULL;
8985 int local_changes_subsumed;
8987 int fd1 = -1, fd2 = -1;
8989 if (staged_status != GOT_STATUS_ADD &&
8990 staged_status != GOT_STATUS_MODIFY &&
8991 staged_status != GOT_STATUS_DELETE)
8994 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8996 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8998 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9000 return got_error_from_errno("asprintf");
9002 err = got_object_id_str(&id_str,
9003 commit_id ? commit_id : a->worktree->base_commit_id);
9006 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9008 err = got_error_from_errno("asprintf");
9012 fd1 = got_opentempfd();
9014 err = got_error_from_errno("got_opentempfd");
9017 fd2 = got_opentempfd();
9019 err = got_error_from_errno("got_opentempfd");
9023 switch (staged_status) {
9024 case GOT_STATUS_MODIFY:
9025 err = got_object_open_as_blob(&blob_base, a->repo,
9026 blob_id, 8192, fd1);
9030 case GOT_STATUS_ADD:
9032 if (staged_status == GOT_STATUS_ADD) {
9033 int choice = GOT_PATCH_CHOICE_NONE;
9034 err = (*a->patch_cb)(&choice, a->patch_arg,
9035 staged_status, ie->path, NULL, 1, 1);
9038 if (choice != GOT_PATCH_CHOICE_YES)
9041 err = unstage_hunks(staged_blob_id,
9042 blob_base, blob_id, ie, ondisk_path,
9043 label_orig, a->worktree, a->repo,
9044 a->patch_cb, a->patch_arg,
9045 a->progress_cb, a->progress_arg);
9046 break; /* Done with this file. */
9049 err = got_object_open_as_blob(&blob_staged, a->repo,
9050 staged_blob_id, 8192, fd2);
9053 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9054 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9055 case GOT_FILEIDX_MODE_REGULAR_FILE:
9056 err = merge_blob(&local_changes_subsumed, a->worktree,
9057 blob_base, ondisk_path, relpath,
9058 got_fileindex_perms_to_st(ie), label_orig,
9059 blob_staged, commit_id ? commit_id :
9060 a->worktree->base_commit_id, a->repo,
9061 a->progress_cb, a->progress_arg);
9063 case GOT_FILEIDX_MODE_SYMLINK:
9064 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9065 char *staged_target;
9066 err = got_object_blob_read_to_str(
9067 &staged_target, blob_staged);
9070 err = merge_symlink(a->worktree, blob_base,
9071 ondisk_path, relpath, label_orig,
9072 staged_target, commit_id ? commit_id :
9073 a->worktree->base_commit_id,
9074 a->repo, a->progress_cb, a->progress_arg);
9075 free(staged_target);
9077 err = merge_blob(&local_changes_subsumed,
9078 a->worktree, blob_base, ondisk_path,
9079 relpath, got_fileindex_perms_to_st(ie),
9080 label_orig, blob_staged,
9081 commit_id ? commit_id :
9082 a->worktree->base_commit_id, a->repo,
9083 a->progress_cb, a->progress_arg);
9087 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9091 got_fileindex_entry_stage_set(ie,
9092 GOT_FILEIDX_STAGE_NONE);
9093 got_fileindex_entry_staged_filetype_set(ie, 0);
9096 case GOT_STATUS_DELETE:
9098 int choice = GOT_PATCH_CHOICE_NONE;
9099 err = (*a->patch_cb)(&choice, a->patch_arg,
9100 staged_status, ie->path, NULL, 1, 1);
9103 if (choice == GOT_PATCH_CHOICE_NO)
9105 if (choice != GOT_PATCH_CHOICE_YES) {
9106 err = got_error(GOT_ERR_PATCH_CHOICE);
9110 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9111 got_fileindex_entry_staged_filetype_set(ie, 0);
9112 err = get_file_status(&status, &sb, ie, ondisk_path,
9113 dirfd, de_name, a->repo);
9116 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9121 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9122 err = got_error_from_errno("close");
9124 got_object_blob_close(blob_base);
9125 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9126 err = got_error_from_errno("close");
9128 got_object_blob_close(blob_staged);
9134 const struct got_error *
9135 got_worktree_unstage(struct got_worktree *worktree,
9136 struct got_pathlist_head *paths,
9137 got_worktree_checkout_cb progress_cb, void *progress_arg,
9138 got_worktree_patch_cb patch_cb, void *patch_arg,
9139 struct got_repository *repo)
9141 const struct got_error *err = NULL, *sync_err, *unlockerr;
9142 struct got_pathlist_entry *pe;
9143 struct got_fileindex *fileindex = NULL;
9144 char *fileindex_path = NULL;
9145 struct unstage_path_arg upa;
9147 err = lock_worktree(worktree, LOCK_EX);
9151 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9155 upa.worktree = worktree;
9156 upa.fileindex = fileindex;
9158 upa.progress_cb = progress_cb;
9159 upa.progress_arg = progress_arg;
9160 upa.patch_cb = patch_cb;
9161 upa.patch_arg = patch_arg;
9162 TAILQ_FOREACH(pe, paths, entry) {
9163 err = worktree_status(worktree, pe->path, fileindex, repo,
9164 unstage_path, &upa, NULL, NULL, 1, 0);
9169 sync_err = sync_fileindex(fileindex, fileindex_path);
9170 if (sync_err && err == NULL)
9173 free(fileindex_path);
9175 got_fileindex_free(fileindex);
9176 unlockerr = lock_worktree(worktree, LOCK_SH);
9177 if (unlockerr && err == NULL)
9182 struct report_file_info_arg {
9183 struct got_worktree *worktree;
9184 got_worktree_path_info_cb info_cb;
9186 struct got_pathlist_head *paths;
9187 got_cancel_cb cancel_cb;
9191 static const struct got_error *
9192 report_file_info(void *arg, struct got_fileindex_entry *ie)
9194 struct report_file_info_arg *a = arg;
9195 struct got_pathlist_entry *pe;
9196 struct got_object_id blob_id, staged_blob_id, commit_id;
9197 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9198 struct got_object_id *commit_idp = NULL;
9201 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9202 return got_error(GOT_ERR_CANCELLED);
9204 TAILQ_FOREACH(pe, a->paths, entry) {
9205 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9206 got_path_is_child(ie->path, pe->path, pe->path_len))
9209 if (pe == NULL) /* not found */
9212 if (got_fileindex_entry_has_blob(ie))
9213 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9214 stage = got_fileindex_entry_stage_get(ie);
9215 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9216 stage == GOT_FILEIDX_STAGE_ADD) {
9217 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9218 &staged_blob_id, ie);
9221 if (got_fileindex_entry_has_commit(ie))
9222 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9224 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9225 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9228 const struct got_error *
9229 got_worktree_path_info(struct got_worktree *worktree,
9230 struct got_pathlist_head *paths,
9231 got_worktree_path_info_cb info_cb, void *info_arg,
9232 got_cancel_cb cancel_cb, void *cancel_arg)
9235 const struct got_error *err = NULL, *unlockerr;
9236 struct got_fileindex *fileindex = NULL;
9237 char *fileindex_path = NULL;
9238 struct report_file_info_arg arg;
9240 err = lock_worktree(worktree, LOCK_SH);
9244 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9248 arg.worktree = worktree;
9249 arg.info_cb = info_cb;
9250 arg.info_arg = info_arg;
9252 arg.cancel_cb = cancel_cb;
9253 arg.cancel_arg = cancel_arg;
9254 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9257 free(fileindex_path);
9259 got_fileindex_free(fileindex);
9260 unlockerr = lock_worktree(worktree, LOCK_UN);
9261 if (unlockerr && err == NULL)
9266 static const struct got_error *
9267 patch_check_path(const char *p, char **path, unsigned char *status,
9268 unsigned char *staged_status, struct got_fileindex *fileindex,
9269 struct got_worktree *worktree, struct got_repository *repo)
9271 const struct got_error *err;
9272 struct got_fileindex_entry *ie;
9274 char *ondisk_path = NULL;
9276 err = got_worktree_resolve_path(path, worktree, p);
9280 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9281 *path[0] ? "/" : "", *path) == -1)
9282 return got_error_from_errno("asprintf");
9284 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9286 *staged_status = get_staged_status(ie);
9287 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9292 *staged_status = GOT_STATUS_NO_CHANGE;
9293 *status = GOT_STATUS_UNVERSIONED;
9294 if (lstat(ondisk_path, &sb) == -1) {
9295 if (errno != ENOENT) {
9296 err = got_error_from_errno2("lstat",
9300 *status = GOT_STATUS_NONEXISTENT;
9309 static const struct got_error *
9310 patch_can_rm(const char *path, unsigned char status,
9311 unsigned char staged_status)
9313 if (status == GOT_STATUS_NONEXISTENT)
9314 return got_error_set_errno(ENOENT, path);
9315 if (status != GOT_STATUS_NO_CHANGE &&
9316 status != GOT_STATUS_ADD &&
9317 status != GOT_STATUS_MODIFY &&
9318 status != GOT_STATUS_MODE_CHANGE)
9319 return got_error_path(path, GOT_ERR_FILE_STATUS);
9320 if (staged_status == GOT_STATUS_DELETE)
9321 return got_error_path(path, GOT_ERR_FILE_STATUS);
9325 static const struct got_error *
9326 patch_can_add(const char *path, unsigned char status)
9328 if (status != GOT_STATUS_NONEXISTENT)
9329 return got_error_path(path, GOT_ERR_FILE_STATUS);
9333 static const struct got_error *
9334 patch_can_edit(const char *path, unsigned char status,
9335 unsigned char staged_status)
9337 if (status == GOT_STATUS_NONEXISTENT)
9338 return got_error_set_errno(ENOENT, path);
9339 if (status != GOT_STATUS_NO_CHANGE &&
9340 status != GOT_STATUS_ADD &&
9341 status != GOT_STATUS_MODIFY)
9342 return got_error_path(path, GOT_ERR_FILE_STATUS);
9343 if (staged_status == GOT_STATUS_DELETE)
9344 return got_error_path(path, GOT_ERR_FILE_STATUS);
9348 const struct got_error *
9349 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9350 char **fileindex_path, struct got_worktree *worktree)
9352 return open_fileindex(fileindex, fileindex_path, worktree);
9355 const struct got_error *
9356 got_worktree_patch_check_path(const char *old, const char *new,
9357 char **oldpath, char **newpath, struct got_worktree *worktree,
9358 struct got_repository *repo, struct got_fileindex *fileindex)
9360 const struct got_error *err = NULL;
9361 int file_renamed = 0;
9362 unsigned char status_old, staged_status_old;
9363 unsigned char status_new, staged_status_new;
9368 err = patch_check_path(old != NULL ? old : new, oldpath,
9369 &status_old, &staged_status_old, fileindex, worktree, repo);
9373 err = patch_check_path(new != NULL ? new : old, newpath,
9374 &status_new, &staged_status_new, fileindex, worktree, repo);
9378 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9381 if (old != NULL && new == NULL)
9382 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9383 else if (file_renamed) {
9384 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9386 err = patch_can_add(*newpath, status_new);
9387 } else if (old == NULL)
9388 err = patch_can_add(*newpath, status_new);
9390 err = patch_can_edit(*newpath, status_new, staged_status_new);
9402 const struct got_error *
9403 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9404 struct got_worktree *worktree, struct got_fileindex *fileindex,
9405 got_worktree_checkout_cb progress_cb, void *progress_arg)
9407 struct schedule_addition_args saa;
9409 memset(&saa, 0, sizeof(saa));
9410 saa.worktree = worktree;
9411 saa.fileindex = fileindex;
9412 saa.progress_cb = progress_cb;
9413 saa.progress_arg = progress_arg;
9416 return worktree_status(worktree, path, fileindex, repo,
9417 schedule_addition, &saa, NULL, NULL, 1, 0);
9420 const struct got_error *
9421 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9422 struct got_worktree *worktree, struct got_fileindex *fileindex,
9423 got_worktree_delete_cb progress_cb, void *progress_arg)
9425 struct schedule_deletion_args sda;
9427 memset(&sda, 0, sizeof(sda));
9428 sda.worktree = worktree;
9429 sda.fileindex = fileindex;
9430 sda.progress_cb = progress_cb;
9431 sda.progress_arg = progress_arg;
9433 sda.delete_local_mods = 0;
9434 sda.keep_on_disk = 0;
9435 sda.ignore_missing_paths = 0;
9436 sda.status_codes = NULL;
9438 return worktree_status(worktree, path, fileindex, repo,
9439 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9442 const struct got_error *
9443 got_worktree_patch_complete(struct got_fileindex *fileindex,
9444 const char *fileindex_path)
9446 const struct got_error *err = NULL;
9448 err = sync_fileindex(fileindex, fileindex_path);
9449 got_fileindex_free(fileindex);