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.
33 #include "got_compat.h"
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_object.h"
40 #include "got_cancel.h"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
45 #include "got_lib_worktree.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
55 #include "got_lib_gotconfig.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #define GOT_MERGE_LABEL_MERGED "merged change"
62 #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 static const struct got_error *
65 create_meta_file(const char *path_got, const char *name, const char *content)
67 const struct got_error *err = NULL;
70 if (asprintf(&path, "%s/%s", path_got, name) == -1)
71 return got_error_from_errno("asprintf");
73 err = got_path_create_file(path, content);
78 static const struct got_error *
79 update_meta_file(const char *path_got, const char *name, const char *content)
81 const struct got_error *err = NULL;
86 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
87 err = got_error_from_errno("asprintf");
92 err = got_opentemp_named(&tmppath, &tmpfile, path);
97 int len = fprintf(tmpfile, "%s\n", content);
98 if (len != strlen(content) + 1) {
99 err = got_error_from_errno2("fprintf", tmppath);
104 if (rename(tmppath, path) != 0) {
105 err = got_error_from_errno3("rename", tmppath, path);
111 if (fclose(tmpfile) == EOF && err == NULL)
112 err = got_error_from_errno2("fclose", tmppath);
117 static const struct got_error *
118 write_head_ref(const char *path_got, struct got_reference *head_ref)
120 const struct got_error *err = NULL;
123 if (got_ref_is_symbolic(head_ref)) {
124 refstr = got_ref_to_str(head_ref);
126 return got_error_from_errno("got_ref_to_str");
128 refstr = strdup(got_ref_get_name(head_ref));
130 return got_error_from_errno("strdup");
132 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
137 const struct got_error *
138 got_worktree_init(const char *path, struct got_reference *head_ref,
139 const char *prefix, struct got_repository *repo)
141 const struct got_error *err = NULL;
142 struct got_object_id *commit_id = NULL;
144 uint32_t uuid_status;
146 char *path_got = NULL;
147 char *formatstr = NULL;
148 char *absprefix = NULL;
149 char *basestr = NULL;
150 char *uuidstr = NULL;
152 if (strcmp(path, got_repo_get_path(repo)) == 0) {
153 err = got_error(GOT_ERR_WORKTREE_REPO);
157 err = got_ref_resolve(&commit_id, repo, head_ref);
160 err = got_object_get_type(&obj_type, repo, commit_id);
163 if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 return got_error(GOT_ERR_OBJ_TYPE);
166 if (!got_path_is_absolute(prefix)) {
167 if (asprintf(&absprefix, "/%s", prefix) == -1)
168 return got_error_from_errno("asprintf");
171 /* Create top-level directory (may already exist). */
172 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 err = got_error_from_errno2("mkdir", path);
177 /* Create .got directory (may already exist). */
178 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 err = got_error_from_errno("asprintf");
182 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 err = got_error_from_errno2("mkdir", path_got);
187 /* Create an empty lock file. */
188 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 /* Create an empty file index. */
193 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 /* Write the HEAD reference. */
198 err = write_head_ref(path_got, head_ref);
202 /* Record our base commit. */
203 err = got_object_id_str(&basestr, commit_id);
206 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 /* Store path to repository. */
211 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
212 got_repo_get_path(repo));
216 /* Store in-repository path prefix. */
217 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
218 absprefix ? absprefix : prefix);
223 uuid_create(&uuid, &uuid_status);
224 if (uuid_status != uuid_s_ok) {
225 err = got_error_uuid(uuid_status, "uuid_create");
228 uuid_to_string(&uuid, &uuidstr, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_to_string");
233 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 /* Stamp work tree with format file. */
238 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
239 err = got_error_from_errno("asprintf");
242 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
256 const struct got_error *
257 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
258 const char *path_prefix)
260 char *absprefix = NULL;
262 if (!got_path_is_absolute(path_prefix)) {
263 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
264 return got_error_from_errno("asprintf");
266 *match = (strcmp(absprefix ? absprefix : path_prefix,
267 worktree->path_prefix) == 0);
273 got_worktree_get_head_ref_name(struct got_worktree *worktree)
275 return worktree->head_ref_name;
278 const struct got_error *
279 got_worktree_set_head_ref(struct got_worktree *worktree,
280 struct got_reference *head_ref)
282 const struct got_error *err = NULL;
283 char *path_got = NULL, *head_ref_name = NULL;
285 if (asprintf(&path_got, "%s/%s", worktree->root_path,
286 GOT_WORKTREE_GOT_DIR) == -1) {
287 err = got_error_from_errno("asprintf");
292 head_ref_name = strdup(got_ref_get_name(head_ref));
293 if (head_ref_name == NULL) {
294 err = got_error_from_errno("strdup");
298 err = write_head_ref(path_got, head_ref);
302 free(worktree->head_ref_name);
303 worktree->head_ref_name = head_ref_name;
311 struct got_object_id *
312 got_worktree_get_base_commit_id(struct got_worktree *worktree)
314 return worktree->base_commit_id;
317 const struct got_error *
318 got_worktree_set_base_commit_id(struct got_worktree *worktree,
319 struct got_repository *repo, struct got_object_id *commit_id)
321 const struct got_error *err;
322 struct got_object *obj = NULL;
324 char *path_got = NULL;
326 if (asprintf(&path_got, "%s/%s", worktree->root_path,
327 GOT_WORKTREE_GOT_DIR) == -1) {
328 err = got_error_from_errno("asprintf");
333 err = got_object_open(&obj, repo, commit_id);
337 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
338 err = got_error(GOT_ERR_OBJ_TYPE);
342 /* Record our base commit. */
343 err = got_object_id_str(&id_str, commit_id);
346 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 free(worktree->base_commit_id);
351 worktree->base_commit_id = got_object_id_dup(commit_id);
352 if (worktree->base_commit_id == NULL) {
353 err = got_error_from_errno("got_object_id_dup");
358 got_object_close(obj);
364 const struct got_gotconfig *
365 got_worktree_get_gotconfig(struct got_worktree *worktree)
367 return worktree->gotconfig;
370 static const struct got_error *
371 lock_worktree(struct got_worktree *worktree, int operation)
373 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
374 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
375 : got_error_from_errno2("flock",
376 got_worktree_get_root_path(worktree)));
380 static const struct got_error *
381 add_dir_on_disk(struct got_worktree *worktree, const char *path)
383 const struct got_error *err = NULL;
386 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
387 return got_error_from_errno("asprintf");
389 err = got_path_mkdir(abspath);
390 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
393 if (lstat(abspath, &sb) == -1) {
394 err = got_error_from_errno2("lstat", abspath);
395 } else if (!S_ISDIR(sb.st_mode)) {
396 /* TODO directory is obstructed; do something */
397 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
404 static const struct got_error *
405 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
407 const struct got_error *err = NULL;
410 size_t flen1 = 0, flen2 = 0;
415 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
416 if (flen1 == 0 && ferror(f1)) {
417 err = got_error_from_errno("fread");
420 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
421 if (flen2 == 0 && ferror(f2)) {
422 err = got_error_from_errno("fread");
429 } else if (flen2 == 0) {
433 } else if (flen1 == flen2) {
434 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
447 static const struct got_error *
448 check_files_equal(int *same, FILE *f1, FILE *f2)
455 if (fstat(fileno(f1), &sb) != 0)
456 return got_error_from_errno("fstat");
459 if (fstat(fileno(f2), &sb) != 0)
460 return got_error_from_errno("fstat");
463 if (size1 != size2) {
468 if (fseek(f1, 0L, SEEK_SET) == -1)
469 return got_ferror(f1, GOT_ERR_IO);
470 if (fseek(f2, 0L, SEEK_SET) == -1)
471 return got_ferror(f2, GOT_ERR_IO);
473 return check_file_contents_equal(same, f1, f2);
476 static const struct got_error *
477 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
485 if (fseek(f, 0L, SEEK_SET) == -1)
486 return got_ferror(f, GOT_ERR_IO);
489 flen = fread(fbuf, 1, sizeof(fbuf), f);
492 return got_error_from_errno("fread");
496 outlen = write(outfd, fbuf, flen);
498 return got_error_from_errno("write");
500 return got_error(GOT_ERR_IO);
507 static const struct got_error *
508 merge_binary_file(int *overlapcnt, int merged_fd,
509 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
510 const char *label_deriv, const char *label_orig, const char *label_deriv2,
511 const char *ondisk_path)
513 const struct got_error *err = NULL;
514 int same_content, changed_deriv, changed_deriv2;
515 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
516 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
517 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
518 char *base_path_orig = NULL, *base_path_deriv = NULL;
519 char *base_path_deriv2 = NULL;
523 err = check_files_equal(&same_content, f_deriv, f_deriv2);
528 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
530 err = check_files_equal(&same_content, f_deriv, f_orig);
533 changed_deriv = !same_content;
534 err = check_files_equal(&same_content, f_deriv2, f_orig);
537 changed_deriv2 = !same_content;
539 if (changed_deriv && changed_deriv2) {
541 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
542 err = got_error_from_errno("asprintf");
545 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
546 err = got_error_from_errno("asprintf");
549 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
550 err = got_error_from_errno("asprintf");
553 err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
568 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
571 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
574 if (dprintf(merged_fd, "Binary files differ and cannot be "
575 "merged automatically:\n") < 0) {
576 err = got_error_from_errno("dprintf");
579 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
580 GOT_DIFF_CONFLICT_MARKER_BEGIN,
581 label_deriv ? " " : "",
582 label_deriv ? label_deriv : "",
584 err = got_error_from_errno("dprintf");
588 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
589 GOT_DIFF_CONFLICT_MARKER_ORIG,
590 label_orig ? " " : "",
591 label_orig ? label_orig : "",
593 err = got_error_from_errno("dprintf");
597 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
598 GOT_DIFF_CONFLICT_MARKER_SEP,
600 GOT_DIFF_CONFLICT_MARKER_END,
601 label_deriv2 ? " " : "",
602 label_deriv2 ? label_deriv2 : "") < 0) {
603 err = got_error_from_errno("dprintf");
606 } else if (changed_deriv)
607 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
608 else if (changed_deriv2)
609 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
611 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
613 err = got_error_from_errno2("unlink", path_orig);
614 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
615 err = got_error_from_errno2("close", path_orig);
616 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
617 err = got_error_from_errno2("close", path_deriv);
618 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
619 err = got_error_from_errno2("close", path_deriv2);
623 free(base_path_orig);
624 free(base_path_deriv);
625 free(base_path_deriv2);
630 * Perform a 3-way merge where the file f_orig acts as the common
631 * ancestor, the file f_deriv acts as the first derived version,
632 * and the file f_deriv2 acts as the second derived version.
633 * The merge result will be written to a new file at ondisk_path; any
634 * existing file at this path will be replaced.
636 static const struct got_error *
637 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
638 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
639 const char *path, uint16_t st_mode,
640 const char *label_orig, const char *label_deriv, const char *label_deriv2,
641 enum got_diff_algorithm diff_algo, struct got_repository *repo,
642 got_worktree_checkout_cb progress_cb, void *progress_arg)
644 const struct got_error *err = NULL;
646 FILE *f_merged = NULL;
647 char *merged_path = NULL, *base_path = NULL;
651 *local_changes_subsumed = 0;
653 err = got_path_dirname(&parent, ondisk_path);
657 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
658 err = got_error_from_errno("asprintf");
662 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
667 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
669 if (err->code != GOT_ERR_FILE_BINARY)
671 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
672 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 err = (*progress_cb)(progress_arg,
679 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 if (fsync(merged_fd) != 0) {
684 err = got_error_from_errno("fsync");
688 f_merged = fdopen(merged_fd, "r");
689 if (f_merged == NULL) {
690 err = got_error_from_errno("fdopen");
695 /* Check if a clean merge has subsumed all local changes. */
696 if (overlapcnt == 0) {
697 err = check_files_equal(local_changes_subsumed, f_deriv,
703 if (fchmod(fileno(f_merged), st_mode) != 0) {
704 err = got_error_from_errno2("fchmod", merged_path);
708 if (rename(merged_path, ondisk_path) != 0) {
709 err = got_error_from_errno3("rename", merged_path,
718 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
719 err = got_error_from_errno("close");
720 if (f_merged && fclose(f_merged) == EOF && err == NULL)
721 err = got_error_from_errno("fclose");
728 static const struct got_error *
729 update_symlink(const char *ondisk_path, const char *target_path,
732 /* This is not atomic but matches what 'ln -sf' does. */
733 if (unlink(ondisk_path) == -1)
734 return got_error_from_errno2("unlink", ondisk_path);
735 if (symlink(target_path, ondisk_path) == -1)
736 return got_error_from_errno3("symlink", target_path,
742 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
743 * in the work tree with a file that contains conflict markers and the
744 * conflicting target paths of the original version, a "derived version"
745 * of a symlink from an incoming change, and a local version of the symlink.
747 * The original versions's target path can be NULL if it is not available,
748 * such as if both derived versions added a new symlink at the same path.
750 * The incoming derived symlink target is NULL in case the incoming change
751 * has deleted this symlink.
753 static const struct got_error *
754 install_symlink_conflict(const char *deriv_target,
755 struct got_object_id *deriv_base_commit_id, const char *orig_target,
756 const char *label_orig, const char *local_target, const char *ondisk_path)
758 const struct got_error *err;
759 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
762 err = got_object_id_str(&id_str, deriv_base_commit_id);
764 return got_error_from_errno("asprintf");
766 if (asprintf(&label_deriv, "%s: commit %s",
767 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
768 err = got_error_from_errno("asprintf");
772 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
777 err = got_error_from_errno2("fchmod", path);
781 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
782 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
783 deriv_target ? deriv_target : "(symlink was deleted)",
784 orig_target ? label_orig : "",
785 orig_target ? "\n" : "",
786 orig_target ? orig_target : "",
787 orig_target ? "\n" : "",
788 GOT_DIFF_CONFLICT_MARKER_SEP,
789 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
790 err = got_error_from_errno2("fprintf", path);
794 if (unlink(ondisk_path) == -1) {
795 err = got_error_from_errno2("unlink", ondisk_path);
798 if (rename(path, ondisk_path) == -1) {
799 err = got_error_from_errno3("rename", path, ondisk_path);
803 if (f != NULL && fclose(f) == EOF && err == NULL)
804 err = got_error_from_errno2("fclose", path);
811 /* forward declaration */
812 static const struct got_error *
813 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
814 const char *, const char *, uint16_t, const char *,
815 struct got_blob_object *, struct got_object_id *,
816 struct got_repository *, got_worktree_checkout_cb, void *);
819 * Merge a symlink into the work tree, where blob_orig acts as the common
820 * ancestor, deriv_target is the link target of the first derived version,
821 * and the symlink on disk acts as the second derived version.
822 * Assume that contents of both blobs represent symlinks.
824 static const struct got_error *
825 merge_symlink(struct got_worktree *worktree,
826 struct got_blob_object *blob_orig, const char *ondisk_path,
827 const char *path, const char *label_orig, const char *deriv_target,
828 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
829 got_worktree_checkout_cb progress_cb, void *progress_arg)
831 const struct got_error *err = NULL;
832 char *ancestor_target = NULL;
834 ssize_t ondisk_len, deriv_len;
835 char ondisk_target[PATH_MAX];
836 int have_local_change = 0;
837 int have_incoming_change = 0;
839 if (lstat(ondisk_path, &sb) == -1)
840 return got_error_from_errno2("lstat", ondisk_path);
842 ondisk_len = readlink(ondisk_path, ondisk_target,
843 sizeof(ondisk_target));
844 if (ondisk_len == -1) {
845 err = got_error_from_errno2("readlink",
849 ondisk_target[ondisk_len] = '\0';
852 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
857 if (ancestor_target == NULL ||
858 (ondisk_len != strlen(ancestor_target) ||
859 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
860 have_local_change = 1;
862 deriv_len = strlen(deriv_target);
863 if (ancestor_target == NULL ||
864 (deriv_len != strlen(ancestor_target) ||
865 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
866 have_incoming_change = 1;
868 if (!have_local_change && !have_incoming_change) {
869 if (ancestor_target) {
870 /* Both sides made the same change. */
871 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
873 } else if (deriv_len == ondisk_len &&
874 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
875 /* Both sides added the same symlink. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
879 /* Both sides added symlinks which don't match. */
880 err = install_symlink_conflict(deriv_target,
881 deriv_base_commit_id, ancestor_target,
882 label_orig, ondisk_target, ondisk_path);
885 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
888 } else if (!have_local_change && have_incoming_change) {
889 /* Apply the incoming change. */
890 err = update_symlink(ondisk_path, deriv_target,
891 strlen(deriv_target));
894 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
895 } else if (have_local_change && have_incoming_change) {
896 if (deriv_len == ondisk_len &&
897 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
898 /* Both sides made the same change. */
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
902 err = install_symlink_conflict(deriv_target,
903 deriv_base_commit_id, ancestor_target, label_orig,
904 ondisk_target, ondisk_path);
907 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 free(ancestor_target);
917 static const struct got_error *
918 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
920 const struct got_error *err = NULL;
921 char target_path[PATH_MAX];
930 return got_error_from_errno("got_opentemp");
931 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
932 if (target_len == -1) {
933 err = got_error_from_errno2("readlink", ondisk_path);
936 n = fwrite(target_path, 1, target_len, f);
937 if (n != target_len) {
938 err = got_ferror(f, GOT_ERR_IO);
941 if (fflush(f) == EOF) {
942 err = got_error_from_errno("fflush");
945 if (fseek(f, 0L, SEEK_SET) == -1) {
946 err = got_ferror(f, GOT_ERR_IO);
958 * Perform a 3-way merge where blob_orig acts as the common ancestor,
959 * blob_deriv acts as the first derived version, and the file on disk
960 * acts as the second derived version.
962 static const struct got_error *
963 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
964 struct got_blob_object *blob_orig, const char *ondisk_path,
965 const char *path, uint16_t st_mode, const char *label_orig,
966 struct got_blob_object *blob_deriv,
967 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
968 got_worktree_checkout_cb progress_cb, void *progress_arg)
970 const struct got_error *err = NULL;
971 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
972 char *blob_orig_path = NULL;
973 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
974 char *label_deriv = NULL, *parent = NULL;
976 *local_changes_subsumed = 0;
978 err = got_path_dirname(&parent, ondisk_path);
983 if (asprintf(&base_path, "%s/got-merge-blob-orig",
985 err = got_error_from_errno("asprintf");
990 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
993 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 * No common ancestor exists. This is an "add vs add" conflict
1001 * and we simply use an empty ancestor file to make both files
1002 * appear in the merged result in their entirety.
1004 f_orig = got_opentemp();
1005 if (f_orig == NULL) {
1006 err = got_error_from_errno("got_opentemp");
1011 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1012 err = got_error_from_errno("asprintf");
1017 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1020 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 err = got_object_id_str(&id_str, deriv_base_commit_id);
1028 if (asprintf(&label_deriv, "%s: commit %s",
1029 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1030 err = got_error_from_errno("asprintf");
1035 * In order the run a 3-way merge with a symlink we copy the symlink's
1036 * target path into a temporary file and use that file with diff3.
1038 if (S_ISLNK(st_mode)) {
1039 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1046 err = got_error_from_errno2("open", ondisk_path);
1049 f_deriv2 = fdopen(fd, "r");
1050 if (f_deriv2 == NULL) {
1051 err = got_error_from_errno2("fdopen", ondisk_path);
1057 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1058 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1059 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1061 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1062 err = got_error_from_errno("fclose");
1063 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1064 err = got_error_from_errno("fclose");
1065 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1068 if (blob_orig_path) {
1069 unlink(blob_orig_path);
1070 free(blob_orig_path);
1072 if (blob_deriv_path) {
1073 unlink(blob_deriv_path);
1074 free(blob_deriv_path);
1082 static const struct got_error *
1083 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1084 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1085 int wt_fd, const char *path, struct got_object_id *blob_id)
1087 const struct got_error *err = NULL;
1088 struct got_fileindex_entry *new_ie;
1092 err = got_fileindex_entry_alloc(&new_ie, path);
1096 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1097 blob_id->sha1, base_commit_id->sha1, 1);
1101 err = got_fileindex_entry_add(fileindex, new_ie);
1104 got_fileindex_entry_free(new_ie);
1111 get_ondisk_perms(int executable, mode_t st_mode)
1113 mode_t xbits = S_IXUSR;
1116 /* Map read bits to execute bits. */
1117 if (st_mode & S_IRGRP)
1119 if (st_mode & S_IROTH)
1121 return st_mode | xbits;
1124 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1127 /* forward declaration */
1128 static const struct got_error *
1129 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1130 const char *path, mode_t te_mode, mode_t st_mode,
1131 struct got_blob_object *blob, int restoring_missing_file,
1132 int reverting_versioned_file, int installing_bad_symlink,
1133 int path_is_unversioned, struct got_repository *repo,
1134 got_worktree_checkout_cb progress_cb, void *progress_arg);
1137 * This function assumes that the provided symlink target points at a
1138 * safe location in the work tree!
1140 static const struct got_error *
1141 replace_existing_symlink(int *did_something, const char *ondisk_path,
1142 const char *target_path, size_t target_len)
1144 const struct got_error *err = NULL;
1146 char etarget[PATH_MAX];
1152 * "Bad" symlinks (those pointing outside the work tree or into the
1153 * .got directory) are installed in the work tree as a regular file
1154 * which contains the bad symlink target path.
1155 * The new symlink target has already been checked for safety by our
1156 * caller. If we can successfully open a regular file then we simply
1157 * replace this file with a symlink below.
1159 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1161 if (!got_err_open_nofollow_on_symlink())
1162 return got_error_from_errno2("open", ondisk_path);
1164 /* We are updating an existing on-disk symlink. */
1165 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1167 return got_error_from_errno2("readlink", ondisk_path);
1169 if (elen == target_len &&
1170 memcmp(etarget, target_path, target_len) == 0)
1171 return NULL; /* nothing to do */
1175 err = update_symlink(ondisk_path, target_path, target_len);
1176 if (fd != -1 && close(fd) == -1 && err == NULL)
1177 err = got_error_from_errno2("close", ondisk_path);
1181 static const struct got_error *
1182 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1183 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1185 const struct got_error *err = NULL;
1186 char canonpath[PATH_MAX];
1187 char *path_got = NULL;
1189 *is_bad_symlink = 0;
1191 if (target_len >= sizeof(canonpath)) {
1192 *is_bad_symlink = 1;
1197 * We do not use realpath(3) to resolve the symlink's target
1198 * path because we don't want to resolve symlinks recursively.
1199 * Instead we make the path absolute and then canonicalize it.
1200 * Relative symlink target lookup should begin at the directory
1201 * in which the blob object is being installed.
1203 if (!got_path_is_absolute(target_path)) {
1204 char *abspath, *parent;
1205 err = got_path_dirname(&parent, ondisk_path);
1208 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1210 return got_error_from_errno("asprintf");
1213 if (strlen(abspath) >= sizeof(canonpath)) {
1214 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1223 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1228 /* Only allow symlinks pointing at paths within the work tree. */
1229 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1230 *is_bad_symlink = 1;
1234 /* Do not allow symlinks pointing into the .got directory. */
1235 if (asprintf(&path_got, "%s/%s", wtroot_path,
1236 GOT_WORKTREE_GOT_DIR) == -1)
1237 return got_error_from_errno("asprintf");
1238 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1239 *is_bad_symlink = 1;
1245 static const struct got_error *
1246 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1247 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1248 int restoring_missing_file, int reverting_versioned_file,
1249 int path_is_unversioned, int allow_bad_symlinks,
1250 struct got_repository *repo,
1251 got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 const struct got_error *err = NULL;
1254 char target_path[PATH_MAX];
1255 size_t len, target_len = 0;
1256 char *path_got = NULL;
1257 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1258 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1260 *is_bad_symlink = 0;
1263 * Blob object content specifies the target path of the link.
1264 * If a symbolic link cannot be installed we instead create
1265 * a regular file which contains the link target path stored
1266 * in the blob object.
1269 err = got_object_blob_read_block(&len, blob);
1270 if (len + target_len >= sizeof(target_path)) {
1271 /* Path too long; install as a regular file. */
1272 *is_bad_symlink = 1;
1273 got_object_blob_rewind(blob);
1274 return install_blob(worktree, ondisk_path, path,
1275 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1276 restoring_missing_file, reverting_versioned_file,
1277 1, path_is_unversioned, repo, progress_cb,
1281 /* Skip blob object header first time around. */
1282 memcpy(target_path + target_len, buf + hdrlen,
1284 target_len += len - hdrlen;
1288 target_path[target_len] = '\0';
1290 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1291 ondisk_path, worktree->root_path);
1295 if (*is_bad_symlink && !allow_bad_symlinks) {
1296 /* install as a regular file */
1297 got_object_blob_rewind(blob);
1298 err = install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file, 1,
1301 path_is_unversioned, repo, progress_cb, progress_arg);
1305 if (symlink(target_path, ondisk_path) == -1) {
1306 if (errno == EEXIST) {
1307 int symlink_replaced;
1308 if (path_is_unversioned) {
1309 err = (*progress_cb)(progress_arg,
1310 GOT_STATUS_UNVERSIONED, path);
1313 err = replace_existing_symlink(&symlink_replaced,
1314 ondisk_path, target_path, target_len);
1318 if (symlink_replaced) {
1319 err = (*progress_cb)(progress_arg,
1320 reverting_versioned_file ?
1322 GOT_STATUS_UPDATE, path);
1324 err = (*progress_cb)(progress_arg,
1325 GOT_STATUS_EXISTS, path);
1328 goto done; /* Nothing else to do. */
1331 if (errno == ENOENT) {
1333 err = got_path_dirname(&parent, ondisk_path);
1336 err = add_dir_on_disk(worktree, parent);
1341 * Retry, and fall through to error handling
1342 * below if this second attempt fails.
1344 if (symlink(target_path, ondisk_path) != -1) {
1345 err = NULL; /* success */
1350 /* Handle errors from first or second creation attempt. */
1351 if (errno == ENAMETOOLONG) {
1352 /* bad target path; install as a regular file */
1353 *is_bad_symlink = 1;
1354 got_object_blob_rewind(blob);
1355 err = install_blob(worktree, ondisk_path, path,
1356 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1357 restoring_missing_file, reverting_versioned_file, 1,
1358 path_is_unversioned, repo,
1359 progress_cb, progress_arg);
1360 } else if (errno == ENOTDIR) {
1361 err = got_error_path(ondisk_path,
1362 GOT_ERR_FILE_OBSTRUCTED);
1364 err = got_error_from_errno3("symlink",
1365 target_path, ondisk_path);
1367 } else if (progress_cb)
1368 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1369 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1375 static const struct got_error *
1376 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1377 const char *path, mode_t te_mode, mode_t st_mode,
1378 struct got_blob_object *blob, int restoring_missing_file,
1379 int reverting_versioned_file, int installing_bad_symlink,
1380 int path_is_unversioned, struct got_repository *repo,
1381 got_worktree_checkout_cb progress_cb, void *progress_arg)
1383 const struct got_error *err = NULL;
1387 char *tmppath = NULL;
1389 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1390 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1392 if (errno == ENOENT) {
1394 err = got_path_dirname(&parent, path);
1397 err = add_dir_on_disk(worktree, parent);
1401 fd = open(ondisk_path,
1402 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1403 GOT_DEFAULT_FILE_MODE);
1405 return got_error_from_errno2("open",
1407 } else if (errno == EEXIST) {
1408 if (path_is_unversioned) {
1409 err = (*progress_cb)(progress_arg,
1410 GOT_STATUS_UNVERSIONED, path);
1413 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1414 !S_ISREG(st_mode) && !installing_bad_symlink) {
1415 /* TODO file is obstructed; do something */
1416 err = got_error_path(ondisk_path,
1417 GOT_ERR_FILE_OBSTRUCTED);
1420 err = got_opentemp_named_fd(&tmppath, &fd,
1427 return got_error_from_errno2("open", ondisk_path);
1430 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1431 err = got_error_from_errno2("fchmod",
1432 update ? tmppath : ondisk_path);
1437 if (restoring_missing_file)
1438 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1440 else if (reverting_versioned_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1444 err = (*progress_cb)(progress_arg,
1445 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1450 hdrlen = got_object_blob_get_hdrlen(blob);
1452 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1453 err = got_object_blob_read_block(&len, blob);
1457 /* Skip blob object header first time around. */
1458 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1460 err = got_error_from_errno("write");
1462 } else if (outlen != len - hdrlen) {
1463 err = got_error(GOT_ERR_IO);
1470 if (fsync(fd) != 0) {
1471 err = got_error_from_errno("fsync");
1476 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1477 err = got_error_from_errno2("unlink", ondisk_path);
1480 if (rename(tmppath, ondisk_path) != 0) {
1481 err = got_error_from_errno3("rename", tmppath,
1490 if (fd != -1 && close(fd) == -1 && err == NULL)
1491 err = got_error_from_errno("close");
1492 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1493 err = got_error_from_errno2("unlink", tmppath);
1498 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1499 static const struct got_error *
1500 get_modified_file_content_status(unsigned char *status, FILE *f)
1502 const struct got_error *err = NULL;
1503 const char *markers[3] = {
1504 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1505 GOT_DIFF_CONFLICT_MARKER_SEP,
1506 GOT_DIFF_CONFLICT_MARKER_END
1510 size_t linesize = 0;
1513 while (*status == GOT_STATUS_MODIFY) {
1514 linelen = getline(&line, &linesize, f);
1515 if (linelen == -1) {
1518 err = got_ferror(f, GOT_ERR_IO);
1522 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1523 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1525 *status = GOT_STATUS_CONFLICT;
1536 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1538 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1539 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1543 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1545 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1546 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1547 ie->mtime_sec == sb->st_mtim.tv_sec &&
1548 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1549 ie->size == (sb->st_size & 0xffffffff) &&
1550 !xbit_differs(ie, sb->st_mode));
1553 static unsigned char
1554 get_staged_status(struct got_fileindex_entry *ie)
1556 switch (got_fileindex_entry_stage_get(ie)) {
1557 case GOT_FILEIDX_STAGE_ADD:
1558 return GOT_STATUS_ADD;
1559 case GOT_FILEIDX_STAGE_DELETE:
1560 return GOT_STATUS_DELETE;
1561 case GOT_FILEIDX_STAGE_MODIFY:
1562 return GOT_STATUS_MODIFY;
1564 return GOT_STATUS_NO_CHANGE;
1568 static const struct got_error *
1569 get_symlink_modification_status(unsigned char *status,
1570 struct got_fileindex_entry *ie, const char *abspath,
1571 int dirfd, const char *de_name, struct got_blob_object *blob)
1573 const struct got_error *err = NULL;
1574 char target_path[PATH_MAX];
1575 char etarget[PATH_MAX];
1577 size_t len, target_len = 0;
1578 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1581 *status = GOT_STATUS_NO_CHANGE;
1583 /* Blob object content specifies the target path of the link. */
1585 err = got_object_blob_read_block(&len, blob);
1588 if (len + target_len >= sizeof(target_path)) {
1590 * Should not happen. The blob contents were OK
1591 * when this symlink was installed.
1593 return got_error(GOT_ERR_NO_SPACE);
1596 /* Skip blob object header first time around. */
1597 memcpy(target_path + target_len, buf + hdrlen,
1599 target_len += len - hdrlen;
1603 target_path[target_len] = '\0';
1606 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1608 return got_error_from_errno2("readlinkat", abspath);
1610 elen = readlink(abspath, etarget, sizeof(etarget));
1612 return got_error_from_errno2("readlink", abspath);
1615 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1616 *status = GOT_STATUS_MODIFY;
1621 static const struct got_error *
1622 get_file_status(unsigned char *status, struct stat *sb,
1623 struct got_fileindex_entry *ie, const char *abspath,
1624 int dirfd, const char *de_name, struct got_repository *repo)
1626 const struct got_error *err = NULL;
1627 struct got_object_id id;
1632 struct got_blob_object *blob = NULL;
1634 unsigned char staged_status = get_staged_status(ie);
1636 *status = GOT_STATUS_NO_CHANGE;
1637 memset(sb, 0, sizeof(*sb));
1640 * Whenever the caller provides a directory descriptor and a
1641 * directory entry name for the file, use them! This prevents
1642 * race conditions if filesystem paths change beneath our feet.
1645 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1646 if (errno == ENOENT) {
1647 if (got_fileindex_entry_has_file_on_disk(ie))
1648 *status = GOT_STATUS_MISSING;
1650 *status = GOT_STATUS_DELETE;
1653 err = got_error_from_errno2("fstatat", abspath);
1657 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1658 if (fd == -1 && errno != ENOENT &&
1659 !got_err_open_nofollow_on_symlink())
1660 return got_error_from_errno2("open", abspath);
1661 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1662 if (lstat(abspath, sb) == -1)
1663 return got_error_from_errno2("lstat", abspath);
1664 } else if (fd == -1 || fstat(fd, sb) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1669 *status = GOT_STATUS_DELETE;
1672 err = got_error_from_errno2("fstat", abspath);
1677 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1678 *status = GOT_STATUS_OBSTRUCTED;
1682 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1683 *status = GOT_STATUS_DELETE;
1685 } else if (!got_fileindex_entry_has_blob(ie) &&
1686 staged_status != GOT_STATUS_ADD) {
1687 *status = GOT_STATUS_ADD;
1691 if (!stat_info_differs(ie, sb))
1694 if (S_ISLNK(sb->st_mode) &&
1695 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1696 *status = GOT_STATUS_MODIFY;
1700 if (staged_status == GOT_STATUS_MODIFY ||
1701 staged_status == GOT_STATUS_ADD)
1702 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1704 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1706 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1710 if (S_ISLNK(sb->st_mode)) {
1711 err = get_symlink_modification_status(status, ie,
1712 abspath, dirfd, de_name, blob);
1717 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1719 err = got_error_from_errno2("openat", abspath);
1724 f = fdopen(fd, "r");
1726 err = got_error_from_errno2("fdopen", abspath);
1730 hdrlen = got_object_blob_get_hdrlen(blob);
1732 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1733 err = got_object_blob_read_block(&blen, blob);
1736 /* Skip length of blob object header first time around. */
1737 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1738 if (flen == 0 && ferror(f)) {
1739 err = got_error_from_errno("fread");
1742 if (blen - hdrlen == 0) {
1744 *status = GOT_STATUS_MODIFY;
1746 } else if (flen == 0) {
1747 if (blen - hdrlen != 0)
1748 *status = GOT_STATUS_MODIFY;
1750 } else if (blen - hdrlen == flen) {
1751 /* Skip blob object header first time around. */
1752 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1753 *status = GOT_STATUS_MODIFY;
1757 *status = GOT_STATUS_MODIFY;
1763 if (*status == GOT_STATUS_MODIFY) {
1765 err = get_modified_file_content_status(status, f);
1766 } else if (xbit_differs(ie, sb->st_mode))
1767 *status = GOT_STATUS_MODE_CHANGE;
1770 got_object_blob_close(blob);
1771 if (f != NULL && fclose(f) == EOF && err == NULL)
1772 err = got_error_from_errno2("fclose", abspath);
1773 if (fd != -1 && close(fd) == -1 && err == NULL)
1774 err = got_error_from_errno2("close", abspath);
1779 * Update timestamps in the file index if a file is unmodified and
1780 * we had to run a full content comparison to find out.
1782 static const struct got_error *
1783 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1784 struct got_fileindex_entry *ie, struct stat *sb)
1786 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1787 return got_fileindex_entry_update(ie, wt_fd, path,
1788 ie->blob_sha1, ie->commit_sha1, 1);
1793 static const struct got_error *
1794 update_blob(struct got_worktree *worktree,
1795 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1796 struct got_tree_entry *te, const char *path,
1797 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1800 const struct got_error *err = NULL;
1801 struct got_blob_object *blob = NULL;
1803 unsigned char status = GOT_STATUS_NO_CHANGE;
1806 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1807 return got_error_from_errno("asprintf");
1810 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1811 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1814 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1818 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1819 sb.st_mode = got_fileindex_perms_to_st(ie);
1821 if (stat(ondisk_path, &sb) == -1) {
1822 if (errno != ENOENT) {
1823 err = got_error_from_errno2("stat",
1827 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1828 status = GOT_STATUS_UNVERSIONED;
1830 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1831 status = GOT_STATUS_UNVERSIONED;
1833 status = GOT_STATUS_OBSTRUCTED;
1837 if (status == GOT_STATUS_OBSTRUCTED) {
1839 got_fileindex_entry_mark_skipped(ie);
1840 err = (*progress_cb)(progress_arg, status, path);
1843 if (status == GOT_STATUS_CONFLICT) {
1845 got_fileindex_entry_mark_skipped(ie);
1846 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1851 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1852 (S_ISLNK(te->mode) ||
1853 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1855 * This is a regular file or an installed bad symlink.
1856 * If the file index indicates that this file is already
1857 * up-to-date with respect to the repository we can skip
1858 * updating contents of this file.
1860 if (got_fileindex_entry_has_commit(ie) &&
1861 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1862 SHA1_DIGEST_LENGTH) == 0) {
1864 err = sync_timestamps(worktree->root_fd,
1865 path, status, ie, &sb);
1868 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1872 if (got_fileindex_entry_has_blob(ie) &&
1873 memcmp(ie->blob_sha1, te->id.sha1,
1874 SHA1_DIGEST_LENGTH) == 0) {
1875 /* Different commit but the same blob. */
1876 err = sync_timestamps(worktree->root_fd,
1877 path, status, ie, &sb);
1880 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1886 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 int update_timestamps;
1892 struct got_blob_object *blob2 = NULL;
1893 char *label_orig = NULL;
1894 if (got_fileindex_entry_has_blob(ie)) {
1895 struct got_object_id id2;
1896 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 if (got_fileindex_entry_has_commit(ie)) {
1902 char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 sizeof(id_str)) == NULL) {
1905 err = got_error_path(id_str,
1906 GOT_ERR_BAD_OBJ_ID_STR);
1909 if (asprintf(&label_orig, "%s: commit %s",
1910 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 err = got_error_from_errno("asprintf");
1915 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1917 err = got_object_blob_read_to_str(&link_target, blob);
1920 err = merge_symlink(worktree, blob2, ondisk_path, path,
1921 label_orig, link_target, worktree->base_commit_id,
1922 repo, progress_cb, progress_arg);
1925 err = merge_blob(&update_timestamps, worktree, blob2,
1926 ondisk_path, path, sb.st_mode, label_orig, blob,
1927 worktree->base_commit_id, repo,
1928 progress_cb, progress_arg);
1932 got_object_blob_close(blob2);
1936 * Do not update timestamps of files with local changes.
1937 * Otherwise, a future status walk would treat them as
1938 * unmodified files again.
1940 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1941 blob->id.sha1, worktree->base_commit_id->sha1,
1943 } else if (status == GOT_STATUS_MODE_CHANGE) {
1944 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1945 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1946 } else if (status == GOT_STATUS_DELETE) {
1947 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1950 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1951 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1955 int is_bad_symlink = 0;
1956 if (S_ISLNK(te->mode)) {
1957 err = install_symlink(&is_bad_symlink, worktree,
1958 ondisk_path, path, blob,
1959 status == GOT_STATUS_MISSING, 0,
1960 status == GOT_STATUS_UNVERSIONED, 0,
1961 repo, progress_cb, progress_arg);
1963 err = install_blob(worktree, ondisk_path, path,
1964 te->mode, sb.st_mode, blob,
1965 status == GOT_STATUS_MISSING, 0, 0,
1966 status == GOT_STATUS_UNVERSIONED, repo,
1967 progress_cb, progress_arg);
1973 err = got_fileindex_entry_update(ie,
1974 worktree->root_fd, path, blob->id.sha1,
1975 worktree->base_commit_id->sha1, 1);
1977 err = create_fileindex_entry(&ie, fileindex,
1978 worktree->base_commit_id, worktree->root_fd, path,
1984 if (is_bad_symlink) {
1985 got_fileindex_entry_filetype_set(ie,
1986 GOT_FILEIDX_MODE_BAD_SYMLINK);
1989 got_object_blob_close(blob);
1995 static const struct got_error *
1996 remove_ondisk_file(const char *root_path, const char *path)
1998 const struct got_error *err = NULL;
1999 char *ondisk_path = NULL, *parent = NULL;
2001 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2002 return got_error_from_errno("asprintf");
2004 if (unlink(ondisk_path) == -1) {
2005 if (errno != ENOENT)
2006 err = got_error_from_errno2("unlink", ondisk_path);
2008 size_t root_len = strlen(root_path);
2009 err = got_path_dirname(&parent, ondisk_path);
2012 while (got_path_cmp(parent, root_path,
2013 strlen(parent), root_len) != 0) {
2015 ondisk_path = parent;
2017 if (rmdir(ondisk_path) == -1) {
2018 if (errno != ENOTEMPTY)
2019 err = got_error_from_errno2("rmdir",
2023 err = got_path_dirname(&parent, ondisk_path);
2034 static const struct got_error *
2035 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2036 struct got_fileindex_entry *ie, struct got_repository *repo,
2037 got_worktree_checkout_cb progress_cb, void *progress_arg)
2039 const struct got_error *err = NULL;
2040 unsigned char status;
2044 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2045 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2047 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2049 return got_error_from_errno("asprintf");
2051 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2055 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2056 char ondisk_target[PATH_MAX];
2057 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2058 sizeof(ondisk_target));
2059 if (ondisk_len == -1) {
2060 err = got_error_from_errno2("readlink", ondisk_path);
2063 ondisk_target[ondisk_len] = '\0';
2064 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2065 NULL, NULL, /* XXX pass common ancestor info? */
2066 ondisk_target, ondisk_path);
2069 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2074 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2075 status == GOT_STATUS_ADD) {
2076 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2080 * Preserve the working file and change the deleted blob's
2081 * entry into a schedule-add entry.
2083 err = got_fileindex_entry_update(ie, worktree->root_fd,
2084 ie->path, NULL, NULL, 0);
2086 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2089 if (status == GOT_STATUS_NO_CHANGE) {
2090 err = remove_ondisk_file(worktree->root_path, ie->path);
2094 got_fileindex_entry_remove(fileindex, ie);
2101 struct diff_cb_arg {
2102 struct got_fileindex *fileindex;
2103 struct got_worktree *worktree;
2104 struct got_repository *repo;
2105 got_worktree_checkout_cb progress_cb;
2107 got_cancel_cb cancel_cb;
2111 static const struct got_error *
2112 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2113 struct got_tree_entry *te, const char *parent_path)
2115 struct diff_cb_arg *a = arg;
2117 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2118 return got_error(GOT_ERR_CANCELLED);
2120 return update_blob(a->worktree, a->fileindex, ie, te,
2121 ie->path, a->repo, a->progress_cb, a->progress_arg);
2124 static const struct got_error *
2125 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2127 struct diff_cb_arg *a = arg;
2129 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2130 return got_error(GOT_ERR_CANCELLED);
2132 return delete_blob(a->worktree, a->fileindex, ie,
2133 a->repo, a->progress_cb, a->progress_arg);
2136 static const struct got_error *
2137 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2139 struct diff_cb_arg *a = arg;
2140 const struct got_error *err;
2143 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2144 return got_error(GOT_ERR_CANCELLED);
2146 if (got_object_tree_entry_is_submodule(te))
2149 if (asprintf(&path, "%s%s%s", parent_path,
2150 parent_path[0] ? "/" : "", te->name)
2152 return got_error_from_errno("asprintf");
2154 if (S_ISDIR(te->mode))
2155 err = add_dir_on_disk(a->worktree, path);
2157 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2158 a->repo, a->progress_cb, a->progress_arg);
2164 const struct got_error *
2165 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2167 uint32_t uuid_status;
2169 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2170 if (uuid_status != uuid_s_ok) {
2172 return got_error_uuid(uuid_status, "uuid_to_string");
2178 static const struct got_error *
2179 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2181 const struct got_error *err = NULL;
2182 char *uuidstr = NULL;
2186 err = got_worktree_get_uuid(&uuidstr, worktree);
2190 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2191 err = got_error_from_errno("asprintf");
2198 const struct got_error *
2199 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2201 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2204 static const struct got_error *
2205 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2207 return get_ref_name(refname, worktree,
2208 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2211 static const struct got_error *
2212 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2214 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2217 static const struct got_error *
2218 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2220 return get_ref_name(refname, worktree,
2221 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2224 static const struct got_error *
2225 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2227 return get_ref_name(refname, worktree,
2228 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2231 static const struct got_error *
2232 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2234 return get_ref_name(refname, worktree,
2235 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2238 static const struct got_error *
2239 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2241 return get_ref_name(refname, worktree,
2242 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2245 static const struct got_error *
2246 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2248 return get_ref_name(refname, worktree,
2249 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2252 static const struct got_error *
2253 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree,
2256 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2259 const struct got_error *
2260 got_worktree_get_histedit_script_path(char **path,
2261 struct got_worktree *worktree)
2263 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2264 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2266 return got_error_from_errno("asprintf");
2271 static const struct got_error *
2272 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree,
2275 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2278 static const struct got_error *
2279 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 return get_ref_name(refname, worktree,
2282 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2286 * Prevent Git's garbage collector from deleting our base commit by
2287 * setting a reference to our base commit's ID.
2289 static const struct got_error *
2290 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2292 const struct got_error *err = NULL;
2293 struct got_reference *ref = NULL;
2296 err = got_worktree_get_base_ref_name(&refname, worktree);
2300 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2304 err = got_ref_write(ref, repo);
2312 static const struct got_error *
2313 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2315 const struct got_error *err = NULL;
2317 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2319 err = got_error_from_errno("asprintf");
2320 *fileindex_path = NULL;
2326 static const struct got_error *
2327 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2328 struct got_worktree *worktree)
2330 const struct got_error *err = NULL;
2333 *fileindex_path = NULL;
2334 *fileindex = got_fileindex_alloc();
2335 if (*fileindex == NULL)
2336 return got_error_from_errno("got_fileindex_alloc");
2338 err = get_fileindex_path(fileindex_path, worktree);
2342 index = fopen(*fileindex_path, "rbe");
2343 if (index == NULL) {
2344 if (errno != ENOENT)
2345 err = got_error_from_errno2("fopen", *fileindex_path);
2347 err = got_fileindex_read(*fileindex, index);
2348 if (fclose(index) == EOF && err == NULL)
2349 err = got_error_from_errno("fclose");
2353 free(*fileindex_path);
2354 *fileindex_path = NULL;
2355 got_fileindex_free(*fileindex);
2361 struct bump_base_commit_id_arg {
2362 struct got_object_id *base_commit_id;
2365 const char *entry_name;
2366 got_worktree_checkout_cb progress_cb;
2370 /* Bump base commit ID of all files within an updated part of the work tree. */
2371 static const struct got_error *
2372 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2374 const struct got_error *err;
2375 struct bump_base_commit_id_arg *a = arg;
2377 if (a->entry_name) {
2378 if (strcmp(ie->path, a->path) != 0)
2380 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2383 if (got_fileindex_entry_was_skipped(ie))
2386 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2387 SHA1_DIGEST_LENGTH) == 0)
2390 if (a->progress_cb) {
2391 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2396 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2400 static const struct got_error *
2401 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2402 struct got_fileindex *fileindex,
2403 got_worktree_checkout_cb progress_cb, void *progress_arg)
2405 struct bump_base_commit_id_arg bbc_arg;
2407 bbc_arg.base_commit_id = worktree->base_commit_id;
2408 bbc_arg.entry_name = NULL;
2410 bbc_arg.path_len = 0;
2411 bbc_arg.progress_cb = progress_cb;
2412 bbc_arg.progress_arg = progress_arg;
2414 return got_fileindex_for_each_entry_safe(fileindex,
2415 bump_base_commit_id, &bbc_arg);
2418 static const struct got_error *
2419 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2421 const struct got_error *err = NULL;
2422 char *new_fileindex_path = NULL;
2423 FILE *new_index = NULL;
2424 struct timespec timeout;
2426 err = got_opentemp_named(&new_fileindex_path, &new_index,
2431 err = got_fileindex_write(fileindex, new_index);
2435 if (rename(new_fileindex_path, fileindex_path) != 0) {
2436 err = got_error_from_errno3("rename", new_fileindex_path,
2438 unlink(new_fileindex_path);
2442 * Sleep for a short amount of time to ensure that files modified after
2443 * this program exits have a different time stamp from the one which
2444 * was recorded in the file index.
2447 timeout.tv_nsec = 1;
2448 nanosleep(&timeout, NULL);
2452 free(new_fileindex_path);
2456 static const struct got_error *
2457 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2458 struct got_object_id **tree_id, const char *wt_relpath,
2459 struct got_commit_object *base_commit, struct got_worktree *worktree,
2460 struct got_repository *repo)
2462 const struct got_error *err = NULL;
2463 struct got_object_id *id = NULL;
2464 char *in_repo_path = NULL;
2465 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2467 *entry_type = GOT_OBJ_TYPE_ANY;
2468 *tree_relpath = NULL;
2471 if (wt_relpath[0] == '\0') {
2472 /* Check out all files within the work tree. */
2473 *entry_type = GOT_OBJ_TYPE_TREE;
2474 *tree_relpath = strdup("");
2475 if (*tree_relpath == NULL) {
2476 err = got_error_from_errno("strdup");
2479 err = got_object_id_by_path(tree_id, repo, base_commit,
2480 worktree->path_prefix);
2486 /* Check out a subset of files in the work tree. */
2488 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2489 is_root_wt ? "" : "/", wt_relpath) == -1) {
2490 err = got_error_from_errno("asprintf");
2494 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2499 in_repo_path = NULL;
2501 err = got_object_get_type(entry_type, repo, id);
2505 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2506 /* Check out a single file. */
2507 if (strchr(wt_relpath, '/') == NULL) {
2508 /* Check out a single file in work tree's root dir. */
2509 in_repo_path = strdup(worktree->path_prefix);
2510 if (in_repo_path == NULL) {
2511 err = got_error_from_errno("strdup");
2514 *tree_relpath = strdup("");
2515 if (*tree_relpath == NULL) {
2516 err = got_error_from_errno("strdup");
2520 /* Check out a single file in a subdirectory. */
2521 err = got_path_dirname(tree_relpath, wt_relpath);
2524 if (asprintf(&in_repo_path, "%s%s%s",
2525 worktree->path_prefix, is_root_wt ? "" : "/",
2526 *tree_relpath) == -1) {
2527 err = got_error_from_errno("asprintf");
2531 err = got_object_id_by_path(tree_id, repo,
2532 base_commit, in_repo_path);
2534 /* Check out all files within a subdirectory. */
2535 *tree_id = got_object_id_dup(id);
2536 if (*tree_id == NULL) {
2537 err = got_error_from_errno("got_object_id_dup");
2540 *tree_relpath = strdup(wt_relpath);
2541 if (*tree_relpath == NULL) {
2542 err = got_error_from_errno("strdup");
2550 *entry_type = GOT_OBJ_TYPE_ANY;
2551 free(*tree_relpath);
2552 *tree_relpath = NULL;
2559 static const struct got_error *
2560 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2561 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2562 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2563 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2565 const struct got_error *err = NULL;
2566 struct got_commit_object *commit = NULL;
2567 struct got_tree_object *tree = NULL;
2568 struct got_fileindex_diff_tree_cb diff_cb;
2569 struct diff_cb_arg arg;
2571 err = ref_base_commit(worktree, repo);
2573 if (!(err->code == GOT_ERR_ERRNO &&
2574 (errno == EACCES || errno == EROFS)))
2576 err = (*progress_cb)(progress_arg,
2577 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2582 err = got_object_open_as_commit(&commit, repo,
2583 worktree->base_commit_id);
2587 err = got_object_open_as_tree(&tree, repo, tree_id);
2592 got_object_tree_find_entry(tree, entry_name) == NULL) {
2593 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2597 diff_cb.diff_old_new = diff_old_new;
2598 diff_cb.diff_old = diff_old;
2599 diff_cb.diff_new = diff_new;
2600 arg.fileindex = fileindex;
2601 arg.worktree = worktree;
2603 arg.progress_cb = progress_cb;
2604 arg.progress_arg = progress_arg;
2605 arg.cancel_cb = cancel_cb;
2606 arg.cancel_arg = cancel_arg;
2607 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2608 entry_name, repo, &diff_cb, &arg);
2611 got_object_tree_close(tree);
2613 got_object_commit_close(commit);
2617 const struct got_error *
2618 got_worktree_checkout_files(struct got_worktree *worktree,
2619 struct got_pathlist_head *paths, struct got_repository *repo,
2620 got_worktree_checkout_cb progress_cb, void *progress_arg,
2621 got_cancel_cb cancel_cb, void *cancel_arg)
2623 const struct got_error *err = NULL, *sync_err, *unlockerr;
2624 struct got_commit_object *commit = NULL;
2625 struct got_tree_object *tree = NULL;
2626 struct got_fileindex *fileindex = NULL;
2627 char *fileindex_path = NULL;
2628 struct got_pathlist_entry *pe;
2629 struct tree_path_data {
2630 STAILQ_ENTRY(tree_path_data) entry;
2631 struct got_object_id *tree_id;
2636 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2638 STAILQ_INIT(&tree_paths);
2640 err = lock_worktree(worktree, LOCK_EX);
2644 err = got_object_open_as_commit(&commit, repo,
2645 worktree->base_commit_id);
2649 /* Map all specified paths to in-repository trees. */
2650 TAILQ_FOREACH(pe, paths, entry) {
2651 tpd = malloc(sizeof(*tpd));
2653 err = got_error_from_errno("malloc");
2657 err = find_tree_entry_for_checkout(&tpd->entry_type,
2658 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2665 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2666 err = got_path_basename(&tpd->entry_name, pe->path);
2674 tpd->entry_name = NULL;
2676 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2680 * Read the file index.
2681 * Checking out files is supposed to be an idempotent operation.
2682 * If the on-disk file index is incomplete we will try to complete it.
2684 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2688 tpd = STAILQ_FIRST(&tree_paths);
2689 TAILQ_FOREACH(pe, paths, entry) {
2690 struct bump_base_commit_id_arg bbc_arg;
2692 err = checkout_files(worktree, fileindex, tpd->relpath,
2693 tpd->tree_id, tpd->entry_name, repo,
2694 progress_cb, progress_arg, cancel_cb, cancel_arg);
2698 bbc_arg.base_commit_id = worktree->base_commit_id;
2699 bbc_arg.entry_name = tpd->entry_name;
2700 bbc_arg.path = pe->path;
2701 bbc_arg.path_len = pe->path_len;
2702 bbc_arg.progress_cb = progress_cb;
2703 bbc_arg.progress_arg = progress_arg;
2704 err = got_fileindex_for_each_entry_safe(fileindex,
2705 bump_base_commit_id, &bbc_arg);
2709 tpd = STAILQ_NEXT(tpd, entry);
2711 sync_err = sync_fileindex(fileindex, fileindex_path);
2712 if (sync_err && err == NULL)
2715 free(fileindex_path);
2717 got_object_tree_close(tree);
2719 got_object_commit_close(commit);
2721 got_fileindex_free(fileindex);
2722 while (!STAILQ_EMPTY(&tree_paths)) {
2723 tpd = STAILQ_FIRST(&tree_paths);
2724 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2729 unlockerr = lock_worktree(worktree, LOCK_SH);
2730 if (unlockerr && err == NULL)
2735 struct merge_file_cb_arg {
2736 struct got_worktree *worktree;
2737 struct got_fileindex *fileindex;
2738 got_worktree_checkout_cb progress_cb;
2740 got_cancel_cb cancel_cb;
2742 const char *label_orig;
2743 struct got_object_id *commit_id2;
2744 int allow_bad_symlinks;
2747 static const struct got_error *
2748 merge_file_cb(void *arg, struct got_blob_object *blob1,
2749 struct got_blob_object *blob2, struct got_object_id *id1,
2750 struct got_object_id *id2, const char *path1, const char *path2,
2751 mode_t mode1, mode_t mode2, struct got_repository *repo)
2753 static const struct got_error *err = NULL;
2754 struct merge_file_cb_arg *a = arg;
2755 struct got_fileindex_entry *ie;
2756 char *ondisk_path = NULL;
2758 unsigned char status;
2759 int local_changes_subsumed;
2760 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2761 char *id_str = NULL, *label_deriv2 = NULL;
2763 if (blob1 && blob2) {
2764 ie = got_fileindex_entry_get(a->fileindex, path2,
2767 return (*a->progress_cb)(a->progress_arg,
2768 GOT_STATUS_MISSING, path2);
2770 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2772 return got_error_from_errno("asprintf");
2774 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2779 if (status == GOT_STATUS_DELETE) {
2780 err = (*a->progress_cb)(a->progress_arg,
2781 GOT_STATUS_MERGE, path2);
2784 if (status != GOT_STATUS_NO_CHANGE &&
2785 status != GOT_STATUS_MODIFY &&
2786 status != GOT_STATUS_CONFLICT &&
2787 status != GOT_STATUS_ADD) {
2788 err = (*a->progress_cb)(a->progress_arg, status, path2);
2792 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2794 err = got_object_blob_read_to_str(&link_target2, blob2);
2797 err = merge_symlink(a->worktree, blob1, ondisk_path,
2798 path2, a->label_orig, link_target2, a->commit_id2,
2799 repo, a->progress_cb, a->progress_arg);
2804 f_orig = got_opentemp();
2805 if (f_orig == NULL) {
2806 err = got_error_from_errno("got_opentemp");
2809 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2814 f_deriv2 = got_opentemp();
2815 if (f_deriv2 == NULL)
2817 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2822 fd = open(ondisk_path,
2823 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2825 err = got_error_from_errno2("open",
2829 f_deriv = fdopen(fd, "r");
2830 if (f_deriv == NULL) {
2831 err = got_error_from_errno2("fdopen",
2836 err = got_object_id_str(&id_str, a->commit_id2);
2839 if (asprintf(&label_deriv2, "%s: commit %s",
2840 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2841 err = got_error_from_errno("asprintf");
2844 err = merge_file(&local_changes_subsumed, a->worktree,
2845 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2846 sb.st_mode, a->label_orig, NULL, label_deriv2,
2847 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2848 a->progress_cb, a->progress_arg);
2851 ie = got_fileindex_entry_get(a->fileindex, path1,
2854 return (*a->progress_cb)(a->progress_arg,
2855 GOT_STATUS_MISSING, path1);
2857 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2859 return got_error_from_errno("asprintf");
2861 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2867 case GOT_STATUS_NO_CHANGE:
2868 err = (*a->progress_cb)(a->progress_arg,
2869 GOT_STATUS_DELETE, path1);
2872 err = remove_ondisk_file(a->worktree->root_path, path1);
2876 got_fileindex_entry_mark_deleted_from_disk(ie);
2878 case GOT_STATUS_DELETE:
2879 case GOT_STATUS_MISSING:
2880 err = (*a->progress_cb)(a->progress_arg,
2881 GOT_STATUS_DELETE, path1);
2885 got_fileindex_entry_mark_deleted_from_disk(ie);
2887 case GOT_STATUS_ADD: {
2888 struct got_object_id *id;
2892 * Delete the added file only if its content already
2893 * exists in the repository.
2895 err = got_object_blob_file_create(&id, &blob1_f,
2896 &blob1_size, path1);
2899 if (got_object_id_cmp(id, id1) == 0) {
2900 err = (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_DELETE, path1);
2904 err = remove_ondisk_file(a->worktree->root_path,
2909 got_fileindex_entry_remove(a->fileindex,
2912 err = (*a->progress_cb)(a->progress_arg,
2913 GOT_STATUS_CANNOT_DELETE, path1);
2915 if (fclose(blob1_f) == EOF && err == NULL)
2916 err = got_error_from_errno("fclose");
2922 case GOT_STATUS_MODIFY:
2923 case GOT_STATUS_CONFLICT:
2924 err = (*a->progress_cb)(a->progress_arg,
2925 GOT_STATUS_CANNOT_DELETE, path1);
2929 case GOT_STATUS_OBSTRUCTED:
2930 err = (*a->progress_cb)(a->progress_arg, status, path1);
2938 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2940 return got_error_from_errno("asprintf");
2941 ie = got_fileindex_entry_get(a->fileindex, path2,
2944 err = get_file_status(&status, &sb, ie, ondisk_path,
2948 if (status != GOT_STATUS_NO_CHANGE &&
2949 status != GOT_STATUS_MODIFY &&
2950 status != GOT_STATUS_CONFLICT &&
2951 status != GOT_STATUS_ADD) {
2952 err = (*a->progress_cb)(a->progress_arg,
2956 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2958 err = got_object_blob_read_to_str(&link_target2,
2962 err = merge_symlink(a->worktree, NULL,
2963 ondisk_path, path2, a->label_orig,
2964 link_target2, a->commit_id2, repo,
2965 a->progress_cb, a->progress_arg);
2967 } else if (S_ISREG(sb.st_mode)) {
2968 err = merge_blob(&local_changes_subsumed,
2969 a->worktree, NULL, ondisk_path, path2,
2970 sb.st_mode, a->label_orig, blob2,
2971 a->commit_id2, repo, a->progress_cb,
2974 err = got_error_path(ondisk_path,
2975 GOT_ERR_FILE_OBSTRUCTED);
2979 if (status == GOT_STATUS_DELETE) {
2980 err = got_fileindex_entry_update(ie,
2981 a->worktree->root_fd, path2, blob2->id.sha1,
2982 a->worktree->base_commit_id->sha1, 0);
2987 int is_bad_symlink = 0;
2988 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2989 if (S_ISLNK(mode2)) {
2990 err = install_symlink(&is_bad_symlink,
2991 a->worktree, ondisk_path, path2, blob2, 0,
2992 0, 1, a->allow_bad_symlinks, repo,
2993 a->progress_cb, a->progress_arg);
2995 err = install_blob(a->worktree, ondisk_path, path2,
2996 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2997 a->progress_cb, a->progress_arg);
3001 err = got_fileindex_entry_alloc(&ie, path2);
3004 err = got_fileindex_entry_update(ie,
3005 a->worktree->root_fd, path2, NULL, NULL, 1);
3007 got_fileindex_entry_free(ie);
3010 err = got_fileindex_entry_add(a->fileindex, ie);
3012 got_fileindex_entry_free(ie);
3015 if (is_bad_symlink) {
3016 got_fileindex_entry_filetype_set(ie,
3017 GOT_FILEIDX_MODE_BAD_SYMLINK);
3022 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3023 err = got_error_from_errno("fclose");
3024 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3025 err = got_error_from_errno("fclose");
3026 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3027 err = got_error_from_errno("fclose");
3034 static const struct got_error *
3035 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3037 struct got_worktree *worktree = arg;
3039 /* Reject merges into a work tree with mixed base commits. */
3040 if (got_fileindex_entry_has_commit(ie) &&
3041 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3042 SHA1_DIGEST_LENGTH) != 0)
3043 return got_error(GOT_ERR_MIXED_COMMITS);
3048 struct check_merge_conflicts_arg {
3049 struct got_worktree *worktree;
3050 struct got_fileindex *fileindex;
3051 struct got_repository *repo;
3054 static const struct got_error *
3055 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3056 struct got_blob_object *blob2, struct got_object_id *id1,
3057 struct got_object_id *id2, const char *path1, const char *path2,
3058 mode_t mode1, mode_t mode2, struct got_repository *repo)
3060 const struct got_error *err = NULL;
3061 struct check_merge_conflicts_arg *a = arg;
3062 unsigned char status;
3064 struct got_fileindex_entry *ie;
3065 const char *path = path2 ? path2 : path1;
3066 struct got_object_id *id = id2 ? id2 : id1;
3072 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3076 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3078 return got_error_from_errno("asprintf");
3080 /* Reject merges into a work tree with conflicted files. */
3081 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3085 if (status == GOT_STATUS_CONFLICT)
3086 return got_error(GOT_ERR_CONFLICTS);
3091 static const struct got_error *
3092 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3093 const char *fileindex_path, struct got_object_id *commit_id1,
3094 struct got_object_id *commit_id2, struct got_repository *repo,
3095 got_worktree_checkout_cb progress_cb, void *progress_arg,
3096 got_cancel_cb cancel_cb, void *cancel_arg)
3098 const struct got_error *err = NULL, *sync_err;
3099 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3100 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3101 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3102 struct check_merge_conflicts_arg cmc_arg;
3103 struct merge_file_cb_arg arg;
3104 char *label_orig = NULL;
3107 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3110 err = got_object_id_by_path(&tree_id1, repo, commit1,
3111 worktree->path_prefix);
3112 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3118 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3122 err = got_object_id_str(&id_str, commit_id1);
3126 if (asprintf(&label_orig, "%s: commit %s",
3127 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3128 err = got_error_from_errno("asprintf");
3135 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3139 err = got_object_id_by_path(&tree_id2, repo, commit2,
3140 worktree->path_prefix);
3144 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3148 cmc_arg.worktree = worktree;
3149 cmc_arg.fileindex = fileindex;
3150 cmc_arg.repo = repo;
3151 err = got_diff_tree(tree1, tree2, "", "", repo,
3152 check_merge_conflicts, &cmc_arg, 0);
3156 arg.worktree = worktree;
3157 arg.fileindex = fileindex;
3158 arg.progress_cb = progress_cb;
3159 arg.progress_arg = progress_arg;
3160 arg.cancel_cb = cancel_cb;
3161 arg.cancel_arg = cancel_arg;
3162 arg.label_orig = label_orig;
3163 arg.commit_id2 = commit_id2;
3164 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3165 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3166 sync_err = sync_fileindex(fileindex, fileindex_path);
3167 if (sync_err && err == NULL)
3171 got_object_commit_close(commit1);
3173 got_object_commit_close(commit2);
3175 got_object_tree_close(tree1);
3177 got_object_tree_close(tree2);
3182 const struct got_error *
3183 got_worktree_merge_files(struct got_worktree *worktree,
3184 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3185 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3186 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3188 const struct got_error *err, *unlockerr;
3189 char *fileindex_path = NULL;
3190 struct got_fileindex *fileindex = NULL;
3192 err = lock_worktree(worktree, LOCK_EX);
3196 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3200 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3205 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3206 commit_id2, repo, progress_cb, progress_arg,
3207 cancel_cb, cancel_arg);
3210 got_fileindex_free(fileindex);
3211 free(fileindex_path);
3212 unlockerr = lock_worktree(worktree, LOCK_SH);
3213 if (unlockerr && err == NULL)
3218 struct diff_dir_cb_arg {
3219 struct got_fileindex *fileindex;
3220 struct got_worktree *worktree;
3221 const char *status_path;
3222 size_t status_path_len;
3223 struct got_repository *repo;
3224 got_worktree_status_cb status_cb;
3226 got_cancel_cb cancel_cb;
3228 /* A pathlist containing per-directory pathlists of ignore patterns. */
3229 struct got_pathlist_head *ignores;
3230 int report_unchanged;
3234 static const struct got_error *
3235 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3236 int dirfd, const char *de_name,
3237 got_worktree_status_cb status_cb, void *status_arg,
3238 struct got_repository *repo, int report_unchanged)
3240 const struct got_error *err = NULL;
3241 unsigned char status = GOT_STATUS_NO_CHANGE;
3242 unsigned char staged_status = get_staged_status(ie);
3244 struct got_object_id blob_id, commit_id, staged_blob_id;
3245 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3246 struct got_object_id *staged_blob_idp = NULL;
3248 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3252 if (status == GOT_STATUS_NO_CHANGE &&
3253 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3256 if (got_fileindex_entry_has_blob(ie)) {
3257 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3258 blob_idp = &blob_id;
3260 if (got_fileindex_entry_has_commit(ie)) {
3261 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3262 commit_idp = &commit_id;
3264 if (staged_status == GOT_STATUS_ADD ||
3265 staged_status == GOT_STATUS_MODIFY) {
3266 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3267 SHA1_DIGEST_LENGTH);
3268 staged_blob_idp = &staged_blob_id;
3271 return (*status_cb)(status_arg, status, staged_status,
3272 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3275 static const struct got_error *
3276 status_old_new(void *arg, struct got_fileindex_entry *ie,
3277 struct dirent *de, const char *parent_path, int dirfd)
3279 const struct got_error *err = NULL;
3280 struct diff_dir_cb_arg *a = arg;
3283 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3284 return got_error(GOT_ERR_CANCELLED);
3286 if (got_path_cmp(parent_path, a->status_path,
3287 strlen(parent_path), a->status_path_len) != 0 &&
3288 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3291 if (parent_path[0]) {
3292 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3293 parent_path, de->d_name) == -1)
3294 return got_error_from_errno("asprintf");
3296 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3298 return got_error_from_errno("asprintf");
3301 err = report_file_status(ie, abspath, dirfd, de->d_name,
3302 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3307 static const struct got_error *
3308 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3310 struct diff_dir_cb_arg *a = arg;
3311 struct got_object_id blob_id, commit_id;
3312 unsigned char status;
3314 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3315 return got_error(GOT_ERR_CANCELLED);
3317 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3320 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3321 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3322 if (got_fileindex_entry_has_file_on_disk(ie))
3323 status = GOT_STATUS_MISSING;
3325 status = GOT_STATUS_DELETE;
3326 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3327 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3331 free_ignorelist(struct got_pathlist_head *ignorelist)
3333 struct got_pathlist_entry *pe;
3335 TAILQ_FOREACH(pe, ignorelist, entry)
3336 free((char *)pe->path);
3337 got_pathlist_free(ignorelist);
3341 free_ignores(struct got_pathlist_head *ignores)
3343 struct got_pathlist_entry *pe;
3345 TAILQ_FOREACH(pe, ignores, entry) {
3346 struct got_pathlist_head *ignorelist = pe->data;
3347 free_ignorelist(ignorelist);
3348 free((char *)pe->path);
3350 got_pathlist_free(ignores);
3353 static const struct got_error *
3354 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3356 const struct got_error *err = NULL;
3357 struct got_pathlist_entry *pe = NULL;
3358 struct got_pathlist_head *ignorelist;
3359 char *line = NULL, *pattern, *dirpath = NULL;
3360 size_t linesize = 0;
3363 ignorelist = calloc(1, sizeof(*ignorelist));
3364 if (ignorelist == NULL)
3365 return got_error_from_errno("calloc");
3366 TAILQ_INIT(ignorelist);
3368 while ((linelen = getline(&line, &linesize, f)) != -1) {
3369 if (linelen > 0 && line[linelen - 1] == '\n')
3370 line[linelen - 1] = '\0';
3372 /* Git's ignores may contain comments. */
3376 /* Git's negated patterns are not (yet?) supported. */
3380 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3382 err = got_error_from_errno("asprintf");
3385 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3390 err = got_error_from_errno("getline");
3394 dirpath = strdup(path);
3395 if (dirpath == NULL) {
3396 err = got_error_from_errno("strdup");
3399 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3402 if (err || pe == NULL) {
3404 free_ignorelist(ignorelist);
3410 match_ignores(struct got_pathlist_head *ignores, const char *path)
3412 struct got_pathlist_entry *pe;
3414 /* Handle patterns which match in all directories. */
3415 TAILQ_FOREACH(pe, ignores, entry) {
3416 struct got_pathlist_head *ignorelist = pe->data;
3417 struct got_pathlist_entry *pi;
3419 TAILQ_FOREACH(pi, ignorelist, entry) {
3420 const char *p, *pattern = pi->path;
3422 if (strncmp(pattern, "**/", 3) != 0)
3427 if (fnmatch(pattern, p,
3428 FNM_PATHNAME | FNM_LEADING_DIR)) {
3429 /* Retry in next directory. */
3430 while (*p && *p != '/')
3442 * The ignores pathlist contains ignore lists from children before
3443 * parents, so we can find the most specific ignorelist by walking
3444 * ignores backwards.
3446 pe = TAILQ_LAST(ignores, got_pathlist_head);
3448 if (got_path_is_child(path, pe->path, pe->path_len)) {
3449 struct got_pathlist_head *ignorelist = pe->data;
3450 struct got_pathlist_entry *pi;
3451 TAILQ_FOREACH(pi, ignorelist, entry) {
3452 const char *pattern = pi->path;
3453 int flags = FNM_LEADING_DIR;
3454 if (strstr(pattern, "/**/") == NULL)
3455 flags |= FNM_PATHNAME;
3456 if (fnmatch(pattern, path, flags))
3461 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3467 static const struct got_error *
3468 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3469 const char *path, int dirfd, const char *ignores_filename)
3471 const struct got_error *err = NULL;
3474 FILE *ignoresfile = NULL;
3476 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3477 path[0] ? "/" : "", ignores_filename) == -1)
3478 return got_error_from_errno("asprintf");
3481 fd = openat(dirfd, ignores_filename,
3482 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3484 if (errno != ENOENT && errno != EACCES)
3485 err = got_error_from_errno2("openat",
3488 ignoresfile = fdopen(fd, "r");
3489 if (ignoresfile == NULL)
3490 err = got_error_from_errno2("fdopen",
3494 err = read_ignores(ignores, path, ignoresfile);
3498 ignoresfile = fopen(ignorespath, "re");
3499 if (ignoresfile == NULL) {
3500 if (errno != ENOENT && errno != EACCES)
3501 err = got_error_from_errno2("fopen",
3504 err = read_ignores(ignores, path, ignoresfile);
3507 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3508 err = got_error_from_errno2("fclose", path);
3509 if (fd != -1 && close(fd) == -1 && err == NULL)
3510 err = got_error_from_errno2("close", path);
3515 static const struct got_error *
3516 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3519 const struct got_error *err = NULL;
3520 struct diff_dir_cb_arg *a = arg;
3526 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3527 return got_error(GOT_ERR_CANCELLED);
3529 if (parent_path[0]) {
3530 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3531 return got_error_from_errno("asprintf");
3536 if (de->d_type == DT_DIR) {
3537 if (!a->no_ignores && ignore != NULL &&
3538 match_ignores(a->ignores, path))
3540 } else if (!match_ignores(a->ignores, path) &&
3541 got_path_is_child(path, a->status_path, a->status_path_len))
3542 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3543 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3549 static const struct got_error *
3550 status_traverse(void *arg, const char *path, int dirfd)
3552 const struct got_error *err = NULL;
3553 struct diff_dir_cb_arg *a = arg;
3558 err = add_ignores(a->ignores, a->worktree->root_path,
3559 path, dirfd, ".cvsignore");
3563 err = add_ignores(a->ignores, a->worktree->root_path, path,
3564 dirfd, ".gitignore");
3569 static const struct got_error *
3570 report_single_file_status(const char *path, const char *ondisk_path,
3571 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3572 void *status_arg, struct got_repository *repo, int report_unchanged,
3573 struct got_pathlist_head *ignores, int no_ignores)
3575 struct got_fileindex_entry *ie;
3578 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3580 return report_file_status(ie, ondisk_path, -1, NULL,
3581 status_cb, status_arg, repo, report_unchanged);
3583 if (lstat(ondisk_path, &sb) == -1) {
3584 if (errno != ENOENT)
3585 return got_error_from_errno2("lstat", ondisk_path);
3586 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3587 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3590 if (!no_ignores && match_ignores(ignores, path))
3593 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3594 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3595 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3600 static const struct got_error *
3601 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3602 const char *root_path, const char *path)
3604 const struct got_error *err;
3605 char *parent_path, *next_parent_path = NULL;
3607 err = add_ignores(ignores, root_path, "", -1,
3612 err = add_ignores(ignores, root_path, "", -1,
3617 err = got_path_dirname(&parent_path, path);
3619 if (err->code == GOT_ERR_BAD_PATH)
3620 return NULL; /* cannot traverse parent */
3624 err = add_ignores(ignores, root_path, parent_path, -1,
3628 err = add_ignores(ignores, root_path, parent_path, -1,
3632 err = got_path_dirname(&next_parent_path, parent_path);
3634 if (err->code == GOT_ERR_BAD_PATH)
3635 err = NULL; /* traversed everything */
3638 if (got_path_is_root_dir(parent_path))
3641 parent_path = next_parent_path;
3642 next_parent_path = NULL;
3646 free(next_parent_path);
3650 static const struct got_error *
3651 worktree_status(struct got_worktree *worktree, const char *path,
3652 struct got_fileindex *fileindex, struct got_repository *repo,
3653 got_worktree_status_cb status_cb, void *status_arg,
3654 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3655 int report_unchanged)
3657 const struct got_error *err = NULL;
3659 struct got_fileindex_diff_dir_cb fdiff_cb;
3660 struct diff_dir_cb_arg arg;
3661 char *ondisk_path = NULL;
3662 struct got_pathlist_head ignores;
3663 struct got_fileindex_entry *ie;
3665 TAILQ_INIT(&ignores);
3667 if (asprintf(&ondisk_path, "%s%s%s",
3668 worktree->root_path, path[0] ? "/" : "", path) == -1)
3669 return got_error_from_errno("asprintf");
3671 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3673 err = report_single_file_status(path, ondisk_path,
3674 fileindex, status_cb, status_arg, repo,
3675 report_unchanged, &ignores, no_ignores);
3679 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3681 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3682 !got_err_open_nofollow_on_symlink())
3683 err = got_error_from_errno2("open", ondisk_path);
3686 err = add_ignores_from_parent_paths(&ignores,
3687 worktree->root_path, ondisk_path);
3691 err = report_single_file_status(path, ondisk_path,
3692 fileindex, status_cb, status_arg, repo,
3693 report_unchanged, &ignores, no_ignores);
3696 fdiff_cb.diff_old_new = status_old_new;
3697 fdiff_cb.diff_old = status_old;
3698 fdiff_cb.diff_new = status_new;
3699 fdiff_cb.diff_traverse = status_traverse;
3700 arg.fileindex = fileindex;
3701 arg.worktree = worktree;
3702 arg.status_path = path;
3703 arg.status_path_len = strlen(path);
3705 arg.status_cb = status_cb;
3706 arg.status_arg = status_arg;
3707 arg.cancel_cb = cancel_cb;
3708 arg.cancel_arg = cancel_arg;
3709 arg.report_unchanged = report_unchanged;
3710 arg.no_ignores = no_ignores;
3712 err = add_ignores_from_parent_paths(&ignores,
3713 worktree->root_path, path);
3717 arg.ignores = &ignores;
3718 err = got_fileindex_diff_dir(fileindex, fd,
3719 worktree->root_path, path, repo, &fdiff_cb, &arg);
3722 free_ignores(&ignores);
3723 if (fd != -1 && close(fd) == -1 && err == NULL)
3724 err = got_error_from_errno("close");
3729 const struct got_error *
3730 got_worktree_status(struct got_worktree *worktree,
3731 struct got_pathlist_head *paths, struct got_repository *repo,
3732 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3733 got_cancel_cb cancel_cb, void *cancel_arg)
3735 const struct got_error *err = NULL;
3736 char *fileindex_path = NULL;
3737 struct got_fileindex *fileindex = NULL;
3738 struct got_pathlist_entry *pe;
3740 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3744 TAILQ_FOREACH(pe, paths, entry) {
3745 err = worktree_status(worktree, pe->path, fileindex, repo,
3746 status_cb, status_arg, cancel_cb, cancel_arg,
3751 free(fileindex_path);
3752 got_fileindex_free(fileindex);
3756 const struct got_error *
3757 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3760 const struct got_error *err = NULL;
3761 char *resolved = NULL, *cwd = NULL, *path = NULL;
3764 char *abspath = NULL;
3765 char canonpath[PATH_MAX];
3769 cwd = getcwd(NULL, 0);
3771 return got_error_from_errno("getcwd");
3773 if (lstat(arg, &sb) == -1) {
3774 if (errno != ENOENT) {
3775 err = got_error_from_errno2("lstat", arg);
3780 if (S_ISLNK(sb.st_mode)) {
3782 * We cannot use realpath(3) with symlinks since we want to
3783 * operate on the symlink itself.
3784 * But we can make the path absolute, assuming it is relative
3785 * to the current working directory, and then canonicalize it.
3787 if (!got_path_is_absolute(arg)) {
3788 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3789 err = got_error_from_errno("asprintf");
3794 err = got_canonpath(abspath ? abspath : arg, canonpath,
3798 resolved = strdup(canonpath);
3799 if (resolved == NULL) {
3800 err = got_error_from_errno("strdup");
3804 resolved = realpath(arg, NULL);
3805 if (resolved == NULL) {
3806 if (errno != ENOENT) {
3807 err = got_error_from_errno2("realpath", arg);
3810 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3811 err = got_error_from_errno("asprintf");
3814 err = got_canonpath(abspath, canonpath,
3818 resolved = strdup(canonpath);
3819 if (resolved == NULL) {
3820 err = got_error_from_errno("strdup");
3826 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3827 strlen(got_worktree_get_root_path(worktree)))) {
3828 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3832 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3833 err = got_path_skip_common_ancestor(&path,
3834 got_worktree_get_root_path(worktree), resolved);
3840 err = got_error_from_errno("strdup");
3845 /* XXX status walk can't deal with trailing slash! */
3847 while (len > 0 && path[len - 1] == '/') {
3848 path[len - 1] = '\0';
3862 struct schedule_addition_args {
3863 struct got_worktree *worktree;
3864 struct got_fileindex *fileindex;
3865 got_worktree_checkout_cb progress_cb;
3867 struct got_repository *repo;
3870 static const struct got_error *
3871 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3872 const char *relpath, struct got_object_id *blob_id,
3873 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3874 int dirfd, const char *de_name)
3876 struct schedule_addition_args *a = arg;
3877 const struct got_error *err = NULL;
3878 struct got_fileindex_entry *ie;
3882 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3884 return got_error_from_errno("asprintf");
3886 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3888 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3892 /* Re-adding an existing entry is a no-op. */
3893 if (status == GOT_STATUS_ADD)
3895 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3900 if (status != GOT_STATUS_UNVERSIONED) {
3901 if (status == GOT_STATUS_NONEXISTENT)
3902 err = got_error_set_errno(ENOENT, ondisk_path);
3904 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3908 err = got_fileindex_entry_alloc(&ie, relpath);
3911 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3912 relpath, NULL, NULL, 1);
3914 got_fileindex_entry_free(ie);
3917 err = got_fileindex_entry_add(a->fileindex, ie);
3919 got_fileindex_entry_free(ie);
3926 if (status == GOT_STATUS_ADD)
3928 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3931 const struct got_error *
3932 got_worktree_schedule_add(struct got_worktree *worktree,
3933 struct got_pathlist_head *paths,
3934 got_worktree_checkout_cb progress_cb, void *progress_arg,
3935 struct got_repository *repo, int no_ignores)
3937 struct got_fileindex *fileindex = NULL;
3938 char *fileindex_path = NULL;
3939 const struct got_error *err = NULL, *sync_err, *unlockerr;
3940 struct got_pathlist_entry *pe;
3941 struct schedule_addition_args saa;
3943 err = lock_worktree(worktree, LOCK_EX);
3947 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3951 saa.worktree = worktree;
3952 saa.fileindex = fileindex;
3953 saa.progress_cb = progress_cb;
3954 saa.progress_arg = progress_arg;
3957 TAILQ_FOREACH(pe, paths, entry) {
3958 err = worktree_status(worktree, pe->path, fileindex, repo,
3959 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3963 sync_err = sync_fileindex(fileindex, fileindex_path);
3964 if (sync_err && err == NULL)
3967 free(fileindex_path);
3969 got_fileindex_free(fileindex);
3970 unlockerr = lock_worktree(worktree, LOCK_SH);
3971 if (unlockerr && err == NULL)
3976 struct schedule_deletion_args {
3977 struct got_worktree *worktree;
3978 struct got_fileindex *fileindex;
3979 got_worktree_delete_cb progress_cb;
3981 struct got_repository *repo;
3982 int delete_local_mods;
3984 int ignore_missing_paths;
3985 const char *status_codes;
3988 static const struct got_error *
3989 schedule_for_deletion(void *arg, unsigned char status,
3990 unsigned char staged_status, const char *relpath,
3991 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3992 struct got_object_id *commit_id, int dirfd, const char *de_name)
3994 struct schedule_deletion_args *a = arg;
3995 const struct got_error *err = NULL;
3996 struct got_fileindex_entry *ie = NULL;
4000 if (status == GOT_STATUS_NONEXISTENT) {
4001 if (a->ignore_missing_paths)
4003 return got_error_set_errno(ENOENT, relpath);
4006 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4008 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4010 staged_status = get_staged_status(ie);
4011 if (staged_status != GOT_STATUS_NO_CHANGE) {
4012 if (staged_status == GOT_STATUS_DELETE)
4014 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4017 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4019 return got_error_from_errno("asprintf");
4021 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4026 if (a->status_codes) {
4027 size_t ncodes = strlen(a->status_codes);
4029 for (i = 0; i < ncodes ; i++) {
4030 if (status == a->status_codes[i])
4034 /* Do not delete files in non-matching status. */
4038 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4039 a->status_codes[i] != GOT_STATUS_MISSING) {
4040 static char msg[64];
4041 snprintf(msg, sizeof(msg),
4042 "invalid status code '%c'", a->status_codes[i]);
4043 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4048 if (status != GOT_STATUS_NO_CHANGE) {
4049 if (status == GOT_STATUS_DELETE)
4051 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4052 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4055 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4056 err = got_error_set_errno(ENOENT, relpath);
4059 if (status != GOT_STATUS_MODIFY &&
4060 status != GOT_STATUS_MISSING) {
4061 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4066 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4070 if (unlinkat(dirfd, de_name, 0) != 0) {
4071 err = got_error_from_errno2("unlinkat",
4075 } else if (unlink(ondisk_path) != 0) {
4076 err = got_error_from_errno2("unlink", ondisk_path);
4080 root_len = strlen(a->worktree->root_path);
4083 err = got_path_dirname(&parent, ondisk_path);
4087 ondisk_path = parent;
4088 if (rmdir(ondisk_path) == -1) {
4089 if (errno != ENOTEMPTY)
4090 err = got_error_from_errno2("rmdir",
4094 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4095 strlen(ondisk_path), root_len) != 0);
4098 got_fileindex_entry_mark_deleted_from_disk(ie);
4103 if (status == GOT_STATUS_DELETE)
4105 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4106 staged_status, relpath);
4109 const struct got_error *
4110 got_worktree_schedule_delete(struct got_worktree *worktree,
4111 struct got_pathlist_head *paths, int delete_local_mods,
4112 const char *status_codes,
4113 got_worktree_delete_cb progress_cb, void *progress_arg,
4114 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4116 struct got_fileindex *fileindex = NULL;
4117 char *fileindex_path = NULL;
4118 const struct got_error *err = NULL, *sync_err, *unlockerr;
4119 struct got_pathlist_entry *pe;
4120 struct schedule_deletion_args sda;
4122 err = lock_worktree(worktree, LOCK_EX);
4126 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4130 sda.worktree = worktree;
4131 sda.fileindex = fileindex;
4132 sda.progress_cb = progress_cb;
4133 sda.progress_arg = progress_arg;
4135 sda.delete_local_mods = delete_local_mods;
4136 sda.keep_on_disk = keep_on_disk;
4137 sda.ignore_missing_paths = ignore_missing_paths;
4138 sda.status_codes = status_codes;
4140 TAILQ_FOREACH(pe, paths, entry) {
4141 err = worktree_status(worktree, pe->path, fileindex, repo,
4142 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4146 sync_err = sync_fileindex(fileindex, fileindex_path);
4147 if (sync_err && err == NULL)
4150 free(fileindex_path);
4152 got_fileindex_free(fileindex);
4153 unlockerr = lock_worktree(worktree, LOCK_SH);
4154 if (unlockerr && err == NULL)
4159 static const struct got_error *
4160 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4162 const struct got_error *err = NULL;
4164 size_t linesize = 0, n;
4167 linelen = getline(&line, &linesize, infile);
4168 if (linelen == -1) {
4169 if (ferror(infile)) {
4170 err = got_error_from_errno("getline");
4176 n = fwrite(line, 1, linelen, outfile);
4178 err = got_ferror(outfile, GOT_ERR_IO);
4183 n = fwrite(line, 1, linelen, rejectfile);
4185 err = got_ferror(outfile, GOT_ERR_IO);
4192 static const struct got_error *
4193 skip_one_line(FILE *f)
4196 size_t linesize = 0;
4199 linelen = getline(&line, &linesize, f);
4200 if (linelen == -1) {
4202 return got_error_from_errno("getline");
4209 static const struct got_error *
4210 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4211 int start_old, int end_old, int start_new, int end_new,
4212 FILE *outfile, FILE *rejectfile)
4214 const struct got_error *err;
4216 /* Copy old file's lines leading up to patch. */
4217 while (!feof(f1) && *line_cur1 < start_old) {
4218 err = copy_one_line(f1, outfile, NULL);
4223 /* Skip new file's lines leading up to patch. */
4224 while (!feof(f2) && *line_cur2 < start_new) {
4226 err = copy_one_line(f2, NULL, rejectfile);
4228 err = skip_one_line(f2);
4233 /* Copy patched lines. */
4234 while (!feof(f2) && *line_cur2 <= end_new) {
4235 err = copy_one_line(f2, outfile, NULL);
4240 /* Skip over old file's replaced lines. */
4241 while (!feof(f1) && *line_cur1 <= end_old) {
4243 err = copy_one_line(f1, NULL, rejectfile);
4245 err = skip_one_line(f1);
4254 static const struct got_error *
4255 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4256 FILE *outfile, FILE *rejectfile)
4258 const struct got_error *err;
4261 /* Copy old file's lines until EOF. */
4263 err = copy_one_line(f1, outfile, NULL);
4270 /* Copy new file's lines until EOF. */
4272 err = copy_one_line(f2, NULL, rejectfile);
4282 static const struct got_error *
4283 apply_or_reject_change(int *choice, int *nchunks_used,
4284 struct diff_result *diff_result, int n,
4285 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4286 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4287 got_worktree_patch_cb patch_cb, void *patch_arg)
4289 const struct got_error *err = NULL;
4290 struct diff_chunk_context cc = {};
4291 int start_old, end_old, start_new, end_new;
4293 struct diff_output_unidiff_state *diff_state;
4294 struct diff_input_info diff_info;
4297 *choice = GOT_PATCH_CHOICE_NONE;
4299 /* Get changed line numbers without context lines for copy_change(). */
4300 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4301 start_old = cc.left.start;
4302 end_old = cc.left.end;
4303 start_new = cc.right.start;
4304 end_new = cc.right.end;
4306 /* Get the same change with context lines for display. */
4307 memset(&cc, 0, sizeof(cc));
4308 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4310 memset(&diff_info, 0, sizeof(diff_info));
4311 diff_info.left_path = relpath;
4312 diff_info.right_path = relpath;
4314 diff_state = diff_output_unidiff_state_alloc();
4315 if (diff_state == NULL)
4316 return got_error_set_errno(ENOMEM,
4317 "diff_output_unidiff_state_alloc");
4319 hunkfile = got_opentemp();
4320 if (hunkfile == NULL) {
4321 err = got_error_from_errno("got_opentemp");
4325 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4327 if (rc != DIFF_RC_OK) {
4328 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4332 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4333 err = got_ferror(hunkfile, GOT_ERR_IO);
4337 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4338 hunkfile, changeno, nchanges);
4343 case GOT_PATCH_CHOICE_YES:
4344 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4345 end_old, start_new, end_new, outfile, rejectfile);
4347 case GOT_PATCH_CHOICE_NO:
4348 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4349 end_old, start_new, end_new, rejectfile, outfile);
4351 case GOT_PATCH_CHOICE_QUIT:
4354 err = got_error(GOT_ERR_PATCH_CHOICE);
4358 diff_output_unidiff_state_free(diff_state);
4359 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4360 err = got_error_from_errno("fclose");
4364 struct revert_file_args {
4365 struct got_worktree *worktree;
4366 struct got_fileindex *fileindex;
4367 got_worktree_checkout_cb progress_cb;
4369 got_worktree_patch_cb patch_cb;
4371 struct got_repository *repo;
4372 int unlink_added_files;
4375 static const struct got_error *
4376 create_patched_content(char **path_outfile, int reverse_patch,
4377 struct got_object_id *blob_id, const char *path2,
4378 int dirfd2, const char *de_name2,
4379 const char *relpath, struct got_repository *repo,
4380 got_worktree_patch_cb patch_cb, void *patch_arg)
4382 const struct got_error *err, *free_err;
4383 struct got_blob_object *blob = NULL;
4384 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4386 char link_target[PATH_MAX];
4387 ssize_t link_len = 0;
4388 char *path1 = NULL, *id_str = NULL;
4390 struct got_diffreg_result *diffreg_result = NULL;
4391 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4392 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4394 *path_outfile = NULL;
4396 err = got_object_id_str(&id_str, blob_id);
4401 fd2 = openat(dirfd2, de_name2,
4402 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4404 if (!got_err_open_nofollow_on_symlink()) {
4405 err = got_error_from_errno2("openat", path2);
4408 link_len = readlinkat(dirfd2, de_name2,
4409 link_target, sizeof(link_target));
4410 if (link_len == -1) {
4411 return got_error_from_errno2("readlinkat",
4414 sb2.st_mode = S_IFLNK;
4415 sb2.st_size = link_len;
4418 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4420 if (!got_err_open_nofollow_on_symlink()) {
4421 err = got_error_from_errno2("open", path2);
4424 link_len = readlink(path2, link_target,
4425 sizeof(link_target));
4427 return got_error_from_errno2("readlink", path2);
4428 sb2.st_mode = S_IFLNK;
4429 sb2.st_size = link_len;
4433 if (fstat(fd2, &sb2) == -1) {
4434 err = got_error_from_errno2("fstat", path2);
4438 f2 = fdopen(fd2, "r");
4440 err = got_error_from_errno2("fdopen", path2);
4446 f2 = got_opentemp();
4448 err = got_error_from_errno2("got_opentemp", path2);
4451 n = fwrite(link_target, 1, link_len, f2);
4452 if (n != link_len) {
4453 err = got_ferror(f2, GOT_ERR_IO);
4456 if (fflush(f2) == EOF) {
4457 err = got_error_from_errno("fflush");
4463 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4467 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4471 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4475 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4480 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4484 if (fseek(f1, 0L, SEEK_SET) == -1)
4485 return got_ferror(f1, GOT_ERR_IO);
4486 if (fseek(f2, 0L, SEEK_SET) == -1)
4487 return got_ferror(f2, GOT_ERR_IO);
4489 /* Count the number of actual changes in the diff result. */
4490 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4491 struct diff_chunk_context cc = {};
4492 diff_chunk_context_load_change(&cc, &nchunks_used,
4493 diffreg_result->result, n, 0);
4496 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4498 err = apply_or_reject_change(&choice, &nchunks_used,
4499 diffreg_result->result, n, relpath, f1, f2,
4500 &line_cur1, &line_cur2,
4501 reverse_patch ? NULL : outfile,
4502 reverse_patch ? outfile : NULL,
4503 ++i, nchanges, patch_cb, patch_arg);
4506 if (choice == GOT_PATCH_CHOICE_YES)
4508 else if (choice == GOT_PATCH_CHOICE_QUIT)
4512 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4513 reverse_patch ? NULL : outfile,
4514 reverse_patch ? outfile : NULL);
4518 if (!S_ISLNK(sb2.st_mode)) {
4519 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4520 err = got_error_from_errno2("fchmod", path2);
4528 got_object_blob_close(blob);
4529 free_err = got_diffreg_result_free(diffreg_result);
4532 if (f1 && fclose(f1) == EOF && err == NULL)
4533 err = got_error_from_errno2("fclose", path1);
4534 if (f2 && fclose(f2) == EOF && err == NULL)
4535 err = got_error_from_errno2("fclose", path2);
4536 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4537 err = got_error_from_errno2("close", path2);
4538 if (outfile && fclose(outfile) == EOF && err == NULL)
4539 err = got_error_from_errno2("fclose", *path_outfile);
4540 if (path1 && unlink(path1) == -1 && err == NULL)
4541 err = got_error_from_errno2("unlink", path1);
4542 if (err || !have_content) {
4543 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4544 err = got_error_from_errno2("unlink", *path_outfile);
4545 free(*path_outfile);
4546 *path_outfile = NULL;
4552 static const struct got_error *
4553 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4554 const char *relpath, struct got_object_id *blob_id,
4555 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4556 int dirfd, const char *de_name)
4558 struct revert_file_args *a = arg;
4559 const struct got_error *err = NULL;
4560 char *parent_path = NULL;
4561 struct got_fileindex_entry *ie;
4562 struct got_commit_object *base_commit = NULL;
4563 struct got_tree_object *tree = NULL;
4564 struct got_object_id *tree_id = NULL;
4565 const struct got_tree_entry *te = NULL;
4566 char *tree_path = NULL, *te_name;
4567 char *ondisk_path = NULL, *path_content = NULL;
4568 struct got_blob_object *blob = NULL;
4570 /* Reverting a staged deletion is a no-op. */
4571 if (status == GOT_STATUS_DELETE &&
4572 staged_status != GOT_STATUS_NO_CHANGE)
4575 if (status == GOT_STATUS_UNVERSIONED)
4576 return (*a->progress_cb)(a->progress_arg,
4577 GOT_STATUS_UNVERSIONED, relpath);
4579 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4581 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4583 /* Construct in-repository path of tree which contains this blob. */
4584 err = got_path_dirname(&parent_path, ie->path);
4586 if (err->code != GOT_ERR_BAD_PATH)
4588 parent_path = strdup("/");
4589 if (parent_path == NULL) {
4590 err = got_error_from_errno("strdup");
4594 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4595 tree_path = strdup(parent_path);
4596 if (tree_path == NULL) {
4597 err = got_error_from_errno("strdup");
4601 if (got_path_is_root_dir(parent_path)) {
4602 tree_path = strdup(a->worktree->path_prefix);
4603 if (tree_path == NULL) {
4604 err = got_error_from_errno("strdup");
4608 if (asprintf(&tree_path, "%s/%s",
4609 a->worktree->path_prefix, parent_path) == -1) {
4610 err = got_error_from_errno("asprintf");
4616 err = got_object_open_as_commit(&base_commit, a->repo,
4617 a->worktree->base_commit_id);
4621 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4623 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4624 (status == GOT_STATUS_ADD ||
4625 staged_status == GOT_STATUS_ADD)))
4628 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4632 err = got_path_basename(&te_name, ie->path);
4636 te = got_object_tree_find_entry(tree, te_name);
4638 if (te == NULL && status != GOT_STATUS_ADD &&
4639 staged_status != GOT_STATUS_ADD) {
4640 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4646 case GOT_STATUS_ADD:
4648 int choice = GOT_PATCH_CHOICE_NONE;
4649 err = (*a->patch_cb)(&choice, a->patch_arg,
4650 status, ie->path, NULL, 1, 1);
4653 if (choice != GOT_PATCH_CHOICE_YES)
4656 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4660 got_fileindex_entry_remove(a->fileindex, ie);
4661 if (a->unlink_added_files) {
4662 if (asprintf(&ondisk_path, "%s/%s",
4663 got_worktree_get_root_path(a->worktree),
4665 err = got_error_from_errno("asprintf");
4668 if (unlink(ondisk_path) == -1) {
4669 err = got_error_from_errno2("unlink",
4675 case GOT_STATUS_DELETE:
4677 int choice = GOT_PATCH_CHOICE_NONE;
4678 err = (*a->patch_cb)(&choice, a->patch_arg,
4679 status, ie->path, NULL, 1, 1);
4682 if (choice != GOT_PATCH_CHOICE_YES)
4686 case GOT_STATUS_MODIFY:
4687 case GOT_STATUS_MODE_CHANGE:
4688 case GOT_STATUS_CONFLICT:
4689 case GOT_STATUS_MISSING: {
4690 struct got_object_id id;
4691 if (staged_status == GOT_STATUS_ADD ||
4692 staged_status == GOT_STATUS_MODIFY) {
4693 memcpy(id.sha1, ie->staged_blob_sha1,
4694 SHA1_DIGEST_LENGTH);
4696 memcpy(id.sha1, ie->blob_sha1,
4697 SHA1_DIGEST_LENGTH);
4698 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4702 if (asprintf(&ondisk_path, "%s/%s",
4703 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4704 err = got_error_from_errno("asprintf");
4708 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4709 status == GOT_STATUS_CONFLICT)) {
4710 int is_bad_symlink = 0;
4711 err = create_patched_content(&path_content, 1, &id,
4712 ondisk_path, dirfd, de_name, ie->path, a->repo,
4713 a->patch_cb, a->patch_arg);
4714 if (err || path_content == NULL)
4716 if (te && S_ISLNK(te->mode)) {
4717 if (unlink(path_content) == -1) {
4718 err = got_error_from_errno2("unlink",
4722 err = install_symlink(&is_bad_symlink,
4723 a->worktree, ondisk_path, ie->path,
4724 blob, 0, 1, 0, 0, a->repo,
4725 a->progress_cb, a->progress_arg);
4727 if (rename(path_content, ondisk_path) == -1) {
4728 err = got_error_from_errno3("rename",
4729 path_content, ondisk_path);
4734 int is_bad_symlink = 0;
4735 if (te && S_ISLNK(te->mode)) {
4736 err = install_symlink(&is_bad_symlink,
4737 a->worktree, ondisk_path, ie->path,
4738 blob, 0, 1, 0, 0, a->repo,
4739 a->progress_cb, a->progress_arg);
4741 err = install_blob(a->worktree, ondisk_path,
4743 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4744 got_fileindex_perms_to_st(ie), blob,
4745 0, 1, 0, 0, a->repo,
4746 a->progress_cb, a->progress_arg);
4750 if (status == GOT_STATUS_DELETE ||
4751 status == GOT_STATUS_MODE_CHANGE) {
4752 err = got_fileindex_entry_update(ie,
4753 a->worktree->root_fd, relpath,
4755 a->worktree->base_commit_id->sha1, 1);
4759 if (is_bad_symlink) {
4760 got_fileindex_entry_filetype_set(ie,
4761 GOT_FILEIDX_MODE_BAD_SYMLINK);
4775 got_object_blob_close(blob);
4777 got_object_tree_close(tree);
4780 got_object_commit_close(base_commit);
4784 const struct got_error *
4785 got_worktree_revert(struct got_worktree *worktree,
4786 struct got_pathlist_head *paths,
4787 got_worktree_checkout_cb progress_cb, void *progress_arg,
4788 got_worktree_patch_cb patch_cb, void *patch_arg,
4789 struct got_repository *repo)
4791 struct got_fileindex *fileindex = NULL;
4792 char *fileindex_path = NULL;
4793 const struct got_error *err = NULL, *unlockerr = NULL;
4794 const struct got_error *sync_err = NULL;
4795 struct got_pathlist_entry *pe;
4796 struct revert_file_args rfa;
4798 err = lock_worktree(worktree, LOCK_EX);
4802 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4806 rfa.worktree = worktree;
4807 rfa.fileindex = fileindex;
4808 rfa.progress_cb = progress_cb;
4809 rfa.progress_arg = progress_arg;
4810 rfa.patch_cb = patch_cb;
4811 rfa.patch_arg = patch_arg;
4813 rfa.unlink_added_files = 0;
4814 TAILQ_FOREACH(pe, paths, entry) {
4815 err = worktree_status(worktree, pe->path, fileindex, repo,
4816 revert_file, &rfa, NULL, NULL, 1, 0);
4820 sync_err = sync_fileindex(fileindex, fileindex_path);
4821 if (sync_err && err == NULL)
4824 free(fileindex_path);
4826 got_fileindex_free(fileindex);
4827 unlockerr = lock_worktree(worktree, LOCK_SH);
4828 if (unlockerr && err == NULL)
4834 free_commitable(struct got_commitable *ct)
4837 free(ct->in_repo_path);
4838 free(ct->ondisk_path);
4840 free(ct->base_blob_id);
4841 free(ct->staged_blob_id);
4842 free(ct->base_commit_id);
4846 struct collect_commitables_arg {
4847 struct got_pathlist_head *commitable_paths;
4848 struct got_repository *repo;
4849 struct got_worktree *worktree;
4850 struct got_fileindex *fileindex;
4851 int have_staged_files;
4852 int allow_bad_symlinks;
4855 static const struct got_error *
4856 collect_commitables(void *arg, unsigned char status,
4857 unsigned char staged_status, const char *relpath,
4858 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4859 struct got_object_id *commit_id, int dirfd, const char *de_name)
4861 struct collect_commitables_arg *a = arg;
4862 const struct got_error *err = NULL;
4863 struct got_commitable *ct = NULL;
4864 struct got_pathlist_entry *new = NULL;
4865 char *parent_path = NULL, *path = NULL;
4868 if (a->have_staged_files) {
4869 if (staged_status != GOT_STATUS_MODIFY &&
4870 staged_status != GOT_STATUS_ADD &&
4871 staged_status != GOT_STATUS_DELETE)
4874 if (status == GOT_STATUS_CONFLICT)
4875 return got_error(GOT_ERR_COMMIT_CONFLICT);
4877 if (status != GOT_STATUS_MODIFY &&
4878 status != GOT_STATUS_MODE_CHANGE &&
4879 status != GOT_STATUS_ADD &&
4880 status != GOT_STATUS_DELETE)
4884 if (asprintf(&path, "/%s", relpath) == -1) {
4885 err = got_error_from_errno("asprintf");
4888 if (strcmp(path, "/") == 0) {
4889 parent_path = strdup("");
4890 if (parent_path == NULL)
4891 return got_error_from_errno("strdup");
4893 err = got_path_dirname(&parent_path, path);
4898 ct = calloc(1, sizeof(*ct));
4900 err = got_error_from_errno("calloc");
4904 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4906 err = got_error_from_errno("asprintf");
4910 if (staged_status == GOT_STATUS_ADD ||
4911 staged_status == GOT_STATUS_MODIFY) {
4912 struct got_fileindex_entry *ie;
4913 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4914 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4915 case GOT_FILEIDX_MODE_REGULAR_FILE:
4916 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4919 case GOT_FILEIDX_MODE_SYMLINK:
4923 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4926 ct->mode |= got_fileindex_entry_perms_get(ie);
4927 } else if (status != GOT_STATUS_DELETE &&
4928 staged_status != GOT_STATUS_DELETE) {
4930 if (fstatat(dirfd, de_name, &sb,
4931 AT_SYMLINK_NOFOLLOW) == -1) {
4932 err = got_error_from_errno2("fstatat",
4936 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4937 err = got_error_from_errno2("lstat", ct->ondisk_path);
4940 ct->mode = sb.st_mode;
4943 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4944 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4946 err = got_error_from_errno("asprintf");
4950 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4951 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4953 char target_path[PATH_MAX];
4955 target_len = readlink(ct->ondisk_path, target_path,
4956 sizeof(target_path));
4957 if (target_len == -1) {
4958 err = got_error_from_errno2("readlink",
4962 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4963 target_len, ct->ondisk_path, a->worktree->root_path);
4966 if (is_bad_symlink) {
4967 err = got_error_path(ct->ondisk_path,
4968 GOT_ERR_BAD_SYMLINK);
4974 ct->status = status;
4975 ct->staged_status = staged_status;
4976 ct->blob_id = NULL; /* will be filled in when blob gets created */
4977 if (ct->status != GOT_STATUS_ADD &&
4978 ct->staged_status != GOT_STATUS_ADD) {
4979 ct->base_blob_id = got_object_id_dup(blob_id);
4980 if (ct->base_blob_id == NULL) {
4981 err = got_error_from_errno("got_object_id_dup");
4984 ct->base_commit_id = got_object_id_dup(commit_id);
4985 if (ct->base_commit_id == NULL) {
4986 err = got_error_from_errno("got_object_id_dup");
4990 if (ct->staged_status == GOT_STATUS_ADD ||
4991 ct->staged_status == GOT_STATUS_MODIFY) {
4992 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4993 if (ct->staged_blob_id == NULL) {
4994 err = got_error_from_errno("got_object_id_dup");
4998 ct->path = strdup(path);
4999 if (ct->path == NULL) {
5000 err = got_error_from_errno("strdup");
5003 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5005 if (ct && (err || new == NULL))
5006 free_commitable(ct);
5012 static const struct got_error *write_tree(struct got_object_id **, int *,
5013 struct got_tree_object *, const char *, struct got_pathlist_head *,
5014 got_worktree_status_cb status_cb, void *status_arg,
5015 struct got_repository *);
5017 static const struct got_error *
5018 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5019 struct got_tree_entry *te, const char *parent_path,
5020 struct got_pathlist_head *commitable_paths,
5021 got_worktree_status_cb status_cb, void *status_arg,
5022 struct got_repository *repo)
5024 const struct got_error *err = NULL;
5025 struct got_tree_object *subtree;
5028 if (asprintf(&subpath, "%s%s%s", parent_path,
5029 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5030 return got_error_from_errno("asprintf");
5032 err = got_object_open_as_tree(&subtree, repo, &te->id);
5036 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5037 commitable_paths, status_cb, status_arg, repo);
5038 got_object_tree_close(subtree);
5043 static const struct got_error *
5044 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5046 const struct got_error *err = NULL;
5047 char *ct_parent_path = NULL;
5051 if (strchr(ct->in_repo_path, '/') == NULL) {
5052 *match = got_path_is_root_dir(path);
5056 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5059 *match = (strcmp(path, ct_parent_path) == 0);
5060 free(ct_parent_path);
5065 get_ct_file_mode(struct got_commitable *ct)
5067 if (S_ISLNK(ct->mode))
5070 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5073 static const struct got_error *
5074 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5075 struct got_tree_entry *te, struct got_commitable *ct)
5077 const struct got_error *err = NULL;
5081 err = got_object_tree_entry_dup(new_te, te);
5085 (*new_te)->mode = get_ct_file_mode(ct);
5087 if (ct->staged_status == GOT_STATUS_MODIFY)
5088 memcpy(&(*new_te)->id, ct->staged_blob_id,
5089 sizeof((*new_te)->id));
5091 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5093 if (err && *new_te) {
5100 static const struct got_error *
5101 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5102 struct got_commitable *ct)
5104 const struct got_error *err = NULL;
5105 char *ct_name = NULL;
5109 *new_te = calloc(1, sizeof(**new_te));
5110 if (*new_te == NULL)
5111 return got_error_from_errno("calloc");
5113 err = got_path_basename(&ct_name, ct->path);
5116 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5117 sizeof((*new_te)->name)) {
5118 err = got_error(GOT_ERR_NO_SPACE);
5122 (*new_te)->mode = get_ct_file_mode(ct);
5124 if (ct->staged_status == GOT_STATUS_ADD)
5125 memcpy(&(*new_te)->id, ct->staged_blob_id,
5126 sizeof((*new_te)->id));
5128 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5131 if (err && *new_te) {
5138 static const struct got_error *
5139 insert_tree_entry(struct got_tree_entry *new_te,
5140 struct got_pathlist_head *paths)
5142 const struct got_error *err = NULL;
5143 struct got_pathlist_entry *new_pe;
5145 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5149 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5153 static const struct got_error *
5154 report_ct_status(struct got_commitable *ct,
5155 got_worktree_status_cb status_cb, void *status_arg)
5157 const char *ct_path = ct->path;
5158 unsigned char status;
5160 if (status_cb == NULL) /* no commit progress output desired */
5163 while (ct_path[0] == '/')
5166 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5167 status = ct->staged_status;
5169 status = ct->status;
5171 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5172 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5175 static const struct got_error *
5176 match_modified_subtree(int *modified, struct got_tree_entry *te,
5177 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5179 const struct got_error *err = NULL;
5180 struct got_pathlist_entry *pe;
5185 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5186 got_path_is_root_dir(base_tree_path) ? "" : "/",
5188 return got_error_from_errno("asprintf");
5190 TAILQ_FOREACH(pe, commitable_paths, entry) {
5191 struct got_commitable *ct = pe->data;
5192 *modified = got_path_is_child(ct->in_repo_path, te_path,
5202 static const struct got_error *
5203 match_deleted_or_modified_ct(struct got_commitable **ctp,
5204 struct got_tree_entry *te, const char *base_tree_path,
5205 struct got_pathlist_head *commitable_paths)
5207 const struct got_error *err = NULL;
5208 struct got_pathlist_entry *pe;
5212 TAILQ_FOREACH(pe, commitable_paths, entry) {
5213 struct got_commitable *ct = pe->data;
5214 char *ct_name = NULL;
5217 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5218 if (ct->status != GOT_STATUS_MODIFY &&
5219 ct->status != GOT_STATUS_MODE_CHANGE &&
5220 ct->status != GOT_STATUS_DELETE)
5223 if (ct->staged_status != GOT_STATUS_MODIFY &&
5224 ct->staged_status != GOT_STATUS_DELETE)
5228 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5231 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5237 err = got_path_basename(&ct_name, pe->path);
5241 if (strcmp(te->name, ct_name) != 0) {
5254 static const struct got_error *
5255 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5256 const char *child_path, const char *path_base_tree,
5257 struct got_pathlist_head *commitable_paths,
5258 got_worktree_status_cb status_cb, void *status_arg,
5259 struct got_repository *repo)
5261 const struct got_error *err = NULL;
5262 struct got_tree_entry *new_te;
5264 struct got_object_id *id = NULL;
5269 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5270 got_path_is_root_dir(path_base_tree) ? "" : "/",
5272 return got_error_from_errno("asprintf");
5274 new_te = calloc(1, sizeof(*new_te));
5276 return got_error_from_errno("calloc");
5277 new_te->mode = S_IFDIR;
5279 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5280 sizeof(new_te->name)) {
5281 err = got_error(GOT_ERR_NO_SPACE);
5284 err = write_tree(&id, &nentries, NULL, subtree_path,
5285 commitable_paths, status_cb, status_arg, repo);
5290 memcpy(&new_te->id, id, sizeof(new_te->id));
5299 static const struct got_error *
5300 write_tree(struct got_object_id **new_tree_id, int *nentries,
5301 struct got_tree_object *base_tree, const char *path_base_tree,
5302 struct got_pathlist_head *commitable_paths,
5303 got_worktree_status_cb status_cb, void *status_arg,
5304 struct got_repository *repo)
5306 const struct got_error *err = NULL;
5307 struct got_pathlist_head paths;
5308 struct got_tree_entry *te, *new_te = NULL;
5309 struct got_pathlist_entry *pe;
5314 /* Insert, and recurse into, newly added entries first. */
5315 TAILQ_FOREACH(pe, commitable_paths, entry) {
5316 struct got_commitable *ct = pe->data;
5317 char *child_path = NULL, *slash;
5319 if ((ct->status != GOT_STATUS_ADD &&
5320 ct->staged_status != GOT_STATUS_ADD) ||
5321 (ct->flags & GOT_COMMITABLE_ADDED))
5324 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5325 strlen(path_base_tree)))
5328 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5333 slash = strchr(child_path, '/');
5334 if (slash == NULL) {
5335 err = alloc_added_blob_tree_entry(&new_te, ct);
5338 err = report_ct_status(ct, status_cb, status_arg);
5341 ct->flags |= GOT_COMMITABLE_ADDED;
5342 err = insert_tree_entry(new_te, &paths);
5347 *slash = '\0'; /* trim trailing path components */
5348 if (base_tree == NULL ||
5349 got_object_tree_find_entry(base_tree, child_path)
5351 err = make_subtree_for_added_blob(&new_te,
5352 child_path, path_base_tree,
5353 commitable_paths, status_cb, status_arg,
5357 err = insert_tree_entry(new_te, &paths);
5366 int i, nbase_entries;
5367 /* Handle modified and deleted entries. */
5368 nbase_entries = got_object_tree_get_nentries(base_tree);
5369 for (i = 0; i < nbase_entries; i++) {
5370 struct got_commitable *ct = NULL;
5372 te = got_object_tree_get_entry(base_tree, i);
5373 if (got_object_tree_entry_is_submodule(te)) {
5374 /* Entry is a submodule; just copy it. */
5375 err = got_object_tree_entry_dup(&new_te, te);
5378 err = insert_tree_entry(new_te, &paths);
5385 if (S_ISDIR(te->mode)) {
5387 err = got_object_tree_entry_dup(&new_te, te);
5390 err = match_modified_subtree(&modified, te,
5391 path_base_tree, commitable_paths);
5394 /* Avoid recursion into unmodified subtrees. */
5396 struct got_object_id *new_id;
5398 err = write_subtree(&new_id,
5400 path_base_tree, commitable_paths,
5401 status_cb, status_arg, repo);
5404 if (nsubentries == 0) {
5405 /* All entries were deleted. */
5409 memcpy(&new_te->id, new_id,
5410 sizeof(new_te->id));
5413 err = insert_tree_entry(new_te, &paths);
5420 err = match_deleted_or_modified_ct(&ct, te,
5421 path_base_tree, commitable_paths);
5425 /* NB: Deleted entries get dropped here. */
5426 if (ct->status == GOT_STATUS_MODIFY ||
5427 ct->status == GOT_STATUS_MODE_CHANGE ||
5428 ct->staged_status == GOT_STATUS_MODIFY) {
5429 err = alloc_modified_blob_tree_entry(
5433 err = insert_tree_entry(new_te, &paths);
5438 err = report_ct_status(ct, status_cb,
5443 /* Entry is unchanged; just copy it. */
5444 err = got_object_tree_entry_dup(&new_te, te);
5447 err = insert_tree_entry(new_te, &paths);
5455 /* Write new list of entries; deleted entries have been dropped. */
5456 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5458 got_pathlist_free(&paths);
5462 static const struct got_error *
5463 update_fileindex_after_commit(struct got_worktree *worktree,
5464 struct got_pathlist_head *commitable_paths,
5465 struct got_object_id *new_base_commit_id,
5466 struct got_fileindex *fileindex, int have_staged_files)
5468 const struct got_error *err = NULL;
5469 struct got_pathlist_entry *pe;
5470 char *relpath = NULL;
5472 TAILQ_FOREACH(pe, commitable_paths, entry) {
5473 struct got_fileindex_entry *ie;
5474 struct got_commitable *ct = pe->data;
5476 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5478 err = got_path_skip_common_ancestor(&relpath,
5479 worktree->root_path, ct->ondisk_path);
5484 if (ct->status == GOT_STATUS_DELETE ||
5485 ct->staged_status == GOT_STATUS_DELETE) {
5486 got_fileindex_entry_remove(fileindex, ie);
5487 } else if (ct->staged_status == GOT_STATUS_ADD ||
5488 ct->staged_status == GOT_STATUS_MODIFY) {
5489 got_fileindex_entry_stage_set(ie,
5490 GOT_FILEIDX_STAGE_NONE);
5491 got_fileindex_entry_staged_filetype_set(ie, 0);
5493 err = got_fileindex_entry_update(ie,
5494 worktree->root_fd, relpath,
5495 ct->staged_blob_id->sha1,
5496 new_base_commit_id->sha1,
5497 !have_staged_files);
5499 err = got_fileindex_entry_update(ie,
5500 worktree->root_fd, relpath,
5502 new_base_commit_id->sha1,
5503 !have_staged_files);
5505 err = got_fileindex_entry_alloc(&ie, pe->path);
5508 err = got_fileindex_entry_update(ie,
5509 worktree->root_fd, relpath, ct->blob_id->sha1,
5510 new_base_commit_id->sha1, 1);
5512 got_fileindex_entry_free(ie);
5515 err = got_fileindex_entry_add(fileindex, ie);
5517 got_fileindex_entry_free(ie);
5530 static const struct got_error *
5531 check_out_of_date(const char *in_repo_path, unsigned char status,
5532 unsigned char staged_status, struct got_object_id *base_blob_id,
5533 struct got_object_id *base_commit_id,
5534 struct got_object_id *head_commit_id, struct got_repository *repo,
5537 const struct got_error *err = NULL;
5538 struct got_commit_object *commit = NULL;
5539 struct got_object_id *id = NULL;
5541 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5542 /* Trivial case: base commit == head commit */
5543 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5546 * Ensure file content which local changes were based
5547 * on matches file content in the branch head.
5549 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5552 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5554 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5555 err = got_error(ood_errcode);
5557 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5558 err = got_error(ood_errcode);
5560 /* Require that added files don't exist in the branch head. */
5561 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5564 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5565 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5567 err = id ? got_error(ood_errcode) : NULL;
5572 got_object_commit_close(commit);
5576 const struct got_error *
5577 commit_worktree(struct got_object_id **new_commit_id,
5578 struct got_pathlist_head *commitable_paths,
5579 struct got_object_id *head_commit_id,
5580 struct got_object_id *parent_id2,
5581 struct got_worktree *worktree,
5582 const char *author, const char *committer,
5583 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5584 got_worktree_status_cb status_cb, void *status_arg,
5585 struct got_repository *repo)
5587 const struct got_error *err = NULL, *unlockerr = NULL;
5588 struct got_pathlist_entry *pe;
5589 const char *head_ref_name = NULL;
5590 struct got_commit_object *head_commit = NULL;
5591 struct got_reference *head_ref2 = NULL;
5592 struct got_object_id *head_commit_id2 = NULL;
5593 struct got_tree_object *head_tree = NULL;
5594 struct got_object_id *new_tree_id = NULL;
5595 int nentries, nparents = 0;
5596 struct got_object_id_queue parent_ids;
5597 struct got_object_qid *pid = NULL;
5598 char *logmsg = NULL;
5600 *new_commit_id = NULL;
5602 STAILQ_INIT(&parent_ids);
5604 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5608 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5612 if (commit_msg_cb != NULL) {
5613 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5618 if (logmsg == NULL || strlen(logmsg) == 0) {
5619 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5623 /* Create blobs from added and modified files and record their IDs. */
5624 TAILQ_FOREACH(pe, commitable_paths, entry) {
5625 struct got_commitable *ct = pe->data;
5628 /* Blobs for staged files already exist. */
5629 if (ct->staged_status == GOT_STATUS_ADD ||
5630 ct->staged_status == GOT_STATUS_MODIFY)
5633 if (ct->status != GOT_STATUS_ADD &&
5634 ct->status != GOT_STATUS_MODIFY &&
5635 ct->status != GOT_STATUS_MODE_CHANGE)
5638 if (asprintf(&ondisk_path, "%s/%s",
5639 worktree->root_path, pe->path) == -1) {
5640 err = got_error_from_errno("asprintf");
5643 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5649 /* Recursively write new tree objects. */
5650 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5651 commitable_paths, status_cb, status_arg, repo);
5655 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5658 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5661 err = got_object_qid_alloc(&pid, parent_id2);
5664 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5667 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5668 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5674 /* Check if a concurrent commit to our branch has occurred. */
5675 head_ref_name = got_worktree_get_head_ref_name(worktree);
5676 if (head_ref_name == NULL) {
5677 err = got_error_from_errno("got_worktree_get_head_ref_name");
5680 /* Lock the reference here to prevent concurrent modification. */
5681 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5684 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5687 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5688 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5691 /* Update branch head in repository. */
5692 err = got_ref_change_ref(head_ref2, *new_commit_id);
5695 err = got_ref_write(head_ref2, repo);
5699 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5703 err = ref_base_commit(worktree, repo);
5707 got_object_id_queue_free(&parent_ids);
5709 got_object_tree_close(head_tree);
5711 got_object_commit_close(head_commit);
5712 free(head_commit_id2);
5714 unlockerr = got_ref_unlock(head_ref2);
5715 if (unlockerr && err == NULL)
5717 got_ref_close(head_ref2);
5722 static const struct got_error *
5723 check_path_is_commitable(const char *path,
5724 struct got_pathlist_head *commitable_paths)
5726 struct got_pathlist_entry *cpe = NULL;
5727 size_t path_len = strlen(path);
5729 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5730 struct got_commitable *ct = cpe->data;
5731 const char *ct_path = ct->path;
5733 while (ct_path[0] == '/')
5736 if (strcmp(path, ct_path) == 0 ||
5737 got_path_is_child(ct_path, path, path_len))
5742 return got_error_path(path, GOT_ERR_BAD_PATH);
5747 static const struct got_error *
5748 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5750 int *have_staged_files = arg;
5752 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5753 *have_staged_files = 1;
5754 return got_error(GOT_ERR_CANCELLED);
5760 static const struct got_error *
5761 check_non_staged_files(struct got_fileindex *fileindex,
5762 struct got_pathlist_head *paths)
5764 struct got_pathlist_entry *pe;
5765 struct got_fileindex_entry *ie;
5767 TAILQ_FOREACH(pe, paths, entry) {
5768 if (pe->path[0] == '\0')
5770 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5772 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5773 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5774 return got_error_path(pe->path,
5775 GOT_ERR_FILE_NOT_STAGED);
5781 const struct got_error *
5782 got_worktree_commit(struct got_object_id **new_commit_id,
5783 struct got_worktree *worktree, struct got_pathlist_head *paths,
5784 const char *author, const char *committer, int allow_bad_symlinks,
5785 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5786 got_worktree_status_cb status_cb, void *status_arg,
5787 struct got_repository *repo)
5789 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5790 struct got_fileindex *fileindex = NULL;
5791 char *fileindex_path = NULL;
5792 struct got_pathlist_head commitable_paths;
5793 struct collect_commitables_arg cc_arg;
5794 struct got_pathlist_entry *pe;
5795 struct got_reference *head_ref = NULL;
5796 struct got_object_id *head_commit_id = NULL;
5797 int have_staged_files = 0;
5799 *new_commit_id = NULL;
5801 TAILQ_INIT(&commitable_paths);
5803 err = lock_worktree(worktree, LOCK_EX);
5807 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5811 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5815 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5819 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5820 &have_staged_files);
5821 if (err && err->code != GOT_ERR_CANCELLED)
5823 if (have_staged_files) {
5824 err = check_non_staged_files(fileindex, paths);
5829 cc_arg.commitable_paths = &commitable_paths;
5830 cc_arg.worktree = worktree;
5831 cc_arg.fileindex = fileindex;
5833 cc_arg.have_staged_files = have_staged_files;
5834 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5835 TAILQ_FOREACH(pe, paths, entry) {
5836 err = worktree_status(worktree, pe->path, fileindex, repo,
5837 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5842 if (TAILQ_EMPTY(&commitable_paths)) {
5843 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5847 TAILQ_FOREACH(pe, paths, entry) {
5848 err = check_path_is_commitable(pe->path, &commitable_paths);
5853 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5854 struct got_commitable *ct = pe->data;
5855 const char *ct_path = ct->in_repo_path;
5857 while (ct_path[0] == '/')
5859 err = check_out_of_date(ct_path, ct->status,
5860 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5861 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5867 err = commit_worktree(new_commit_id, &commitable_paths,
5868 head_commit_id, NULL, worktree, author, committer,
5869 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5873 err = update_fileindex_after_commit(worktree, &commitable_paths,
5874 *new_commit_id, fileindex, have_staged_files);
5875 sync_err = sync_fileindex(fileindex, fileindex_path);
5876 if (sync_err && err == NULL)
5880 got_fileindex_free(fileindex);
5881 free(fileindex_path);
5882 unlockerr = lock_worktree(worktree, LOCK_SH);
5883 if (unlockerr && err == NULL)
5885 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5886 struct got_commitable *ct = pe->data;
5887 free_commitable(ct);
5889 got_pathlist_free(&commitable_paths);
5894 got_commitable_get_path(struct got_commitable *ct)
5900 got_commitable_get_status(struct got_commitable *ct)
5905 struct check_rebase_ok_arg {
5906 struct got_worktree *worktree;
5907 struct got_repository *repo;
5910 static const struct got_error *
5911 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5913 const struct got_error *err = NULL;
5914 struct check_rebase_ok_arg *a = arg;
5915 unsigned char status;
5919 /* Reject rebase of a work tree with mixed base commits. */
5920 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5921 SHA1_DIGEST_LENGTH))
5922 return got_error(GOT_ERR_MIXED_COMMITS);
5924 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5926 return got_error_from_errno("asprintf");
5928 /* Reject rebase of a work tree with modified or staged files. */
5929 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5934 if (status != GOT_STATUS_NO_CHANGE)
5935 return got_error(GOT_ERR_MODIFIED);
5936 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5937 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5942 const struct got_error *
5943 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5944 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5945 struct got_worktree *worktree, struct got_reference *branch,
5946 struct got_repository *repo)
5948 const struct got_error *err = NULL;
5949 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5950 char *branch_ref_name = NULL;
5951 char *fileindex_path = NULL;
5952 struct check_rebase_ok_arg ok_arg;
5953 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5954 struct got_object_id *wt_branch_tip = NULL;
5956 *new_base_branch_ref = NULL;
5960 err = lock_worktree(worktree, LOCK_EX);
5964 err = open_fileindex(fileindex, &fileindex_path, worktree);
5968 ok_arg.worktree = worktree;
5970 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5975 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5979 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5983 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5987 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5992 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5995 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5996 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6000 err = got_ref_alloc_symref(new_base_branch_ref,
6001 new_base_branch_ref_name, wt_branch);
6004 err = got_ref_write(*new_base_branch_ref, repo);
6008 /* TODO Lock original branch's ref while rebasing? */
6010 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6014 err = got_ref_write(branch_ref, repo);
6018 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6019 worktree->base_commit_id);
6022 err = got_ref_write(*tmp_branch, repo);
6026 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6030 free(fileindex_path);
6031 free(tmp_branch_name);
6032 free(new_base_branch_ref_name);
6033 free(branch_ref_name);
6035 got_ref_close(branch_ref);
6037 got_ref_close(wt_branch);
6038 free(wt_branch_tip);
6040 if (*new_base_branch_ref) {
6041 got_ref_close(*new_base_branch_ref);
6042 *new_base_branch_ref = NULL;
6045 got_ref_close(*tmp_branch);
6049 got_fileindex_free(*fileindex);
6052 lock_worktree(worktree, LOCK_SH);
6057 const struct got_error *
6058 got_worktree_rebase_continue(struct got_object_id **commit_id,
6059 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6060 struct got_reference **branch, struct got_fileindex **fileindex,
6061 struct got_worktree *worktree, struct got_repository *repo)
6063 const struct got_error *err;
6064 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6065 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6066 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6067 char *fileindex_path = NULL;
6068 int have_staged_files = 0;
6071 *new_base_branch = NULL;
6076 err = lock_worktree(worktree, LOCK_EX);
6080 err = open_fileindex(fileindex, &fileindex_path, worktree);
6084 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6085 &have_staged_files);
6086 if (err && err->code != GOT_ERR_CANCELLED)
6088 if (have_staged_files) {
6089 err = got_error(GOT_ERR_STAGED_PATHS);
6093 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6097 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6101 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6105 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6109 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6113 err = got_ref_open(branch, repo,
6114 got_ref_get_symref_target(branch_ref), 0);
6118 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6122 err = got_ref_resolve(commit_id, repo, commit_ref);
6126 err = got_ref_open(new_base_branch, repo,
6127 new_base_branch_ref_name, 0);
6131 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6135 free(commit_ref_name);
6136 free(branch_ref_name);
6137 free(fileindex_path);
6139 got_ref_close(commit_ref);
6141 got_ref_close(branch_ref);
6146 got_ref_close(*tmp_branch);
6149 if (*new_base_branch) {
6150 got_ref_close(*new_base_branch);
6151 *new_base_branch = NULL;
6154 got_ref_close(*branch);
6158 got_fileindex_free(*fileindex);
6161 lock_worktree(worktree, LOCK_SH);
6166 const struct got_error *
6167 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6169 const struct got_error *err;
6170 char *tmp_branch_name = NULL;
6172 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6176 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6177 free(tmp_branch_name);
6181 static const struct got_error *
6182 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6183 char **logmsg, void *arg)
6189 static const struct got_error *
6190 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6191 const char *path, struct got_object_id *blob_id,
6192 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6193 int dirfd, const char *de_name)
6198 struct collect_merged_paths_arg {
6199 got_worktree_checkout_cb progress_cb;
6201 struct got_pathlist_head *merged_paths;
6204 static const struct got_error *
6205 collect_merged_paths(void *arg, unsigned char status, const char *path)
6207 const struct got_error *err;
6208 struct collect_merged_paths_arg *a = arg;
6210 struct got_pathlist_entry *new;
6212 err = (*a->progress_cb)(a->progress_arg, status, path);
6216 if (status != GOT_STATUS_MERGE &&
6217 status != GOT_STATUS_ADD &&
6218 status != GOT_STATUS_DELETE &&
6219 status != GOT_STATUS_CONFLICT)
6224 return got_error_from_errno("strdup");
6226 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6227 if (err || new == NULL)
6233 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6235 struct got_pathlist_entry *pe;
6237 TAILQ_FOREACH(pe, merged_paths, entry)
6238 free((char *)pe->path);
6240 got_pathlist_free(merged_paths);
6243 static const struct got_error *
6244 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6245 int is_rebase, struct got_repository *repo)
6247 const struct got_error *err;
6248 struct got_reference *commit_ref = NULL;
6250 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6252 if (err->code != GOT_ERR_NOT_REF)
6254 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6257 err = got_ref_write(commit_ref, repo);
6260 } else if (is_rebase) {
6261 struct got_object_id *stored_id;
6264 err = got_ref_resolve(&stored_id, repo, commit_ref);
6267 cmp = got_object_id_cmp(commit_id, stored_id);
6270 err = got_error(GOT_ERR_REBASE_COMMITID);
6276 got_ref_close(commit_ref);
6280 static const struct got_error *
6281 rebase_merge_files(struct got_pathlist_head *merged_paths,
6282 const char *commit_ref_name, struct got_worktree *worktree,
6283 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6284 struct got_object_id *commit_id, struct got_repository *repo,
6285 got_worktree_checkout_cb progress_cb, void *progress_arg,
6286 got_cancel_cb cancel_cb, void *cancel_arg)
6288 const struct got_error *err;
6289 struct got_reference *commit_ref = NULL;
6290 struct collect_merged_paths_arg cmp_arg;
6291 char *fileindex_path;
6293 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6295 err = get_fileindex_path(&fileindex_path, worktree);
6299 cmp_arg.progress_cb = progress_cb;
6300 cmp_arg.progress_arg = progress_arg;
6301 cmp_arg.merged_paths = merged_paths;
6302 err = merge_files(worktree, fileindex, fileindex_path,
6303 parent_commit_id, commit_id, repo, collect_merged_paths,
6304 &cmp_arg, cancel_cb, cancel_arg);
6306 got_ref_close(commit_ref);
6310 const struct got_error *
6311 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6312 struct got_worktree *worktree, struct got_fileindex *fileindex,
6313 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6314 struct got_repository *repo,
6315 got_worktree_checkout_cb progress_cb, void *progress_arg,
6316 got_cancel_cb cancel_cb, void *cancel_arg)
6318 const struct got_error *err;
6319 char *commit_ref_name;
6321 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6325 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6329 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6330 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6331 progress_arg, cancel_cb, cancel_arg);
6333 free(commit_ref_name);
6337 const struct got_error *
6338 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6339 struct got_worktree *worktree, struct got_fileindex *fileindex,
6340 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6341 struct got_repository *repo,
6342 got_worktree_checkout_cb progress_cb, void *progress_arg,
6343 got_cancel_cb cancel_cb, void *cancel_arg)
6345 const struct got_error *err;
6346 char *commit_ref_name;
6348 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6352 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6356 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6357 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6358 progress_arg, cancel_cb, cancel_arg);
6360 free(commit_ref_name);
6364 static const struct got_error *
6365 rebase_commit(struct got_object_id **new_commit_id,
6366 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6367 struct got_worktree *worktree, struct got_fileindex *fileindex,
6368 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6369 const char *new_logmsg, struct got_repository *repo)
6371 const struct got_error *err, *sync_err;
6372 struct got_pathlist_head commitable_paths;
6373 struct collect_commitables_arg cc_arg;
6374 char *fileindex_path = NULL;
6375 struct got_reference *head_ref = NULL;
6376 struct got_object_id *head_commit_id = NULL;
6377 char *logmsg = NULL;
6379 TAILQ_INIT(&commitable_paths);
6380 *new_commit_id = NULL;
6382 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6384 err = get_fileindex_path(&fileindex_path, worktree);
6388 cc_arg.commitable_paths = &commitable_paths;
6389 cc_arg.worktree = worktree;
6391 cc_arg.have_staged_files = 0;
6393 * If possible get the status of individual files directly to
6394 * avoid crawling the entire work tree once per rebased commit.
6396 * Ideally, merged_paths would contain a list of commitables
6397 * we could use so we could skip worktree_status() entirely.
6398 * However, we would then need carefully keep track of cumulative
6399 * effects of operations such as file additions and deletions
6400 * in 'got histedit -f' (folding multiple commits into one),
6401 * and this extra complexity is not really worth it.
6404 struct got_pathlist_entry *pe;
6405 TAILQ_FOREACH(pe, merged_paths, entry) {
6406 err = worktree_status(worktree, pe->path, fileindex,
6407 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6413 err = worktree_status(worktree, "", fileindex, repo,
6414 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6419 if (TAILQ_EMPTY(&commitable_paths)) {
6420 /* No-op change; commit will be elided. */
6421 err = got_ref_delete(commit_ref, repo);
6424 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6428 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6432 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6437 logmsg = strdup(new_logmsg);
6438 if (logmsg == NULL) {
6439 err = got_error_from_errno("strdup");
6443 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6448 /* NB: commit_worktree will call free(logmsg) */
6449 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6450 NULL, worktree, got_object_commit_get_author(orig_commit),
6451 got_object_commit_get_committer(orig_commit),
6452 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6456 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6460 err = got_ref_delete(commit_ref, repo);
6464 err = update_fileindex_after_commit(worktree, &commitable_paths,
6465 *new_commit_id, fileindex, 0);
6466 sync_err = sync_fileindex(fileindex, fileindex_path);
6467 if (sync_err && err == NULL)
6470 free(fileindex_path);
6471 free(head_commit_id);
6473 got_ref_close(head_ref);
6475 free(*new_commit_id);
6476 *new_commit_id = NULL;
6481 const struct got_error *
6482 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6483 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6484 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6485 struct got_commit_object *orig_commit,
6486 struct got_object_id *orig_commit_id, struct got_repository *repo)
6488 const struct got_error *err;
6489 char *commit_ref_name;
6490 struct got_reference *commit_ref = NULL;
6491 struct got_object_id *commit_id = NULL;
6493 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6497 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6500 err = got_ref_resolve(&commit_id, repo, commit_ref);
6503 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6504 err = got_error(GOT_ERR_REBASE_COMMITID);
6508 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6509 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6512 got_ref_close(commit_ref);
6513 free(commit_ref_name);
6518 const struct got_error *
6519 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6520 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6521 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6522 struct got_commit_object *orig_commit,
6523 struct got_object_id *orig_commit_id, const char *new_logmsg,
6524 struct got_repository *repo)
6526 const struct got_error *err;
6527 char *commit_ref_name;
6528 struct got_reference *commit_ref = NULL;
6530 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6534 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6538 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6539 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6542 got_ref_close(commit_ref);
6543 free(commit_ref_name);
6547 const struct got_error *
6548 got_worktree_rebase_postpone(struct got_worktree *worktree,
6549 struct got_fileindex *fileindex)
6552 got_fileindex_free(fileindex);
6553 return lock_worktree(worktree, LOCK_SH);
6556 static const struct got_error *
6557 delete_ref(const char *name, struct got_repository *repo)
6559 const struct got_error *err;
6560 struct got_reference *ref;
6562 err = got_ref_open(&ref, repo, name, 0);
6564 if (err->code == GOT_ERR_NOT_REF)
6569 err = got_ref_delete(ref, repo);
6574 static const struct got_error *
6575 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6577 const struct got_error *err;
6578 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6579 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6581 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6584 err = delete_ref(tmp_branch_name, repo);
6588 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6591 err = delete_ref(new_base_branch_ref_name, repo);
6595 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6598 err = delete_ref(branch_ref_name, repo);
6602 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6605 err = delete_ref(commit_ref_name, repo);
6610 free(tmp_branch_name);
6611 free(new_base_branch_ref_name);
6612 free(branch_ref_name);
6613 free(commit_ref_name);
6617 const struct got_error *
6618 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6619 struct got_object_id *new_commit_id, struct got_repository *repo)
6621 const struct got_error *err;
6622 struct got_reference *ref = NULL;
6623 struct got_object_id *old_commit_id = NULL;
6624 const char *branch_name = NULL;
6625 char *new_id_str = NULL;
6626 char *refname = NULL;
6628 branch_name = got_ref_get_name(branch);
6629 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6630 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6633 err = got_object_id_str(&new_id_str, new_commit_id);
6637 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6638 new_id_str) == -1) {
6639 err = got_error_from_errno("asprintf");
6643 err = got_ref_resolve(&old_commit_id, repo, branch);
6647 err = got_ref_alloc(&ref, refname, old_commit_id);
6651 err = got_ref_write(ref, repo);
6655 free(old_commit_id);
6661 const struct got_error *
6662 got_worktree_rebase_complete(struct got_worktree *worktree,
6663 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6664 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6665 struct got_repository *repo, int create_backup)
6667 const struct got_error *err, *unlockerr, *sync_err;
6668 struct got_object_id *new_head_commit_id = NULL;
6669 char *fileindex_path = NULL;
6671 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6675 if (create_backup) {
6676 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6677 rebased_branch, new_head_commit_id, repo);
6682 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6686 err = got_ref_write(rebased_branch, repo);
6690 err = got_worktree_set_head_ref(worktree, rebased_branch);
6694 err = delete_rebase_refs(worktree, repo);
6698 err = get_fileindex_path(&fileindex_path, worktree);
6701 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6702 sync_err = sync_fileindex(fileindex, fileindex_path);
6703 if (sync_err && err == NULL)
6706 got_fileindex_free(fileindex);
6707 free(fileindex_path);
6708 free(new_head_commit_id);
6709 unlockerr = lock_worktree(worktree, LOCK_SH);
6710 if (unlockerr && err == NULL)
6715 const struct got_error *
6716 got_worktree_rebase_abort(struct got_worktree *worktree,
6717 struct got_fileindex *fileindex, struct got_repository *repo,
6718 struct got_reference *new_base_branch,
6719 got_worktree_checkout_cb progress_cb, void *progress_arg)
6721 const struct got_error *err, *unlockerr, *sync_err;
6722 struct got_reference *resolved = NULL;
6723 struct got_object_id *commit_id = NULL;
6724 struct got_commit_object *commit = NULL;
6725 char *fileindex_path = NULL;
6726 struct revert_file_args rfa;
6727 struct got_object_id *tree_id = NULL;
6729 err = lock_worktree(worktree, LOCK_EX);
6733 err = got_object_open_as_commit(&commit, repo,
6734 worktree->base_commit_id);
6738 err = got_ref_open(&resolved, repo,
6739 got_ref_get_symref_target(new_base_branch), 0);
6743 err = got_worktree_set_head_ref(worktree, resolved);
6748 * XXX commits to the base branch could have happened while
6749 * we were busy rebasing; should we store the original commit ID
6750 * when rebase begins and read it back here?
6752 err = got_ref_resolve(&commit_id, repo, resolved);
6756 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6760 err = got_object_id_by_path(&tree_id, repo, commit,
6761 worktree->path_prefix);
6765 err = delete_rebase_refs(worktree, repo);
6769 err = get_fileindex_path(&fileindex_path, worktree);
6773 rfa.worktree = worktree;
6774 rfa.fileindex = fileindex;
6775 rfa.progress_cb = progress_cb;
6776 rfa.progress_arg = progress_arg;
6777 rfa.patch_cb = NULL;
6778 rfa.patch_arg = NULL;
6780 rfa.unlink_added_files = 0;
6781 err = worktree_status(worktree, "", fileindex, repo,
6782 revert_file, &rfa, NULL, NULL, 1, 0);
6786 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6787 repo, progress_cb, progress_arg, NULL, NULL);
6789 sync_err = sync_fileindex(fileindex, fileindex_path);
6790 if (sync_err && err == NULL)
6793 got_ref_close(resolved);
6797 got_object_commit_close(commit);
6799 got_fileindex_free(fileindex);
6800 free(fileindex_path);
6802 unlockerr = lock_worktree(worktree, LOCK_SH);
6803 if (unlockerr && err == NULL)
6808 const struct got_error *
6809 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6810 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6811 struct got_fileindex **fileindex, struct got_worktree *worktree,
6812 struct got_repository *repo)
6814 const struct got_error *err = NULL;
6815 char *tmp_branch_name = NULL;
6816 char *branch_ref_name = NULL;
6817 char *base_commit_ref_name = NULL;
6818 char *fileindex_path = NULL;
6819 struct check_rebase_ok_arg ok_arg;
6820 struct got_reference *wt_branch = NULL;
6821 struct got_reference *base_commit_ref = NULL;
6825 *base_commit_id = NULL;
6828 err = lock_worktree(worktree, LOCK_EX);
6832 err = open_fileindex(fileindex, &fileindex_path, worktree);
6836 ok_arg.worktree = worktree;
6838 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6843 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6847 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6851 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6856 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6861 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6865 err = got_ref_write(*branch_ref, repo);
6869 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6870 worktree->base_commit_id);
6873 err = got_ref_write(base_commit_ref, repo);
6876 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6877 if (*base_commit_id == NULL) {
6878 err = got_error_from_errno("got_object_id_dup");
6882 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6883 worktree->base_commit_id);
6886 err = got_ref_write(*tmp_branch, repo);
6890 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6894 free(fileindex_path);
6895 free(tmp_branch_name);
6896 free(branch_ref_name);
6897 free(base_commit_ref_name);
6899 got_ref_close(wt_branch);
6902 got_ref_close(*branch_ref);
6906 got_ref_close(*tmp_branch);
6909 free(*base_commit_id);
6911 got_fileindex_free(*fileindex);
6914 lock_worktree(worktree, LOCK_SH);
6919 const struct got_error *
6920 got_worktree_histedit_postpone(struct got_worktree *worktree,
6921 struct got_fileindex *fileindex)
6924 got_fileindex_free(fileindex);
6925 return lock_worktree(worktree, LOCK_SH);
6928 const struct got_error *
6929 got_worktree_histedit_in_progress(int *in_progress,
6930 struct got_worktree *worktree)
6932 const struct got_error *err;
6933 char *tmp_branch_name = NULL;
6935 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6939 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6940 free(tmp_branch_name);
6944 const struct got_error *
6945 got_worktree_histedit_continue(struct got_object_id **commit_id,
6946 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6947 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6948 struct got_worktree *worktree, struct got_repository *repo)
6950 const struct got_error *err;
6951 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6952 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6953 struct got_reference *commit_ref = NULL;
6954 struct got_reference *base_commit_ref = NULL;
6955 char *fileindex_path = NULL;
6956 int have_staged_files = 0;
6960 *base_commit_id = NULL;
6963 err = lock_worktree(worktree, LOCK_EX);
6967 err = open_fileindex(fileindex, &fileindex_path, worktree);
6971 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6972 &have_staged_files);
6973 if (err && err->code != GOT_ERR_CANCELLED)
6975 if (have_staged_files) {
6976 err = got_error(GOT_ERR_STAGED_PATHS);
6980 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6984 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6988 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6992 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6997 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7001 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7004 err = got_ref_resolve(commit_id, repo, commit_ref);
7008 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7011 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7015 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7019 free(commit_ref_name);
7020 free(branch_ref_name);
7021 free(fileindex_path);
7023 got_ref_close(commit_ref);
7024 if (base_commit_ref)
7025 got_ref_close(base_commit_ref);
7029 free(*base_commit_id);
7030 *base_commit_id = NULL;
7032 got_ref_close(*tmp_branch);
7036 got_fileindex_free(*fileindex);
7039 lock_worktree(worktree, LOCK_EX);
7044 static const struct got_error *
7045 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7047 const struct got_error *err;
7048 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7049 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7051 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7054 err = delete_ref(tmp_branch_name, repo);
7058 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7062 err = delete_ref(base_commit_ref_name, repo);
7066 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7069 err = delete_ref(branch_ref_name, repo);
7073 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7076 err = delete_ref(commit_ref_name, repo);
7080 free(tmp_branch_name);
7081 free(base_commit_ref_name);
7082 free(branch_ref_name);
7083 free(commit_ref_name);
7087 const struct got_error *
7088 got_worktree_histedit_abort(struct got_worktree *worktree,
7089 struct got_fileindex *fileindex, struct got_repository *repo,
7090 struct got_reference *branch, struct got_object_id *base_commit_id,
7091 got_worktree_checkout_cb progress_cb, void *progress_arg)
7093 const struct got_error *err, *unlockerr, *sync_err;
7094 struct got_reference *resolved = NULL;
7095 char *fileindex_path = NULL;
7096 struct got_commit_object *commit = NULL;
7097 struct got_object_id *tree_id = NULL;
7098 struct revert_file_args rfa;
7100 err = lock_worktree(worktree, LOCK_EX);
7104 err = got_object_open_as_commit(&commit, repo,
7105 worktree->base_commit_id);
7109 err = got_ref_open(&resolved, repo,
7110 got_ref_get_symref_target(branch), 0);
7114 err = got_worktree_set_head_ref(worktree, resolved);
7118 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7122 err = got_object_id_by_path(&tree_id, repo, commit,
7123 worktree->path_prefix);
7127 err = delete_histedit_refs(worktree, repo);
7131 err = get_fileindex_path(&fileindex_path, worktree);
7135 rfa.worktree = worktree;
7136 rfa.fileindex = fileindex;
7137 rfa.progress_cb = progress_cb;
7138 rfa.progress_arg = progress_arg;
7139 rfa.patch_cb = NULL;
7140 rfa.patch_arg = NULL;
7142 rfa.unlink_added_files = 0;
7143 err = worktree_status(worktree, "", fileindex, repo,
7144 revert_file, &rfa, NULL, NULL, 1, 0);
7148 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7149 repo, progress_cb, progress_arg, NULL, NULL);
7151 sync_err = sync_fileindex(fileindex, fileindex_path);
7152 if (sync_err && err == NULL)
7155 got_ref_close(resolved);
7157 free(fileindex_path);
7159 unlockerr = lock_worktree(worktree, LOCK_SH);
7160 if (unlockerr && err == NULL)
7165 const struct got_error *
7166 got_worktree_histedit_complete(struct got_worktree *worktree,
7167 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7168 struct got_reference *edited_branch, struct got_repository *repo)
7170 const struct got_error *err, *unlockerr, *sync_err;
7171 struct got_object_id *new_head_commit_id = NULL;
7172 struct got_reference *resolved = NULL;
7173 char *fileindex_path = NULL;
7175 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7179 err = got_ref_open(&resolved, repo,
7180 got_ref_get_symref_target(edited_branch), 0);
7184 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7185 resolved, new_head_commit_id, repo);
7189 err = got_ref_change_ref(resolved, new_head_commit_id);
7193 err = got_ref_write(resolved, repo);
7197 err = got_worktree_set_head_ref(worktree, resolved);
7201 err = delete_histedit_refs(worktree, repo);
7205 err = get_fileindex_path(&fileindex_path, worktree);
7208 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7209 sync_err = sync_fileindex(fileindex, fileindex_path);
7210 if (sync_err && err == NULL)
7213 got_fileindex_free(fileindex);
7214 free(fileindex_path);
7215 free(new_head_commit_id);
7216 unlockerr = lock_worktree(worktree, LOCK_SH);
7217 if (unlockerr && err == NULL)
7222 const struct got_error *
7223 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7224 struct got_object_id *commit_id, struct got_repository *repo)
7226 const struct got_error *err;
7227 char *commit_ref_name;
7229 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7233 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7237 err = delete_ref(commit_ref_name, repo);
7239 free(commit_ref_name);
7243 const struct got_error *
7244 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7245 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7246 struct got_worktree *worktree, const char *refname,
7247 struct got_repository *repo)
7249 const struct got_error *err = NULL;
7250 char *fileindex_path = NULL;
7251 struct check_rebase_ok_arg ok_arg;
7255 *base_branch_ref = NULL;
7257 err = lock_worktree(worktree, LOCK_EX);
7261 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7262 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7263 "cannot integrate a branch into itself; "
7264 "update -b or different branch name required");
7268 err = open_fileindex(fileindex, &fileindex_path, worktree);
7272 /* Preconditions are the same as for rebase. */
7273 ok_arg.worktree = worktree;
7275 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7280 err = got_ref_open(branch_ref, repo, refname, 1);
7284 err = got_ref_open(base_branch_ref, repo,
7285 got_worktree_get_head_ref_name(worktree), 1);
7289 got_ref_close(*branch_ref);
7292 if (*base_branch_ref) {
7293 got_ref_close(*base_branch_ref);
7294 *base_branch_ref = NULL;
7297 got_fileindex_free(*fileindex);
7300 lock_worktree(worktree, LOCK_SH);
7305 const struct got_error *
7306 got_worktree_integrate_continue(struct got_worktree *worktree,
7307 struct got_fileindex *fileindex, struct got_repository *repo,
7308 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7309 got_worktree_checkout_cb progress_cb, void *progress_arg,
7310 got_cancel_cb cancel_cb, void *cancel_arg)
7312 const struct got_error *err = NULL, *sync_err, *unlockerr;
7313 char *fileindex_path = NULL;
7314 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7315 struct got_commit_object *commit = NULL;
7317 err = get_fileindex_path(&fileindex_path, worktree);
7321 err = got_ref_resolve(&commit_id, repo, branch_ref);
7325 err = got_object_open_as_commit(&commit, repo, commit_id);
7329 err = got_object_id_by_path(&tree_id, repo, commit,
7330 worktree->path_prefix);
7334 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7338 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7339 progress_cb, progress_arg, cancel_cb, cancel_arg);
7343 err = got_ref_change_ref(base_branch_ref, commit_id);
7347 err = got_ref_write(base_branch_ref, repo);
7351 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7353 sync_err = sync_fileindex(fileindex, fileindex_path);
7354 if (sync_err && err == NULL)
7358 unlockerr = got_ref_unlock(branch_ref);
7359 if (unlockerr && err == NULL)
7361 got_ref_close(branch_ref);
7363 unlockerr = got_ref_unlock(base_branch_ref);
7364 if (unlockerr && err == NULL)
7366 got_ref_close(base_branch_ref);
7368 got_fileindex_free(fileindex);
7369 free(fileindex_path);
7372 got_object_commit_close(commit);
7374 unlockerr = lock_worktree(worktree, LOCK_SH);
7375 if (unlockerr && err == NULL)
7380 const struct got_error *
7381 got_worktree_integrate_abort(struct got_worktree *worktree,
7382 struct got_fileindex *fileindex, struct got_repository *repo,
7383 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7385 const struct got_error *err = NULL, *unlockerr = NULL;
7387 got_fileindex_free(fileindex);
7389 err = lock_worktree(worktree, LOCK_SH);
7391 unlockerr = got_ref_unlock(branch_ref);
7392 if (unlockerr && err == NULL)
7394 got_ref_close(branch_ref);
7396 unlockerr = got_ref_unlock(base_branch_ref);
7397 if (unlockerr && err == NULL)
7399 got_ref_close(base_branch_ref);
7404 const struct got_error *
7405 got_worktree_merge_postpone(struct got_worktree *worktree,
7406 struct got_fileindex *fileindex)
7408 const struct got_error *err, *sync_err;
7409 char *fileindex_path = NULL;
7411 err = get_fileindex_path(&fileindex_path, worktree);
7415 sync_err = sync_fileindex(fileindex, fileindex_path);
7417 err = lock_worktree(worktree, LOCK_SH);
7418 if (sync_err && err == NULL)
7421 got_fileindex_free(fileindex);
7422 free(fileindex_path);
7426 static const struct got_error *
7427 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7429 const struct got_error *err;
7430 char *branch_refname = NULL, *commit_refname = NULL;
7432 err = get_merge_branch_ref_name(&branch_refname, worktree);
7435 err = delete_ref(branch_refname, repo);
7439 err = get_merge_commit_ref_name(&commit_refname, worktree);
7442 err = delete_ref(commit_refname, repo);
7447 free(branch_refname);
7448 free(commit_refname);
7452 struct merge_commit_msg_arg {
7453 struct got_worktree *worktree;
7454 const char *branch_name;
7457 static const struct got_error *
7458 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7461 struct merge_commit_msg_arg *a = arg;
7463 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7464 got_worktree_get_head_ref_name(a->worktree)) == -1)
7465 return got_error_from_errno("asprintf");
7471 const struct got_error *
7472 got_worktree_merge_branch(struct got_worktree *worktree,
7473 struct got_fileindex *fileindex,
7474 struct got_object_id *yca_commit_id,
7475 struct got_object_id *branch_tip,
7476 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7477 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7479 const struct got_error *err;
7480 char *fileindex_path = NULL;
7482 err = get_fileindex_path(&fileindex_path, worktree);
7486 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7491 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7492 branch_tip, repo, progress_cb, progress_arg,
7493 cancel_cb, cancel_arg);
7495 free(fileindex_path);
7499 const struct got_error *
7500 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7501 struct got_worktree *worktree, struct got_fileindex *fileindex,
7502 const char *author, const char *committer, int allow_bad_symlinks,
7503 struct got_object_id *branch_tip, const char *branch_name,
7504 struct got_repository *repo,
7505 got_worktree_status_cb status_cb, void *status_arg)
7508 const struct got_error *err = NULL, *sync_err;
7509 struct got_pathlist_head commitable_paths;
7510 struct collect_commitables_arg cc_arg;
7511 struct got_pathlist_entry *pe;
7512 struct got_reference *head_ref = NULL;
7513 struct got_object_id *head_commit_id = NULL;
7514 int have_staged_files = 0;
7515 struct merge_commit_msg_arg mcm_arg;
7516 char *fileindex_path = NULL;
7518 *new_commit_id = NULL;
7520 TAILQ_INIT(&commitable_paths);
7522 err = get_fileindex_path(&fileindex_path, worktree);
7526 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7530 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7534 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7535 &have_staged_files);
7536 if (err && err->code != GOT_ERR_CANCELLED)
7538 if (have_staged_files) {
7539 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7543 cc_arg.commitable_paths = &commitable_paths;
7544 cc_arg.worktree = worktree;
7545 cc_arg.fileindex = fileindex;
7547 cc_arg.have_staged_files = have_staged_files;
7548 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7549 err = worktree_status(worktree, "", fileindex, repo,
7550 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7554 if (TAILQ_EMPTY(&commitable_paths)) {
7555 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7556 "merge of %s cannot proceed", branch_name);
7560 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7561 struct got_commitable *ct = pe->data;
7562 const char *ct_path = ct->in_repo_path;
7564 while (ct_path[0] == '/')
7566 err = check_out_of_date(ct_path, ct->status,
7567 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7568 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7574 mcm_arg.worktree = worktree;
7575 mcm_arg.branch_name = branch_name;
7576 err = commit_worktree(new_commit_id, &commitable_paths,
7577 head_commit_id, branch_tip, worktree, author, committer,
7578 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7582 err = update_fileindex_after_commit(worktree, &commitable_paths,
7583 *new_commit_id, fileindex, have_staged_files);
7584 sync_err = sync_fileindex(fileindex, fileindex_path);
7585 if (sync_err && err == NULL)
7588 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7589 struct got_commitable *ct = pe->data;
7590 free_commitable(ct);
7592 got_pathlist_free(&commitable_paths);
7593 free(fileindex_path);
7597 const struct got_error *
7598 got_worktree_merge_complete(struct got_worktree *worktree,
7599 struct got_fileindex *fileindex, struct got_repository *repo)
7601 const struct got_error *err, *unlockerr, *sync_err;
7602 char *fileindex_path = NULL;
7604 err = delete_merge_refs(worktree, repo);
7608 err = get_fileindex_path(&fileindex_path, worktree);
7611 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7612 sync_err = sync_fileindex(fileindex, fileindex_path);
7613 if (sync_err && err == NULL)
7616 got_fileindex_free(fileindex);
7617 free(fileindex_path);
7618 unlockerr = lock_worktree(worktree, LOCK_SH);
7619 if (unlockerr && err == NULL)
7624 const struct got_error *
7625 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7626 struct got_repository *repo)
7628 const struct got_error *err;
7629 char *branch_refname = NULL;
7630 struct got_reference *branch_ref = NULL;
7634 err = get_merge_branch_ref_name(&branch_refname, worktree);
7637 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7638 free(branch_refname);
7640 if (err->code != GOT_ERR_NOT_REF)
7648 const struct got_error *got_worktree_merge_prepare(
7649 struct got_fileindex **fileindex, struct got_worktree *worktree,
7650 struct got_reference *branch, struct got_repository *repo)
7652 const struct got_error *err = NULL;
7653 char *fileindex_path = NULL;
7654 char *branch_refname = NULL, *commit_refname = NULL;
7655 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7656 struct got_reference *commit_ref = NULL;
7657 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7658 struct check_rebase_ok_arg ok_arg;
7662 err = lock_worktree(worktree, LOCK_EX);
7666 err = open_fileindex(fileindex, &fileindex_path, worktree);
7670 /* Preconditions are the same as for rebase. */
7671 ok_arg.worktree = worktree;
7673 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7678 err = get_merge_branch_ref_name(&branch_refname, worktree);
7682 err = get_merge_commit_ref_name(&commit_refname, worktree);
7686 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7691 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7695 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7696 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7700 err = got_ref_resolve(&branch_tip, repo, branch);
7704 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7707 err = got_ref_write(branch_ref, repo);
7711 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7714 err = got_ref_write(commit_ref, repo);
7719 free(branch_refname);
7720 free(commit_refname);
7721 free(fileindex_path);
7723 got_ref_close(branch_ref);
7725 got_ref_close(commit_ref);
7727 got_ref_close(wt_branch);
7728 free(wt_branch_tip);
7731 got_fileindex_free(*fileindex);
7734 lock_worktree(worktree, LOCK_SH);
7739 const struct got_error *
7740 got_worktree_merge_continue(char **branch_name,
7741 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7742 struct got_worktree *worktree, struct got_repository *repo)
7744 const struct got_error *err;
7745 char *commit_refname = NULL, *branch_refname = NULL;
7746 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7747 char *fileindex_path = NULL;
7748 int have_staged_files = 0;
7750 *branch_name = NULL;
7754 err = lock_worktree(worktree, LOCK_EX);
7758 err = open_fileindex(fileindex, &fileindex_path, worktree);
7762 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7763 &have_staged_files);
7764 if (err && err->code != GOT_ERR_CANCELLED)
7766 if (have_staged_files) {
7767 err = got_error(GOT_ERR_STAGED_PATHS);
7771 err = get_merge_branch_ref_name(&branch_refname, worktree);
7775 err = get_merge_commit_ref_name(&commit_refname, worktree);
7779 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7783 if (!got_ref_is_symbolic(branch_ref)) {
7784 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7785 "%s is not a symbolic reference",
7786 got_ref_get_name(branch_ref));
7789 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7790 if (*branch_name == NULL) {
7791 err = got_error_from_errno("strdup");
7795 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7799 err = got_ref_resolve(branch_tip, repo, commit_ref);
7803 free(commit_refname);
7804 free(branch_refname);
7805 free(fileindex_path);
7807 got_ref_close(commit_ref);
7809 got_ref_close(branch_ref);
7813 *branch_name = NULL;
7818 got_fileindex_free(*fileindex);
7821 lock_worktree(worktree, LOCK_SH);
7826 const struct got_error *
7827 got_worktree_merge_abort(struct got_worktree *worktree,
7828 struct got_fileindex *fileindex, struct got_repository *repo,
7829 got_worktree_checkout_cb progress_cb, void *progress_arg)
7831 const struct got_error *err, *unlockerr, *sync_err;
7832 struct got_object_id *commit_id = NULL;
7833 struct got_commit_object *commit = NULL;
7834 char *fileindex_path = NULL;
7835 struct revert_file_args rfa;
7836 struct got_object_id *tree_id = NULL;
7838 err = got_object_open_as_commit(&commit, repo,
7839 worktree->base_commit_id);
7843 err = got_object_id_by_path(&tree_id, repo, commit,
7844 worktree->path_prefix);
7848 err = delete_merge_refs(worktree, repo);
7852 err = get_fileindex_path(&fileindex_path, worktree);
7856 rfa.worktree = worktree;
7857 rfa.fileindex = fileindex;
7858 rfa.progress_cb = progress_cb;
7859 rfa.progress_arg = progress_arg;
7860 rfa.patch_cb = NULL;
7861 rfa.patch_arg = NULL;
7863 rfa.unlink_added_files = 1;
7864 err = worktree_status(worktree, "", fileindex, repo,
7865 revert_file, &rfa, NULL, NULL, 1, 0);
7869 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7870 repo, progress_cb, progress_arg, NULL, NULL);
7872 sync_err = sync_fileindex(fileindex, fileindex_path);
7873 if (sync_err && err == NULL)
7879 got_object_commit_close(commit);
7881 got_fileindex_free(fileindex);
7882 free(fileindex_path);
7884 unlockerr = lock_worktree(worktree, LOCK_SH);
7885 if (unlockerr && err == NULL)
7890 struct check_stage_ok_arg {
7891 struct got_object_id *head_commit_id;
7892 struct got_worktree *worktree;
7893 struct got_fileindex *fileindex;
7894 struct got_repository *repo;
7898 const struct got_error *
7899 check_stage_ok(void *arg, unsigned char status,
7900 unsigned char staged_status, const char *relpath,
7901 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7902 struct got_object_id *commit_id, int dirfd, const char *de_name)
7904 struct check_stage_ok_arg *a = arg;
7905 const struct got_error *err = NULL;
7906 struct got_fileindex_entry *ie;
7907 struct got_object_id base_commit_id;
7908 struct got_object_id *base_commit_idp = NULL;
7909 char *in_repo_path = NULL, *p;
7911 if (status == GOT_STATUS_UNVERSIONED ||
7912 status == GOT_STATUS_NO_CHANGE)
7914 if (status == GOT_STATUS_NONEXISTENT)
7915 return got_error_set_errno(ENOENT, relpath);
7917 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7919 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7921 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7922 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7924 return got_error_from_errno("asprintf");
7926 if (got_fileindex_entry_has_commit(ie)) {
7927 memcpy(base_commit_id.sha1, ie->commit_sha1,
7928 SHA1_DIGEST_LENGTH);
7929 base_commit_idp = &base_commit_id;
7932 if (status == GOT_STATUS_CONFLICT) {
7933 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7935 } else if (status != GOT_STATUS_ADD &&
7936 status != GOT_STATUS_MODIFY &&
7937 status != GOT_STATUS_DELETE) {
7938 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7942 a->have_changes = 1;
7947 err = check_out_of_date(p, status, staged_status,
7948 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7949 GOT_ERR_STAGE_OUT_OF_DATE);
7955 struct stage_path_arg {
7956 struct got_worktree *worktree;
7957 struct got_fileindex *fileindex;
7958 struct got_repository *repo;
7959 got_worktree_status_cb status_cb;
7961 got_worktree_patch_cb patch_cb;
7963 int staged_something;
7964 int allow_bad_symlinks;
7967 static const struct got_error *
7968 stage_path(void *arg, unsigned char status,
7969 unsigned char staged_status, const char *relpath,
7970 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7971 struct got_object_id *commit_id, int dirfd, const char *de_name)
7973 struct stage_path_arg *a = arg;
7974 const struct got_error *err = NULL;
7975 struct got_fileindex_entry *ie;
7976 char *ondisk_path = NULL, *path_content = NULL;
7978 struct got_object_id *new_staged_blob_id = NULL;
7981 if (status == GOT_STATUS_UNVERSIONED)
7984 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7986 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7988 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7990 return got_error_from_errno("asprintf");
7993 case GOT_STATUS_ADD:
7994 case GOT_STATUS_MODIFY:
7995 /* XXX could sb.st_mode be passed in by our caller? */
7996 if (lstat(ondisk_path, &sb) == -1) {
7997 err = got_error_from_errno2("lstat", ondisk_path);
8001 if (status == GOT_STATUS_ADD) {
8002 int choice = GOT_PATCH_CHOICE_NONE;
8003 err = (*a->patch_cb)(&choice, a->patch_arg,
8004 status, ie->path, NULL, 1, 1);
8007 if (choice != GOT_PATCH_CHOICE_YES)
8010 err = create_patched_content(&path_content, 0,
8011 staged_blob_id ? staged_blob_id : blob_id,
8012 ondisk_path, dirfd, de_name, ie->path,
8013 a->repo, a->patch_cb, a->patch_arg);
8014 if (err || path_content == NULL)
8018 err = got_object_blob_create(&new_staged_blob_id,
8019 path_content ? path_content : ondisk_path, a->repo);
8022 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8023 SHA1_DIGEST_LENGTH);
8024 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8025 stage = GOT_FILEIDX_STAGE_ADD;
8027 stage = GOT_FILEIDX_STAGE_MODIFY;
8028 got_fileindex_entry_stage_set(ie, stage);
8029 if (S_ISLNK(sb.st_mode)) {
8030 int is_bad_symlink = 0;
8031 if (!a->allow_bad_symlinks) {
8032 char target_path[PATH_MAX];
8034 target_len = readlink(ondisk_path, target_path,
8035 sizeof(target_path));
8036 if (target_len == -1) {
8037 err = got_error_from_errno2("readlink",
8041 err = is_bad_symlink_target(&is_bad_symlink,
8042 target_path, target_len, ondisk_path,
8043 a->worktree->root_path);
8046 if (is_bad_symlink) {
8047 err = got_error_path(ondisk_path,
8048 GOT_ERR_BAD_SYMLINK);
8053 got_fileindex_entry_staged_filetype_set(ie,
8054 GOT_FILEIDX_MODE_BAD_SYMLINK);
8056 got_fileindex_entry_staged_filetype_set(ie,
8057 GOT_FILEIDX_MODE_SYMLINK);
8059 got_fileindex_entry_staged_filetype_set(ie,
8060 GOT_FILEIDX_MODE_REGULAR_FILE);
8062 a->staged_something = 1;
8063 if (a->status_cb == NULL)
8065 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8066 get_staged_status(ie), relpath, blob_id,
8067 new_staged_blob_id, NULL, dirfd, de_name);
8069 case GOT_STATUS_DELETE:
8070 if (staged_status == GOT_STATUS_DELETE)
8073 int choice = GOT_PATCH_CHOICE_NONE;
8074 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8075 ie->path, NULL, 1, 1);
8078 if (choice == GOT_PATCH_CHOICE_NO)
8080 if (choice != GOT_PATCH_CHOICE_YES) {
8081 err = got_error(GOT_ERR_PATCH_CHOICE);
8085 stage = GOT_FILEIDX_STAGE_DELETE;
8086 got_fileindex_entry_stage_set(ie, stage);
8087 a->staged_something = 1;
8088 if (a->status_cb == NULL)
8090 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8091 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8094 case GOT_STATUS_NO_CHANGE:
8096 case GOT_STATUS_CONFLICT:
8097 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8099 case GOT_STATUS_NONEXISTENT:
8100 err = got_error_set_errno(ENOENT, relpath);
8103 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8107 if (path_content && unlink(path_content) == -1 && err == NULL)
8108 err = got_error_from_errno2("unlink", path_content);
8111 free(new_staged_blob_id);
8115 const struct got_error *
8116 got_worktree_stage(struct got_worktree *worktree,
8117 struct got_pathlist_head *paths,
8118 got_worktree_status_cb status_cb, void *status_arg,
8119 got_worktree_patch_cb patch_cb, void *patch_arg,
8120 int allow_bad_symlinks, struct got_repository *repo)
8122 const struct got_error *err = NULL, *sync_err, *unlockerr;
8123 struct got_pathlist_entry *pe;
8124 struct got_fileindex *fileindex = NULL;
8125 char *fileindex_path = NULL;
8126 struct got_reference *head_ref = NULL;
8127 struct got_object_id *head_commit_id = NULL;
8128 struct check_stage_ok_arg oka;
8129 struct stage_path_arg spa;
8131 err = lock_worktree(worktree, LOCK_EX);
8135 err = got_ref_open(&head_ref, repo,
8136 got_worktree_get_head_ref_name(worktree), 0);
8139 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8142 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8146 /* Check pre-conditions before staging anything. */
8147 oka.head_commit_id = head_commit_id;
8148 oka.worktree = worktree;
8149 oka.fileindex = fileindex;
8151 oka.have_changes = 0;
8152 TAILQ_FOREACH(pe, paths, entry) {
8153 err = worktree_status(worktree, pe->path, fileindex, repo,
8154 check_stage_ok, &oka, NULL, NULL, 1, 0);
8158 if (!oka.have_changes) {
8159 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8163 spa.worktree = worktree;
8164 spa.fileindex = fileindex;
8166 spa.patch_cb = patch_cb;
8167 spa.patch_arg = patch_arg;
8168 spa.status_cb = status_cb;
8169 spa.status_arg = status_arg;
8170 spa.staged_something = 0;
8171 spa.allow_bad_symlinks = allow_bad_symlinks;
8172 TAILQ_FOREACH(pe, paths, entry) {
8173 err = worktree_status(worktree, pe->path, fileindex, repo,
8174 stage_path, &spa, NULL, NULL, 1, 0);
8178 if (!spa.staged_something) {
8179 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8183 sync_err = sync_fileindex(fileindex, fileindex_path);
8184 if (sync_err && err == NULL)
8188 got_ref_close(head_ref);
8189 free(head_commit_id);
8190 free(fileindex_path);
8192 got_fileindex_free(fileindex);
8193 unlockerr = lock_worktree(worktree, LOCK_SH);
8194 if (unlockerr && err == NULL)
8199 struct unstage_path_arg {
8200 struct got_worktree *worktree;
8201 struct got_fileindex *fileindex;
8202 struct got_repository *repo;
8203 got_worktree_checkout_cb progress_cb;
8205 got_worktree_patch_cb patch_cb;
8209 static const struct got_error *
8210 create_unstaged_content(char **path_unstaged_content,
8211 char **path_new_staged_content, struct got_object_id *blob_id,
8212 struct got_object_id *staged_blob_id, const char *relpath,
8213 struct got_repository *repo,
8214 got_worktree_patch_cb patch_cb, void *patch_arg)
8216 const struct got_error *err, *free_err;
8217 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8218 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8219 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8220 struct got_diffreg_result *diffreg_result = NULL;
8221 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8222 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8224 *path_unstaged_content = NULL;
8225 *path_new_staged_content = NULL;
8227 err = got_object_id_str(&label1, blob_id);
8230 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8234 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8238 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8242 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8246 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8250 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8254 err = got_diff_files(&diffreg_result, f1, label1, f2,
8255 path2, 3, 0, 1, NULL);
8259 err = got_opentemp_named(path_unstaged_content, &outfile,
8260 "got-unstaged-content");
8263 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8264 "got-new-staged-content");
8268 if (fseek(f1, 0L, SEEK_SET) == -1) {
8269 err = got_ferror(f1, GOT_ERR_IO);
8272 if (fseek(f2, 0L, SEEK_SET) == -1) {
8273 err = got_ferror(f2, GOT_ERR_IO);
8276 /* Count the number of actual changes in the diff result. */
8277 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8278 struct diff_chunk_context cc = {};
8279 diff_chunk_context_load_change(&cc, &nchunks_used,
8280 diffreg_result->result, n, 0);
8283 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8285 err = apply_or_reject_change(&choice, &nchunks_used,
8286 diffreg_result->result, n, relpath, f1, f2,
8287 &line_cur1, &line_cur2,
8288 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8291 if (choice == GOT_PATCH_CHOICE_YES)
8294 have_rejected_content = 1;
8295 if (choice == GOT_PATCH_CHOICE_QUIT)
8298 if (have_content || have_rejected_content)
8299 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8300 outfile, rejectfile);
8304 got_object_blob_close(blob);
8306 got_object_blob_close(staged_blob);
8307 free_err = got_diffreg_result_free(diffreg_result);
8308 if (free_err && err == NULL)
8310 if (f1 && fclose(f1) == EOF && err == NULL)
8311 err = got_error_from_errno2("fclose", path1);
8312 if (f2 && fclose(f2) == EOF && err == NULL)
8313 err = got_error_from_errno2("fclose", path2);
8314 if (outfile && fclose(outfile) == EOF && err == NULL)
8315 err = got_error_from_errno2("fclose", *path_unstaged_content);
8316 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8317 err = got_error_from_errno2("fclose", *path_new_staged_content);
8318 if (path1 && unlink(path1) == -1 && err == NULL)
8319 err = got_error_from_errno2("unlink", path1);
8320 if (path2 && unlink(path2) == -1 && err == NULL)
8321 err = got_error_from_errno2("unlink", path2);
8322 if (err || !have_content) {
8323 if (*path_unstaged_content &&
8324 unlink(*path_unstaged_content) == -1 && err == NULL)
8325 err = got_error_from_errno2("unlink",
8326 *path_unstaged_content);
8327 free(*path_unstaged_content);
8328 *path_unstaged_content = NULL;
8330 if (err || !have_content || !have_rejected_content) {
8331 if (*path_new_staged_content &&
8332 unlink(*path_new_staged_content) == -1 && err == NULL)
8333 err = got_error_from_errno2("unlink",
8334 *path_new_staged_content);
8335 free(*path_new_staged_content);
8336 *path_new_staged_content = NULL;
8343 static const struct got_error *
8344 unstage_hunks(struct got_object_id *staged_blob_id,
8345 struct got_blob_object *blob_base,
8346 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8347 const char *ondisk_path, const char *label_orig,
8348 struct got_worktree *worktree, struct got_repository *repo,
8349 got_worktree_patch_cb patch_cb, void *patch_arg,
8350 got_worktree_checkout_cb progress_cb, void *progress_arg)
8352 const struct got_error *err = NULL;
8353 char *path_unstaged_content = NULL;
8354 char *path_new_staged_content = NULL;
8355 char *parent = NULL, *base_path = NULL;
8356 char *blob_base_path = NULL;
8357 struct got_object_id *new_staged_blob_id = NULL;
8358 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8361 err = create_unstaged_content(&path_unstaged_content,
8362 &path_new_staged_content, blob_id, staged_blob_id,
8363 ie->path, repo, patch_cb, patch_arg);
8367 if (path_unstaged_content == NULL)
8370 if (path_new_staged_content) {
8371 err = got_object_blob_create(&new_staged_blob_id,
8372 path_new_staged_content, repo);
8377 f = fopen(path_unstaged_content, "re");
8379 err = got_error_from_errno2("fopen",
8380 path_unstaged_content);
8383 if (fstat(fileno(f), &sb) == -1) {
8384 err = got_error_from_errno2("fstat", path_unstaged_content);
8387 if (got_fileindex_entry_staged_filetype_get(ie) ==
8388 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8389 char link_target[PATH_MAX];
8391 r = fread(link_target, 1, sizeof(link_target), f);
8392 if (r == 0 && ferror(f)) {
8393 err = got_error_from_errno("fread");
8396 if (r >= sizeof(link_target)) { /* should not happen */
8397 err = got_error(GOT_ERR_NO_SPACE);
8400 link_target[r] = '\0';
8401 err = merge_symlink(worktree, blob_base,
8402 ondisk_path, ie->path, label_orig, link_target,
8403 worktree->base_commit_id, repo, progress_cb,
8406 int local_changes_subsumed;
8408 err = got_path_dirname(&parent, ondisk_path);
8412 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8414 err = got_error_from_errno("asprintf");
8419 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8422 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8428 * In order the run a 3-way merge with a symlink we copy the symlink's
8429 * target path into a temporary file and use that file with diff3.
8431 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8432 err = dump_symlink_target_path_to_file(&f_deriv2,
8438 fd = open(ondisk_path,
8439 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8441 err = got_error_from_errno2("open", ondisk_path);
8444 f_deriv2 = fdopen(fd, "r");
8445 if (f_deriv2 == NULL) {
8446 err = got_error_from_errno2("fdopen", ondisk_path);
8452 err = merge_file(&local_changes_subsumed, worktree,
8453 f_base, f, f_deriv2, ondisk_path, ie->path,
8454 got_fileindex_perms_to_st(ie),
8455 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8456 repo, progress_cb, progress_arg);
8461 if (new_staged_blob_id) {
8462 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8463 SHA1_DIGEST_LENGTH);
8465 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8466 got_fileindex_entry_staged_filetype_set(ie, 0);
8469 free(new_staged_blob_id);
8470 if (path_unstaged_content &&
8471 unlink(path_unstaged_content) == -1 && err == NULL)
8472 err = got_error_from_errno2("unlink", path_unstaged_content);
8473 if (path_new_staged_content &&
8474 unlink(path_new_staged_content) == -1 && err == NULL)
8475 err = got_error_from_errno2("unlink", path_new_staged_content);
8476 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8477 err = got_error_from_errno2("unlink", blob_base_path);
8478 if (f_base && fclose(f_base) == EOF && err == NULL)
8479 err = got_error_from_errno2("fclose", path_unstaged_content);
8480 if (f && fclose(f) == EOF && err == NULL)
8481 err = got_error_from_errno2("fclose", path_unstaged_content);
8482 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8483 err = got_error_from_errno2("fclose", ondisk_path);
8484 free(path_unstaged_content);
8485 free(path_new_staged_content);
8486 free(blob_base_path);
8492 static const struct got_error *
8493 unstage_path(void *arg, unsigned char status,
8494 unsigned char staged_status, const char *relpath,
8495 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8496 struct got_object_id *commit_id, int dirfd, const char *de_name)
8498 const struct got_error *err = NULL;
8499 struct unstage_path_arg *a = arg;
8500 struct got_fileindex_entry *ie;
8501 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8502 char *ondisk_path = NULL;
8503 char *id_str = NULL, *label_orig = NULL;
8504 int local_changes_subsumed;
8507 if (staged_status != GOT_STATUS_ADD &&
8508 staged_status != GOT_STATUS_MODIFY &&
8509 staged_status != GOT_STATUS_DELETE)
8512 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8514 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8516 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8518 return got_error_from_errno("asprintf");
8520 err = got_object_id_str(&id_str,
8521 commit_id ? commit_id : a->worktree->base_commit_id);
8524 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8526 err = got_error_from_errno("asprintf");
8530 switch (staged_status) {
8531 case GOT_STATUS_MODIFY:
8532 err = got_object_open_as_blob(&blob_base, a->repo,
8537 case GOT_STATUS_ADD:
8539 if (staged_status == GOT_STATUS_ADD) {
8540 int choice = GOT_PATCH_CHOICE_NONE;
8541 err = (*a->patch_cb)(&choice, a->patch_arg,
8542 staged_status, ie->path, NULL, 1, 1);
8545 if (choice != GOT_PATCH_CHOICE_YES)
8548 err = unstage_hunks(staged_blob_id,
8549 blob_base, blob_id, ie, ondisk_path,
8550 label_orig, a->worktree, a->repo,
8551 a->patch_cb, a->patch_arg,
8552 a->progress_cb, a->progress_arg);
8553 break; /* Done with this file. */
8556 err = got_object_open_as_blob(&blob_staged, a->repo,
8557 staged_blob_id, 8192);
8560 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8561 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8562 case GOT_FILEIDX_MODE_REGULAR_FILE:
8563 err = merge_blob(&local_changes_subsumed, a->worktree,
8564 blob_base, ondisk_path, relpath,
8565 got_fileindex_perms_to_st(ie), label_orig,
8566 blob_staged, commit_id ? commit_id :
8567 a->worktree->base_commit_id, a->repo,
8568 a->progress_cb, a->progress_arg);
8570 case GOT_FILEIDX_MODE_SYMLINK:
8571 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8572 char *staged_target;
8573 err = got_object_blob_read_to_str(
8574 &staged_target, blob_staged);
8577 err = merge_symlink(a->worktree, blob_base,
8578 ondisk_path, relpath, label_orig,
8579 staged_target, commit_id ? commit_id :
8580 a->worktree->base_commit_id,
8581 a->repo, a->progress_cb, a->progress_arg);
8582 free(staged_target);
8584 err = merge_blob(&local_changes_subsumed,
8585 a->worktree, blob_base, ondisk_path,
8586 relpath, got_fileindex_perms_to_st(ie),
8587 label_orig, blob_staged,
8588 commit_id ? commit_id :
8589 a->worktree->base_commit_id, a->repo,
8590 a->progress_cb, a->progress_arg);
8594 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8598 got_fileindex_entry_stage_set(ie,
8599 GOT_FILEIDX_STAGE_NONE);
8600 got_fileindex_entry_staged_filetype_set(ie, 0);
8603 case GOT_STATUS_DELETE:
8605 int choice = GOT_PATCH_CHOICE_NONE;
8606 err = (*a->patch_cb)(&choice, a->patch_arg,
8607 staged_status, ie->path, NULL, 1, 1);
8610 if (choice == GOT_PATCH_CHOICE_NO)
8612 if (choice != GOT_PATCH_CHOICE_YES) {
8613 err = got_error(GOT_ERR_PATCH_CHOICE);
8617 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8618 got_fileindex_entry_staged_filetype_set(ie, 0);
8619 err = get_file_status(&status, &sb, ie, ondisk_path,
8620 dirfd, de_name, a->repo);
8623 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8629 got_object_blob_close(blob_base);
8631 got_object_blob_close(blob_staged);
8637 const struct got_error *
8638 got_worktree_unstage(struct got_worktree *worktree,
8639 struct got_pathlist_head *paths,
8640 got_worktree_checkout_cb progress_cb, void *progress_arg,
8641 got_worktree_patch_cb patch_cb, void *patch_arg,
8642 struct got_repository *repo)
8644 const struct got_error *err = NULL, *sync_err, *unlockerr;
8645 struct got_pathlist_entry *pe;
8646 struct got_fileindex *fileindex = NULL;
8647 char *fileindex_path = NULL;
8648 struct unstage_path_arg upa;
8650 err = lock_worktree(worktree, LOCK_EX);
8654 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8658 upa.worktree = worktree;
8659 upa.fileindex = fileindex;
8661 upa.progress_cb = progress_cb;
8662 upa.progress_arg = progress_arg;
8663 upa.patch_cb = patch_cb;
8664 upa.patch_arg = patch_arg;
8665 TAILQ_FOREACH(pe, paths, entry) {
8666 err = worktree_status(worktree, pe->path, fileindex, repo,
8667 unstage_path, &upa, NULL, NULL, 1, 0);
8672 sync_err = sync_fileindex(fileindex, fileindex_path);
8673 if (sync_err && err == NULL)
8676 free(fileindex_path);
8678 got_fileindex_free(fileindex);
8679 unlockerr = lock_worktree(worktree, LOCK_SH);
8680 if (unlockerr && err == NULL)
8685 struct report_file_info_arg {
8686 struct got_worktree *worktree;
8687 got_worktree_path_info_cb info_cb;
8689 struct got_pathlist_head *paths;
8690 got_cancel_cb cancel_cb;
8694 static const struct got_error *
8695 report_file_info(void *arg, struct got_fileindex_entry *ie)
8697 struct report_file_info_arg *a = arg;
8698 struct got_pathlist_entry *pe;
8699 struct got_object_id blob_id, staged_blob_id, commit_id;
8700 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8701 struct got_object_id *commit_idp = NULL;
8704 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8705 return got_error(GOT_ERR_CANCELLED);
8707 TAILQ_FOREACH(pe, a->paths, entry) {
8708 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8709 got_path_is_child(ie->path, pe->path, pe->path_len))
8712 if (pe == NULL) /* not found */
8715 if (got_fileindex_entry_has_blob(ie)) {
8716 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8717 blob_idp = &blob_id;
8719 stage = got_fileindex_entry_stage_get(ie);
8720 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8721 stage == GOT_FILEIDX_STAGE_ADD) {
8722 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8723 SHA1_DIGEST_LENGTH);
8724 staged_blob_idp = &staged_blob_id;
8727 if (got_fileindex_entry_has_commit(ie)) {
8728 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8729 commit_idp = &commit_id;
8732 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8733 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8736 const struct got_error *
8737 got_worktree_path_info(struct got_worktree *worktree,
8738 struct got_pathlist_head *paths,
8739 got_worktree_path_info_cb info_cb, void *info_arg,
8740 got_cancel_cb cancel_cb, void *cancel_arg)
8743 const struct got_error *err = NULL, *unlockerr;
8744 struct got_fileindex *fileindex = NULL;
8745 char *fileindex_path = NULL;
8746 struct report_file_info_arg arg;
8748 err = lock_worktree(worktree, LOCK_SH);
8752 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8756 arg.worktree = worktree;
8757 arg.info_cb = info_cb;
8758 arg.info_arg = info_arg;
8760 arg.cancel_cb = cancel_cb;
8761 arg.cancel_arg = cancel_arg;
8762 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8765 free(fileindex_path);
8767 got_fileindex_free(fileindex);
8768 unlockerr = lock_worktree(worktree, LOCK_UN);
8769 if (unlockerr && err == NULL)
8774 static const struct got_error *
8775 patch_check_path(const char *p, char **path, unsigned char *status,
8776 unsigned char *staged_status, struct got_fileindex *fileindex,
8777 struct got_worktree *worktree, struct got_repository *repo)
8779 const struct got_error *err;
8780 struct got_fileindex_entry *ie;
8782 char *ondisk_path = NULL;
8784 err = got_worktree_resolve_path(path, worktree, p);
8788 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8789 *path[0] ? "/" : "", *path) == -1)
8790 return got_error_from_errno("asprintf");
8792 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8794 *staged_status = get_staged_status(ie);
8795 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8800 *staged_status = GOT_STATUS_NO_CHANGE;
8801 *status = GOT_STATUS_UNVERSIONED;
8802 if (lstat(ondisk_path, &sb) == -1) {
8803 if (errno != ENOENT) {
8804 err = got_error_from_errno2("lstat",
8808 *status = GOT_STATUS_NONEXISTENT;
8817 static const struct got_error *
8818 patch_can_rm(const char *path, unsigned char status,
8819 unsigned char staged_status)
8821 if (status == GOT_STATUS_NONEXISTENT)
8822 return got_error_set_errno(ENOENT, path);
8823 if (status != GOT_STATUS_NO_CHANGE &&
8824 status != GOT_STATUS_ADD &&
8825 status != GOT_STATUS_MODIFY &&
8826 status != GOT_STATUS_MODE_CHANGE)
8827 return got_error_path(path, GOT_ERR_FILE_STATUS);
8828 if (staged_status == GOT_STATUS_DELETE)
8829 return got_error_path(path, GOT_ERR_FILE_STATUS);
8833 static const struct got_error *
8834 patch_can_add(const char *path, unsigned char status)
8836 if (status != GOT_STATUS_NONEXISTENT)
8837 return got_error_path(path, GOT_ERR_FILE_STATUS);
8841 static const struct got_error *
8842 patch_can_edit(const char *path, unsigned char status,
8843 unsigned char staged_status)
8845 if (status == GOT_STATUS_NONEXISTENT)
8846 return got_error_set_errno(ENOENT, path);
8847 if (status != GOT_STATUS_NO_CHANGE &&
8848 status != GOT_STATUS_ADD &&
8849 status != GOT_STATUS_MODIFY)
8850 return got_error_path(path, GOT_ERR_FILE_STATUS);
8851 if (staged_status == GOT_STATUS_DELETE)
8852 return got_error_path(path, GOT_ERR_FILE_STATUS);
8856 const struct got_error *
8857 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8858 struct got_worktree *worktree)
8860 const struct got_error *err;
8861 char *fileindex_path = NULL;
8863 err = open_fileindex(fileindex, &fileindex_path, worktree);
8864 free(fileindex_path);
8868 const struct got_error *
8869 got_worktree_patch_check_path(const char *old, const char *new,
8870 char **oldpath, char **newpath, struct got_worktree *worktree,
8871 struct got_repository *repo, struct got_fileindex *fileindex)
8873 const struct got_error *err = NULL;
8874 int file_renamed = 0;
8875 unsigned char status_old, staged_status_old;
8876 unsigned char status_new, staged_status_new;
8881 err = patch_check_path(old != NULL ? old : new, oldpath,
8882 &status_old, &staged_status_old, fileindex, worktree, repo);
8886 err = patch_check_path(new != NULL ? new : old, newpath,
8887 &status_new, &staged_status_new, fileindex, worktree, repo);
8891 if (old != NULL && new != NULL && strcmp(old, new) != 0)
8894 if (old != NULL && new == NULL)
8895 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8896 else if (file_renamed) {
8897 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8899 err = patch_can_add(*newpath, status_new);
8900 } else if (old == NULL)
8901 err = patch_can_add(*newpath, status_new);
8903 err = patch_can_edit(*newpath, status_new, staged_status_new);
8915 const struct got_error *
8916 got_worktree_patch_complete(struct got_fileindex *fileindex)
8918 got_fileindex_free(fileindex);