2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t apply_umask(mode_t);
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
129 return got_error_from_errno("got_ref_to_str");
131 refstr = strdup(got_ref_get_name(head_ref));
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
147 uint32_t uuid_status;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
160 err = got_ref_resolve(&commit_id, repo, head_ref);
163 err = got_object_get_type(&obj_type, repo, commit_id);
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
301 err = write_head_ref(path_got, head_ref);
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
336 err = got_object_open(&obj, repo, commit_id);
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
361 got_object_close(obj);
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
413 size_t flen1 = 0, flen2 = 0;
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
432 } else if (flen2 == 0) {
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
466 if (size1 != size2) {
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
495 return got_error_from_errno("fread");
499 outlen = write(outfd, fbuf, flen);
501 return got_error_from_errno("write");
503 return got_error(GOT_ERR_IO);
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 base_path_deriv, "");
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 base_path_deriv2, "");
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
587 err = got_error_from_errno("dprintf");
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
596 err = got_error_from_errno("dprintf");
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
672 if (err->code != GOT_ERR_FILE_BINARY)
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
706 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
779 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 err = got_error_from_errno2("fchmod", path);
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
852 ondisk_target[ondisk_len] = '\0';
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
916 free(ancestor_target);
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
988 err = got_error_from_errno("asprintf");
993 err = got_opentemp_named(&blob_orig_path, &f_orig,
997 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1004 * No common ancestor exists. This is an "add vs add" conflict
1005 * and we simply use an empty ancestor file to make both files
1006 * appear in the merged result in their entirety.
1008 f_orig = got_opentemp();
1009 if (f_orig == NULL) {
1010 err = got_error_from_errno("got_opentemp");
1015 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 err = got_error_from_errno("asprintf");
1021 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1024 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1029 err = got_object_id_str(&id_str, deriv_base_commit_id);
1032 if (asprintf(&label_deriv, "%s: commit %s",
1033 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 err = got_error_from_errno("asprintf");
1039 * In order the run a 3-way merge with a symlink we copy the symlink's
1040 * target path into a temporary file and use that file with diff3.
1042 if (S_ISLNK(st_mode)) {
1043 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1048 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1050 err = got_error_from_errno2("open", ondisk_path);
1053 f_deriv2 = fdopen(fd, "r");
1054 if (f_deriv2 == NULL) {
1055 err = got_error_from_errno2("fdopen", ondisk_path);
1061 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1065 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1067 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1072 if (blob_orig_path) {
1073 unlink(blob_orig_path);
1074 free(blob_orig_path);
1076 if (blob_deriv_path) {
1077 unlink(blob_deriv_path);
1078 free(blob_deriv_path);
1086 static const struct got_error *
1087 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 int wt_fd, const char *path, struct got_object_id *blob_id)
1091 const struct got_error *err = NULL;
1092 struct got_fileindex_entry *new_ie;
1096 err = got_fileindex_entry_alloc(&new_ie, path);
1100 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 blob_id->sha1, base_commit_id->sha1, 1);
1105 err = got_fileindex_entry_add(fileindex, new_ie);
1108 got_fileindex_entry_free(new_ie);
1115 get_ondisk_perms(int executable, mode_t st_mode)
1117 mode_t xbits = S_IXUSR;
1120 /* Map read bits to execute bits. */
1121 if (st_mode & S_IRGRP)
1123 if (st_mode & S_IROTH)
1125 return st_mode | xbits;
1132 apply_umask(mode_t mode)
1141 /* forward declaration */
1142 static const struct got_error *
1143 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 const char *path, mode_t te_mode, mode_t st_mode,
1145 struct got_blob_object *blob, int restoring_missing_file,
1146 int reverting_versioned_file, int installing_bad_symlink,
1147 int path_is_unversioned, struct got_repository *repo,
1148 got_worktree_checkout_cb progress_cb, void *progress_arg);
1151 * This function assumes that the provided symlink target points at a
1152 * safe location in the work tree!
1154 static const struct got_error *
1155 replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 const char *target_path, size_t target_len)
1158 const struct got_error *err = NULL;
1160 char etarget[PATH_MAX];
1166 * "Bad" symlinks (those pointing outside the work tree or into the
1167 * .got directory) are installed in the work tree as a regular file
1168 * which contains the bad symlink target path.
1169 * The new symlink target has already been checked for safety by our
1170 * caller. If we can successfully open a regular file then we simply
1171 * replace this file with a symlink below.
1173 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1175 if (!got_err_open_nofollow_on_symlink())
1176 return got_error_from_errno2("open", ondisk_path);
1178 /* We are updating an existing on-disk symlink. */
1179 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1181 return got_error_from_errno2("readlink", ondisk_path);
1183 if (elen == target_len &&
1184 memcmp(etarget, target_path, target_len) == 0)
1185 return NULL; /* nothing to do */
1189 err = update_symlink(ondisk_path, target_path, target_len);
1190 if (fd != -1 && close(fd) == -1 && err == NULL)
1191 err = got_error_from_errno2("close", ondisk_path);
1195 static const struct got_error *
1196 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1199 const struct got_error *err = NULL;
1200 char canonpath[PATH_MAX];
1201 char *path_got = NULL;
1203 *is_bad_symlink = 0;
1205 if (target_len >= sizeof(canonpath)) {
1206 *is_bad_symlink = 1;
1211 * We do not use realpath(3) to resolve the symlink's target
1212 * path because we don't want to resolve symlinks recursively.
1213 * Instead we make the path absolute and then canonicalize it.
1214 * Relative symlink target lookup should begin at the directory
1215 * in which the blob object is being installed.
1217 if (!got_path_is_absolute(target_path)) {
1218 char *abspath, *parent;
1219 err = got_path_dirname(&parent, ondisk_path);
1222 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 return got_error_from_errno("asprintf");
1227 if (strlen(abspath) >= sizeof(canonpath)) {
1228 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1232 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1237 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1242 /* Only allow symlinks pointing at paths within the work tree. */
1243 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 *is_bad_symlink = 1;
1248 /* Do not allow symlinks pointing into the .got directory. */
1249 if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 GOT_WORKTREE_GOT_DIR) == -1)
1251 return got_error_from_errno("asprintf");
1252 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 *is_bad_symlink = 1;
1259 static const struct got_error *
1260 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 int restoring_missing_file, int reverting_versioned_file,
1263 int path_is_unversioned, int allow_bad_symlinks,
1264 struct got_repository *repo,
1265 got_worktree_checkout_cb progress_cb, void *progress_arg)
1267 const struct got_error *err = NULL;
1268 char target_path[PATH_MAX];
1269 size_t len, target_len = 0;
1270 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1273 *is_bad_symlink = 0;
1276 * Blob object content specifies the target path of the link.
1277 * If a symbolic link cannot be installed we instead create
1278 * a regular file which contains the link target path stored
1279 * in the blob object.
1282 err = got_object_blob_read_block(&len, blob);
1286 if (len + target_len >= sizeof(target_path)) {
1287 /* Path too long; install as a regular file. */
1288 *is_bad_symlink = 1;
1289 got_object_blob_rewind(blob);
1290 return install_blob(worktree, ondisk_path, path,
1291 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 restoring_missing_file, reverting_versioned_file,
1293 1, path_is_unversioned, repo, progress_cb,
1297 /* Skip blob object header first time around. */
1298 memcpy(target_path + target_len, buf + hdrlen,
1300 target_len += len - hdrlen;
1304 target_path[target_len] = '\0';
1306 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 ondisk_path, worktree->root_path);
1311 if (*is_bad_symlink && !allow_bad_symlinks) {
1312 /* install as a regular file */
1313 got_object_blob_rewind(blob);
1314 err = install_blob(worktree, ondisk_path, path,
1315 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 restoring_missing_file, reverting_versioned_file, 1,
1317 path_is_unversioned, repo, progress_cb, progress_arg);
1321 if (symlink(target_path, ondisk_path) == -1) {
1322 if (errno == EEXIST) {
1323 int symlink_replaced;
1324 if (path_is_unversioned) {
1325 err = (*progress_cb)(progress_arg,
1326 GOT_STATUS_UNVERSIONED, path);
1329 err = replace_existing_symlink(&symlink_replaced,
1330 ondisk_path, target_path, target_len);
1334 if (symlink_replaced) {
1335 err = (*progress_cb)(progress_arg,
1336 reverting_versioned_file ?
1338 GOT_STATUS_UPDATE, path);
1340 err = (*progress_cb)(progress_arg,
1341 GOT_STATUS_EXISTS, path);
1344 return err; /* Nothing else to do. */
1347 if (errno == ENOENT) {
1349 err = got_path_dirname(&parent, ondisk_path);
1352 err = add_dir_on_disk(worktree, parent);
1357 * Retry, and fall through to error handling
1358 * below if this second attempt fails.
1360 if (symlink(target_path, ondisk_path) != -1) {
1361 err = NULL; /* success */
1366 /* Handle errors from first or second creation attempt. */
1367 if (errno == ENAMETOOLONG) {
1368 /* bad target path; install as a regular file */
1369 *is_bad_symlink = 1;
1370 got_object_blob_rewind(blob);
1371 err = install_blob(worktree, ondisk_path, path,
1372 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 restoring_missing_file, reverting_versioned_file, 1,
1374 path_is_unversioned, repo,
1375 progress_cb, progress_arg);
1376 } else if (errno == ENOTDIR) {
1377 err = got_error_path(ondisk_path,
1378 GOT_ERR_FILE_OBSTRUCTED);
1380 err = got_error_from_errno3("symlink",
1381 target_path, ondisk_path);
1383 } else if (progress_cb)
1384 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1389 static const struct got_error *
1390 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 const char *path, mode_t te_mode, mode_t st_mode,
1392 struct got_blob_object *blob, int restoring_missing_file,
1393 int reverting_versioned_file, int installing_bad_symlink,
1394 int path_is_unversioned, struct got_repository *repo,
1395 got_worktree_checkout_cb progress_cb, void *progress_arg)
1397 const struct got_error *err = NULL;
1401 char *tmppath = NULL;
1404 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1408 if (errno == ENOENT) {
1410 err = got_path_dirname(&parent, path);
1413 err = add_dir_on_disk(worktree, parent);
1417 fd = open(ondisk_path,
1418 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 return got_error_from_errno2("open",
1423 } else if (errno == EEXIST) {
1424 if (path_is_unversioned) {
1425 err = (*progress_cb)(progress_arg,
1426 GOT_STATUS_UNVERSIONED, path);
1429 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 /* TODO file is obstructed; do something */
1432 err = got_error_path(ondisk_path,
1433 GOT_ERR_FILE_OBSTRUCTED);
1436 err = got_opentemp_named_fd(&tmppath, &fd,
1442 if (fchmod(fd, apply_umask(mode)) == -1) {
1443 err = got_error_from_errno2("fchmod",
1449 return got_error_from_errno2("open", ondisk_path);
1453 if (restoring_missing_file)
1454 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1456 else if (reverting_versioned_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 err = (*progress_cb)(progress_arg,
1461 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1466 hdrlen = got_object_blob_get_hdrlen(blob);
1468 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 err = got_object_blob_read_block(&len, blob);
1473 /* Skip blob object header first time around. */
1474 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1476 err = got_error_from_errno("write");
1478 } else if (outlen != len - hdrlen) {
1479 err = got_error(GOT_ERR_IO);
1486 if (fsync(fd) != 0) {
1487 err = got_error_from_errno("fsync");
1492 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 err = got_error_from_errno2("unlink", ondisk_path);
1496 if (rename(tmppath, ondisk_path) != 0) {
1497 err = got_error_from_errno3("rename", tmppath,
1506 if (fd != -1 && close(fd) == -1 && err == NULL)
1507 err = got_error_from_errno("close");
1508 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 err = got_error_from_errno2("unlink", tmppath);
1515 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 * conflict marker is found in newly added lines only.
1518 static const struct got_error *
1519 get_modified_file_content_status(unsigned char *status,
1520 struct got_blob_object *blob, const char *path, struct stat *sb,
1523 const struct got_error *err, *free_err;
1524 const char *markers[3] = {
1525 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 GOT_DIFF_CONFLICT_MARKER_SEP,
1527 GOT_DIFF_CONFLICT_MARKER_END
1530 struct got_diffreg_result *diffreg_result = NULL;
1531 struct diff_result *r;
1532 int nchunks_parsed, n, i = 0, ln = 0;
1534 size_t linesize = 0;
1537 if (*status != GOT_STATUS_MODIFY)
1540 f1 = got_opentemp();
1542 return got_error_from_errno("got_opentemp");
1545 got_object_blob_rewind(blob);
1546 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1551 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1552 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1556 r = diffreg_result->result;
1558 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1559 struct diff_chunk *c;
1560 struct diff_chunk_context cc = {};
1564 * We can optimise a little by advancing straight
1565 * to the next chunk if this one has no added lines.
1567 c = diff_chunk_get(r, n);
1569 if (diff_chunk_type(c) != CHUNK_PLUS) {
1571 continue; /* removed or unchanged lines */
1574 pos = diff_chunk_get_right_start_pos(c);
1575 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1576 err = got_ferror(ondisk_file, GOT_ERR_IO);
1580 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1581 ln = cc.right.start;
1583 while (ln < cc.right.end) {
1584 linelen = getline(&line, &linesize, ondisk_file);
1585 if (linelen == -1) {
1586 if (feof(ondisk_file))
1588 err = got_ferror(ondisk_file, GOT_ERR_IO);
1592 if (line && strncmp(line, markers[i],
1593 strlen(markers[i])) == 0) {
1594 if (strcmp(markers[i],
1595 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1596 *status = GOT_STATUS_CONFLICT;
1607 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1608 err = got_error_from_errno("fclose");
1609 free_err = got_diffreg_result_free(diffreg_result);
1617 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1619 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1620 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1624 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1626 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1627 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1628 ie->mtime_sec == sb->st_mtim.tv_sec &&
1629 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1630 ie->size == (sb->st_size & 0xffffffff) &&
1631 !xbit_differs(ie, sb->st_mode));
1634 static unsigned char
1635 get_staged_status(struct got_fileindex_entry *ie)
1637 switch (got_fileindex_entry_stage_get(ie)) {
1638 case GOT_FILEIDX_STAGE_ADD:
1639 return GOT_STATUS_ADD;
1640 case GOT_FILEIDX_STAGE_DELETE:
1641 return GOT_STATUS_DELETE;
1642 case GOT_FILEIDX_STAGE_MODIFY:
1643 return GOT_STATUS_MODIFY;
1645 return GOT_STATUS_NO_CHANGE;
1649 static const struct got_error *
1650 get_symlink_modification_status(unsigned char *status,
1651 struct got_fileindex_entry *ie, const char *abspath,
1652 int dirfd, const char *de_name, struct got_blob_object *blob)
1654 const struct got_error *err = NULL;
1655 char target_path[PATH_MAX];
1656 char etarget[PATH_MAX];
1658 size_t len, target_len = 0;
1659 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1660 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1662 *status = GOT_STATUS_NO_CHANGE;
1664 /* Blob object content specifies the target path of the link. */
1666 err = got_object_blob_read_block(&len, blob);
1669 if (len + target_len >= sizeof(target_path)) {
1671 * Should not happen. The blob contents were OK
1672 * when this symlink was installed.
1674 return got_error(GOT_ERR_NO_SPACE);
1677 /* Skip blob object header first time around. */
1678 memcpy(target_path + target_len, buf + hdrlen,
1680 target_len += len - hdrlen;
1684 target_path[target_len] = '\0';
1687 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1689 return got_error_from_errno2("readlinkat", abspath);
1691 elen = readlink(abspath, etarget, sizeof(etarget));
1693 return got_error_from_errno2("readlink", abspath);
1696 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1697 *status = GOT_STATUS_MODIFY;
1702 static const struct got_error *
1703 get_file_status(unsigned char *status, struct stat *sb,
1704 struct got_fileindex_entry *ie, const char *abspath,
1705 int dirfd, const char *de_name, struct got_repository *repo)
1707 const struct got_error *err = NULL;
1708 struct got_object_id id;
1710 int fd = -1, fd1 = -1;
1713 struct got_blob_object *blob = NULL;
1715 unsigned char staged_status;
1717 staged_status = get_staged_status(ie);
1718 *status = GOT_STATUS_NO_CHANGE;
1719 memset(sb, 0, sizeof(*sb));
1722 * Whenever the caller provides a directory descriptor and a
1723 * directory entry name for the file, use them! This prevents
1724 * race conditions if filesystem paths change beneath our feet.
1727 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1728 if (errno == ENOENT) {
1729 if (got_fileindex_entry_has_file_on_disk(ie))
1730 *status = GOT_STATUS_MISSING;
1732 *status = GOT_STATUS_DELETE;
1735 err = got_error_from_errno2("fstatat", abspath);
1739 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 if (fd == -1 && errno != ENOENT &&
1741 !got_err_open_nofollow_on_symlink())
1742 return got_error_from_errno2("open", abspath);
1743 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1744 if (lstat(abspath, sb) == -1)
1745 return got_error_from_errno2("lstat", abspath);
1746 } else if (fd == -1 || fstat(fd, sb) == -1) {
1747 if (errno == ENOENT) {
1748 if (got_fileindex_entry_has_file_on_disk(ie))
1749 *status = GOT_STATUS_MISSING;
1751 *status = GOT_STATUS_DELETE;
1754 err = got_error_from_errno2("fstat", abspath);
1759 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1760 *status = GOT_STATUS_OBSTRUCTED;
1764 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1765 *status = GOT_STATUS_DELETE;
1767 } else if (!got_fileindex_entry_has_blob(ie) &&
1768 staged_status != GOT_STATUS_ADD) {
1769 *status = GOT_STATUS_ADD;
1773 if (!stat_info_differs(ie, sb))
1776 if (S_ISLNK(sb->st_mode) &&
1777 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1778 *status = GOT_STATUS_MODIFY;
1782 if (staged_status == GOT_STATUS_MODIFY ||
1783 staged_status == GOT_STATUS_ADD)
1784 got_fileindex_entry_get_staged_blob_id(&id, ie);
1786 got_fileindex_entry_get_blob_id(&id, ie);
1788 fd1 = got_opentempfd();
1790 err = got_error_from_errno("got_opentempfd");
1793 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1797 if (S_ISLNK(sb->st_mode)) {
1798 err = get_symlink_modification_status(status, ie,
1799 abspath, dirfd, de_name, blob);
1804 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1806 err = got_error_from_errno2("openat", abspath);
1811 f = fdopen(fd, "r");
1813 err = got_error_from_errno2("fdopen", abspath);
1817 hdrlen = got_object_blob_get_hdrlen(blob);
1819 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1820 err = got_object_blob_read_block(&blen, blob);
1823 /* Skip length of blob object header first time around. */
1824 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1825 if (flen == 0 && ferror(f)) {
1826 err = got_error_from_errno("fread");
1829 if (blen - hdrlen == 0) {
1831 *status = GOT_STATUS_MODIFY;
1833 } else if (flen == 0) {
1834 if (blen - hdrlen != 0)
1835 *status = GOT_STATUS_MODIFY;
1837 } else if (blen - hdrlen == flen) {
1838 /* Skip blob object header first time around. */
1839 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1840 *status = GOT_STATUS_MODIFY;
1844 *status = GOT_STATUS_MODIFY;
1850 if (*status == GOT_STATUS_MODIFY) {
1852 err = get_modified_file_content_status(status, blob, ie->path,
1854 } else if (xbit_differs(ie, sb->st_mode))
1855 *status = GOT_STATUS_MODE_CHANGE;
1857 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1858 err = got_error_from_errno("close");
1860 got_object_blob_close(blob);
1861 if (f != NULL && fclose(f) == EOF && err == NULL)
1862 err = got_error_from_errno2("fclose", abspath);
1863 if (fd != -1 && close(fd) == -1 && err == NULL)
1864 err = got_error_from_errno2("close", abspath);
1869 * Update timestamps in the file index if a file is unmodified and
1870 * we had to run a full content comparison to find out.
1872 static const struct got_error *
1873 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1874 struct got_fileindex_entry *ie, struct stat *sb)
1876 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1877 return got_fileindex_entry_update(ie, wt_fd, path,
1878 ie->blob_sha1, ie->commit_sha1, 1);
1883 static const struct got_error *
1884 update_blob(struct got_worktree *worktree,
1885 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1886 struct got_tree_entry *te, const char *path,
1887 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1890 const struct got_error *err = NULL;
1891 struct got_blob_object *blob = NULL;
1892 char *ondisk_path = NULL;
1893 unsigned char status = GOT_STATUS_NO_CHANGE;
1895 int fd1 = -1, fd2 = -1;
1897 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1898 return got_error_from_errno("asprintf");
1901 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1902 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1905 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1909 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1910 sb.st_mode = got_fileindex_perms_to_st(ie);
1912 if (stat(ondisk_path, &sb) == -1) {
1913 if (errno != ENOENT) {
1914 err = got_error_from_errno2("stat",
1918 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1919 status = GOT_STATUS_UNVERSIONED;
1921 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1922 status = GOT_STATUS_UNVERSIONED;
1924 status = GOT_STATUS_OBSTRUCTED;
1928 if (status == GOT_STATUS_OBSTRUCTED) {
1930 got_fileindex_entry_mark_skipped(ie);
1931 err = (*progress_cb)(progress_arg, status, path);
1934 if (status == GOT_STATUS_CONFLICT) {
1936 got_fileindex_entry_mark_skipped(ie);
1937 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1942 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1943 (S_ISLNK(te->mode) ||
1944 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1946 * This is a regular file or an installed bad symlink.
1947 * If the file index indicates that this file is already
1948 * up-to-date with respect to the repository we can skip
1949 * updating contents of this file.
1951 if (got_fileindex_entry_has_commit(ie) &&
1952 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1953 SHA1_DIGEST_LENGTH) == 0) {
1955 err = sync_timestamps(worktree->root_fd,
1956 path, status, ie, &sb);
1959 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1963 if (got_fileindex_entry_has_blob(ie) &&
1964 memcmp(ie->blob_sha1, te->id.sha1,
1965 SHA1_DIGEST_LENGTH) == 0) {
1966 /* Different commit but the same blob. */
1967 err = sync_timestamps(worktree->root_fd,
1968 path, status, ie, &sb);
1971 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1977 fd1 = got_opentempfd();
1979 err = got_error_from_errno("got_opentempfd");
1982 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1986 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1987 int update_timestamps;
1988 struct got_blob_object *blob2 = NULL;
1989 char *label_orig = NULL;
1990 if (got_fileindex_entry_has_blob(ie)) {
1991 fd2 = got_opentempfd();
1993 err = got_error_from_errno("got_opentempfd");
1996 struct got_object_id id2;
1997 got_fileindex_entry_get_blob_id(&id2, ie);
1998 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2003 if (got_fileindex_entry_has_commit(ie)) {
2004 char id_str[SHA1_DIGEST_STRING_LENGTH];
2005 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2006 sizeof(id_str)) == NULL) {
2007 err = got_error_path(id_str,
2008 GOT_ERR_BAD_OBJ_ID_STR);
2011 if (asprintf(&label_orig, "%s: commit %s",
2012 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2013 err = got_error_from_errno("asprintf");
2017 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2019 err = got_object_blob_read_to_str(&link_target, blob);
2022 err = merge_symlink(worktree, blob2, ondisk_path, path,
2023 label_orig, link_target, worktree->base_commit_id,
2024 repo, progress_cb, progress_arg);
2027 err = merge_blob(&update_timestamps, worktree, blob2,
2028 ondisk_path, path, sb.st_mode, label_orig, blob,
2029 worktree->base_commit_id, repo,
2030 progress_cb, progress_arg);
2033 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2034 err = got_error_from_errno("close");
2038 got_object_blob_close(blob2);
2042 * Do not update timestamps of files with local changes.
2043 * Otherwise, a future status walk would treat them as
2044 * unmodified files again.
2046 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2047 blob->id.sha1, worktree->base_commit_id->sha1,
2049 } else if (status == GOT_STATUS_MODE_CHANGE) {
2050 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2052 } else if (status == GOT_STATUS_DELETE) {
2053 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2056 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2057 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2061 int is_bad_symlink = 0;
2062 if (S_ISLNK(te->mode)) {
2063 err = install_symlink(&is_bad_symlink, worktree,
2064 ondisk_path, path, blob,
2065 status == GOT_STATUS_MISSING, 0,
2066 status == GOT_STATUS_UNVERSIONED, 0,
2067 repo, progress_cb, progress_arg);
2069 err = install_blob(worktree, ondisk_path, path,
2070 te->mode, sb.st_mode, blob,
2071 status == GOT_STATUS_MISSING, 0, 0,
2072 status == GOT_STATUS_UNVERSIONED, repo,
2073 progress_cb, progress_arg);
2079 err = got_fileindex_entry_update(ie,
2080 worktree->root_fd, path, blob->id.sha1,
2081 worktree->base_commit_id->sha1, 1);
2083 err = create_fileindex_entry(&ie, fileindex,
2084 worktree->base_commit_id, worktree->root_fd, path,
2090 if (is_bad_symlink) {
2091 got_fileindex_entry_filetype_set(ie,
2092 GOT_FILEIDX_MODE_BAD_SYMLINK);
2096 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2097 err = got_error_from_errno("close");
2100 got_object_blob_close(blob);
2106 static const struct got_error *
2107 remove_ondisk_file(const char *root_path, const char *path)
2109 const struct got_error *err = NULL;
2110 char *ondisk_path = NULL, *parent = NULL;
2112 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2113 return got_error_from_errno("asprintf");
2115 if (unlink(ondisk_path) == -1) {
2116 if (errno != ENOENT)
2117 err = got_error_from_errno2("unlink", ondisk_path);
2119 size_t root_len = strlen(root_path);
2120 err = got_path_dirname(&parent, ondisk_path);
2123 while (got_path_cmp(parent, root_path,
2124 strlen(parent), root_len) != 0) {
2126 ondisk_path = parent;
2128 if (rmdir(ondisk_path) == -1) {
2129 if (errno != ENOTEMPTY)
2130 err = got_error_from_errno2("rmdir",
2134 err = got_path_dirname(&parent, ondisk_path);
2145 static const struct got_error *
2146 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2147 struct got_fileindex_entry *ie, struct got_repository *repo,
2148 got_worktree_checkout_cb progress_cb, void *progress_arg)
2150 const struct got_error *err = NULL;
2151 unsigned char status;
2155 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2156 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2158 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2160 return got_error_from_errno("asprintf");
2162 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2166 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2167 char ondisk_target[PATH_MAX];
2168 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2169 sizeof(ondisk_target));
2170 if (ondisk_len == -1) {
2171 err = got_error_from_errno2("readlink", ondisk_path);
2174 ondisk_target[ondisk_len] = '\0';
2175 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2176 NULL, NULL, /* XXX pass common ancestor info? */
2177 ondisk_target, ondisk_path);
2180 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2185 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2186 status == GOT_STATUS_ADD) {
2187 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2191 * Preserve the working file and change the deleted blob's
2192 * entry into a schedule-add entry.
2194 err = got_fileindex_entry_update(ie, worktree->root_fd,
2195 ie->path, NULL, NULL, 0);
2197 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2200 if (status == GOT_STATUS_NO_CHANGE) {
2201 err = remove_ondisk_file(worktree->root_path, ie->path);
2205 got_fileindex_entry_remove(fileindex, ie);
2212 struct diff_cb_arg {
2213 struct got_fileindex *fileindex;
2214 struct got_worktree *worktree;
2215 struct got_repository *repo;
2216 got_worktree_checkout_cb progress_cb;
2218 got_cancel_cb cancel_cb;
2222 static const struct got_error *
2223 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2224 struct got_tree_entry *te, const char *parent_path)
2226 struct diff_cb_arg *a = arg;
2228 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2229 return got_error(GOT_ERR_CANCELLED);
2231 return update_blob(a->worktree, a->fileindex, ie, te,
2232 ie->path, a->repo, a->progress_cb, a->progress_arg);
2235 static const struct got_error *
2236 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2238 struct diff_cb_arg *a = arg;
2240 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2241 return got_error(GOT_ERR_CANCELLED);
2243 return delete_blob(a->worktree, a->fileindex, ie,
2244 a->repo, a->progress_cb, a->progress_arg);
2247 static const struct got_error *
2248 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2250 struct diff_cb_arg *a = arg;
2251 const struct got_error *err;
2254 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2255 return got_error(GOT_ERR_CANCELLED);
2257 if (got_object_tree_entry_is_submodule(te))
2260 if (asprintf(&path, "%s%s%s", parent_path,
2261 parent_path[0] ? "/" : "", te->name)
2263 return got_error_from_errno("asprintf");
2265 if (S_ISDIR(te->mode))
2266 err = add_dir_on_disk(a->worktree, path);
2268 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2269 a->repo, a->progress_cb, a->progress_arg);
2275 const struct got_error *
2276 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2278 uint32_t uuid_status;
2280 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2281 if (uuid_status != uuid_s_ok) {
2283 return got_error_uuid(uuid_status, "uuid_to_string");
2289 static const struct got_error *
2290 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2292 const struct got_error *err = NULL;
2293 char *uuidstr = NULL;
2297 err = got_worktree_get_uuid(&uuidstr, worktree);
2301 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2302 err = got_error_from_errno("asprintf");
2309 const struct got_error *
2310 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2313 return get_ref_name(refname, worktree, prefix);
2316 const struct got_error *
2317 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2322 static const struct got_error *
2323 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2325 return get_ref_name(refname, worktree,
2326 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2329 static const struct got_error *
2330 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2335 static const struct got_error *
2336 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2338 return get_ref_name(refname, worktree,
2339 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2342 static const struct got_error *
2343 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2345 return get_ref_name(refname, worktree,
2346 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2349 static const struct got_error *
2350 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2352 return get_ref_name(refname, worktree,
2353 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2356 static const struct got_error *
2357 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2359 return get_ref_name(refname, worktree,
2360 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2363 static const struct got_error *
2364 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2366 return get_ref_name(refname, worktree,
2367 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2370 static const struct got_error *
2371 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2373 return get_ref_name(refname, worktree,
2374 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2377 const struct got_error *
2378 got_worktree_get_histedit_script_path(char **path,
2379 struct got_worktree *worktree)
2381 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2382 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2384 return got_error_from_errno("asprintf");
2389 static const struct got_error *
2390 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2392 return get_ref_name(refname, worktree,
2393 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2396 static const struct got_error *
2397 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2404 * Prevent Git's garbage collector from deleting our base commit by
2405 * setting a reference to our base commit's ID.
2407 static const struct got_error *
2408 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2410 const struct got_error *err = NULL;
2411 struct got_reference *ref = NULL;
2414 err = got_worktree_get_base_ref_name(&refname, worktree);
2418 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2422 err = got_ref_write(ref, repo);
2430 static const struct got_error *
2431 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2433 const struct got_error *err = NULL;
2435 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2436 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2437 err = got_error_from_errno("asprintf");
2438 *fileindex_path = NULL;
2444 static const struct got_error *
2445 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2446 struct got_worktree *worktree)
2448 const struct got_error *err = NULL;
2451 *fileindex_path = NULL;
2452 *fileindex = got_fileindex_alloc();
2453 if (*fileindex == NULL)
2454 return got_error_from_errno("got_fileindex_alloc");
2456 err = get_fileindex_path(fileindex_path, worktree);
2460 index = fopen(*fileindex_path, "rbe");
2461 if (index == NULL) {
2462 if (errno != ENOENT)
2463 err = got_error_from_errno2("fopen", *fileindex_path);
2465 err = got_fileindex_read(*fileindex, index);
2466 if (fclose(index) == EOF && err == NULL)
2467 err = got_error_from_errno("fclose");
2471 free(*fileindex_path);
2472 *fileindex_path = NULL;
2473 got_fileindex_free(*fileindex);
2479 struct bump_base_commit_id_arg {
2480 struct got_object_id *base_commit_id;
2483 const char *entry_name;
2484 got_worktree_checkout_cb progress_cb;
2488 static const struct got_error *
2489 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2491 const struct got_error *err;
2492 struct bump_base_commit_id_arg *a = arg;
2494 if (a->entry_name) {
2495 if (strcmp(ie->path, a->path) != 0)
2497 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2500 if (got_fileindex_entry_was_skipped(ie))
2503 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2504 SHA1_DIGEST_LENGTH) == 0)
2507 if (a->progress_cb) {
2508 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2513 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2517 /* Bump base commit ID of all files within an updated part of the work tree. */
2518 static const struct got_error *
2519 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2520 struct got_fileindex *fileindex,
2521 got_worktree_checkout_cb progress_cb, void *progress_arg)
2523 struct bump_base_commit_id_arg bbc_arg;
2525 bbc_arg.base_commit_id = worktree->base_commit_id;
2526 bbc_arg.entry_name = NULL;
2528 bbc_arg.path_len = 0;
2529 bbc_arg.progress_cb = progress_cb;
2530 bbc_arg.progress_arg = progress_arg;
2532 return got_fileindex_for_each_entry_safe(fileindex,
2533 bump_base_commit_id, &bbc_arg);
2536 static const struct got_error *
2537 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2539 const struct got_error *err = NULL;
2540 char *new_fileindex_path = NULL;
2541 FILE *new_index = NULL;
2542 struct timespec timeout;
2544 err = got_opentemp_named(&new_fileindex_path, &new_index,
2545 fileindex_path, "");
2549 err = got_fileindex_write(fileindex, new_index);
2553 if (rename(new_fileindex_path, fileindex_path) != 0) {
2554 err = got_error_from_errno3("rename", new_fileindex_path,
2556 unlink(new_fileindex_path);
2560 * Sleep for a short amount of time to ensure that files modified after
2561 * this program exits have a different time stamp from the one which
2562 * was recorded in the file index.
2565 timeout.tv_nsec = 1;
2566 nanosleep(&timeout, NULL);
2570 free(new_fileindex_path);
2574 static const struct got_error *
2575 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2576 struct got_object_id **tree_id, const char *wt_relpath,
2577 struct got_commit_object *base_commit, struct got_worktree *worktree,
2578 struct got_repository *repo)
2580 const struct got_error *err = NULL;
2581 struct got_object_id *id = NULL;
2582 char *in_repo_path = NULL;
2583 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2585 *entry_type = GOT_OBJ_TYPE_ANY;
2586 *tree_relpath = NULL;
2589 if (wt_relpath[0] == '\0') {
2590 /* Check out all files within the work tree. */
2591 *entry_type = GOT_OBJ_TYPE_TREE;
2592 *tree_relpath = strdup("");
2593 if (*tree_relpath == NULL) {
2594 err = got_error_from_errno("strdup");
2597 err = got_object_id_by_path(tree_id, repo, base_commit,
2598 worktree->path_prefix);
2604 /* Check out a subset of files in the work tree. */
2606 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2607 is_root_wt ? "" : "/", wt_relpath) == -1) {
2608 err = got_error_from_errno("asprintf");
2612 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2617 in_repo_path = NULL;
2619 err = got_object_get_type(entry_type, repo, id);
2623 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2624 /* Check out a single file. */
2625 if (strchr(wt_relpath, '/') == NULL) {
2626 /* Check out a single file in work tree's root dir. */
2627 in_repo_path = strdup(worktree->path_prefix);
2628 if (in_repo_path == NULL) {
2629 err = got_error_from_errno("strdup");
2632 *tree_relpath = strdup("");
2633 if (*tree_relpath == NULL) {
2634 err = got_error_from_errno("strdup");
2638 /* Check out a single file in a subdirectory. */
2639 err = got_path_dirname(tree_relpath, wt_relpath);
2642 if (asprintf(&in_repo_path, "%s%s%s",
2643 worktree->path_prefix, is_root_wt ? "" : "/",
2644 *tree_relpath) == -1) {
2645 err = got_error_from_errno("asprintf");
2649 err = got_object_id_by_path(tree_id, repo,
2650 base_commit, in_repo_path);
2652 /* Check out all files within a subdirectory. */
2653 *tree_id = got_object_id_dup(id);
2654 if (*tree_id == NULL) {
2655 err = got_error_from_errno("got_object_id_dup");
2658 *tree_relpath = strdup(wt_relpath);
2659 if (*tree_relpath == NULL) {
2660 err = got_error_from_errno("strdup");
2668 *entry_type = GOT_OBJ_TYPE_ANY;
2669 free(*tree_relpath);
2670 *tree_relpath = NULL;
2677 static const struct got_error *
2678 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2679 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2680 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2681 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2683 const struct got_error *err = NULL;
2684 struct got_commit_object *commit = NULL;
2685 struct got_tree_object *tree = NULL;
2686 struct got_fileindex_diff_tree_cb diff_cb;
2687 struct diff_cb_arg arg;
2689 err = ref_base_commit(worktree, repo);
2691 if (!(err->code == GOT_ERR_ERRNO &&
2692 (errno == EACCES || errno == EROFS)))
2694 err = (*progress_cb)(progress_arg,
2695 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2700 err = got_object_open_as_commit(&commit, repo,
2701 worktree->base_commit_id);
2705 err = got_object_open_as_tree(&tree, repo, tree_id);
2710 got_object_tree_find_entry(tree, entry_name) == NULL) {
2711 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2715 diff_cb.diff_old_new = diff_old_new;
2716 diff_cb.diff_old = diff_old;
2717 diff_cb.diff_new = diff_new;
2718 arg.fileindex = fileindex;
2719 arg.worktree = worktree;
2721 arg.progress_cb = progress_cb;
2722 arg.progress_arg = progress_arg;
2723 arg.cancel_cb = cancel_cb;
2724 arg.cancel_arg = cancel_arg;
2725 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2726 entry_name, repo, &diff_cb, &arg);
2729 got_object_tree_close(tree);
2731 got_object_commit_close(commit);
2735 const struct got_error *
2736 got_worktree_checkout_files(struct got_worktree *worktree,
2737 struct got_pathlist_head *paths, struct got_repository *repo,
2738 got_worktree_checkout_cb progress_cb, void *progress_arg,
2739 got_cancel_cb cancel_cb, void *cancel_arg)
2741 const struct got_error *err = NULL, *sync_err, *unlockerr;
2742 struct got_commit_object *commit = NULL;
2743 struct got_tree_object *tree = NULL;
2744 struct got_fileindex *fileindex = NULL;
2745 char *fileindex_path = NULL;
2746 struct got_pathlist_entry *pe;
2747 struct tree_path_data {
2748 STAILQ_ENTRY(tree_path_data) entry;
2749 struct got_object_id *tree_id;
2754 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2756 STAILQ_INIT(&tree_paths);
2758 err = lock_worktree(worktree, LOCK_EX);
2762 err = got_object_open_as_commit(&commit, repo,
2763 worktree->base_commit_id);
2767 /* Map all specified paths to in-repository trees. */
2768 TAILQ_FOREACH(pe, paths, entry) {
2769 tpd = malloc(sizeof(*tpd));
2771 err = got_error_from_errno("malloc");
2775 err = find_tree_entry_for_checkout(&tpd->entry_type,
2776 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2783 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2784 err = got_path_basename(&tpd->entry_name, pe->path);
2792 tpd->entry_name = NULL;
2794 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2798 * Read the file index.
2799 * Checking out files is supposed to be an idempotent operation.
2800 * If the on-disk file index is incomplete we will try to complete it.
2802 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2806 tpd = STAILQ_FIRST(&tree_paths);
2807 TAILQ_FOREACH(pe, paths, entry) {
2808 struct bump_base_commit_id_arg bbc_arg;
2810 err = checkout_files(worktree, fileindex, tpd->relpath,
2811 tpd->tree_id, tpd->entry_name, repo,
2812 progress_cb, progress_arg, cancel_cb, cancel_arg);
2816 bbc_arg.base_commit_id = worktree->base_commit_id;
2817 bbc_arg.entry_name = tpd->entry_name;
2818 bbc_arg.path = pe->path;
2819 bbc_arg.path_len = pe->path_len;
2820 bbc_arg.progress_cb = progress_cb;
2821 bbc_arg.progress_arg = progress_arg;
2822 err = got_fileindex_for_each_entry_safe(fileindex,
2823 bump_base_commit_id, &bbc_arg);
2827 tpd = STAILQ_NEXT(tpd, entry);
2829 sync_err = sync_fileindex(fileindex, fileindex_path);
2830 if (sync_err && err == NULL)
2833 free(fileindex_path);
2835 got_object_tree_close(tree);
2837 got_object_commit_close(commit);
2839 got_fileindex_free(fileindex);
2840 while (!STAILQ_EMPTY(&tree_paths)) {
2841 tpd = STAILQ_FIRST(&tree_paths);
2842 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2847 unlockerr = lock_worktree(worktree, LOCK_SH);
2848 if (unlockerr && err == NULL)
2853 struct merge_file_cb_arg {
2854 struct got_worktree *worktree;
2855 struct got_fileindex *fileindex;
2856 got_worktree_checkout_cb progress_cb;
2858 got_cancel_cb cancel_cb;
2860 const char *label_orig;
2861 struct got_object_id *commit_id2;
2862 int allow_bad_symlinks;
2865 static const struct got_error *
2866 merge_file_cb(void *arg, struct got_blob_object *blob1,
2867 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2868 struct got_object_id *id1, struct got_object_id *id2,
2869 const char *path1, const char *path2,
2870 mode_t mode1, mode_t mode2, struct got_repository *repo)
2872 static const struct got_error *err = NULL;
2873 struct merge_file_cb_arg *a = arg;
2874 struct got_fileindex_entry *ie;
2875 char *ondisk_path = NULL;
2877 unsigned char status;
2878 int local_changes_subsumed;
2879 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2880 char *id_str = NULL, *label_deriv2 = NULL;
2882 if (blob1 && blob2) {
2883 ie = got_fileindex_entry_get(a->fileindex, path2,
2886 return (*a->progress_cb)(a->progress_arg,
2887 GOT_STATUS_MISSING, path2);
2889 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2891 return got_error_from_errno("asprintf");
2893 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2898 if (status == GOT_STATUS_DELETE) {
2899 err = (*a->progress_cb)(a->progress_arg,
2900 GOT_STATUS_MERGE, path2);
2903 if (status != GOT_STATUS_NO_CHANGE &&
2904 status != GOT_STATUS_MODIFY &&
2905 status != GOT_STATUS_CONFLICT &&
2906 status != GOT_STATUS_ADD) {
2907 err = (*a->progress_cb)(a->progress_arg, status, path2);
2911 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2913 err = got_object_blob_read_to_str(&link_target2, blob2);
2916 err = merge_symlink(a->worktree, blob1, ondisk_path,
2917 path2, a->label_orig, link_target2, a->commit_id2,
2918 repo, a->progress_cb, a->progress_arg);
2923 f_orig = got_opentemp();
2924 if (f_orig == NULL) {
2925 err = got_error_from_errno("got_opentemp");
2928 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2933 f_deriv2 = got_opentemp();
2934 if (f_deriv2 == NULL)
2936 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2941 fd = open(ondisk_path,
2942 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2944 err = got_error_from_errno2("open",
2948 f_deriv = fdopen(fd, "r");
2949 if (f_deriv == NULL) {
2950 err = got_error_from_errno2("fdopen",
2955 err = got_object_id_str(&id_str, a->commit_id2);
2958 if (asprintf(&label_deriv2, "%s: commit %s",
2959 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2960 err = got_error_from_errno("asprintf");
2963 err = merge_file(&local_changes_subsumed, a->worktree,
2964 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2965 mode2, a->label_orig, NULL, label_deriv2,
2966 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2967 a->progress_cb, a->progress_arg);
2970 ie = got_fileindex_entry_get(a->fileindex, path1,
2973 return (*a->progress_cb)(a->progress_arg,
2974 GOT_STATUS_MISSING, path1);
2976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2978 return got_error_from_errno("asprintf");
2980 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2986 case GOT_STATUS_NO_CHANGE:
2987 err = (*a->progress_cb)(a->progress_arg,
2988 GOT_STATUS_DELETE, path1);
2991 err = remove_ondisk_file(a->worktree->root_path, path1);
2995 got_fileindex_entry_mark_deleted_from_disk(ie);
2997 case GOT_STATUS_DELETE:
2998 case GOT_STATUS_MISSING:
2999 err = (*a->progress_cb)(a->progress_arg,
3000 GOT_STATUS_DELETE, path1);
3004 got_fileindex_entry_mark_deleted_from_disk(ie);
3006 case GOT_STATUS_ADD: {
3007 struct got_object_id *id;
3011 * Delete the added file only if its content already
3012 * exists in the repository.
3014 err = got_object_blob_file_create(&id, &blob1_f,
3015 &blob1_size, path1);
3018 if (got_object_id_cmp(id, id1) == 0) {
3019 err = (*a->progress_cb)(a->progress_arg,
3020 GOT_STATUS_DELETE, path1);
3023 err = remove_ondisk_file(a->worktree->root_path,
3028 got_fileindex_entry_remove(a->fileindex,
3031 err = (*a->progress_cb)(a->progress_arg,
3032 GOT_STATUS_CANNOT_DELETE, path1);
3034 if (fclose(blob1_f) == EOF && err == NULL)
3035 err = got_error_from_errno("fclose");
3041 case GOT_STATUS_MODIFY:
3042 case GOT_STATUS_CONFLICT:
3043 err = (*a->progress_cb)(a->progress_arg,
3044 GOT_STATUS_CANNOT_DELETE, path1);
3048 case GOT_STATUS_OBSTRUCTED:
3049 err = (*a->progress_cb)(a->progress_arg, status, path1);
3057 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3059 return got_error_from_errno("asprintf");
3060 ie = got_fileindex_entry_get(a->fileindex, path2,
3063 err = get_file_status(&status, &sb, ie, ondisk_path,
3067 if (status != GOT_STATUS_NO_CHANGE &&
3068 status != GOT_STATUS_MODIFY &&
3069 status != GOT_STATUS_CONFLICT &&
3070 status != GOT_STATUS_ADD) {
3071 err = (*a->progress_cb)(a->progress_arg,
3075 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3077 err = got_object_blob_read_to_str(&link_target2,
3081 err = merge_symlink(a->worktree, NULL,
3082 ondisk_path, path2, a->label_orig,
3083 link_target2, a->commit_id2, repo,
3084 a->progress_cb, a->progress_arg);
3086 } else if (S_ISREG(sb.st_mode)) {
3087 err = merge_blob(&local_changes_subsumed,
3088 a->worktree, NULL, ondisk_path, path2,
3089 sb.st_mode, a->label_orig, blob2,
3090 a->commit_id2, repo, a->progress_cb,
3093 err = got_error_path(ondisk_path,
3094 GOT_ERR_FILE_OBSTRUCTED);
3098 if (status == GOT_STATUS_DELETE) {
3099 err = got_fileindex_entry_update(ie,
3100 a->worktree->root_fd, path2, blob2->id.sha1,
3101 a->worktree->base_commit_id->sha1, 0);
3106 int is_bad_symlink = 0;
3107 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3108 if (S_ISLNK(mode2)) {
3109 err = install_symlink(&is_bad_symlink,
3110 a->worktree, ondisk_path, path2, blob2, 0,
3111 0, 1, a->allow_bad_symlinks, repo,
3112 a->progress_cb, a->progress_arg);
3114 err = install_blob(a->worktree, ondisk_path, path2,
3115 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3116 a->progress_cb, a->progress_arg);
3120 err = got_fileindex_entry_alloc(&ie, path2);
3123 err = got_fileindex_entry_update(ie,
3124 a->worktree->root_fd, path2, NULL, NULL, 1);
3126 got_fileindex_entry_free(ie);
3129 err = got_fileindex_entry_add(a->fileindex, ie);
3131 got_fileindex_entry_free(ie);
3134 if (is_bad_symlink) {
3135 got_fileindex_entry_filetype_set(ie,
3136 GOT_FILEIDX_MODE_BAD_SYMLINK);
3141 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3142 err = got_error_from_errno("fclose");
3143 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3144 err = got_error_from_errno("fclose");
3145 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3146 err = got_error_from_errno("fclose");
3153 static const struct got_error *
3154 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3156 struct got_worktree *worktree = arg;
3158 /* Reject merges into a work tree with mixed base commits. */
3159 if (got_fileindex_entry_has_commit(ie) &&
3160 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3161 SHA1_DIGEST_LENGTH) != 0)
3162 return got_error(GOT_ERR_MIXED_COMMITS);
3167 struct check_merge_conflicts_arg {
3168 struct got_worktree *worktree;
3169 struct got_fileindex *fileindex;
3170 struct got_repository *repo;
3173 static const struct got_error *
3174 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3175 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3176 struct got_object_id *id1, struct got_object_id *id2,
3177 const char *path1, const char *path2,
3178 mode_t mode1, mode_t mode2, struct got_repository *repo)
3180 const struct got_error *err = NULL;
3181 struct check_merge_conflicts_arg *a = arg;
3182 unsigned char status;
3184 struct got_fileindex_entry *ie;
3185 const char *path = path2 ? path2 : path1;
3186 struct got_object_id *id = id2 ? id2 : id1;
3192 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3196 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3198 return got_error_from_errno("asprintf");
3200 /* Reject merges into a work tree with conflicted files. */
3201 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3205 if (status == GOT_STATUS_CONFLICT)
3206 return got_error(GOT_ERR_CONFLICTS);
3211 static const struct got_error *
3212 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3213 const char *fileindex_path, struct got_object_id *commit_id1,
3214 struct got_object_id *commit_id2, struct got_repository *repo,
3215 got_worktree_checkout_cb progress_cb, void *progress_arg,
3216 got_cancel_cb cancel_cb, void *cancel_arg)
3218 const struct got_error *err = NULL, *sync_err;
3219 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3220 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3221 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3222 struct check_merge_conflicts_arg cmc_arg;
3223 struct merge_file_cb_arg arg;
3224 char *label_orig = NULL;
3225 FILE *f1 = NULL, *f2 = NULL;
3226 int fd1 = -1, fd2 = -1;
3229 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3232 err = got_object_id_by_path(&tree_id1, repo, commit1,
3233 worktree->path_prefix);
3234 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3240 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3244 err = got_object_id_str(&id_str, commit_id1);
3248 if (asprintf(&label_orig, "%s: commit %s",
3249 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3250 err = got_error_from_errno("asprintf");
3256 f1 = got_opentemp();
3258 err = got_error_from_errno("got_opentemp");
3263 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3267 err = got_object_id_by_path(&tree_id2, repo, commit2,
3268 worktree->path_prefix);
3272 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3276 f2 = got_opentemp();
3278 err = got_error_from_errno("got_opentemp");
3282 fd1 = got_opentempfd();
3284 err = got_error_from_errno("got_opentempfd");
3288 fd2 = got_opentempfd();
3290 err = got_error_from_errno("got_opentempfd");
3294 cmc_arg.worktree = worktree;
3295 cmc_arg.fileindex = fileindex;
3296 cmc_arg.repo = repo;
3297 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3298 check_merge_conflicts, &cmc_arg, 0);
3302 arg.worktree = worktree;
3303 arg.fileindex = fileindex;
3304 arg.progress_cb = progress_cb;
3305 arg.progress_arg = progress_arg;
3306 arg.cancel_cb = cancel_cb;
3307 arg.cancel_arg = cancel_arg;
3308 arg.label_orig = label_orig;
3309 arg.commit_id2 = commit_id2;
3310 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3311 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3312 merge_file_cb, &arg, 1);
3313 sync_err = sync_fileindex(fileindex, fileindex_path);
3314 if (sync_err && err == NULL)
3318 got_object_commit_close(commit1);
3320 got_object_commit_close(commit2);
3322 got_object_tree_close(tree1);
3324 got_object_tree_close(tree2);
3325 if (f1 && fclose(f1) == EOF && err == NULL)
3326 err = got_error_from_errno("fclose");
3327 if (f2 && fclose(f2) == EOF && err == NULL)
3328 err = got_error_from_errno("fclose");
3329 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3330 err = got_error_from_errno("close");
3331 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3332 err = got_error_from_errno("close");
3337 const struct got_error *
3338 got_worktree_merge_files(struct got_worktree *worktree,
3339 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3340 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3341 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3343 const struct got_error *err, *unlockerr;
3344 char *fileindex_path = NULL;
3345 struct got_fileindex *fileindex = NULL;
3347 err = lock_worktree(worktree, LOCK_EX);
3351 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3355 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3360 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3361 commit_id2, repo, progress_cb, progress_arg,
3362 cancel_cb, cancel_arg);
3365 got_fileindex_free(fileindex);
3366 free(fileindex_path);
3367 unlockerr = lock_worktree(worktree, LOCK_SH);
3368 if (unlockerr && err == NULL)
3373 struct diff_dir_cb_arg {
3374 struct got_fileindex *fileindex;
3375 struct got_worktree *worktree;
3376 const char *status_path;
3377 size_t status_path_len;
3378 struct got_repository *repo;
3379 got_worktree_status_cb status_cb;
3381 got_cancel_cb cancel_cb;
3383 /* A pathlist containing per-directory pathlists of ignore patterns. */
3384 struct got_pathlist_head *ignores;
3385 int report_unchanged;
3389 static const struct got_error *
3390 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3391 int dirfd, const char *de_name,
3392 got_worktree_status_cb status_cb, void *status_arg,
3393 struct got_repository *repo, int report_unchanged)
3395 const struct got_error *err = NULL;
3396 unsigned char status = GOT_STATUS_NO_CHANGE;
3397 unsigned char staged_status;
3399 struct got_object_id blob_id, commit_id, staged_blob_id;
3400 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3401 struct got_object_id *staged_blob_idp = NULL;
3403 staged_status = get_staged_status(ie);
3404 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3408 if (status == GOT_STATUS_NO_CHANGE &&
3409 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3412 if (got_fileindex_entry_has_blob(ie))
3413 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3414 if (got_fileindex_entry_has_commit(ie))
3415 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3416 if (staged_status == GOT_STATUS_ADD ||
3417 staged_status == GOT_STATUS_MODIFY) {
3418 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3419 &staged_blob_id, ie);
3422 return (*status_cb)(status_arg, status, staged_status,
3423 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3426 static const struct got_error *
3427 status_old_new(void *arg, struct got_fileindex_entry *ie,
3428 struct dirent *de, const char *parent_path, int dirfd)
3430 const struct got_error *err = NULL;
3431 struct diff_dir_cb_arg *a = arg;
3434 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3435 return got_error(GOT_ERR_CANCELLED);
3437 if (got_path_cmp(parent_path, a->status_path,
3438 strlen(parent_path), a->status_path_len) != 0 &&
3439 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3442 if (parent_path[0]) {
3443 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3444 parent_path, de->d_name) == -1)
3445 return got_error_from_errno("asprintf");
3447 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3449 return got_error_from_errno("asprintf");
3452 err = report_file_status(ie, abspath, dirfd, de->d_name,
3453 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3458 static const struct got_error *
3459 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3461 struct diff_dir_cb_arg *a = arg;
3462 struct got_object_id blob_id, commit_id;
3463 unsigned char status;
3465 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3466 return got_error(GOT_ERR_CANCELLED);
3468 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3471 got_fileindex_entry_get_blob_id(&blob_id, ie);
3472 got_fileindex_entry_get_commit_id(&commit_id, ie);
3473 if (got_fileindex_entry_has_file_on_disk(ie))
3474 status = GOT_STATUS_MISSING;
3476 status = GOT_STATUS_DELETE;
3477 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3478 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3482 free_ignores(struct got_pathlist_head *ignores)
3484 struct got_pathlist_entry *pe;
3486 TAILQ_FOREACH(pe, ignores, entry) {
3487 struct got_pathlist_head *ignorelist = pe->data;
3489 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3491 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3494 static const struct got_error *
3495 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3497 const struct got_error *err = NULL;
3498 struct got_pathlist_entry *pe = NULL;
3499 struct got_pathlist_head *ignorelist;
3500 char *line = NULL, *pattern, *dirpath = NULL;
3501 size_t linesize = 0;
3504 ignorelist = calloc(1, sizeof(*ignorelist));
3505 if (ignorelist == NULL)
3506 return got_error_from_errno("calloc");
3507 TAILQ_INIT(ignorelist);
3509 while ((linelen = getline(&line, &linesize, f)) != -1) {
3510 if (linelen > 0 && line[linelen - 1] == '\n')
3511 line[linelen - 1] = '\0';
3513 /* Git's ignores may contain comments. */
3517 /* Git's negated patterns are not (yet?) supported. */
3521 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3523 err = got_error_from_errno("asprintf");
3526 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3531 err = got_error_from_errno("getline");
3535 dirpath = strdup(path);
3536 if (dirpath == NULL) {
3537 err = got_error_from_errno("strdup");
3540 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3543 if (err || pe == NULL) {
3545 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3551 match_path(const char *pattern, size_t pattern_len, const char *path,
3557 * Trailing slashes signify directories.
3558 * Append a * to make such patterns conform to fnmatch rules.
3560 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3561 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3562 return FNM_NOMATCH; /* XXX */
3564 return fnmatch(buf, path, flags);
3567 return fnmatch(pattern, path, flags);
3571 match_ignores(struct got_pathlist_head *ignores, const char *path)
3573 struct got_pathlist_entry *pe;
3575 /* Handle patterns which match in all directories. */
3576 TAILQ_FOREACH(pe, ignores, entry) {
3577 struct got_pathlist_head *ignorelist = pe->data;
3578 struct got_pathlist_entry *pi;
3580 TAILQ_FOREACH(pi, ignorelist, entry) {
3583 if (pi->path_len < 3 ||
3584 strncmp(pi->path, "**/", 3) != 0)
3588 if (match_path(pi->path + 3,
3589 pi->path_len - 3, p,
3590 FNM_PATHNAME | FNM_LEADING_DIR)) {
3591 /* Retry in next directory. */
3592 while (*p && *p != '/')
3604 * The ignores pathlist contains ignore lists from children before
3605 * parents, so we can find the most specific ignorelist by walking
3606 * ignores backwards.
3608 pe = TAILQ_LAST(ignores, got_pathlist_head);
3610 if (got_path_is_child(path, pe->path, pe->path_len)) {
3611 struct got_pathlist_head *ignorelist = pe->data;
3612 struct got_pathlist_entry *pi;
3613 TAILQ_FOREACH(pi, ignorelist, entry) {
3614 int flags = FNM_LEADING_DIR;
3615 if (strstr(pi->path, "/**/") == NULL)
3616 flags |= FNM_PATHNAME;
3617 if (match_path(pi->path, pi->path_len,
3623 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3629 static const struct got_error *
3630 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3631 const char *path, int dirfd, const char *ignores_filename)
3633 const struct got_error *err = NULL;
3636 FILE *ignoresfile = NULL;
3638 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3639 path[0] ? "/" : "", ignores_filename) == -1)
3640 return got_error_from_errno("asprintf");
3643 fd = openat(dirfd, ignores_filename,
3644 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3646 if (errno != ENOENT && errno != EACCES)
3647 err = got_error_from_errno2("openat",
3650 ignoresfile = fdopen(fd, "r");
3651 if (ignoresfile == NULL)
3652 err = got_error_from_errno2("fdopen",
3656 err = read_ignores(ignores, path, ignoresfile);
3660 ignoresfile = fopen(ignorespath, "re");
3661 if (ignoresfile == NULL) {
3662 if (errno != ENOENT && errno != EACCES)
3663 err = got_error_from_errno2("fopen",
3666 err = read_ignores(ignores, path, ignoresfile);
3669 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3670 err = got_error_from_errno2("fclose", path);
3671 if (fd != -1 && close(fd) == -1 && err == NULL)
3672 err = got_error_from_errno2("close", path);
3677 static const struct got_error *
3678 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3681 const struct got_error *err = NULL;
3682 struct diff_dir_cb_arg *a = arg;
3688 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3689 return got_error(GOT_ERR_CANCELLED);
3691 if (parent_path[0]) {
3692 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3693 return got_error_from_errno("asprintf");
3698 if (de->d_type == DT_DIR) {
3699 if (!a->no_ignores && ignore != NULL &&
3700 match_ignores(a->ignores, path))
3702 } else if (!match_ignores(a->ignores, path) &&
3703 got_path_is_child(path, a->status_path, a->status_path_len))
3704 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3705 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3711 static const struct got_error *
3712 status_traverse(void *arg, const char *path, int dirfd)
3714 const struct got_error *err = NULL;
3715 struct diff_dir_cb_arg *a = arg;
3720 err = add_ignores(a->ignores, a->worktree->root_path,
3721 path, dirfd, ".cvsignore");
3725 err = add_ignores(a->ignores, a->worktree->root_path, path,
3726 dirfd, ".gitignore");
3731 static const struct got_error *
3732 report_single_file_status(const char *path, const char *ondisk_path,
3733 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3734 void *status_arg, struct got_repository *repo, int report_unchanged,
3735 struct got_pathlist_head *ignores, int no_ignores)
3737 struct got_fileindex_entry *ie;
3740 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3742 return report_file_status(ie, ondisk_path, -1, NULL,
3743 status_cb, status_arg, repo, report_unchanged);
3745 if (lstat(ondisk_path, &sb) == -1) {
3746 if (errno != ENOENT)
3747 return got_error_from_errno2("lstat", ondisk_path);
3748 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3749 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3752 if (!no_ignores && match_ignores(ignores, path))
3755 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3756 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3757 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3762 static const struct got_error *
3763 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3764 const char *root_path, const char *path)
3766 const struct got_error *err;
3767 char *parent_path, *next_parent_path = NULL;
3769 err = add_ignores(ignores, root_path, "", -1,
3774 err = add_ignores(ignores, root_path, "", -1,
3779 err = got_path_dirname(&parent_path, path);
3781 if (err->code == GOT_ERR_BAD_PATH)
3782 return NULL; /* cannot traverse parent */
3786 err = add_ignores(ignores, root_path, parent_path, -1,
3790 err = add_ignores(ignores, root_path, parent_path, -1,
3794 err = got_path_dirname(&next_parent_path, parent_path);
3796 if (err->code == GOT_ERR_BAD_PATH)
3797 err = NULL; /* traversed everything */
3800 if (got_path_is_root_dir(parent_path))
3803 parent_path = next_parent_path;
3804 next_parent_path = NULL;
3808 free(next_parent_path);
3812 static const struct got_error *
3813 worktree_status(struct got_worktree *worktree, const char *path,
3814 struct got_fileindex *fileindex, struct got_repository *repo,
3815 got_worktree_status_cb status_cb, void *status_arg,
3816 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3817 int report_unchanged)
3819 const struct got_error *err = NULL;
3821 struct got_fileindex_diff_dir_cb fdiff_cb;
3822 struct diff_dir_cb_arg arg;
3823 char *ondisk_path = NULL;
3824 struct got_pathlist_head ignores;
3825 struct got_fileindex_entry *ie;
3827 TAILQ_INIT(&ignores);
3829 if (asprintf(&ondisk_path, "%s%s%s",
3830 worktree->root_path, path[0] ? "/" : "", path) == -1)
3831 return got_error_from_errno("asprintf");
3833 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3835 err = report_single_file_status(path, ondisk_path,
3836 fileindex, status_cb, status_arg, repo,
3837 report_unchanged, &ignores, no_ignores);
3841 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3843 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3844 !got_err_open_nofollow_on_symlink())
3845 err = got_error_from_errno2("open", ondisk_path);
3848 err = add_ignores_from_parent_paths(&ignores,
3849 worktree->root_path, ondisk_path);
3853 err = report_single_file_status(path, ondisk_path,
3854 fileindex, status_cb, status_arg, repo,
3855 report_unchanged, &ignores, no_ignores);
3858 fdiff_cb.diff_old_new = status_old_new;
3859 fdiff_cb.diff_old = status_old;
3860 fdiff_cb.diff_new = status_new;
3861 fdiff_cb.diff_traverse = status_traverse;
3862 arg.fileindex = fileindex;
3863 arg.worktree = worktree;
3864 arg.status_path = path;
3865 arg.status_path_len = strlen(path);
3867 arg.status_cb = status_cb;
3868 arg.status_arg = status_arg;
3869 arg.cancel_cb = cancel_cb;
3870 arg.cancel_arg = cancel_arg;
3871 arg.report_unchanged = report_unchanged;
3872 arg.no_ignores = no_ignores;
3874 err = add_ignores_from_parent_paths(&ignores,
3875 worktree->root_path, path);
3879 arg.ignores = &ignores;
3880 err = got_fileindex_diff_dir(fileindex, fd,
3881 worktree->root_path, path, repo, &fdiff_cb, &arg);
3884 free_ignores(&ignores);
3885 if (fd != -1 && close(fd) == -1 && err == NULL)
3886 err = got_error_from_errno("close");
3891 const struct got_error *
3892 got_worktree_status(struct got_worktree *worktree,
3893 struct got_pathlist_head *paths, struct got_repository *repo,
3894 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3895 got_cancel_cb cancel_cb, void *cancel_arg)
3897 const struct got_error *err = NULL;
3898 char *fileindex_path = NULL;
3899 struct got_fileindex *fileindex = NULL;
3900 struct got_pathlist_entry *pe;
3902 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3906 TAILQ_FOREACH(pe, paths, entry) {
3907 err = worktree_status(worktree, pe->path, fileindex, repo,
3908 status_cb, status_arg, cancel_cb, cancel_arg,
3913 free(fileindex_path);
3914 got_fileindex_free(fileindex);
3918 const struct got_error *
3919 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3922 const struct got_error *err = NULL;
3923 char *resolved = NULL, *cwd = NULL, *path = NULL;
3926 char *abspath = NULL;
3927 char canonpath[PATH_MAX];
3931 cwd = getcwd(NULL, 0);
3933 return got_error_from_errno("getcwd");
3935 if (lstat(arg, &sb) == -1) {
3936 if (errno != ENOENT) {
3937 err = got_error_from_errno2("lstat", arg);
3942 if (S_ISLNK(sb.st_mode)) {
3944 * We cannot use realpath(3) with symlinks since we want to
3945 * operate on the symlink itself.
3946 * But we can make the path absolute, assuming it is relative
3947 * to the current working directory, and then canonicalize it.
3949 if (!got_path_is_absolute(arg)) {
3950 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3951 err = got_error_from_errno("asprintf");
3956 err = got_canonpath(abspath ? abspath : arg, canonpath,
3960 resolved = strdup(canonpath);
3961 if (resolved == NULL) {
3962 err = got_error_from_errno("strdup");
3966 resolved = realpath(arg, NULL);
3967 if (resolved == NULL) {
3968 if (errno != ENOENT) {
3969 err = got_error_from_errno2("realpath", arg);
3972 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3973 err = got_error_from_errno("asprintf");
3976 err = got_canonpath(abspath, canonpath,
3980 resolved = strdup(canonpath);
3981 if (resolved == NULL) {
3982 err = got_error_from_errno("strdup");
3988 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3989 strlen(got_worktree_get_root_path(worktree)))) {
3990 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3994 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3995 err = got_path_skip_common_ancestor(&path,
3996 got_worktree_get_root_path(worktree), resolved);
4002 err = got_error_from_errno("strdup");
4007 /* XXX status walk can't deal with trailing slash! */
4009 while (len > 0 && path[len - 1] == '/') {
4010 path[len - 1] = '\0';
4024 struct schedule_addition_args {
4025 struct got_worktree *worktree;
4026 struct got_fileindex *fileindex;
4027 got_worktree_checkout_cb progress_cb;
4029 struct got_repository *repo;
4032 static const struct got_error *
4033 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4034 const char *relpath, struct got_object_id *blob_id,
4035 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4036 int dirfd, const char *de_name)
4038 struct schedule_addition_args *a = arg;
4039 const struct got_error *err = NULL;
4040 struct got_fileindex_entry *ie;
4044 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4046 return got_error_from_errno("asprintf");
4048 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4050 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4054 /* Re-adding an existing entry is a no-op. */
4055 if (status == GOT_STATUS_ADD)
4057 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4062 if (status != GOT_STATUS_UNVERSIONED) {
4063 if (status == GOT_STATUS_NONEXISTENT)
4064 err = got_error_set_errno(ENOENT, ondisk_path);
4066 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4070 err = got_fileindex_entry_alloc(&ie, relpath);
4073 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4074 relpath, NULL, NULL, 1);
4076 got_fileindex_entry_free(ie);
4079 err = got_fileindex_entry_add(a->fileindex, ie);
4081 got_fileindex_entry_free(ie);
4088 if (status == GOT_STATUS_ADD)
4090 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4093 const struct got_error *
4094 got_worktree_schedule_add(struct got_worktree *worktree,
4095 struct got_pathlist_head *paths,
4096 got_worktree_checkout_cb progress_cb, void *progress_arg,
4097 struct got_repository *repo, int no_ignores)
4099 struct got_fileindex *fileindex = NULL;
4100 char *fileindex_path = NULL;
4101 const struct got_error *err = NULL, *sync_err, *unlockerr;
4102 struct got_pathlist_entry *pe;
4103 struct schedule_addition_args saa;
4105 err = lock_worktree(worktree, LOCK_EX);
4109 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4113 saa.worktree = worktree;
4114 saa.fileindex = fileindex;
4115 saa.progress_cb = progress_cb;
4116 saa.progress_arg = progress_arg;
4119 TAILQ_FOREACH(pe, paths, entry) {
4120 err = worktree_status(worktree, pe->path, fileindex, repo,
4121 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4125 sync_err = sync_fileindex(fileindex, fileindex_path);
4126 if (sync_err && err == NULL)
4129 free(fileindex_path);
4131 got_fileindex_free(fileindex);
4132 unlockerr = lock_worktree(worktree, LOCK_SH);
4133 if (unlockerr && err == NULL)
4138 struct schedule_deletion_args {
4139 struct got_worktree *worktree;
4140 struct got_fileindex *fileindex;
4141 got_worktree_delete_cb progress_cb;
4143 struct got_repository *repo;
4144 int delete_local_mods;
4146 int ignore_missing_paths;
4147 const char *status_codes;
4150 static const struct got_error *
4151 schedule_for_deletion(void *arg, unsigned char status,
4152 unsigned char staged_status, const char *relpath,
4153 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4154 struct got_object_id *commit_id, int dirfd, const char *de_name)
4156 struct schedule_deletion_args *a = arg;
4157 const struct got_error *err = NULL;
4158 struct got_fileindex_entry *ie = NULL;
4162 if (status == GOT_STATUS_NONEXISTENT) {
4163 if (a->ignore_missing_paths)
4165 return got_error_set_errno(ENOENT, relpath);
4168 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4170 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4172 staged_status = get_staged_status(ie);
4173 if (staged_status != GOT_STATUS_NO_CHANGE) {
4174 if (staged_status == GOT_STATUS_DELETE)
4176 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4179 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4181 return got_error_from_errno("asprintf");
4183 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4188 if (a->status_codes) {
4189 size_t ncodes = strlen(a->status_codes);
4191 for (i = 0; i < ncodes ; i++) {
4192 if (status == a->status_codes[i])
4196 /* Do not delete files in non-matching status. */
4200 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4201 a->status_codes[i] != GOT_STATUS_MISSING) {
4202 static char msg[64];
4203 snprintf(msg, sizeof(msg),
4204 "invalid status code '%c'", a->status_codes[i]);
4205 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4210 if (status != GOT_STATUS_NO_CHANGE) {
4211 if (status == GOT_STATUS_DELETE)
4213 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4214 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4217 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4218 err = got_error_set_errno(ENOENT, relpath);
4221 if (status != GOT_STATUS_MODIFY &&
4222 status != GOT_STATUS_MISSING) {
4223 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4228 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4232 if (unlinkat(dirfd, de_name, 0) == -1) {
4233 err = got_error_from_errno2("unlinkat",
4237 } else if (unlink(ondisk_path) == -1) {
4238 err = got_error_from_errno2("unlink", ondisk_path);
4242 root_len = strlen(a->worktree->root_path);
4245 err = got_path_dirname(&parent, ondisk_path);
4249 ondisk_path = parent;
4250 if (rmdir(ondisk_path) == -1) {
4251 if (errno != ENOTEMPTY)
4252 err = got_error_from_errno2("rmdir",
4256 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4257 strlen(ondisk_path), root_len) != 0);
4260 got_fileindex_entry_mark_deleted_from_disk(ie);
4265 if (status == GOT_STATUS_DELETE)
4267 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4268 staged_status, relpath);
4271 const struct got_error *
4272 got_worktree_schedule_delete(struct got_worktree *worktree,
4273 struct got_pathlist_head *paths, int delete_local_mods,
4274 const char *status_codes,
4275 got_worktree_delete_cb progress_cb, void *progress_arg,
4276 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4278 struct got_fileindex *fileindex = NULL;
4279 char *fileindex_path = NULL;
4280 const struct got_error *err = NULL, *sync_err, *unlockerr;
4281 struct got_pathlist_entry *pe;
4282 struct schedule_deletion_args sda;
4284 err = lock_worktree(worktree, LOCK_EX);
4288 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4292 sda.worktree = worktree;
4293 sda.fileindex = fileindex;
4294 sda.progress_cb = progress_cb;
4295 sda.progress_arg = progress_arg;
4297 sda.delete_local_mods = delete_local_mods;
4298 sda.keep_on_disk = keep_on_disk;
4299 sda.ignore_missing_paths = ignore_missing_paths;
4300 sda.status_codes = status_codes;
4302 TAILQ_FOREACH(pe, paths, entry) {
4303 err = worktree_status(worktree, pe->path, fileindex, repo,
4304 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4308 sync_err = sync_fileindex(fileindex, fileindex_path);
4309 if (sync_err && err == NULL)
4312 free(fileindex_path);
4314 got_fileindex_free(fileindex);
4315 unlockerr = lock_worktree(worktree, LOCK_SH);
4316 if (unlockerr && err == NULL)
4321 static const struct got_error *
4322 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4324 const struct got_error *err = NULL;
4326 size_t linesize = 0, n;
4329 linelen = getline(&line, &linesize, infile);
4330 if (linelen == -1) {
4331 if (ferror(infile)) {
4332 err = got_error_from_errno("getline");
4338 n = fwrite(line, 1, linelen, outfile);
4340 err = got_ferror(outfile, GOT_ERR_IO);
4345 n = fwrite(line, 1, linelen, rejectfile);
4347 err = got_ferror(rejectfile, GOT_ERR_IO);
4354 static const struct got_error *
4355 skip_one_line(FILE *f)
4358 size_t linesize = 0;
4361 linelen = getline(&line, &linesize, f);
4362 if (linelen == -1) {
4364 return got_error_from_errno("getline");
4371 static const struct got_error *
4372 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4373 int start_old, int end_old, int start_new, int end_new,
4374 FILE *outfile, FILE *rejectfile)
4376 const struct got_error *err;
4378 /* Copy old file's lines leading up to patch. */
4379 while (!feof(f1) && *line_cur1 < start_old) {
4380 err = copy_one_line(f1, outfile, NULL);
4385 /* Skip new file's lines leading up to patch. */
4386 while (!feof(f2) && *line_cur2 < start_new) {
4388 err = copy_one_line(f2, NULL, rejectfile);
4390 err = skip_one_line(f2);
4395 /* Copy patched lines. */
4396 while (!feof(f2) && *line_cur2 <= end_new) {
4397 err = copy_one_line(f2, outfile, NULL);
4402 /* Skip over old file's replaced lines. */
4403 while (!feof(f1) && *line_cur1 <= end_old) {
4405 err = copy_one_line(f1, NULL, rejectfile);
4407 err = skip_one_line(f1);
4416 static const struct got_error *
4417 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4418 FILE *outfile, FILE *rejectfile)
4420 const struct got_error *err;
4423 /* Copy old file's lines until EOF. */
4425 err = copy_one_line(f1, outfile, NULL);
4432 /* Copy new file's lines until EOF. */
4434 err = copy_one_line(f2, NULL, rejectfile);
4444 static const struct got_error *
4445 apply_or_reject_change(int *choice, int *nchunks_used,
4446 struct diff_result *diff_result, int n,
4447 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4448 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4449 got_worktree_patch_cb patch_cb, void *patch_arg)
4451 const struct got_error *err = NULL;
4452 struct diff_chunk_context cc = {};
4453 int start_old, end_old, start_new, end_new;
4455 struct diff_output_unidiff_state *diff_state;
4456 struct diff_input_info diff_info;
4459 *choice = GOT_PATCH_CHOICE_NONE;
4461 /* Get changed line numbers without context lines for copy_change(). */
4462 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4463 start_old = cc.left.start;
4464 end_old = cc.left.end;
4465 start_new = cc.right.start;
4466 end_new = cc.right.end;
4468 /* Get the same change with context lines for display. */
4469 memset(&cc, 0, sizeof(cc));
4470 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4472 memset(&diff_info, 0, sizeof(diff_info));
4473 diff_info.left_path = relpath;
4474 diff_info.right_path = relpath;
4476 diff_state = diff_output_unidiff_state_alloc();
4477 if (diff_state == NULL)
4478 return got_error_set_errno(ENOMEM,
4479 "diff_output_unidiff_state_alloc");
4481 hunkfile = got_opentemp();
4482 if (hunkfile == NULL) {
4483 err = got_error_from_errno("got_opentemp");
4487 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4489 if (rc != DIFF_RC_OK) {
4490 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4494 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4495 err = got_ferror(hunkfile, GOT_ERR_IO);
4499 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4500 hunkfile, changeno, nchanges);
4505 case GOT_PATCH_CHOICE_YES:
4506 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4507 end_old, start_new, end_new, outfile, rejectfile);
4509 case GOT_PATCH_CHOICE_NO:
4510 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4511 end_old, start_new, end_new, rejectfile, outfile);
4513 case GOT_PATCH_CHOICE_QUIT:
4516 err = got_error(GOT_ERR_PATCH_CHOICE);
4520 diff_output_unidiff_state_free(diff_state);
4521 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4522 err = got_error_from_errno("fclose");
4526 struct revert_file_args {
4527 struct got_worktree *worktree;
4528 struct got_fileindex *fileindex;
4529 got_worktree_checkout_cb progress_cb;
4531 got_worktree_patch_cb patch_cb;
4533 struct got_repository *repo;
4534 int unlink_added_files;
4537 static const struct got_error *
4538 create_patched_content(char **path_outfile, int reverse_patch,
4539 struct got_object_id *blob_id, const char *path2,
4540 int dirfd2, const char *de_name2,
4541 const char *relpath, struct got_repository *repo,
4542 got_worktree_patch_cb patch_cb, void *patch_arg)
4544 const struct got_error *err, *free_err;
4545 struct got_blob_object *blob = NULL;
4546 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4547 int fd = -1, fd2 = -1;
4548 char link_target[PATH_MAX];
4549 ssize_t link_len = 0;
4550 char *path1 = NULL, *id_str = NULL;
4552 struct got_diffreg_result *diffreg_result = NULL;
4553 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4554 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4556 *path_outfile = NULL;
4558 err = got_object_id_str(&id_str, blob_id);
4563 fd2 = openat(dirfd2, de_name2,
4564 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4566 if (!got_err_open_nofollow_on_symlink()) {
4567 err = got_error_from_errno2("openat", path2);
4570 link_len = readlinkat(dirfd2, de_name2,
4571 link_target, sizeof(link_target));
4572 if (link_len == -1) {
4573 return got_error_from_errno2("readlinkat",
4576 sb2.st_mode = S_IFLNK;
4577 sb2.st_size = link_len;
4580 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4582 if (!got_err_open_nofollow_on_symlink()) {
4583 err = got_error_from_errno2("open", path2);
4586 link_len = readlink(path2, link_target,
4587 sizeof(link_target));
4589 return got_error_from_errno2("readlink", path2);
4590 sb2.st_mode = S_IFLNK;
4591 sb2.st_size = link_len;
4595 if (fstat(fd2, &sb2) == -1) {
4596 err = got_error_from_errno2("fstat", path2);
4600 f2 = fdopen(fd2, "r");
4602 err = got_error_from_errno2("fdopen", path2);
4608 f2 = got_opentemp();
4610 err = got_error_from_errno2("got_opentemp", path2);
4613 n = fwrite(link_target, 1, link_len, f2);
4614 if (n != link_len) {
4615 err = got_ferror(f2, GOT_ERR_IO);
4618 if (fflush(f2) == EOF) {
4619 err = got_error_from_errno("fflush");
4625 fd = got_opentempfd();
4627 err = got_error_from_errno("got_opentempfd");
4631 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4635 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4639 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4643 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4644 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4648 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4653 if (fseek(f1, 0L, SEEK_SET) == -1)
4654 return got_ferror(f1, GOT_ERR_IO);
4655 if (fseek(f2, 0L, SEEK_SET) == -1)
4656 return got_ferror(f2, GOT_ERR_IO);
4658 /* Count the number of actual changes in the diff result. */
4659 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4660 struct diff_chunk_context cc = {};
4661 diff_chunk_context_load_change(&cc, &nchunks_used,
4662 diffreg_result->result, n, 0);
4665 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4667 err = apply_or_reject_change(&choice, &nchunks_used,
4668 diffreg_result->result, n, relpath, f1, f2,
4669 &line_cur1, &line_cur2,
4670 reverse_patch ? NULL : outfile,
4671 reverse_patch ? outfile : NULL,
4672 ++i, nchanges, patch_cb, patch_arg);
4675 if (choice == GOT_PATCH_CHOICE_YES)
4677 else if (choice == GOT_PATCH_CHOICE_QUIT)
4681 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4682 reverse_patch ? NULL : outfile,
4683 reverse_patch ? outfile : NULL);
4687 if (!S_ISLNK(sb2.st_mode)) {
4690 mode = apply_umask(sb2.st_mode);
4691 if (fchmod(fileno(outfile), mode) == -1) {
4692 err = got_error_from_errno2("fchmod", path2);
4699 if (fd != -1 && close(fd) == -1 && err == NULL)
4700 err = got_error_from_errno("close");
4702 got_object_blob_close(blob);
4703 free_err = got_diffreg_result_free(diffreg_result);
4706 if (f1 && fclose(f1) == EOF && err == NULL)
4707 err = got_error_from_errno2("fclose", path1);
4708 if (f2 && fclose(f2) == EOF && err == NULL)
4709 err = got_error_from_errno2("fclose", path2);
4710 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4711 err = got_error_from_errno2("close", path2);
4712 if (outfile && fclose(outfile) == EOF && err == NULL)
4713 err = got_error_from_errno2("fclose", *path_outfile);
4714 if (path1 && unlink(path1) == -1 && err == NULL)
4715 err = got_error_from_errno2("unlink", path1);
4716 if (err || !have_content) {
4717 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4718 err = got_error_from_errno2("unlink", *path_outfile);
4719 free(*path_outfile);
4720 *path_outfile = NULL;
4726 static const struct got_error *
4727 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4728 const char *relpath, struct got_object_id *blob_id,
4729 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4730 int dirfd, const char *de_name)
4732 struct revert_file_args *a = arg;
4733 const struct got_error *err = NULL;
4734 char *parent_path = NULL;
4735 struct got_fileindex_entry *ie;
4736 struct got_commit_object *base_commit = NULL;
4737 struct got_tree_object *tree = NULL;
4738 struct got_object_id *tree_id = NULL;
4739 const struct got_tree_entry *te = NULL;
4740 char *tree_path = NULL, *te_name;
4741 char *ondisk_path = NULL, *path_content = NULL;
4742 struct got_blob_object *blob = NULL;
4745 /* Reverting a staged deletion is a no-op. */
4746 if (status == GOT_STATUS_DELETE &&
4747 staged_status != GOT_STATUS_NO_CHANGE)
4750 if (status == GOT_STATUS_UNVERSIONED)
4751 return (*a->progress_cb)(a->progress_arg,
4752 GOT_STATUS_UNVERSIONED, relpath);
4754 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4756 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4758 /* Construct in-repository path of tree which contains this blob. */
4759 err = got_path_dirname(&parent_path, ie->path);
4761 if (err->code != GOT_ERR_BAD_PATH)
4763 parent_path = strdup("/");
4764 if (parent_path == NULL) {
4765 err = got_error_from_errno("strdup");
4769 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4770 tree_path = strdup(parent_path);
4771 if (tree_path == NULL) {
4772 err = got_error_from_errno("strdup");
4776 if (got_path_is_root_dir(parent_path)) {
4777 tree_path = strdup(a->worktree->path_prefix);
4778 if (tree_path == NULL) {
4779 err = got_error_from_errno("strdup");
4783 if (asprintf(&tree_path, "%s/%s",
4784 a->worktree->path_prefix, parent_path) == -1) {
4785 err = got_error_from_errno("asprintf");
4791 err = got_object_open_as_commit(&base_commit, a->repo,
4792 a->worktree->base_commit_id);
4796 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4798 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4799 (status == GOT_STATUS_ADD ||
4800 staged_status == GOT_STATUS_ADD)))
4803 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4807 err = got_path_basename(&te_name, ie->path);
4811 te = got_object_tree_find_entry(tree, te_name);
4813 if (te == NULL && status != GOT_STATUS_ADD &&
4814 staged_status != GOT_STATUS_ADD) {
4815 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4821 case GOT_STATUS_ADD:
4823 int choice = GOT_PATCH_CHOICE_NONE;
4824 err = (*a->patch_cb)(&choice, a->patch_arg,
4825 status, ie->path, NULL, 1, 1);
4828 if (choice != GOT_PATCH_CHOICE_YES)
4831 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4835 got_fileindex_entry_remove(a->fileindex, ie);
4836 if (a->unlink_added_files) {
4837 if (asprintf(&ondisk_path, "%s/%s",
4838 got_worktree_get_root_path(a->worktree),
4840 err = got_error_from_errno("asprintf");
4843 if (unlink(ondisk_path) == -1) {
4844 err = got_error_from_errno2("unlink",
4850 case GOT_STATUS_DELETE:
4852 int choice = GOT_PATCH_CHOICE_NONE;
4853 err = (*a->patch_cb)(&choice, a->patch_arg,
4854 status, ie->path, NULL, 1, 1);
4857 if (choice != GOT_PATCH_CHOICE_YES)
4861 case GOT_STATUS_MODIFY:
4862 case GOT_STATUS_MODE_CHANGE:
4863 case GOT_STATUS_CONFLICT:
4864 case GOT_STATUS_MISSING: {
4865 struct got_object_id id;
4866 if (staged_status == GOT_STATUS_ADD ||
4867 staged_status == GOT_STATUS_MODIFY)
4868 got_fileindex_entry_get_staged_blob_id(&id, ie);
4870 got_fileindex_entry_get_blob_id(&id, ie);
4871 fd = got_opentempfd();
4873 err = got_error_from_errno("got_opentempfd");
4877 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4881 if (asprintf(&ondisk_path, "%s/%s",
4882 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4883 err = got_error_from_errno("asprintf");
4887 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4888 status == GOT_STATUS_CONFLICT)) {
4889 int is_bad_symlink = 0;
4890 err = create_patched_content(&path_content, 1, &id,
4891 ondisk_path, dirfd, de_name, ie->path, a->repo,
4892 a->patch_cb, a->patch_arg);
4893 if (err || path_content == NULL)
4895 if (te && S_ISLNK(te->mode)) {
4896 if (unlink(path_content) == -1) {
4897 err = got_error_from_errno2("unlink",
4901 err = install_symlink(&is_bad_symlink,
4902 a->worktree, ondisk_path, ie->path,
4903 blob, 0, 1, 0, 0, a->repo,
4904 a->progress_cb, a->progress_arg);
4906 if (rename(path_content, ondisk_path) == -1) {
4907 err = got_error_from_errno3("rename",
4908 path_content, ondisk_path);
4913 int is_bad_symlink = 0;
4914 if (te && S_ISLNK(te->mode)) {
4915 err = install_symlink(&is_bad_symlink,
4916 a->worktree, ondisk_path, ie->path,
4917 blob, 0, 1, 0, 0, a->repo,
4918 a->progress_cb, a->progress_arg);
4920 err = install_blob(a->worktree, ondisk_path,
4922 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4923 got_fileindex_perms_to_st(ie), blob,
4924 0, 1, 0, 0, a->repo,
4925 a->progress_cb, a->progress_arg);
4929 if (status == GOT_STATUS_DELETE ||
4930 status == GOT_STATUS_MODE_CHANGE) {
4931 err = got_fileindex_entry_update(ie,
4932 a->worktree->root_fd, relpath,
4934 a->worktree->base_commit_id->sha1, 1);
4938 if (is_bad_symlink) {
4939 got_fileindex_entry_filetype_set(ie,
4940 GOT_FILEIDX_MODE_BAD_SYMLINK);
4953 if (fd != -1 && close(fd) == -1 && err == NULL)
4954 err = got_error_from_errno("close");
4956 got_object_blob_close(blob);
4958 got_object_tree_close(tree);
4961 got_object_commit_close(base_commit);
4965 const struct got_error *
4966 got_worktree_revert(struct got_worktree *worktree,
4967 struct got_pathlist_head *paths,
4968 got_worktree_checkout_cb progress_cb, void *progress_arg,
4969 got_worktree_patch_cb patch_cb, void *patch_arg,
4970 struct got_repository *repo)
4972 struct got_fileindex *fileindex = NULL;
4973 char *fileindex_path = NULL;
4974 const struct got_error *err = NULL, *unlockerr = NULL;
4975 const struct got_error *sync_err = NULL;
4976 struct got_pathlist_entry *pe;
4977 struct revert_file_args rfa;
4979 err = lock_worktree(worktree, LOCK_EX);
4983 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4987 rfa.worktree = worktree;
4988 rfa.fileindex = fileindex;
4989 rfa.progress_cb = progress_cb;
4990 rfa.progress_arg = progress_arg;
4991 rfa.patch_cb = patch_cb;
4992 rfa.patch_arg = patch_arg;
4994 rfa.unlink_added_files = 0;
4995 TAILQ_FOREACH(pe, paths, entry) {
4996 err = worktree_status(worktree, pe->path, fileindex, repo,
4997 revert_file, &rfa, NULL, NULL, 1, 0);
5001 sync_err = sync_fileindex(fileindex, fileindex_path);
5002 if (sync_err && err == NULL)
5005 free(fileindex_path);
5007 got_fileindex_free(fileindex);
5008 unlockerr = lock_worktree(worktree, LOCK_SH);
5009 if (unlockerr && err == NULL)
5015 free_commitable(struct got_commitable *ct)
5018 free(ct->in_repo_path);
5019 free(ct->ondisk_path);
5021 free(ct->base_blob_id);
5022 free(ct->staged_blob_id);
5023 free(ct->base_commit_id);
5027 struct collect_commitables_arg {
5028 struct got_pathlist_head *commitable_paths;
5029 struct got_repository *repo;
5030 struct got_worktree *worktree;
5031 struct got_fileindex *fileindex;
5032 int have_staged_files;
5033 int allow_bad_symlinks;
5034 int diff_header_shown;
5035 int commit_conflicts;
5042 * Create a file which contains the target path of a symlink so we can feed
5043 * it as content to the diff engine.
5045 static const struct got_error *
5046 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5047 const char *abspath)
5049 const struct got_error *err = NULL;
5050 char target_path[PATH_MAX];
5051 ssize_t target_len, outlen;
5056 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5057 if (target_len == -1)
5058 return got_error_from_errno2("readlinkat", abspath);
5060 target_len = readlink(abspath, target_path, PATH_MAX);
5061 if (target_len == -1)
5062 return got_error_from_errno2("readlink", abspath);
5065 *fd = got_opentempfd();
5067 return got_error_from_errno("got_opentempfd");
5069 outlen = write(*fd, target_path, target_len);
5071 err = got_error_from_errno("got_opentempfd");
5075 if (lseek(*fd, 0, SEEK_SET) == -1) {
5076 err = got_error_from_errno2("lseek", abspath);
5087 static const struct got_error *
5088 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5089 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5090 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5092 const struct got_error *err = NULL;
5093 struct got_blob_object *blob1 = NULL;
5094 int fd = -1, fd1 = -1, fd2 = -1;
5095 FILE *ondisk_file = NULL;
5096 char *label1 = NULL;
5100 char *id_str = NULL;
5102 memset(&sb, 0, sizeof(sb));
5105 if (ct->staged_status != GOT_STATUS_MODIFY &&
5106 ct->staged_status != GOT_STATUS_ADD &&
5107 ct->staged_status != GOT_STATUS_DELETE)
5110 if (ct->status != GOT_STATUS_MODIFY &&
5111 ct->status != GOT_STATUS_ADD &&
5112 ct->status != GOT_STATUS_DELETE &&
5113 ct->status != GOT_STATUS_CONFLICT)
5117 err = got_opentemp_truncate(f1);
5119 return got_error_from_errno("got_opentemp_truncate");
5120 err = got_opentemp_truncate(f2);
5122 return got_error_from_errno("got_opentemp_truncate");
5124 if (!*diff_header_shown) {
5125 err = got_object_id_str(&id_str, worktree->base_commit_id);
5128 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5129 got_worktree_get_root_path(worktree));
5130 fprintf(diff_outfile, "commit - %s\n", id_str);
5131 fprintf(diff_outfile, "path + %s%s\n",
5132 got_worktree_get_root_path(worktree),
5133 diff_staged ? " (staged changes)" : "");
5134 *diff_header_shown = 1;
5138 const char *label1 = NULL, *label2 = NULL;
5139 switch (ct->staged_status) {
5140 case GOT_STATUS_MODIFY:
5144 case GOT_STATUS_ADD:
5147 case GOT_STATUS_DELETE:
5151 return got_error(GOT_ERR_FILE_STATUS);
5153 fd1 = got_opentempfd();
5155 err = got_error_from_errno("got_opentempfd");
5158 fd2 = got_opentempfd();
5160 err = got_error_from_errno("got_opentempfd");
5163 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5164 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5165 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5166 NULL, repo, diff_outfile);
5170 fd1 = got_opentempfd();
5172 err = got_error_from_errno("got_opentempfd");
5176 if (ct->status != GOT_STATUS_ADD) {
5177 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5183 if (ct->status != GOT_STATUS_DELETE) {
5185 fd = openat(dirfd, de_name,
5186 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5188 if (!got_err_open_nofollow_on_symlink()) {
5189 err = got_error_from_errno2("openat",
5193 err = get_symlink_target_file(&fd, dirfd,
5194 de_name, ct->ondisk_path);
5199 fd = open(ct->ondisk_path,
5200 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5202 if (!got_err_open_nofollow_on_symlink()) {
5203 err = got_error_from_errno2("open",
5207 err = get_symlink_target_file(&fd, dirfd,
5208 de_name, ct->ondisk_path);
5213 if (fstatat(fd, ct->ondisk_path, &sb,
5214 AT_SYMLINK_NOFOLLOW) == -1) {
5215 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5218 ondisk_file = fdopen(fd, "r");
5219 if (ondisk_file == NULL) {
5220 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5228 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5234 err = got_diff_blob_file(blob1, f1, size1, label1,
5235 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5236 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5238 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5239 err = got_error_from_errno("close");
5240 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5241 err = got_error_from_errno("close");
5243 got_object_blob_close(blob1);
5244 if (fd != -1 && close(fd) == -1 && err == NULL)
5245 err = got_error_from_errno("close");
5246 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5247 err = got_error_from_errno("fclose");
5251 static const struct got_error *
5252 collect_commitables(void *arg, unsigned char status,
5253 unsigned char staged_status, const char *relpath,
5254 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5255 struct got_object_id *commit_id, int dirfd, const char *de_name)
5257 struct collect_commitables_arg *a = arg;
5258 const struct got_error *err = NULL;
5259 struct got_commitable *ct = NULL;
5260 struct got_pathlist_entry *new = NULL;
5261 char *parent_path = NULL, *path = NULL;
5264 if (a->have_staged_files) {
5265 if (staged_status != GOT_STATUS_MODIFY &&
5266 staged_status != GOT_STATUS_ADD &&
5267 staged_status != GOT_STATUS_DELETE)
5270 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5271 printf("C %s\n", relpath);
5272 return got_error(GOT_ERR_COMMIT_CONFLICT);
5275 if (status != GOT_STATUS_MODIFY &&
5276 status != GOT_STATUS_MODE_CHANGE &&
5277 status != GOT_STATUS_ADD &&
5278 status != GOT_STATUS_DELETE &&
5279 status != GOT_STATUS_CONFLICT)
5283 if (asprintf(&path, "/%s", relpath) == -1) {
5284 err = got_error_from_errno("asprintf");
5287 if (strcmp(path, "/") == 0) {
5288 parent_path = strdup("");
5289 if (parent_path == NULL)
5290 return got_error_from_errno("strdup");
5292 err = got_path_dirname(&parent_path, path);
5297 ct = calloc(1, sizeof(*ct));
5299 err = got_error_from_errno("calloc");
5303 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5305 err = got_error_from_errno("asprintf");
5309 if (staged_status == GOT_STATUS_ADD ||
5310 staged_status == GOT_STATUS_MODIFY) {
5311 struct got_fileindex_entry *ie;
5312 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5313 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5314 case GOT_FILEIDX_MODE_REGULAR_FILE:
5315 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5318 case GOT_FILEIDX_MODE_SYMLINK:
5322 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5325 ct->mode |= got_fileindex_entry_perms_get(ie);
5326 } else if (status != GOT_STATUS_DELETE &&
5327 staged_status != GOT_STATUS_DELETE) {
5329 if (fstatat(dirfd, de_name, &sb,
5330 AT_SYMLINK_NOFOLLOW) == -1) {
5331 err = got_error_from_errno2("fstatat",
5335 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5336 err = got_error_from_errno2("lstat", ct->ondisk_path);
5339 ct->mode = sb.st_mode;
5342 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5343 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5345 err = got_error_from_errno("asprintf");
5349 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5350 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5352 char target_path[PATH_MAX];
5354 target_len = readlink(ct->ondisk_path, target_path,
5355 sizeof(target_path));
5356 if (target_len == -1) {
5357 err = got_error_from_errno2("readlink",
5361 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5362 target_len, ct->ondisk_path, a->worktree->root_path);
5365 if (is_bad_symlink) {
5366 err = got_error_path(ct->ondisk_path,
5367 GOT_ERR_BAD_SYMLINK);
5373 ct->status = status;
5374 ct->staged_status = staged_status;
5375 ct->blob_id = NULL; /* will be filled in when blob gets created */
5376 if (ct->status != GOT_STATUS_ADD &&
5377 ct->staged_status != GOT_STATUS_ADD) {
5378 ct->base_blob_id = got_object_id_dup(blob_id);
5379 if (ct->base_blob_id == NULL) {
5380 err = got_error_from_errno("got_object_id_dup");
5383 ct->base_commit_id = got_object_id_dup(commit_id);
5384 if (ct->base_commit_id == NULL) {
5385 err = got_error_from_errno("got_object_id_dup");
5389 if (ct->staged_status == GOT_STATUS_ADD ||
5390 ct->staged_status == GOT_STATUS_MODIFY) {
5391 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5392 if (ct->staged_blob_id == NULL) {
5393 err = got_error_from_errno("got_object_id_dup");
5397 ct->path = strdup(path);
5398 if (ct->path == NULL) {
5399 err = got_error_from_errno("strdup");
5402 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5406 if (a->diff_outfile && ct && new != NULL) {
5407 err = append_ct_diff(ct, &a->diff_header_shown,
5408 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5409 a->have_staged_files, a->repo, a->worktree);
5414 if (ct && (err || new == NULL))
5415 free_commitable(ct);
5421 static const struct got_error *write_tree(struct got_object_id **, int *,
5422 struct got_tree_object *, const char *, struct got_pathlist_head *,
5423 got_worktree_status_cb status_cb, void *status_arg,
5424 struct got_repository *);
5426 static const struct got_error *
5427 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5428 struct got_tree_entry *te, const char *parent_path,
5429 struct got_pathlist_head *commitable_paths,
5430 got_worktree_status_cb status_cb, void *status_arg,
5431 struct got_repository *repo)
5433 const struct got_error *err = NULL;
5434 struct got_tree_object *subtree;
5437 if (asprintf(&subpath, "%s%s%s", parent_path,
5438 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5439 return got_error_from_errno("asprintf");
5441 err = got_object_open_as_tree(&subtree, repo, &te->id);
5445 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5446 commitable_paths, status_cb, status_arg, repo);
5447 got_object_tree_close(subtree);
5452 static const struct got_error *
5453 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5455 const struct got_error *err = NULL;
5456 char *ct_parent_path = NULL;
5460 if (strchr(ct->in_repo_path, '/') == NULL) {
5461 *match = got_path_is_root_dir(path);
5465 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5468 *match = (strcmp(path, ct_parent_path) == 0);
5469 free(ct_parent_path);
5474 get_ct_file_mode(struct got_commitable *ct)
5476 if (S_ISLNK(ct->mode))
5479 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5482 static const struct got_error *
5483 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5484 struct got_tree_entry *te, struct got_commitable *ct)
5486 const struct got_error *err = NULL;
5490 err = got_object_tree_entry_dup(new_te, te);
5494 (*new_te)->mode = get_ct_file_mode(ct);
5496 if (ct->staged_status == GOT_STATUS_MODIFY)
5497 memcpy(&(*new_te)->id, ct->staged_blob_id,
5498 sizeof((*new_te)->id));
5500 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5502 if (err && *new_te) {
5509 static const struct got_error *
5510 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5511 struct got_commitable *ct)
5513 const struct got_error *err = NULL;
5514 char *ct_name = NULL;
5518 *new_te = calloc(1, sizeof(**new_te));
5519 if (*new_te == NULL)
5520 return got_error_from_errno("calloc");
5522 err = got_path_basename(&ct_name, ct->path);
5525 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5526 sizeof((*new_te)->name)) {
5527 err = got_error(GOT_ERR_NO_SPACE);
5531 (*new_te)->mode = get_ct_file_mode(ct);
5533 if (ct->staged_status == GOT_STATUS_ADD)
5534 memcpy(&(*new_te)->id, ct->staged_blob_id,
5535 sizeof((*new_te)->id));
5537 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5540 if (err && *new_te) {
5547 static const struct got_error *
5548 insert_tree_entry(struct got_tree_entry *new_te,
5549 struct got_pathlist_head *paths)
5551 const struct got_error *err = NULL;
5552 struct got_pathlist_entry *new_pe;
5554 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5558 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5562 static const struct got_error *
5563 report_ct_status(struct got_commitable *ct,
5564 got_worktree_status_cb status_cb, void *status_arg)
5566 const char *ct_path = ct->path;
5567 unsigned char status;
5569 if (status_cb == NULL) /* no commit progress output desired */
5572 while (ct_path[0] == '/')
5575 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5576 status = ct->staged_status;
5578 status = ct->status;
5580 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5581 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5584 static const struct got_error *
5585 match_modified_subtree(int *modified, struct got_tree_entry *te,
5586 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5588 const struct got_error *err = NULL;
5589 struct got_pathlist_entry *pe;
5594 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5595 got_path_is_root_dir(base_tree_path) ? "" : "/",
5597 return got_error_from_errno("asprintf");
5599 TAILQ_FOREACH(pe, commitable_paths, entry) {
5600 struct got_commitable *ct = pe->data;
5601 *modified = got_path_is_child(ct->in_repo_path, te_path,
5611 static const struct got_error *
5612 match_deleted_or_modified_ct(struct got_commitable **ctp,
5613 struct got_tree_entry *te, const char *base_tree_path,
5614 struct got_pathlist_head *commitable_paths)
5616 const struct got_error *err = NULL;
5617 struct got_pathlist_entry *pe;
5621 TAILQ_FOREACH(pe, commitable_paths, entry) {
5622 struct got_commitable *ct = pe->data;
5623 char *ct_name = NULL;
5626 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5627 if (ct->status != GOT_STATUS_MODIFY &&
5628 ct->status != GOT_STATUS_MODE_CHANGE &&
5629 ct->status != GOT_STATUS_DELETE &&
5630 ct->status != GOT_STATUS_CONFLICT)
5633 if (ct->staged_status != GOT_STATUS_MODIFY &&
5634 ct->staged_status != GOT_STATUS_DELETE)
5638 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5641 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5647 err = got_path_basename(&ct_name, pe->path);
5651 if (strcmp(te->name, ct_name) != 0) {
5664 static const struct got_error *
5665 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5666 const char *child_path, const char *path_base_tree,
5667 struct got_pathlist_head *commitable_paths,
5668 got_worktree_status_cb status_cb, void *status_arg,
5669 struct got_repository *repo)
5671 const struct got_error *err = NULL;
5672 struct got_tree_entry *new_te;
5674 struct got_object_id *id = NULL;
5679 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5680 got_path_is_root_dir(path_base_tree) ? "" : "/",
5682 return got_error_from_errno("asprintf");
5684 new_te = calloc(1, sizeof(*new_te));
5686 return got_error_from_errno("calloc");
5687 new_te->mode = S_IFDIR;
5689 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5690 sizeof(new_te->name)) {
5691 err = got_error(GOT_ERR_NO_SPACE);
5694 err = write_tree(&id, &nentries, NULL, subtree_path,
5695 commitable_paths, status_cb, status_arg, repo);
5700 memcpy(&new_te->id, id, sizeof(new_te->id));
5709 static const struct got_error *
5710 write_tree(struct got_object_id **new_tree_id, int *nentries,
5711 struct got_tree_object *base_tree, const char *path_base_tree,
5712 struct got_pathlist_head *commitable_paths,
5713 got_worktree_status_cb status_cb, void *status_arg,
5714 struct got_repository *repo)
5716 const struct got_error *err = NULL;
5717 struct got_pathlist_head paths;
5718 struct got_tree_entry *te, *new_te = NULL;
5719 struct got_pathlist_entry *pe;
5724 /* Insert, and recurse into, newly added entries first. */
5725 TAILQ_FOREACH(pe, commitable_paths, entry) {
5726 struct got_commitable *ct = pe->data;
5727 char *child_path = NULL, *slash;
5729 if ((ct->status != GOT_STATUS_ADD &&
5730 ct->staged_status != GOT_STATUS_ADD) ||
5731 (ct->flags & GOT_COMMITABLE_ADDED))
5734 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5735 strlen(path_base_tree)))
5738 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5743 slash = strchr(child_path, '/');
5744 if (slash == NULL) {
5745 err = alloc_added_blob_tree_entry(&new_te, ct);
5748 err = report_ct_status(ct, status_cb, status_arg);
5751 ct->flags |= GOT_COMMITABLE_ADDED;
5752 err = insert_tree_entry(new_te, &paths);
5757 *slash = '\0'; /* trim trailing path components */
5758 if (base_tree == NULL ||
5759 got_object_tree_find_entry(base_tree, child_path)
5761 err = make_subtree_for_added_blob(&new_te,
5762 child_path, path_base_tree,
5763 commitable_paths, status_cb, status_arg,
5767 err = insert_tree_entry(new_te, &paths);
5776 int i, nbase_entries;
5777 /* Handle modified and deleted entries. */
5778 nbase_entries = got_object_tree_get_nentries(base_tree);
5779 for (i = 0; i < nbase_entries; i++) {
5780 struct got_commitable *ct = NULL;
5782 te = got_object_tree_get_entry(base_tree, i);
5783 if (got_object_tree_entry_is_submodule(te)) {
5784 /* Entry is a submodule; just copy it. */
5785 err = got_object_tree_entry_dup(&new_te, te);
5788 err = insert_tree_entry(new_te, &paths);
5795 if (S_ISDIR(te->mode)) {
5797 err = got_object_tree_entry_dup(&new_te, te);
5800 err = match_modified_subtree(&modified, te,
5801 path_base_tree, commitable_paths);
5804 /* Avoid recursion into unmodified subtrees. */
5806 struct got_object_id *new_id;
5808 err = write_subtree(&new_id,
5810 path_base_tree, commitable_paths,
5811 status_cb, status_arg, repo);
5814 if (nsubentries == 0) {
5815 /* All entries were deleted. */
5819 memcpy(&new_te->id, new_id,
5820 sizeof(new_te->id));
5823 err = insert_tree_entry(new_te, &paths);
5830 err = match_deleted_or_modified_ct(&ct, te,
5831 path_base_tree, commitable_paths);
5835 /* NB: Deleted entries get dropped here. */
5836 if (ct->status == GOT_STATUS_MODIFY ||
5837 ct->status == GOT_STATUS_MODE_CHANGE ||
5838 ct->status == GOT_STATUS_CONFLICT ||
5839 ct->staged_status == GOT_STATUS_MODIFY) {
5840 err = alloc_modified_blob_tree_entry(
5844 err = insert_tree_entry(new_te, &paths);
5849 err = report_ct_status(ct, status_cb,
5854 /* Entry is unchanged; just copy it. */
5855 err = got_object_tree_entry_dup(&new_te, te);
5858 err = insert_tree_entry(new_te, &paths);
5866 /* Write new list of entries; deleted entries have been dropped. */
5867 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5869 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5873 static const struct got_error *
5874 update_fileindex_after_commit(struct got_worktree *worktree,
5875 struct got_pathlist_head *commitable_paths,
5876 struct got_object_id *new_base_commit_id,
5877 struct got_fileindex *fileindex, int have_staged_files)
5879 const struct got_error *err = NULL;
5880 struct got_pathlist_entry *pe;
5881 char *relpath = NULL;
5883 TAILQ_FOREACH(pe, commitable_paths, entry) {
5884 struct got_fileindex_entry *ie;
5885 struct got_commitable *ct = pe->data;
5887 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5889 err = got_path_skip_common_ancestor(&relpath,
5890 worktree->root_path, ct->ondisk_path);
5895 if (ct->status == GOT_STATUS_DELETE ||
5896 ct->staged_status == GOT_STATUS_DELETE) {
5897 got_fileindex_entry_remove(fileindex, ie);
5898 } else if (ct->staged_status == GOT_STATUS_ADD ||
5899 ct->staged_status == GOT_STATUS_MODIFY) {
5900 got_fileindex_entry_stage_set(ie,
5901 GOT_FILEIDX_STAGE_NONE);
5902 got_fileindex_entry_staged_filetype_set(ie, 0);
5904 err = got_fileindex_entry_update(ie,
5905 worktree->root_fd, relpath,
5906 ct->staged_blob_id->sha1,
5907 new_base_commit_id->sha1,
5908 !have_staged_files);
5910 err = got_fileindex_entry_update(ie,
5911 worktree->root_fd, relpath,
5913 new_base_commit_id->sha1,
5914 !have_staged_files);
5916 err = got_fileindex_entry_alloc(&ie, pe->path);
5919 err = got_fileindex_entry_update(ie,
5920 worktree->root_fd, relpath, ct->blob_id->sha1,
5921 new_base_commit_id->sha1, 1);
5923 got_fileindex_entry_free(ie);
5926 err = got_fileindex_entry_add(fileindex, ie);
5928 got_fileindex_entry_free(ie);
5941 static const struct got_error *
5942 check_out_of_date(const char *in_repo_path, unsigned char status,
5943 unsigned char staged_status, struct got_object_id *base_blob_id,
5944 struct got_object_id *base_commit_id,
5945 struct got_object_id *head_commit_id, struct got_repository *repo,
5948 const struct got_error *err = NULL;
5949 struct got_commit_object *commit = NULL;
5950 struct got_object_id *id = NULL;
5952 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5953 /* Trivial case: base commit == head commit */
5954 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5957 * Ensure file content which local changes were based
5958 * on matches file content in the branch head.
5960 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5963 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5965 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5966 err = got_error(ood_errcode);
5968 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5969 err = got_error(ood_errcode);
5971 /* Require that added files don't exist in the branch head. */
5972 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5975 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5976 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5978 err = id ? got_error(ood_errcode) : NULL;
5983 got_object_commit_close(commit);
5987 static const struct got_error *
5988 commit_worktree(struct got_object_id **new_commit_id,
5989 struct got_pathlist_head *commitable_paths,
5990 struct got_object_id *head_commit_id,
5991 struct got_object_id *parent_id2,
5992 struct got_worktree *worktree,
5993 const char *author, const char *committer, char *diff_path,
5994 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5995 got_worktree_status_cb status_cb, void *status_arg,
5996 struct got_repository *repo)
5998 const struct got_error *err = NULL, *unlockerr = NULL;
5999 struct got_pathlist_entry *pe;
6000 const char *head_ref_name = NULL;
6001 struct got_commit_object *head_commit = NULL;
6002 struct got_reference *head_ref2 = NULL;
6003 struct got_object_id *head_commit_id2 = NULL;
6004 struct got_tree_object *head_tree = NULL;
6005 struct got_object_id *new_tree_id = NULL;
6006 int nentries, nparents = 0;
6007 struct got_object_id_queue parent_ids;
6008 struct got_object_qid *pid = NULL;
6009 char *logmsg = NULL;
6012 *new_commit_id = NULL;
6014 STAILQ_INIT(&parent_ids);
6016 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6020 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6024 if (commit_msg_cb != NULL) {
6025 err = commit_msg_cb(commitable_paths, diff_path,
6026 &logmsg, commit_arg);
6031 if (logmsg == NULL || strlen(logmsg) == 0) {
6032 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6036 /* Create blobs from added and modified files and record their IDs. */
6037 TAILQ_FOREACH(pe, commitable_paths, entry) {
6038 struct got_commitable *ct = pe->data;
6041 /* Blobs for staged files already exist. */
6042 if (ct->staged_status == GOT_STATUS_ADD ||
6043 ct->staged_status == GOT_STATUS_MODIFY)
6046 if (ct->status != GOT_STATUS_ADD &&
6047 ct->status != GOT_STATUS_MODIFY &&
6048 ct->status != GOT_STATUS_MODE_CHANGE &&
6049 ct->status != GOT_STATUS_CONFLICT)
6052 if (asprintf(&ondisk_path, "%s/%s",
6053 worktree->root_path, pe->path) == -1) {
6054 err = got_error_from_errno("asprintf");
6057 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6063 /* Recursively write new tree objects. */
6064 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6065 commitable_paths, status_cb, status_arg, repo);
6069 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6072 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6075 err = got_object_qid_alloc(&pid, parent_id2);
6078 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6081 timestamp = time(NULL);
6082 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6083 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6089 /* Check if a concurrent commit to our branch has occurred. */
6090 head_ref_name = got_worktree_get_head_ref_name(worktree);
6091 if (head_ref_name == NULL) {
6092 err = got_error_from_errno("got_worktree_get_head_ref_name");
6095 /* Lock the reference here to prevent concurrent modification. */
6096 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6099 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6102 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6103 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6106 /* Update branch head in repository. */
6107 err = got_ref_change_ref(head_ref2, *new_commit_id);
6110 err = got_ref_write(head_ref2, repo);
6114 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6118 err = ref_base_commit(worktree, repo);
6122 got_object_id_queue_free(&parent_ids);
6124 got_object_tree_close(head_tree);
6126 got_object_commit_close(head_commit);
6127 free(head_commit_id2);
6129 unlockerr = got_ref_unlock(head_ref2);
6130 if (unlockerr && err == NULL)
6132 got_ref_close(head_ref2);
6137 static const struct got_error *
6138 check_path_is_commitable(const char *path,
6139 struct got_pathlist_head *commitable_paths)
6141 struct got_pathlist_entry *cpe = NULL;
6142 size_t path_len = strlen(path);
6144 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6145 struct got_commitable *ct = cpe->data;
6146 const char *ct_path = ct->path;
6148 while (ct_path[0] == '/')
6151 if (strcmp(path, ct_path) == 0 ||
6152 got_path_is_child(ct_path, path, path_len))
6157 return got_error_path(path, GOT_ERR_BAD_PATH);
6162 static const struct got_error *
6163 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6165 int *have_staged_files = arg;
6167 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6168 *have_staged_files = 1;
6169 return got_error(GOT_ERR_CANCELLED);
6175 static const struct got_error *
6176 check_non_staged_files(struct got_fileindex *fileindex,
6177 struct got_pathlist_head *paths)
6179 struct got_pathlist_entry *pe;
6180 struct got_fileindex_entry *ie;
6182 TAILQ_FOREACH(pe, paths, entry) {
6183 if (pe->path[0] == '\0')
6185 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6187 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6188 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6189 return got_error_path(pe->path,
6190 GOT_ERR_FILE_NOT_STAGED);
6196 const struct got_error *
6197 got_worktree_commit(struct got_object_id **new_commit_id,
6198 struct got_worktree *worktree, struct got_pathlist_head *paths,
6199 const char *author, const char *committer, int allow_bad_symlinks,
6200 int show_diff, int commit_conflicts,
6201 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6202 got_worktree_status_cb status_cb, void *status_arg,
6203 struct got_repository *repo)
6205 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6206 struct got_fileindex *fileindex = NULL;
6207 char *fileindex_path = NULL;
6208 struct got_pathlist_head commitable_paths;
6209 struct collect_commitables_arg cc_arg;
6210 struct got_pathlist_entry *pe;
6211 struct got_reference *head_ref = NULL;
6212 struct got_object_id *head_commit_id = NULL;
6213 char *diff_path = NULL;
6214 int have_staged_files = 0;
6216 *new_commit_id = NULL;
6218 memset(&cc_arg, 0, sizeof(cc_arg));
6219 TAILQ_INIT(&commitable_paths);
6221 err = lock_worktree(worktree, LOCK_EX);
6225 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6229 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6233 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6237 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6238 &have_staged_files);
6239 if (err && err->code != GOT_ERR_CANCELLED)
6241 if (have_staged_files) {
6242 err = check_non_staged_files(fileindex, paths);
6247 cc_arg.commitable_paths = &commitable_paths;
6248 cc_arg.worktree = worktree;
6249 cc_arg.fileindex = fileindex;
6251 cc_arg.have_staged_files = have_staged_files;
6252 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6253 cc_arg.diff_header_shown = 0;
6254 cc_arg.commit_conflicts = commit_conflicts;
6256 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6257 GOT_TMPDIR_STR "/got", ".diff");
6260 cc_arg.f1 = got_opentemp();
6261 if (cc_arg.f1 == NULL) {
6262 err = got_error_from_errno("got_opentemp");
6265 cc_arg.f2 = got_opentemp();
6266 if (cc_arg.f2 == NULL) {
6267 err = got_error_from_errno("got_opentemp");
6272 TAILQ_FOREACH(pe, paths, entry) {
6273 err = worktree_status(worktree, pe->path, fileindex, repo,
6274 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6280 if (fflush(cc_arg.diff_outfile) == EOF) {
6281 err = got_error_from_errno("fflush");
6286 if (TAILQ_EMPTY(&commitable_paths)) {
6287 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6291 TAILQ_FOREACH(pe, paths, entry) {
6292 err = check_path_is_commitable(pe->path, &commitable_paths);
6297 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6298 struct got_commitable *ct = pe->data;
6299 const char *ct_path = ct->in_repo_path;
6301 while (ct_path[0] == '/')
6303 err = check_out_of_date(ct_path, ct->status,
6304 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6305 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6311 err = commit_worktree(new_commit_id, &commitable_paths,
6312 head_commit_id, NULL, worktree, author, committer,
6313 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6314 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6318 err = update_fileindex_after_commit(worktree, &commitable_paths,
6319 *new_commit_id, fileindex, have_staged_files);
6320 sync_err = sync_fileindex(fileindex, fileindex_path);
6321 if (sync_err && err == NULL)
6325 got_fileindex_free(fileindex);
6326 free(fileindex_path);
6327 unlockerr = lock_worktree(worktree, LOCK_SH);
6328 if (unlockerr && err == NULL)
6330 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6331 struct got_commitable *ct = pe->data;
6333 free_commitable(ct);
6335 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6336 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6337 err = got_error_from_errno2("unlink", diff_path);
6339 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6341 err = got_error_from_errno("fclose");
6346 got_commitable_get_path(struct got_commitable *ct)
6352 got_commitable_get_status(struct got_commitable *ct)
6357 struct check_rebase_ok_arg {
6358 struct got_worktree *worktree;
6359 struct got_repository *repo;
6362 static const struct got_error *
6363 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6365 const struct got_error *err = NULL;
6366 struct check_rebase_ok_arg *a = arg;
6367 unsigned char status;
6371 /* Reject rebase of a work tree with mixed base commits. */
6372 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6373 SHA1_DIGEST_LENGTH))
6374 return got_error(GOT_ERR_MIXED_COMMITS);
6376 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6378 return got_error_from_errno("asprintf");
6380 /* Reject rebase of a work tree with modified or staged files. */
6381 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6386 if (status != GOT_STATUS_NO_CHANGE)
6387 return got_error(GOT_ERR_MODIFIED);
6388 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6389 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6394 const struct got_error *
6395 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6396 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6397 struct got_worktree *worktree, struct got_reference *branch,
6398 struct got_repository *repo)
6400 const struct got_error *err = NULL;
6401 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6402 char *branch_ref_name = NULL;
6403 char *fileindex_path = NULL;
6404 struct check_rebase_ok_arg ok_arg;
6405 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6406 struct got_object_id *wt_branch_tip = NULL;
6408 *new_base_branch_ref = NULL;
6412 err = lock_worktree(worktree, LOCK_EX);
6416 err = open_fileindex(fileindex, &fileindex_path, worktree);
6420 ok_arg.worktree = worktree;
6422 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6427 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6431 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6435 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6439 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6444 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6447 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6448 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6452 err = got_ref_alloc_symref(new_base_branch_ref,
6453 new_base_branch_ref_name, wt_branch);
6456 err = got_ref_write(*new_base_branch_ref, repo);
6460 /* TODO Lock original branch's ref while rebasing? */
6462 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6466 err = got_ref_write(branch_ref, repo);
6470 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6471 worktree->base_commit_id);
6474 err = got_ref_write(*tmp_branch, repo);
6478 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6482 free(fileindex_path);
6483 free(tmp_branch_name);
6484 free(new_base_branch_ref_name);
6485 free(branch_ref_name);
6487 got_ref_close(branch_ref);
6489 got_ref_close(wt_branch);
6490 free(wt_branch_tip);
6492 if (*new_base_branch_ref) {
6493 got_ref_close(*new_base_branch_ref);
6494 *new_base_branch_ref = NULL;
6497 got_ref_close(*tmp_branch);
6501 got_fileindex_free(*fileindex);
6504 lock_worktree(worktree, LOCK_SH);
6509 const struct got_error *
6510 got_worktree_rebase_continue(struct got_object_id **commit_id,
6511 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6512 struct got_reference **branch, struct got_fileindex **fileindex,
6513 struct got_worktree *worktree, struct got_repository *repo)
6515 const struct got_error *err;
6516 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6517 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6518 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6519 char *fileindex_path = NULL;
6520 int have_staged_files = 0;
6523 *new_base_branch = NULL;
6528 err = lock_worktree(worktree, LOCK_EX);
6532 err = open_fileindex(fileindex, &fileindex_path, worktree);
6536 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6537 &have_staged_files);
6538 if (err && err->code != GOT_ERR_CANCELLED)
6540 if (have_staged_files) {
6541 err = got_error(GOT_ERR_STAGED_PATHS);
6545 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6549 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6553 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6557 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6561 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6565 err = got_ref_open(branch, repo,
6566 got_ref_get_symref_target(branch_ref), 0);
6570 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6574 err = got_ref_resolve(commit_id, repo, commit_ref);
6578 err = got_ref_open(new_base_branch, repo,
6579 new_base_branch_ref_name, 0);
6583 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6587 free(commit_ref_name);
6588 free(branch_ref_name);
6589 free(fileindex_path);
6591 got_ref_close(commit_ref);
6593 got_ref_close(branch_ref);
6598 got_ref_close(*tmp_branch);
6601 if (*new_base_branch) {
6602 got_ref_close(*new_base_branch);
6603 *new_base_branch = NULL;
6606 got_ref_close(*branch);
6610 got_fileindex_free(*fileindex);
6613 lock_worktree(worktree, LOCK_SH);
6618 const struct got_error *
6619 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6621 const struct got_error *err;
6622 char *tmp_branch_name = NULL;
6624 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6628 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6629 free(tmp_branch_name);
6633 static const struct got_error *
6634 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6635 const char *diff_path, char **logmsg, void *arg)
6641 static const struct got_error *
6642 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6643 const char *path, struct got_object_id *blob_id,
6644 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6645 int dirfd, const char *de_name)
6650 struct collect_merged_paths_arg {
6651 got_worktree_checkout_cb progress_cb;
6653 struct got_pathlist_head *merged_paths;
6656 static const struct got_error *
6657 collect_merged_paths(void *arg, unsigned char status, const char *path)
6659 const struct got_error *err;
6660 struct collect_merged_paths_arg *a = arg;
6662 struct got_pathlist_entry *new;
6664 err = (*a->progress_cb)(a->progress_arg, status, path);
6668 if (status != GOT_STATUS_MERGE &&
6669 status != GOT_STATUS_ADD &&
6670 status != GOT_STATUS_DELETE &&
6671 status != GOT_STATUS_CONFLICT)
6676 return got_error_from_errno("strdup");
6678 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6679 if (err || new == NULL)
6684 static const struct got_error *
6685 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6686 int is_rebase, struct got_repository *repo)
6688 const struct got_error *err;
6689 struct got_reference *commit_ref = NULL;
6691 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6693 if (err->code != GOT_ERR_NOT_REF)
6695 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6698 err = got_ref_write(commit_ref, repo);
6701 } else if (is_rebase) {
6702 struct got_object_id *stored_id;
6705 err = got_ref_resolve(&stored_id, repo, commit_ref);
6708 cmp = got_object_id_cmp(commit_id, stored_id);
6711 err = got_error(GOT_ERR_REBASE_COMMITID);
6717 got_ref_close(commit_ref);
6721 static const struct got_error *
6722 rebase_merge_files(struct got_pathlist_head *merged_paths,
6723 const char *commit_ref_name, struct got_worktree *worktree,
6724 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6725 struct got_object_id *commit_id, struct got_repository *repo,
6726 got_worktree_checkout_cb progress_cb, void *progress_arg,
6727 got_cancel_cb cancel_cb, void *cancel_arg)
6729 const struct got_error *err;
6730 struct got_reference *commit_ref = NULL;
6731 struct collect_merged_paths_arg cmp_arg;
6732 char *fileindex_path;
6734 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6736 err = get_fileindex_path(&fileindex_path, worktree);
6740 cmp_arg.progress_cb = progress_cb;
6741 cmp_arg.progress_arg = progress_arg;
6742 cmp_arg.merged_paths = merged_paths;
6743 err = merge_files(worktree, fileindex, fileindex_path,
6744 parent_commit_id, commit_id, repo, collect_merged_paths,
6745 &cmp_arg, cancel_cb, cancel_arg);
6747 got_ref_close(commit_ref);
6751 const struct got_error *
6752 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6753 struct got_worktree *worktree, struct got_fileindex *fileindex,
6754 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6755 struct got_repository *repo,
6756 got_worktree_checkout_cb progress_cb, void *progress_arg,
6757 got_cancel_cb cancel_cb, void *cancel_arg)
6759 const struct got_error *err;
6760 char *commit_ref_name;
6762 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6766 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6770 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6771 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6772 progress_arg, cancel_cb, cancel_arg);
6774 free(commit_ref_name);
6778 const struct got_error *
6779 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6780 struct got_worktree *worktree, struct got_fileindex *fileindex,
6781 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6782 struct got_repository *repo,
6783 got_worktree_checkout_cb progress_cb, void *progress_arg,
6784 got_cancel_cb cancel_cb, void *cancel_arg)
6786 const struct got_error *err;
6787 char *commit_ref_name;
6789 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6793 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6797 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6798 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6799 progress_arg, cancel_cb, cancel_arg);
6801 free(commit_ref_name);
6805 static const struct got_error *
6806 rebase_commit(struct got_object_id **new_commit_id,
6807 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6808 struct got_worktree *worktree, struct got_fileindex *fileindex,
6809 struct got_reference *tmp_branch, const char *committer,
6810 struct got_commit_object *orig_commit, const char *new_logmsg,
6811 int allow_conflict, struct got_repository *repo)
6813 const struct got_error *err, *sync_err;
6814 struct got_pathlist_head commitable_paths;
6815 struct collect_commitables_arg cc_arg;
6816 char *fileindex_path = NULL;
6817 struct got_reference *head_ref = NULL;
6818 struct got_object_id *head_commit_id = NULL;
6819 char *logmsg = NULL;
6821 memset(&cc_arg, 0, sizeof(cc_arg));
6822 TAILQ_INIT(&commitable_paths);
6823 *new_commit_id = NULL;
6825 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6827 err = get_fileindex_path(&fileindex_path, worktree);
6831 cc_arg.commitable_paths = &commitable_paths;
6832 cc_arg.worktree = worktree;
6834 cc_arg.have_staged_files = 0;
6835 cc_arg.commit_conflicts = allow_conflict;
6837 * If possible get the status of individual files directly to
6838 * avoid crawling the entire work tree once per rebased commit.
6840 * Ideally, merged_paths would contain a list of commitables
6841 * we could use so we could skip worktree_status() entirely.
6842 * However, we would then need carefully keep track of cumulative
6843 * effects of operations such as file additions and deletions
6844 * in 'got histedit -f' (folding multiple commits into one),
6845 * and this extra complexity is not really worth it.
6848 struct got_pathlist_entry *pe;
6849 TAILQ_FOREACH(pe, merged_paths, entry) {
6850 err = worktree_status(worktree, pe->path, fileindex,
6851 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6857 err = worktree_status(worktree, "", fileindex, repo,
6858 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6863 if (TAILQ_EMPTY(&commitable_paths)) {
6864 /* No-op change; commit will be elided. */
6865 err = got_ref_delete(commit_ref, repo);
6868 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6872 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6876 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6881 logmsg = strdup(new_logmsg);
6882 if (logmsg == NULL) {
6883 err = got_error_from_errno("strdup");
6887 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6892 /* NB: commit_worktree will call free(logmsg) */
6893 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6894 NULL, worktree, got_object_commit_get_author(orig_commit),
6895 committer ? committer :
6896 got_object_commit_get_committer(orig_commit), NULL,
6897 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6901 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6905 err = got_ref_delete(commit_ref, repo);
6909 err = update_fileindex_after_commit(worktree, &commitable_paths,
6910 *new_commit_id, fileindex, 0);
6911 sync_err = sync_fileindex(fileindex, fileindex_path);
6912 if (sync_err && err == NULL)
6915 free(fileindex_path);
6916 free(head_commit_id);
6918 got_ref_close(head_ref);
6920 free(*new_commit_id);
6921 *new_commit_id = NULL;
6926 const struct got_error *
6927 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6928 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6929 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6930 const char *committer, struct got_commit_object *orig_commit,
6931 struct got_object_id *orig_commit_id, int allow_conflict,
6932 struct got_repository *repo)
6934 const struct got_error *err;
6935 char *commit_ref_name;
6936 struct got_reference *commit_ref = NULL;
6937 struct got_object_id *commit_id = NULL;
6939 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6943 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6946 err = got_ref_resolve(&commit_id, repo, commit_ref);
6949 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6950 err = got_error(GOT_ERR_REBASE_COMMITID);
6954 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6955 worktree, fileindex, tmp_branch, committer, orig_commit,
6956 NULL, allow_conflict, repo);
6959 got_ref_close(commit_ref);
6960 free(commit_ref_name);
6965 const struct got_error *
6966 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6967 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6968 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6969 const char *committer, struct got_commit_object *orig_commit,
6970 struct got_object_id *orig_commit_id, const char *new_logmsg,
6971 int allow_conflict, struct got_repository *repo)
6973 const struct got_error *err;
6974 char *commit_ref_name;
6975 struct got_reference *commit_ref = NULL;
6977 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6981 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6985 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6986 worktree, fileindex, tmp_branch, committer, orig_commit,
6987 new_logmsg, allow_conflict, repo);
6990 got_ref_close(commit_ref);
6991 free(commit_ref_name);
6995 const struct got_error *
6996 got_worktree_rebase_postpone(struct got_worktree *worktree,
6997 struct got_fileindex *fileindex)
7000 got_fileindex_free(fileindex);
7001 return lock_worktree(worktree, LOCK_SH);
7004 static const struct got_error *
7005 delete_ref(const char *name, struct got_repository *repo)
7007 const struct got_error *err;
7008 struct got_reference *ref;
7010 err = got_ref_open(&ref, repo, name, 0);
7012 if (err->code == GOT_ERR_NOT_REF)
7017 err = got_ref_delete(ref, repo);
7022 static const struct got_error *
7023 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7025 const struct got_error *err;
7026 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7027 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7029 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7032 err = delete_ref(tmp_branch_name, repo);
7036 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7039 err = delete_ref(new_base_branch_ref_name, repo);
7043 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7046 err = delete_ref(branch_ref_name, repo);
7050 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7053 err = delete_ref(commit_ref_name, repo);
7058 free(tmp_branch_name);
7059 free(new_base_branch_ref_name);
7060 free(branch_ref_name);
7061 free(commit_ref_name);
7065 static const struct got_error *
7066 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7067 struct got_object_id *new_commit_id, struct got_repository *repo)
7069 const struct got_error *err;
7070 struct got_reference *ref = NULL;
7071 struct got_object_id *old_commit_id = NULL;
7072 const char *branch_name = NULL;
7073 char *new_id_str = NULL;
7074 char *refname = NULL;
7076 branch_name = got_ref_get_name(branch);
7077 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7078 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7081 err = got_object_id_str(&new_id_str, new_commit_id);
7085 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7086 new_id_str) == -1) {
7087 err = got_error_from_errno("asprintf");
7091 err = got_ref_resolve(&old_commit_id, repo, branch);
7095 err = got_ref_alloc(&ref, refname, old_commit_id);
7099 err = got_ref_write(ref, repo);
7103 free(old_commit_id);
7109 const struct got_error *
7110 got_worktree_rebase_complete(struct got_worktree *worktree,
7111 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7112 struct got_reference *rebased_branch, struct got_repository *repo,
7115 const struct got_error *err, *unlockerr, *sync_err;
7116 struct got_object_id *new_head_commit_id = NULL;
7117 char *fileindex_path = NULL;
7119 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7123 if (create_backup) {
7124 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7125 rebased_branch, new_head_commit_id, repo);
7130 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7134 err = got_ref_write(rebased_branch, repo);
7138 err = got_worktree_set_head_ref(worktree, rebased_branch);
7142 err = delete_rebase_refs(worktree, repo);
7146 err = get_fileindex_path(&fileindex_path, worktree);
7149 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7150 sync_err = sync_fileindex(fileindex, fileindex_path);
7151 if (sync_err && err == NULL)
7154 got_fileindex_free(fileindex);
7155 free(fileindex_path);
7156 free(new_head_commit_id);
7157 unlockerr = lock_worktree(worktree, LOCK_SH);
7158 if (unlockerr && err == NULL)
7163 const struct got_error *
7164 got_worktree_rebase_abort(struct got_worktree *worktree,
7165 struct got_fileindex *fileindex, struct got_repository *repo,
7166 struct got_reference *new_base_branch,
7167 got_worktree_checkout_cb progress_cb, void *progress_arg)
7169 const struct got_error *err, *unlockerr, *sync_err;
7170 struct got_reference *resolved = NULL;
7171 struct got_object_id *commit_id = NULL;
7172 struct got_commit_object *commit = NULL;
7173 char *fileindex_path = NULL;
7174 struct revert_file_args rfa;
7175 struct got_object_id *tree_id = NULL;
7177 err = lock_worktree(worktree, LOCK_EX);
7181 err = got_object_open_as_commit(&commit, repo,
7182 worktree->base_commit_id);
7186 err = got_ref_open(&resolved, repo,
7187 got_ref_get_symref_target(new_base_branch), 0);
7191 err = got_worktree_set_head_ref(worktree, resolved);
7196 * XXX commits to the base branch could have happened while
7197 * we were busy rebasing; should we store the original commit ID
7198 * when rebase begins and read it back here?
7200 err = got_ref_resolve(&commit_id, repo, resolved);
7204 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7208 err = got_object_id_by_path(&tree_id, repo, commit,
7209 worktree->path_prefix);
7213 err = delete_rebase_refs(worktree, repo);
7217 err = get_fileindex_path(&fileindex_path, worktree);
7221 rfa.worktree = worktree;
7222 rfa.fileindex = fileindex;
7223 rfa.progress_cb = progress_cb;
7224 rfa.progress_arg = progress_arg;
7225 rfa.patch_cb = NULL;
7226 rfa.patch_arg = NULL;
7228 rfa.unlink_added_files = 0;
7229 err = worktree_status(worktree, "", fileindex, repo,
7230 revert_file, &rfa, NULL, NULL, 1, 0);
7234 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7235 repo, progress_cb, progress_arg, NULL, NULL);
7237 sync_err = sync_fileindex(fileindex, fileindex_path);
7238 if (sync_err && err == NULL)
7241 got_ref_close(resolved);
7245 got_object_commit_close(commit);
7247 got_fileindex_free(fileindex);
7248 free(fileindex_path);
7250 unlockerr = lock_worktree(worktree, LOCK_SH);
7251 if (unlockerr && err == NULL)
7256 const struct got_error *
7257 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7258 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7259 struct got_fileindex **fileindex, struct got_worktree *worktree,
7260 struct got_repository *repo)
7262 const struct got_error *err = NULL;
7263 char *tmp_branch_name = NULL;
7264 char *branch_ref_name = NULL;
7265 char *base_commit_ref_name = NULL;
7266 char *fileindex_path = NULL;
7267 struct check_rebase_ok_arg ok_arg;
7268 struct got_reference *wt_branch = NULL;
7269 struct got_reference *base_commit_ref = NULL;
7273 *base_commit_id = NULL;
7276 err = lock_worktree(worktree, LOCK_EX);
7280 err = open_fileindex(fileindex, &fileindex_path, worktree);
7284 ok_arg.worktree = worktree;
7286 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7291 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7295 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7299 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7304 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7309 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7313 err = got_ref_write(*branch_ref, repo);
7317 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7318 worktree->base_commit_id);
7321 err = got_ref_write(base_commit_ref, repo);
7324 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7325 if (*base_commit_id == NULL) {
7326 err = got_error_from_errno("got_object_id_dup");
7330 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7331 worktree->base_commit_id);
7334 err = got_ref_write(*tmp_branch, repo);
7338 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7342 free(fileindex_path);
7343 free(tmp_branch_name);
7344 free(branch_ref_name);
7345 free(base_commit_ref_name);
7347 got_ref_close(wt_branch);
7350 got_ref_close(*branch_ref);
7354 got_ref_close(*tmp_branch);
7357 free(*base_commit_id);
7359 got_fileindex_free(*fileindex);
7362 lock_worktree(worktree, LOCK_SH);
7367 const struct got_error *
7368 got_worktree_histedit_postpone(struct got_worktree *worktree,
7369 struct got_fileindex *fileindex)
7372 got_fileindex_free(fileindex);
7373 return lock_worktree(worktree, LOCK_SH);
7376 const struct got_error *
7377 got_worktree_histedit_in_progress(int *in_progress,
7378 struct got_worktree *worktree)
7380 const struct got_error *err;
7381 char *tmp_branch_name = NULL;
7383 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7387 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7388 free(tmp_branch_name);
7392 const struct got_error *
7393 got_worktree_histedit_continue(struct got_object_id **commit_id,
7394 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7395 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7396 struct got_worktree *worktree, struct got_repository *repo)
7398 const struct got_error *err;
7399 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7400 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7401 struct got_reference *commit_ref = NULL;
7402 struct got_reference *base_commit_ref = NULL;
7403 char *fileindex_path = NULL;
7404 int have_staged_files = 0;
7408 *base_commit_id = NULL;
7411 err = lock_worktree(worktree, LOCK_EX);
7415 err = open_fileindex(fileindex, &fileindex_path, worktree);
7419 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7420 &have_staged_files);
7421 if (err && err->code != GOT_ERR_CANCELLED)
7423 if (have_staged_files) {
7424 err = got_error(GOT_ERR_STAGED_PATHS);
7428 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7432 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7436 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7440 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7445 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7449 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7452 err = got_ref_resolve(commit_id, repo, commit_ref);
7456 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7459 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7463 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7467 free(commit_ref_name);
7468 free(branch_ref_name);
7469 free(fileindex_path);
7471 got_ref_close(commit_ref);
7472 if (base_commit_ref)
7473 got_ref_close(base_commit_ref);
7477 free(*base_commit_id);
7478 *base_commit_id = NULL;
7480 got_ref_close(*tmp_branch);
7484 got_fileindex_free(*fileindex);
7487 lock_worktree(worktree, LOCK_EX);
7492 static const struct got_error *
7493 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7495 const struct got_error *err;
7496 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7497 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7499 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7502 err = delete_ref(tmp_branch_name, repo);
7506 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7510 err = delete_ref(base_commit_ref_name, repo);
7514 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7517 err = delete_ref(branch_ref_name, repo);
7521 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7524 err = delete_ref(commit_ref_name, repo);
7528 free(tmp_branch_name);
7529 free(base_commit_ref_name);
7530 free(branch_ref_name);
7531 free(commit_ref_name);
7535 const struct got_error *
7536 got_worktree_histedit_abort(struct got_worktree *worktree,
7537 struct got_fileindex *fileindex, struct got_repository *repo,
7538 struct got_reference *branch, struct got_object_id *base_commit_id,
7539 got_worktree_checkout_cb progress_cb, void *progress_arg)
7541 const struct got_error *err, *unlockerr, *sync_err;
7542 struct got_reference *resolved = NULL;
7543 char *fileindex_path = NULL;
7544 struct got_commit_object *commit = NULL;
7545 struct got_object_id *tree_id = NULL;
7546 struct revert_file_args rfa;
7548 err = lock_worktree(worktree, LOCK_EX);
7552 err = got_object_open_as_commit(&commit, repo,
7553 worktree->base_commit_id);
7557 err = got_ref_open(&resolved, repo,
7558 got_ref_get_symref_target(branch), 0);
7562 err = got_worktree_set_head_ref(worktree, resolved);
7566 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7570 err = got_object_id_by_path(&tree_id, repo, commit,
7571 worktree->path_prefix);
7575 err = delete_histedit_refs(worktree, repo);
7579 err = get_fileindex_path(&fileindex_path, worktree);
7583 rfa.worktree = worktree;
7584 rfa.fileindex = fileindex;
7585 rfa.progress_cb = progress_cb;
7586 rfa.progress_arg = progress_arg;
7587 rfa.patch_cb = NULL;
7588 rfa.patch_arg = NULL;
7590 rfa.unlink_added_files = 0;
7591 err = worktree_status(worktree, "", fileindex, repo,
7592 revert_file, &rfa, NULL, NULL, 1, 0);
7596 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7597 repo, progress_cb, progress_arg, NULL, NULL);
7599 sync_err = sync_fileindex(fileindex, fileindex_path);
7600 if (sync_err && err == NULL)
7603 got_ref_close(resolved);
7605 free(fileindex_path);
7607 unlockerr = lock_worktree(worktree, LOCK_SH);
7608 if (unlockerr && err == NULL)
7613 const struct got_error *
7614 got_worktree_histedit_complete(struct got_worktree *worktree,
7615 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7616 struct got_reference *edited_branch, struct got_repository *repo)
7618 const struct got_error *err, *unlockerr, *sync_err;
7619 struct got_object_id *new_head_commit_id = NULL;
7620 struct got_reference *resolved = NULL;
7621 char *fileindex_path = NULL;
7623 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7627 err = got_ref_open(&resolved, repo,
7628 got_ref_get_symref_target(edited_branch), 0);
7632 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7633 resolved, new_head_commit_id, repo);
7637 err = got_ref_change_ref(resolved, new_head_commit_id);
7641 err = got_ref_write(resolved, repo);
7645 err = got_worktree_set_head_ref(worktree, resolved);
7649 err = delete_histedit_refs(worktree, repo);
7653 err = get_fileindex_path(&fileindex_path, worktree);
7656 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7657 sync_err = sync_fileindex(fileindex, fileindex_path);
7658 if (sync_err && err == NULL)
7661 got_fileindex_free(fileindex);
7662 free(fileindex_path);
7663 free(new_head_commit_id);
7664 unlockerr = lock_worktree(worktree, LOCK_SH);
7665 if (unlockerr && err == NULL)
7670 const struct got_error *
7671 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7672 struct got_object_id *commit_id, struct got_repository *repo)
7674 const struct got_error *err;
7675 char *commit_ref_name;
7677 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7681 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7685 err = delete_ref(commit_ref_name, repo);
7687 free(commit_ref_name);
7691 const struct got_error *
7692 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7693 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7694 struct got_worktree *worktree, const char *refname,
7695 struct got_repository *repo)
7697 const struct got_error *err = NULL;
7698 char *fileindex_path = NULL;
7699 struct check_rebase_ok_arg ok_arg;
7703 *base_branch_ref = NULL;
7705 err = lock_worktree(worktree, LOCK_EX);
7709 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7710 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7711 "cannot integrate a branch into itself; "
7712 "update -b or different branch name required");
7716 err = open_fileindex(fileindex, &fileindex_path, worktree);
7720 /* Preconditions are the same as for rebase. */
7721 ok_arg.worktree = worktree;
7723 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7728 err = got_ref_open(branch_ref, repo, refname, 1);
7732 err = got_ref_open(base_branch_ref, repo,
7733 got_worktree_get_head_ref_name(worktree), 1);
7737 got_ref_close(*branch_ref);
7740 if (*base_branch_ref) {
7741 got_ref_close(*base_branch_ref);
7742 *base_branch_ref = NULL;
7745 got_fileindex_free(*fileindex);
7748 lock_worktree(worktree, LOCK_SH);
7753 const struct got_error *
7754 got_worktree_integrate_continue(struct got_worktree *worktree,
7755 struct got_fileindex *fileindex, struct got_repository *repo,
7756 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7757 got_worktree_checkout_cb progress_cb, void *progress_arg,
7758 got_cancel_cb cancel_cb, void *cancel_arg)
7760 const struct got_error *err = NULL, *sync_err, *unlockerr;
7761 char *fileindex_path = NULL;
7762 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7763 struct got_commit_object *commit = NULL;
7765 err = get_fileindex_path(&fileindex_path, worktree);
7769 err = got_ref_resolve(&commit_id, repo, branch_ref);
7773 err = got_object_open_as_commit(&commit, repo, commit_id);
7777 err = got_object_id_by_path(&tree_id, repo, commit,
7778 worktree->path_prefix);
7782 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7786 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7787 progress_cb, progress_arg, cancel_cb, cancel_arg);
7791 err = got_ref_change_ref(base_branch_ref, commit_id);
7795 err = got_ref_write(base_branch_ref, repo);
7799 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7801 sync_err = sync_fileindex(fileindex, fileindex_path);
7802 if (sync_err && err == NULL)
7806 unlockerr = got_ref_unlock(branch_ref);
7807 if (unlockerr && err == NULL)
7809 got_ref_close(branch_ref);
7811 unlockerr = got_ref_unlock(base_branch_ref);
7812 if (unlockerr && err == NULL)
7814 got_ref_close(base_branch_ref);
7816 got_fileindex_free(fileindex);
7817 free(fileindex_path);
7820 got_object_commit_close(commit);
7822 unlockerr = lock_worktree(worktree, LOCK_SH);
7823 if (unlockerr && err == NULL)
7828 const struct got_error *
7829 got_worktree_integrate_abort(struct got_worktree *worktree,
7830 struct got_fileindex *fileindex, struct got_repository *repo,
7831 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7833 const struct got_error *err = NULL, *unlockerr = NULL;
7835 got_fileindex_free(fileindex);
7837 err = lock_worktree(worktree, LOCK_SH);
7839 unlockerr = got_ref_unlock(branch_ref);
7840 if (unlockerr && err == NULL)
7842 got_ref_close(branch_ref);
7844 unlockerr = got_ref_unlock(base_branch_ref);
7845 if (unlockerr && err == NULL)
7847 got_ref_close(base_branch_ref);
7852 const struct got_error *
7853 got_worktree_merge_postpone(struct got_worktree *worktree,
7854 struct got_fileindex *fileindex)
7856 const struct got_error *err, *sync_err;
7857 char *fileindex_path = NULL;
7859 err = get_fileindex_path(&fileindex_path, worktree);
7863 sync_err = sync_fileindex(fileindex, fileindex_path);
7865 err = lock_worktree(worktree, LOCK_SH);
7866 if (sync_err && err == NULL)
7869 got_fileindex_free(fileindex);
7870 free(fileindex_path);
7874 static const struct got_error *
7875 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7877 const struct got_error *err;
7878 char *branch_refname = NULL, *commit_refname = NULL;
7880 err = get_merge_branch_ref_name(&branch_refname, worktree);
7883 err = delete_ref(branch_refname, repo);
7887 err = get_merge_commit_ref_name(&commit_refname, worktree);
7890 err = delete_ref(commit_refname, repo);
7895 free(branch_refname);
7896 free(commit_refname);
7900 struct merge_commit_msg_arg {
7901 struct got_worktree *worktree;
7902 const char *branch_name;
7905 static const struct got_error *
7906 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7907 const char *diff_path, char **logmsg, void *arg)
7909 struct merge_commit_msg_arg *a = arg;
7911 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7912 got_worktree_get_head_ref_name(a->worktree)) == -1)
7913 return got_error_from_errno("asprintf");
7919 const struct got_error *
7920 got_worktree_merge_branch(struct got_worktree *worktree,
7921 struct got_fileindex *fileindex,
7922 struct got_object_id *yca_commit_id,
7923 struct got_object_id *branch_tip,
7924 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7925 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7927 const struct got_error *err;
7928 char *fileindex_path = NULL;
7930 err = get_fileindex_path(&fileindex_path, worktree);
7934 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7939 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7940 branch_tip, repo, progress_cb, progress_arg,
7941 cancel_cb, cancel_arg);
7943 free(fileindex_path);
7947 const struct got_error *
7948 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7949 struct got_worktree *worktree, struct got_fileindex *fileindex,
7950 const char *author, const char *committer, int allow_bad_symlinks,
7951 struct got_object_id *branch_tip, const char *branch_name,
7952 int allow_conflict, struct got_repository *repo,
7953 got_worktree_status_cb status_cb, void *status_arg)
7956 const struct got_error *err = NULL, *sync_err;
7957 struct got_pathlist_head commitable_paths;
7958 struct collect_commitables_arg cc_arg;
7959 struct got_pathlist_entry *pe;
7960 struct got_reference *head_ref = NULL;
7961 struct got_object_id *head_commit_id = NULL;
7962 int have_staged_files = 0;
7963 struct merge_commit_msg_arg mcm_arg;
7964 char *fileindex_path = NULL;
7966 memset(&cc_arg, 0, sizeof(cc_arg));
7967 *new_commit_id = NULL;
7969 TAILQ_INIT(&commitable_paths);
7971 err = get_fileindex_path(&fileindex_path, worktree);
7975 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7979 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7983 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7984 &have_staged_files);
7985 if (err && err->code != GOT_ERR_CANCELLED)
7987 if (have_staged_files) {
7988 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7992 cc_arg.commitable_paths = &commitable_paths;
7993 cc_arg.worktree = worktree;
7994 cc_arg.fileindex = fileindex;
7996 cc_arg.have_staged_files = have_staged_files;
7997 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7998 cc_arg.commit_conflicts = allow_conflict;
7999 err = worktree_status(worktree, "", fileindex, repo,
8000 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8004 if (TAILQ_EMPTY(&commitable_paths)) {
8005 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8006 "merge of %s cannot proceed", branch_name);
8010 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8011 struct got_commitable *ct = pe->data;
8012 const char *ct_path = ct->in_repo_path;
8014 while (ct_path[0] == '/')
8016 err = check_out_of_date(ct_path, ct->status,
8017 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8018 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8024 mcm_arg.worktree = worktree;
8025 mcm_arg.branch_name = branch_name;
8026 err = commit_worktree(new_commit_id, &commitable_paths,
8027 head_commit_id, branch_tip, worktree, author, committer, NULL,
8028 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8032 err = update_fileindex_after_commit(worktree, &commitable_paths,
8033 *new_commit_id, fileindex, have_staged_files);
8034 sync_err = sync_fileindex(fileindex, fileindex_path);
8035 if (sync_err && err == NULL)
8038 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8039 struct got_commitable *ct = pe->data;
8041 free_commitable(ct);
8043 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8044 free(fileindex_path);
8048 const struct got_error *
8049 got_worktree_merge_complete(struct got_worktree *worktree,
8050 struct got_fileindex *fileindex, struct got_repository *repo)
8052 const struct got_error *err, *unlockerr, *sync_err;
8053 char *fileindex_path = NULL;
8055 err = delete_merge_refs(worktree, repo);
8059 err = get_fileindex_path(&fileindex_path, worktree);
8062 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8063 sync_err = sync_fileindex(fileindex, fileindex_path);
8064 if (sync_err && err == NULL)
8067 got_fileindex_free(fileindex);
8068 free(fileindex_path);
8069 unlockerr = lock_worktree(worktree, LOCK_SH);
8070 if (unlockerr && err == NULL)
8075 const struct got_error *
8076 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8077 struct got_repository *repo)
8079 const struct got_error *err;
8080 char *branch_refname = NULL;
8081 struct got_reference *branch_ref = NULL;
8085 err = get_merge_branch_ref_name(&branch_refname, worktree);
8088 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8089 free(branch_refname);
8091 if (err->code != GOT_ERR_NOT_REF)
8099 const struct got_error *got_worktree_merge_prepare(
8100 struct got_fileindex **fileindex, struct got_worktree *worktree,
8101 struct got_reference *branch, struct got_repository *repo)
8103 const struct got_error *err = NULL;
8104 char *fileindex_path = NULL;
8105 char *branch_refname = NULL, *commit_refname = NULL;
8106 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8107 struct got_reference *commit_ref = NULL;
8108 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8109 struct check_rebase_ok_arg ok_arg;
8113 err = lock_worktree(worktree, LOCK_EX);
8117 err = open_fileindex(fileindex, &fileindex_path, worktree);
8121 /* Preconditions are the same as for rebase. */
8122 ok_arg.worktree = worktree;
8124 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8129 err = get_merge_branch_ref_name(&branch_refname, worktree);
8133 err = get_merge_commit_ref_name(&commit_refname, worktree);
8137 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8142 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8146 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8147 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8151 err = got_ref_resolve(&branch_tip, repo, branch);
8155 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8158 err = got_ref_write(branch_ref, repo);
8162 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8165 err = got_ref_write(commit_ref, repo);
8170 free(branch_refname);
8171 free(commit_refname);
8172 free(fileindex_path);
8174 got_ref_close(branch_ref);
8176 got_ref_close(commit_ref);
8178 got_ref_close(wt_branch);
8179 free(wt_branch_tip);
8182 got_fileindex_free(*fileindex);
8185 lock_worktree(worktree, LOCK_SH);
8190 const struct got_error *
8191 got_worktree_merge_continue(char **branch_name,
8192 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8193 struct got_worktree *worktree, struct got_repository *repo)
8195 const struct got_error *err;
8196 char *commit_refname = NULL, *branch_refname = NULL;
8197 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8198 char *fileindex_path = NULL;
8199 int have_staged_files = 0;
8201 *branch_name = NULL;
8205 err = lock_worktree(worktree, LOCK_EX);
8209 err = open_fileindex(fileindex, &fileindex_path, worktree);
8213 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8214 &have_staged_files);
8215 if (err && err->code != GOT_ERR_CANCELLED)
8217 if (have_staged_files) {
8218 err = got_error(GOT_ERR_STAGED_PATHS);
8222 err = get_merge_branch_ref_name(&branch_refname, worktree);
8226 err = get_merge_commit_ref_name(&commit_refname, worktree);
8230 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8234 if (!got_ref_is_symbolic(branch_ref)) {
8235 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8236 "%s is not a symbolic reference",
8237 got_ref_get_name(branch_ref));
8240 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8241 if (*branch_name == NULL) {
8242 err = got_error_from_errno("strdup");
8246 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8250 err = got_ref_resolve(branch_tip, repo, commit_ref);
8254 free(commit_refname);
8255 free(branch_refname);
8256 free(fileindex_path);
8258 got_ref_close(commit_ref);
8260 got_ref_close(branch_ref);
8264 *branch_name = NULL;
8269 got_fileindex_free(*fileindex);
8272 lock_worktree(worktree, LOCK_SH);
8277 const struct got_error *
8278 got_worktree_merge_abort(struct got_worktree *worktree,
8279 struct got_fileindex *fileindex, struct got_repository *repo,
8280 got_worktree_checkout_cb progress_cb, void *progress_arg)
8282 const struct got_error *err, *unlockerr, *sync_err;
8283 struct got_object_id *commit_id = NULL;
8284 struct got_commit_object *commit = NULL;
8285 char *fileindex_path = NULL;
8286 struct revert_file_args rfa;
8287 struct got_object_id *tree_id = NULL;
8289 err = got_object_open_as_commit(&commit, repo,
8290 worktree->base_commit_id);
8294 err = got_object_id_by_path(&tree_id, repo, commit,
8295 worktree->path_prefix);
8299 err = delete_merge_refs(worktree, repo);
8303 err = get_fileindex_path(&fileindex_path, worktree);
8307 rfa.worktree = worktree;
8308 rfa.fileindex = fileindex;
8309 rfa.progress_cb = progress_cb;
8310 rfa.progress_arg = progress_arg;
8311 rfa.patch_cb = NULL;
8312 rfa.patch_arg = NULL;
8314 rfa.unlink_added_files = 1;
8315 err = worktree_status(worktree, "", fileindex, repo,
8316 revert_file, &rfa, NULL, NULL, 1, 0);
8320 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8321 repo, progress_cb, progress_arg, NULL, NULL);
8323 sync_err = sync_fileindex(fileindex, fileindex_path);
8324 if (sync_err && err == NULL)
8330 got_object_commit_close(commit);
8332 got_fileindex_free(fileindex);
8333 free(fileindex_path);
8335 unlockerr = lock_worktree(worktree, LOCK_SH);
8336 if (unlockerr && err == NULL)
8341 struct check_stage_ok_arg {
8342 struct got_object_id *head_commit_id;
8343 struct got_worktree *worktree;
8344 struct got_fileindex *fileindex;
8345 struct got_repository *repo;
8349 static const struct got_error *
8350 check_stage_ok(void *arg, unsigned char status,
8351 unsigned char staged_status, const char *relpath,
8352 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8353 struct got_object_id *commit_id, int dirfd, const char *de_name)
8355 struct check_stage_ok_arg *a = arg;
8356 const struct got_error *err = NULL;
8357 struct got_fileindex_entry *ie;
8358 struct got_object_id base_commit_id;
8359 struct got_object_id *base_commit_idp = NULL;
8360 char *in_repo_path = NULL, *p;
8362 if (status == GOT_STATUS_UNVERSIONED ||
8363 status == GOT_STATUS_NO_CHANGE)
8365 if (status == GOT_STATUS_NONEXISTENT)
8366 return got_error_set_errno(ENOENT, relpath);
8368 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8370 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8372 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8373 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8375 return got_error_from_errno("asprintf");
8377 if (got_fileindex_entry_has_commit(ie)) {
8378 base_commit_idp = got_fileindex_entry_get_commit_id(
8379 &base_commit_id, ie);
8382 if (status == GOT_STATUS_CONFLICT) {
8383 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8385 } else if (status != GOT_STATUS_ADD &&
8386 status != GOT_STATUS_MODIFY &&
8387 status != GOT_STATUS_DELETE) {
8388 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8392 a->have_changes = 1;
8397 err = check_out_of_date(p, status, staged_status,
8398 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8399 GOT_ERR_STAGE_OUT_OF_DATE);
8405 struct stage_path_arg {
8406 struct got_worktree *worktree;
8407 struct got_fileindex *fileindex;
8408 struct got_repository *repo;
8409 got_worktree_status_cb status_cb;
8411 got_worktree_patch_cb patch_cb;
8413 int staged_something;
8414 int allow_bad_symlinks;
8417 static const struct got_error *
8418 stage_path(void *arg, unsigned char status,
8419 unsigned char staged_status, const char *relpath,
8420 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8421 struct got_object_id *commit_id, int dirfd, const char *de_name)
8423 struct stage_path_arg *a = arg;
8424 const struct got_error *err = NULL;
8425 struct got_fileindex_entry *ie;
8426 char *ondisk_path = NULL, *path_content = NULL;
8428 struct got_object_id *new_staged_blob_id = NULL;
8431 if (status == GOT_STATUS_UNVERSIONED)
8434 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8436 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8438 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8440 return got_error_from_errno("asprintf");
8443 case GOT_STATUS_ADD:
8444 case GOT_STATUS_MODIFY:
8445 /* XXX could sb.st_mode be passed in by our caller? */
8446 if (lstat(ondisk_path, &sb) == -1) {
8447 err = got_error_from_errno2("lstat", ondisk_path);
8451 if (status == GOT_STATUS_ADD) {
8452 int choice = GOT_PATCH_CHOICE_NONE;
8453 err = (*a->patch_cb)(&choice, a->patch_arg,
8454 status, ie->path, NULL, 1, 1);
8457 if (choice != GOT_PATCH_CHOICE_YES)
8460 err = create_patched_content(&path_content, 0,
8461 staged_blob_id ? staged_blob_id : blob_id,
8462 ondisk_path, dirfd, de_name, ie->path,
8463 a->repo, a->patch_cb, a->patch_arg);
8464 if (err || path_content == NULL)
8468 err = got_object_blob_create(&new_staged_blob_id,
8469 path_content ? path_content : ondisk_path, a->repo);
8472 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8473 SHA1_DIGEST_LENGTH);
8474 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8475 stage = GOT_FILEIDX_STAGE_ADD;
8477 stage = GOT_FILEIDX_STAGE_MODIFY;
8478 got_fileindex_entry_stage_set(ie, stage);
8479 if (S_ISLNK(sb.st_mode)) {
8480 int is_bad_symlink = 0;
8481 if (!a->allow_bad_symlinks) {
8482 char target_path[PATH_MAX];
8484 target_len = readlink(ondisk_path, target_path,
8485 sizeof(target_path));
8486 if (target_len == -1) {
8487 err = got_error_from_errno2("readlink",
8491 err = is_bad_symlink_target(&is_bad_symlink,
8492 target_path, target_len, ondisk_path,
8493 a->worktree->root_path);
8496 if (is_bad_symlink) {
8497 err = got_error_path(ondisk_path,
8498 GOT_ERR_BAD_SYMLINK);
8503 got_fileindex_entry_staged_filetype_set(ie,
8504 GOT_FILEIDX_MODE_BAD_SYMLINK);
8506 got_fileindex_entry_staged_filetype_set(ie,
8507 GOT_FILEIDX_MODE_SYMLINK);
8509 got_fileindex_entry_staged_filetype_set(ie,
8510 GOT_FILEIDX_MODE_REGULAR_FILE);
8512 a->staged_something = 1;
8513 if (a->status_cb == NULL)
8515 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8516 get_staged_status(ie), relpath, blob_id,
8517 new_staged_blob_id, NULL, dirfd, de_name);
8521 * When staging the reverse of the staged diff,
8522 * implicitly unstage the file.
8524 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8525 sizeof(ie->blob_sha1)) == 0) {
8526 got_fileindex_entry_stage_set(ie,
8527 GOT_FILEIDX_STAGE_NONE);
8530 case GOT_STATUS_DELETE:
8531 if (staged_status == GOT_STATUS_DELETE)
8534 int choice = GOT_PATCH_CHOICE_NONE;
8535 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8536 ie->path, NULL, 1, 1);
8539 if (choice == GOT_PATCH_CHOICE_NO)
8541 if (choice != GOT_PATCH_CHOICE_YES) {
8542 err = got_error(GOT_ERR_PATCH_CHOICE);
8546 stage = GOT_FILEIDX_STAGE_DELETE;
8547 got_fileindex_entry_stage_set(ie, stage);
8548 a->staged_something = 1;
8549 if (a->status_cb == NULL)
8551 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8552 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8555 case GOT_STATUS_NO_CHANGE:
8557 case GOT_STATUS_CONFLICT:
8558 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8560 case GOT_STATUS_NONEXISTENT:
8561 err = got_error_set_errno(ENOENT, relpath);
8564 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8568 if (path_content && unlink(path_content) == -1 && err == NULL)
8569 err = got_error_from_errno2("unlink", path_content);
8572 free(new_staged_blob_id);
8576 const struct got_error *
8577 got_worktree_stage(struct got_worktree *worktree,
8578 struct got_pathlist_head *paths,
8579 got_worktree_status_cb status_cb, void *status_arg,
8580 got_worktree_patch_cb patch_cb, void *patch_arg,
8581 int allow_bad_symlinks, struct got_repository *repo)
8583 const struct got_error *err = NULL, *sync_err, *unlockerr;
8584 struct got_pathlist_entry *pe;
8585 struct got_fileindex *fileindex = NULL;
8586 char *fileindex_path = NULL;
8587 struct got_reference *head_ref = NULL;
8588 struct got_object_id *head_commit_id = NULL;
8589 struct check_stage_ok_arg oka;
8590 struct stage_path_arg spa;
8592 err = lock_worktree(worktree, LOCK_EX);
8596 err = got_ref_open(&head_ref, repo,
8597 got_worktree_get_head_ref_name(worktree), 0);
8600 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8603 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8607 /* Check pre-conditions before staging anything. */
8608 oka.head_commit_id = head_commit_id;
8609 oka.worktree = worktree;
8610 oka.fileindex = fileindex;
8612 oka.have_changes = 0;
8613 TAILQ_FOREACH(pe, paths, entry) {
8614 err = worktree_status(worktree, pe->path, fileindex, repo,
8615 check_stage_ok, &oka, NULL, NULL, 1, 0);
8619 if (!oka.have_changes) {
8620 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8624 spa.worktree = worktree;
8625 spa.fileindex = fileindex;
8627 spa.patch_cb = patch_cb;
8628 spa.patch_arg = patch_arg;
8629 spa.status_cb = status_cb;
8630 spa.status_arg = status_arg;
8631 spa.staged_something = 0;
8632 spa.allow_bad_symlinks = allow_bad_symlinks;
8633 TAILQ_FOREACH(pe, paths, entry) {
8634 err = worktree_status(worktree, pe->path, fileindex, repo,
8635 stage_path, &spa, NULL, NULL, 1, 0);
8639 if (!spa.staged_something) {
8640 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8644 sync_err = sync_fileindex(fileindex, fileindex_path);
8645 if (sync_err && err == NULL)
8649 got_ref_close(head_ref);
8650 free(head_commit_id);
8651 free(fileindex_path);
8653 got_fileindex_free(fileindex);
8654 unlockerr = lock_worktree(worktree, LOCK_SH);
8655 if (unlockerr && err == NULL)
8660 struct unstage_path_arg {
8661 struct got_worktree *worktree;
8662 struct got_fileindex *fileindex;
8663 struct got_repository *repo;
8664 got_worktree_checkout_cb progress_cb;
8666 got_worktree_patch_cb patch_cb;
8670 static const struct got_error *
8671 create_unstaged_content(char **path_unstaged_content,
8672 char **path_new_staged_content, struct got_object_id *blob_id,
8673 struct got_object_id *staged_blob_id, const char *relpath,
8674 struct got_repository *repo,
8675 got_worktree_patch_cb patch_cb, void *patch_arg)
8677 const struct got_error *err, *free_err;
8678 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8679 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8680 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8681 struct got_diffreg_result *diffreg_result = NULL;
8682 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8683 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8684 int fd1 = -1, fd2 = -1;
8686 *path_unstaged_content = NULL;
8687 *path_new_staged_content = NULL;
8689 err = got_object_id_str(&label1, blob_id);
8693 fd1 = got_opentempfd();
8695 err = got_error_from_errno("got_opentempfd");
8698 fd2 = got_opentempfd();
8700 err = got_error_from_errno("got_opentempfd");
8704 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8708 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8712 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8716 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8721 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8725 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8729 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8730 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8734 err = got_opentemp_named(path_unstaged_content, &outfile,
8735 "got-unstaged-content", "");
8738 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8739 "got-new-staged-content", "");
8743 if (fseek(f1, 0L, SEEK_SET) == -1) {
8744 err = got_ferror(f1, GOT_ERR_IO);
8747 if (fseek(f2, 0L, SEEK_SET) == -1) {
8748 err = got_ferror(f2, GOT_ERR_IO);
8751 /* Count the number of actual changes in the diff result. */
8752 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8753 struct diff_chunk_context cc = {};
8754 diff_chunk_context_load_change(&cc, &nchunks_used,
8755 diffreg_result->result, n, 0);
8758 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8760 err = apply_or_reject_change(&choice, &nchunks_used,
8761 diffreg_result->result, n, relpath, f1, f2,
8762 &line_cur1, &line_cur2,
8763 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8766 if (choice == GOT_PATCH_CHOICE_YES)
8769 have_rejected_content = 1;
8770 if (choice == GOT_PATCH_CHOICE_QUIT)
8773 if (have_content || have_rejected_content)
8774 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8775 outfile, rejectfile);
8778 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8779 err = got_error_from_errno("close");
8781 got_object_blob_close(blob);
8782 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8783 err = got_error_from_errno("close");
8785 got_object_blob_close(staged_blob);
8786 free_err = got_diffreg_result_free(diffreg_result);
8787 if (free_err && err == NULL)
8789 if (f1 && fclose(f1) == EOF && err == NULL)
8790 err = got_error_from_errno2("fclose", path1);
8791 if (f2 && fclose(f2) == EOF && err == NULL)
8792 err = got_error_from_errno2("fclose", path2);
8793 if (outfile && fclose(outfile) == EOF && err == NULL)
8794 err = got_error_from_errno2("fclose", *path_unstaged_content);
8795 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8796 err = got_error_from_errno2("fclose", *path_new_staged_content);
8797 if (path1 && unlink(path1) == -1 && err == NULL)
8798 err = got_error_from_errno2("unlink", path1);
8799 if (path2 && unlink(path2) == -1 && err == NULL)
8800 err = got_error_from_errno2("unlink", path2);
8801 if (err || !have_content) {
8802 if (*path_unstaged_content &&
8803 unlink(*path_unstaged_content) == -1 && err == NULL)
8804 err = got_error_from_errno2("unlink",
8805 *path_unstaged_content);
8806 free(*path_unstaged_content);
8807 *path_unstaged_content = NULL;
8809 if (err || !have_content || !have_rejected_content) {
8810 if (*path_new_staged_content &&
8811 unlink(*path_new_staged_content) == -1 && err == NULL)
8812 err = got_error_from_errno2("unlink",
8813 *path_new_staged_content);
8814 free(*path_new_staged_content);
8815 *path_new_staged_content = NULL;
8822 static const struct got_error *
8823 unstage_hunks(struct got_object_id *staged_blob_id,
8824 struct got_blob_object *blob_base,
8825 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8826 const char *ondisk_path, const char *label_orig,
8827 struct got_worktree *worktree, struct got_repository *repo,
8828 got_worktree_patch_cb patch_cb, void *patch_arg,
8829 got_worktree_checkout_cb progress_cb, void *progress_arg)
8831 const struct got_error *err = NULL;
8832 char *path_unstaged_content = NULL;
8833 char *path_new_staged_content = NULL;
8834 char *parent = NULL, *base_path = NULL;
8835 char *blob_base_path = NULL;
8836 struct got_object_id *new_staged_blob_id = NULL;
8837 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8840 err = create_unstaged_content(&path_unstaged_content,
8841 &path_new_staged_content, blob_id, staged_blob_id,
8842 ie->path, repo, patch_cb, patch_arg);
8846 if (path_unstaged_content == NULL)
8849 if (path_new_staged_content) {
8850 err = got_object_blob_create(&new_staged_blob_id,
8851 path_new_staged_content, repo);
8856 f = fopen(path_unstaged_content, "re");
8858 err = got_error_from_errno2("fopen",
8859 path_unstaged_content);
8862 if (fstat(fileno(f), &sb) == -1) {
8863 err = got_error_from_errno2("fstat", path_unstaged_content);
8866 if (got_fileindex_entry_staged_filetype_get(ie) ==
8867 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8868 char link_target[PATH_MAX];
8870 r = fread(link_target, 1, sizeof(link_target), f);
8871 if (r == 0 && ferror(f)) {
8872 err = got_error_from_errno("fread");
8875 if (r >= sizeof(link_target)) { /* should not happen */
8876 err = got_error(GOT_ERR_NO_SPACE);
8879 link_target[r] = '\0';
8880 err = merge_symlink(worktree, blob_base,
8881 ondisk_path, ie->path, label_orig, link_target,
8882 worktree->base_commit_id, repo, progress_cb,
8885 int local_changes_subsumed;
8887 err = got_path_dirname(&parent, ondisk_path);
8891 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8893 err = got_error_from_errno("asprintf");
8898 err = got_opentemp_named(&blob_base_path, &f_base,
8902 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8908 * In order the run a 3-way merge with a symlink we copy the symlink's
8909 * target path into a temporary file and use that file with diff3.
8911 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8912 err = dump_symlink_target_path_to_file(&f_deriv2,
8918 fd = open(ondisk_path,
8919 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8921 err = got_error_from_errno2("open", ondisk_path);
8924 f_deriv2 = fdopen(fd, "r");
8925 if (f_deriv2 == NULL) {
8926 err = got_error_from_errno2("fdopen", ondisk_path);
8932 err = merge_file(&local_changes_subsumed, worktree,
8933 f_base, f, f_deriv2, ondisk_path, ie->path,
8934 got_fileindex_perms_to_st(ie),
8935 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8936 repo, progress_cb, progress_arg);
8941 if (new_staged_blob_id) {
8942 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8943 SHA1_DIGEST_LENGTH);
8945 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8946 got_fileindex_entry_staged_filetype_set(ie, 0);
8949 free(new_staged_blob_id);
8950 if (path_unstaged_content &&
8951 unlink(path_unstaged_content) == -1 && err == NULL)
8952 err = got_error_from_errno2("unlink", path_unstaged_content);
8953 if (path_new_staged_content &&
8954 unlink(path_new_staged_content) == -1 && err == NULL)
8955 err = got_error_from_errno2("unlink", path_new_staged_content);
8956 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8957 err = got_error_from_errno2("unlink", blob_base_path);
8958 if (f_base && fclose(f_base) == EOF && err == NULL)
8959 err = got_error_from_errno2("fclose", path_unstaged_content);
8960 if (f && fclose(f) == EOF && err == NULL)
8961 err = got_error_from_errno2("fclose", path_unstaged_content);
8962 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8963 err = got_error_from_errno2("fclose", ondisk_path);
8964 free(path_unstaged_content);
8965 free(path_new_staged_content);
8966 free(blob_base_path);
8972 static const struct got_error *
8973 unstage_path(void *arg, unsigned char status,
8974 unsigned char staged_status, const char *relpath,
8975 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8976 struct got_object_id *commit_id, int dirfd, const char *de_name)
8978 const struct got_error *err = NULL;
8979 struct unstage_path_arg *a = arg;
8980 struct got_fileindex_entry *ie;
8981 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8982 char *ondisk_path = NULL;
8983 char *id_str = NULL, *label_orig = NULL;
8984 int local_changes_subsumed;
8986 int fd1 = -1, fd2 = -1;
8988 if (staged_status != GOT_STATUS_ADD &&
8989 staged_status != GOT_STATUS_MODIFY &&
8990 staged_status != GOT_STATUS_DELETE)
8993 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8995 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8997 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8999 return got_error_from_errno("asprintf");
9001 err = got_object_id_str(&id_str,
9002 commit_id ? commit_id : a->worktree->base_commit_id);
9005 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9007 err = got_error_from_errno("asprintf");
9011 fd1 = got_opentempfd();
9013 err = got_error_from_errno("got_opentempfd");
9016 fd2 = got_opentempfd();
9018 err = got_error_from_errno("got_opentempfd");
9022 switch (staged_status) {
9023 case GOT_STATUS_MODIFY:
9024 err = got_object_open_as_blob(&blob_base, a->repo,
9025 blob_id, 8192, fd1);
9029 case GOT_STATUS_ADD:
9031 if (staged_status == GOT_STATUS_ADD) {
9032 int choice = GOT_PATCH_CHOICE_NONE;
9033 err = (*a->patch_cb)(&choice, a->patch_arg,
9034 staged_status, ie->path, NULL, 1, 1);
9037 if (choice != GOT_PATCH_CHOICE_YES)
9040 err = unstage_hunks(staged_blob_id,
9041 blob_base, blob_id, ie, ondisk_path,
9042 label_orig, a->worktree, a->repo,
9043 a->patch_cb, a->patch_arg,
9044 a->progress_cb, a->progress_arg);
9045 break; /* Done with this file. */
9048 err = got_object_open_as_blob(&blob_staged, a->repo,
9049 staged_blob_id, 8192, fd2);
9052 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9053 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9054 case GOT_FILEIDX_MODE_REGULAR_FILE:
9055 err = merge_blob(&local_changes_subsumed, a->worktree,
9056 blob_base, ondisk_path, relpath,
9057 got_fileindex_perms_to_st(ie), label_orig,
9058 blob_staged, commit_id ? commit_id :
9059 a->worktree->base_commit_id, a->repo,
9060 a->progress_cb, a->progress_arg);
9062 case GOT_FILEIDX_MODE_SYMLINK:
9063 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9064 char *staged_target;
9065 err = got_object_blob_read_to_str(
9066 &staged_target, blob_staged);
9069 err = merge_symlink(a->worktree, blob_base,
9070 ondisk_path, relpath, label_orig,
9071 staged_target, commit_id ? commit_id :
9072 a->worktree->base_commit_id,
9073 a->repo, a->progress_cb, a->progress_arg);
9074 free(staged_target);
9076 err = merge_blob(&local_changes_subsumed,
9077 a->worktree, blob_base, ondisk_path,
9078 relpath, got_fileindex_perms_to_st(ie),
9079 label_orig, blob_staged,
9080 commit_id ? commit_id :
9081 a->worktree->base_commit_id, a->repo,
9082 a->progress_cb, a->progress_arg);
9086 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9090 got_fileindex_entry_stage_set(ie,
9091 GOT_FILEIDX_STAGE_NONE);
9092 got_fileindex_entry_staged_filetype_set(ie, 0);
9095 case GOT_STATUS_DELETE:
9097 int choice = GOT_PATCH_CHOICE_NONE;
9098 err = (*a->patch_cb)(&choice, a->patch_arg,
9099 staged_status, ie->path, NULL, 1, 1);
9102 if (choice == GOT_PATCH_CHOICE_NO)
9104 if (choice != GOT_PATCH_CHOICE_YES) {
9105 err = got_error(GOT_ERR_PATCH_CHOICE);
9109 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9110 got_fileindex_entry_staged_filetype_set(ie, 0);
9111 err = get_file_status(&status, &sb, ie, ondisk_path,
9112 dirfd, de_name, a->repo);
9115 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9120 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9121 err = got_error_from_errno("close");
9123 got_object_blob_close(blob_base);
9124 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9125 err = got_error_from_errno("close");
9127 got_object_blob_close(blob_staged);
9133 const struct got_error *
9134 got_worktree_unstage(struct got_worktree *worktree,
9135 struct got_pathlist_head *paths,
9136 got_worktree_checkout_cb progress_cb, void *progress_arg,
9137 got_worktree_patch_cb patch_cb, void *patch_arg,
9138 struct got_repository *repo)
9140 const struct got_error *err = NULL, *sync_err, *unlockerr;
9141 struct got_pathlist_entry *pe;
9142 struct got_fileindex *fileindex = NULL;
9143 char *fileindex_path = NULL;
9144 struct unstage_path_arg upa;
9146 err = lock_worktree(worktree, LOCK_EX);
9150 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9154 upa.worktree = worktree;
9155 upa.fileindex = fileindex;
9157 upa.progress_cb = progress_cb;
9158 upa.progress_arg = progress_arg;
9159 upa.patch_cb = patch_cb;
9160 upa.patch_arg = patch_arg;
9161 TAILQ_FOREACH(pe, paths, entry) {
9162 err = worktree_status(worktree, pe->path, fileindex, repo,
9163 unstage_path, &upa, NULL, NULL, 1, 0);
9168 sync_err = sync_fileindex(fileindex, fileindex_path);
9169 if (sync_err && err == NULL)
9172 free(fileindex_path);
9174 got_fileindex_free(fileindex);
9175 unlockerr = lock_worktree(worktree, LOCK_SH);
9176 if (unlockerr && err == NULL)
9181 struct report_file_info_arg {
9182 struct got_worktree *worktree;
9183 got_worktree_path_info_cb info_cb;
9185 struct got_pathlist_head *paths;
9186 got_cancel_cb cancel_cb;
9190 static const struct got_error *
9191 report_file_info(void *arg, struct got_fileindex_entry *ie)
9193 struct report_file_info_arg *a = arg;
9194 struct got_pathlist_entry *pe;
9195 struct got_object_id blob_id, staged_blob_id, commit_id;
9196 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9197 struct got_object_id *commit_idp = NULL;
9200 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9201 return got_error(GOT_ERR_CANCELLED);
9203 TAILQ_FOREACH(pe, a->paths, entry) {
9204 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9205 got_path_is_child(ie->path, pe->path, pe->path_len))
9208 if (pe == NULL) /* not found */
9211 if (got_fileindex_entry_has_blob(ie))
9212 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9213 stage = got_fileindex_entry_stage_get(ie);
9214 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9215 stage == GOT_FILEIDX_STAGE_ADD) {
9216 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9217 &staged_blob_id, ie);
9220 if (got_fileindex_entry_has_commit(ie))
9221 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9223 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9224 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9227 const struct got_error *
9228 got_worktree_path_info(struct got_worktree *worktree,
9229 struct got_pathlist_head *paths,
9230 got_worktree_path_info_cb info_cb, void *info_arg,
9231 got_cancel_cb cancel_cb, void *cancel_arg)
9234 const struct got_error *err = NULL, *unlockerr;
9235 struct got_fileindex *fileindex = NULL;
9236 char *fileindex_path = NULL;
9237 struct report_file_info_arg arg;
9239 err = lock_worktree(worktree, LOCK_SH);
9243 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9247 arg.worktree = worktree;
9248 arg.info_cb = info_cb;
9249 arg.info_arg = info_arg;
9251 arg.cancel_cb = cancel_cb;
9252 arg.cancel_arg = cancel_arg;
9253 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9256 free(fileindex_path);
9258 got_fileindex_free(fileindex);
9259 unlockerr = lock_worktree(worktree, LOCK_UN);
9260 if (unlockerr && err == NULL)
9265 static const struct got_error *
9266 patch_check_path(const char *p, char **path, unsigned char *status,
9267 unsigned char *staged_status, struct got_fileindex *fileindex,
9268 struct got_worktree *worktree, struct got_repository *repo)
9270 const struct got_error *err;
9271 struct got_fileindex_entry *ie;
9273 char *ondisk_path = NULL;
9275 err = got_worktree_resolve_path(path, worktree, p);
9279 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9280 *path[0] ? "/" : "", *path) == -1)
9281 return got_error_from_errno("asprintf");
9283 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9285 *staged_status = get_staged_status(ie);
9286 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9291 *staged_status = GOT_STATUS_NO_CHANGE;
9292 *status = GOT_STATUS_UNVERSIONED;
9293 if (lstat(ondisk_path, &sb) == -1) {
9294 if (errno != ENOENT) {
9295 err = got_error_from_errno2("lstat",
9299 *status = GOT_STATUS_NONEXISTENT;
9308 static const struct got_error *
9309 patch_can_rm(const char *path, unsigned char status,
9310 unsigned char staged_status)
9312 if (status == GOT_STATUS_NONEXISTENT)
9313 return got_error_set_errno(ENOENT, path);
9314 if (status != GOT_STATUS_NO_CHANGE &&
9315 status != GOT_STATUS_ADD &&
9316 status != GOT_STATUS_MODIFY &&
9317 status != GOT_STATUS_MODE_CHANGE)
9318 return got_error_path(path, GOT_ERR_FILE_STATUS);
9319 if (staged_status == GOT_STATUS_DELETE)
9320 return got_error_path(path, GOT_ERR_FILE_STATUS);
9324 static const struct got_error *
9325 patch_can_add(const char *path, unsigned char status)
9327 if (status != GOT_STATUS_NONEXISTENT)
9328 return got_error_path(path, GOT_ERR_FILE_STATUS);
9332 static const struct got_error *
9333 patch_can_edit(const char *path, unsigned char status,
9334 unsigned char staged_status)
9336 if (status == GOT_STATUS_NONEXISTENT)
9337 return got_error_set_errno(ENOENT, path);
9338 if (status != GOT_STATUS_NO_CHANGE &&
9339 status != GOT_STATUS_ADD &&
9340 status != GOT_STATUS_MODIFY)
9341 return got_error_path(path, GOT_ERR_FILE_STATUS);
9342 if (staged_status == GOT_STATUS_DELETE)
9343 return got_error_path(path, GOT_ERR_FILE_STATUS);
9347 const struct got_error *
9348 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9349 char **fileindex_path, struct got_worktree *worktree)
9351 return open_fileindex(fileindex, fileindex_path, worktree);
9354 const struct got_error *
9355 got_worktree_patch_check_path(const char *old, const char *new,
9356 char **oldpath, char **newpath, struct got_worktree *worktree,
9357 struct got_repository *repo, struct got_fileindex *fileindex)
9359 const struct got_error *err = NULL;
9360 int file_renamed = 0;
9361 unsigned char status_old, staged_status_old;
9362 unsigned char status_new, staged_status_new;
9367 err = patch_check_path(old != NULL ? old : new, oldpath,
9368 &status_old, &staged_status_old, fileindex, worktree, repo);
9372 err = patch_check_path(new != NULL ? new : old, newpath,
9373 &status_new, &staged_status_new, fileindex, worktree, repo);
9377 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9380 if (old != NULL && new == NULL)
9381 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9382 else if (file_renamed) {
9383 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9385 err = patch_can_add(*newpath, status_new);
9386 } else if (old == NULL)
9387 err = patch_can_add(*newpath, status_new);
9389 err = patch_can_edit(*newpath, status_new, staged_status_new);
9401 const struct got_error *
9402 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9403 struct got_worktree *worktree, struct got_fileindex *fileindex,
9404 got_worktree_checkout_cb progress_cb, void *progress_arg)
9406 struct schedule_addition_args saa;
9408 memset(&saa, 0, sizeof(saa));
9409 saa.worktree = worktree;
9410 saa.fileindex = fileindex;
9411 saa.progress_cb = progress_cb;
9412 saa.progress_arg = progress_arg;
9415 return worktree_status(worktree, path, fileindex, repo,
9416 schedule_addition, &saa, NULL, NULL, 1, 0);
9419 const struct got_error *
9420 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9421 struct got_worktree *worktree, struct got_fileindex *fileindex,
9422 got_worktree_delete_cb progress_cb, void *progress_arg)
9424 struct schedule_deletion_args sda;
9426 memset(&sda, 0, sizeof(sda));
9427 sda.worktree = worktree;
9428 sda.fileindex = fileindex;
9429 sda.progress_cb = progress_cb;
9430 sda.progress_arg = progress_arg;
9432 sda.delete_local_mods = 0;
9433 sda.keep_on_disk = 0;
9434 sda.ignore_missing_paths = 0;
9435 sda.status_codes = NULL;
9437 return worktree_status(worktree, path, fileindex, repo,
9438 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9441 const struct got_error *
9442 got_worktree_patch_complete(struct got_fileindex *fileindex,
9443 const char *fileindex_path)
9445 const struct got_error *err = NULL;
9447 err = sync_fileindex(fileindex, fileindex_path);
9448 got_fileindex_free(fileindex);