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>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
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);
480 * Perform a 3-way merge where the file f_orig acts as the common
481 * ancestor, the file f_deriv acts as the first derived version,
482 * and the file f_deriv2 acts as the second derived version.
483 * The merge result will be written to a new file at ondisk_path; any
484 * existing file at this path will be replaced.
486 static const struct got_error *
487 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
488 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
489 const char *path, uint16_t st_mode,
490 const char *label_orig, const char *label_deriv, const char *label_deriv2,
491 enum got_diff_algorithm diff_algo, struct got_repository *repo,
492 got_worktree_checkout_cb progress_cb, void *progress_arg)
494 const struct got_error *err = NULL;
496 FILE *f_merged = NULL;
497 char *merged_path = NULL, *base_path = NULL;
501 *local_changes_subsumed = 0;
503 err = got_path_dirname(&parent, ondisk_path);
507 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
508 err = got_error_from_errno("asprintf");
512 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
516 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
517 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
521 err = (*progress_cb)(progress_arg,
522 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
526 if (fsync(merged_fd) != 0) {
527 err = got_error_from_errno("fsync");
531 f_merged = fdopen(merged_fd, "r");
532 if (f_merged == NULL) {
533 err = got_error_from_errno("fdopen");
538 /* Check if a clean merge has subsumed all local changes. */
539 if (overlapcnt == 0) {
540 err = check_files_equal(local_changes_subsumed, f_deriv,
546 if (fchmod(fileno(f_merged), st_mode) != 0) {
547 err = got_error_from_errno2("fchmod", merged_path);
551 if (rename(merged_path, ondisk_path) != 0) {
552 err = got_error_from_errno3("rename", merged_path,
561 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
562 err = got_error_from_errno("close");
563 if (f_merged && fclose(f_merged) == EOF && err == NULL)
564 err = got_error_from_errno("fclose");
571 static const struct got_error *
572 update_symlink(const char *ondisk_path, const char *target_path,
575 /* This is not atomic but matches what 'ln -sf' does. */
576 if (unlink(ondisk_path) == -1)
577 return got_error_from_errno2("unlink", ondisk_path);
578 if (symlink(target_path, ondisk_path) == -1)
579 return got_error_from_errno3("symlink", target_path,
585 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
586 * in the work tree with a file that contains conflict markers and the
587 * conflicting target paths of the original version, a "derived version"
588 * of a symlink from an incoming change, and a local version of the symlink.
590 * The original versions's target path can be NULL if it is not available,
591 * such as if both derived versions added a new symlink at the same path.
593 * The incoming derived symlink target is NULL in case the incoming change
594 * has deleted this symlink.
596 static const struct got_error *
597 install_symlink_conflict(const char *deriv_target,
598 struct got_object_id *deriv_base_commit_id, const char *orig_target,
599 const char *label_orig, const char *local_target, const char *ondisk_path)
601 const struct got_error *err;
602 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
605 err = got_object_id_str(&id_str, deriv_base_commit_id);
607 return got_error_from_errno("asprintf");
609 if (asprintf(&label_deriv, "%s: commit %s",
610 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
611 err = got_error_from_errno("asprintf");
615 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
619 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
620 err = got_error_from_errno2("fchmod", path);
624 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
625 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
626 deriv_target ? deriv_target : "(symlink was deleted)",
627 orig_target ? label_orig : "",
628 orig_target ? "\n" : "",
629 orig_target ? orig_target : "",
630 orig_target ? "\n" : "",
631 GOT_DIFF_CONFLICT_MARKER_SEP,
632 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
633 err = got_error_from_errno2("fprintf", path);
637 if (unlink(ondisk_path) == -1) {
638 err = got_error_from_errno2("unlink", ondisk_path);
641 if (rename(path, ondisk_path) == -1) {
642 err = got_error_from_errno3("rename", path, ondisk_path);
646 if (f != NULL && fclose(f) == EOF && err == NULL)
647 err = got_error_from_errno2("fclose", path);
654 /* forward declaration */
655 static const struct got_error *
656 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
657 const char *, const char *, uint16_t, const char *,
658 struct got_blob_object *, struct got_object_id *,
659 struct got_repository *, got_worktree_checkout_cb, void *);
662 * Merge a symlink into the work tree, where blob_orig acts as the common
663 * ancestor, deriv_target is the link target of the first derived version,
664 * and the symlink on disk acts as the second derived version.
665 * Assume that contents of both blobs represent symlinks.
667 static const struct got_error *
668 merge_symlink(struct got_worktree *worktree,
669 struct got_blob_object *blob_orig, const char *ondisk_path,
670 const char *path, const char *label_orig, const char *deriv_target,
671 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
672 got_worktree_checkout_cb progress_cb, void *progress_arg)
674 const struct got_error *err = NULL;
675 char *ancestor_target = NULL;
677 ssize_t ondisk_len, deriv_len;
678 char ondisk_target[PATH_MAX];
679 int have_local_change = 0;
680 int have_incoming_change = 0;
682 if (lstat(ondisk_path, &sb) == -1)
683 return got_error_from_errno2("lstat", ondisk_path);
685 ondisk_len = readlink(ondisk_path, ondisk_target,
686 sizeof(ondisk_target));
687 if (ondisk_len == -1) {
688 err = got_error_from_errno2("readlink",
692 ondisk_target[ondisk_len] = '\0';
695 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
700 if (ancestor_target == NULL ||
701 (ondisk_len != strlen(ancestor_target) ||
702 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
703 have_local_change = 1;
705 deriv_len = strlen(deriv_target);
706 if (ancestor_target == NULL ||
707 (deriv_len != strlen(ancestor_target) ||
708 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
709 have_incoming_change = 1;
711 if (!have_local_change && !have_incoming_change) {
712 if (ancestor_target) {
713 /* Both sides made the same change. */
714 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
716 } else if (deriv_len == ondisk_len &&
717 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
718 /* Both sides added the same symlink. */
719 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
722 /* Both sides added symlinks which don't match. */
723 err = install_symlink_conflict(deriv_target,
724 deriv_base_commit_id, ancestor_target,
725 label_orig, ondisk_target, ondisk_path);
728 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
731 } else if (!have_local_change && have_incoming_change) {
732 /* Apply the incoming change. */
733 err = update_symlink(ondisk_path, deriv_target,
734 strlen(deriv_target));
737 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
738 } else if (have_local_change && have_incoming_change) {
739 if (deriv_len == ondisk_len &&
740 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
741 /* Both sides made the same change. */
742 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
745 err = install_symlink_conflict(deriv_target,
746 deriv_base_commit_id, ancestor_target, label_orig,
747 ondisk_target, ondisk_path);
750 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
756 free(ancestor_target);
760 static const struct got_error *
761 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
763 const struct got_error *err = NULL;
764 char target_path[PATH_MAX];
773 return got_error_from_errno("got_opentemp");
774 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
775 if (target_len == -1) {
776 err = got_error_from_errno2("readlink", ondisk_path);
779 n = fwrite(target_path, 1, target_len, f);
780 if (n != target_len) {
781 err = got_ferror(f, GOT_ERR_IO);
784 if (fflush(f) == EOF) {
785 err = got_error_from_errno("fflush");
788 if (fseek(f, 0L, SEEK_SET) == -1) {
789 err = got_ferror(f, GOT_ERR_IO);
801 * Perform a 3-way merge where blob_orig acts as the common ancestor,
802 * blob_deriv acts as the first derived version, and the file on disk
803 * acts as the second derived version.
805 static const struct got_error *
806 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
807 struct got_blob_object *blob_orig, const char *ondisk_path,
808 const char *path, uint16_t st_mode, const char *label_orig,
809 struct got_blob_object *blob_deriv,
810 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
811 got_worktree_checkout_cb progress_cb, void *progress_arg)
813 const struct got_error *err = NULL;
814 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
815 char *blob_orig_path = NULL;
816 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
817 char *label_deriv = NULL, *parent = NULL;
819 *local_changes_subsumed = 0;
821 err = got_path_dirname(&parent, ondisk_path);
826 if (asprintf(&base_path, "%s/got-merge-blob-orig",
828 err = got_error_from_errno("asprintf");
833 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
836 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
843 * No common ancestor exists. This is an "add vs add" conflict
844 * and we simply use an empty ancestor file to make both files
845 * appear in the merged result in their entirety.
847 f_orig = got_opentemp();
848 if (f_orig == NULL) {
849 err = got_error_from_errno("got_opentemp");
854 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
855 err = got_error_from_errno("asprintf");
860 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
863 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
868 err = got_object_id_str(&id_str, deriv_base_commit_id);
871 if (asprintf(&label_deriv, "%s: commit %s",
872 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
873 err = got_error_from_errno("asprintf");
878 * In order the run a 3-way merge with a symlink we copy the symlink's
879 * target path into a temporary file and use that file with diff3.
881 if (S_ISLNK(st_mode)) {
882 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
887 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
889 err = got_error_from_errno2("open", ondisk_path);
892 f_deriv2 = fdopen(fd, "r");
893 if (f_deriv2 == NULL) {
894 err = got_error_from_errno2("fdopen", ondisk_path);
900 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
901 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
902 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
904 if (f_orig && fclose(f_orig) == EOF && err == NULL)
905 err = got_error_from_errno("fclose");
906 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
907 err = got_error_from_errno("fclose");
908 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
909 err = got_error_from_errno("fclose");
911 if (blob_orig_path) {
912 unlink(blob_orig_path);
913 free(blob_orig_path);
915 if (blob_deriv_path) {
916 unlink(blob_deriv_path);
917 free(blob_deriv_path);
925 static const struct got_error *
926 create_fileindex_entry(struct got_fileindex_entry **new_iep,
927 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
928 int wt_fd, const char *path, struct got_object_id *blob_id)
930 const struct got_error *err = NULL;
931 struct got_fileindex_entry *new_ie;
935 err = got_fileindex_entry_alloc(&new_ie, path);
939 err = got_fileindex_entry_update(new_ie, wt_fd, path,
940 blob_id->sha1, base_commit_id->sha1, 1);
944 err = got_fileindex_entry_add(fileindex, new_ie);
947 got_fileindex_entry_free(new_ie);
954 get_ondisk_perms(int executable, mode_t st_mode)
956 mode_t xbits = S_IXUSR;
959 /* Map read bits to execute bits. */
960 if (st_mode & S_IRGRP)
962 if (st_mode & S_IROTH)
964 return st_mode | xbits;
967 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
970 /* forward declaration */
971 static const struct got_error *
972 install_blob(struct got_worktree *worktree, const char *ondisk_path,
973 const char *path, mode_t te_mode, mode_t st_mode,
974 struct got_blob_object *blob, int restoring_missing_file,
975 int reverting_versioned_file, int installing_bad_symlink,
976 int path_is_unversioned, struct got_repository *repo,
977 got_worktree_checkout_cb progress_cb, void *progress_arg);
980 * This function assumes that the provided symlink target points at a
981 * safe location in the work tree!
983 static const struct got_error *
984 replace_existing_symlink(int *did_something, const char *ondisk_path,
985 const char *target_path, size_t target_len)
987 const struct got_error *err = NULL;
989 char etarget[PATH_MAX];
995 * "Bad" symlinks (those pointing outside the work tree or into the
996 * .got directory) are installed in the work tree as a regular file
997 * which contains the bad symlink target path.
998 * The new symlink target has already been checked for safety by our
999 * caller. If we can successfully open a regular file then we simply
1000 * replace this file with a symlink below.
1002 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1004 if (!got_err_open_nofollow_on_symlink())
1005 return got_error_from_errno2("open", ondisk_path);
1007 /* We are updating an existing on-disk symlink. */
1008 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1010 return got_error_from_errno2("readlink", ondisk_path);
1012 if (elen == target_len &&
1013 memcmp(etarget, target_path, target_len) == 0)
1014 return NULL; /* nothing to do */
1018 err = update_symlink(ondisk_path, target_path, target_len);
1019 if (fd != -1 && close(fd) == -1 && err == NULL)
1020 err = got_error_from_errno2("close", ondisk_path);
1024 static const struct got_error *
1025 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1026 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1028 const struct got_error *err = NULL;
1029 char canonpath[PATH_MAX];
1030 char *path_got = NULL;
1032 *is_bad_symlink = 0;
1034 if (target_len >= sizeof(canonpath)) {
1035 *is_bad_symlink = 1;
1040 * We do not use realpath(3) to resolve the symlink's target
1041 * path because we don't want to resolve symlinks recursively.
1042 * Instead we make the path absolute and then canonicalize it.
1043 * Relative symlink target lookup should begin at the directory
1044 * in which the blob object is being installed.
1046 if (!got_path_is_absolute(target_path)) {
1047 char *abspath, *parent;
1048 err = got_path_dirname(&parent, ondisk_path);
1051 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1053 return got_error_from_errno("asprintf");
1056 if (strlen(abspath) >= sizeof(canonpath)) {
1057 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1061 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1066 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1071 /* Only allow symlinks pointing at paths within the work tree. */
1072 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1073 *is_bad_symlink = 1;
1077 /* Do not allow symlinks pointing into the .got directory. */
1078 if (asprintf(&path_got, "%s/%s", wtroot_path,
1079 GOT_WORKTREE_GOT_DIR) == -1)
1080 return got_error_from_errno("asprintf");
1081 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1082 *is_bad_symlink = 1;
1088 static const struct got_error *
1089 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1090 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1091 int restoring_missing_file, int reverting_versioned_file,
1092 int path_is_unversioned, int allow_bad_symlinks,
1093 struct got_repository *repo,
1094 got_worktree_checkout_cb progress_cb, void *progress_arg)
1096 const struct got_error *err = NULL;
1097 char target_path[PATH_MAX];
1098 size_t len, target_len = 0;
1099 char *path_got = NULL;
1100 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1101 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1103 *is_bad_symlink = 0;
1106 * Blob object content specifies the target path of the link.
1107 * If a symbolic link cannot be installed we instead create
1108 * a regular file which contains the link target path stored
1109 * in the blob object.
1112 err = got_object_blob_read_block(&len, blob);
1113 if (len + target_len >= sizeof(target_path)) {
1114 /* Path too long; install as a regular file. */
1115 *is_bad_symlink = 1;
1116 got_object_blob_rewind(blob);
1117 return install_blob(worktree, ondisk_path, path,
1118 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1119 restoring_missing_file, reverting_versioned_file,
1120 1, path_is_unversioned, repo, progress_cb,
1124 /* Skip blob object header first time around. */
1125 memcpy(target_path + target_len, buf + hdrlen,
1127 target_len += len - hdrlen;
1131 target_path[target_len] = '\0';
1133 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1134 ondisk_path, worktree->root_path);
1138 if (*is_bad_symlink && !allow_bad_symlinks) {
1139 /* install as a regular file */
1140 got_object_blob_rewind(blob);
1141 err = install_blob(worktree, ondisk_path, path,
1142 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1143 restoring_missing_file, reverting_versioned_file, 1,
1144 path_is_unversioned, repo, progress_cb, progress_arg);
1148 if (symlink(target_path, ondisk_path) == -1) {
1149 if (errno == EEXIST) {
1150 int symlink_replaced;
1151 if (path_is_unversioned) {
1152 err = (*progress_cb)(progress_arg,
1153 GOT_STATUS_UNVERSIONED, path);
1156 err = replace_existing_symlink(&symlink_replaced,
1157 ondisk_path, target_path, target_len);
1161 if (symlink_replaced) {
1162 err = (*progress_cb)(progress_arg,
1163 reverting_versioned_file ?
1165 GOT_STATUS_UPDATE, path);
1167 err = (*progress_cb)(progress_arg,
1168 GOT_STATUS_EXISTS, path);
1171 goto done; /* Nothing else to do. */
1174 if (errno == ENOENT) {
1176 err = got_path_dirname(&parent, ondisk_path);
1179 err = add_dir_on_disk(worktree, parent);
1184 * Retry, and fall through to error handling
1185 * below if this second attempt fails.
1187 if (symlink(target_path, ondisk_path) != -1) {
1188 err = NULL; /* success */
1193 /* Handle errors from first or second creation attempt. */
1194 if (errno == ENAMETOOLONG) {
1195 /* bad target path; install as a regular file */
1196 *is_bad_symlink = 1;
1197 got_object_blob_rewind(blob);
1198 err = install_blob(worktree, ondisk_path, path,
1199 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1200 restoring_missing_file, reverting_versioned_file, 1,
1201 path_is_unversioned, repo,
1202 progress_cb, progress_arg);
1203 } else if (errno == ENOTDIR) {
1204 err = got_error_path(ondisk_path,
1205 GOT_ERR_FILE_OBSTRUCTED);
1207 err = got_error_from_errno3("symlink",
1208 target_path, ondisk_path);
1210 } else if (progress_cb)
1211 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1212 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1218 static const struct got_error *
1219 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1220 const char *path, mode_t te_mode, mode_t st_mode,
1221 struct got_blob_object *blob, int restoring_missing_file,
1222 int reverting_versioned_file, int installing_bad_symlink,
1223 int path_is_unversioned, struct got_repository *repo,
1224 got_worktree_checkout_cb progress_cb, void *progress_arg)
1226 const struct got_error *err = NULL;
1230 char *tmppath = NULL;
1232 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1233 GOT_DEFAULT_FILE_MODE);
1235 if (errno == ENOENT) {
1237 err = got_path_dirname(&parent, path);
1240 err = add_dir_on_disk(worktree, parent);
1244 fd = open(ondisk_path,
1245 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1246 GOT_DEFAULT_FILE_MODE);
1248 return got_error_from_errno2("open",
1250 } else if (errno == EEXIST) {
1251 if (path_is_unversioned) {
1252 err = (*progress_cb)(progress_arg,
1253 GOT_STATUS_UNVERSIONED, path);
1256 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1257 !S_ISREG(st_mode) && !installing_bad_symlink) {
1258 /* TODO file is obstructed; do something */
1259 err = got_error_path(ondisk_path,
1260 GOT_ERR_FILE_OBSTRUCTED);
1263 err = got_opentemp_named_fd(&tmppath, &fd,
1270 return got_error_from_errno2("open", ondisk_path);
1273 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1274 err = got_error_from_errno2("fchmod",
1275 update ? tmppath : ondisk_path);
1280 if (restoring_missing_file)
1281 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1283 else if (reverting_versioned_file)
1284 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1287 err = (*progress_cb)(progress_arg,
1288 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1293 hdrlen = got_object_blob_get_hdrlen(blob);
1295 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1296 err = got_object_blob_read_block(&len, blob);
1300 /* Skip blob object header first time around. */
1301 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1303 err = got_error_from_errno("write");
1305 } else if (outlen != len - hdrlen) {
1306 err = got_error(GOT_ERR_IO);
1313 if (fsync(fd) != 0) {
1314 err = got_error_from_errno("fsync");
1319 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1320 err = got_error_from_errno2("unlink", ondisk_path);
1323 if (rename(tmppath, ondisk_path) != 0) {
1324 err = got_error_from_errno3("rename", tmppath,
1333 if (fd != -1 && close(fd) == -1 && err == NULL)
1334 err = got_error_from_errno("close");
1335 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1336 err = got_error_from_errno2("unlink", tmppath);
1341 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1342 static const struct got_error *
1343 get_modified_file_content_status(unsigned char *status, FILE *f)
1345 const struct got_error *err = NULL;
1346 const char *markers[3] = {
1347 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1348 GOT_DIFF_CONFLICT_MARKER_SEP,
1349 GOT_DIFF_CONFLICT_MARKER_END
1353 size_t linesize = 0;
1356 while (*status == GOT_STATUS_MODIFY) {
1357 linelen = getline(&line, &linesize, f);
1358 if (linelen == -1) {
1361 err = got_ferror(f, GOT_ERR_IO);
1365 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1366 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1368 *status = GOT_STATUS_CONFLICT;
1379 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1381 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1382 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1386 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1388 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1389 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1390 ie->mtime_sec == sb->st_mtim.tv_sec &&
1391 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1392 ie->size == (sb->st_size & 0xffffffff) &&
1393 !xbit_differs(ie, sb->st_mode));
1396 static unsigned char
1397 get_staged_status(struct got_fileindex_entry *ie)
1399 switch (got_fileindex_entry_stage_get(ie)) {
1400 case GOT_FILEIDX_STAGE_ADD:
1401 return GOT_STATUS_ADD;
1402 case GOT_FILEIDX_STAGE_DELETE:
1403 return GOT_STATUS_DELETE;
1404 case GOT_FILEIDX_STAGE_MODIFY:
1405 return GOT_STATUS_MODIFY;
1407 return GOT_STATUS_NO_CHANGE;
1411 static const struct got_error *
1412 get_symlink_modification_status(unsigned char *status,
1413 struct got_fileindex_entry *ie, const char *abspath,
1414 int dirfd, const char *de_name, struct got_blob_object *blob)
1416 const struct got_error *err = NULL;
1417 char target_path[PATH_MAX];
1418 char etarget[PATH_MAX];
1420 size_t len, target_len = 0;
1421 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1422 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1424 *status = GOT_STATUS_NO_CHANGE;
1426 /* Blob object content specifies the target path of the link. */
1428 err = got_object_blob_read_block(&len, blob);
1431 if (len + target_len >= sizeof(target_path)) {
1433 * Should not happen. The blob contents were OK
1434 * when this symlink was installed.
1436 return got_error(GOT_ERR_NO_SPACE);
1439 /* Skip blob object header first time around. */
1440 memcpy(target_path + target_len, buf + hdrlen,
1442 target_len += len - hdrlen;
1446 target_path[target_len] = '\0';
1449 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1451 return got_error_from_errno2("readlinkat", abspath);
1453 elen = readlink(abspath, etarget, sizeof(etarget));
1455 return got_error_from_errno2("readlink", abspath);
1458 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1459 *status = GOT_STATUS_MODIFY;
1464 static const struct got_error *
1465 get_file_status(unsigned char *status, struct stat *sb,
1466 struct got_fileindex_entry *ie, const char *abspath,
1467 int dirfd, const char *de_name, struct got_repository *repo)
1469 const struct got_error *err = NULL;
1470 struct got_object_id id;
1475 struct got_blob_object *blob = NULL;
1477 unsigned char staged_status = get_staged_status(ie);
1479 *status = GOT_STATUS_NO_CHANGE;
1480 memset(sb, 0, sizeof(*sb));
1483 * Whenever the caller provides a directory descriptor and a
1484 * directory entry name for the file, use them! This prevents
1485 * race conditions if filesystem paths change beneath our feet.
1488 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1489 if (errno == ENOENT) {
1490 if (got_fileindex_entry_has_file_on_disk(ie))
1491 *status = GOT_STATUS_MISSING;
1493 *status = GOT_STATUS_DELETE;
1496 err = got_error_from_errno2("fstatat", abspath);
1500 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1501 if (fd == -1 && errno != ENOENT &&
1502 !got_err_open_nofollow_on_symlink())
1503 return got_error_from_errno2("open", abspath);
1504 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1505 if (lstat(abspath, sb) == -1)
1506 return got_error_from_errno2("lstat", abspath);
1507 } else if (fd == -1 || fstat(fd, sb) == -1) {
1508 if (errno == ENOENT) {
1509 if (got_fileindex_entry_has_file_on_disk(ie))
1510 *status = GOT_STATUS_MISSING;
1512 *status = GOT_STATUS_DELETE;
1515 err = got_error_from_errno2("fstat", abspath);
1520 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1521 *status = GOT_STATUS_OBSTRUCTED;
1525 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1526 *status = GOT_STATUS_DELETE;
1528 } else if (!got_fileindex_entry_has_blob(ie) &&
1529 staged_status != GOT_STATUS_ADD) {
1530 *status = GOT_STATUS_ADD;
1534 if (!stat_info_differs(ie, sb))
1537 if (S_ISLNK(sb->st_mode) &&
1538 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1539 *status = GOT_STATUS_MODIFY;
1543 if (staged_status == GOT_STATUS_MODIFY ||
1544 staged_status == GOT_STATUS_ADD)
1545 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1547 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1549 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1553 if (S_ISLNK(sb->st_mode)) {
1554 err = get_symlink_modification_status(status, ie,
1555 abspath, dirfd, de_name, blob);
1560 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1562 err = got_error_from_errno2("openat", abspath);
1567 f = fdopen(fd, "r");
1569 err = got_error_from_errno2("fdopen", abspath);
1573 hdrlen = got_object_blob_get_hdrlen(blob);
1575 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1576 err = got_object_blob_read_block(&blen, blob);
1579 /* Skip length of blob object header first time around. */
1580 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1581 if (flen == 0 && ferror(f)) {
1582 err = got_error_from_errno("fread");
1585 if (blen - hdrlen == 0) {
1587 *status = GOT_STATUS_MODIFY;
1589 } else if (flen == 0) {
1590 if (blen - hdrlen != 0)
1591 *status = GOT_STATUS_MODIFY;
1593 } else if (blen - hdrlen == flen) {
1594 /* Skip blob object header first time around. */
1595 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1596 *status = GOT_STATUS_MODIFY;
1600 *status = GOT_STATUS_MODIFY;
1606 if (*status == GOT_STATUS_MODIFY) {
1608 err = get_modified_file_content_status(status, f);
1609 } else if (xbit_differs(ie, sb->st_mode))
1610 *status = GOT_STATUS_MODE_CHANGE;
1613 got_object_blob_close(blob);
1614 if (f != NULL && fclose(f) == EOF && err == NULL)
1615 err = got_error_from_errno2("fclose", abspath);
1616 if (fd != -1 && close(fd) == -1 && err == NULL)
1617 err = got_error_from_errno2("close", abspath);
1622 * Update timestamps in the file index if a file is unmodified and
1623 * we had to run a full content comparison to find out.
1625 static const struct got_error *
1626 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1627 struct got_fileindex_entry *ie, struct stat *sb)
1629 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1630 return got_fileindex_entry_update(ie, wt_fd, path,
1631 ie->blob_sha1, ie->commit_sha1, 1);
1636 static const struct got_error *
1637 update_blob(struct got_worktree *worktree,
1638 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1639 struct got_tree_entry *te, const char *path,
1640 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1643 const struct got_error *err = NULL;
1644 struct got_blob_object *blob = NULL;
1646 unsigned char status = GOT_STATUS_NO_CHANGE;
1649 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1650 return got_error_from_errno("asprintf");
1653 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1654 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1657 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1661 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1662 sb.st_mode = got_fileindex_perms_to_st(ie);
1664 if (stat(ondisk_path, &sb) == -1) {
1665 if (errno != ENOENT) {
1666 err = got_error_from_errno2("stat",
1670 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1671 status = GOT_STATUS_UNVERSIONED;
1673 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1674 status = GOT_STATUS_UNVERSIONED;
1676 status = GOT_STATUS_OBSTRUCTED;
1680 if (status == GOT_STATUS_OBSTRUCTED) {
1682 got_fileindex_entry_mark_skipped(ie);
1683 err = (*progress_cb)(progress_arg, status, path);
1686 if (status == GOT_STATUS_CONFLICT) {
1688 got_fileindex_entry_mark_skipped(ie);
1689 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1694 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1695 (S_ISLNK(te->mode) ||
1696 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1698 * This is a regular file or an installed bad symlink.
1699 * If the file index indicates that this file is already
1700 * up-to-date with respect to the repository we can skip
1701 * updating contents of this file.
1703 if (got_fileindex_entry_has_commit(ie) &&
1704 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1705 SHA1_DIGEST_LENGTH) == 0) {
1707 err = sync_timestamps(worktree->root_fd,
1708 path, status, ie, &sb);
1711 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1715 if (got_fileindex_entry_has_blob(ie) &&
1716 memcmp(ie->blob_sha1, te->id.sha1,
1717 SHA1_DIGEST_LENGTH) == 0) {
1718 /* Different commit but the same blob. */
1719 err = sync_timestamps(worktree->root_fd,
1720 path, status, ie, &sb);
1723 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1729 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1733 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1734 int update_timestamps;
1735 struct got_blob_object *blob2 = NULL;
1736 char *label_orig = NULL;
1737 if (got_fileindex_entry_has_blob(ie)) {
1738 struct got_object_id id2;
1739 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1740 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1744 if (got_fileindex_entry_has_commit(ie)) {
1745 char id_str[SHA1_DIGEST_STRING_LENGTH];
1746 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1747 sizeof(id_str)) == NULL) {
1748 err = got_error_path(id_str,
1749 GOT_ERR_BAD_OBJ_ID_STR);
1752 if (asprintf(&label_orig, "%s: commit %s",
1753 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1754 err = got_error_from_errno("asprintf");
1758 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1760 err = got_object_blob_read_to_str(&link_target, blob);
1763 err = merge_symlink(worktree, blob2, ondisk_path, path,
1764 label_orig, link_target, worktree->base_commit_id,
1765 repo, progress_cb, progress_arg);
1768 err = merge_blob(&update_timestamps, worktree, blob2,
1769 ondisk_path, path, sb.st_mode, label_orig, blob,
1770 worktree->base_commit_id, repo,
1771 progress_cb, progress_arg);
1775 got_object_blob_close(blob2);
1779 * Do not update timestamps of files with local changes.
1780 * Otherwise, a future status walk would treat them as
1781 * unmodified files again.
1783 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1784 blob->id.sha1, worktree->base_commit_id->sha1,
1786 } else if (status == GOT_STATUS_MODE_CHANGE) {
1787 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1788 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1789 } else if (status == GOT_STATUS_DELETE) {
1790 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1793 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1794 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1798 int is_bad_symlink = 0;
1799 if (S_ISLNK(te->mode)) {
1800 err = install_symlink(&is_bad_symlink, worktree,
1801 ondisk_path, path, blob,
1802 status == GOT_STATUS_MISSING, 0,
1803 status == GOT_STATUS_UNVERSIONED, 0,
1804 repo, progress_cb, progress_arg);
1806 err = install_blob(worktree, ondisk_path, path,
1807 te->mode, sb.st_mode, blob,
1808 status == GOT_STATUS_MISSING, 0, 0,
1809 status == GOT_STATUS_UNVERSIONED, repo,
1810 progress_cb, progress_arg);
1816 err = got_fileindex_entry_update(ie,
1817 worktree->root_fd, path, blob->id.sha1,
1818 worktree->base_commit_id->sha1, 1);
1820 err = create_fileindex_entry(&ie, fileindex,
1821 worktree->base_commit_id, worktree->root_fd, path,
1827 if (is_bad_symlink) {
1828 got_fileindex_entry_filetype_set(ie,
1829 GOT_FILEIDX_MODE_BAD_SYMLINK);
1832 got_object_blob_close(blob);
1838 static const struct got_error *
1839 remove_ondisk_file(const char *root_path, const char *path)
1841 const struct got_error *err = NULL;
1842 char *ondisk_path = NULL, *parent = NULL;
1844 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1845 return got_error_from_errno("asprintf");
1847 if (unlink(ondisk_path) == -1) {
1848 if (errno != ENOENT)
1849 err = got_error_from_errno2("unlink", ondisk_path);
1851 size_t root_len = strlen(root_path);
1852 err = got_path_dirname(&parent, ondisk_path);
1855 while (got_path_cmp(parent, root_path,
1856 strlen(parent), root_len) != 0) {
1858 ondisk_path = parent;
1860 if (rmdir(ondisk_path) == -1) {
1861 if (errno != ENOTEMPTY)
1862 err = got_error_from_errno2("rmdir",
1866 err = got_path_dirname(&parent, ondisk_path);
1877 static const struct got_error *
1878 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1879 struct got_fileindex_entry *ie, struct got_repository *repo,
1880 got_worktree_checkout_cb progress_cb, void *progress_arg)
1882 const struct got_error *err = NULL;
1883 unsigned char status;
1887 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1888 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1890 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1892 return got_error_from_errno("asprintf");
1894 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1898 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1899 char ondisk_target[PATH_MAX];
1900 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1901 sizeof(ondisk_target));
1902 if (ondisk_len == -1) {
1903 err = got_error_from_errno2("readlink", ondisk_path);
1906 ondisk_target[ondisk_len] = '\0';
1907 err = install_symlink_conflict(NULL, worktree->base_commit_id,
1908 NULL, NULL, /* XXX pass common ancestor info? */
1909 ondisk_target, ondisk_path);
1912 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1917 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1918 status == GOT_STATUS_ADD) {
1919 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1923 * Preserve the working file and change the deleted blob's
1924 * entry into a schedule-add entry.
1926 err = got_fileindex_entry_update(ie, worktree->root_fd,
1927 ie->path, NULL, NULL, 0);
1929 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1932 if (status == GOT_STATUS_NO_CHANGE) {
1933 err = remove_ondisk_file(worktree->root_path, ie->path);
1937 got_fileindex_entry_remove(fileindex, ie);
1944 struct diff_cb_arg {
1945 struct got_fileindex *fileindex;
1946 struct got_worktree *worktree;
1947 struct got_repository *repo;
1948 got_worktree_checkout_cb progress_cb;
1950 got_cancel_cb cancel_cb;
1954 static const struct got_error *
1955 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1956 struct got_tree_entry *te, const char *parent_path)
1958 struct diff_cb_arg *a = arg;
1960 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1961 return got_error(GOT_ERR_CANCELLED);
1963 return update_blob(a->worktree, a->fileindex, ie, te,
1964 ie->path, a->repo, a->progress_cb, a->progress_arg);
1967 static const struct got_error *
1968 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1970 struct diff_cb_arg *a = arg;
1972 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1973 return got_error(GOT_ERR_CANCELLED);
1975 return delete_blob(a->worktree, a->fileindex, ie,
1976 a->repo, a->progress_cb, a->progress_arg);
1979 static const struct got_error *
1980 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1982 struct diff_cb_arg *a = arg;
1983 const struct got_error *err;
1986 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1987 return got_error(GOT_ERR_CANCELLED);
1989 if (got_object_tree_entry_is_submodule(te))
1992 if (asprintf(&path, "%s%s%s", parent_path,
1993 parent_path[0] ? "/" : "", te->name)
1995 return got_error_from_errno("asprintf");
1997 if (S_ISDIR(te->mode))
1998 err = add_dir_on_disk(a->worktree, path);
2000 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2001 a->repo, a->progress_cb, a->progress_arg);
2007 const struct got_error *
2008 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2010 uint32_t uuid_status;
2012 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2013 if (uuid_status != uuid_s_ok) {
2015 return got_error_uuid(uuid_status, "uuid_to_string");
2021 static const struct got_error *
2022 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2024 const struct got_error *err = NULL;
2025 char *uuidstr = NULL;
2029 err = got_worktree_get_uuid(&uuidstr, worktree);
2033 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2034 err = got_error_from_errno("asprintf");
2041 const struct got_error *
2042 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2044 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2047 static const struct got_error *
2048 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2050 return get_ref_name(refname, worktree,
2051 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2054 static const struct got_error *
2055 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2057 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2060 static const struct got_error *
2061 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2063 return get_ref_name(refname, worktree,
2064 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2067 static const struct got_error *
2068 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2070 return get_ref_name(refname, worktree,
2071 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2074 static const struct got_error *
2075 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2077 return get_ref_name(refname, worktree,
2078 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2081 static const struct got_error *
2082 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2084 return get_ref_name(refname, worktree,
2085 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2088 static const struct got_error *
2089 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2091 return get_ref_name(refname, worktree,
2092 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2095 static const struct got_error *
2096 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2098 return get_ref_name(refname, worktree,
2099 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2102 const struct got_error *
2103 got_worktree_get_histedit_script_path(char **path,
2104 struct got_worktree *worktree)
2106 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2107 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2109 return got_error_from_errno("asprintf");
2114 static const struct got_error *
2115 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2117 return get_ref_name(refname, worktree,
2118 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2121 static const struct got_error *
2122 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2124 return get_ref_name(refname, worktree,
2125 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2129 * Prevent Git's garbage collector from deleting our base commit by
2130 * setting a reference to our base commit's ID.
2132 static const struct got_error *
2133 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2135 const struct got_error *err = NULL;
2136 struct got_reference *ref = NULL;
2139 err = got_worktree_get_base_ref_name(&refname, worktree);
2143 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2147 err = got_ref_write(ref, repo);
2155 static const struct got_error *
2156 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2158 const struct got_error *err = NULL;
2160 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2161 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2162 err = got_error_from_errno("asprintf");
2163 *fileindex_path = NULL;
2169 static const struct got_error *
2170 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2171 struct got_worktree *worktree)
2173 const struct got_error *err = NULL;
2176 *fileindex_path = NULL;
2177 *fileindex = got_fileindex_alloc();
2178 if (*fileindex == NULL)
2179 return got_error_from_errno("got_fileindex_alloc");
2181 err = get_fileindex_path(fileindex_path, worktree);
2185 index = fopen(*fileindex_path, "rb");
2186 if (index == NULL) {
2187 if (errno != ENOENT)
2188 err = got_error_from_errno2("fopen", *fileindex_path);
2190 err = got_fileindex_read(*fileindex, index);
2191 if (fclose(index) == EOF && err == NULL)
2192 err = got_error_from_errno("fclose");
2196 free(*fileindex_path);
2197 *fileindex_path = NULL;
2198 got_fileindex_free(*fileindex);
2204 struct bump_base_commit_id_arg {
2205 struct got_object_id *base_commit_id;
2208 const char *entry_name;
2209 got_worktree_checkout_cb progress_cb;
2213 /* Bump base commit ID of all files within an updated part of the work tree. */
2214 static const struct got_error *
2215 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2217 const struct got_error *err;
2218 struct bump_base_commit_id_arg *a = arg;
2220 if (a->entry_name) {
2221 if (strcmp(ie->path, a->path) != 0)
2223 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2226 if (got_fileindex_entry_was_skipped(ie))
2229 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2230 SHA1_DIGEST_LENGTH) == 0)
2233 if (a->progress_cb) {
2234 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2239 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2243 static const struct got_error *
2244 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2245 struct got_fileindex *fileindex,
2246 got_worktree_checkout_cb progress_cb, void *progress_arg)
2248 struct bump_base_commit_id_arg bbc_arg;
2250 bbc_arg.base_commit_id = worktree->base_commit_id;
2251 bbc_arg.entry_name = NULL;
2253 bbc_arg.path_len = 0;
2254 bbc_arg.progress_cb = progress_cb;
2255 bbc_arg.progress_arg = progress_arg;
2257 return got_fileindex_for_each_entry_safe(fileindex,
2258 bump_base_commit_id, &bbc_arg);
2261 static const struct got_error *
2262 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2264 const struct got_error *err = NULL;
2265 char *new_fileindex_path = NULL;
2266 FILE *new_index = NULL;
2267 struct timespec timeout;
2269 err = got_opentemp_named(&new_fileindex_path, &new_index,
2274 err = got_fileindex_write(fileindex, new_index);
2278 if (rename(new_fileindex_path, fileindex_path) != 0) {
2279 err = got_error_from_errno3("rename", new_fileindex_path,
2281 unlink(new_fileindex_path);
2285 * Sleep for a short amount of time to ensure that files modified after
2286 * this program exits have a different time stamp from the one which
2287 * was recorded in the file index.
2290 timeout.tv_nsec = 1;
2291 nanosleep(&timeout, NULL);
2295 free(new_fileindex_path);
2299 static const struct got_error *
2300 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2301 struct got_object_id **tree_id, const char *wt_relpath,
2302 struct got_worktree *worktree, struct got_repository *repo)
2304 const struct got_error *err = NULL;
2305 struct got_object_id *id = NULL;
2306 char *in_repo_path = NULL;
2307 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2309 *entry_type = GOT_OBJ_TYPE_ANY;
2310 *tree_relpath = NULL;
2313 if (wt_relpath[0] == '\0') {
2314 /* Check out all files within the work tree. */
2315 *entry_type = GOT_OBJ_TYPE_TREE;
2316 *tree_relpath = strdup("");
2317 if (*tree_relpath == NULL) {
2318 err = got_error_from_errno("strdup");
2321 err = got_object_id_by_path(tree_id, repo,
2322 worktree->base_commit_id, worktree->path_prefix);
2328 /* Check out a subset of files in the work tree. */
2330 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2331 is_root_wt ? "" : "/", wt_relpath) == -1) {
2332 err = got_error_from_errno("asprintf");
2336 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2342 in_repo_path = NULL;
2344 err = got_object_get_type(entry_type, repo, id);
2348 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2349 /* Check out a single file. */
2350 if (strchr(wt_relpath, '/') == NULL) {
2351 /* Check out a single file in work tree's root dir. */
2352 in_repo_path = strdup(worktree->path_prefix);
2353 if (in_repo_path == NULL) {
2354 err = got_error_from_errno("strdup");
2357 *tree_relpath = strdup("");
2358 if (*tree_relpath == NULL) {
2359 err = got_error_from_errno("strdup");
2363 /* Check out a single file in a subdirectory. */
2364 err = got_path_dirname(tree_relpath, wt_relpath);
2367 if (asprintf(&in_repo_path, "%s%s%s",
2368 worktree->path_prefix, is_root_wt ? "" : "/",
2369 *tree_relpath) == -1) {
2370 err = got_error_from_errno("asprintf");
2374 err = got_object_id_by_path(tree_id, repo,
2375 worktree->base_commit_id, in_repo_path);
2377 /* Check out all files within a subdirectory. */
2378 *tree_id = got_object_id_dup(id);
2379 if (*tree_id == NULL) {
2380 err = got_error_from_errno("got_object_id_dup");
2383 *tree_relpath = strdup(wt_relpath);
2384 if (*tree_relpath == NULL) {
2385 err = got_error_from_errno("strdup");
2393 *entry_type = GOT_OBJ_TYPE_ANY;
2394 free(*tree_relpath);
2395 *tree_relpath = NULL;
2402 static const struct got_error *
2403 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2404 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2405 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2406 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2408 const struct got_error *err = NULL;
2409 struct got_commit_object *commit = NULL;
2410 struct got_tree_object *tree = NULL;
2411 struct got_fileindex_diff_tree_cb diff_cb;
2412 struct diff_cb_arg arg;
2414 err = ref_base_commit(worktree, repo);
2416 if (!(err->code == GOT_ERR_ERRNO &&
2417 (errno == EACCES || errno == EROFS)))
2419 err = (*progress_cb)(progress_arg,
2420 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2425 err = got_object_open_as_commit(&commit, repo,
2426 worktree->base_commit_id);
2430 err = got_object_open_as_tree(&tree, repo, tree_id);
2435 got_object_tree_find_entry(tree, entry_name) == NULL) {
2436 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2440 diff_cb.diff_old_new = diff_old_new;
2441 diff_cb.diff_old = diff_old;
2442 diff_cb.diff_new = diff_new;
2443 arg.fileindex = fileindex;
2444 arg.worktree = worktree;
2446 arg.progress_cb = progress_cb;
2447 arg.progress_arg = progress_arg;
2448 arg.cancel_cb = cancel_cb;
2449 arg.cancel_arg = cancel_arg;
2450 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2451 entry_name, repo, &diff_cb, &arg);
2454 got_object_tree_close(tree);
2456 got_object_commit_close(commit);
2460 const struct got_error *
2461 got_worktree_checkout_files(struct got_worktree *worktree,
2462 struct got_pathlist_head *paths, struct got_repository *repo,
2463 got_worktree_checkout_cb progress_cb, void *progress_arg,
2464 got_cancel_cb cancel_cb, void *cancel_arg)
2466 const struct got_error *err = NULL, *sync_err, *unlockerr;
2467 struct got_commit_object *commit = NULL;
2468 struct got_tree_object *tree = NULL;
2469 struct got_fileindex *fileindex = NULL;
2470 char *fileindex_path = NULL;
2471 struct got_pathlist_entry *pe;
2472 struct tree_path_data {
2473 STAILQ_ENTRY(tree_path_data) entry;
2474 struct got_object_id *tree_id;
2479 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2481 STAILQ_INIT(&tree_paths);
2483 err = lock_worktree(worktree, LOCK_EX);
2487 /* Map all specified paths to in-repository trees. */
2488 TAILQ_FOREACH(pe, paths, entry) {
2489 tpd = malloc(sizeof(*tpd));
2491 err = got_error_from_errno("malloc");
2495 err = find_tree_entry_for_checkout(&tpd->entry_type,
2496 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2502 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2503 err = got_path_basename(&tpd->entry_name, pe->path);
2511 tpd->entry_name = NULL;
2513 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2517 * Read the file index.
2518 * Checking out files is supposed to be an idempotent operation.
2519 * If the on-disk file index is incomplete we will try to complete it.
2521 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2525 tpd = STAILQ_FIRST(&tree_paths);
2526 TAILQ_FOREACH(pe, paths, entry) {
2527 struct bump_base_commit_id_arg bbc_arg;
2529 err = checkout_files(worktree, fileindex, tpd->relpath,
2530 tpd->tree_id, tpd->entry_name, repo,
2531 progress_cb, progress_arg, cancel_cb, cancel_arg);
2535 bbc_arg.base_commit_id = worktree->base_commit_id;
2536 bbc_arg.entry_name = tpd->entry_name;
2537 bbc_arg.path = pe->path;
2538 bbc_arg.path_len = pe->path_len;
2539 bbc_arg.progress_cb = progress_cb;
2540 bbc_arg.progress_arg = progress_arg;
2541 err = got_fileindex_for_each_entry_safe(fileindex,
2542 bump_base_commit_id, &bbc_arg);
2546 tpd = STAILQ_NEXT(tpd, entry);
2548 sync_err = sync_fileindex(fileindex, fileindex_path);
2549 if (sync_err && err == NULL)
2552 free(fileindex_path);
2554 got_object_tree_close(tree);
2556 got_object_commit_close(commit);
2558 got_fileindex_free(fileindex);
2559 while (!STAILQ_EMPTY(&tree_paths)) {
2560 tpd = STAILQ_FIRST(&tree_paths);
2561 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2566 unlockerr = lock_worktree(worktree, LOCK_SH);
2567 if (unlockerr && err == NULL)
2572 struct merge_file_cb_arg {
2573 struct got_worktree *worktree;
2574 struct got_fileindex *fileindex;
2575 got_worktree_checkout_cb progress_cb;
2577 got_cancel_cb cancel_cb;
2579 const char *label_orig;
2580 struct got_object_id *commit_id2;
2581 int allow_bad_symlinks;
2584 static const struct got_error *
2585 merge_file_cb(void *arg, struct got_blob_object *blob1,
2586 struct got_blob_object *blob2, struct got_object_id *id1,
2587 struct got_object_id *id2, const char *path1, const char *path2,
2588 mode_t mode1, mode_t mode2, struct got_repository *repo)
2590 static const struct got_error *err = NULL;
2591 struct merge_file_cb_arg *a = arg;
2592 struct got_fileindex_entry *ie;
2593 char *ondisk_path = NULL;
2595 unsigned char status;
2596 int local_changes_subsumed;
2597 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2598 char *id_str = NULL, *label_deriv2 = NULL;
2600 if (blob1 && blob2) {
2601 ie = got_fileindex_entry_get(a->fileindex, path2,
2604 return (*a->progress_cb)(a->progress_arg,
2605 GOT_STATUS_MISSING, path2);
2607 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2609 return got_error_from_errno("asprintf");
2611 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2616 if (status == GOT_STATUS_DELETE) {
2617 err = (*a->progress_cb)(a->progress_arg,
2618 GOT_STATUS_MERGE, path2);
2621 if (status != GOT_STATUS_NO_CHANGE &&
2622 status != GOT_STATUS_MODIFY &&
2623 status != GOT_STATUS_CONFLICT &&
2624 status != GOT_STATUS_ADD) {
2625 err = (*a->progress_cb)(a->progress_arg, status, path2);
2629 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2631 err = got_object_blob_read_to_str(&link_target2, blob2);
2634 err = merge_symlink(a->worktree, blob1, ondisk_path,
2635 path2, a->label_orig, link_target2, a->commit_id2,
2636 repo, a->progress_cb, a->progress_arg);
2641 f_orig = got_opentemp();
2642 if (f_orig == NULL) {
2643 err = got_error_from_errno("got_opentemp");
2646 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2651 f_deriv2 = got_opentemp();
2652 if (f_deriv2 == NULL)
2654 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2659 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2661 err = got_error_from_errno2("open",
2665 f_deriv = fdopen(fd, "r");
2666 if (f_deriv == NULL) {
2667 err = got_error_from_errno2("fdopen",
2672 err = got_object_id_str(&id_str, a->commit_id2);
2675 if (asprintf(&label_deriv2, "%s: commit %s",
2676 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2677 err = got_error_from_errno("asprintf");
2680 err = merge_file(&local_changes_subsumed, a->worktree,
2681 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2682 sb.st_mode, a->label_orig, NULL, label_deriv2,
2683 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2684 a->progress_cb, a->progress_arg);
2687 ie = got_fileindex_entry_get(a->fileindex, path1,
2690 return (*a->progress_cb)(a->progress_arg,
2691 GOT_STATUS_MISSING, path1);
2693 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2695 return got_error_from_errno("asprintf");
2697 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2703 case GOT_STATUS_NO_CHANGE:
2704 err = (*a->progress_cb)(a->progress_arg,
2705 GOT_STATUS_DELETE, path1);
2708 err = remove_ondisk_file(a->worktree->root_path, path1);
2712 got_fileindex_entry_mark_deleted_from_disk(ie);
2714 case GOT_STATUS_DELETE:
2715 case GOT_STATUS_MISSING:
2716 err = (*a->progress_cb)(a->progress_arg,
2717 GOT_STATUS_DELETE, path1);
2721 got_fileindex_entry_mark_deleted_from_disk(ie);
2723 case GOT_STATUS_ADD: {
2724 struct got_object_id *id;
2727 * Delete the added file only if its content already
2728 * exists in the repository.
2730 err = got_object_blob_file_create(&id, &blob1_f, path1);
2733 if (got_object_id_cmp(id, id1) == 0) {
2734 err = (*a->progress_cb)(a->progress_arg,
2735 GOT_STATUS_DELETE, path1);
2738 err = remove_ondisk_file(a->worktree->root_path,
2743 got_fileindex_entry_remove(a->fileindex,
2746 err = (*a->progress_cb)(a->progress_arg,
2747 GOT_STATUS_CANNOT_DELETE, path1);
2749 if (fclose(blob1_f) == EOF && err == NULL)
2750 err = got_error_from_errno("fclose");
2756 case GOT_STATUS_MODIFY:
2757 case GOT_STATUS_CONFLICT:
2758 err = (*a->progress_cb)(a->progress_arg,
2759 GOT_STATUS_CANNOT_DELETE, path1);
2763 case GOT_STATUS_OBSTRUCTED:
2764 err = (*a->progress_cb)(a->progress_arg, status, path1);
2772 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2774 return got_error_from_errno("asprintf");
2775 ie = got_fileindex_entry_get(a->fileindex, path2,
2778 err = get_file_status(&status, &sb, ie, ondisk_path,
2782 if (status != GOT_STATUS_NO_CHANGE &&
2783 status != GOT_STATUS_MODIFY &&
2784 status != GOT_STATUS_CONFLICT &&
2785 status != GOT_STATUS_ADD) {
2786 err = (*a->progress_cb)(a->progress_arg,
2790 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2792 err = got_object_blob_read_to_str(&link_target2,
2796 err = merge_symlink(a->worktree, NULL,
2797 ondisk_path, path2, a->label_orig,
2798 link_target2, a->commit_id2, repo,
2799 a->progress_cb, a->progress_arg);
2801 } else if (S_ISREG(sb.st_mode)) {
2802 err = merge_blob(&local_changes_subsumed,
2803 a->worktree, NULL, ondisk_path, path2,
2804 sb.st_mode, a->label_orig, blob2,
2805 a->commit_id2, repo, a->progress_cb,
2808 err = got_error_path(ondisk_path,
2809 GOT_ERR_FILE_OBSTRUCTED);
2813 if (status == GOT_STATUS_DELETE) {
2814 err = got_fileindex_entry_update(ie,
2815 a->worktree->root_fd, path2, blob2->id.sha1,
2816 a->worktree->base_commit_id->sha1, 0);
2821 int is_bad_symlink = 0;
2822 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2823 if (S_ISLNK(mode2)) {
2824 err = install_symlink(&is_bad_symlink,
2825 a->worktree, ondisk_path, path2, blob2, 0,
2826 0, 1, a->allow_bad_symlinks, repo,
2827 a->progress_cb, a->progress_arg);
2829 err = install_blob(a->worktree, ondisk_path, path2,
2830 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2831 a->progress_cb, a->progress_arg);
2835 err = got_fileindex_entry_alloc(&ie, path2);
2838 err = got_fileindex_entry_update(ie,
2839 a->worktree->root_fd, path2, NULL, NULL, 1);
2841 got_fileindex_entry_free(ie);
2844 err = got_fileindex_entry_add(a->fileindex, ie);
2846 got_fileindex_entry_free(ie);
2849 if (is_bad_symlink) {
2850 got_fileindex_entry_filetype_set(ie,
2851 GOT_FILEIDX_MODE_BAD_SYMLINK);
2856 if (f_orig && fclose(f_orig) == EOF && err == NULL)
2857 err = got_error_from_errno("fclose");
2858 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
2859 err = got_error_from_errno("fclose");
2860 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
2861 err = got_error_from_errno("fclose");
2868 static const struct got_error *
2869 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
2871 struct got_worktree *worktree = arg;
2873 /* Reject merges into a work tree with mixed base commits. */
2874 if (got_fileindex_entry_has_commit(ie) &&
2875 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2876 SHA1_DIGEST_LENGTH) != 0)
2877 return got_error(GOT_ERR_MIXED_COMMITS);
2882 struct check_merge_conflicts_arg {
2883 struct got_worktree *worktree;
2884 struct got_fileindex *fileindex;
2885 struct got_repository *repo;
2888 static const struct got_error *
2889 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
2890 struct got_blob_object *blob2, struct got_object_id *id1,
2891 struct got_object_id *id2, const char *path1, const char *path2,
2892 mode_t mode1, mode_t mode2, struct got_repository *repo)
2894 const struct got_error *err = NULL;
2895 struct check_merge_conflicts_arg *a = arg;
2896 unsigned char status;
2898 struct got_fileindex_entry *ie;
2899 const char *path = path2 ? path2 : path1;
2900 struct got_object_id *id = id2 ? id2 : id1;
2906 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
2910 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2912 return got_error_from_errno("asprintf");
2914 /* Reject merges into a work tree with conflicted files. */
2915 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2919 if (status == GOT_STATUS_CONFLICT)
2920 return got_error(GOT_ERR_CONFLICTS);
2925 static const struct got_error *
2926 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2927 const char *fileindex_path, struct got_object_id *commit_id1,
2928 struct got_object_id *commit_id2, struct got_repository *repo,
2929 got_worktree_checkout_cb progress_cb, void *progress_arg,
2930 got_cancel_cb cancel_cb, void *cancel_arg)
2932 const struct got_error *err = NULL, *sync_err;
2933 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2934 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2935 struct check_merge_conflicts_arg cmc_arg;
2936 struct merge_file_cb_arg arg;
2937 char *label_orig = NULL;
2940 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2941 worktree->path_prefix);
2942 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2948 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2952 err = got_object_id_str(&id_str, commit_id1);
2956 if (asprintf(&label_orig, "%s: commit %s",
2957 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2958 err = got_error_from_errno("asprintf");
2965 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2966 worktree->path_prefix);
2970 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2974 cmc_arg.worktree = worktree;
2975 cmc_arg.fileindex = fileindex;
2976 cmc_arg.repo = repo;
2977 err = got_diff_tree(tree1, tree2, "", "", repo,
2978 check_merge_conflicts, &cmc_arg, 0);
2982 arg.worktree = worktree;
2983 arg.fileindex = fileindex;
2984 arg.progress_cb = progress_cb;
2985 arg.progress_arg = progress_arg;
2986 arg.cancel_cb = cancel_cb;
2987 arg.cancel_arg = cancel_arg;
2988 arg.label_orig = label_orig;
2989 arg.commit_id2 = commit_id2;
2990 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
2991 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2992 sync_err = sync_fileindex(fileindex, fileindex_path);
2993 if (sync_err && err == NULL)
2997 got_object_tree_close(tree1);
2999 got_object_tree_close(tree2);
3004 const struct got_error *
3005 got_worktree_merge_files(struct got_worktree *worktree,
3006 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3007 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3008 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3010 const struct got_error *err, *unlockerr;
3011 char *fileindex_path = NULL;
3012 struct got_fileindex *fileindex = NULL;
3014 err = lock_worktree(worktree, LOCK_EX);
3018 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3022 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3027 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3028 commit_id2, repo, progress_cb, progress_arg,
3029 cancel_cb, cancel_arg);
3032 got_fileindex_free(fileindex);
3033 free(fileindex_path);
3034 unlockerr = lock_worktree(worktree, LOCK_SH);
3035 if (unlockerr && err == NULL)
3040 struct diff_dir_cb_arg {
3041 struct got_fileindex *fileindex;
3042 struct got_worktree *worktree;
3043 const char *status_path;
3044 size_t status_path_len;
3045 struct got_repository *repo;
3046 got_worktree_status_cb status_cb;
3048 got_cancel_cb cancel_cb;
3050 /* A pathlist containing per-directory pathlists of ignore patterns. */
3051 struct got_pathlist_head *ignores;
3052 int report_unchanged;
3056 static const struct got_error *
3057 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3058 int dirfd, const char *de_name,
3059 got_worktree_status_cb status_cb, void *status_arg,
3060 struct got_repository *repo, int report_unchanged)
3062 const struct got_error *err = NULL;
3063 unsigned char status = GOT_STATUS_NO_CHANGE;
3064 unsigned char staged_status = get_staged_status(ie);
3066 struct got_object_id blob_id, commit_id, staged_blob_id;
3067 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3068 struct got_object_id *staged_blob_idp = NULL;
3070 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3074 if (status == GOT_STATUS_NO_CHANGE &&
3075 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3078 if (got_fileindex_entry_has_blob(ie)) {
3079 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3080 blob_idp = &blob_id;
3082 if (got_fileindex_entry_has_commit(ie)) {
3083 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3084 commit_idp = &commit_id;
3086 if (staged_status == GOT_STATUS_ADD ||
3087 staged_status == GOT_STATUS_MODIFY) {
3088 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3089 SHA1_DIGEST_LENGTH);
3090 staged_blob_idp = &staged_blob_id;
3093 return (*status_cb)(status_arg, status, staged_status,
3094 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3097 static const struct got_error *
3098 status_old_new(void *arg, struct got_fileindex_entry *ie,
3099 struct dirent *de, const char *parent_path, int dirfd)
3101 const struct got_error *err = NULL;
3102 struct diff_dir_cb_arg *a = arg;
3105 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3106 return got_error(GOT_ERR_CANCELLED);
3108 if (got_path_cmp(parent_path, a->status_path,
3109 strlen(parent_path), a->status_path_len) != 0 &&
3110 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3113 if (parent_path[0]) {
3114 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3115 parent_path, de->d_name) == -1)
3116 return got_error_from_errno("asprintf");
3118 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3120 return got_error_from_errno("asprintf");
3123 err = report_file_status(ie, abspath, dirfd, de->d_name,
3124 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3129 static const struct got_error *
3130 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3132 struct diff_dir_cb_arg *a = arg;
3133 struct got_object_id blob_id, commit_id;
3134 unsigned char status;
3136 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3137 return got_error(GOT_ERR_CANCELLED);
3139 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3142 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3143 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3144 if (got_fileindex_entry_has_file_on_disk(ie))
3145 status = GOT_STATUS_MISSING;
3147 status = GOT_STATUS_DELETE;
3148 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3149 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3153 free_ignorelist(struct got_pathlist_head *ignorelist)
3155 struct got_pathlist_entry *pe;
3157 TAILQ_FOREACH(pe, ignorelist, entry)
3158 free((char *)pe->path);
3159 got_pathlist_free(ignorelist);
3163 free_ignores(struct got_pathlist_head *ignores)
3165 struct got_pathlist_entry *pe;
3167 TAILQ_FOREACH(pe, ignores, entry) {
3168 struct got_pathlist_head *ignorelist = pe->data;
3169 free_ignorelist(ignorelist);
3170 free((char *)pe->path);
3172 got_pathlist_free(ignores);
3175 static const struct got_error *
3176 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3178 const struct got_error *err = NULL;
3179 struct got_pathlist_entry *pe = NULL;
3180 struct got_pathlist_head *ignorelist;
3181 char *line = NULL, *pattern, *dirpath = NULL;
3182 size_t linesize = 0;
3185 ignorelist = calloc(1, sizeof(*ignorelist));
3186 if (ignorelist == NULL)
3187 return got_error_from_errno("calloc");
3188 TAILQ_INIT(ignorelist);
3190 while ((linelen = getline(&line, &linesize, f)) != -1) {
3191 if (linelen > 0 && line[linelen - 1] == '\n')
3192 line[linelen - 1] = '\0';
3194 /* Git's ignores may contain comments. */
3198 /* Git's negated patterns are not (yet?) supported. */
3202 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3204 err = got_error_from_errno("asprintf");
3207 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3212 err = got_error_from_errno("getline");
3216 dirpath = strdup(path);
3217 if (dirpath == NULL) {
3218 err = got_error_from_errno("strdup");
3221 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3224 if (err || pe == NULL) {
3226 free_ignorelist(ignorelist);
3232 match_ignores(struct got_pathlist_head *ignores, const char *path)
3234 struct got_pathlist_entry *pe;
3236 /* Handle patterns which match in all directories. */
3237 TAILQ_FOREACH(pe, ignores, entry) {
3238 struct got_pathlist_head *ignorelist = pe->data;
3239 struct got_pathlist_entry *pi;
3241 TAILQ_FOREACH(pi, ignorelist, entry) {
3242 const char *p, *pattern = pi->path;
3244 if (strncmp(pattern, "**/", 3) != 0)
3249 if (fnmatch(pattern, p,
3250 FNM_PATHNAME | FNM_LEADING_DIR)) {
3251 /* Retry in next directory. */
3252 while (*p && *p != '/')
3264 * The ignores pathlist contains ignore lists from children before
3265 * parents, so we can find the most specific ignorelist by walking
3266 * ignores backwards.
3268 pe = TAILQ_LAST(ignores, got_pathlist_head);
3270 if (got_path_is_child(path, pe->path, pe->path_len)) {
3271 struct got_pathlist_head *ignorelist = pe->data;
3272 struct got_pathlist_entry *pi;
3273 TAILQ_FOREACH(pi, ignorelist, entry) {
3274 const char *pattern = pi->path;
3275 int flags = FNM_LEADING_DIR;
3276 if (strstr(pattern, "/**/") == NULL)
3277 flags |= FNM_PATHNAME;
3278 if (fnmatch(pattern, path, flags))
3283 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3289 static const struct got_error *
3290 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3291 const char *path, int dirfd, const char *ignores_filename)
3293 const struct got_error *err = NULL;
3296 FILE *ignoresfile = NULL;
3298 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3299 path[0] ? "/" : "", ignores_filename) == -1)
3300 return got_error_from_errno("asprintf");
3303 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3305 if (errno != ENOENT && errno != EACCES)
3306 err = got_error_from_errno2("openat",
3309 ignoresfile = fdopen(fd, "r");
3310 if (ignoresfile == NULL)
3311 err = got_error_from_errno2("fdopen",
3315 err = read_ignores(ignores, path, ignoresfile);
3319 ignoresfile = fopen(ignorespath, "r");
3320 if (ignoresfile == NULL) {
3321 if (errno != ENOENT && errno != EACCES)
3322 err = got_error_from_errno2("fopen",
3325 err = read_ignores(ignores, path, ignoresfile);
3328 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3329 err = got_error_from_errno2("fclose", path);
3330 if (fd != -1 && close(fd) == -1 && err == NULL)
3331 err = got_error_from_errno2("close", path);
3336 static const struct got_error *
3337 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3340 const struct got_error *err = NULL;
3341 struct diff_dir_cb_arg *a = arg;
3347 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3348 return got_error(GOT_ERR_CANCELLED);
3350 if (parent_path[0]) {
3351 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3352 return got_error_from_errno("asprintf");
3357 if (de->d_type == DT_DIR) {
3358 if (!a->no_ignores && ignore != NULL &&
3359 match_ignores(a->ignores, path))
3361 } else if (!match_ignores(a->ignores, path) &&
3362 got_path_is_child(path, a->status_path, a->status_path_len))
3363 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3364 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3370 static const struct got_error *
3371 status_traverse(void *arg, const char *path, int dirfd)
3373 const struct got_error *err = NULL;
3374 struct diff_dir_cb_arg *a = arg;
3379 err = add_ignores(a->ignores, a->worktree->root_path,
3380 path, dirfd, ".cvsignore");
3384 err = add_ignores(a->ignores, a->worktree->root_path, path,
3385 dirfd, ".gitignore");
3390 static const struct got_error *
3391 report_single_file_status(const char *path, const char *ondisk_path,
3392 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3393 void *status_arg, struct got_repository *repo, int report_unchanged,
3394 struct got_pathlist_head *ignores, int no_ignores)
3396 struct got_fileindex_entry *ie;
3399 if (!no_ignores && match_ignores(ignores, path))
3402 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3404 return report_file_status(ie, ondisk_path, -1, NULL,
3405 status_cb, status_arg, repo, report_unchanged);
3407 if (lstat(ondisk_path, &sb) == -1) {
3408 if (errno != ENOENT)
3409 return got_error_from_errno2("lstat", ondisk_path);
3410 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3411 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3415 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3416 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3417 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3422 static const struct got_error *
3423 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3424 const char *root_path, const char *path)
3426 const struct got_error *err;
3427 char *parent_path, *next_parent_path = NULL;
3429 err = add_ignores(ignores, root_path, "", -1,
3434 err = add_ignores(ignores, root_path, "", -1,
3439 err = got_path_dirname(&parent_path, path);
3441 if (err->code == GOT_ERR_BAD_PATH)
3442 return NULL; /* cannot traverse parent */
3446 err = add_ignores(ignores, root_path, parent_path, -1,
3450 err = add_ignores(ignores, root_path, parent_path, -1,
3454 err = got_path_dirname(&next_parent_path, parent_path);
3456 if (err->code == GOT_ERR_BAD_PATH)
3457 err = NULL; /* traversed everything */
3460 if (got_path_is_root_dir(parent_path))
3463 parent_path = next_parent_path;
3464 next_parent_path = NULL;
3468 free(next_parent_path);
3472 static const struct got_error *
3473 worktree_status(struct got_worktree *worktree, const char *path,
3474 struct got_fileindex *fileindex, struct got_repository *repo,
3475 got_worktree_status_cb status_cb, void *status_arg,
3476 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3477 int report_unchanged)
3479 const struct got_error *err = NULL;
3481 struct got_fileindex_diff_dir_cb fdiff_cb;
3482 struct diff_dir_cb_arg arg;
3483 char *ondisk_path = NULL;
3484 struct got_pathlist_head ignores;
3486 TAILQ_INIT(&ignores);
3488 if (asprintf(&ondisk_path, "%s%s%s",
3489 worktree->root_path, path[0] ? "/" : "", path) == -1)
3490 return got_error_from_errno("asprintf");
3492 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3494 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3495 !got_err_open_nofollow_on_symlink())
3496 err = got_error_from_errno2("open", ondisk_path);
3499 err = add_ignores_from_parent_paths(&ignores,
3500 worktree->root_path, ondisk_path);
3504 err = report_single_file_status(path, ondisk_path,
3505 fileindex, status_cb, status_arg, repo,
3506 report_unchanged, &ignores, no_ignores);
3509 fdiff_cb.diff_old_new = status_old_new;
3510 fdiff_cb.diff_old = status_old;
3511 fdiff_cb.diff_new = status_new;
3512 fdiff_cb.diff_traverse = status_traverse;
3513 arg.fileindex = fileindex;
3514 arg.worktree = worktree;
3515 arg.status_path = path;
3516 arg.status_path_len = strlen(path);
3518 arg.status_cb = status_cb;
3519 arg.status_arg = status_arg;
3520 arg.cancel_cb = cancel_cb;
3521 arg.cancel_arg = cancel_arg;
3522 arg.report_unchanged = report_unchanged;
3523 arg.no_ignores = no_ignores;
3525 err = add_ignores_from_parent_paths(&ignores,
3526 worktree->root_path, path);
3530 arg.ignores = &ignores;
3531 err = got_fileindex_diff_dir(fileindex, fd,
3532 worktree->root_path, path, repo, &fdiff_cb, &arg);
3535 free_ignores(&ignores);
3536 if (fd != -1 && close(fd) == -1 && err == NULL)
3537 err = got_error_from_errno("close");
3542 const struct got_error *
3543 got_worktree_status(struct got_worktree *worktree,
3544 struct got_pathlist_head *paths, struct got_repository *repo,
3545 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3546 got_cancel_cb cancel_cb, void *cancel_arg)
3548 const struct got_error *err = NULL;
3549 char *fileindex_path = NULL;
3550 struct got_fileindex *fileindex = NULL;
3551 struct got_pathlist_entry *pe;
3553 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3557 TAILQ_FOREACH(pe, paths, entry) {
3558 err = worktree_status(worktree, pe->path, fileindex, repo,
3559 status_cb, status_arg, cancel_cb, cancel_arg,
3564 free(fileindex_path);
3565 got_fileindex_free(fileindex);
3569 const struct got_error *
3570 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3573 const struct got_error *err = NULL;
3574 char *resolved = NULL, *cwd = NULL, *path = NULL;
3577 char *abspath = NULL;
3578 char canonpath[PATH_MAX];
3582 cwd = getcwd(NULL, 0);
3584 return got_error_from_errno("getcwd");
3586 if (lstat(arg, &sb) == -1) {
3587 if (errno != ENOENT) {
3588 err = got_error_from_errno2("lstat", arg);
3593 if (S_ISLNK(sb.st_mode)) {
3595 * We cannot use realpath(3) with symlinks since we want to
3596 * operate on the symlink itself.
3597 * But we can make the path absolute, assuming it is relative
3598 * to the current working directory, and then canonicalize it.
3600 if (!got_path_is_absolute(arg)) {
3601 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3602 err = got_error_from_errno("asprintf");
3607 err = got_canonpath(abspath ? abspath : arg, canonpath,
3611 resolved = strdup(canonpath);
3612 if (resolved == NULL) {
3613 err = got_error_from_errno("strdup");
3617 resolved = realpath(arg, NULL);
3618 if (resolved == NULL) {
3619 if (errno != ENOENT) {
3620 err = got_error_from_errno2("realpath", arg);
3623 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3624 err = got_error_from_errno("asprintf");
3627 err = got_canonpath(abspath, canonpath,
3631 resolved = strdup(canonpath);
3632 if (resolved == NULL) {
3633 err = got_error_from_errno("strdup");
3639 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3640 strlen(got_worktree_get_root_path(worktree)))) {
3641 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3645 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3646 err = got_path_skip_common_ancestor(&path,
3647 got_worktree_get_root_path(worktree), resolved);
3653 err = got_error_from_errno("strdup");
3658 /* XXX status walk can't deal with trailing slash! */
3660 while (len > 0 && path[len - 1] == '/') {
3661 path[len - 1] = '\0';
3675 struct schedule_addition_args {
3676 struct got_worktree *worktree;
3677 struct got_fileindex *fileindex;
3678 got_worktree_checkout_cb progress_cb;
3680 struct got_repository *repo;
3683 static const struct got_error *
3684 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3685 const char *relpath, struct got_object_id *blob_id,
3686 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3687 int dirfd, const char *de_name)
3689 struct schedule_addition_args *a = arg;
3690 const struct got_error *err = NULL;
3691 struct got_fileindex_entry *ie;
3695 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3697 return got_error_from_errno("asprintf");
3699 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3701 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3705 /* Re-adding an existing entry is a no-op. */
3706 if (status == GOT_STATUS_ADD)
3708 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3713 if (status != GOT_STATUS_UNVERSIONED) {
3714 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3718 err = got_fileindex_entry_alloc(&ie, relpath);
3721 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3722 relpath, NULL, NULL, 1);
3724 got_fileindex_entry_free(ie);
3727 err = got_fileindex_entry_add(a->fileindex, ie);
3729 got_fileindex_entry_free(ie);
3736 if (status == GOT_STATUS_ADD)
3738 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3741 const struct got_error *
3742 got_worktree_schedule_add(struct got_worktree *worktree,
3743 struct got_pathlist_head *paths,
3744 got_worktree_checkout_cb progress_cb, void *progress_arg,
3745 struct got_repository *repo, int no_ignores)
3747 struct got_fileindex *fileindex = NULL;
3748 char *fileindex_path = NULL;
3749 const struct got_error *err = NULL, *sync_err, *unlockerr;
3750 struct got_pathlist_entry *pe;
3751 struct schedule_addition_args saa;
3753 err = lock_worktree(worktree, LOCK_EX);
3757 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3761 saa.worktree = worktree;
3762 saa.fileindex = fileindex;
3763 saa.progress_cb = progress_cb;
3764 saa.progress_arg = progress_arg;
3767 TAILQ_FOREACH(pe, paths, entry) {
3768 err = worktree_status(worktree, pe->path, fileindex, repo,
3769 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3773 sync_err = sync_fileindex(fileindex, fileindex_path);
3774 if (sync_err && err == NULL)
3777 free(fileindex_path);
3779 got_fileindex_free(fileindex);
3780 unlockerr = lock_worktree(worktree, LOCK_SH);
3781 if (unlockerr && err == NULL)
3786 struct schedule_deletion_args {
3787 struct got_worktree *worktree;
3788 struct got_fileindex *fileindex;
3789 got_worktree_delete_cb progress_cb;
3791 struct got_repository *repo;
3792 int delete_local_mods;
3794 const char *status_codes;
3797 static const struct got_error *
3798 schedule_for_deletion(void *arg, unsigned char status,
3799 unsigned char staged_status, const char *relpath,
3800 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3801 struct got_object_id *commit_id, int dirfd, const char *de_name)
3803 struct schedule_deletion_args *a = arg;
3804 const struct got_error *err = NULL;
3805 struct got_fileindex_entry *ie = NULL;
3809 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3811 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3813 staged_status = get_staged_status(ie);
3814 if (staged_status != GOT_STATUS_NO_CHANGE) {
3815 if (staged_status == GOT_STATUS_DELETE)
3817 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3820 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3822 return got_error_from_errno("asprintf");
3824 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3829 if (a->status_codes) {
3830 size_t ncodes = strlen(a->status_codes);
3832 for (i = 0; i < ncodes ; i++) {
3833 if (status == a->status_codes[i])
3837 /* Do not delete files in non-matching status. */
3841 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3842 a->status_codes[i] != GOT_STATUS_MISSING) {
3843 static char msg[64];
3844 snprintf(msg, sizeof(msg),
3845 "invalid status code '%c'", a->status_codes[i]);
3846 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3851 if (status != GOT_STATUS_NO_CHANGE) {
3852 if (status == GOT_STATUS_DELETE)
3854 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3855 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3858 if (status != GOT_STATUS_MODIFY &&
3859 status != GOT_STATUS_MISSING) {
3860 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3865 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3869 if (unlinkat(dirfd, de_name, 0) != 0) {
3870 err = got_error_from_errno2("unlinkat",
3874 } else if (unlink(ondisk_path) != 0) {
3875 err = got_error_from_errno2("unlink", ondisk_path);
3879 root_len = strlen(a->worktree->root_path);
3882 err = got_path_dirname(&parent, ondisk_path);
3886 ondisk_path = parent;
3887 if (rmdir(ondisk_path) == -1) {
3888 if (errno != ENOTEMPTY)
3889 err = got_error_from_errno2("rmdir",
3893 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3894 strlen(ondisk_path), root_len) != 0);
3897 got_fileindex_entry_mark_deleted_from_disk(ie);
3902 if (status == GOT_STATUS_DELETE)
3904 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3905 staged_status, relpath);
3908 const struct got_error *
3909 got_worktree_schedule_delete(struct got_worktree *worktree,
3910 struct got_pathlist_head *paths, int delete_local_mods,
3911 const char *status_codes,
3912 got_worktree_delete_cb progress_cb, void *progress_arg,
3913 struct got_repository *repo, int keep_on_disk)
3915 struct got_fileindex *fileindex = NULL;
3916 char *fileindex_path = NULL;
3917 const struct got_error *err = NULL, *sync_err, *unlockerr;
3918 struct got_pathlist_entry *pe;
3919 struct schedule_deletion_args sda;
3921 err = lock_worktree(worktree, LOCK_EX);
3925 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3929 sda.worktree = worktree;
3930 sda.fileindex = fileindex;
3931 sda.progress_cb = progress_cb;
3932 sda.progress_arg = progress_arg;
3934 sda.delete_local_mods = delete_local_mods;
3935 sda.keep_on_disk = keep_on_disk;
3936 sda.status_codes = status_codes;
3938 TAILQ_FOREACH(pe, paths, entry) {
3939 err = worktree_status(worktree, pe->path, fileindex, repo,
3940 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
3944 sync_err = sync_fileindex(fileindex, fileindex_path);
3945 if (sync_err && err == NULL)
3948 free(fileindex_path);
3950 got_fileindex_free(fileindex);
3951 unlockerr = lock_worktree(worktree, LOCK_SH);
3952 if (unlockerr && err == NULL)
3957 static const struct got_error *
3958 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3960 const struct got_error *err = NULL;
3962 size_t linesize = 0, n;
3965 linelen = getline(&line, &linesize, infile);
3966 if (linelen == -1) {
3967 if (ferror(infile)) {
3968 err = got_error_from_errno("getline");
3974 n = fwrite(line, 1, linelen, outfile);
3976 err = got_ferror(outfile, GOT_ERR_IO);
3981 n = fwrite(line, 1, linelen, rejectfile);
3983 err = got_ferror(outfile, GOT_ERR_IO);
3990 static const struct got_error *
3991 skip_one_line(FILE *f)
3994 size_t linesize = 0;
3997 linelen = getline(&line, &linesize, f);
3998 if (linelen == -1) {
4000 return got_error_from_errno("getline");
4007 static const struct got_error *
4008 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4009 int start_old, int end_old, int start_new, int end_new,
4010 FILE *outfile, FILE *rejectfile)
4012 const struct got_error *err;
4014 /* Copy old file's lines leading up to patch. */
4015 while (!feof(f1) && *line_cur1 < start_old) {
4016 err = copy_one_line(f1, outfile, NULL);
4021 /* Skip new file's lines leading up to patch. */
4022 while (!feof(f2) && *line_cur2 < start_new) {
4024 err = copy_one_line(f2, NULL, rejectfile);
4026 err = skip_one_line(f2);
4031 /* Copy patched lines. */
4032 while (!feof(f2) && *line_cur2 <= end_new) {
4033 err = copy_one_line(f2, outfile, NULL);
4038 /* Skip over old file's replaced lines. */
4039 while (!feof(f1) && *line_cur1 <= end_old) {
4041 err = copy_one_line(f1, NULL, rejectfile);
4043 err = skip_one_line(f1);
4052 static const struct got_error *
4053 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4054 FILE *outfile, FILE *rejectfile)
4056 const struct got_error *err;
4059 /* Copy old file's lines until EOF. */
4061 err = copy_one_line(f1, outfile, NULL);
4068 /* Copy new file's lines until EOF. */
4070 err = copy_one_line(f2, NULL, rejectfile);
4080 static const struct got_error *
4081 apply_or_reject_change(int *choice, int *nchunks_used,
4082 struct diff_result *diff_result, int n,
4083 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4084 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4085 got_worktree_patch_cb patch_cb, void *patch_arg)
4087 const struct got_error *err = NULL;
4088 struct diff_chunk_context cc = {};
4089 int start_old, end_old, start_new, end_new;
4091 struct diff_output_unidiff_state *diff_state;
4092 struct diff_input_info diff_info;
4095 *choice = GOT_PATCH_CHOICE_NONE;
4097 /* Get changed line numbers without context lines for copy_change(). */
4098 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4099 start_old = cc.left.start;
4100 end_old = cc.left.end;
4101 start_new = cc.right.start;
4102 end_new = cc.right.end;
4104 /* Get the same change with context lines for display. */
4105 memset(&cc, 0, sizeof(cc));
4106 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4108 memset(&diff_info, 0, sizeof(diff_info));
4109 diff_info.left_path = relpath;
4110 diff_info.right_path = relpath;
4112 diff_state = diff_output_unidiff_state_alloc();
4113 if (diff_state == NULL)
4114 return got_error_set_errno(ENOMEM,
4115 "diff_output_unidiff_state_alloc");
4117 hunkfile = got_opentemp();
4118 if (hunkfile == NULL) {
4119 err = got_error_from_errno("got_opentemp");
4123 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4125 if (rc != DIFF_RC_OK) {
4126 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4130 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4131 err = got_ferror(hunkfile, GOT_ERR_IO);
4135 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4136 hunkfile, changeno, nchanges);
4141 case GOT_PATCH_CHOICE_YES:
4142 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4143 end_old, start_new, end_new, outfile, rejectfile);
4145 case GOT_PATCH_CHOICE_NO:
4146 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4147 end_old, start_new, end_new, rejectfile, outfile);
4149 case GOT_PATCH_CHOICE_QUIT:
4152 err = got_error(GOT_ERR_PATCH_CHOICE);
4156 diff_output_unidiff_state_free(diff_state);
4157 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4158 err = got_error_from_errno("fclose");
4162 struct revert_file_args {
4163 struct got_worktree *worktree;
4164 struct got_fileindex *fileindex;
4165 got_worktree_checkout_cb progress_cb;
4167 got_worktree_patch_cb patch_cb;
4169 struct got_repository *repo;
4170 int unlink_added_files;
4173 static const struct got_error *
4174 create_patched_content(char **path_outfile, int reverse_patch,
4175 struct got_object_id *blob_id, const char *path2,
4176 int dirfd2, const char *de_name2,
4177 const char *relpath, struct got_repository *repo,
4178 got_worktree_patch_cb patch_cb, void *patch_arg)
4180 const struct got_error *err, *free_err;
4181 struct got_blob_object *blob = NULL;
4182 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4184 char link_target[PATH_MAX];
4185 ssize_t link_len = 0;
4186 char *path1 = NULL, *id_str = NULL;
4188 struct got_diffreg_result *diffreg_result = NULL;
4189 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4190 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4192 *path_outfile = NULL;
4194 err = got_object_id_str(&id_str, blob_id);
4199 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4201 if (!got_err_open_nofollow_on_symlink()) {
4202 err = got_error_from_errno2("openat", path2);
4205 link_len = readlinkat(dirfd2, de_name2,
4206 link_target, sizeof(link_target));
4208 return got_error_from_errno2("readlinkat", path2);
4209 sb2.st_mode = S_IFLNK;
4210 sb2.st_size = link_len;
4213 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4215 if (!got_err_open_nofollow_on_symlink()) {
4216 err = got_error_from_errno2("open", path2);
4219 link_len = readlink(path2, link_target,
4220 sizeof(link_target));
4222 return got_error_from_errno2("readlink", path2);
4223 sb2.st_mode = S_IFLNK;
4224 sb2.st_size = link_len;
4228 if (fstat(fd2, &sb2) == -1) {
4229 err = got_error_from_errno2("fstat", path2);
4233 f2 = fdopen(fd2, "r");
4235 err = got_error_from_errno2("fdopen", path2);
4241 f2 = got_opentemp();
4243 err = got_error_from_errno2("got_opentemp", path2);
4246 n = fwrite(link_target, 1, link_len, f2);
4247 if (n != link_len) {
4248 err = got_ferror(f2, GOT_ERR_IO);
4251 if (fflush(f2) == EOF) {
4252 err = got_error_from_errno("fflush");
4258 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4262 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4266 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4270 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4275 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4279 if (fseek(f1, 0L, SEEK_SET) == -1)
4280 return got_ferror(f1, GOT_ERR_IO);
4281 if (fseek(f2, 0L, SEEK_SET) == -1)
4282 return got_ferror(f2, GOT_ERR_IO);
4284 /* Count the number of actual changes in the diff result. */
4285 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4286 struct diff_chunk_context cc = {};
4287 diff_chunk_context_load_change(&cc, &nchunks_used,
4288 diffreg_result->result, n, 0);
4291 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4293 err = apply_or_reject_change(&choice, &nchunks_used,
4294 diffreg_result->result, n, relpath, f1, f2,
4295 &line_cur1, &line_cur2,
4296 reverse_patch ? NULL : outfile,
4297 reverse_patch ? outfile : NULL,
4298 ++i, nchanges, patch_cb, patch_arg);
4301 if (choice == GOT_PATCH_CHOICE_YES)
4303 else if (choice == GOT_PATCH_CHOICE_QUIT)
4307 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4308 reverse_patch ? NULL : outfile,
4309 reverse_patch ? outfile : NULL);
4313 if (!S_ISLNK(sb2.st_mode)) {
4314 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4315 err = got_error_from_errno2("fchmod", path2);
4323 got_object_blob_close(blob);
4324 free_err = got_diffreg_result_free(diffreg_result);
4327 if (f1 && fclose(f1) == EOF && err == NULL)
4328 err = got_error_from_errno2("fclose", path1);
4329 if (f2 && fclose(f2) == EOF && err == NULL)
4330 err = got_error_from_errno2("fclose", path2);
4331 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4332 err = got_error_from_errno2("close", path2);
4333 if (outfile && fclose(outfile) == EOF && err == NULL)
4334 err = got_error_from_errno2("fclose", *path_outfile);
4335 if (path1 && unlink(path1) == -1 && err == NULL)
4336 err = got_error_from_errno2("unlink", path1);
4337 if (err || !have_content) {
4338 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4339 err = got_error_from_errno2("unlink", *path_outfile);
4340 free(*path_outfile);
4341 *path_outfile = NULL;
4347 static const struct got_error *
4348 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4349 const char *relpath, struct got_object_id *blob_id,
4350 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4351 int dirfd, const char *de_name)
4353 struct revert_file_args *a = arg;
4354 const struct got_error *err = NULL;
4355 char *parent_path = NULL;
4356 struct got_fileindex_entry *ie;
4357 struct got_tree_object *tree = NULL;
4358 struct got_object_id *tree_id = NULL;
4359 const struct got_tree_entry *te = NULL;
4360 char *tree_path = NULL, *te_name;
4361 char *ondisk_path = NULL, *path_content = NULL;
4362 struct got_blob_object *blob = NULL;
4364 /* Reverting a staged deletion is a no-op. */
4365 if (status == GOT_STATUS_DELETE &&
4366 staged_status != GOT_STATUS_NO_CHANGE)
4369 if (status == GOT_STATUS_UNVERSIONED)
4370 return (*a->progress_cb)(a->progress_arg,
4371 GOT_STATUS_UNVERSIONED, relpath);
4373 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4375 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4377 /* Construct in-repository path of tree which contains this blob. */
4378 err = got_path_dirname(&parent_path, ie->path);
4380 if (err->code != GOT_ERR_BAD_PATH)
4382 parent_path = strdup("/");
4383 if (parent_path == NULL) {
4384 err = got_error_from_errno("strdup");
4388 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4389 tree_path = strdup(parent_path);
4390 if (tree_path == NULL) {
4391 err = got_error_from_errno("strdup");
4395 if (got_path_is_root_dir(parent_path)) {
4396 tree_path = strdup(a->worktree->path_prefix);
4397 if (tree_path == NULL) {
4398 err = got_error_from_errno("strdup");
4402 if (asprintf(&tree_path, "%s/%s",
4403 a->worktree->path_prefix, parent_path) == -1) {
4404 err = got_error_from_errno("asprintf");
4410 err = got_object_id_by_path(&tree_id, a->repo,
4411 a->worktree->base_commit_id, tree_path);
4413 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4414 (status == GOT_STATUS_ADD ||
4415 staged_status == GOT_STATUS_ADD)))
4418 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4422 err = got_path_basename(&te_name, ie->path);
4426 te = got_object_tree_find_entry(tree, te_name);
4428 if (te == NULL && status != GOT_STATUS_ADD &&
4429 staged_status != GOT_STATUS_ADD) {
4430 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4436 case GOT_STATUS_ADD:
4438 int choice = GOT_PATCH_CHOICE_NONE;
4439 err = (*a->patch_cb)(&choice, a->patch_arg,
4440 status, ie->path, NULL, 1, 1);
4443 if (choice != GOT_PATCH_CHOICE_YES)
4446 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4450 got_fileindex_entry_remove(a->fileindex, ie);
4451 if (a->unlink_added_files) {
4452 if (asprintf(&ondisk_path, "%s/%s",
4453 got_worktree_get_root_path(a->worktree),
4455 err = got_error_from_errno("asprintf");
4458 if (unlink(ondisk_path) == -1) {
4459 err = got_error_from_errno2("unlink",
4465 case GOT_STATUS_DELETE:
4467 int choice = GOT_PATCH_CHOICE_NONE;
4468 err = (*a->patch_cb)(&choice, a->patch_arg,
4469 status, ie->path, NULL, 1, 1);
4472 if (choice != GOT_PATCH_CHOICE_YES)
4476 case GOT_STATUS_MODIFY:
4477 case GOT_STATUS_MODE_CHANGE:
4478 case GOT_STATUS_CONFLICT:
4479 case GOT_STATUS_MISSING: {
4480 struct got_object_id id;
4481 if (staged_status == GOT_STATUS_ADD ||
4482 staged_status == GOT_STATUS_MODIFY) {
4483 memcpy(id.sha1, ie->staged_blob_sha1,
4484 SHA1_DIGEST_LENGTH);
4486 memcpy(id.sha1, ie->blob_sha1,
4487 SHA1_DIGEST_LENGTH);
4488 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4492 if (asprintf(&ondisk_path, "%s/%s",
4493 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4494 err = got_error_from_errno("asprintf");
4498 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4499 status == GOT_STATUS_CONFLICT)) {
4500 int is_bad_symlink = 0;
4501 err = create_patched_content(&path_content, 1, &id,
4502 ondisk_path, dirfd, de_name, ie->path, a->repo,
4503 a->patch_cb, a->patch_arg);
4504 if (err || path_content == NULL)
4506 if (te && S_ISLNK(te->mode)) {
4507 if (unlink(path_content) == -1) {
4508 err = got_error_from_errno2("unlink",
4512 err = install_symlink(&is_bad_symlink,
4513 a->worktree, ondisk_path, ie->path,
4514 blob, 0, 1, 0, 0, a->repo,
4515 a->progress_cb, a->progress_arg);
4517 if (rename(path_content, ondisk_path) == -1) {
4518 err = got_error_from_errno3("rename",
4519 path_content, ondisk_path);
4524 int is_bad_symlink = 0;
4525 if (te && S_ISLNK(te->mode)) {
4526 err = install_symlink(&is_bad_symlink,
4527 a->worktree, ondisk_path, ie->path,
4528 blob, 0, 1, 0, 0, a->repo,
4529 a->progress_cb, a->progress_arg);
4531 err = install_blob(a->worktree, ondisk_path,
4533 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4534 got_fileindex_perms_to_st(ie), blob,
4535 0, 1, 0, 0, a->repo,
4536 a->progress_cb, a->progress_arg);
4540 if (status == GOT_STATUS_DELETE ||
4541 status == GOT_STATUS_MODE_CHANGE) {
4542 err = got_fileindex_entry_update(ie,
4543 a->worktree->root_fd, relpath,
4545 a->worktree->base_commit_id->sha1, 1);
4549 if (is_bad_symlink) {
4550 got_fileindex_entry_filetype_set(ie,
4551 GOT_FILEIDX_MODE_BAD_SYMLINK);
4565 got_object_blob_close(blob);
4567 got_object_tree_close(tree);
4572 const struct got_error *
4573 got_worktree_revert(struct got_worktree *worktree,
4574 struct got_pathlist_head *paths,
4575 got_worktree_checkout_cb progress_cb, void *progress_arg,
4576 got_worktree_patch_cb patch_cb, void *patch_arg,
4577 struct got_repository *repo)
4579 struct got_fileindex *fileindex = NULL;
4580 char *fileindex_path = NULL;
4581 const struct got_error *err = NULL, *unlockerr = NULL;
4582 const struct got_error *sync_err = NULL;
4583 struct got_pathlist_entry *pe;
4584 struct revert_file_args rfa;
4586 err = lock_worktree(worktree, LOCK_EX);
4590 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4594 rfa.worktree = worktree;
4595 rfa.fileindex = fileindex;
4596 rfa.progress_cb = progress_cb;
4597 rfa.progress_arg = progress_arg;
4598 rfa.patch_cb = patch_cb;
4599 rfa.patch_arg = patch_arg;
4601 rfa.unlink_added_files = 0;
4602 TAILQ_FOREACH(pe, paths, entry) {
4603 err = worktree_status(worktree, pe->path, fileindex, repo,
4604 revert_file, &rfa, NULL, NULL, 1, 0);
4608 sync_err = sync_fileindex(fileindex, fileindex_path);
4609 if (sync_err && err == NULL)
4612 free(fileindex_path);
4614 got_fileindex_free(fileindex);
4615 unlockerr = lock_worktree(worktree, LOCK_SH);
4616 if (unlockerr && err == NULL)
4622 free_commitable(struct got_commitable *ct)
4625 free(ct->in_repo_path);
4626 free(ct->ondisk_path);
4628 free(ct->base_blob_id);
4629 free(ct->staged_blob_id);
4630 free(ct->base_commit_id);
4634 struct collect_commitables_arg {
4635 struct got_pathlist_head *commitable_paths;
4636 struct got_repository *repo;
4637 struct got_worktree *worktree;
4638 struct got_fileindex *fileindex;
4639 int have_staged_files;
4640 int allow_bad_symlinks;
4643 static const struct got_error *
4644 collect_commitables(void *arg, unsigned char status,
4645 unsigned char staged_status, const char *relpath,
4646 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4647 struct got_object_id *commit_id, int dirfd, const char *de_name)
4649 struct collect_commitables_arg *a = arg;
4650 const struct got_error *err = NULL;
4651 struct got_commitable *ct = NULL;
4652 struct got_pathlist_entry *new = NULL;
4653 char *parent_path = NULL, *path = NULL;
4656 if (a->have_staged_files) {
4657 if (staged_status != GOT_STATUS_MODIFY &&
4658 staged_status != GOT_STATUS_ADD &&
4659 staged_status != GOT_STATUS_DELETE)
4662 if (status == GOT_STATUS_CONFLICT)
4663 return got_error(GOT_ERR_COMMIT_CONFLICT);
4665 if (status != GOT_STATUS_MODIFY &&
4666 status != GOT_STATUS_MODE_CHANGE &&
4667 status != GOT_STATUS_ADD &&
4668 status != GOT_STATUS_DELETE)
4672 if (asprintf(&path, "/%s", relpath) == -1) {
4673 err = got_error_from_errno("asprintf");
4676 if (strcmp(path, "/") == 0) {
4677 parent_path = strdup("");
4678 if (parent_path == NULL)
4679 return got_error_from_errno("strdup");
4681 err = got_path_dirname(&parent_path, path);
4686 ct = calloc(1, sizeof(*ct));
4688 err = got_error_from_errno("calloc");
4692 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4694 err = got_error_from_errno("asprintf");
4698 if (staged_status == GOT_STATUS_ADD ||
4699 staged_status == GOT_STATUS_MODIFY) {
4700 struct got_fileindex_entry *ie;
4701 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4702 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4703 case GOT_FILEIDX_MODE_REGULAR_FILE:
4704 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4707 case GOT_FILEIDX_MODE_SYMLINK:
4711 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4714 ct->mode |= got_fileindex_entry_perms_get(ie);
4715 } else if (status != GOT_STATUS_DELETE &&
4716 staged_status != GOT_STATUS_DELETE) {
4718 if (fstatat(dirfd, de_name, &sb,
4719 AT_SYMLINK_NOFOLLOW) == -1) {
4720 err = got_error_from_errno2("fstatat",
4724 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4725 err = got_error_from_errno2("lstat", ct->ondisk_path);
4728 ct->mode = sb.st_mode;
4731 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4732 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4734 err = got_error_from_errno("asprintf");
4738 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4739 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4741 char target_path[PATH_MAX];
4743 target_len = readlink(ct->ondisk_path, target_path,
4744 sizeof(target_path));
4745 if (target_len == -1) {
4746 err = got_error_from_errno2("readlink",
4750 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4751 target_len, ct->ondisk_path, a->worktree->root_path);
4754 if (is_bad_symlink) {
4755 err = got_error_path(ct->ondisk_path,
4756 GOT_ERR_BAD_SYMLINK);
4762 ct->status = status;
4763 ct->staged_status = staged_status;
4764 ct->blob_id = NULL; /* will be filled in when blob gets created */
4765 if (ct->status != GOT_STATUS_ADD &&
4766 ct->staged_status != GOT_STATUS_ADD) {
4767 ct->base_blob_id = got_object_id_dup(blob_id);
4768 if (ct->base_blob_id == NULL) {
4769 err = got_error_from_errno("got_object_id_dup");
4772 ct->base_commit_id = got_object_id_dup(commit_id);
4773 if (ct->base_commit_id == NULL) {
4774 err = got_error_from_errno("got_object_id_dup");
4778 if (ct->staged_status == GOT_STATUS_ADD ||
4779 ct->staged_status == GOT_STATUS_MODIFY) {
4780 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4781 if (ct->staged_blob_id == NULL) {
4782 err = got_error_from_errno("got_object_id_dup");
4786 ct->path = strdup(path);
4787 if (ct->path == NULL) {
4788 err = got_error_from_errno("strdup");
4791 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4793 if (ct && (err || new == NULL))
4794 free_commitable(ct);
4800 static const struct got_error *write_tree(struct got_object_id **, int *,
4801 struct got_tree_object *, const char *, struct got_pathlist_head *,
4802 got_worktree_status_cb status_cb, void *status_arg,
4803 struct got_repository *);
4805 static const struct got_error *
4806 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4807 struct got_tree_entry *te, const char *parent_path,
4808 struct got_pathlist_head *commitable_paths,
4809 got_worktree_status_cb status_cb, void *status_arg,
4810 struct got_repository *repo)
4812 const struct got_error *err = NULL;
4813 struct got_tree_object *subtree;
4816 if (asprintf(&subpath, "%s%s%s", parent_path,
4817 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4818 return got_error_from_errno("asprintf");
4820 err = got_object_open_as_tree(&subtree, repo, &te->id);
4824 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4825 commitable_paths, status_cb, status_arg, repo);
4826 got_object_tree_close(subtree);
4831 static const struct got_error *
4832 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4834 const struct got_error *err = NULL;
4835 char *ct_parent_path = NULL;
4839 if (strchr(ct->in_repo_path, '/') == NULL) {
4840 *match = got_path_is_root_dir(path);
4844 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4847 *match = (strcmp(path, ct_parent_path) == 0);
4848 free(ct_parent_path);
4853 get_ct_file_mode(struct got_commitable *ct)
4855 if (S_ISLNK(ct->mode))
4858 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4861 static const struct got_error *
4862 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4863 struct got_tree_entry *te, struct got_commitable *ct)
4865 const struct got_error *err = NULL;
4869 err = got_object_tree_entry_dup(new_te, te);
4873 (*new_te)->mode = get_ct_file_mode(ct);
4875 if (ct->staged_status == GOT_STATUS_MODIFY)
4876 memcpy(&(*new_te)->id, ct->staged_blob_id,
4877 sizeof((*new_te)->id));
4879 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4881 if (err && *new_te) {
4888 static const struct got_error *
4889 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4890 struct got_commitable *ct)
4892 const struct got_error *err = NULL;
4893 char *ct_name = NULL;
4897 *new_te = calloc(1, sizeof(**new_te));
4898 if (*new_te == NULL)
4899 return got_error_from_errno("calloc");
4901 err = got_path_basename(&ct_name, ct->path);
4904 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4905 sizeof((*new_te)->name)) {
4906 err = got_error(GOT_ERR_NO_SPACE);
4910 (*new_te)->mode = get_ct_file_mode(ct);
4912 if (ct->staged_status == GOT_STATUS_ADD)
4913 memcpy(&(*new_te)->id, ct->staged_blob_id,
4914 sizeof((*new_te)->id));
4916 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4919 if (err && *new_te) {
4926 static const struct got_error *
4927 insert_tree_entry(struct got_tree_entry *new_te,
4928 struct got_pathlist_head *paths)
4930 const struct got_error *err = NULL;
4931 struct got_pathlist_entry *new_pe;
4933 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4937 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4941 static const struct got_error *
4942 report_ct_status(struct got_commitable *ct,
4943 got_worktree_status_cb status_cb, void *status_arg)
4945 const char *ct_path = ct->path;
4946 unsigned char status;
4948 if (status_cb == NULL) /* no commit progress output desired */
4951 while (ct_path[0] == '/')
4954 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4955 status = ct->staged_status;
4957 status = ct->status;
4959 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4960 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4963 static const struct got_error *
4964 match_modified_subtree(int *modified, struct got_tree_entry *te,
4965 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4967 const struct got_error *err = NULL;
4968 struct got_pathlist_entry *pe;
4973 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4974 got_path_is_root_dir(base_tree_path) ? "" : "/",
4976 return got_error_from_errno("asprintf");
4978 TAILQ_FOREACH(pe, commitable_paths, entry) {
4979 struct got_commitable *ct = pe->data;
4980 *modified = got_path_is_child(ct->in_repo_path, te_path,
4990 static const struct got_error *
4991 match_deleted_or_modified_ct(struct got_commitable **ctp,
4992 struct got_tree_entry *te, const char *base_tree_path,
4993 struct got_pathlist_head *commitable_paths)
4995 const struct got_error *err = NULL;
4996 struct got_pathlist_entry *pe;
5000 TAILQ_FOREACH(pe, commitable_paths, entry) {
5001 struct got_commitable *ct = pe->data;
5002 char *ct_name = NULL;
5005 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5006 if (ct->status != GOT_STATUS_MODIFY &&
5007 ct->status != GOT_STATUS_MODE_CHANGE &&
5008 ct->status != GOT_STATUS_DELETE)
5011 if (ct->staged_status != GOT_STATUS_MODIFY &&
5012 ct->staged_status != GOT_STATUS_DELETE)
5016 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5019 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5025 err = got_path_basename(&ct_name, pe->path);
5029 if (strcmp(te->name, ct_name) != 0) {
5042 static const struct got_error *
5043 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5044 const char *child_path, const char *path_base_tree,
5045 struct got_pathlist_head *commitable_paths,
5046 got_worktree_status_cb status_cb, void *status_arg,
5047 struct got_repository *repo)
5049 const struct got_error *err = NULL;
5050 struct got_tree_entry *new_te;
5052 struct got_object_id *id = NULL;
5057 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5058 got_path_is_root_dir(path_base_tree) ? "" : "/",
5060 return got_error_from_errno("asprintf");
5062 new_te = calloc(1, sizeof(*new_te));
5064 return got_error_from_errno("calloc");
5065 new_te->mode = S_IFDIR;
5067 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5068 sizeof(new_te->name)) {
5069 err = got_error(GOT_ERR_NO_SPACE);
5072 err = write_tree(&id, &nentries, NULL, subtree_path,
5073 commitable_paths, status_cb, status_arg, repo);
5078 memcpy(&new_te->id, id, sizeof(new_te->id));
5087 static const struct got_error *
5088 write_tree(struct got_object_id **new_tree_id, int *nentries,
5089 struct got_tree_object *base_tree, const char *path_base_tree,
5090 struct got_pathlist_head *commitable_paths,
5091 got_worktree_status_cb status_cb, void *status_arg,
5092 struct got_repository *repo)
5094 const struct got_error *err = NULL;
5095 struct got_pathlist_head paths;
5096 struct got_tree_entry *te, *new_te = NULL;
5097 struct got_pathlist_entry *pe;
5102 /* Insert, and recurse into, newly added entries first. */
5103 TAILQ_FOREACH(pe, commitable_paths, entry) {
5104 struct got_commitable *ct = pe->data;
5105 char *child_path = NULL, *slash;
5107 if ((ct->status != GOT_STATUS_ADD &&
5108 ct->staged_status != GOT_STATUS_ADD) ||
5109 (ct->flags & GOT_COMMITABLE_ADDED))
5112 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5113 strlen(path_base_tree)))
5116 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5121 slash = strchr(child_path, '/');
5122 if (slash == NULL) {
5123 err = alloc_added_blob_tree_entry(&new_te, ct);
5126 err = report_ct_status(ct, status_cb, status_arg);
5129 ct->flags |= GOT_COMMITABLE_ADDED;
5130 err = insert_tree_entry(new_te, &paths);
5135 *slash = '\0'; /* trim trailing path components */
5136 if (base_tree == NULL ||
5137 got_object_tree_find_entry(base_tree, child_path)
5139 err = make_subtree_for_added_blob(&new_te,
5140 child_path, path_base_tree,
5141 commitable_paths, status_cb, status_arg,
5145 err = insert_tree_entry(new_te, &paths);
5154 int i, nbase_entries;
5155 /* Handle modified and deleted entries. */
5156 nbase_entries = got_object_tree_get_nentries(base_tree);
5157 for (i = 0; i < nbase_entries; i++) {
5158 struct got_commitable *ct = NULL;
5160 te = got_object_tree_get_entry(base_tree, i);
5161 if (got_object_tree_entry_is_submodule(te)) {
5162 /* Entry is a submodule; just copy it. */
5163 err = got_object_tree_entry_dup(&new_te, te);
5166 err = insert_tree_entry(new_te, &paths);
5173 if (S_ISDIR(te->mode)) {
5175 err = got_object_tree_entry_dup(&new_te, te);
5178 err = match_modified_subtree(&modified, te,
5179 path_base_tree, commitable_paths);
5182 /* Avoid recursion into unmodified subtrees. */
5184 struct got_object_id *new_id;
5186 err = write_subtree(&new_id,
5188 path_base_tree, commitable_paths,
5189 status_cb, status_arg, repo);
5192 if (nsubentries == 0) {
5193 /* All entries were deleted. */
5197 memcpy(&new_te->id, new_id,
5198 sizeof(new_te->id));
5201 err = insert_tree_entry(new_te, &paths);
5208 err = match_deleted_or_modified_ct(&ct, te,
5209 path_base_tree, commitable_paths);
5213 /* NB: Deleted entries get dropped here. */
5214 if (ct->status == GOT_STATUS_MODIFY ||
5215 ct->status == GOT_STATUS_MODE_CHANGE ||
5216 ct->staged_status == GOT_STATUS_MODIFY) {
5217 err = alloc_modified_blob_tree_entry(
5221 err = insert_tree_entry(new_te, &paths);
5226 err = report_ct_status(ct, status_cb,
5231 /* Entry is unchanged; just copy it. */
5232 err = got_object_tree_entry_dup(&new_te, te);
5235 err = insert_tree_entry(new_te, &paths);
5243 /* Write new list of entries; deleted entries have been dropped. */
5244 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5246 got_pathlist_free(&paths);
5250 static const struct got_error *
5251 update_fileindex_after_commit(struct got_worktree *worktree,
5252 struct got_pathlist_head *commitable_paths,
5253 struct got_object_id *new_base_commit_id,
5254 struct got_fileindex *fileindex, int have_staged_files)
5256 const struct got_error *err = NULL;
5257 struct got_pathlist_entry *pe;
5258 char *relpath = NULL;
5260 TAILQ_FOREACH(pe, commitable_paths, entry) {
5261 struct got_fileindex_entry *ie;
5262 struct got_commitable *ct = pe->data;
5264 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5266 err = got_path_skip_common_ancestor(&relpath,
5267 worktree->root_path, ct->ondisk_path);
5272 if (ct->status == GOT_STATUS_DELETE ||
5273 ct->staged_status == GOT_STATUS_DELETE) {
5274 got_fileindex_entry_remove(fileindex, ie);
5275 } else if (ct->staged_status == GOT_STATUS_ADD ||
5276 ct->staged_status == GOT_STATUS_MODIFY) {
5277 got_fileindex_entry_stage_set(ie,
5278 GOT_FILEIDX_STAGE_NONE);
5279 got_fileindex_entry_staged_filetype_set(ie, 0);
5281 err = got_fileindex_entry_update(ie,
5282 worktree->root_fd, relpath,
5283 ct->staged_blob_id->sha1,
5284 new_base_commit_id->sha1,
5285 !have_staged_files);
5287 err = got_fileindex_entry_update(ie,
5288 worktree->root_fd, relpath,
5290 new_base_commit_id->sha1,
5291 !have_staged_files);
5293 err = got_fileindex_entry_alloc(&ie, pe->path);
5296 err = got_fileindex_entry_update(ie,
5297 worktree->root_fd, relpath, ct->blob_id->sha1,
5298 new_base_commit_id->sha1, 1);
5300 got_fileindex_entry_free(ie);
5303 err = got_fileindex_entry_add(fileindex, ie);
5305 got_fileindex_entry_free(ie);
5318 static const struct got_error *
5319 check_out_of_date(const char *in_repo_path, unsigned char status,
5320 unsigned char staged_status, struct got_object_id *base_blob_id,
5321 struct got_object_id *base_commit_id,
5322 struct got_object_id *head_commit_id, struct got_repository *repo,
5325 const struct got_error *err = NULL;
5326 struct got_object_id *id = NULL;
5328 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5329 /* Trivial case: base commit == head commit */
5330 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5333 * Ensure file content which local changes were based
5334 * on matches file content in the branch head.
5336 err = got_object_id_by_path(&id, repo, head_commit_id,
5339 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5340 err = got_error(ood_errcode);
5342 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5343 err = got_error(ood_errcode);
5345 /* Require that added files don't exist in the branch head. */
5346 err = got_object_id_by_path(&id, repo, head_commit_id,
5348 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5350 err = id ? got_error(ood_errcode) : NULL;
5357 const struct got_error *
5358 commit_worktree(struct got_object_id **new_commit_id,
5359 struct got_pathlist_head *commitable_paths,
5360 struct got_object_id *head_commit_id,
5361 struct got_object_id *parent_id2,
5362 struct got_worktree *worktree,
5363 const char *author, const char *committer,
5364 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5365 got_worktree_status_cb status_cb, void *status_arg,
5366 struct got_repository *repo)
5368 const struct got_error *err = NULL, *unlockerr = NULL;
5369 struct got_pathlist_entry *pe;
5370 const char *head_ref_name = NULL;
5371 struct got_commit_object *head_commit = NULL;
5372 struct got_reference *head_ref2 = NULL;
5373 struct got_object_id *head_commit_id2 = NULL;
5374 struct got_tree_object *head_tree = NULL;
5375 struct got_object_id *new_tree_id = NULL;
5376 int nentries, nparents = 0;
5377 struct got_object_id_queue parent_ids;
5378 struct got_object_qid *pid = NULL;
5379 char *logmsg = NULL;
5381 *new_commit_id = NULL;
5383 STAILQ_INIT(&parent_ids);
5385 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5389 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5393 if (commit_msg_cb != NULL) {
5394 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5399 if (logmsg == NULL || strlen(logmsg) == 0) {
5400 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5404 /* Create blobs from added and modified files and record their IDs. */
5405 TAILQ_FOREACH(pe, commitable_paths, entry) {
5406 struct got_commitable *ct = pe->data;
5409 /* Blobs for staged files already exist. */
5410 if (ct->staged_status == GOT_STATUS_ADD ||
5411 ct->staged_status == GOT_STATUS_MODIFY)
5414 if (ct->status != GOT_STATUS_ADD &&
5415 ct->status != GOT_STATUS_MODIFY &&
5416 ct->status != GOT_STATUS_MODE_CHANGE)
5419 if (asprintf(&ondisk_path, "%s/%s",
5420 worktree->root_path, pe->path) == -1) {
5421 err = got_error_from_errno("asprintf");
5424 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5430 /* Recursively write new tree objects. */
5431 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5432 commitable_paths, status_cb, status_arg, repo);
5436 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5439 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5442 err = got_object_qid_alloc(&pid, parent_id2);
5445 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5448 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5449 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5455 /* Check if a concurrent commit to our branch has occurred. */
5456 head_ref_name = got_worktree_get_head_ref_name(worktree);
5457 if (head_ref_name == NULL) {
5458 err = got_error_from_errno("got_worktree_get_head_ref_name");
5461 /* Lock the reference here to prevent concurrent modification. */
5462 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5465 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5468 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5469 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5472 /* Update branch head in repository. */
5473 err = got_ref_change_ref(head_ref2, *new_commit_id);
5476 err = got_ref_write(head_ref2, repo);
5480 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5484 err = ref_base_commit(worktree, repo);
5488 got_object_id_queue_free(&parent_ids);
5490 got_object_tree_close(head_tree);
5492 got_object_commit_close(head_commit);
5493 free(head_commit_id2);
5495 unlockerr = got_ref_unlock(head_ref2);
5496 if (unlockerr && err == NULL)
5498 got_ref_close(head_ref2);
5503 static const struct got_error *
5504 check_path_is_commitable(const char *path,
5505 struct got_pathlist_head *commitable_paths)
5507 struct got_pathlist_entry *cpe = NULL;
5508 size_t path_len = strlen(path);
5510 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5511 struct got_commitable *ct = cpe->data;
5512 const char *ct_path = ct->path;
5514 while (ct_path[0] == '/')
5517 if (strcmp(path, ct_path) == 0 ||
5518 got_path_is_child(ct_path, path, path_len))
5523 return got_error_path(path, GOT_ERR_BAD_PATH);
5528 static const struct got_error *
5529 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5531 int *have_staged_files = arg;
5533 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5534 *have_staged_files = 1;
5535 return got_error(GOT_ERR_CANCELLED);
5541 static const struct got_error *
5542 check_non_staged_files(struct got_fileindex *fileindex,
5543 struct got_pathlist_head *paths)
5545 struct got_pathlist_entry *pe;
5546 struct got_fileindex_entry *ie;
5548 TAILQ_FOREACH(pe, paths, entry) {
5549 if (pe->path[0] == '\0')
5551 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5553 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5554 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5555 return got_error_path(pe->path,
5556 GOT_ERR_FILE_NOT_STAGED);
5562 const struct got_error *
5563 got_worktree_commit(struct got_object_id **new_commit_id,
5564 struct got_worktree *worktree, struct got_pathlist_head *paths,
5565 const char *author, const char *committer, int allow_bad_symlinks,
5566 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5567 got_worktree_status_cb status_cb, void *status_arg,
5568 struct got_repository *repo)
5570 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5571 struct got_fileindex *fileindex = NULL;
5572 char *fileindex_path = NULL;
5573 struct got_pathlist_head commitable_paths;
5574 struct collect_commitables_arg cc_arg;
5575 struct got_pathlist_entry *pe;
5576 struct got_reference *head_ref = NULL;
5577 struct got_object_id *head_commit_id = NULL;
5578 int have_staged_files = 0;
5580 *new_commit_id = NULL;
5582 TAILQ_INIT(&commitable_paths);
5584 err = lock_worktree(worktree, LOCK_EX);
5588 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5592 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5596 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5600 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5601 &have_staged_files);
5602 if (err && err->code != GOT_ERR_CANCELLED)
5604 if (have_staged_files) {
5605 err = check_non_staged_files(fileindex, paths);
5610 cc_arg.commitable_paths = &commitable_paths;
5611 cc_arg.worktree = worktree;
5612 cc_arg.fileindex = fileindex;
5614 cc_arg.have_staged_files = have_staged_files;
5615 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5616 TAILQ_FOREACH(pe, paths, entry) {
5617 err = worktree_status(worktree, pe->path, fileindex, repo,
5618 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5623 if (TAILQ_EMPTY(&commitable_paths)) {
5624 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5628 TAILQ_FOREACH(pe, paths, entry) {
5629 err = check_path_is_commitable(pe->path, &commitable_paths);
5634 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5635 struct got_commitable *ct = pe->data;
5636 const char *ct_path = ct->in_repo_path;
5638 while (ct_path[0] == '/')
5640 err = check_out_of_date(ct_path, ct->status,
5641 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5642 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5648 err = commit_worktree(new_commit_id, &commitable_paths,
5649 head_commit_id, NULL, worktree, author, committer,
5650 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5654 err = update_fileindex_after_commit(worktree, &commitable_paths,
5655 *new_commit_id, fileindex, have_staged_files);
5656 sync_err = sync_fileindex(fileindex, fileindex_path);
5657 if (sync_err && err == NULL)
5661 got_fileindex_free(fileindex);
5662 free(fileindex_path);
5663 unlockerr = lock_worktree(worktree, LOCK_SH);
5664 if (unlockerr && err == NULL)
5666 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5667 struct got_commitable *ct = pe->data;
5668 free_commitable(ct);
5670 got_pathlist_free(&commitable_paths);
5675 got_commitable_get_path(struct got_commitable *ct)
5681 got_commitable_get_status(struct got_commitable *ct)
5686 struct check_rebase_ok_arg {
5687 struct got_worktree *worktree;
5688 struct got_repository *repo;
5691 static const struct got_error *
5692 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5694 const struct got_error *err = NULL;
5695 struct check_rebase_ok_arg *a = arg;
5696 unsigned char status;
5700 /* Reject rebase of a work tree with mixed base commits. */
5701 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5702 SHA1_DIGEST_LENGTH))
5703 return got_error(GOT_ERR_MIXED_COMMITS);
5705 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5707 return got_error_from_errno("asprintf");
5709 /* Reject rebase of a work tree with modified or staged files. */
5710 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5715 if (status != GOT_STATUS_NO_CHANGE)
5716 return got_error(GOT_ERR_MODIFIED);
5717 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5718 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5723 const struct got_error *
5724 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5725 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5726 struct got_worktree *worktree, struct got_reference *branch,
5727 struct got_repository *repo)
5729 const struct got_error *err = NULL;
5730 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5731 char *branch_ref_name = NULL;
5732 char *fileindex_path = NULL;
5733 struct check_rebase_ok_arg ok_arg;
5734 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5735 struct got_object_id *wt_branch_tip = NULL;
5737 *new_base_branch_ref = NULL;
5741 err = lock_worktree(worktree, LOCK_EX);
5745 err = open_fileindex(fileindex, &fileindex_path, worktree);
5749 ok_arg.worktree = worktree;
5751 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5756 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5760 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5764 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5768 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5773 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5776 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5777 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5781 err = got_ref_alloc_symref(new_base_branch_ref,
5782 new_base_branch_ref_name, wt_branch);
5785 err = got_ref_write(*new_base_branch_ref, repo);
5789 /* TODO Lock original branch's ref while rebasing? */
5791 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5795 err = got_ref_write(branch_ref, repo);
5799 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5800 worktree->base_commit_id);
5803 err = got_ref_write(*tmp_branch, repo);
5807 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5811 free(fileindex_path);
5812 free(tmp_branch_name);
5813 free(new_base_branch_ref_name);
5814 free(branch_ref_name);
5816 got_ref_close(branch_ref);
5818 got_ref_close(wt_branch);
5819 free(wt_branch_tip);
5821 if (*new_base_branch_ref) {
5822 got_ref_close(*new_base_branch_ref);
5823 *new_base_branch_ref = NULL;
5826 got_ref_close(*tmp_branch);
5830 got_fileindex_free(*fileindex);
5833 lock_worktree(worktree, LOCK_SH);
5838 const struct got_error *
5839 got_worktree_rebase_continue(struct got_object_id **commit_id,
5840 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5841 struct got_reference **branch, struct got_fileindex **fileindex,
5842 struct got_worktree *worktree, struct got_repository *repo)
5844 const struct got_error *err;
5845 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5846 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5847 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5848 char *fileindex_path = NULL;
5849 int have_staged_files = 0;
5852 *new_base_branch = NULL;
5857 err = lock_worktree(worktree, LOCK_EX);
5861 err = open_fileindex(fileindex, &fileindex_path, worktree);
5865 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5866 &have_staged_files);
5867 if (err && err->code != GOT_ERR_CANCELLED)
5869 if (have_staged_files) {
5870 err = got_error(GOT_ERR_STAGED_PATHS);
5874 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5878 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5882 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5886 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5890 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5894 err = got_ref_open(branch, repo,
5895 got_ref_get_symref_target(branch_ref), 0);
5899 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5903 err = got_ref_resolve(commit_id, repo, commit_ref);
5907 err = got_ref_open(new_base_branch, repo,
5908 new_base_branch_ref_name, 0);
5912 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5916 free(commit_ref_name);
5917 free(branch_ref_name);
5918 free(fileindex_path);
5920 got_ref_close(commit_ref);
5922 got_ref_close(branch_ref);
5927 got_ref_close(*tmp_branch);
5930 if (*new_base_branch) {
5931 got_ref_close(*new_base_branch);
5932 *new_base_branch = NULL;
5935 got_ref_close(*branch);
5939 got_fileindex_free(*fileindex);
5942 lock_worktree(worktree, LOCK_SH);
5947 const struct got_error *
5948 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5950 const struct got_error *err;
5951 char *tmp_branch_name = NULL;
5953 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5957 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5958 free(tmp_branch_name);
5962 static const struct got_error *
5963 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5964 char **logmsg, void *arg)
5970 static const struct got_error *
5971 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5972 const char *path, struct got_object_id *blob_id,
5973 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5974 int dirfd, const char *de_name)
5979 struct collect_merged_paths_arg {
5980 got_worktree_checkout_cb progress_cb;
5982 struct got_pathlist_head *merged_paths;
5985 static const struct got_error *
5986 collect_merged_paths(void *arg, unsigned char status, const char *path)
5988 const struct got_error *err;
5989 struct collect_merged_paths_arg *a = arg;
5991 struct got_pathlist_entry *new;
5993 err = (*a->progress_cb)(a->progress_arg, status, path);
5997 if (status != GOT_STATUS_MERGE &&
5998 status != GOT_STATUS_ADD &&
5999 status != GOT_STATUS_DELETE &&
6000 status != GOT_STATUS_CONFLICT)
6005 return got_error_from_errno("strdup");
6007 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6008 if (err || new == NULL)
6014 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6016 struct got_pathlist_entry *pe;
6018 TAILQ_FOREACH(pe, merged_paths, entry)
6019 free((char *)pe->path);
6021 got_pathlist_free(merged_paths);
6024 static const struct got_error *
6025 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6026 int is_rebase, struct got_repository *repo)
6028 const struct got_error *err;
6029 struct got_reference *commit_ref = NULL;
6031 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6033 if (err->code != GOT_ERR_NOT_REF)
6035 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6038 err = got_ref_write(commit_ref, repo);
6041 } else if (is_rebase) {
6042 struct got_object_id *stored_id;
6045 err = got_ref_resolve(&stored_id, repo, commit_ref);
6048 cmp = got_object_id_cmp(commit_id, stored_id);
6051 err = got_error(GOT_ERR_REBASE_COMMITID);
6057 got_ref_close(commit_ref);
6061 static const struct got_error *
6062 rebase_merge_files(struct got_pathlist_head *merged_paths,
6063 const char *commit_ref_name, struct got_worktree *worktree,
6064 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6065 struct got_object_id *commit_id, struct got_repository *repo,
6066 got_worktree_checkout_cb progress_cb, void *progress_arg,
6067 got_cancel_cb cancel_cb, void *cancel_arg)
6069 const struct got_error *err;
6070 struct got_reference *commit_ref = NULL;
6071 struct collect_merged_paths_arg cmp_arg;
6072 char *fileindex_path;
6074 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6076 err = get_fileindex_path(&fileindex_path, worktree);
6080 cmp_arg.progress_cb = progress_cb;
6081 cmp_arg.progress_arg = progress_arg;
6082 cmp_arg.merged_paths = merged_paths;
6083 err = merge_files(worktree, fileindex, fileindex_path,
6084 parent_commit_id, commit_id, repo, collect_merged_paths,
6085 &cmp_arg, cancel_cb, cancel_arg);
6087 got_ref_close(commit_ref);
6091 const struct got_error *
6092 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6093 struct got_worktree *worktree, struct got_fileindex *fileindex,
6094 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6095 struct got_repository *repo,
6096 got_worktree_checkout_cb progress_cb, void *progress_arg,
6097 got_cancel_cb cancel_cb, void *cancel_arg)
6099 const struct got_error *err;
6100 char *commit_ref_name;
6102 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6106 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6110 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6111 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6112 progress_arg, cancel_cb, cancel_arg);
6114 free(commit_ref_name);
6118 const struct got_error *
6119 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6120 struct got_worktree *worktree, struct got_fileindex *fileindex,
6121 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6122 struct got_repository *repo,
6123 got_worktree_checkout_cb progress_cb, void *progress_arg,
6124 got_cancel_cb cancel_cb, void *cancel_arg)
6126 const struct got_error *err;
6127 char *commit_ref_name;
6129 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6133 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6137 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6138 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6139 progress_arg, cancel_cb, cancel_arg);
6141 free(commit_ref_name);
6145 static const struct got_error *
6146 rebase_commit(struct got_object_id **new_commit_id,
6147 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6148 struct got_worktree *worktree, struct got_fileindex *fileindex,
6149 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6150 const char *new_logmsg, struct got_repository *repo)
6152 const struct got_error *err, *sync_err;
6153 struct got_pathlist_head commitable_paths;
6154 struct collect_commitables_arg cc_arg;
6155 char *fileindex_path = NULL;
6156 struct got_reference *head_ref = NULL;
6157 struct got_object_id *head_commit_id = NULL;
6158 char *logmsg = NULL;
6160 TAILQ_INIT(&commitable_paths);
6161 *new_commit_id = NULL;
6163 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6165 err = get_fileindex_path(&fileindex_path, worktree);
6169 cc_arg.commitable_paths = &commitable_paths;
6170 cc_arg.worktree = worktree;
6172 cc_arg.have_staged_files = 0;
6174 * If possible get the status of individual files directly to
6175 * avoid crawling the entire work tree once per rebased commit.
6177 * Ideally, merged_paths would contain a list of commitables
6178 * we could use so we could skip worktree_status() entirely.
6179 * However, we would then need carefully keep track of cumulative
6180 * effects of operations such as file additions and deletions
6181 * in 'got histedit -f' (folding multiple commits into one),
6182 * and this extra complexity is not really worth it.
6185 struct got_pathlist_entry *pe;
6186 TAILQ_FOREACH(pe, merged_paths, entry) {
6187 err = worktree_status(worktree, pe->path, fileindex,
6188 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6194 err = worktree_status(worktree, "", fileindex, repo,
6195 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6200 if (TAILQ_EMPTY(&commitable_paths)) {
6201 /* No-op change; commit will be elided. */
6202 err = got_ref_delete(commit_ref, repo);
6205 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6209 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6213 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6218 logmsg = strdup(new_logmsg);
6219 if (logmsg == NULL) {
6220 err = got_error_from_errno("strdup");
6224 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6229 /* NB: commit_worktree will call free(logmsg) */
6230 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6231 NULL, worktree, got_object_commit_get_author(orig_commit),
6232 got_object_commit_get_committer(orig_commit),
6233 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6237 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6241 err = got_ref_delete(commit_ref, repo);
6245 err = update_fileindex_after_commit(worktree, &commitable_paths,
6246 *new_commit_id, fileindex, 0);
6247 sync_err = sync_fileindex(fileindex, fileindex_path);
6248 if (sync_err && err == NULL)
6251 free(fileindex_path);
6252 free(head_commit_id);
6254 got_ref_close(head_ref);
6256 free(*new_commit_id);
6257 *new_commit_id = NULL;
6262 const struct got_error *
6263 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6264 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6265 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6266 struct got_commit_object *orig_commit,
6267 struct got_object_id *orig_commit_id, struct got_repository *repo)
6269 const struct got_error *err;
6270 char *commit_ref_name;
6271 struct got_reference *commit_ref = NULL;
6272 struct got_object_id *commit_id = NULL;
6274 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6278 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6281 err = got_ref_resolve(&commit_id, repo, commit_ref);
6284 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6285 err = got_error(GOT_ERR_REBASE_COMMITID);
6289 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6290 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6293 got_ref_close(commit_ref);
6294 free(commit_ref_name);
6299 const struct got_error *
6300 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6301 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6302 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6303 struct got_commit_object *orig_commit,
6304 struct got_object_id *orig_commit_id, const char *new_logmsg,
6305 struct got_repository *repo)
6307 const struct got_error *err;
6308 char *commit_ref_name;
6309 struct got_reference *commit_ref = NULL;
6311 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6315 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6319 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6320 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6323 got_ref_close(commit_ref);
6324 free(commit_ref_name);
6328 const struct got_error *
6329 got_worktree_rebase_postpone(struct got_worktree *worktree,
6330 struct got_fileindex *fileindex)
6333 got_fileindex_free(fileindex);
6334 return lock_worktree(worktree, LOCK_SH);
6337 static const struct got_error *
6338 delete_ref(const char *name, struct got_repository *repo)
6340 const struct got_error *err;
6341 struct got_reference *ref;
6343 err = got_ref_open(&ref, repo, name, 0);
6345 if (err->code == GOT_ERR_NOT_REF)
6350 err = got_ref_delete(ref, repo);
6355 static const struct got_error *
6356 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6358 const struct got_error *err;
6359 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6360 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6362 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6365 err = delete_ref(tmp_branch_name, repo);
6369 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6372 err = delete_ref(new_base_branch_ref_name, repo);
6376 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6379 err = delete_ref(branch_ref_name, repo);
6383 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6386 err = delete_ref(commit_ref_name, repo);
6391 free(tmp_branch_name);
6392 free(new_base_branch_ref_name);
6393 free(branch_ref_name);
6394 free(commit_ref_name);
6398 const struct got_error *
6399 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6400 struct got_object_id *new_commit_id, struct got_repository *repo)
6402 const struct got_error *err;
6403 struct got_reference *ref = NULL;
6404 struct got_object_id *old_commit_id = NULL;
6405 const char *branch_name = NULL;
6406 char *new_id_str = NULL;
6407 char *refname = NULL;
6409 branch_name = got_ref_get_name(branch);
6410 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6411 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6414 err = got_object_id_str(&new_id_str, new_commit_id);
6418 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6419 new_id_str) == -1) {
6420 err = got_error_from_errno("asprintf");
6424 err = got_ref_resolve(&old_commit_id, repo, branch);
6428 err = got_ref_alloc(&ref, refname, old_commit_id);
6432 err = got_ref_write(ref, repo);
6436 free(old_commit_id);
6442 const struct got_error *
6443 got_worktree_rebase_complete(struct got_worktree *worktree,
6444 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6445 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6446 struct got_repository *repo, int create_backup)
6448 const struct got_error *err, *unlockerr, *sync_err;
6449 struct got_object_id *new_head_commit_id = NULL;
6450 char *fileindex_path = NULL;
6452 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6456 if (create_backup) {
6457 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6458 rebased_branch, new_head_commit_id, repo);
6463 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6467 err = got_ref_write(rebased_branch, repo);
6471 err = got_worktree_set_head_ref(worktree, rebased_branch);
6475 err = delete_rebase_refs(worktree, repo);
6479 err = get_fileindex_path(&fileindex_path, worktree);
6482 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6483 sync_err = sync_fileindex(fileindex, fileindex_path);
6484 if (sync_err && err == NULL)
6487 got_fileindex_free(fileindex);
6488 free(fileindex_path);
6489 free(new_head_commit_id);
6490 unlockerr = lock_worktree(worktree, LOCK_SH);
6491 if (unlockerr && err == NULL)
6496 const struct got_error *
6497 got_worktree_rebase_abort(struct got_worktree *worktree,
6498 struct got_fileindex *fileindex, struct got_repository *repo,
6499 struct got_reference *new_base_branch,
6500 got_worktree_checkout_cb progress_cb, void *progress_arg)
6502 const struct got_error *err, *unlockerr, *sync_err;
6503 struct got_reference *resolved = NULL;
6504 struct got_object_id *commit_id = NULL;
6505 char *fileindex_path = NULL;
6506 struct revert_file_args rfa;
6507 struct got_object_id *tree_id = NULL;
6509 err = lock_worktree(worktree, LOCK_EX);
6513 err = got_ref_open(&resolved, repo,
6514 got_ref_get_symref_target(new_base_branch), 0);
6518 err = got_worktree_set_head_ref(worktree, resolved);
6523 * XXX commits to the base branch could have happened while
6524 * we were busy rebasing; should we store the original commit ID
6525 * when rebase begins and read it back here?
6527 err = got_ref_resolve(&commit_id, repo, resolved);
6531 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6535 err = got_object_id_by_path(&tree_id, repo,
6536 worktree->base_commit_id, worktree->path_prefix);
6540 err = delete_rebase_refs(worktree, repo);
6544 err = get_fileindex_path(&fileindex_path, worktree);
6548 rfa.worktree = worktree;
6549 rfa.fileindex = fileindex;
6550 rfa.progress_cb = progress_cb;
6551 rfa.progress_arg = progress_arg;
6552 rfa.patch_cb = NULL;
6553 rfa.patch_arg = NULL;
6555 rfa.unlink_added_files = 0;
6556 err = worktree_status(worktree, "", fileindex, repo,
6557 revert_file, &rfa, NULL, NULL, 1, 0);
6561 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6562 repo, progress_cb, progress_arg, NULL, NULL);
6564 sync_err = sync_fileindex(fileindex, fileindex_path);
6565 if (sync_err && err == NULL)
6568 got_ref_close(resolved);
6572 got_fileindex_free(fileindex);
6573 free(fileindex_path);
6575 unlockerr = lock_worktree(worktree, LOCK_SH);
6576 if (unlockerr && err == NULL)
6581 const struct got_error *
6582 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6583 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6584 struct got_fileindex **fileindex, struct got_worktree *worktree,
6585 struct got_repository *repo)
6587 const struct got_error *err = NULL;
6588 char *tmp_branch_name = NULL;
6589 char *branch_ref_name = NULL;
6590 char *base_commit_ref_name = NULL;
6591 char *fileindex_path = NULL;
6592 struct check_rebase_ok_arg ok_arg;
6593 struct got_reference *wt_branch = NULL;
6594 struct got_reference *base_commit_ref = NULL;
6598 *base_commit_id = NULL;
6601 err = lock_worktree(worktree, LOCK_EX);
6605 err = open_fileindex(fileindex, &fileindex_path, worktree);
6609 ok_arg.worktree = worktree;
6611 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6616 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6620 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6624 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6629 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6634 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6638 err = got_ref_write(*branch_ref, repo);
6642 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6643 worktree->base_commit_id);
6646 err = got_ref_write(base_commit_ref, repo);
6649 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6650 if (*base_commit_id == NULL) {
6651 err = got_error_from_errno("got_object_id_dup");
6655 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6656 worktree->base_commit_id);
6659 err = got_ref_write(*tmp_branch, repo);
6663 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6667 free(fileindex_path);
6668 free(tmp_branch_name);
6669 free(branch_ref_name);
6670 free(base_commit_ref_name);
6672 got_ref_close(wt_branch);
6675 got_ref_close(*branch_ref);
6679 got_ref_close(*tmp_branch);
6682 free(*base_commit_id);
6684 got_fileindex_free(*fileindex);
6687 lock_worktree(worktree, LOCK_SH);
6692 const struct got_error *
6693 got_worktree_histedit_postpone(struct got_worktree *worktree,
6694 struct got_fileindex *fileindex)
6697 got_fileindex_free(fileindex);
6698 return lock_worktree(worktree, LOCK_SH);
6701 const struct got_error *
6702 got_worktree_histedit_in_progress(int *in_progress,
6703 struct got_worktree *worktree)
6705 const struct got_error *err;
6706 char *tmp_branch_name = NULL;
6708 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6712 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6713 free(tmp_branch_name);
6717 const struct got_error *
6718 got_worktree_histedit_continue(struct got_object_id **commit_id,
6719 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6720 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6721 struct got_worktree *worktree, struct got_repository *repo)
6723 const struct got_error *err;
6724 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6725 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6726 struct got_reference *commit_ref = NULL;
6727 struct got_reference *base_commit_ref = NULL;
6728 char *fileindex_path = NULL;
6729 int have_staged_files = 0;
6733 *base_commit_id = NULL;
6736 err = lock_worktree(worktree, LOCK_EX);
6740 err = open_fileindex(fileindex, &fileindex_path, worktree);
6744 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6745 &have_staged_files);
6746 if (err && err->code != GOT_ERR_CANCELLED)
6748 if (have_staged_files) {
6749 err = got_error(GOT_ERR_STAGED_PATHS);
6753 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6757 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6761 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6765 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6770 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6774 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6777 err = got_ref_resolve(commit_id, repo, commit_ref);
6781 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6784 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6788 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6792 free(commit_ref_name);
6793 free(branch_ref_name);
6794 free(fileindex_path);
6796 got_ref_close(commit_ref);
6797 if (base_commit_ref)
6798 got_ref_close(base_commit_ref);
6802 free(*base_commit_id);
6803 *base_commit_id = NULL;
6805 got_ref_close(*tmp_branch);
6809 got_fileindex_free(*fileindex);
6812 lock_worktree(worktree, LOCK_EX);
6817 static const struct got_error *
6818 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6820 const struct got_error *err;
6821 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6822 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6824 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6827 err = delete_ref(tmp_branch_name, repo);
6831 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6835 err = delete_ref(base_commit_ref_name, repo);
6839 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6842 err = delete_ref(branch_ref_name, repo);
6846 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6849 err = delete_ref(commit_ref_name, repo);
6853 free(tmp_branch_name);
6854 free(base_commit_ref_name);
6855 free(branch_ref_name);
6856 free(commit_ref_name);
6860 const struct got_error *
6861 got_worktree_histedit_abort(struct got_worktree *worktree,
6862 struct got_fileindex *fileindex, struct got_repository *repo,
6863 struct got_reference *branch, struct got_object_id *base_commit_id,
6864 got_worktree_checkout_cb progress_cb, void *progress_arg)
6866 const struct got_error *err, *unlockerr, *sync_err;
6867 struct got_reference *resolved = NULL;
6868 char *fileindex_path = NULL;
6869 struct got_object_id *tree_id = NULL;
6870 struct revert_file_args rfa;
6872 err = lock_worktree(worktree, LOCK_EX);
6876 err = got_ref_open(&resolved, repo,
6877 got_ref_get_symref_target(branch), 0);
6881 err = got_worktree_set_head_ref(worktree, resolved);
6885 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6889 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6890 worktree->path_prefix);
6894 err = delete_histedit_refs(worktree, repo);
6898 err = get_fileindex_path(&fileindex_path, worktree);
6902 rfa.worktree = worktree;
6903 rfa.fileindex = fileindex;
6904 rfa.progress_cb = progress_cb;
6905 rfa.progress_arg = progress_arg;
6906 rfa.patch_cb = NULL;
6907 rfa.patch_arg = NULL;
6909 rfa.unlink_added_files = 0;
6910 err = worktree_status(worktree, "", fileindex, repo,
6911 revert_file, &rfa, NULL, NULL, 1, 0);
6915 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6916 repo, progress_cb, progress_arg, NULL, NULL);
6918 sync_err = sync_fileindex(fileindex, fileindex_path);
6919 if (sync_err && err == NULL)
6922 got_ref_close(resolved);
6924 free(fileindex_path);
6926 unlockerr = lock_worktree(worktree, LOCK_SH);
6927 if (unlockerr && err == NULL)
6932 const struct got_error *
6933 got_worktree_histedit_complete(struct got_worktree *worktree,
6934 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6935 struct got_reference *edited_branch, struct got_repository *repo)
6937 const struct got_error *err, *unlockerr, *sync_err;
6938 struct got_object_id *new_head_commit_id = NULL;
6939 struct got_reference *resolved = NULL;
6940 char *fileindex_path = NULL;
6942 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6946 err = got_ref_open(&resolved, repo,
6947 got_ref_get_symref_target(edited_branch), 0);
6951 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
6952 resolved, new_head_commit_id, repo);
6956 err = got_ref_change_ref(resolved, new_head_commit_id);
6960 err = got_ref_write(resolved, repo);
6964 err = got_worktree_set_head_ref(worktree, resolved);
6968 err = delete_histedit_refs(worktree, repo);
6972 err = get_fileindex_path(&fileindex_path, worktree);
6975 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6976 sync_err = sync_fileindex(fileindex, fileindex_path);
6977 if (sync_err && err == NULL)
6980 got_fileindex_free(fileindex);
6981 free(fileindex_path);
6982 free(new_head_commit_id);
6983 unlockerr = lock_worktree(worktree, LOCK_SH);
6984 if (unlockerr && err == NULL)
6989 const struct got_error *
6990 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6991 struct got_object_id *commit_id, struct got_repository *repo)
6993 const struct got_error *err;
6994 char *commit_ref_name;
6996 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7000 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7004 err = delete_ref(commit_ref_name, repo);
7006 free(commit_ref_name);
7010 const struct got_error *
7011 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7012 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7013 struct got_worktree *worktree, const char *refname,
7014 struct got_repository *repo)
7016 const struct got_error *err = NULL;
7017 char *fileindex_path = NULL;
7018 struct check_rebase_ok_arg ok_arg;
7022 *base_branch_ref = NULL;
7024 err = lock_worktree(worktree, LOCK_EX);
7028 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7029 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7030 "cannot integrate a branch into itself; "
7031 "update -b or different branch name required");
7035 err = open_fileindex(fileindex, &fileindex_path, worktree);
7039 /* Preconditions are the same as for rebase. */
7040 ok_arg.worktree = worktree;
7042 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7047 err = got_ref_open(branch_ref, repo, refname, 1);
7051 err = got_ref_open(base_branch_ref, repo,
7052 got_worktree_get_head_ref_name(worktree), 1);
7056 got_ref_close(*branch_ref);
7059 if (*base_branch_ref) {
7060 got_ref_close(*base_branch_ref);
7061 *base_branch_ref = NULL;
7064 got_fileindex_free(*fileindex);
7067 lock_worktree(worktree, LOCK_SH);
7072 const struct got_error *
7073 got_worktree_integrate_continue(struct got_worktree *worktree,
7074 struct got_fileindex *fileindex, struct got_repository *repo,
7075 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7076 got_worktree_checkout_cb progress_cb, void *progress_arg,
7077 got_cancel_cb cancel_cb, void *cancel_arg)
7079 const struct got_error *err = NULL, *sync_err, *unlockerr;
7080 char *fileindex_path = NULL;
7081 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7083 err = get_fileindex_path(&fileindex_path, worktree);
7087 err = got_ref_resolve(&commit_id, repo, branch_ref);
7091 err = got_object_id_by_path(&tree_id, repo, commit_id,
7092 worktree->path_prefix);
7096 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7100 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7101 progress_cb, progress_arg, cancel_cb, cancel_arg);
7105 err = got_ref_change_ref(base_branch_ref, commit_id);
7109 err = got_ref_write(base_branch_ref, repo);
7113 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7115 sync_err = sync_fileindex(fileindex, fileindex_path);
7116 if (sync_err && err == NULL)
7120 unlockerr = got_ref_unlock(branch_ref);
7121 if (unlockerr && err == NULL)
7123 got_ref_close(branch_ref);
7125 unlockerr = got_ref_unlock(base_branch_ref);
7126 if (unlockerr && err == NULL)
7128 got_ref_close(base_branch_ref);
7130 got_fileindex_free(fileindex);
7131 free(fileindex_path);
7134 unlockerr = lock_worktree(worktree, LOCK_SH);
7135 if (unlockerr && err == NULL)
7140 const struct got_error *
7141 got_worktree_integrate_abort(struct got_worktree *worktree,
7142 struct got_fileindex *fileindex, struct got_repository *repo,
7143 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7145 const struct got_error *err = NULL, *unlockerr = NULL;
7147 got_fileindex_free(fileindex);
7149 err = lock_worktree(worktree, LOCK_SH);
7151 unlockerr = got_ref_unlock(branch_ref);
7152 if (unlockerr && err == NULL)
7154 got_ref_close(branch_ref);
7156 unlockerr = got_ref_unlock(base_branch_ref);
7157 if (unlockerr && err == NULL)
7159 got_ref_close(base_branch_ref);
7164 const struct got_error *
7165 got_worktree_merge_postpone(struct got_worktree *worktree,
7166 struct got_fileindex *fileindex)
7168 const struct got_error *err, *sync_err;
7169 char *fileindex_path = NULL;
7171 err = get_fileindex_path(&fileindex_path, worktree);
7175 sync_err = sync_fileindex(fileindex, fileindex_path);
7177 err = lock_worktree(worktree, LOCK_SH);
7178 if (sync_err && err == NULL)
7181 got_fileindex_free(fileindex);
7182 free(fileindex_path);
7186 static const struct got_error *
7187 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7189 const struct got_error *err;
7190 char *branch_refname = NULL, *commit_refname = NULL;
7192 err = get_merge_branch_ref_name(&branch_refname, worktree);
7195 err = delete_ref(branch_refname, repo);
7199 err = get_merge_commit_ref_name(&commit_refname, worktree);
7202 err = delete_ref(commit_refname, repo);
7207 free(branch_refname);
7208 free(commit_refname);
7212 struct merge_commit_msg_arg {
7213 struct got_worktree *worktree;
7214 const char *branch_name;
7217 static const struct got_error *
7218 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7221 struct merge_commit_msg_arg *a = arg;
7223 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7224 got_worktree_get_head_ref_name(a->worktree)) == -1)
7225 return got_error_from_errno("asprintf");
7231 const struct got_error *
7232 got_worktree_merge_branch(struct got_worktree *worktree,
7233 struct got_fileindex *fileindex,
7234 struct got_object_id *yca_commit_id,
7235 struct got_object_id *branch_tip,
7236 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7237 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7239 const struct got_error *err;
7240 char *fileindex_path = NULL;
7242 err = get_fileindex_path(&fileindex_path, worktree);
7246 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7251 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7252 branch_tip, repo, progress_cb, progress_arg,
7253 cancel_cb, cancel_arg);
7255 free(fileindex_path);
7259 const struct got_error *
7260 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7261 struct got_worktree *worktree, struct got_fileindex *fileindex,
7262 const char *author, const char *committer, int allow_bad_symlinks,
7263 struct got_object_id *branch_tip, const char *branch_name,
7264 struct got_repository *repo,
7265 got_worktree_status_cb status_cb, void *status_arg)
7268 const struct got_error *err = NULL, *sync_err;
7269 struct got_pathlist_head commitable_paths;
7270 struct collect_commitables_arg cc_arg;
7271 struct got_pathlist_entry *pe;
7272 struct got_reference *head_ref = NULL;
7273 struct got_object_id *head_commit_id = NULL;
7274 int have_staged_files = 0;
7275 struct merge_commit_msg_arg mcm_arg;
7276 char *fileindex_path = NULL;
7278 *new_commit_id = NULL;
7280 TAILQ_INIT(&commitable_paths);
7282 err = get_fileindex_path(&fileindex_path, worktree);
7286 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7290 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7294 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7295 &have_staged_files);
7296 if (err && err->code != GOT_ERR_CANCELLED)
7298 if (have_staged_files) {
7299 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7303 cc_arg.commitable_paths = &commitable_paths;
7304 cc_arg.worktree = worktree;
7305 cc_arg.fileindex = fileindex;
7307 cc_arg.have_staged_files = have_staged_files;
7308 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7309 err = worktree_status(worktree, "", fileindex, repo,
7310 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7314 if (TAILQ_EMPTY(&commitable_paths)) {
7315 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7316 "merge of %s cannot proceed", branch_name);
7320 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7321 struct got_commitable *ct = pe->data;
7322 const char *ct_path = ct->in_repo_path;
7324 while (ct_path[0] == '/')
7326 err = check_out_of_date(ct_path, ct->status,
7327 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7328 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7334 mcm_arg.worktree = worktree;
7335 mcm_arg.branch_name = branch_name;
7336 err = commit_worktree(new_commit_id, &commitable_paths,
7337 head_commit_id, branch_tip, worktree, author, committer,
7338 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7342 err = update_fileindex_after_commit(worktree, &commitable_paths,
7343 *new_commit_id, fileindex, have_staged_files);
7344 sync_err = sync_fileindex(fileindex, fileindex_path);
7345 if (sync_err && err == NULL)
7348 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7349 struct got_commitable *ct = pe->data;
7350 free_commitable(ct);
7352 got_pathlist_free(&commitable_paths);
7353 free(fileindex_path);
7357 const struct got_error *
7358 got_worktree_merge_complete(struct got_worktree *worktree,
7359 struct got_fileindex *fileindex, struct got_repository *repo)
7361 const struct got_error *err, *unlockerr, *sync_err;
7362 char *fileindex_path = NULL;
7364 err = delete_merge_refs(worktree, repo);
7368 err = get_fileindex_path(&fileindex_path, worktree);
7371 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7372 sync_err = sync_fileindex(fileindex, fileindex_path);
7373 if (sync_err && err == NULL)
7376 got_fileindex_free(fileindex);
7377 free(fileindex_path);
7378 unlockerr = lock_worktree(worktree, LOCK_SH);
7379 if (unlockerr && err == NULL)
7384 const struct got_error *
7385 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7386 struct got_repository *repo)
7388 const struct got_error *err;
7389 char *branch_refname = NULL;
7390 struct got_reference *branch_ref = NULL;
7394 err = get_merge_branch_ref_name(&branch_refname, worktree);
7397 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7398 free(branch_refname);
7400 if (err->code != GOT_ERR_NOT_REF)
7408 const struct got_error *got_worktree_merge_prepare(
7409 struct got_fileindex **fileindex, struct got_worktree *worktree,
7410 struct got_reference *branch, struct got_repository *repo)
7412 const struct got_error *err = NULL;
7413 char *fileindex_path = NULL;
7414 char *branch_refname = NULL, *commit_refname = NULL;
7415 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7416 struct got_reference *commit_ref = NULL;
7417 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7418 struct check_rebase_ok_arg ok_arg;
7422 err = lock_worktree(worktree, LOCK_EX);
7426 err = open_fileindex(fileindex, &fileindex_path, worktree);
7430 /* Preconditions are the same as for rebase. */
7431 ok_arg.worktree = worktree;
7433 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7438 err = get_merge_branch_ref_name(&branch_refname, worktree);
7442 err = get_merge_commit_ref_name(&commit_refname, worktree);
7446 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7451 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7455 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7456 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7460 err = got_ref_resolve(&branch_tip, repo, branch);
7464 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7467 err = got_ref_write(branch_ref, repo);
7471 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7474 err = got_ref_write(commit_ref, repo);
7479 free(branch_refname);
7480 free(commit_refname);
7481 free(fileindex_path);
7483 got_ref_close(branch_ref);
7485 got_ref_close(commit_ref);
7487 got_ref_close(wt_branch);
7488 free(wt_branch_tip);
7491 got_fileindex_free(*fileindex);
7494 lock_worktree(worktree, LOCK_SH);
7499 const struct got_error *
7500 got_worktree_merge_continue(char **branch_name,
7501 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7502 struct got_worktree *worktree, struct got_repository *repo)
7504 const struct got_error *err;
7505 char *commit_refname = NULL, *branch_refname = NULL;
7506 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7507 char *fileindex_path = NULL;
7508 int have_staged_files = 0;
7510 *branch_name = NULL;
7514 err = lock_worktree(worktree, LOCK_EX);
7518 err = open_fileindex(fileindex, &fileindex_path, worktree);
7522 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7523 &have_staged_files);
7524 if (err && err->code != GOT_ERR_CANCELLED)
7526 if (have_staged_files) {
7527 err = got_error(GOT_ERR_STAGED_PATHS);
7531 err = get_merge_branch_ref_name(&branch_refname, worktree);
7535 err = get_merge_commit_ref_name(&commit_refname, worktree);
7539 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7543 if (!got_ref_is_symbolic(branch_ref)) {
7544 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7545 "%s is not a symbolic reference",
7546 got_ref_get_name(branch_ref));
7549 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7550 if (*branch_name == NULL) {
7551 err = got_error_from_errno("strdup");
7555 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7559 err = got_ref_resolve(branch_tip, repo, commit_ref);
7563 free(commit_refname);
7564 free(branch_refname);
7565 free(fileindex_path);
7567 got_ref_close(commit_ref);
7569 got_ref_close(branch_ref);
7573 *branch_name = NULL;
7578 got_fileindex_free(*fileindex);
7581 lock_worktree(worktree, LOCK_SH);
7586 const struct got_error *
7587 got_worktree_merge_abort(struct got_worktree *worktree,
7588 struct got_fileindex *fileindex, struct got_repository *repo,
7589 got_worktree_checkout_cb progress_cb, void *progress_arg)
7591 const struct got_error *err, *unlockerr, *sync_err;
7592 struct got_object_id *commit_id = NULL;
7593 char *fileindex_path = NULL;
7594 struct revert_file_args rfa;
7595 struct got_object_id *tree_id = NULL;
7597 err = got_object_id_by_path(&tree_id, repo,
7598 worktree->base_commit_id, worktree->path_prefix);
7602 err = delete_merge_refs(worktree, repo);
7606 err = get_fileindex_path(&fileindex_path, worktree);
7610 rfa.worktree = worktree;
7611 rfa.fileindex = fileindex;
7612 rfa.progress_cb = progress_cb;
7613 rfa.progress_arg = progress_arg;
7614 rfa.patch_cb = NULL;
7615 rfa.patch_arg = NULL;
7617 rfa.unlink_added_files = 1;
7618 err = worktree_status(worktree, "", fileindex, repo,
7619 revert_file, &rfa, NULL, NULL, 1, 0);
7623 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7624 repo, progress_cb, progress_arg, NULL, NULL);
7626 sync_err = sync_fileindex(fileindex, fileindex_path);
7627 if (sync_err && err == NULL)
7633 got_fileindex_free(fileindex);
7634 free(fileindex_path);
7636 unlockerr = lock_worktree(worktree, LOCK_SH);
7637 if (unlockerr && err == NULL)
7642 struct check_stage_ok_arg {
7643 struct got_object_id *head_commit_id;
7644 struct got_worktree *worktree;
7645 struct got_fileindex *fileindex;
7646 struct got_repository *repo;
7650 const struct got_error *
7651 check_stage_ok(void *arg, unsigned char status,
7652 unsigned char staged_status, const char *relpath,
7653 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7654 struct got_object_id *commit_id, int dirfd, const char *de_name)
7656 struct check_stage_ok_arg *a = arg;
7657 const struct got_error *err = NULL;
7658 struct got_fileindex_entry *ie;
7659 struct got_object_id base_commit_id;
7660 struct got_object_id *base_commit_idp = NULL;
7661 char *in_repo_path = NULL, *p;
7663 if (status == GOT_STATUS_UNVERSIONED ||
7664 status == GOT_STATUS_NO_CHANGE)
7666 if (status == GOT_STATUS_NONEXISTENT)
7667 return got_error_set_errno(ENOENT, relpath);
7669 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7671 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7673 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7674 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7676 return got_error_from_errno("asprintf");
7678 if (got_fileindex_entry_has_commit(ie)) {
7679 memcpy(base_commit_id.sha1, ie->commit_sha1,
7680 SHA1_DIGEST_LENGTH);
7681 base_commit_idp = &base_commit_id;
7684 if (status == GOT_STATUS_CONFLICT) {
7685 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7687 } else if (status != GOT_STATUS_ADD &&
7688 status != GOT_STATUS_MODIFY &&
7689 status != GOT_STATUS_DELETE) {
7690 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7694 a->have_changes = 1;
7699 err = check_out_of_date(p, status, staged_status,
7700 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7701 GOT_ERR_STAGE_OUT_OF_DATE);
7707 struct stage_path_arg {
7708 struct got_worktree *worktree;
7709 struct got_fileindex *fileindex;
7710 struct got_repository *repo;
7711 got_worktree_status_cb status_cb;
7713 got_worktree_patch_cb patch_cb;
7715 int staged_something;
7716 int allow_bad_symlinks;
7719 static const struct got_error *
7720 stage_path(void *arg, unsigned char status,
7721 unsigned char staged_status, const char *relpath,
7722 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7723 struct got_object_id *commit_id, int dirfd, const char *de_name)
7725 struct stage_path_arg *a = arg;
7726 const struct got_error *err = NULL;
7727 struct got_fileindex_entry *ie;
7728 char *ondisk_path = NULL, *path_content = NULL;
7730 struct got_object_id *new_staged_blob_id = NULL;
7733 if (status == GOT_STATUS_UNVERSIONED)
7736 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7738 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7740 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7742 return got_error_from_errno("asprintf");
7745 case GOT_STATUS_ADD:
7746 case GOT_STATUS_MODIFY:
7747 /* XXX could sb.st_mode be passed in by our caller? */
7748 if (lstat(ondisk_path, &sb) == -1) {
7749 err = got_error_from_errno2("lstat", ondisk_path);
7753 if (status == GOT_STATUS_ADD) {
7754 int choice = GOT_PATCH_CHOICE_NONE;
7755 err = (*a->patch_cb)(&choice, a->patch_arg,
7756 status, ie->path, NULL, 1, 1);
7759 if (choice != GOT_PATCH_CHOICE_YES)
7762 err = create_patched_content(&path_content, 0,
7763 staged_blob_id ? staged_blob_id : blob_id,
7764 ondisk_path, dirfd, de_name, ie->path,
7765 a->repo, a->patch_cb, a->patch_arg);
7766 if (err || path_content == NULL)
7770 err = got_object_blob_create(&new_staged_blob_id,
7771 path_content ? path_content : ondisk_path, a->repo);
7774 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7775 SHA1_DIGEST_LENGTH);
7776 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7777 stage = GOT_FILEIDX_STAGE_ADD;
7779 stage = GOT_FILEIDX_STAGE_MODIFY;
7780 got_fileindex_entry_stage_set(ie, stage);
7781 if (S_ISLNK(sb.st_mode)) {
7782 int is_bad_symlink = 0;
7783 if (!a->allow_bad_symlinks) {
7784 char target_path[PATH_MAX];
7786 target_len = readlink(ondisk_path, target_path,
7787 sizeof(target_path));
7788 if (target_len == -1) {
7789 err = got_error_from_errno2("readlink",
7793 err = is_bad_symlink_target(&is_bad_symlink,
7794 target_path, target_len, ondisk_path,
7795 a->worktree->root_path);
7798 if (is_bad_symlink) {
7799 err = got_error_path(ondisk_path,
7800 GOT_ERR_BAD_SYMLINK);
7805 got_fileindex_entry_staged_filetype_set(ie,
7806 GOT_FILEIDX_MODE_BAD_SYMLINK);
7808 got_fileindex_entry_staged_filetype_set(ie,
7809 GOT_FILEIDX_MODE_SYMLINK);
7811 got_fileindex_entry_staged_filetype_set(ie,
7812 GOT_FILEIDX_MODE_REGULAR_FILE);
7814 a->staged_something = 1;
7815 if (a->status_cb == NULL)
7817 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7818 get_staged_status(ie), relpath, blob_id,
7819 new_staged_blob_id, NULL, dirfd, de_name);
7821 case GOT_STATUS_DELETE:
7822 if (staged_status == GOT_STATUS_DELETE)
7825 int choice = GOT_PATCH_CHOICE_NONE;
7826 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7827 ie->path, NULL, 1, 1);
7830 if (choice == GOT_PATCH_CHOICE_NO)
7832 if (choice != GOT_PATCH_CHOICE_YES) {
7833 err = got_error(GOT_ERR_PATCH_CHOICE);
7837 stage = GOT_FILEIDX_STAGE_DELETE;
7838 got_fileindex_entry_stage_set(ie, stage);
7839 a->staged_something = 1;
7840 if (a->status_cb == NULL)
7842 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7843 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7846 case GOT_STATUS_NO_CHANGE:
7848 case GOT_STATUS_CONFLICT:
7849 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7851 case GOT_STATUS_NONEXISTENT:
7852 err = got_error_set_errno(ENOENT, relpath);
7855 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7859 if (path_content && unlink(path_content) == -1 && err == NULL)
7860 err = got_error_from_errno2("unlink", path_content);
7863 free(new_staged_blob_id);
7867 const struct got_error *
7868 got_worktree_stage(struct got_worktree *worktree,
7869 struct got_pathlist_head *paths,
7870 got_worktree_status_cb status_cb, void *status_arg,
7871 got_worktree_patch_cb patch_cb, void *patch_arg,
7872 int allow_bad_symlinks, struct got_repository *repo)
7874 const struct got_error *err = NULL, *sync_err, *unlockerr;
7875 struct got_pathlist_entry *pe;
7876 struct got_fileindex *fileindex = NULL;
7877 char *fileindex_path = NULL;
7878 struct got_reference *head_ref = NULL;
7879 struct got_object_id *head_commit_id = NULL;
7880 struct check_stage_ok_arg oka;
7881 struct stage_path_arg spa;
7883 err = lock_worktree(worktree, LOCK_EX);
7887 err = got_ref_open(&head_ref, repo,
7888 got_worktree_get_head_ref_name(worktree), 0);
7891 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7894 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7898 /* Check pre-conditions before staging anything. */
7899 oka.head_commit_id = head_commit_id;
7900 oka.worktree = worktree;
7901 oka.fileindex = fileindex;
7903 oka.have_changes = 0;
7904 TAILQ_FOREACH(pe, paths, entry) {
7905 err = worktree_status(worktree, pe->path, fileindex, repo,
7906 check_stage_ok, &oka, NULL, NULL, 1, 0);
7910 if (!oka.have_changes) {
7911 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7915 spa.worktree = worktree;
7916 spa.fileindex = fileindex;
7918 spa.patch_cb = patch_cb;
7919 spa.patch_arg = patch_arg;
7920 spa.status_cb = status_cb;
7921 spa.status_arg = status_arg;
7922 spa.staged_something = 0;
7923 spa.allow_bad_symlinks = allow_bad_symlinks;
7924 TAILQ_FOREACH(pe, paths, entry) {
7925 err = worktree_status(worktree, pe->path, fileindex, repo,
7926 stage_path, &spa, NULL, NULL, 1, 0);
7930 if (!spa.staged_something) {
7931 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7935 sync_err = sync_fileindex(fileindex, fileindex_path);
7936 if (sync_err && err == NULL)
7940 got_ref_close(head_ref);
7941 free(head_commit_id);
7942 free(fileindex_path);
7944 got_fileindex_free(fileindex);
7945 unlockerr = lock_worktree(worktree, LOCK_SH);
7946 if (unlockerr && err == NULL)
7951 struct unstage_path_arg {
7952 struct got_worktree *worktree;
7953 struct got_fileindex *fileindex;
7954 struct got_repository *repo;
7955 got_worktree_checkout_cb progress_cb;
7957 got_worktree_patch_cb patch_cb;
7961 static const struct got_error *
7962 create_unstaged_content(char **path_unstaged_content,
7963 char **path_new_staged_content, struct got_object_id *blob_id,
7964 struct got_object_id *staged_blob_id, const char *relpath,
7965 struct got_repository *repo,
7966 got_worktree_patch_cb patch_cb, void *patch_arg)
7968 const struct got_error *err, *free_err;
7969 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7970 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7971 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7972 struct got_diffreg_result *diffreg_result = NULL;
7973 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7974 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7976 *path_unstaged_content = NULL;
7977 *path_new_staged_content = NULL;
7979 err = got_object_id_str(&label1, blob_id);
7982 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7986 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7990 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7994 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7998 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8002 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8006 err = got_diff_files(&diffreg_result, f1, label1, f2,
8007 path2, 3, 0, 1, NULL);
8011 err = got_opentemp_named(path_unstaged_content, &outfile,
8012 "got-unstaged-content");
8015 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8016 "got-new-staged-content");
8020 if (fseek(f1, 0L, SEEK_SET) == -1) {
8021 err = got_ferror(f1, GOT_ERR_IO);
8024 if (fseek(f2, 0L, SEEK_SET) == -1) {
8025 err = got_ferror(f2, GOT_ERR_IO);
8028 /* Count the number of actual changes in the diff result. */
8029 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8030 struct diff_chunk_context cc = {};
8031 diff_chunk_context_load_change(&cc, &nchunks_used,
8032 diffreg_result->result, n, 0);
8035 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8037 err = apply_or_reject_change(&choice, &nchunks_used,
8038 diffreg_result->result, n, relpath, f1, f2,
8039 &line_cur1, &line_cur2,
8040 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8043 if (choice == GOT_PATCH_CHOICE_YES)
8046 have_rejected_content = 1;
8047 if (choice == GOT_PATCH_CHOICE_QUIT)
8050 if (have_content || have_rejected_content)
8051 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8052 outfile, rejectfile);
8056 got_object_blob_close(blob);
8058 got_object_blob_close(staged_blob);
8059 free_err = got_diffreg_result_free(diffreg_result);
8060 if (free_err && err == NULL)
8062 if (f1 && fclose(f1) == EOF && err == NULL)
8063 err = got_error_from_errno2("fclose", path1);
8064 if (f2 && fclose(f2) == EOF && err == NULL)
8065 err = got_error_from_errno2("fclose", path2);
8066 if (outfile && fclose(outfile) == EOF && err == NULL)
8067 err = got_error_from_errno2("fclose", *path_unstaged_content);
8068 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8069 err = got_error_from_errno2("fclose", *path_new_staged_content);
8070 if (path1 && unlink(path1) == -1 && err == NULL)
8071 err = got_error_from_errno2("unlink", path1);
8072 if (path2 && unlink(path2) == -1 && err == NULL)
8073 err = got_error_from_errno2("unlink", path2);
8074 if (err || !have_content) {
8075 if (*path_unstaged_content &&
8076 unlink(*path_unstaged_content) == -1 && err == NULL)
8077 err = got_error_from_errno2("unlink",
8078 *path_unstaged_content);
8079 free(*path_unstaged_content);
8080 *path_unstaged_content = NULL;
8082 if (err || !have_content || !have_rejected_content) {
8083 if (*path_new_staged_content &&
8084 unlink(*path_new_staged_content) == -1 && err == NULL)
8085 err = got_error_from_errno2("unlink",
8086 *path_new_staged_content);
8087 free(*path_new_staged_content);
8088 *path_new_staged_content = NULL;
8095 static const struct got_error *
8096 unstage_hunks(struct got_object_id *staged_blob_id,
8097 struct got_blob_object *blob_base,
8098 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8099 const char *ondisk_path, const char *label_orig,
8100 struct got_worktree *worktree, struct got_repository *repo,
8101 got_worktree_patch_cb patch_cb, void *patch_arg,
8102 got_worktree_checkout_cb progress_cb, void *progress_arg)
8104 const struct got_error *err = NULL;
8105 char *path_unstaged_content = NULL;
8106 char *path_new_staged_content = NULL;
8107 char *parent = NULL, *base_path = NULL;
8108 char *blob_base_path = NULL;
8109 struct got_object_id *new_staged_blob_id = NULL;
8110 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8113 err = create_unstaged_content(&path_unstaged_content,
8114 &path_new_staged_content, blob_id, staged_blob_id,
8115 ie->path, repo, patch_cb, patch_arg);
8119 if (path_unstaged_content == NULL)
8122 if (path_new_staged_content) {
8123 err = got_object_blob_create(&new_staged_blob_id,
8124 path_new_staged_content, repo);
8129 f = fopen(path_unstaged_content, "r");
8131 err = got_error_from_errno2("fopen",
8132 path_unstaged_content);
8135 if (fstat(fileno(f), &sb) == -1) {
8136 err = got_error_from_errno2("fstat", path_unstaged_content);
8139 if (got_fileindex_entry_staged_filetype_get(ie) ==
8140 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8141 char link_target[PATH_MAX];
8143 r = fread(link_target, 1, sizeof(link_target), f);
8144 if (r == 0 && ferror(f)) {
8145 err = got_error_from_errno("fread");
8148 if (r >= sizeof(link_target)) { /* should not happen */
8149 err = got_error(GOT_ERR_NO_SPACE);
8152 link_target[r] = '\0';
8153 err = merge_symlink(worktree, blob_base,
8154 ondisk_path, ie->path, label_orig, link_target,
8155 worktree->base_commit_id, repo, progress_cb,
8158 int local_changes_subsumed;
8160 err = got_path_dirname(&parent, ondisk_path);
8164 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8166 err = got_error_from_errno("asprintf");
8171 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8174 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8180 * In order the run a 3-way merge with a symlink we copy the symlink's
8181 * target path into a temporary file and use that file with diff3.
8183 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8184 err = dump_symlink_target_path_to_file(&f_deriv2,
8190 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8192 err = got_error_from_errno2("open", ondisk_path);
8195 f_deriv2 = fdopen(fd, "r");
8196 if (f_deriv2 == NULL) {
8197 err = got_error_from_errno2("fdopen", ondisk_path);
8203 err = merge_file(&local_changes_subsumed, worktree,
8204 f_base, f, f_deriv2, ondisk_path, ie->path,
8205 got_fileindex_perms_to_st(ie),
8206 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8207 repo, progress_cb, progress_arg);
8212 if (new_staged_blob_id) {
8213 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8214 SHA1_DIGEST_LENGTH);
8216 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8217 got_fileindex_entry_staged_filetype_set(ie, 0);
8220 free(new_staged_blob_id);
8221 if (path_unstaged_content &&
8222 unlink(path_unstaged_content) == -1 && err == NULL)
8223 err = got_error_from_errno2("unlink", path_unstaged_content);
8224 if (path_new_staged_content &&
8225 unlink(path_new_staged_content) == -1 && err == NULL)
8226 err = got_error_from_errno2("unlink", path_new_staged_content);
8227 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8228 err = got_error_from_errno2("unlink", blob_base_path);
8229 if (f_base && fclose(f_base) == EOF && err == NULL)
8230 err = got_error_from_errno2("fclose", path_unstaged_content);
8231 if (f && fclose(f) == EOF && err == NULL)
8232 err = got_error_from_errno2("fclose", path_unstaged_content);
8233 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8234 err = got_error_from_errno2("fclose", ondisk_path);
8235 free(path_unstaged_content);
8236 free(path_new_staged_content);
8237 free(blob_base_path);
8243 static const struct got_error *
8244 unstage_path(void *arg, unsigned char status,
8245 unsigned char staged_status, const char *relpath,
8246 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8247 struct got_object_id *commit_id, int dirfd, const char *de_name)
8249 const struct got_error *err = NULL;
8250 struct unstage_path_arg *a = arg;
8251 struct got_fileindex_entry *ie;
8252 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8253 char *ondisk_path = NULL;
8254 char *id_str = NULL, *label_orig = NULL;
8255 int local_changes_subsumed;
8258 if (staged_status != GOT_STATUS_ADD &&
8259 staged_status != GOT_STATUS_MODIFY &&
8260 staged_status != GOT_STATUS_DELETE)
8263 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8265 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8267 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8269 return got_error_from_errno("asprintf");
8271 err = got_object_id_str(&id_str,
8272 commit_id ? commit_id : a->worktree->base_commit_id);
8275 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8277 err = got_error_from_errno("asprintf");
8281 switch (staged_status) {
8282 case GOT_STATUS_MODIFY:
8283 err = got_object_open_as_blob(&blob_base, a->repo,
8288 case GOT_STATUS_ADD:
8290 if (staged_status == GOT_STATUS_ADD) {
8291 int choice = GOT_PATCH_CHOICE_NONE;
8292 err = (*a->patch_cb)(&choice, a->patch_arg,
8293 staged_status, ie->path, NULL, 1, 1);
8296 if (choice != GOT_PATCH_CHOICE_YES)
8299 err = unstage_hunks(staged_blob_id,
8300 blob_base, blob_id, ie, ondisk_path,
8301 label_orig, a->worktree, a->repo,
8302 a->patch_cb, a->patch_arg,
8303 a->progress_cb, a->progress_arg);
8304 break; /* Done with this file. */
8307 err = got_object_open_as_blob(&blob_staged, a->repo,
8308 staged_blob_id, 8192);
8311 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8312 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8313 case GOT_FILEIDX_MODE_REGULAR_FILE:
8314 err = merge_blob(&local_changes_subsumed, a->worktree,
8315 blob_base, ondisk_path, relpath,
8316 got_fileindex_perms_to_st(ie), label_orig,
8317 blob_staged, commit_id ? commit_id :
8318 a->worktree->base_commit_id, a->repo,
8319 a->progress_cb, a->progress_arg);
8321 case GOT_FILEIDX_MODE_SYMLINK:
8322 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8323 char *staged_target;
8324 err = got_object_blob_read_to_str(
8325 &staged_target, blob_staged);
8328 err = merge_symlink(a->worktree, blob_base,
8329 ondisk_path, relpath, label_orig,
8330 staged_target, commit_id ? commit_id :
8331 a->worktree->base_commit_id,
8332 a->repo, a->progress_cb, a->progress_arg);
8333 free(staged_target);
8335 err = merge_blob(&local_changes_subsumed,
8336 a->worktree, blob_base, ondisk_path,
8337 relpath, got_fileindex_perms_to_st(ie),
8338 label_orig, blob_staged,
8339 commit_id ? commit_id :
8340 a->worktree->base_commit_id, a->repo,
8341 a->progress_cb, a->progress_arg);
8345 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8349 got_fileindex_entry_stage_set(ie,
8350 GOT_FILEIDX_STAGE_NONE);
8351 got_fileindex_entry_staged_filetype_set(ie, 0);
8354 case GOT_STATUS_DELETE:
8356 int choice = GOT_PATCH_CHOICE_NONE;
8357 err = (*a->patch_cb)(&choice, a->patch_arg,
8358 staged_status, ie->path, NULL, 1, 1);
8361 if (choice == GOT_PATCH_CHOICE_NO)
8363 if (choice != GOT_PATCH_CHOICE_YES) {
8364 err = got_error(GOT_ERR_PATCH_CHOICE);
8368 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8369 got_fileindex_entry_staged_filetype_set(ie, 0);
8370 err = get_file_status(&status, &sb, ie, ondisk_path,
8371 dirfd, de_name, a->repo);
8374 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8380 got_object_blob_close(blob_base);
8382 got_object_blob_close(blob_staged);
8388 const struct got_error *
8389 got_worktree_unstage(struct got_worktree *worktree,
8390 struct got_pathlist_head *paths,
8391 got_worktree_checkout_cb progress_cb, void *progress_arg,
8392 got_worktree_patch_cb patch_cb, void *patch_arg,
8393 struct got_repository *repo)
8395 const struct got_error *err = NULL, *sync_err, *unlockerr;
8396 struct got_pathlist_entry *pe;
8397 struct got_fileindex *fileindex = NULL;
8398 char *fileindex_path = NULL;
8399 struct unstage_path_arg upa;
8401 err = lock_worktree(worktree, LOCK_EX);
8405 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8409 upa.worktree = worktree;
8410 upa.fileindex = fileindex;
8412 upa.progress_cb = progress_cb;
8413 upa.progress_arg = progress_arg;
8414 upa.patch_cb = patch_cb;
8415 upa.patch_arg = patch_arg;
8416 TAILQ_FOREACH(pe, paths, entry) {
8417 err = worktree_status(worktree, pe->path, fileindex, repo,
8418 unstage_path, &upa, NULL, NULL, 1, 0);
8423 sync_err = sync_fileindex(fileindex, fileindex_path);
8424 if (sync_err && err == NULL)
8427 free(fileindex_path);
8429 got_fileindex_free(fileindex);
8430 unlockerr = lock_worktree(worktree, LOCK_SH);
8431 if (unlockerr && err == NULL)
8436 struct report_file_info_arg {
8437 struct got_worktree *worktree;
8438 got_worktree_path_info_cb info_cb;
8440 struct got_pathlist_head *paths;
8441 got_cancel_cb cancel_cb;
8445 static const struct got_error *
8446 report_file_info(void *arg, struct got_fileindex_entry *ie)
8448 struct report_file_info_arg *a = arg;
8449 struct got_pathlist_entry *pe;
8450 struct got_object_id blob_id, staged_blob_id, commit_id;
8451 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8452 struct got_object_id *commit_idp = NULL;
8455 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8456 return got_error(GOT_ERR_CANCELLED);
8458 TAILQ_FOREACH(pe, a->paths, entry) {
8459 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8460 got_path_is_child(ie->path, pe->path, pe->path_len))
8463 if (pe == NULL) /* not found */
8466 if (got_fileindex_entry_has_blob(ie)) {
8467 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8468 blob_idp = &blob_id;
8470 stage = got_fileindex_entry_stage_get(ie);
8471 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8472 stage == GOT_FILEIDX_STAGE_ADD) {
8473 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8474 SHA1_DIGEST_LENGTH);
8475 staged_blob_idp = &staged_blob_id;
8478 if (got_fileindex_entry_has_commit(ie)) {
8479 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8480 commit_idp = &commit_id;
8483 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8484 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8487 const struct got_error *
8488 got_worktree_path_info(struct got_worktree *worktree,
8489 struct got_pathlist_head *paths,
8490 got_worktree_path_info_cb info_cb, void *info_arg,
8491 got_cancel_cb cancel_cb, void *cancel_arg)
8494 const struct got_error *err = NULL, *unlockerr;
8495 struct got_fileindex *fileindex = NULL;
8496 char *fileindex_path = NULL;
8497 struct report_file_info_arg arg;
8499 err = lock_worktree(worktree, LOCK_SH);
8503 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8507 arg.worktree = worktree;
8508 arg.info_cb = info_cb;
8509 arg.info_arg = info_arg;
8511 arg.cancel_cb = cancel_cb;
8512 arg.cancel_arg = cancel_arg;
8513 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8516 free(fileindex_path);
8518 got_fileindex_free(fileindex);
8519 unlockerr = lock_worktree(worktree, LOCK_UN);
8520 if (unlockerr && err == NULL)