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 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
142 err = got_error_from_errno2("open", path);
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
171 (*content)[sb.st_size - 1] = '\0';
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
193 return got_error_from_errno("got_ref_to_str");
195 refstr = strdup(got_ref_get_name(head_ref));
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
211 uint32_t uuid_status;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
224 err = got_ref_resolve(&commit_id, repo, head_ref);
227 err = got_object_get_type(&obj_type, repo, commit_id);
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
433 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 if ((*worktree)->root_fd == -1) {
435 err = got_error_from_errno2("open", (*worktree)->root_path);
440 const struct got_error *close_err = got_repo_close(repo);
446 free(base_commit_id_str);
452 if (*worktree != NULL)
453 got_worktree_close(*worktree);
456 (*worktree)->lockfd = fd;
461 const struct got_error *
462 got_worktree_open(struct got_worktree **worktree, const char *path)
464 const struct got_error *err = NULL;
467 worktree_path = strdup(path);
468 if (worktree_path == NULL)
469 return got_error_from_errno("strdup");
474 err = open_worktree(worktree, worktree_path);
475 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
483 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
485 err = got_path_dirname(&parent_path, worktree_path);
487 if (err->code != GOT_ERR_BAD_PATH) {
494 worktree_path = parent_path;
498 return got_error(GOT_ERR_NOT_WORKTREE);
501 const struct got_error *
502 got_worktree_close(struct got_worktree *worktree)
504 const struct got_error *err = NULL;
506 if (worktree->lockfd != -1) {
507 if (close(worktree->lockfd) == -1)
508 err = got_error_from_errno2("close",
509 got_worktree_get_root_path(worktree));
511 if (close(worktree->root_fd) == -1 && err == NULL)
512 err = got_error_from_errno2("close",
513 got_worktree_get_root_path(worktree));
514 free(worktree->repo_path);
515 free(worktree->path_prefix);
516 free(worktree->base_commit_id);
517 free(worktree->head_ref_name);
518 free(worktree->root_path);
519 free(worktree->gotconfig_path);
520 got_gotconfig_free(worktree->gotconfig);
526 got_worktree_get_root_path(struct got_worktree *worktree)
528 return worktree->root_path;
532 got_worktree_get_repo_path(struct got_worktree *worktree)
534 return worktree->repo_path;
537 got_worktree_get_path_prefix(struct got_worktree *worktree)
539 return worktree->path_prefix;
542 const struct got_error *
543 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
544 const char *path_prefix)
546 char *absprefix = NULL;
548 if (!got_path_is_absolute(path_prefix)) {
549 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
550 return got_error_from_errno("asprintf");
552 *match = (strcmp(absprefix ? absprefix : path_prefix,
553 worktree->path_prefix) == 0);
559 got_worktree_get_head_ref_name(struct got_worktree *worktree)
561 return worktree->head_ref_name;
564 const struct got_error *
565 got_worktree_set_head_ref(struct got_worktree *worktree,
566 struct got_reference *head_ref)
568 const struct got_error *err = NULL;
569 char *path_got = NULL, *head_ref_name = NULL;
571 if (asprintf(&path_got, "%s/%s", worktree->root_path,
572 GOT_WORKTREE_GOT_DIR) == -1) {
573 err = got_error_from_errno("asprintf");
578 head_ref_name = strdup(got_ref_get_name(head_ref));
579 if (head_ref_name == NULL) {
580 err = got_error_from_errno("strdup");
584 err = write_head_ref(path_got, head_ref);
588 free(worktree->head_ref_name);
589 worktree->head_ref_name = head_ref_name;
597 struct got_object_id *
598 got_worktree_get_base_commit_id(struct got_worktree *worktree)
600 return worktree->base_commit_id;
603 const struct got_error *
604 got_worktree_set_base_commit_id(struct got_worktree *worktree,
605 struct got_repository *repo, struct got_object_id *commit_id)
607 const struct got_error *err;
608 struct got_object *obj = NULL;
610 char *path_got = NULL;
612 if (asprintf(&path_got, "%s/%s", worktree->root_path,
613 GOT_WORKTREE_GOT_DIR) == -1) {
614 err = got_error_from_errno("asprintf");
619 err = got_object_open(&obj, repo, commit_id);
623 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
624 err = got_error(GOT_ERR_OBJ_TYPE);
628 /* Record our base commit. */
629 err = got_object_id_str(&id_str, commit_id);
632 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
636 free(worktree->base_commit_id);
637 worktree->base_commit_id = got_object_id_dup(commit_id);
638 if (worktree->base_commit_id == NULL) {
639 err = got_error_from_errno("got_object_id_dup");
644 got_object_close(obj);
650 const struct got_gotconfig *
651 got_worktree_get_gotconfig(struct got_worktree *worktree)
653 return worktree->gotconfig;
656 static const struct got_error *
657 lock_worktree(struct got_worktree *worktree, int operation)
659 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
660 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
661 : got_error_from_errno2("flock",
662 got_worktree_get_root_path(worktree)));
666 static const struct got_error *
667 add_dir_on_disk(struct got_worktree *worktree, const char *path)
669 const struct got_error *err = NULL;
672 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
673 return got_error_from_errno("asprintf");
675 err = got_path_mkdir(abspath);
676 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
679 if (lstat(abspath, &sb) == -1) {
680 err = got_error_from_errno2("lstat", abspath);
681 } else if (!S_ISDIR(sb.st_mode)) {
682 /* TODO directory is obstructed; do something */
683 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
690 static const struct got_error *
691 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
693 const struct got_error *err = NULL;
696 size_t flen1 = 0, flen2 = 0;
701 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
702 if (flen1 == 0 && ferror(f1)) {
703 err = got_error_from_errno("fread");
706 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
707 if (flen2 == 0 && ferror(f2)) {
708 err = got_error_from_errno("fread");
715 } else if (flen2 == 0) {
719 } else if (flen1 == flen2) {
720 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
733 static const struct got_error *
734 check_files_equal(int *same, FILE *f1, FILE *f2)
741 if (fstat(fileno(f1), &sb) != 0)
742 return got_error_from_errno("fstat");
745 if (fstat(fileno(f2), &sb) != 0)
746 return got_error_from_errno("fstat");
749 if (size1 != size2) {
754 if (fseek(f1, 0L, SEEK_SET) == -1)
755 return got_ferror(f1, GOT_ERR_IO);
756 if (fseek(f2, 0L, SEEK_SET) == -1)
757 return got_ferror(f2, GOT_ERR_IO);
759 return check_file_contents_equal(same, f1, f2);
763 * Perform a 3-way merge where the file f_orig acts as the common
764 * ancestor, the file f_deriv acts as the first derived version,
765 * and the file f_deriv2 acts as the second derived version.
766 * The merge result will be written to a new file at ondisk_path; any
767 * existing file at this path will be replaced.
769 static const struct got_error *
770 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
771 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
772 const char *path, uint16_t st_mode,
773 const char *label_orig, const char *label_deriv, const char *label_deriv2,
774 enum got_diff_algorithm diff_algo, struct got_repository *repo,
775 got_worktree_checkout_cb progress_cb, void *progress_arg)
777 const struct got_error *err = NULL;
779 FILE *f_merged = NULL;
780 char *merged_path = NULL, *base_path = NULL;
784 *local_changes_subsumed = 0;
786 err = got_path_dirname(&parent, ondisk_path);
790 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
791 err = got_error_from_errno("asprintf");
795 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
799 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
800 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
804 err = (*progress_cb)(progress_arg,
805 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 if (fsync(merged_fd) != 0) {
810 err = got_error_from_errno("fsync");
814 f_merged = fdopen(merged_fd, "r");
815 if (f_merged == NULL) {
816 err = got_error_from_errno("fdopen");
821 /* Check if a clean merge has subsumed all local changes. */
822 if (overlapcnt == 0) {
823 err = check_files_equal(local_changes_subsumed, f_deriv,
829 if (fchmod(fileno(f_merged), st_mode) != 0) {
830 err = got_error_from_errno2("fchmod", merged_path);
834 if (rename(merged_path, ondisk_path) != 0) {
835 err = got_error_from_errno3("rename", merged_path,
844 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
845 err = got_error_from_errno("close");
846 if (f_merged && fclose(f_merged) == EOF && err == NULL)
847 err = got_error_from_errno("fclose");
854 static const struct got_error *
855 update_symlink(const char *ondisk_path, const char *target_path,
858 /* This is not atomic but matches what 'ln -sf' does. */
859 if (unlink(ondisk_path) == -1)
860 return got_error_from_errno2("unlink", ondisk_path);
861 if (symlink(target_path, ondisk_path) == -1)
862 return got_error_from_errno3("symlink", target_path,
868 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
869 * in the work tree with a file that contains conflict markers and the
870 * conflicting target paths of the original version, a "derived version"
871 * of a symlink from an incoming change, and a local version of the symlink.
873 * The original versions's target path can be NULL if it is not available,
874 * such as if both derived versions added a new symlink at the same path.
876 * The incoming derived symlink target is NULL in case the incoming change
877 * has deleted this symlink.
879 static const struct got_error *
880 install_symlink_conflict(const char *deriv_target,
881 struct got_object_id *deriv_base_commit_id, const char *orig_target,
882 const char *label_orig, const char *local_target, const char *ondisk_path)
884 const struct got_error *err;
885 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
888 err = got_object_id_str(&id_str, deriv_base_commit_id);
890 return got_error_from_errno("asprintf");
892 if (asprintf(&label_deriv, "%s: commit %s",
893 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
894 err = got_error_from_errno("asprintf");
898 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
902 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
903 err = got_error_from_errno2("fchmod", path);
907 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
908 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
909 deriv_target ? deriv_target : "(symlink was deleted)",
910 orig_target ? label_orig : "",
911 orig_target ? "\n" : "",
912 orig_target ? orig_target : "",
913 orig_target ? "\n" : "",
914 GOT_DIFF_CONFLICT_MARKER_SEP,
915 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
916 err = got_error_from_errno2("fprintf", path);
920 if (unlink(ondisk_path) == -1) {
921 err = got_error_from_errno2("unlink", ondisk_path);
924 if (rename(path, ondisk_path) == -1) {
925 err = got_error_from_errno3("rename", path, ondisk_path);
929 if (f != NULL && fclose(f) == EOF && err == NULL)
930 err = got_error_from_errno2("fclose", path);
937 /* forward declaration */
938 static const struct got_error *
939 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
940 const char *, const char *, uint16_t, const char *,
941 struct got_blob_object *, struct got_object_id *,
942 struct got_repository *, got_worktree_checkout_cb, void *);
945 * Merge a symlink into the work tree, where blob_orig acts as the common
946 * ancestor, deriv_target is the link target of the first derived version,
947 * and the symlink on disk acts as the second derived version.
948 * Assume that contents of both blobs represent symlinks.
950 static const struct got_error *
951 merge_symlink(struct got_worktree *worktree,
952 struct got_blob_object *blob_orig, const char *ondisk_path,
953 const char *path, const char *label_orig, const char *deriv_target,
954 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
955 got_worktree_checkout_cb progress_cb, void *progress_arg)
957 const struct got_error *err = NULL;
958 char *ancestor_target = NULL;
960 ssize_t ondisk_len, deriv_len;
961 char ondisk_target[PATH_MAX];
962 int have_local_change = 0;
963 int have_incoming_change = 0;
965 if (lstat(ondisk_path, &sb) == -1)
966 return got_error_from_errno2("lstat", ondisk_path);
968 ondisk_len = readlink(ondisk_path, ondisk_target,
969 sizeof(ondisk_target));
970 if (ondisk_len == -1) {
971 err = got_error_from_errno2("readlink",
975 ondisk_target[ondisk_len] = '\0';
978 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
983 if (ancestor_target == NULL ||
984 (ondisk_len != strlen(ancestor_target) ||
985 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
986 have_local_change = 1;
988 deriv_len = strlen(deriv_target);
989 if (ancestor_target == NULL ||
990 (deriv_len != strlen(ancestor_target) ||
991 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
992 have_incoming_change = 1;
994 if (!have_local_change && !have_incoming_change) {
995 if (ancestor_target) {
996 /* Both sides made the same change. */
997 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
999 } else if (deriv_len == ondisk_len &&
1000 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1001 /* Both sides added the same symlink. */
1002 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1005 /* Both sides added symlinks which don't match. */
1006 err = install_symlink_conflict(deriv_target,
1007 deriv_base_commit_id, ancestor_target,
1008 label_orig, ondisk_target, ondisk_path);
1011 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1014 } else if (!have_local_change && have_incoming_change) {
1015 /* Apply the incoming change. */
1016 err = update_symlink(ondisk_path, deriv_target,
1017 strlen(deriv_target));
1020 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1021 } else if (have_local_change && have_incoming_change) {
1022 if (deriv_len == ondisk_len &&
1023 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1024 /* Both sides made the same change. */
1025 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1028 err = install_symlink_conflict(deriv_target,
1029 deriv_base_commit_id, ancestor_target, label_orig,
1030 ondisk_target, ondisk_path);
1033 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1039 free(ancestor_target);
1043 static const struct got_error *
1044 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1046 const struct got_error *err = NULL;
1047 char target_path[PATH_MAX];
1056 return got_error_from_errno("got_opentemp");
1057 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1058 if (target_len == -1) {
1059 err = got_error_from_errno2("readlink", ondisk_path);
1062 n = fwrite(target_path, 1, target_len, f);
1063 if (n != target_len) {
1064 err = got_ferror(f, GOT_ERR_IO);
1067 if (fflush(f) == EOF) {
1068 err = got_error_from_errno("fflush");
1071 if (fseek(f, 0L, SEEK_SET) == -1) {
1072 err = got_ferror(f, GOT_ERR_IO);
1084 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1085 * blob_deriv acts as the first derived version, and the file on disk
1086 * acts as the second derived version.
1088 static const struct got_error *
1089 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1090 struct got_blob_object *blob_orig, const char *ondisk_path,
1091 const char *path, uint16_t st_mode, const char *label_orig,
1092 struct got_blob_object *blob_deriv,
1093 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1094 got_worktree_checkout_cb progress_cb, void *progress_arg)
1096 const struct got_error *err = NULL;
1097 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1098 char *blob_orig_path = NULL;
1099 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1100 char *label_deriv = NULL, *parent = NULL;
1102 *local_changes_subsumed = 0;
1104 err = got_path_dirname(&parent, ondisk_path);
1109 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1111 err = got_error_from_errno("asprintf");
1116 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1119 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1126 * No common ancestor exists. This is an "add vs add" conflict
1127 * and we simply use an empty ancestor file to make both files
1128 * appear in the merged result in their entirety.
1130 f_orig = got_opentemp();
1131 if (f_orig == NULL) {
1132 err = got_error_from_errno("got_opentemp");
1137 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 err = got_error_from_errno("asprintf");
1143 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1151 err = got_object_id_str(&id_str, deriv_base_commit_id);
1154 if (asprintf(&label_deriv, "%s: commit %s",
1155 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 err = got_error_from_errno("asprintf");
1161 * In order the run a 3-way merge with a symlink we copy the symlink's
1162 * target path into a temporary file and use that file with diff3.
1164 if (S_ISLNK(st_mode)) {
1165 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1170 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1172 err = got_error_from_errno2("open", ondisk_path);
1175 f_deriv2 = fdopen(fd, "r");
1176 if (f_deriv2 == NULL) {
1177 err = got_error_from_errno2("fdopen", ondisk_path);
1183 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1184 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1185 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1187 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1188 err = got_error_from_errno("fclose");
1189 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1190 err = got_error_from_errno("fclose");
1191 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1192 err = got_error_from_errno("fclose");
1194 if (blob_orig_path) {
1195 unlink(blob_orig_path);
1196 free(blob_orig_path);
1198 if (blob_deriv_path) {
1199 unlink(blob_deriv_path);
1200 free(blob_deriv_path);
1208 static const struct got_error *
1209 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1210 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1211 int wt_fd, const char *path, struct got_object_id *blob_id)
1213 const struct got_error *err = NULL;
1214 struct got_fileindex_entry *new_ie;
1218 err = got_fileindex_entry_alloc(&new_ie, path);
1222 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1223 blob_id->sha1, base_commit_id->sha1, 1);
1227 err = got_fileindex_entry_add(fileindex, new_ie);
1230 got_fileindex_entry_free(new_ie);
1237 get_ondisk_perms(int executable, mode_t st_mode)
1239 mode_t xbits = S_IXUSR;
1242 /* Map read bits to execute bits. */
1243 if (st_mode & S_IRGRP)
1245 if (st_mode & S_IROTH)
1247 return st_mode | xbits;
1250 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1253 /* forward declaration */
1254 static const struct got_error *
1255 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1256 const char *path, mode_t te_mode, mode_t st_mode,
1257 struct got_blob_object *blob, int restoring_missing_file,
1258 int reverting_versioned_file, int installing_bad_symlink,
1259 int path_is_unversioned, struct got_repository *repo,
1260 got_worktree_checkout_cb progress_cb, void *progress_arg);
1263 * This function assumes that the provided symlink target points at a
1264 * safe location in the work tree!
1266 static const struct got_error *
1267 replace_existing_symlink(int *did_something, const char *ondisk_path,
1268 const char *target_path, size_t target_len)
1270 const struct got_error *err = NULL;
1272 char etarget[PATH_MAX];
1278 * "Bad" symlinks (those pointing outside the work tree or into the
1279 * .got directory) are installed in the work tree as a regular file
1280 * which contains the bad symlink target path.
1281 * The new symlink target has already been checked for safety by our
1282 * caller. If we can successfully open a regular file then we simply
1283 * replace this file with a symlink below.
1285 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1288 return got_error_from_errno2("open", ondisk_path);
1290 /* We are updating an existing on-disk symlink. */
1291 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1293 return got_error_from_errno2("readlink", ondisk_path);
1295 if (elen == target_len &&
1296 memcmp(etarget, target_path, target_len) == 0)
1297 return NULL; /* nothing to do */
1301 err = update_symlink(ondisk_path, target_path, target_len);
1302 if (fd != -1 && close(fd) == -1 && err == NULL)
1303 err = got_error_from_errno2("close", ondisk_path);
1307 static const struct got_error *
1308 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1309 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1311 const struct got_error *err = NULL;
1312 char canonpath[PATH_MAX];
1313 char *path_got = NULL;
1315 *is_bad_symlink = 0;
1317 if (target_len >= sizeof(canonpath)) {
1318 *is_bad_symlink = 1;
1323 * We do not use realpath(3) to resolve the symlink's target
1324 * path because we don't want to resolve symlinks recursively.
1325 * Instead we make the path absolute and then canonicalize it.
1326 * Relative symlink target lookup should begin at the directory
1327 * in which the blob object is being installed.
1329 if (!got_path_is_absolute(target_path)) {
1330 char *abspath, *parent;
1331 err = got_path_dirname(&parent, ondisk_path);
1334 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1336 return got_error_from_errno("asprintf");
1339 if (strlen(abspath) >= sizeof(canonpath)) {
1340 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1344 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1349 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1354 /* Only allow symlinks pointing at paths within the work tree. */
1355 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1356 *is_bad_symlink = 1;
1360 /* Do not allow symlinks pointing into the .got directory. */
1361 if (asprintf(&path_got, "%s/%s", wtroot_path,
1362 GOT_WORKTREE_GOT_DIR) == -1)
1363 return got_error_from_errno("asprintf");
1364 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1365 *is_bad_symlink = 1;
1371 static const struct got_error *
1372 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1373 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1374 int restoring_missing_file, int reverting_versioned_file,
1375 int path_is_unversioned, struct got_repository *repo,
1376 got_worktree_checkout_cb progress_cb, void *progress_arg)
1378 const struct got_error *err = NULL;
1379 char target_path[PATH_MAX];
1380 size_t len, target_len = 0;
1381 char *path_got = NULL;
1382 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1383 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1385 *is_bad_symlink = 0;
1388 * Blob object content specifies the target path of the link.
1389 * If a symbolic link cannot be installed we instead create
1390 * a regular file which contains the link target path stored
1391 * in the blob object.
1394 err = got_object_blob_read_block(&len, blob);
1395 if (len + target_len >= sizeof(target_path)) {
1396 /* Path too long; install as a regular file. */
1397 *is_bad_symlink = 1;
1398 got_object_blob_rewind(blob);
1399 return install_blob(worktree, ondisk_path, path,
1400 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1401 restoring_missing_file, reverting_versioned_file,
1402 1, path_is_unversioned, repo, progress_cb,
1406 /* Skip blob object header first time around. */
1407 memcpy(target_path + target_len, buf + hdrlen,
1409 target_len += len - hdrlen;
1413 target_path[target_len] = '\0';
1415 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1416 ondisk_path, worktree->root_path);
1420 if (*is_bad_symlink) {
1421 /* install as a regular file */
1422 got_object_blob_rewind(blob);
1423 err = install_blob(worktree, ondisk_path, path,
1424 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1425 restoring_missing_file, reverting_versioned_file, 1,
1426 path_is_unversioned, repo, progress_cb, progress_arg);
1430 if (symlink(target_path, ondisk_path) == -1) {
1431 if (errno == EEXIST) {
1432 int symlink_replaced;
1433 if (path_is_unversioned) {
1434 err = (*progress_cb)(progress_arg,
1435 GOT_STATUS_UNVERSIONED, path);
1438 err = replace_existing_symlink(&symlink_replaced,
1439 ondisk_path, target_path, target_len);
1443 if (symlink_replaced) {
1444 err = (*progress_cb)(progress_arg,
1445 reverting_versioned_file ?
1447 GOT_STATUS_UPDATE, path);
1449 err = (*progress_cb)(progress_arg,
1450 GOT_STATUS_EXISTS, path);
1453 goto done; /* Nothing else to do. */
1456 if (errno == ENOENT) {
1458 err = got_path_dirname(&parent, ondisk_path);
1461 err = add_dir_on_disk(worktree, parent);
1466 * Retry, and fall through to error handling
1467 * below if this second attempt fails.
1469 if (symlink(target_path, ondisk_path) != -1) {
1470 err = NULL; /* success */
1475 /* Handle errors from first or second creation attempt. */
1476 if (errno == ENAMETOOLONG) {
1477 /* bad target path; install as a regular file */
1478 *is_bad_symlink = 1;
1479 got_object_blob_rewind(blob);
1480 err = install_blob(worktree, ondisk_path, path,
1481 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1482 restoring_missing_file, reverting_versioned_file, 1,
1483 path_is_unversioned, repo,
1484 progress_cb, progress_arg);
1485 } else if (errno == ENOTDIR) {
1486 err = got_error_path(ondisk_path,
1487 GOT_ERR_FILE_OBSTRUCTED);
1489 err = got_error_from_errno3("symlink",
1490 target_path, ondisk_path);
1492 } else if (progress_cb)
1493 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1494 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1500 static const struct got_error *
1501 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1502 const char *path, mode_t te_mode, mode_t st_mode,
1503 struct got_blob_object *blob, int restoring_missing_file,
1504 int reverting_versioned_file, int installing_bad_symlink,
1505 int path_is_unversioned, struct got_repository *repo,
1506 got_worktree_checkout_cb progress_cb, void *progress_arg)
1508 const struct got_error *err = NULL;
1512 char *tmppath = NULL;
1514 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1515 GOT_DEFAULT_FILE_MODE);
1517 if (errno == ENOENT) {
1519 err = got_path_dirname(&parent, path);
1522 err = add_dir_on_disk(worktree, parent);
1526 fd = open(ondisk_path,
1527 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1528 GOT_DEFAULT_FILE_MODE);
1530 return got_error_from_errno2("open",
1532 } else if (errno == EEXIST) {
1533 if (path_is_unversioned) {
1534 err = (*progress_cb)(progress_arg,
1535 GOT_STATUS_UNVERSIONED, path);
1538 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1539 !S_ISREG(st_mode) && !installing_bad_symlink) {
1540 /* TODO file is obstructed; do something */
1541 err = got_error_path(ondisk_path,
1542 GOT_ERR_FILE_OBSTRUCTED);
1545 err = got_opentemp_named_fd(&tmppath, &fd,
1552 return got_error_from_errno2("open", ondisk_path);
1555 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1556 err = got_error_from_errno2("fchmod",
1557 update ? tmppath : ondisk_path);
1562 if (restoring_missing_file)
1563 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1565 else if (reverting_versioned_file)
1566 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1569 err = (*progress_cb)(progress_arg,
1570 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1575 hdrlen = got_object_blob_get_hdrlen(blob);
1577 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1578 err = got_object_blob_read_block(&len, blob);
1582 /* Skip blob object header first time around. */
1583 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1585 err = got_error_from_errno("write");
1587 } else if (outlen != len - hdrlen) {
1588 err = got_error(GOT_ERR_IO);
1595 if (fsync(fd) != 0) {
1596 err = got_error_from_errno("fsync");
1601 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1602 err = got_error_from_errno2("unlink", ondisk_path);
1605 if (rename(tmppath, ondisk_path) != 0) {
1606 err = got_error_from_errno3("rename", tmppath,
1615 if (fd != -1 && close(fd) == -1 && err == NULL)
1616 err = got_error_from_errno("close");
1617 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1618 err = got_error_from_errno2("unlink", tmppath);
1623 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1624 static const struct got_error *
1625 get_modified_file_content_status(unsigned char *status, FILE *f)
1627 const struct got_error *err = NULL;
1628 const char *markers[3] = {
1629 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1630 GOT_DIFF_CONFLICT_MARKER_SEP,
1631 GOT_DIFF_CONFLICT_MARKER_END
1635 size_t linesize = 0;
1638 while (*status == GOT_STATUS_MODIFY) {
1639 linelen = getline(&line, &linesize, f);
1640 if (linelen == -1) {
1643 err = got_ferror(f, GOT_ERR_IO);
1647 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1648 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1650 *status = GOT_STATUS_CONFLICT;
1661 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1663 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1664 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1668 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1670 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1671 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1672 ie->mtime_sec == sb->st_mtim.tv_sec &&
1673 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1674 ie->size == (sb->st_size & 0xffffffff) &&
1675 !xbit_differs(ie, sb->st_mode));
1678 static unsigned char
1679 get_staged_status(struct got_fileindex_entry *ie)
1681 switch (got_fileindex_entry_stage_get(ie)) {
1682 case GOT_FILEIDX_STAGE_ADD:
1683 return GOT_STATUS_ADD;
1684 case GOT_FILEIDX_STAGE_DELETE:
1685 return GOT_STATUS_DELETE;
1686 case GOT_FILEIDX_STAGE_MODIFY:
1687 return GOT_STATUS_MODIFY;
1689 return GOT_STATUS_NO_CHANGE;
1693 static const struct got_error *
1694 get_symlink_modification_status(unsigned char *status,
1695 struct got_fileindex_entry *ie, const char *abspath,
1696 int dirfd, const char *de_name, struct got_blob_object *blob)
1698 const struct got_error *err = NULL;
1699 char target_path[PATH_MAX];
1700 char etarget[PATH_MAX];
1702 size_t len, target_len = 0;
1703 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1704 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1706 *status = GOT_STATUS_NO_CHANGE;
1708 /* Blob object content specifies the target path of the link. */
1710 err = got_object_blob_read_block(&len, blob);
1713 if (len + target_len >= sizeof(target_path)) {
1715 * Should not happen. The blob contents were OK
1716 * when this symlink was installed.
1718 return got_error(GOT_ERR_NO_SPACE);
1721 /* Skip blob object header first time around. */
1722 memcpy(target_path + target_len, buf + hdrlen,
1724 target_len += len - hdrlen;
1728 target_path[target_len] = '\0';
1731 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1733 return got_error_from_errno2("readlinkat", abspath);
1735 elen = readlink(abspath, etarget, sizeof(etarget));
1737 return got_error_from_errno2("readlink", abspath);
1740 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1741 *status = GOT_STATUS_MODIFY;
1746 static const struct got_error *
1747 get_file_status(unsigned char *status, struct stat *sb,
1748 struct got_fileindex_entry *ie, const char *abspath,
1749 int dirfd, const char *de_name, struct got_repository *repo)
1751 const struct got_error *err = NULL;
1752 struct got_object_id id;
1757 struct got_blob_object *blob = NULL;
1759 unsigned char staged_status = get_staged_status(ie);
1761 *status = GOT_STATUS_NO_CHANGE;
1762 memset(sb, 0, sizeof(*sb));
1765 * Whenever the caller provides a directory descriptor and a
1766 * directory entry name for the file, use them! This prevents
1767 * race conditions if filesystem paths change beneath our feet.
1770 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1771 if (errno == ENOENT) {
1772 if (got_fileindex_entry_has_file_on_disk(ie))
1773 *status = GOT_STATUS_MISSING;
1775 *status = GOT_STATUS_DELETE;
1778 err = got_error_from_errno2("fstatat", abspath);
1782 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1783 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1784 return got_error_from_errno2("open", abspath);
1785 else if (fd == -1 && errno == ELOOP) {
1786 if (lstat(abspath, sb) == -1)
1787 return got_error_from_errno2("lstat", abspath);
1788 } else if (fd == -1 || fstat(fd, sb) == -1) {
1789 if (errno == ENOENT) {
1790 if (got_fileindex_entry_has_file_on_disk(ie))
1791 *status = GOT_STATUS_MISSING;
1793 *status = GOT_STATUS_DELETE;
1796 err = got_error_from_errno2("fstat", abspath);
1801 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1802 *status = GOT_STATUS_OBSTRUCTED;
1806 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1807 *status = GOT_STATUS_DELETE;
1809 } else if (!got_fileindex_entry_has_blob(ie) &&
1810 staged_status != GOT_STATUS_ADD) {
1811 *status = GOT_STATUS_ADD;
1815 if (!stat_info_differs(ie, sb))
1818 if (S_ISLNK(sb->st_mode) &&
1819 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1820 *status = GOT_STATUS_MODIFY;
1824 if (staged_status == GOT_STATUS_MODIFY ||
1825 staged_status == GOT_STATUS_ADD)
1826 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1828 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1830 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1834 if (S_ISLNK(sb->st_mode)) {
1835 err = get_symlink_modification_status(status, ie,
1836 abspath, dirfd, de_name, blob);
1841 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1843 err = got_error_from_errno2("openat", abspath);
1848 f = fdopen(fd, "r");
1850 err = got_error_from_errno2("fdopen", abspath);
1854 hdrlen = got_object_blob_get_hdrlen(blob);
1856 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1857 err = got_object_blob_read_block(&blen, blob);
1860 /* Skip length of blob object header first time around. */
1861 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1862 if (flen == 0 && ferror(f)) {
1863 err = got_error_from_errno("fread");
1866 if (blen - hdrlen == 0) {
1868 *status = GOT_STATUS_MODIFY;
1870 } else if (flen == 0) {
1871 if (blen - hdrlen != 0)
1872 *status = GOT_STATUS_MODIFY;
1874 } else if (blen - hdrlen == flen) {
1875 /* Skip blob object header first time around. */
1876 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1877 *status = GOT_STATUS_MODIFY;
1881 *status = GOT_STATUS_MODIFY;
1887 if (*status == GOT_STATUS_MODIFY) {
1889 err = get_modified_file_content_status(status, f);
1890 } else if (xbit_differs(ie, sb->st_mode))
1891 *status = GOT_STATUS_MODE_CHANGE;
1894 got_object_blob_close(blob);
1895 if (f != NULL && fclose(f) == EOF && err == NULL)
1896 err = got_error_from_errno2("fclose", abspath);
1897 if (fd != -1 && close(fd) == -1 && err == NULL)
1898 err = got_error_from_errno2("close", abspath);
1903 * Update timestamps in the file index if a file is unmodified and
1904 * we had to run a full content comparison to find out.
1906 static const struct got_error *
1907 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1908 struct got_fileindex_entry *ie, struct stat *sb)
1910 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1911 return got_fileindex_entry_update(ie, wt_fd, path,
1912 ie->blob_sha1, ie->commit_sha1, 1);
1917 static const struct got_error *
1918 update_blob(struct got_worktree *worktree,
1919 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1920 struct got_tree_entry *te, const char *path,
1921 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1924 const struct got_error *err = NULL;
1925 struct got_blob_object *blob = NULL;
1927 unsigned char status = GOT_STATUS_NO_CHANGE;
1930 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1931 return got_error_from_errno("asprintf");
1934 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1935 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1938 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1942 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1943 sb.st_mode = got_fileindex_perms_to_st(ie);
1945 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1946 status = GOT_STATUS_UNVERSIONED;
1949 if (status == GOT_STATUS_OBSTRUCTED) {
1951 got_fileindex_entry_mark_skipped(ie);
1952 err = (*progress_cb)(progress_arg, status, path);
1955 if (status == GOT_STATUS_CONFLICT) {
1957 got_fileindex_entry_mark_skipped(ie);
1958 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1963 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1964 (S_ISLNK(te->mode) ||
1965 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1967 * This is a regular file or an installed bad symlink.
1968 * If the file index indicates that this file is already
1969 * up-to-date with respect to the repository we can skip
1970 * updating contents of this file.
1972 if (got_fileindex_entry_has_commit(ie) &&
1973 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1974 SHA1_DIGEST_LENGTH) == 0) {
1976 err = sync_timestamps(worktree->root_fd,
1977 path, status, ie, &sb);
1980 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1984 if (got_fileindex_entry_has_blob(ie) &&
1985 memcmp(ie->blob_sha1, te->id.sha1,
1986 SHA1_DIGEST_LENGTH) == 0) {
1987 /* Different commit but the same blob. */
1988 err = sync_timestamps(worktree->root_fd,
1989 path, status, ie, &sb);
1992 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1998 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2002 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2003 int update_timestamps;
2004 struct got_blob_object *blob2 = NULL;
2005 char *label_orig = NULL;
2006 if (got_fileindex_entry_has_blob(ie)) {
2007 struct got_object_id id2;
2008 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2009 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2013 if (got_fileindex_entry_has_commit(ie)) {
2014 char id_str[SHA1_DIGEST_STRING_LENGTH];
2015 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2016 sizeof(id_str)) == NULL) {
2017 err = got_error_path(id_str,
2018 GOT_ERR_BAD_OBJ_ID_STR);
2021 if (asprintf(&label_orig, "%s: commit %s",
2022 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2023 err = got_error_from_errno("asprintf");
2027 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2029 err = got_object_blob_read_to_str(&link_target, blob);
2032 err = merge_symlink(worktree, blob2, ondisk_path, path,
2033 label_orig, link_target, worktree->base_commit_id,
2034 repo, progress_cb, progress_arg);
2037 err = merge_blob(&update_timestamps, worktree, blob2,
2038 ondisk_path, path, sb.st_mode, label_orig, blob,
2039 worktree->base_commit_id, repo,
2040 progress_cb, progress_arg);
2044 got_object_blob_close(blob2);
2048 * Do not update timestamps of files with local changes.
2049 * Otherwise, a future status walk would treat them as
2050 * unmodified files again.
2052 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2053 blob->id.sha1, worktree->base_commit_id->sha1,
2055 } else if (status == GOT_STATUS_MODE_CHANGE) {
2056 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2057 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2058 } else if (status == GOT_STATUS_DELETE) {
2059 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2062 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2063 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2067 int is_bad_symlink = 0;
2068 if (S_ISLNK(te->mode)) {
2069 err = install_symlink(&is_bad_symlink, worktree,
2070 ondisk_path, path, blob,
2071 status == GOT_STATUS_MISSING, 0,
2072 status == GOT_STATUS_UNVERSIONED, repo,
2073 progress_cb, progress_arg);
2075 err = install_blob(worktree, ondisk_path, path,
2076 te->mode, sb.st_mode, blob,
2077 status == GOT_STATUS_MISSING, 0, 0,
2078 status == GOT_STATUS_UNVERSIONED, repo,
2079 progress_cb, progress_arg);
2085 err = got_fileindex_entry_update(ie,
2086 worktree->root_fd, path, blob->id.sha1,
2087 worktree->base_commit_id->sha1, 1);
2089 err = create_fileindex_entry(&ie, fileindex,
2090 worktree->base_commit_id, worktree->root_fd, path,
2096 if (is_bad_symlink) {
2097 got_fileindex_entry_filetype_set(ie,
2098 GOT_FILEIDX_MODE_BAD_SYMLINK);
2101 got_object_blob_close(blob);
2107 static const struct got_error *
2108 remove_ondisk_file(const char *root_path, const char *path)
2110 const struct got_error *err = NULL;
2111 char *ondisk_path = NULL, *parent = NULL;
2113 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2114 return got_error_from_errno("asprintf");
2116 if (unlink(ondisk_path) == -1) {
2117 if (errno != ENOENT)
2118 err = got_error_from_errno2("unlink", ondisk_path);
2120 size_t root_len = strlen(root_path);
2121 err = got_path_dirname(&parent, ondisk_path);
2124 while (got_path_cmp(parent, root_path,
2125 strlen(parent), root_len) != 0) {
2127 ondisk_path = parent;
2129 if (rmdir(ondisk_path) == -1) {
2130 if (errno != ENOTEMPTY)
2131 err = got_error_from_errno2("rmdir",
2135 err = got_path_dirname(&parent, ondisk_path);
2146 static const struct got_error *
2147 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 struct got_fileindex_entry *ie, struct got_repository *repo,
2149 got_worktree_checkout_cb progress_cb, void *progress_arg)
2151 const struct got_error *err = NULL;
2152 unsigned char status;
2156 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2157 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2159 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2161 return got_error_from_errno("asprintf");
2163 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2167 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2168 char ondisk_target[PATH_MAX];
2169 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2170 sizeof(ondisk_target));
2171 if (ondisk_len == -1) {
2172 err = got_error_from_errno2("readlink", ondisk_path);
2175 ondisk_target[ondisk_len] = '\0';
2176 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2177 NULL, NULL, /* XXX pass common ancestor info? */
2178 ondisk_target, ondisk_path);
2181 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2186 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2187 status == GOT_STATUS_ADD) {
2188 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2192 * Preserve the working file and change the deleted blob's
2193 * entry into a schedule-add entry.
2195 err = got_fileindex_entry_update(ie, worktree->root_fd,
2196 ie->path, NULL, NULL, 0);
2198 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2201 if (status == GOT_STATUS_NO_CHANGE) {
2202 err = remove_ondisk_file(worktree->root_path, ie->path);
2206 got_fileindex_entry_remove(fileindex, ie);
2213 struct diff_cb_arg {
2214 struct got_fileindex *fileindex;
2215 struct got_worktree *worktree;
2216 struct got_repository *repo;
2217 got_worktree_checkout_cb progress_cb;
2219 got_cancel_cb cancel_cb;
2223 static const struct got_error *
2224 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2225 struct got_tree_entry *te, const char *parent_path)
2227 struct diff_cb_arg *a = arg;
2229 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2230 return got_error(GOT_ERR_CANCELLED);
2232 return update_blob(a->worktree, a->fileindex, ie, te,
2233 ie->path, a->repo, a->progress_cb, a->progress_arg);
2236 static const struct got_error *
2237 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2239 struct diff_cb_arg *a = arg;
2241 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 return got_error(GOT_ERR_CANCELLED);
2244 return delete_blob(a->worktree, a->fileindex, ie,
2245 a->repo, a->progress_cb, a->progress_arg);
2248 static const struct got_error *
2249 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2251 struct diff_cb_arg *a = arg;
2252 const struct got_error *err;
2255 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 return got_error(GOT_ERR_CANCELLED);
2258 if (got_object_tree_entry_is_submodule(te))
2261 if (asprintf(&path, "%s%s%s", parent_path,
2262 parent_path[0] ? "/" : "", te->name)
2264 return got_error_from_errno("asprintf");
2266 if (S_ISDIR(te->mode))
2267 err = add_dir_on_disk(a->worktree, path);
2269 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2270 a->repo, a->progress_cb, a->progress_arg);
2276 const struct got_error *
2277 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2279 uint32_t uuid_status;
2281 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2282 if (uuid_status != uuid_s_ok) {
2284 return got_error_uuid(uuid_status, "uuid_to_string");
2290 static const struct got_error *
2291 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2293 const struct got_error *err = NULL;
2294 char *uuidstr = NULL;
2298 err = got_worktree_get_uuid(&uuidstr, worktree);
2302 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2303 err = got_error_from_errno("asprintf");
2310 const struct got_error *
2311 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2313 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2316 static const struct got_error *
2317 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree,
2320 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2323 static const struct got_error *
2324 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2329 static const struct got_error *
2330 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree,
2333 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2336 static const struct got_error *
2337 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2339 return get_ref_name(refname, worktree,
2340 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2343 static const struct got_error *
2344 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2346 return get_ref_name(refname, worktree,
2347 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2350 static const struct got_error *
2351 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree,
2354 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2357 static const struct got_error *
2358 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2360 return get_ref_name(refname, worktree,
2361 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2364 static const struct got_error *
2365 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2367 return get_ref_name(refname, worktree,
2368 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2371 const struct got_error *
2372 got_worktree_get_histedit_script_path(char **path,
2373 struct got_worktree *worktree)
2375 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2376 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2378 return got_error_from_errno("asprintf");
2384 * Prevent Git's garbage collector from deleting our base commit by
2385 * setting a reference to our base commit's ID.
2387 static const struct got_error *
2388 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2390 const struct got_error *err = NULL;
2391 struct got_reference *ref = NULL;
2394 err = got_worktree_get_base_ref_name(&refname, worktree);
2398 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2402 err = got_ref_write(ref, repo);
2410 static const struct got_error *
2411 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2413 const struct got_error *err = NULL;
2415 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2416 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2417 err = got_error_from_errno("asprintf");
2418 *fileindex_path = NULL;
2424 static const struct got_error *
2425 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2426 struct got_worktree *worktree)
2428 const struct got_error *err = NULL;
2431 *fileindex_path = NULL;
2432 *fileindex = got_fileindex_alloc();
2433 if (*fileindex == NULL)
2434 return got_error_from_errno("got_fileindex_alloc");
2436 err = get_fileindex_path(fileindex_path, worktree);
2440 index = fopen(*fileindex_path, "rb");
2441 if (index == NULL) {
2442 if (errno != ENOENT)
2443 err = got_error_from_errno2("fopen", *fileindex_path);
2445 err = got_fileindex_read(*fileindex, index);
2446 if (fclose(index) == EOF && err == NULL)
2447 err = got_error_from_errno("fclose");
2451 free(*fileindex_path);
2452 *fileindex_path = NULL;
2453 got_fileindex_free(*fileindex);
2459 struct bump_base_commit_id_arg {
2460 struct got_object_id *base_commit_id;
2463 const char *entry_name;
2464 got_worktree_checkout_cb progress_cb;
2468 /* Bump base commit ID of all files within an updated part of the work tree. */
2469 static const struct got_error *
2470 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2472 const struct got_error *err;
2473 struct bump_base_commit_id_arg *a = arg;
2475 if (a->entry_name) {
2476 if (strcmp(ie->path, a->path) != 0)
2478 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2481 if (got_fileindex_entry_was_skipped(ie))
2484 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2485 SHA1_DIGEST_LENGTH) == 0)
2488 if (a->progress_cb) {
2489 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2494 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2498 static const struct got_error *
2499 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2500 struct got_fileindex *fileindex,
2501 got_worktree_checkout_cb progress_cb, void *progress_arg)
2503 struct bump_base_commit_id_arg bbc_arg;
2505 bbc_arg.base_commit_id = worktree->base_commit_id;
2506 bbc_arg.entry_name = NULL;
2508 bbc_arg.path_len = 0;
2509 bbc_arg.progress_cb = progress_cb;
2510 bbc_arg.progress_arg = progress_arg;
2512 return got_fileindex_for_each_entry_safe(fileindex,
2513 bump_base_commit_id, &bbc_arg);
2516 static const struct got_error *
2517 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2519 const struct got_error *err = NULL;
2520 char *new_fileindex_path = NULL;
2521 FILE *new_index = NULL;
2522 struct timespec timeout;
2524 err = got_opentemp_named(&new_fileindex_path, &new_index,
2529 err = got_fileindex_write(fileindex, new_index);
2533 if (rename(new_fileindex_path, fileindex_path) != 0) {
2534 err = got_error_from_errno3("rename", new_fileindex_path,
2536 unlink(new_fileindex_path);
2540 * Sleep for a short amount of time to ensure that files modified after
2541 * this program exits have a different time stamp from the one which
2542 * was recorded in the file index.
2545 timeout.tv_nsec = 1;
2546 nanosleep(&timeout, NULL);
2550 free(new_fileindex_path);
2554 static const struct got_error *
2555 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2556 struct got_object_id **tree_id, const char *wt_relpath,
2557 struct got_worktree *worktree, struct got_repository *repo)
2559 const struct got_error *err = NULL;
2560 struct got_object_id *id = NULL;
2561 char *in_repo_path = NULL;
2562 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2564 *entry_type = GOT_OBJ_TYPE_ANY;
2565 *tree_relpath = NULL;
2568 if (wt_relpath[0] == '\0') {
2569 /* Check out all files within the work tree. */
2570 *entry_type = GOT_OBJ_TYPE_TREE;
2571 *tree_relpath = strdup("");
2572 if (*tree_relpath == NULL) {
2573 err = got_error_from_errno("strdup");
2576 err = got_object_id_by_path(tree_id, repo,
2577 worktree->base_commit_id, worktree->path_prefix);
2583 /* Check out a subset of files in the work tree. */
2585 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2586 is_root_wt ? "" : "/", wt_relpath) == -1) {
2587 err = got_error_from_errno("asprintf");
2591 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2597 in_repo_path = NULL;
2599 err = got_object_get_type(entry_type, repo, id);
2603 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2604 /* Check out a single file. */
2605 if (strchr(wt_relpath, '/') == NULL) {
2606 /* Check out a single file in work tree's root dir. */
2607 in_repo_path = strdup(worktree->path_prefix);
2608 if (in_repo_path == NULL) {
2609 err = got_error_from_errno("strdup");
2612 *tree_relpath = strdup("");
2613 if (*tree_relpath == NULL) {
2614 err = got_error_from_errno("strdup");
2618 /* Check out a single file in a subdirectory. */
2619 err = got_path_dirname(tree_relpath, wt_relpath);
2622 if (asprintf(&in_repo_path, "%s%s%s",
2623 worktree->path_prefix, is_root_wt ? "" : "/",
2624 *tree_relpath) == -1) {
2625 err = got_error_from_errno("asprintf");
2629 err = got_object_id_by_path(tree_id, repo,
2630 worktree->base_commit_id, in_repo_path);
2632 /* Check out all files within a subdirectory. */
2633 *tree_id = got_object_id_dup(id);
2634 if (*tree_id == NULL) {
2635 err = got_error_from_errno("got_object_id_dup");
2638 *tree_relpath = strdup(wt_relpath);
2639 if (*tree_relpath == NULL) {
2640 err = got_error_from_errno("strdup");
2648 *entry_type = GOT_OBJ_TYPE_ANY;
2649 free(*tree_relpath);
2650 *tree_relpath = NULL;
2657 static const struct got_error *
2658 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2659 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2660 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2661 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2663 const struct got_error *err = NULL;
2664 struct got_commit_object *commit = NULL;
2665 struct got_tree_object *tree = NULL;
2666 struct got_fileindex_diff_tree_cb diff_cb;
2667 struct diff_cb_arg arg;
2669 err = ref_base_commit(worktree, repo);
2671 if (!(err->code == GOT_ERR_ERRNO &&
2672 (errno == EACCES || errno == EROFS)))
2674 err = (*progress_cb)(progress_arg,
2675 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2680 err = got_object_open_as_commit(&commit, repo,
2681 worktree->base_commit_id);
2685 err = got_object_open_as_tree(&tree, repo, tree_id);
2690 got_object_tree_find_entry(tree, entry_name) == NULL) {
2691 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2695 diff_cb.diff_old_new = diff_old_new;
2696 diff_cb.diff_old = diff_old;
2697 diff_cb.diff_new = diff_new;
2698 arg.fileindex = fileindex;
2699 arg.worktree = worktree;
2701 arg.progress_cb = progress_cb;
2702 arg.progress_arg = progress_arg;
2703 arg.cancel_cb = cancel_cb;
2704 arg.cancel_arg = cancel_arg;
2705 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2706 entry_name, repo, &diff_cb, &arg);
2709 got_object_tree_close(tree);
2711 got_object_commit_close(commit);
2715 const struct got_error *
2716 got_worktree_checkout_files(struct got_worktree *worktree,
2717 struct got_pathlist_head *paths, struct got_repository *repo,
2718 got_worktree_checkout_cb progress_cb, void *progress_arg,
2719 got_cancel_cb cancel_cb, void *cancel_arg)
2721 const struct got_error *err = NULL, *sync_err, *unlockerr;
2722 struct got_commit_object *commit = NULL;
2723 struct got_tree_object *tree = NULL;
2724 struct got_fileindex *fileindex = NULL;
2725 char *fileindex_path = NULL;
2726 struct got_pathlist_entry *pe;
2727 struct tree_path_data {
2728 STAILQ_ENTRY(tree_path_data) entry;
2729 struct got_object_id *tree_id;
2734 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2736 STAILQ_INIT(&tree_paths);
2738 err = lock_worktree(worktree, LOCK_EX);
2742 /* Map all specified paths to in-repository trees. */
2743 TAILQ_FOREACH(pe, paths, entry) {
2744 tpd = malloc(sizeof(*tpd));
2746 err = got_error_from_errno("malloc");
2750 err = find_tree_entry_for_checkout(&tpd->entry_type,
2751 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2757 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2758 err = got_path_basename(&tpd->entry_name, pe->path);
2766 tpd->entry_name = NULL;
2768 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2772 * Read the file index.
2773 * Checking out files is supposed to be an idempotent operation.
2774 * If the on-disk file index is incomplete we will try to complete it.
2776 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2780 tpd = STAILQ_FIRST(&tree_paths);
2781 TAILQ_FOREACH(pe, paths, entry) {
2782 struct bump_base_commit_id_arg bbc_arg;
2784 err = checkout_files(worktree, fileindex, tpd->relpath,
2785 tpd->tree_id, tpd->entry_name, repo,
2786 progress_cb, progress_arg, cancel_cb, cancel_arg);
2790 bbc_arg.base_commit_id = worktree->base_commit_id;
2791 bbc_arg.entry_name = tpd->entry_name;
2792 bbc_arg.path = pe->path;
2793 bbc_arg.path_len = pe->path_len;
2794 bbc_arg.progress_cb = progress_cb;
2795 bbc_arg.progress_arg = progress_arg;
2796 err = got_fileindex_for_each_entry_safe(fileindex,
2797 bump_base_commit_id, &bbc_arg);
2801 tpd = STAILQ_NEXT(tpd, entry);
2803 sync_err = sync_fileindex(fileindex, fileindex_path);
2804 if (sync_err && err == NULL)
2807 free(fileindex_path);
2809 got_object_tree_close(tree);
2811 got_object_commit_close(commit);
2813 got_fileindex_free(fileindex);
2814 while (!STAILQ_EMPTY(&tree_paths)) {
2815 tpd = STAILQ_FIRST(&tree_paths);
2816 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2821 unlockerr = lock_worktree(worktree, LOCK_SH);
2822 if (unlockerr && err == NULL)
2827 struct merge_file_cb_arg {
2828 struct got_worktree *worktree;
2829 struct got_fileindex *fileindex;
2830 got_worktree_checkout_cb progress_cb;
2832 got_cancel_cb cancel_cb;
2834 const char *label_orig;
2835 struct got_object_id *commit_id2;
2838 static const struct got_error *
2839 merge_file_cb(void *arg, struct got_blob_object *blob1,
2840 struct got_blob_object *blob2, struct got_object_id *id1,
2841 struct got_object_id *id2, const char *path1, const char *path2,
2842 mode_t mode1, mode_t mode2, struct got_repository *repo)
2844 static const struct got_error *err = NULL;
2845 struct merge_file_cb_arg *a = arg;
2846 struct got_fileindex_entry *ie;
2847 char *ondisk_path = NULL;
2849 unsigned char status;
2850 int local_changes_subsumed;
2851 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2852 char *id_str = NULL, *label_deriv2 = NULL;
2854 if (blob1 && blob2) {
2855 ie = got_fileindex_entry_get(a->fileindex, path2,
2858 return (*a->progress_cb)(a->progress_arg,
2859 GOT_STATUS_MISSING, path2);
2861 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2863 return got_error_from_errno("asprintf");
2865 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2870 if (status == GOT_STATUS_DELETE) {
2871 err = (*a->progress_cb)(a->progress_arg,
2872 GOT_STATUS_MERGE, path2);
2875 if (status != GOT_STATUS_NO_CHANGE &&
2876 status != GOT_STATUS_MODIFY &&
2877 status != GOT_STATUS_CONFLICT &&
2878 status != GOT_STATUS_ADD) {
2879 err = (*a->progress_cb)(a->progress_arg, status, path2);
2883 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2885 err = got_object_blob_read_to_str(&link_target2, blob2);
2888 err = merge_symlink(a->worktree, blob1, ondisk_path,
2889 path2, a->label_orig, link_target2, a->commit_id2,
2890 repo, a->progress_cb, a->progress_arg);
2895 f_orig = got_opentemp();
2896 if (f_orig == NULL) {
2897 err = got_error_from_errno("got_opentemp");
2900 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2905 f_deriv2 = got_opentemp();
2906 if (f_deriv2 == NULL)
2908 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2913 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2915 err = got_error_from_errno2("open",
2919 f_deriv = fdopen(fd, "r");
2920 if (f_deriv == NULL) {
2921 err = got_error_from_errno2("fdopen",
2926 err = got_object_id_str(&id_str, a->commit_id2);
2929 if (asprintf(&label_deriv2, "%s: commit %s",
2930 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2931 err = got_error_from_errno("asprintf");
2934 err = merge_file(&local_changes_subsumed, a->worktree,
2935 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2936 sb.st_mode, a->label_orig, NULL, label_deriv2,
2937 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2938 a->progress_cb, a->progress_arg);
2941 ie = got_fileindex_entry_get(a->fileindex, path1,
2944 return (*a->progress_cb)(a->progress_arg,
2945 GOT_STATUS_MISSING, path1);
2947 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2949 return got_error_from_errno("asprintf");
2951 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2957 case GOT_STATUS_NO_CHANGE:
2958 err = (*a->progress_cb)(a->progress_arg,
2959 GOT_STATUS_DELETE, path1);
2962 err = remove_ondisk_file(a->worktree->root_path, path1);
2966 got_fileindex_entry_mark_deleted_from_disk(ie);
2968 case GOT_STATUS_DELETE:
2969 case GOT_STATUS_MISSING:
2970 err = (*a->progress_cb)(a->progress_arg,
2971 GOT_STATUS_DELETE, path1);
2975 got_fileindex_entry_mark_deleted_from_disk(ie);
2977 case GOT_STATUS_ADD: {
2978 struct got_object_id *id;
2981 * Delete the added file only if its content already
2982 * exists in the repository.
2984 err = got_object_blob_file_create(&id, &blob1_f, path1);
2987 if (got_object_id_cmp(id, id1) == 0) {
2988 err = (*a->progress_cb)(a->progress_arg,
2989 GOT_STATUS_DELETE, path1);
2992 err = remove_ondisk_file(a->worktree->root_path,
2997 got_fileindex_entry_remove(a->fileindex,
3000 err = (*a->progress_cb)(a->progress_arg,
3001 GOT_STATUS_CANNOT_DELETE, path1);
3003 if (fclose(blob1_f) == EOF && err == NULL)
3004 err = got_error_from_errno("fclose");
3010 case GOT_STATUS_MODIFY:
3011 case GOT_STATUS_CONFLICT:
3012 err = (*a->progress_cb)(a->progress_arg,
3013 GOT_STATUS_CANNOT_DELETE, path1);
3017 case GOT_STATUS_OBSTRUCTED:
3018 err = (*a->progress_cb)(a->progress_arg, status, path1);
3026 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3028 return got_error_from_errno("asprintf");
3029 ie = got_fileindex_entry_get(a->fileindex, path2,
3032 err = get_file_status(&status, &sb, ie, ondisk_path,
3036 if (status != GOT_STATUS_NO_CHANGE &&
3037 status != GOT_STATUS_MODIFY &&
3038 status != GOT_STATUS_CONFLICT &&
3039 status != GOT_STATUS_ADD) {
3040 err = (*a->progress_cb)(a->progress_arg,
3044 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3046 err = got_object_blob_read_to_str(&link_target2,
3050 err = merge_symlink(a->worktree, NULL,
3051 ondisk_path, path2, a->label_orig,
3052 link_target2, a->commit_id2, repo,
3053 a->progress_cb, a->progress_arg);
3055 } else if (S_ISREG(sb.st_mode)) {
3056 err = merge_blob(&local_changes_subsumed,
3057 a->worktree, NULL, ondisk_path, path2,
3058 sb.st_mode, a->label_orig, blob2,
3059 a->commit_id2, repo, a->progress_cb,
3062 err = got_error_path(ondisk_path,
3063 GOT_ERR_FILE_OBSTRUCTED);
3067 if (status == GOT_STATUS_DELETE) {
3068 err = got_fileindex_entry_update(ie,
3069 a->worktree->root_fd, path2, blob2->id.sha1,
3070 a->worktree->base_commit_id->sha1, 0);
3075 int is_bad_symlink = 0;
3076 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3077 if (S_ISLNK(mode2)) {
3078 err = install_symlink(&is_bad_symlink,
3079 a->worktree, ondisk_path, path2, blob2, 0,
3080 0, 1, repo, a->progress_cb, a->progress_arg);
3082 err = install_blob(a->worktree, ondisk_path, path2,
3083 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3084 a->progress_cb, a->progress_arg);
3088 err = got_fileindex_entry_alloc(&ie, path2);
3091 err = got_fileindex_entry_update(ie,
3092 a->worktree->root_fd, path2, NULL, NULL, 1);
3094 got_fileindex_entry_free(ie);
3097 err = got_fileindex_entry_add(a->fileindex, ie);
3099 got_fileindex_entry_free(ie);
3102 if (is_bad_symlink) {
3103 got_fileindex_entry_filetype_set(ie,
3104 GOT_FILEIDX_MODE_BAD_SYMLINK);
3109 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3110 err = got_error_from_errno("fclose");
3111 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3112 err = got_error_from_errno("fclose");
3113 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3114 err = got_error_from_errno("fclose");
3121 struct check_merge_ok_arg {
3122 struct got_worktree *worktree;
3123 struct got_repository *repo;
3126 static const struct got_error *
3127 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3129 const struct got_error *err = NULL;
3130 struct check_merge_ok_arg *a = arg;
3131 unsigned char status;
3135 /* Reject merges into a work tree with mixed base commits. */
3136 if (got_fileindex_entry_has_commit(ie) &&
3137 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3138 SHA1_DIGEST_LENGTH))
3139 return got_error(GOT_ERR_MIXED_COMMITS);
3141 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3143 return got_error_from_errno("asprintf");
3145 /* Reject merges into a work tree with conflicted files. */
3146 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3149 if (status == GOT_STATUS_CONFLICT)
3150 return got_error(GOT_ERR_CONFLICTS);
3155 static const struct got_error *
3156 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3157 const char *fileindex_path, struct got_object_id *commit_id1,
3158 struct got_object_id *commit_id2, struct got_repository *repo,
3159 got_worktree_checkout_cb progress_cb, void *progress_arg,
3160 got_cancel_cb cancel_cb, void *cancel_arg)
3162 const struct got_error *err = NULL, *sync_err;
3163 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3164 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3165 struct merge_file_cb_arg arg;
3166 char *label_orig = NULL;
3169 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3170 worktree->path_prefix);
3171 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3177 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3181 err = got_object_id_str(&id_str, commit_id1);
3185 if (asprintf(&label_orig, "%s: commit %s",
3186 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3187 err = got_error_from_errno("asprintf");
3194 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3195 worktree->path_prefix);
3199 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3203 arg.worktree = worktree;
3204 arg.fileindex = fileindex;
3205 arg.progress_cb = progress_cb;
3206 arg.progress_arg = progress_arg;
3207 arg.cancel_cb = cancel_cb;
3208 arg.cancel_arg = cancel_arg;
3209 arg.label_orig = label_orig;
3210 arg.commit_id2 = commit_id2;
3211 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3212 sync_err = sync_fileindex(fileindex, fileindex_path);
3213 if (sync_err && err == NULL)
3217 got_object_tree_close(tree1);
3219 got_object_tree_close(tree2);
3224 const struct got_error *
3225 got_worktree_merge_files(struct got_worktree *worktree,
3226 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3227 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3228 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3230 const struct got_error *err, *unlockerr;
3231 char *fileindex_path = NULL;
3232 struct got_fileindex *fileindex = NULL;
3233 struct check_merge_ok_arg mok_arg;
3235 err = lock_worktree(worktree, LOCK_EX);
3239 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3243 mok_arg.worktree = worktree;
3244 mok_arg.repo = repo;
3245 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3250 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3251 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3254 got_fileindex_free(fileindex);
3255 free(fileindex_path);
3256 unlockerr = lock_worktree(worktree, LOCK_SH);
3257 if (unlockerr && err == NULL)
3262 struct diff_dir_cb_arg {
3263 struct got_fileindex *fileindex;
3264 struct got_worktree *worktree;
3265 const char *status_path;
3266 size_t status_path_len;
3267 struct got_repository *repo;
3268 got_worktree_status_cb status_cb;
3270 got_cancel_cb cancel_cb;
3272 /* A pathlist containing per-directory pathlists of ignore patterns. */
3273 struct got_pathlist_head *ignores;
3274 int report_unchanged;
3278 static const struct got_error *
3279 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3280 int dirfd, const char *de_name,
3281 got_worktree_status_cb status_cb, void *status_arg,
3282 struct got_repository *repo, int report_unchanged)
3284 const struct got_error *err = NULL;
3285 unsigned char status = GOT_STATUS_NO_CHANGE;
3286 unsigned char staged_status = get_staged_status(ie);
3288 struct got_object_id blob_id, commit_id, staged_blob_id;
3289 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3290 struct got_object_id *staged_blob_idp = NULL;
3292 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3296 if (status == GOT_STATUS_NO_CHANGE &&
3297 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3300 if (got_fileindex_entry_has_blob(ie)) {
3301 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3302 blob_idp = &blob_id;
3304 if (got_fileindex_entry_has_commit(ie)) {
3305 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3306 commit_idp = &commit_id;
3308 if (staged_status == GOT_STATUS_ADD ||
3309 staged_status == GOT_STATUS_MODIFY) {
3310 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3311 SHA1_DIGEST_LENGTH);
3312 staged_blob_idp = &staged_blob_id;
3315 return (*status_cb)(status_arg, status, staged_status,
3316 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3319 static const struct got_error *
3320 status_old_new(void *arg, struct got_fileindex_entry *ie,
3321 struct dirent *de, const char *parent_path, int dirfd)
3323 const struct got_error *err = NULL;
3324 struct diff_dir_cb_arg *a = arg;
3327 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3328 return got_error(GOT_ERR_CANCELLED);
3330 if (got_path_cmp(parent_path, a->status_path,
3331 strlen(parent_path), a->status_path_len) != 0 &&
3332 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3335 if (parent_path[0]) {
3336 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3337 parent_path, de->d_name) == -1)
3338 return got_error_from_errno("asprintf");
3340 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3342 return got_error_from_errno("asprintf");
3345 err = report_file_status(ie, abspath, dirfd, de->d_name,
3346 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3351 static const struct got_error *
3352 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3354 struct diff_dir_cb_arg *a = arg;
3355 struct got_object_id blob_id, commit_id;
3356 unsigned char status;
3358 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3359 return got_error(GOT_ERR_CANCELLED);
3361 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3364 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3365 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3366 if (got_fileindex_entry_has_file_on_disk(ie))
3367 status = GOT_STATUS_MISSING;
3369 status = GOT_STATUS_DELETE;
3370 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3371 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3375 free_ignorelist(struct got_pathlist_head *ignorelist)
3377 struct got_pathlist_entry *pe;
3379 TAILQ_FOREACH(pe, ignorelist, entry)
3380 free((char *)pe->path);
3381 got_pathlist_free(ignorelist);
3385 free_ignores(struct got_pathlist_head *ignores)
3387 struct got_pathlist_entry *pe;
3389 TAILQ_FOREACH(pe, ignores, entry) {
3390 struct got_pathlist_head *ignorelist = pe->data;
3391 free_ignorelist(ignorelist);
3392 free((char *)pe->path);
3394 got_pathlist_free(ignores);
3397 static const struct got_error *
3398 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3400 const struct got_error *err = NULL;
3401 struct got_pathlist_entry *pe = NULL;
3402 struct got_pathlist_head *ignorelist;
3403 char *line = NULL, *pattern, *dirpath = NULL;
3404 size_t linesize = 0;
3407 ignorelist = calloc(1, sizeof(*ignorelist));
3408 if (ignorelist == NULL)
3409 return got_error_from_errno("calloc");
3410 TAILQ_INIT(ignorelist);
3412 while ((linelen = getline(&line, &linesize, f)) != -1) {
3413 if (linelen > 0 && line[linelen - 1] == '\n')
3414 line[linelen - 1] = '\0';
3416 /* Git's ignores may contain comments. */
3420 /* Git's negated patterns are not (yet?) supported. */
3424 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3426 err = got_error_from_errno("asprintf");
3429 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3434 err = got_error_from_errno("getline");
3438 dirpath = strdup(path);
3439 if (dirpath == NULL) {
3440 err = got_error_from_errno("strdup");
3443 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3446 if (err || pe == NULL) {
3448 free_ignorelist(ignorelist);
3454 match_ignores(struct got_pathlist_head *ignores, const char *path)
3456 struct got_pathlist_entry *pe;
3458 /* Handle patterns which match in all directories. */
3459 TAILQ_FOREACH(pe, ignores, entry) {
3460 struct got_pathlist_head *ignorelist = pe->data;
3461 struct got_pathlist_entry *pi;
3463 TAILQ_FOREACH(pi, ignorelist, entry) {
3464 const char *p, *pattern = pi->path;
3466 if (strncmp(pattern, "**/", 3) != 0)
3471 if (fnmatch(pattern, p,
3472 FNM_PATHNAME | FNM_LEADING_DIR)) {
3473 /* Retry in next directory. */
3474 while (*p && *p != '/')
3486 * The ignores pathlist contains ignore lists from children before
3487 * parents, so we can find the most specific ignorelist by walking
3488 * ignores backwards.
3490 pe = TAILQ_LAST(ignores, got_pathlist_head);
3492 if (got_path_is_child(path, pe->path, pe->path_len)) {
3493 struct got_pathlist_head *ignorelist = pe->data;
3494 struct got_pathlist_entry *pi;
3495 TAILQ_FOREACH(pi, ignorelist, entry) {
3496 const char *pattern = pi->path;
3497 int flags = FNM_LEADING_DIR;
3498 if (strstr(pattern, "/**/") == NULL)
3499 flags |= FNM_PATHNAME;
3500 if (fnmatch(pattern, path, flags))
3505 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3511 static const struct got_error *
3512 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3513 const char *path, int dirfd, const char *ignores_filename)
3515 const struct got_error *err = NULL;
3518 FILE *ignoresfile = NULL;
3520 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3521 path[0] ? "/" : "", ignores_filename) == -1)
3522 return got_error_from_errno("asprintf");
3525 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3527 if (errno != ENOENT && errno != EACCES)
3528 err = got_error_from_errno2("openat",
3531 ignoresfile = fdopen(fd, "r");
3532 if (ignoresfile == NULL)
3533 err = got_error_from_errno2("fdopen",
3537 err = read_ignores(ignores, path, ignoresfile);
3541 ignoresfile = fopen(ignorespath, "r");
3542 if (ignoresfile == NULL) {
3543 if (errno != ENOENT && errno != EACCES)
3544 err = got_error_from_errno2("fopen",
3547 err = read_ignores(ignores, path, ignoresfile);
3550 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3551 err = got_error_from_errno2("fclose", path);
3552 if (fd != -1 && close(fd) == -1 && err == NULL)
3553 err = got_error_from_errno2("close", path);
3558 static const struct got_error *
3559 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3561 const struct got_error *err = NULL;
3562 struct diff_dir_cb_arg *a = arg;
3565 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3566 return got_error(GOT_ERR_CANCELLED);
3568 if (parent_path[0]) {
3569 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3570 return got_error_from_errno("asprintf");
3575 if (de->d_type != DT_DIR &&
3576 got_path_is_child(path, a->status_path, a->status_path_len)
3577 && !match_ignores(a->ignores, path))
3578 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3579 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3585 static const struct got_error *
3586 status_traverse(void *arg, const char *path, int dirfd)
3588 const struct got_error *err = NULL;
3589 struct diff_dir_cb_arg *a = arg;
3594 err = add_ignores(a->ignores, a->worktree->root_path,
3595 path, dirfd, ".cvsignore");
3599 err = add_ignores(a->ignores, a->worktree->root_path, path,
3600 dirfd, ".gitignore");
3605 static const struct got_error *
3606 report_single_file_status(const char *path, const char *ondisk_path,
3607 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3608 void *status_arg, struct got_repository *repo, int report_unchanged,
3609 struct got_pathlist_head *ignores, int no_ignores)
3611 struct got_fileindex_entry *ie;
3614 if (!no_ignores && match_ignores(ignores, path))
3617 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3619 return report_file_status(ie, ondisk_path, -1, NULL,
3620 status_cb, status_arg, repo, report_unchanged);
3622 if (lstat(ondisk_path, &sb) == -1) {
3623 if (errno != ENOENT)
3624 return got_error_from_errno2("lstat", ondisk_path);
3625 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3626 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3630 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3631 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3632 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3637 static const struct got_error *
3638 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3639 const char *root_path, const char *path)
3641 const struct got_error *err;
3642 char *parent_path, *next_parent_path = NULL;
3644 err = add_ignores(ignores, root_path, "", -1,
3649 err = add_ignores(ignores, root_path, "", -1,
3654 err = got_path_dirname(&parent_path, path);
3656 if (err->code == GOT_ERR_BAD_PATH)
3657 return NULL; /* cannot traverse parent */
3661 err = add_ignores(ignores, root_path, parent_path, -1,
3665 err = add_ignores(ignores, root_path, parent_path, -1,
3669 err = got_path_dirname(&next_parent_path, parent_path);
3671 if (err->code == GOT_ERR_BAD_PATH)
3672 err = NULL; /* traversed everything */
3675 if (got_path_is_root_dir(parent_path))
3678 parent_path = next_parent_path;
3679 next_parent_path = NULL;
3683 free(next_parent_path);
3687 static const struct got_error *
3688 worktree_status(struct got_worktree *worktree, const char *path,
3689 struct got_fileindex *fileindex, struct got_repository *repo,
3690 got_worktree_status_cb status_cb, void *status_arg,
3691 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3692 int report_unchanged)
3694 const struct got_error *err = NULL;
3696 struct got_fileindex_diff_dir_cb fdiff_cb;
3697 struct diff_dir_cb_arg arg;
3698 char *ondisk_path = NULL;
3699 struct got_pathlist_head ignores;
3701 TAILQ_INIT(&ignores);
3703 if (asprintf(&ondisk_path, "%s%s%s",
3704 worktree->root_path, path[0] ? "/" : "", path) == -1)
3705 return got_error_from_errno("asprintf");
3707 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3709 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3711 err = got_error_from_errno2("open", ondisk_path);
3714 err = add_ignores_from_parent_paths(&ignores,
3715 worktree->root_path, ondisk_path);
3719 err = report_single_file_status(path, ondisk_path,
3720 fileindex, status_cb, status_arg, repo,
3721 report_unchanged, &ignores, no_ignores);
3724 fdiff_cb.diff_old_new = status_old_new;
3725 fdiff_cb.diff_old = status_old;
3726 fdiff_cb.diff_new = status_new;
3727 fdiff_cb.diff_traverse = status_traverse;
3728 arg.fileindex = fileindex;
3729 arg.worktree = worktree;
3730 arg.status_path = path;
3731 arg.status_path_len = strlen(path);
3733 arg.status_cb = status_cb;
3734 arg.status_arg = status_arg;
3735 arg.cancel_cb = cancel_cb;
3736 arg.cancel_arg = cancel_arg;
3737 arg.report_unchanged = report_unchanged;
3738 arg.no_ignores = no_ignores;
3740 err = add_ignores_from_parent_paths(&ignores,
3741 worktree->root_path, path);
3745 arg.ignores = &ignores;
3746 err = got_fileindex_diff_dir(fileindex, fd,
3747 worktree->root_path, path, repo, &fdiff_cb, &arg);
3750 free_ignores(&ignores);
3751 if (fd != -1 && close(fd) == -1 && err == NULL)
3752 err = got_error_from_errno("close");
3757 const struct got_error *
3758 got_worktree_status(struct got_worktree *worktree,
3759 struct got_pathlist_head *paths, struct got_repository *repo,
3760 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3761 got_cancel_cb cancel_cb, void *cancel_arg)
3763 const struct got_error *err = NULL;
3764 char *fileindex_path = NULL;
3765 struct got_fileindex *fileindex = NULL;
3766 struct got_pathlist_entry *pe;
3768 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3772 TAILQ_FOREACH(pe, paths, entry) {
3773 err = worktree_status(worktree, pe->path, fileindex, repo,
3774 status_cb, status_arg, cancel_cb, cancel_arg,
3779 free(fileindex_path);
3780 got_fileindex_free(fileindex);
3784 const struct got_error *
3785 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3788 const struct got_error *err = NULL;
3789 char *resolved = NULL, *cwd = NULL, *path = NULL;
3792 char *abspath = NULL;
3793 char canonpath[PATH_MAX];
3797 cwd = getcwd(NULL, 0);
3799 return got_error_from_errno("getcwd");
3801 if (lstat(arg, &sb) == -1) {
3802 if (errno != ENOENT) {
3803 err = got_error_from_errno2("lstat", arg);
3808 if (S_ISLNK(sb.st_mode)) {
3810 * We cannot use realpath(3) with symlinks since we want to
3811 * operate on the symlink itself.
3812 * But we can make the path absolute, assuming it is relative
3813 * to the current working directory, and then canonicalize it.
3815 if (!got_path_is_absolute(arg)) {
3816 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3817 err = got_error_from_errno("asprintf");
3822 err = got_canonpath(abspath ? abspath : arg, canonpath,
3826 resolved = strdup(canonpath);
3827 if (resolved == NULL) {
3828 err = got_error_from_errno("strdup");
3832 resolved = realpath(arg, NULL);
3833 if (resolved == NULL) {
3834 if (errno != ENOENT) {
3835 err = got_error_from_errno2("realpath", arg);
3838 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3839 err = got_error_from_errno("asprintf");
3842 err = got_canonpath(abspath, canonpath,
3846 resolved = strdup(canonpath);
3847 if (resolved == NULL) {
3848 err = got_error_from_errno("strdup");
3854 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3855 strlen(got_worktree_get_root_path(worktree)))) {
3856 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3860 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3861 err = got_path_skip_common_ancestor(&path,
3862 got_worktree_get_root_path(worktree), resolved);
3868 err = got_error_from_errno("strdup");
3873 /* XXX status walk can't deal with trailing slash! */
3875 while (len > 0 && path[len - 1] == '/') {
3876 path[len - 1] = '\0';
3890 struct schedule_addition_args {
3891 struct got_worktree *worktree;
3892 struct got_fileindex *fileindex;
3893 got_worktree_checkout_cb progress_cb;
3895 struct got_repository *repo;
3898 static const struct got_error *
3899 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3900 const char *relpath, struct got_object_id *blob_id,
3901 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3902 int dirfd, const char *de_name)
3904 struct schedule_addition_args *a = arg;
3905 const struct got_error *err = NULL;
3906 struct got_fileindex_entry *ie;
3910 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3912 return got_error_from_errno("asprintf");
3914 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3916 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3920 /* Re-adding an existing entry is a no-op. */
3921 if (status == GOT_STATUS_ADD)
3923 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3928 if (status != GOT_STATUS_UNVERSIONED) {
3929 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3933 err = got_fileindex_entry_alloc(&ie, relpath);
3936 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3937 relpath, NULL, NULL, 1);
3939 got_fileindex_entry_free(ie);
3942 err = got_fileindex_entry_add(a->fileindex, ie);
3944 got_fileindex_entry_free(ie);
3951 if (status == GOT_STATUS_ADD)
3953 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3956 const struct got_error *
3957 got_worktree_schedule_add(struct got_worktree *worktree,
3958 struct got_pathlist_head *paths,
3959 got_worktree_checkout_cb progress_cb, void *progress_arg,
3960 struct got_repository *repo, int no_ignores)
3962 struct got_fileindex *fileindex = NULL;
3963 char *fileindex_path = NULL;
3964 const struct got_error *err = NULL, *sync_err, *unlockerr;
3965 struct got_pathlist_entry *pe;
3966 struct schedule_addition_args saa;
3968 err = lock_worktree(worktree, LOCK_EX);
3972 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3976 saa.worktree = worktree;
3977 saa.fileindex = fileindex;
3978 saa.progress_cb = progress_cb;
3979 saa.progress_arg = progress_arg;
3982 TAILQ_FOREACH(pe, paths, entry) {
3983 err = worktree_status(worktree, pe->path, fileindex, repo,
3984 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3988 sync_err = sync_fileindex(fileindex, fileindex_path);
3989 if (sync_err && err == NULL)
3992 free(fileindex_path);
3994 got_fileindex_free(fileindex);
3995 unlockerr = lock_worktree(worktree, LOCK_SH);
3996 if (unlockerr && err == NULL)
4001 struct schedule_deletion_args {
4002 struct got_worktree *worktree;
4003 struct got_fileindex *fileindex;
4004 got_worktree_delete_cb progress_cb;
4006 struct got_repository *repo;
4007 int delete_local_mods;
4009 const char *status_codes;
4012 static const struct got_error *
4013 schedule_for_deletion(void *arg, unsigned char status,
4014 unsigned char staged_status, const char *relpath,
4015 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4016 struct got_object_id *commit_id, int dirfd, const char *de_name)
4018 struct schedule_deletion_args *a = arg;
4019 const struct got_error *err = NULL;
4020 struct got_fileindex_entry *ie = NULL;
4024 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4026 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4028 staged_status = get_staged_status(ie);
4029 if (staged_status != GOT_STATUS_NO_CHANGE) {
4030 if (staged_status == GOT_STATUS_DELETE)
4032 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4035 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4037 return got_error_from_errno("asprintf");
4039 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4044 if (a->status_codes) {
4045 size_t ncodes = strlen(a->status_codes);
4047 for (i = 0; i < ncodes ; i++) {
4048 if (status == a->status_codes[i])
4052 /* Do not delete files in non-matching status. */
4056 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4057 a->status_codes[i] != GOT_STATUS_MISSING) {
4058 static char msg[64];
4059 snprintf(msg, sizeof(msg),
4060 "invalid status code '%c'", a->status_codes[i]);
4061 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4066 if (status != GOT_STATUS_NO_CHANGE) {
4067 if (status == GOT_STATUS_DELETE)
4069 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4070 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4073 if (status != GOT_STATUS_MODIFY &&
4074 status != GOT_STATUS_MISSING) {
4075 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4080 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4084 if (unlinkat(dirfd, de_name, 0) != 0) {
4085 err = got_error_from_errno2("unlinkat",
4089 } else if (unlink(ondisk_path) != 0) {
4090 err = got_error_from_errno2("unlink", ondisk_path);
4094 root_len = strlen(a->worktree->root_path);
4097 err = got_path_dirname(&parent, ondisk_path);
4101 ondisk_path = parent;
4102 if (rmdir(ondisk_path) == -1) {
4103 if (errno != ENOTEMPTY)
4104 err = got_error_from_errno2("rmdir",
4108 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4109 strlen(ondisk_path), root_len) != 0);
4112 got_fileindex_entry_mark_deleted_from_disk(ie);
4117 if (status == GOT_STATUS_DELETE)
4119 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4120 staged_status, relpath);
4123 const struct got_error *
4124 got_worktree_schedule_delete(struct got_worktree *worktree,
4125 struct got_pathlist_head *paths, int delete_local_mods,
4126 const char *status_codes,
4127 got_worktree_delete_cb progress_cb, void *progress_arg,
4128 struct got_repository *repo, int keep_on_disk)
4130 struct got_fileindex *fileindex = NULL;
4131 char *fileindex_path = NULL;
4132 const struct got_error *err = NULL, *sync_err, *unlockerr;
4133 struct got_pathlist_entry *pe;
4134 struct schedule_deletion_args sda;
4136 err = lock_worktree(worktree, LOCK_EX);
4140 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4144 sda.worktree = worktree;
4145 sda.fileindex = fileindex;
4146 sda.progress_cb = progress_cb;
4147 sda.progress_arg = progress_arg;
4149 sda.delete_local_mods = delete_local_mods;
4150 sda.keep_on_disk = keep_on_disk;
4151 sda.status_codes = status_codes;
4153 TAILQ_FOREACH(pe, paths, entry) {
4154 err = worktree_status(worktree, pe->path, fileindex, repo,
4155 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4159 sync_err = sync_fileindex(fileindex, fileindex_path);
4160 if (sync_err && err == NULL)
4163 free(fileindex_path);
4165 got_fileindex_free(fileindex);
4166 unlockerr = lock_worktree(worktree, LOCK_SH);
4167 if (unlockerr && err == NULL)
4172 static const struct got_error *
4173 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4175 const struct got_error *err = NULL;
4177 size_t linesize = 0, n;
4180 linelen = getline(&line, &linesize, infile);
4181 if (linelen == -1) {
4182 if (ferror(infile)) {
4183 err = got_error_from_errno("getline");
4189 n = fwrite(line, 1, linelen, outfile);
4191 err = got_ferror(outfile, GOT_ERR_IO);
4196 n = fwrite(line, 1, linelen, rejectfile);
4198 err = got_ferror(outfile, GOT_ERR_IO);
4205 static const struct got_error *
4206 skip_one_line(FILE *f)
4209 size_t linesize = 0;
4212 linelen = getline(&line, &linesize, f);
4213 if (linelen == -1) {
4215 return got_error_from_errno("getline");
4222 static const struct got_error *
4223 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4224 int start_old, int end_old, int start_new, int end_new,
4225 FILE *outfile, FILE *rejectfile)
4227 const struct got_error *err;
4229 /* Copy old file's lines leading up to patch. */
4230 while (!feof(f1) && *line_cur1 < start_old) {
4231 err = copy_one_line(f1, outfile, NULL);
4236 /* Skip new file's lines leading up to patch. */
4237 while (!feof(f2) && *line_cur2 < start_new) {
4239 err = copy_one_line(f2, NULL, rejectfile);
4241 err = skip_one_line(f2);
4246 /* Copy patched lines. */
4247 while (!feof(f2) && *line_cur2 <= end_new) {
4248 err = copy_one_line(f2, outfile, NULL);
4253 /* Skip over old file's replaced lines. */
4254 while (!feof(f1) && *line_cur1 <= end_old) {
4256 err = copy_one_line(f1, NULL, rejectfile);
4258 err = skip_one_line(f1);
4267 static const struct got_error *
4268 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4269 FILE *outfile, FILE *rejectfile)
4271 const struct got_error *err;
4274 /* Copy old file's lines until EOF. */
4276 err = copy_one_line(f1, outfile, NULL);
4283 /* Copy new file's lines until EOF. */
4285 err = copy_one_line(f2, NULL, rejectfile);
4295 static const struct got_error *
4296 apply_or_reject_change(int *choice, int *nchunks_used,
4297 struct diff_result *diff_result, int n,
4298 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4299 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4300 got_worktree_patch_cb patch_cb, void *patch_arg)
4302 const struct got_error *err = NULL;
4303 struct diff_chunk_context cc = {};
4304 int start_old, end_old, start_new, end_new;
4306 struct diff_output_unidiff_state *diff_state;
4307 struct diff_input_info diff_info;
4310 *choice = GOT_PATCH_CHOICE_NONE;
4312 /* Get changed line numbers without context lines for copy_change(). */
4313 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4314 start_old = cc.left.start;
4315 end_old = cc.left.end;
4316 start_new = cc.right.start;
4317 end_new = cc.right.end;
4319 /* Get the same change with context lines for display. */
4320 memset(&cc, 0, sizeof(cc));
4321 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4323 memset(&diff_info, 0, sizeof(diff_info));
4324 diff_info.left_path = relpath;
4325 diff_info.right_path = relpath;
4327 diff_state = diff_output_unidiff_state_alloc();
4328 if (diff_state == NULL)
4329 return got_error_set_errno(ENOMEM,
4330 "diff_output_unidiff_state_alloc");
4332 hunkfile = got_opentemp();
4333 if (hunkfile == NULL) {
4334 err = got_error_from_errno("got_opentemp");
4338 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4340 if (rc != DIFF_RC_OK) {
4341 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4345 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4346 err = got_ferror(hunkfile, GOT_ERR_IO);
4350 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4351 hunkfile, changeno, nchanges);
4356 case GOT_PATCH_CHOICE_YES:
4357 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4358 end_old, start_new, end_new, outfile, rejectfile);
4360 case GOT_PATCH_CHOICE_NO:
4361 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4362 end_old, start_new, end_new, rejectfile, outfile);
4364 case GOT_PATCH_CHOICE_QUIT:
4367 err = got_error(GOT_ERR_PATCH_CHOICE);
4371 diff_output_unidiff_state_free(diff_state);
4372 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4373 err = got_error_from_errno("fclose");
4377 struct revert_file_args {
4378 struct got_worktree *worktree;
4379 struct got_fileindex *fileindex;
4380 got_worktree_checkout_cb progress_cb;
4382 got_worktree_patch_cb patch_cb;
4384 struct got_repository *repo;
4387 static const struct got_error *
4388 create_patched_content(char **path_outfile, int reverse_patch,
4389 struct got_object_id *blob_id, const char *path2,
4390 int dirfd2, const char *de_name2,
4391 const char *relpath, struct got_repository *repo,
4392 got_worktree_patch_cb patch_cb, void *patch_arg)
4394 const struct got_error *err, *free_err;
4395 struct got_blob_object *blob = NULL;
4396 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4398 char link_target[PATH_MAX];
4399 ssize_t link_len = 0;
4400 char *path1 = NULL, *id_str = NULL;
4402 struct got_diffreg_result *diffreg_result = NULL;
4403 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4404 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4406 *path_outfile = NULL;
4408 err = got_object_id_str(&id_str, blob_id);
4413 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4415 if (errno != ELOOP) {
4416 err = got_error_from_errno2("openat", path2);
4419 link_len = readlinkat(dirfd2, de_name2,
4420 link_target, sizeof(link_target));
4422 return got_error_from_errno2("readlinkat", path2);
4423 sb2.st_mode = S_IFLNK;
4424 sb2.st_size = link_len;
4427 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4429 if (errno != ELOOP) {
4430 err = got_error_from_errno2("open", path2);
4433 link_len = readlink(path2, link_target,
4434 sizeof(link_target));
4436 return got_error_from_errno2("readlink", path2);
4437 sb2.st_mode = S_IFLNK;
4438 sb2.st_size = link_len;
4442 if (fstat(fd2, &sb2) == -1) {
4443 err = got_error_from_errno2("fstat", path2);
4447 f2 = fdopen(fd2, "r");
4449 err = got_error_from_errno2("fdopen", path2);
4455 f2 = got_opentemp();
4457 err = got_error_from_errno2("got_opentemp", path2);
4460 n = fwrite(link_target, 1, link_len, f2);
4461 if (n != link_len) {
4462 err = got_ferror(f2, GOT_ERR_IO);
4465 if (fflush(f2) == EOF) {
4466 err = got_error_from_errno("fflush");
4472 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4476 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4480 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4484 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4489 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4493 if (fseek(f1, 0L, SEEK_SET) == -1)
4494 return got_ferror(f1, GOT_ERR_IO);
4495 if (fseek(f2, 0L, SEEK_SET) == -1)
4496 return got_ferror(f2, GOT_ERR_IO);
4498 /* Count the number of actual changes in the diff result. */
4499 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4500 struct diff_chunk_context cc = {};
4501 diff_chunk_context_load_change(&cc, &nchunks_used,
4502 diffreg_result->result, n, 0);
4505 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4507 err = apply_or_reject_change(&choice, &nchunks_used,
4508 diffreg_result->result, n, relpath, f1, f2,
4509 &line_cur1, &line_cur2,
4510 reverse_patch ? NULL : outfile,
4511 reverse_patch ? outfile : NULL,
4512 ++i, nchanges, patch_cb, patch_arg);
4515 if (choice == GOT_PATCH_CHOICE_YES)
4517 else if (choice == GOT_PATCH_CHOICE_QUIT)
4521 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4522 reverse_patch ? NULL : outfile,
4523 reverse_patch ? outfile : NULL);
4527 if (!S_ISLNK(sb2.st_mode)) {
4528 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4529 err = got_error_from_errno2("fchmod", path2);
4537 got_object_blob_close(blob);
4538 free_err = got_diffreg_result_free(diffreg_result);
4541 if (f1 && fclose(f1) == EOF && err == NULL)
4542 err = got_error_from_errno2("fclose", path1);
4543 if (f2 && fclose(f2) == EOF && err == NULL)
4544 err = got_error_from_errno2("fclose", path2);
4545 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4546 err = got_error_from_errno2("close", path2);
4547 if (outfile && fclose(outfile) == EOF && err == NULL)
4548 err = got_error_from_errno2("fclose", *path_outfile);
4549 if (path1 && unlink(path1) == -1 && err == NULL)
4550 err = got_error_from_errno2("unlink", path1);
4551 if (err || !have_content) {
4552 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4553 err = got_error_from_errno2("unlink", *path_outfile);
4554 free(*path_outfile);
4555 *path_outfile = NULL;
4561 static const struct got_error *
4562 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4563 const char *relpath, struct got_object_id *blob_id,
4564 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4565 int dirfd, const char *de_name)
4567 struct revert_file_args *a = arg;
4568 const struct got_error *err = NULL;
4569 char *parent_path = NULL;
4570 struct got_fileindex_entry *ie;
4571 struct got_tree_object *tree = NULL;
4572 struct got_object_id *tree_id = NULL;
4573 const struct got_tree_entry *te = NULL;
4574 char *tree_path = NULL, *te_name;
4575 char *ondisk_path = NULL, *path_content = NULL;
4576 struct got_blob_object *blob = NULL;
4578 /* Reverting a staged deletion is a no-op. */
4579 if (status == GOT_STATUS_DELETE &&
4580 staged_status != GOT_STATUS_NO_CHANGE)
4583 if (status == GOT_STATUS_UNVERSIONED)
4584 return (*a->progress_cb)(a->progress_arg,
4585 GOT_STATUS_UNVERSIONED, relpath);
4587 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4589 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4591 /* Construct in-repository path of tree which contains this blob. */
4592 err = got_path_dirname(&parent_path, ie->path);
4594 if (err->code != GOT_ERR_BAD_PATH)
4596 parent_path = strdup("/");
4597 if (parent_path == NULL) {
4598 err = got_error_from_errno("strdup");
4602 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4603 tree_path = strdup(parent_path);
4604 if (tree_path == NULL) {
4605 err = got_error_from_errno("strdup");
4609 if (got_path_is_root_dir(parent_path)) {
4610 tree_path = strdup(a->worktree->path_prefix);
4611 if (tree_path == NULL) {
4612 err = got_error_from_errno("strdup");
4616 if (asprintf(&tree_path, "%s/%s",
4617 a->worktree->path_prefix, parent_path) == -1) {
4618 err = got_error_from_errno("asprintf");
4624 err = got_object_id_by_path(&tree_id, a->repo,
4625 a->worktree->base_commit_id, tree_path);
4627 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4628 (status == GOT_STATUS_ADD ||
4629 staged_status == GOT_STATUS_ADD)))
4632 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4636 err = got_path_basename(&te_name, ie->path);
4640 te = got_object_tree_find_entry(tree, te_name);
4642 if (te == NULL && status != GOT_STATUS_ADD &&
4643 staged_status != GOT_STATUS_ADD) {
4644 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4650 case GOT_STATUS_ADD:
4652 int choice = GOT_PATCH_CHOICE_NONE;
4653 err = (*a->patch_cb)(&choice, a->patch_arg,
4654 status, ie->path, NULL, 1, 1);
4657 if (choice != GOT_PATCH_CHOICE_YES)
4660 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4664 got_fileindex_entry_remove(a->fileindex, ie);
4666 case GOT_STATUS_DELETE:
4668 int choice = GOT_PATCH_CHOICE_NONE;
4669 err = (*a->patch_cb)(&choice, a->patch_arg,
4670 status, ie->path, NULL, 1, 1);
4673 if (choice != GOT_PATCH_CHOICE_YES)
4677 case GOT_STATUS_MODIFY:
4678 case GOT_STATUS_MODE_CHANGE:
4679 case GOT_STATUS_CONFLICT:
4680 case GOT_STATUS_MISSING: {
4681 struct got_object_id id;
4682 if (staged_status == GOT_STATUS_ADD ||
4683 staged_status == GOT_STATUS_MODIFY) {
4684 memcpy(id.sha1, ie->staged_blob_sha1,
4685 SHA1_DIGEST_LENGTH);
4687 memcpy(id.sha1, ie->blob_sha1,
4688 SHA1_DIGEST_LENGTH);
4689 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4693 if (asprintf(&ondisk_path, "%s/%s",
4694 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4695 err = got_error_from_errno("asprintf");
4699 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4700 status == GOT_STATUS_CONFLICT)) {
4701 int is_bad_symlink = 0;
4702 err = create_patched_content(&path_content, 1, &id,
4703 ondisk_path, dirfd, de_name, ie->path, a->repo,
4704 a->patch_cb, a->patch_arg);
4705 if (err || path_content == NULL)
4707 if (te && S_ISLNK(te->mode)) {
4708 if (unlink(path_content) == -1) {
4709 err = got_error_from_errno2("unlink",
4713 err = install_symlink(&is_bad_symlink,
4714 a->worktree, ondisk_path, ie->path,
4715 blob, 0, 1, 0, a->repo,
4716 a->progress_cb, a->progress_arg);
4718 if (rename(path_content, ondisk_path) == -1) {
4719 err = got_error_from_errno3("rename",
4720 path_content, ondisk_path);
4725 int is_bad_symlink = 0;
4726 if (te && S_ISLNK(te->mode)) {
4727 err = install_symlink(&is_bad_symlink,
4728 a->worktree, ondisk_path, ie->path,
4729 blob, 0, 1, 0, a->repo,
4730 a->progress_cb, a->progress_arg);
4732 err = install_blob(a->worktree, ondisk_path,
4734 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4735 got_fileindex_perms_to_st(ie), blob,
4736 0, 1, 0, 0, a->repo,
4737 a->progress_cb, a->progress_arg);
4741 if (status == GOT_STATUS_DELETE ||
4742 status == GOT_STATUS_MODE_CHANGE) {
4743 err = got_fileindex_entry_update(ie,
4744 a->worktree->root_fd, relpath,
4746 a->worktree->base_commit_id->sha1, 1);
4750 if (is_bad_symlink) {
4751 got_fileindex_entry_filetype_set(ie,
4752 GOT_FILEIDX_MODE_BAD_SYMLINK);
4766 got_object_blob_close(blob);
4768 got_object_tree_close(tree);
4773 const struct got_error *
4774 got_worktree_revert(struct got_worktree *worktree,
4775 struct got_pathlist_head *paths,
4776 got_worktree_checkout_cb progress_cb, void *progress_arg,
4777 got_worktree_patch_cb patch_cb, void *patch_arg,
4778 struct got_repository *repo)
4780 struct got_fileindex *fileindex = NULL;
4781 char *fileindex_path = NULL;
4782 const struct got_error *err = NULL, *unlockerr = NULL;
4783 const struct got_error *sync_err = NULL;
4784 struct got_pathlist_entry *pe;
4785 struct revert_file_args rfa;
4787 err = lock_worktree(worktree, LOCK_EX);
4791 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4795 rfa.worktree = worktree;
4796 rfa.fileindex = fileindex;
4797 rfa.progress_cb = progress_cb;
4798 rfa.progress_arg = progress_arg;
4799 rfa.patch_cb = patch_cb;
4800 rfa.patch_arg = patch_arg;
4802 TAILQ_FOREACH(pe, paths, entry) {
4803 err = worktree_status(worktree, pe->path, fileindex, repo,
4804 revert_file, &rfa, NULL, NULL, 0, 0);
4808 sync_err = sync_fileindex(fileindex, fileindex_path);
4809 if (sync_err && err == NULL)
4812 free(fileindex_path);
4814 got_fileindex_free(fileindex);
4815 unlockerr = lock_worktree(worktree, LOCK_SH);
4816 if (unlockerr && err == NULL)
4822 free_commitable(struct got_commitable *ct)
4825 free(ct->in_repo_path);
4826 free(ct->ondisk_path);
4828 free(ct->base_blob_id);
4829 free(ct->staged_blob_id);
4830 free(ct->base_commit_id);
4834 struct collect_commitables_arg {
4835 struct got_pathlist_head *commitable_paths;
4836 struct got_repository *repo;
4837 struct got_worktree *worktree;
4838 struct got_fileindex *fileindex;
4839 int have_staged_files;
4840 int allow_bad_symlinks;
4843 static const struct got_error *
4844 collect_commitables(void *arg, unsigned char status,
4845 unsigned char staged_status, const char *relpath,
4846 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4847 struct got_object_id *commit_id, int dirfd, const char *de_name)
4849 struct collect_commitables_arg *a = arg;
4850 const struct got_error *err = NULL;
4851 struct got_commitable *ct = NULL;
4852 struct got_pathlist_entry *new = NULL;
4853 char *parent_path = NULL, *path = NULL;
4856 if (a->have_staged_files) {
4857 if (staged_status != GOT_STATUS_MODIFY &&
4858 staged_status != GOT_STATUS_ADD &&
4859 staged_status != GOT_STATUS_DELETE)
4862 if (status == GOT_STATUS_CONFLICT)
4863 return got_error(GOT_ERR_COMMIT_CONFLICT);
4865 if (status != GOT_STATUS_MODIFY &&
4866 status != GOT_STATUS_MODE_CHANGE &&
4867 status != GOT_STATUS_ADD &&
4868 status != GOT_STATUS_DELETE)
4872 if (asprintf(&path, "/%s", relpath) == -1) {
4873 err = got_error_from_errno("asprintf");
4876 if (strcmp(path, "/") == 0) {
4877 parent_path = strdup("");
4878 if (parent_path == NULL)
4879 return got_error_from_errno("strdup");
4881 err = got_path_dirname(&parent_path, path);
4886 ct = calloc(1, sizeof(*ct));
4888 err = got_error_from_errno("calloc");
4892 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4894 err = got_error_from_errno("asprintf");
4898 if (staged_status == GOT_STATUS_ADD ||
4899 staged_status == GOT_STATUS_MODIFY) {
4900 struct got_fileindex_entry *ie;
4901 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4902 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4903 case GOT_FILEIDX_MODE_REGULAR_FILE:
4904 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4907 case GOT_FILEIDX_MODE_SYMLINK:
4911 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4914 ct->mode |= got_fileindex_entry_perms_get(ie);
4915 } else if (status != GOT_STATUS_DELETE &&
4916 staged_status != GOT_STATUS_DELETE) {
4918 if (fstatat(dirfd, de_name, &sb,
4919 AT_SYMLINK_NOFOLLOW) == -1) {
4920 err = got_error_from_errno2("fstatat",
4924 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4925 err = got_error_from_errno2("lstat", ct->ondisk_path);
4928 ct->mode = sb.st_mode;
4931 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4932 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4934 err = got_error_from_errno("asprintf");
4938 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4939 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4941 char target_path[PATH_MAX];
4943 target_len = readlink(ct->ondisk_path, target_path,
4944 sizeof(target_path));
4945 if (target_len == -1) {
4946 err = got_error_from_errno2("readlink",
4950 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4951 target_len, ct->ondisk_path, a->worktree->root_path);
4954 if (is_bad_symlink) {
4955 err = got_error_path(ct->ondisk_path,
4956 GOT_ERR_BAD_SYMLINK);
4962 ct->status = status;
4963 ct->staged_status = staged_status;
4964 ct->blob_id = NULL; /* will be filled in when blob gets created */
4965 if (ct->status != GOT_STATUS_ADD &&
4966 ct->staged_status != GOT_STATUS_ADD) {
4967 ct->base_blob_id = got_object_id_dup(blob_id);
4968 if (ct->base_blob_id == NULL) {
4969 err = got_error_from_errno("got_object_id_dup");
4972 ct->base_commit_id = got_object_id_dup(commit_id);
4973 if (ct->base_commit_id == NULL) {
4974 err = got_error_from_errno("got_object_id_dup");
4978 if (ct->staged_status == GOT_STATUS_ADD ||
4979 ct->staged_status == GOT_STATUS_MODIFY) {
4980 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4981 if (ct->staged_blob_id == NULL) {
4982 err = got_error_from_errno("got_object_id_dup");
4986 ct->path = strdup(path);
4987 if (ct->path == NULL) {
4988 err = got_error_from_errno("strdup");
4991 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4993 if (ct && (err || new == NULL))
4994 free_commitable(ct);
5000 static const struct got_error *write_tree(struct got_object_id **, int *,
5001 struct got_tree_object *, const char *, struct got_pathlist_head *,
5002 got_worktree_status_cb status_cb, void *status_arg,
5003 struct got_repository *);
5005 static const struct got_error *
5006 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5007 struct got_tree_entry *te, const char *parent_path,
5008 struct got_pathlist_head *commitable_paths,
5009 got_worktree_status_cb status_cb, void *status_arg,
5010 struct got_repository *repo)
5012 const struct got_error *err = NULL;
5013 struct got_tree_object *subtree;
5016 if (asprintf(&subpath, "%s%s%s", parent_path,
5017 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5018 return got_error_from_errno("asprintf");
5020 err = got_object_open_as_tree(&subtree, repo, &te->id);
5024 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5025 commitable_paths, status_cb, status_arg, repo);
5026 got_object_tree_close(subtree);
5031 static const struct got_error *
5032 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5034 const struct got_error *err = NULL;
5035 char *ct_parent_path = NULL;
5039 if (strchr(ct->in_repo_path, '/') == NULL) {
5040 *match = got_path_is_root_dir(path);
5044 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5047 *match = (strcmp(path, ct_parent_path) == 0);
5048 free(ct_parent_path);
5053 get_ct_file_mode(struct got_commitable *ct)
5055 if (S_ISLNK(ct->mode))
5058 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5061 static const struct got_error *
5062 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5063 struct got_tree_entry *te, struct got_commitable *ct)
5065 const struct got_error *err = NULL;
5069 err = got_object_tree_entry_dup(new_te, te);
5073 (*new_te)->mode = get_ct_file_mode(ct);
5075 if (ct->staged_status == GOT_STATUS_MODIFY)
5076 memcpy(&(*new_te)->id, ct->staged_blob_id,
5077 sizeof((*new_te)->id));
5079 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5081 if (err && *new_te) {
5088 static const struct got_error *
5089 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5090 struct got_commitable *ct)
5092 const struct got_error *err = NULL;
5093 char *ct_name = NULL;
5097 *new_te = calloc(1, sizeof(**new_te));
5098 if (*new_te == NULL)
5099 return got_error_from_errno("calloc");
5101 err = got_path_basename(&ct_name, ct->path);
5104 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5105 sizeof((*new_te)->name)) {
5106 err = got_error(GOT_ERR_NO_SPACE);
5110 (*new_te)->mode = get_ct_file_mode(ct);
5112 if (ct->staged_status == GOT_STATUS_ADD)
5113 memcpy(&(*new_te)->id, ct->staged_blob_id,
5114 sizeof((*new_te)->id));
5116 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5119 if (err && *new_te) {
5126 static const struct got_error *
5127 insert_tree_entry(struct got_tree_entry *new_te,
5128 struct got_pathlist_head *paths)
5130 const struct got_error *err = NULL;
5131 struct got_pathlist_entry *new_pe;
5133 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5137 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5141 static const struct got_error *
5142 report_ct_status(struct got_commitable *ct,
5143 got_worktree_status_cb status_cb, void *status_arg)
5145 const char *ct_path = ct->path;
5146 unsigned char status;
5148 while (ct_path[0] == '/')
5151 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5152 status = ct->staged_status;
5154 status = ct->status;
5156 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5157 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5160 static const struct got_error *
5161 match_modified_subtree(int *modified, struct got_tree_entry *te,
5162 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5164 const struct got_error *err = NULL;
5165 struct got_pathlist_entry *pe;
5170 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5171 got_path_is_root_dir(base_tree_path) ? "" : "/",
5173 return got_error_from_errno("asprintf");
5175 TAILQ_FOREACH(pe, commitable_paths, entry) {
5176 struct got_commitable *ct = pe->data;
5177 *modified = got_path_is_child(ct->in_repo_path, te_path,
5187 static const struct got_error *
5188 match_deleted_or_modified_ct(struct got_commitable **ctp,
5189 struct got_tree_entry *te, const char *base_tree_path,
5190 struct got_pathlist_head *commitable_paths)
5192 const struct got_error *err = NULL;
5193 struct got_pathlist_entry *pe;
5197 TAILQ_FOREACH(pe, commitable_paths, entry) {
5198 struct got_commitable *ct = pe->data;
5199 char *ct_name = NULL;
5202 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5203 if (ct->status != GOT_STATUS_MODIFY &&
5204 ct->status != GOT_STATUS_MODE_CHANGE &&
5205 ct->status != GOT_STATUS_DELETE)
5208 if (ct->staged_status != GOT_STATUS_MODIFY &&
5209 ct->staged_status != GOT_STATUS_DELETE)
5213 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5216 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5222 err = got_path_basename(&ct_name, pe->path);
5226 if (strcmp(te->name, ct_name) != 0) {
5239 static const struct got_error *
5240 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5241 const char *child_path, const char *path_base_tree,
5242 struct got_pathlist_head *commitable_paths,
5243 got_worktree_status_cb status_cb, void *status_arg,
5244 struct got_repository *repo)
5246 const struct got_error *err = NULL;
5247 struct got_tree_entry *new_te;
5249 struct got_object_id *id = NULL;
5254 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5255 got_path_is_root_dir(path_base_tree) ? "" : "/",
5257 return got_error_from_errno("asprintf");
5259 new_te = calloc(1, sizeof(*new_te));
5261 return got_error_from_errno("calloc");
5262 new_te->mode = S_IFDIR;
5264 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5265 sizeof(new_te->name)) {
5266 err = got_error(GOT_ERR_NO_SPACE);
5269 err = write_tree(&id, &nentries, NULL, subtree_path,
5270 commitable_paths, status_cb, status_arg, repo);
5275 memcpy(&new_te->id, id, sizeof(new_te->id));
5284 static const struct got_error *
5285 write_tree(struct got_object_id **new_tree_id, int *nentries,
5286 struct got_tree_object *base_tree, const char *path_base_tree,
5287 struct got_pathlist_head *commitable_paths,
5288 got_worktree_status_cb status_cb, void *status_arg,
5289 struct got_repository *repo)
5291 const struct got_error *err = NULL;
5292 struct got_pathlist_head paths;
5293 struct got_tree_entry *te, *new_te = NULL;
5294 struct got_pathlist_entry *pe;
5299 /* Insert, and recurse into, newly added entries first. */
5300 TAILQ_FOREACH(pe, commitable_paths, entry) {
5301 struct got_commitable *ct = pe->data;
5302 char *child_path = NULL, *slash;
5304 if ((ct->status != GOT_STATUS_ADD &&
5305 ct->staged_status != GOT_STATUS_ADD) ||
5306 (ct->flags & GOT_COMMITABLE_ADDED))
5309 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5310 strlen(path_base_tree)))
5313 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5318 slash = strchr(child_path, '/');
5319 if (slash == NULL) {
5320 err = alloc_added_blob_tree_entry(&new_te, ct);
5323 err = report_ct_status(ct, status_cb, status_arg);
5326 ct->flags |= GOT_COMMITABLE_ADDED;
5327 err = insert_tree_entry(new_te, &paths);
5332 *slash = '\0'; /* trim trailing path components */
5333 if (base_tree == NULL ||
5334 got_object_tree_find_entry(base_tree, child_path)
5336 err = make_subtree_for_added_blob(&new_te,
5337 child_path, path_base_tree,
5338 commitable_paths, status_cb, status_arg,
5342 err = insert_tree_entry(new_te, &paths);
5351 int i, nbase_entries;
5352 /* Handle modified and deleted entries. */
5353 nbase_entries = got_object_tree_get_nentries(base_tree);
5354 for (i = 0; i < nbase_entries; i++) {
5355 struct got_commitable *ct = NULL;
5357 te = got_object_tree_get_entry(base_tree, i);
5358 if (got_object_tree_entry_is_submodule(te)) {
5359 /* Entry is a submodule; just copy it. */
5360 err = got_object_tree_entry_dup(&new_te, te);
5363 err = insert_tree_entry(new_te, &paths);
5370 if (S_ISDIR(te->mode)) {
5372 err = got_object_tree_entry_dup(&new_te, te);
5375 err = match_modified_subtree(&modified, te,
5376 path_base_tree, commitable_paths);
5379 /* Avoid recursion into unmodified subtrees. */
5381 struct got_object_id *new_id;
5383 err = write_subtree(&new_id,
5385 path_base_tree, commitable_paths,
5386 status_cb, status_arg, repo);
5389 if (nsubentries == 0) {
5390 /* All entries were deleted. */
5394 memcpy(&new_te->id, new_id,
5395 sizeof(new_te->id));
5398 err = insert_tree_entry(new_te, &paths);
5405 err = match_deleted_or_modified_ct(&ct, te,
5406 path_base_tree, commitable_paths);
5410 /* NB: Deleted entries get dropped here. */
5411 if (ct->status == GOT_STATUS_MODIFY ||
5412 ct->status == GOT_STATUS_MODE_CHANGE ||
5413 ct->staged_status == GOT_STATUS_MODIFY) {
5414 err = alloc_modified_blob_tree_entry(
5418 err = insert_tree_entry(new_te, &paths);
5423 err = report_ct_status(ct, status_cb,
5428 /* Entry is unchanged; just copy it. */
5429 err = got_object_tree_entry_dup(&new_te, te);
5432 err = insert_tree_entry(new_te, &paths);
5440 /* Write new list of entries; deleted entries have been dropped. */
5441 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5443 got_pathlist_free(&paths);
5447 static const struct got_error *
5448 update_fileindex_after_commit(struct got_worktree *worktree,
5449 struct got_pathlist_head *commitable_paths,
5450 struct got_object_id *new_base_commit_id,
5451 struct got_fileindex *fileindex, int have_staged_files)
5453 const struct got_error *err = NULL;
5454 struct got_pathlist_entry *pe;
5455 char *relpath = NULL;
5457 TAILQ_FOREACH(pe, commitable_paths, entry) {
5458 struct got_fileindex_entry *ie;
5459 struct got_commitable *ct = pe->data;
5461 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5463 err = got_path_skip_common_ancestor(&relpath,
5464 worktree->root_path, ct->ondisk_path);
5469 if (ct->status == GOT_STATUS_DELETE ||
5470 ct->staged_status == GOT_STATUS_DELETE) {
5471 got_fileindex_entry_remove(fileindex, ie);
5472 } else if (ct->staged_status == GOT_STATUS_ADD ||
5473 ct->staged_status == GOT_STATUS_MODIFY) {
5474 got_fileindex_entry_stage_set(ie,
5475 GOT_FILEIDX_STAGE_NONE);
5476 got_fileindex_entry_staged_filetype_set(ie, 0);
5478 err = got_fileindex_entry_update(ie,
5479 worktree->root_fd, relpath,
5480 ct->staged_blob_id->sha1,
5481 new_base_commit_id->sha1,
5482 !have_staged_files);
5484 err = got_fileindex_entry_update(ie,
5485 worktree->root_fd, relpath,
5487 new_base_commit_id->sha1,
5488 !have_staged_files);
5490 err = got_fileindex_entry_alloc(&ie, pe->path);
5493 err = got_fileindex_entry_update(ie,
5494 worktree->root_fd, relpath, ct->blob_id->sha1,
5495 new_base_commit_id->sha1, 1);
5497 got_fileindex_entry_free(ie);
5500 err = got_fileindex_entry_add(fileindex, ie);
5502 got_fileindex_entry_free(ie);
5515 static const struct got_error *
5516 check_out_of_date(const char *in_repo_path, unsigned char status,
5517 unsigned char staged_status, struct got_object_id *base_blob_id,
5518 struct got_object_id *base_commit_id,
5519 struct got_object_id *head_commit_id, struct got_repository *repo,
5522 const struct got_error *err = NULL;
5523 struct got_object_id *id = NULL;
5525 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5526 /* Trivial case: base commit == head commit */
5527 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5530 * Ensure file content which local changes were based
5531 * on matches file content in the branch head.
5533 err = got_object_id_by_path(&id, repo, head_commit_id,
5536 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5537 err = got_error(ood_errcode);
5539 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5540 err = got_error(ood_errcode);
5542 /* Require that added files don't exist in the branch head. */
5543 err = got_object_id_by_path(&id, repo, head_commit_id,
5545 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5547 err = id ? got_error(ood_errcode) : NULL;
5554 const struct got_error *
5555 commit_worktree(struct got_object_id **new_commit_id,
5556 struct got_pathlist_head *commitable_paths,
5557 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5558 const char *author, const char *committer,
5559 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5560 got_worktree_status_cb status_cb, void *status_arg,
5561 struct got_repository *repo)
5563 const struct got_error *err = NULL, *unlockerr = NULL;
5564 struct got_pathlist_entry *pe;
5565 const char *head_ref_name = NULL;
5566 struct got_commit_object *head_commit = NULL;
5567 struct got_reference *head_ref2 = NULL;
5568 struct got_object_id *head_commit_id2 = NULL;
5569 struct got_tree_object *head_tree = NULL;
5570 struct got_object_id *new_tree_id = NULL;
5572 struct got_object_id_queue parent_ids;
5573 struct got_object_qid *pid = NULL;
5574 char *logmsg = NULL;
5576 *new_commit_id = NULL;
5578 STAILQ_INIT(&parent_ids);
5580 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5584 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5588 if (commit_msg_cb != NULL) {
5589 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5594 if (logmsg == NULL || strlen(logmsg) == 0) {
5595 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5599 /* Create blobs from added and modified files and record their IDs. */
5600 TAILQ_FOREACH(pe, commitable_paths, entry) {
5601 struct got_commitable *ct = pe->data;
5604 /* Blobs for staged files already exist. */
5605 if (ct->staged_status == GOT_STATUS_ADD ||
5606 ct->staged_status == GOT_STATUS_MODIFY)
5609 if (ct->status != GOT_STATUS_ADD &&
5610 ct->status != GOT_STATUS_MODIFY &&
5611 ct->status != GOT_STATUS_MODE_CHANGE)
5614 if (asprintf(&ondisk_path, "%s/%s",
5615 worktree->root_path, pe->path) == -1) {
5616 err = got_error_from_errno("asprintf");
5619 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5625 /* Recursively write new tree objects. */
5626 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5627 commitable_paths, status_cb, status_arg, repo);
5631 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5634 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5635 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5636 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5637 got_object_qid_free(pid);
5643 /* Check if a concurrent commit to our branch has occurred. */
5644 head_ref_name = got_worktree_get_head_ref_name(worktree);
5645 if (head_ref_name == NULL) {
5646 err = got_error_from_errno("got_worktree_get_head_ref_name");
5649 /* Lock the reference here to prevent concurrent modification. */
5650 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5653 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5656 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5657 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5660 /* Update branch head in repository. */
5661 err = got_ref_change_ref(head_ref2, *new_commit_id);
5664 err = got_ref_write(head_ref2, repo);
5668 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5672 err = ref_base_commit(worktree, repo);
5677 got_object_tree_close(head_tree);
5679 got_object_commit_close(head_commit);
5680 free(head_commit_id2);
5682 unlockerr = got_ref_unlock(head_ref2);
5683 if (unlockerr && err == NULL)
5685 got_ref_close(head_ref2);
5690 static const struct got_error *
5691 check_path_is_commitable(const char *path,
5692 struct got_pathlist_head *commitable_paths)
5694 struct got_pathlist_entry *cpe = NULL;
5695 size_t path_len = strlen(path);
5697 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5698 struct got_commitable *ct = cpe->data;
5699 const char *ct_path = ct->path;
5701 while (ct_path[0] == '/')
5704 if (strcmp(path, ct_path) == 0 ||
5705 got_path_is_child(ct_path, path, path_len))
5710 return got_error_path(path, GOT_ERR_BAD_PATH);
5715 static const struct got_error *
5716 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5718 int *have_staged_files = arg;
5720 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5721 *have_staged_files = 1;
5722 return got_error(GOT_ERR_CANCELLED);
5728 static const struct got_error *
5729 check_non_staged_files(struct got_fileindex *fileindex,
5730 struct got_pathlist_head *paths)
5732 struct got_pathlist_entry *pe;
5733 struct got_fileindex_entry *ie;
5735 TAILQ_FOREACH(pe, paths, entry) {
5736 if (pe->path[0] == '\0')
5738 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5740 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5741 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5742 return got_error_path(pe->path,
5743 GOT_ERR_FILE_NOT_STAGED);
5749 const struct got_error *
5750 got_worktree_commit(struct got_object_id **new_commit_id,
5751 struct got_worktree *worktree, struct got_pathlist_head *paths,
5752 const char *author, const char *committer, int allow_bad_symlinks,
5753 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5754 got_worktree_status_cb status_cb, void *status_arg,
5755 struct got_repository *repo)
5757 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5758 struct got_fileindex *fileindex = NULL;
5759 char *fileindex_path = NULL;
5760 struct got_pathlist_head commitable_paths;
5761 struct collect_commitables_arg cc_arg;
5762 struct got_pathlist_entry *pe;
5763 struct got_reference *head_ref = NULL;
5764 struct got_object_id *head_commit_id = NULL;
5765 int have_staged_files = 0;
5767 *new_commit_id = NULL;
5769 TAILQ_INIT(&commitable_paths);
5771 err = lock_worktree(worktree, LOCK_EX);
5775 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5779 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5783 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5787 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5788 &have_staged_files);
5789 if (err && err->code != GOT_ERR_CANCELLED)
5791 if (have_staged_files) {
5792 err = check_non_staged_files(fileindex, paths);
5797 cc_arg.commitable_paths = &commitable_paths;
5798 cc_arg.worktree = worktree;
5799 cc_arg.fileindex = fileindex;
5801 cc_arg.have_staged_files = have_staged_files;
5802 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5803 TAILQ_FOREACH(pe, paths, entry) {
5804 err = worktree_status(worktree, pe->path, fileindex, repo,
5805 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5810 if (TAILQ_EMPTY(&commitable_paths)) {
5811 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5815 TAILQ_FOREACH(pe, paths, entry) {
5816 err = check_path_is_commitable(pe->path, &commitable_paths);
5821 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5822 struct got_commitable *ct = pe->data;
5823 const char *ct_path = ct->in_repo_path;
5825 while (ct_path[0] == '/')
5827 err = check_out_of_date(ct_path, ct->status,
5828 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5829 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5835 err = commit_worktree(new_commit_id, &commitable_paths,
5836 head_commit_id, worktree, author, committer,
5837 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5841 err = update_fileindex_after_commit(worktree, &commitable_paths,
5842 *new_commit_id, fileindex, have_staged_files);
5843 sync_err = sync_fileindex(fileindex, fileindex_path);
5844 if (sync_err && err == NULL)
5848 got_fileindex_free(fileindex);
5849 free(fileindex_path);
5850 unlockerr = lock_worktree(worktree, LOCK_SH);
5851 if (unlockerr && err == NULL)
5853 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5854 struct got_commitable *ct = pe->data;
5855 free_commitable(ct);
5857 got_pathlist_free(&commitable_paths);
5862 got_commitable_get_path(struct got_commitable *ct)
5868 got_commitable_get_status(struct got_commitable *ct)
5873 struct check_rebase_ok_arg {
5874 struct got_worktree *worktree;
5875 struct got_repository *repo;
5878 static const struct got_error *
5879 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5881 const struct got_error *err = NULL;
5882 struct check_rebase_ok_arg *a = arg;
5883 unsigned char status;
5887 /* Reject rebase of a work tree with mixed base commits. */
5888 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5889 SHA1_DIGEST_LENGTH))
5890 return got_error(GOT_ERR_MIXED_COMMITS);
5892 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5894 return got_error_from_errno("asprintf");
5896 /* Reject rebase of a work tree with modified or staged files. */
5897 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5902 if (status != GOT_STATUS_NO_CHANGE)
5903 return got_error(GOT_ERR_MODIFIED);
5904 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5905 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5910 const struct got_error *
5911 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5912 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5913 struct got_worktree *worktree, struct got_reference *branch,
5914 struct got_repository *repo)
5916 const struct got_error *err = NULL;
5917 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5918 char *branch_ref_name = NULL;
5919 char *fileindex_path = NULL;
5920 struct check_rebase_ok_arg ok_arg;
5921 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5922 struct got_object_id *wt_branch_tip = NULL;
5924 *new_base_branch_ref = NULL;
5928 err = lock_worktree(worktree, LOCK_EX);
5932 err = open_fileindex(fileindex, &fileindex_path, worktree);
5936 ok_arg.worktree = worktree;
5938 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5943 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5947 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5951 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5955 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5960 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5963 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5964 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5968 err = got_ref_alloc_symref(new_base_branch_ref,
5969 new_base_branch_ref_name, wt_branch);
5972 err = got_ref_write(*new_base_branch_ref, repo);
5976 /* TODO Lock original branch's ref while rebasing? */
5978 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5982 err = got_ref_write(branch_ref, repo);
5986 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5987 worktree->base_commit_id);
5990 err = got_ref_write(*tmp_branch, repo);
5994 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5998 free(fileindex_path);
5999 free(tmp_branch_name);
6000 free(new_base_branch_ref_name);
6001 free(branch_ref_name);
6003 got_ref_close(branch_ref);
6005 got_ref_close(wt_branch);
6006 free(wt_branch_tip);
6008 if (*new_base_branch_ref) {
6009 got_ref_close(*new_base_branch_ref);
6010 *new_base_branch_ref = NULL;
6013 got_ref_close(*tmp_branch);
6017 got_fileindex_free(*fileindex);
6020 lock_worktree(worktree, LOCK_SH);
6025 const struct got_error *
6026 got_worktree_rebase_continue(struct got_object_id **commit_id,
6027 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6028 struct got_reference **branch, struct got_fileindex **fileindex,
6029 struct got_worktree *worktree, struct got_repository *repo)
6031 const struct got_error *err;
6032 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6033 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6034 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6035 char *fileindex_path = NULL;
6036 int have_staged_files = 0;
6039 *new_base_branch = NULL;
6044 err = lock_worktree(worktree, LOCK_EX);
6048 err = open_fileindex(fileindex, &fileindex_path, worktree);
6052 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6053 &have_staged_files);
6054 if (err && err->code != GOT_ERR_CANCELLED)
6056 if (have_staged_files) {
6057 err = got_error(GOT_ERR_STAGED_PATHS);
6061 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6065 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6069 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6073 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6077 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6081 err = got_ref_open(branch, repo,
6082 got_ref_get_symref_target(branch_ref), 0);
6086 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6090 err = got_ref_resolve(commit_id, repo, commit_ref);
6094 err = got_ref_open(new_base_branch, repo,
6095 new_base_branch_ref_name, 0);
6099 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6103 free(commit_ref_name);
6104 free(branch_ref_name);
6105 free(fileindex_path);
6107 got_ref_close(commit_ref);
6109 got_ref_close(branch_ref);
6114 got_ref_close(*tmp_branch);
6117 if (*new_base_branch) {
6118 got_ref_close(*new_base_branch);
6119 *new_base_branch = NULL;
6122 got_ref_close(*branch);
6126 got_fileindex_free(*fileindex);
6129 lock_worktree(worktree, LOCK_SH);
6134 const struct got_error *
6135 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6137 const struct got_error *err;
6138 char *tmp_branch_name = NULL;
6140 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6144 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6145 free(tmp_branch_name);
6149 static const struct got_error *
6150 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6151 char **logmsg, void *arg)
6157 static const struct got_error *
6158 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6159 const char *path, struct got_object_id *blob_id,
6160 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6161 int dirfd, const char *de_name)
6166 struct collect_merged_paths_arg {
6167 got_worktree_checkout_cb progress_cb;
6169 struct got_pathlist_head *merged_paths;
6172 static const struct got_error *
6173 collect_merged_paths(void *arg, unsigned char status, const char *path)
6175 const struct got_error *err;
6176 struct collect_merged_paths_arg *a = arg;
6178 struct got_pathlist_entry *new;
6180 err = (*a->progress_cb)(a->progress_arg, status, path);
6184 if (status != GOT_STATUS_MERGE &&
6185 status != GOT_STATUS_ADD &&
6186 status != GOT_STATUS_DELETE &&
6187 status != GOT_STATUS_CONFLICT)
6192 return got_error_from_errno("strdup");
6194 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6195 if (err || new == NULL)
6201 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6203 struct got_pathlist_entry *pe;
6205 TAILQ_FOREACH(pe, merged_paths, entry)
6206 free((char *)pe->path);
6208 got_pathlist_free(merged_paths);
6211 static const struct got_error *
6212 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6213 int is_rebase, struct got_repository *repo)
6215 const struct got_error *err;
6216 struct got_reference *commit_ref = NULL;
6218 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6220 if (err->code != GOT_ERR_NOT_REF)
6222 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6225 err = got_ref_write(commit_ref, repo);
6228 } else if (is_rebase) {
6229 struct got_object_id *stored_id;
6232 err = got_ref_resolve(&stored_id, repo, commit_ref);
6235 cmp = got_object_id_cmp(commit_id, stored_id);
6238 err = got_error(GOT_ERR_REBASE_COMMITID);
6244 got_ref_close(commit_ref);
6248 static const struct got_error *
6249 rebase_merge_files(struct got_pathlist_head *merged_paths,
6250 const char *commit_ref_name, struct got_worktree *worktree,
6251 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6252 struct got_object_id *commit_id, struct got_repository *repo,
6253 got_worktree_checkout_cb progress_cb, void *progress_arg,
6254 got_cancel_cb cancel_cb, void *cancel_arg)
6256 const struct got_error *err;
6257 struct got_reference *commit_ref = NULL;
6258 struct collect_merged_paths_arg cmp_arg;
6259 char *fileindex_path;
6261 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6263 err = get_fileindex_path(&fileindex_path, worktree);
6267 cmp_arg.progress_cb = progress_cb;
6268 cmp_arg.progress_arg = progress_arg;
6269 cmp_arg.merged_paths = merged_paths;
6270 err = merge_files(worktree, fileindex, fileindex_path,
6271 parent_commit_id, commit_id, repo, collect_merged_paths,
6272 &cmp_arg, cancel_cb, cancel_arg);
6274 got_ref_close(commit_ref);
6278 const struct got_error *
6279 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6280 struct got_worktree *worktree, struct got_fileindex *fileindex,
6281 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6282 struct got_repository *repo,
6283 got_worktree_checkout_cb progress_cb, void *progress_arg,
6284 got_cancel_cb cancel_cb, void *cancel_arg)
6286 const struct got_error *err;
6287 char *commit_ref_name;
6289 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6293 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6297 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6298 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6299 progress_arg, cancel_cb, cancel_arg);
6301 free(commit_ref_name);
6305 const struct got_error *
6306 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6307 struct got_worktree *worktree, struct got_fileindex *fileindex,
6308 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6309 struct got_repository *repo,
6310 got_worktree_checkout_cb progress_cb, void *progress_arg,
6311 got_cancel_cb cancel_cb, void *cancel_arg)
6313 const struct got_error *err;
6314 char *commit_ref_name;
6316 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6320 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6324 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6325 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6326 progress_arg, cancel_cb, cancel_arg);
6328 free(commit_ref_name);
6332 static const struct got_error *
6333 rebase_commit(struct got_object_id **new_commit_id,
6334 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6335 struct got_worktree *worktree, struct got_fileindex *fileindex,
6336 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6337 const char *new_logmsg, struct got_repository *repo)
6339 const struct got_error *err, *sync_err;
6340 struct got_pathlist_head commitable_paths;
6341 struct collect_commitables_arg cc_arg;
6342 char *fileindex_path = NULL;
6343 struct got_reference *head_ref = NULL;
6344 struct got_object_id *head_commit_id = NULL;
6345 char *logmsg = NULL;
6347 TAILQ_INIT(&commitable_paths);
6348 *new_commit_id = NULL;
6350 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6352 err = get_fileindex_path(&fileindex_path, worktree);
6356 cc_arg.commitable_paths = &commitable_paths;
6357 cc_arg.worktree = worktree;
6359 cc_arg.have_staged_files = 0;
6361 * If possible get the status of individual files directly to
6362 * avoid crawling the entire work tree once per rebased commit.
6364 * Ideally, merged_paths would contain a list of commitables
6365 * we could use so we could skip worktree_status() entirely.
6366 * However, we would then need carefully keep track of cumulative
6367 * effects of operations such as file additions and deletions
6368 * in 'got histedit -f' (folding multiple commits into one),
6369 * and this extra complexity is not really worth it.
6372 struct got_pathlist_entry *pe;
6373 TAILQ_FOREACH(pe, merged_paths, entry) {
6374 err = worktree_status(worktree, pe->path, fileindex,
6375 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6381 err = worktree_status(worktree, "", fileindex, repo,
6382 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6387 if (TAILQ_EMPTY(&commitable_paths)) {
6388 /* No-op change; commit will be elided. */
6389 err = got_ref_delete(commit_ref, repo);
6392 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6396 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6400 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6405 logmsg = strdup(new_logmsg);
6406 if (logmsg == NULL) {
6407 err = got_error_from_errno("strdup");
6411 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6416 /* NB: commit_worktree will call free(logmsg) */
6417 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6418 worktree, got_object_commit_get_author(orig_commit),
6419 got_object_commit_get_committer(orig_commit),
6420 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6424 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6428 err = got_ref_delete(commit_ref, repo);
6432 err = update_fileindex_after_commit(worktree, &commitable_paths,
6433 *new_commit_id, fileindex, 0);
6434 sync_err = sync_fileindex(fileindex, fileindex_path);
6435 if (sync_err && err == NULL)
6438 free(fileindex_path);
6439 free(head_commit_id);
6441 got_ref_close(head_ref);
6443 free(*new_commit_id);
6444 *new_commit_id = NULL;
6449 const struct got_error *
6450 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6451 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6452 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6453 struct got_commit_object *orig_commit,
6454 struct got_object_id *orig_commit_id, struct got_repository *repo)
6456 const struct got_error *err;
6457 char *commit_ref_name;
6458 struct got_reference *commit_ref = NULL;
6459 struct got_object_id *commit_id = NULL;
6461 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6465 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6468 err = got_ref_resolve(&commit_id, repo, commit_ref);
6471 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6472 err = got_error(GOT_ERR_REBASE_COMMITID);
6476 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6477 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6480 got_ref_close(commit_ref);
6481 free(commit_ref_name);
6486 const struct got_error *
6487 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6488 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6489 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6490 struct got_commit_object *orig_commit,
6491 struct got_object_id *orig_commit_id, const char *new_logmsg,
6492 struct got_repository *repo)
6494 const struct got_error *err;
6495 char *commit_ref_name;
6496 struct got_reference *commit_ref = NULL;
6498 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6502 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6506 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6507 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6510 got_ref_close(commit_ref);
6511 free(commit_ref_name);
6515 const struct got_error *
6516 got_worktree_rebase_postpone(struct got_worktree *worktree,
6517 struct got_fileindex *fileindex)
6520 got_fileindex_free(fileindex);
6521 return lock_worktree(worktree, LOCK_SH);
6524 static const struct got_error *
6525 delete_ref(const char *name, struct got_repository *repo)
6527 const struct got_error *err;
6528 struct got_reference *ref;
6530 err = got_ref_open(&ref, repo, name, 0);
6532 if (err->code == GOT_ERR_NOT_REF)
6537 err = got_ref_delete(ref, repo);
6542 static const struct got_error *
6543 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6545 const struct got_error *err;
6546 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6547 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6549 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6552 err = delete_ref(tmp_branch_name, repo);
6556 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6559 err = delete_ref(new_base_branch_ref_name, repo);
6563 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6566 err = delete_ref(branch_ref_name, repo);
6570 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6573 err = delete_ref(commit_ref_name, repo);
6578 free(tmp_branch_name);
6579 free(new_base_branch_ref_name);
6580 free(branch_ref_name);
6581 free(commit_ref_name);
6585 const struct got_error *
6586 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6587 struct got_object_id *new_commit_id, struct got_repository *repo)
6589 const struct got_error *err;
6590 struct got_reference *ref = NULL;
6591 struct got_object_id *old_commit_id = NULL;
6592 const char *branch_name = NULL;
6593 char *new_id_str = NULL;
6594 char *refname = NULL;
6596 branch_name = got_ref_get_name(branch);
6597 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6598 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6601 err = got_object_id_str(&new_id_str, new_commit_id);
6605 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6606 new_id_str) == -1) {
6607 err = got_error_from_errno("asprintf");
6611 err = got_ref_resolve(&old_commit_id, repo, branch);
6615 err = got_ref_alloc(&ref, refname, old_commit_id);
6619 err = got_ref_write(ref, repo);
6623 free(old_commit_id);
6629 const struct got_error *
6630 got_worktree_rebase_complete(struct got_worktree *worktree,
6631 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6632 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6633 struct got_repository *repo, int create_backup)
6635 const struct got_error *err, *unlockerr, *sync_err;
6636 struct got_object_id *new_head_commit_id = NULL;
6637 char *fileindex_path = NULL;
6639 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6643 if (create_backup) {
6644 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6645 rebased_branch, new_head_commit_id, repo);
6650 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6654 err = got_ref_write(rebased_branch, repo);
6658 err = got_worktree_set_head_ref(worktree, rebased_branch);
6662 err = delete_rebase_refs(worktree, repo);
6666 err = get_fileindex_path(&fileindex_path, worktree);
6669 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6670 sync_err = sync_fileindex(fileindex, fileindex_path);
6671 if (sync_err && err == NULL)
6674 got_fileindex_free(fileindex);
6675 free(fileindex_path);
6676 free(new_head_commit_id);
6677 unlockerr = lock_worktree(worktree, LOCK_SH);
6678 if (unlockerr && err == NULL)
6683 const struct got_error *
6684 got_worktree_rebase_abort(struct got_worktree *worktree,
6685 struct got_fileindex *fileindex, struct got_repository *repo,
6686 struct got_reference *new_base_branch,
6687 got_worktree_checkout_cb progress_cb, void *progress_arg)
6689 const struct got_error *err, *unlockerr, *sync_err;
6690 struct got_reference *resolved = NULL;
6691 struct got_object_id *commit_id = NULL;
6692 char *fileindex_path = NULL;
6693 struct revert_file_args rfa;
6694 struct got_object_id *tree_id = NULL;
6696 err = lock_worktree(worktree, LOCK_EX);
6700 err = got_ref_open(&resolved, repo,
6701 got_ref_get_symref_target(new_base_branch), 0);
6705 err = got_worktree_set_head_ref(worktree, resolved);
6710 * XXX commits to the base branch could have happened while
6711 * we were busy rebasing; should we store the original commit ID
6712 * when rebase begins and read it back here?
6714 err = got_ref_resolve(&commit_id, repo, resolved);
6718 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6722 err = got_object_id_by_path(&tree_id, repo,
6723 worktree->base_commit_id, worktree->path_prefix);
6727 err = delete_rebase_refs(worktree, repo);
6731 err = get_fileindex_path(&fileindex_path, worktree);
6735 rfa.worktree = worktree;
6736 rfa.fileindex = fileindex;
6737 rfa.progress_cb = progress_cb;
6738 rfa.progress_arg = progress_arg;
6739 rfa.patch_cb = NULL;
6740 rfa.patch_arg = NULL;
6742 err = worktree_status(worktree, "", fileindex, repo,
6743 revert_file, &rfa, NULL, NULL, 0, 0);
6747 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6748 repo, progress_cb, progress_arg, NULL, NULL);
6750 sync_err = sync_fileindex(fileindex, fileindex_path);
6751 if (sync_err && err == NULL)
6754 got_ref_close(resolved);
6758 got_fileindex_free(fileindex);
6759 free(fileindex_path);
6761 unlockerr = lock_worktree(worktree, LOCK_SH);
6762 if (unlockerr && err == NULL)
6767 const struct got_error *
6768 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6769 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6770 struct got_fileindex **fileindex, struct got_worktree *worktree,
6771 struct got_repository *repo)
6773 const struct got_error *err = NULL;
6774 char *tmp_branch_name = NULL;
6775 char *branch_ref_name = NULL;
6776 char *base_commit_ref_name = NULL;
6777 char *fileindex_path = NULL;
6778 struct check_rebase_ok_arg ok_arg;
6779 struct got_reference *wt_branch = NULL;
6780 struct got_reference *base_commit_ref = NULL;
6784 *base_commit_id = NULL;
6787 err = lock_worktree(worktree, LOCK_EX);
6791 err = open_fileindex(fileindex, &fileindex_path, worktree);
6795 ok_arg.worktree = worktree;
6797 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6802 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6806 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6810 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6815 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6820 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6824 err = got_ref_write(*branch_ref, repo);
6828 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6829 worktree->base_commit_id);
6832 err = got_ref_write(base_commit_ref, repo);
6835 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6836 if (*base_commit_id == NULL) {
6837 err = got_error_from_errno("got_object_id_dup");
6841 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6842 worktree->base_commit_id);
6845 err = got_ref_write(*tmp_branch, repo);
6849 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6853 free(fileindex_path);
6854 free(tmp_branch_name);
6855 free(branch_ref_name);
6856 free(base_commit_ref_name);
6858 got_ref_close(wt_branch);
6861 got_ref_close(*branch_ref);
6865 got_ref_close(*tmp_branch);
6868 free(*base_commit_id);
6870 got_fileindex_free(*fileindex);
6873 lock_worktree(worktree, LOCK_SH);
6878 const struct got_error *
6879 got_worktree_histedit_postpone(struct got_worktree *worktree,
6880 struct got_fileindex *fileindex)
6883 got_fileindex_free(fileindex);
6884 return lock_worktree(worktree, LOCK_SH);
6887 const struct got_error *
6888 got_worktree_histedit_in_progress(int *in_progress,
6889 struct got_worktree *worktree)
6891 const struct got_error *err;
6892 char *tmp_branch_name = NULL;
6894 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6898 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6899 free(tmp_branch_name);
6903 const struct got_error *
6904 got_worktree_histedit_continue(struct got_object_id **commit_id,
6905 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6906 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6907 struct got_worktree *worktree, struct got_repository *repo)
6909 const struct got_error *err;
6910 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6911 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6912 struct got_reference *commit_ref = NULL;
6913 struct got_reference *base_commit_ref = NULL;
6914 char *fileindex_path = NULL;
6915 int have_staged_files = 0;
6919 *base_commit_id = NULL;
6922 err = lock_worktree(worktree, LOCK_EX);
6926 err = open_fileindex(fileindex, &fileindex_path, worktree);
6930 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6931 &have_staged_files);
6932 if (err && err->code != GOT_ERR_CANCELLED)
6934 if (have_staged_files) {
6935 err = got_error(GOT_ERR_STAGED_PATHS);
6939 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6943 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6947 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6951 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6956 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6960 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6963 err = got_ref_resolve(commit_id, repo, commit_ref);
6967 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6970 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6974 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6978 free(commit_ref_name);
6979 free(branch_ref_name);
6980 free(fileindex_path);
6982 got_ref_close(commit_ref);
6983 if (base_commit_ref)
6984 got_ref_close(base_commit_ref);
6988 free(*base_commit_id);
6989 *base_commit_id = NULL;
6991 got_ref_close(*tmp_branch);
6995 got_fileindex_free(*fileindex);
6998 lock_worktree(worktree, LOCK_EX);
7003 static const struct got_error *
7004 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7006 const struct got_error *err;
7007 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7008 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7010 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7013 err = delete_ref(tmp_branch_name, repo);
7017 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7021 err = delete_ref(base_commit_ref_name, repo);
7025 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7028 err = delete_ref(branch_ref_name, repo);
7032 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7035 err = delete_ref(commit_ref_name, repo);
7039 free(tmp_branch_name);
7040 free(base_commit_ref_name);
7041 free(branch_ref_name);
7042 free(commit_ref_name);
7046 const struct got_error *
7047 got_worktree_histedit_abort(struct got_worktree *worktree,
7048 struct got_fileindex *fileindex, struct got_repository *repo,
7049 struct got_reference *branch, struct got_object_id *base_commit_id,
7050 got_worktree_checkout_cb progress_cb, void *progress_arg)
7052 const struct got_error *err, *unlockerr, *sync_err;
7053 struct got_reference *resolved = NULL;
7054 char *fileindex_path = NULL;
7055 struct got_object_id *tree_id = NULL;
7056 struct revert_file_args rfa;
7058 err = lock_worktree(worktree, LOCK_EX);
7062 err = got_ref_open(&resolved, repo,
7063 got_ref_get_symref_target(branch), 0);
7067 err = got_worktree_set_head_ref(worktree, resolved);
7071 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7075 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7076 worktree->path_prefix);
7080 err = delete_histedit_refs(worktree, repo);
7084 err = get_fileindex_path(&fileindex_path, worktree);
7088 rfa.worktree = worktree;
7089 rfa.fileindex = fileindex;
7090 rfa.progress_cb = progress_cb;
7091 rfa.progress_arg = progress_arg;
7092 rfa.patch_cb = NULL;
7093 rfa.patch_arg = NULL;
7095 err = worktree_status(worktree, "", fileindex, repo,
7096 revert_file, &rfa, NULL, NULL, 0, 0);
7100 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7101 repo, progress_cb, progress_arg, NULL, NULL);
7103 sync_err = sync_fileindex(fileindex, fileindex_path);
7104 if (sync_err && err == NULL)
7107 got_ref_close(resolved);
7109 free(fileindex_path);
7111 unlockerr = lock_worktree(worktree, LOCK_SH);
7112 if (unlockerr && err == NULL)
7117 const struct got_error *
7118 got_worktree_histedit_complete(struct got_worktree *worktree,
7119 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7120 struct got_reference *edited_branch, struct got_repository *repo)
7122 const struct got_error *err, *unlockerr, *sync_err;
7123 struct got_object_id *new_head_commit_id = NULL;
7124 struct got_reference *resolved = NULL;
7125 char *fileindex_path = NULL;
7127 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7131 err = got_ref_open(&resolved, repo,
7132 got_ref_get_symref_target(edited_branch), 0);
7136 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7137 resolved, new_head_commit_id, repo);
7141 err = got_ref_change_ref(resolved, new_head_commit_id);
7145 err = got_ref_write(resolved, repo);
7149 err = got_worktree_set_head_ref(worktree, resolved);
7153 err = delete_histedit_refs(worktree, repo);
7157 err = get_fileindex_path(&fileindex_path, worktree);
7160 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7161 sync_err = sync_fileindex(fileindex, fileindex_path);
7162 if (sync_err && err == NULL)
7165 got_fileindex_free(fileindex);
7166 free(fileindex_path);
7167 free(new_head_commit_id);
7168 unlockerr = lock_worktree(worktree, LOCK_SH);
7169 if (unlockerr && err == NULL)
7174 const struct got_error *
7175 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7176 struct got_object_id *commit_id, struct got_repository *repo)
7178 const struct got_error *err;
7179 char *commit_ref_name;
7181 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7185 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7189 err = delete_ref(commit_ref_name, repo);
7191 free(commit_ref_name);
7195 const struct got_error *
7196 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7197 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7198 struct got_worktree *worktree, const char *refname,
7199 struct got_repository *repo)
7201 const struct got_error *err = NULL;
7202 char *fileindex_path = NULL;
7203 struct check_rebase_ok_arg ok_arg;
7207 *base_branch_ref = NULL;
7209 err = lock_worktree(worktree, LOCK_EX);
7213 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7214 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7215 "cannot integrate a branch into itself; "
7216 "update -b or different branch name required");
7220 err = open_fileindex(fileindex, &fileindex_path, worktree);
7224 /* Preconditions are the same as for rebase. */
7225 ok_arg.worktree = worktree;
7227 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7232 err = got_ref_open(branch_ref, repo, refname, 1);
7236 err = got_ref_open(base_branch_ref, repo,
7237 got_worktree_get_head_ref_name(worktree), 1);
7241 got_ref_close(*branch_ref);
7244 if (*base_branch_ref) {
7245 got_ref_close(*base_branch_ref);
7246 *base_branch_ref = NULL;
7249 got_fileindex_free(*fileindex);
7252 lock_worktree(worktree, LOCK_SH);
7257 const struct got_error *
7258 got_worktree_integrate_continue(struct got_worktree *worktree,
7259 struct got_fileindex *fileindex, struct got_repository *repo,
7260 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7261 got_worktree_checkout_cb progress_cb, void *progress_arg,
7262 got_cancel_cb cancel_cb, void *cancel_arg)
7264 const struct got_error *err = NULL, *sync_err, *unlockerr;
7265 char *fileindex_path = NULL;
7266 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7268 err = get_fileindex_path(&fileindex_path, worktree);
7272 err = got_ref_resolve(&commit_id, repo, branch_ref);
7276 err = got_object_id_by_path(&tree_id, repo, commit_id,
7277 worktree->path_prefix);
7281 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7285 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7286 progress_cb, progress_arg, cancel_cb, cancel_arg);
7290 err = got_ref_change_ref(base_branch_ref, commit_id);
7294 err = got_ref_write(base_branch_ref, repo);
7298 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7300 sync_err = sync_fileindex(fileindex, fileindex_path);
7301 if (sync_err && err == NULL)
7305 unlockerr = got_ref_unlock(branch_ref);
7306 if (unlockerr && err == NULL)
7308 got_ref_close(branch_ref);
7310 unlockerr = got_ref_unlock(base_branch_ref);
7311 if (unlockerr && err == NULL)
7313 got_ref_close(base_branch_ref);
7315 got_fileindex_free(fileindex);
7316 free(fileindex_path);
7319 unlockerr = lock_worktree(worktree, LOCK_SH);
7320 if (unlockerr && err == NULL)
7325 const struct got_error *
7326 got_worktree_integrate_abort(struct got_worktree *worktree,
7327 struct got_fileindex *fileindex, struct got_repository *repo,
7328 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7330 const struct got_error *err = NULL, *unlockerr = NULL;
7332 got_fileindex_free(fileindex);
7334 err = lock_worktree(worktree, LOCK_SH);
7336 unlockerr = got_ref_unlock(branch_ref);
7337 if (unlockerr && err == NULL)
7339 got_ref_close(branch_ref);
7341 unlockerr = got_ref_unlock(base_branch_ref);
7342 if (unlockerr && err == NULL)
7344 got_ref_close(base_branch_ref);
7349 struct check_stage_ok_arg {
7350 struct got_object_id *head_commit_id;
7351 struct got_worktree *worktree;
7352 struct got_fileindex *fileindex;
7353 struct got_repository *repo;
7357 const struct got_error *
7358 check_stage_ok(void *arg, unsigned char status,
7359 unsigned char staged_status, const char *relpath,
7360 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7361 struct got_object_id *commit_id, int dirfd, const char *de_name)
7363 struct check_stage_ok_arg *a = arg;
7364 const struct got_error *err = NULL;
7365 struct got_fileindex_entry *ie;
7366 struct got_object_id base_commit_id;
7367 struct got_object_id *base_commit_idp = NULL;
7368 char *in_repo_path = NULL, *p;
7370 if (status == GOT_STATUS_UNVERSIONED ||
7371 status == GOT_STATUS_NO_CHANGE)
7373 if (status == GOT_STATUS_NONEXISTENT)
7374 return got_error_set_errno(ENOENT, relpath);
7376 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7378 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7380 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7381 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7383 return got_error_from_errno("asprintf");
7385 if (got_fileindex_entry_has_commit(ie)) {
7386 memcpy(base_commit_id.sha1, ie->commit_sha1,
7387 SHA1_DIGEST_LENGTH);
7388 base_commit_idp = &base_commit_id;
7391 if (status == GOT_STATUS_CONFLICT) {
7392 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7394 } else if (status != GOT_STATUS_ADD &&
7395 status != GOT_STATUS_MODIFY &&
7396 status != GOT_STATUS_DELETE) {
7397 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7401 a->have_changes = 1;
7406 err = check_out_of_date(p, status, staged_status,
7407 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7408 GOT_ERR_STAGE_OUT_OF_DATE);
7414 struct stage_path_arg {
7415 struct got_worktree *worktree;
7416 struct got_fileindex *fileindex;
7417 struct got_repository *repo;
7418 got_worktree_status_cb status_cb;
7420 got_worktree_patch_cb patch_cb;
7422 int staged_something;
7423 int allow_bad_symlinks;
7426 static const struct got_error *
7427 stage_path(void *arg, unsigned char status,
7428 unsigned char staged_status, const char *relpath,
7429 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7430 struct got_object_id *commit_id, int dirfd, const char *de_name)
7432 struct stage_path_arg *a = arg;
7433 const struct got_error *err = NULL;
7434 struct got_fileindex_entry *ie;
7435 char *ondisk_path = NULL, *path_content = NULL;
7437 struct got_object_id *new_staged_blob_id = NULL;
7440 if (status == GOT_STATUS_UNVERSIONED)
7443 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7445 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7447 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7449 return got_error_from_errno("asprintf");
7452 case GOT_STATUS_ADD:
7453 case GOT_STATUS_MODIFY:
7454 /* XXX could sb.st_mode be passed in by our caller? */
7455 if (lstat(ondisk_path, &sb) == -1) {
7456 err = got_error_from_errno2("lstat", ondisk_path);
7460 if (status == GOT_STATUS_ADD) {
7461 int choice = GOT_PATCH_CHOICE_NONE;
7462 err = (*a->patch_cb)(&choice, a->patch_arg,
7463 status, ie->path, NULL, 1, 1);
7466 if (choice != GOT_PATCH_CHOICE_YES)
7469 err = create_patched_content(&path_content, 0,
7470 staged_blob_id ? staged_blob_id : blob_id,
7471 ondisk_path, dirfd, de_name, ie->path,
7472 a->repo, a->patch_cb, a->patch_arg);
7473 if (err || path_content == NULL)
7477 err = got_object_blob_create(&new_staged_blob_id,
7478 path_content ? path_content : ondisk_path, a->repo);
7481 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7482 SHA1_DIGEST_LENGTH);
7483 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7484 stage = GOT_FILEIDX_STAGE_ADD;
7486 stage = GOT_FILEIDX_STAGE_MODIFY;
7487 got_fileindex_entry_stage_set(ie, stage);
7488 if (S_ISLNK(sb.st_mode)) {
7489 int is_bad_symlink = 0;
7490 if (!a->allow_bad_symlinks) {
7491 char target_path[PATH_MAX];
7493 target_len = readlink(ondisk_path, target_path,
7494 sizeof(target_path));
7495 if (target_len == -1) {
7496 err = got_error_from_errno2("readlink",
7500 err = is_bad_symlink_target(&is_bad_symlink,
7501 target_path, target_len, ondisk_path,
7502 a->worktree->root_path);
7505 if (is_bad_symlink) {
7506 err = got_error_path(ondisk_path,
7507 GOT_ERR_BAD_SYMLINK);
7512 got_fileindex_entry_staged_filetype_set(ie,
7513 GOT_FILEIDX_MODE_BAD_SYMLINK);
7515 got_fileindex_entry_staged_filetype_set(ie,
7516 GOT_FILEIDX_MODE_SYMLINK);
7518 got_fileindex_entry_staged_filetype_set(ie,
7519 GOT_FILEIDX_MODE_REGULAR_FILE);
7521 a->staged_something = 1;
7522 if (a->status_cb == NULL)
7524 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7525 get_staged_status(ie), relpath, blob_id,
7526 new_staged_blob_id, NULL, dirfd, de_name);
7528 case GOT_STATUS_DELETE:
7529 if (staged_status == GOT_STATUS_DELETE)
7532 int choice = GOT_PATCH_CHOICE_NONE;
7533 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7534 ie->path, NULL, 1, 1);
7537 if (choice == GOT_PATCH_CHOICE_NO)
7539 if (choice != GOT_PATCH_CHOICE_YES) {
7540 err = got_error(GOT_ERR_PATCH_CHOICE);
7544 stage = GOT_FILEIDX_STAGE_DELETE;
7545 got_fileindex_entry_stage_set(ie, stage);
7546 a->staged_something = 1;
7547 if (a->status_cb == NULL)
7549 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7550 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7553 case GOT_STATUS_NO_CHANGE:
7555 case GOT_STATUS_CONFLICT:
7556 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7558 case GOT_STATUS_NONEXISTENT:
7559 err = got_error_set_errno(ENOENT, relpath);
7562 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7566 if (path_content && unlink(path_content) == -1 && err == NULL)
7567 err = got_error_from_errno2("unlink", path_content);
7570 free(new_staged_blob_id);
7574 const struct got_error *
7575 got_worktree_stage(struct got_worktree *worktree,
7576 struct got_pathlist_head *paths,
7577 got_worktree_status_cb status_cb, void *status_arg,
7578 got_worktree_patch_cb patch_cb, void *patch_arg,
7579 int allow_bad_symlinks, struct got_repository *repo)
7581 const struct got_error *err = NULL, *sync_err, *unlockerr;
7582 struct got_pathlist_entry *pe;
7583 struct got_fileindex *fileindex = NULL;
7584 char *fileindex_path = NULL;
7585 struct got_reference *head_ref = NULL;
7586 struct got_object_id *head_commit_id = NULL;
7587 struct check_stage_ok_arg oka;
7588 struct stage_path_arg spa;
7590 err = lock_worktree(worktree, LOCK_EX);
7594 err = got_ref_open(&head_ref, repo,
7595 got_worktree_get_head_ref_name(worktree), 0);
7598 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7601 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7605 /* Check pre-conditions before staging anything. */
7606 oka.head_commit_id = head_commit_id;
7607 oka.worktree = worktree;
7608 oka.fileindex = fileindex;
7610 oka.have_changes = 0;
7611 TAILQ_FOREACH(pe, paths, entry) {
7612 err = worktree_status(worktree, pe->path, fileindex, repo,
7613 check_stage_ok, &oka, NULL, NULL, 0, 0);
7617 if (!oka.have_changes) {
7618 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7622 spa.worktree = worktree;
7623 spa.fileindex = fileindex;
7625 spa.patch_cb = patch_cb;
7626 spa.patch_arg = patch_arg;
7627 spa.status_cb = status_cb;
7628 spa.status_arg = status_arg;
7629 spa.staged_something = 0;
7630 spa.allow_bad_symlinks = allow_bad_symlinks;
7631 TAILQ_FOREACH(pe, paths, entry) {
7632 err = worktree_status(worktree, pe->path, fileindex, repo,
7633 stage_path, &spa, NULL, NULL, 0, 0);
7637 if (!spa.staged_something) {
7638 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7642 sync_err = sync_fileindex(fileindex, fileindex_path);
7643 if (sync_err && err == NULL)
7647 got_ref_close(head_ref);
7648 free(head_commit_id);
7649 free(fileindex_path);
7651 got_fileindex_free(fileindex);
7652 unlockerr = lock_worktree(worktree, LOCK_SH);
7653 if (unlockerr && err == NULL)
7658 struct unstage_path_arg {
7659 struct got_worktree *worktree;
7660 struct got_fileindex *fileindex;
7661 struct got_repository *repo;
7662 got_worktree_checkout_cb progress_cb;
7664 got_worktree_patch_cb patch_cb;
7668 static const struct got_error *
7669 create_unstaged_content(char **path_unstaged_content,
7670 char **path_new_staged_content, struct got_object_id *blob_id,
7671 struct got_object_id *staged_blob_id, const char *relpath,
7672 struct got_repository *repo,
7673 got_worktree_patch_cb patch_cb, void *patch_arg)
7675 const struct got_error *err, *free_err;
7676 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7677 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7678 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7679 struct got_diffreg_result *diffreg_result = NULL;
7680 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7681 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7683 *path_unstaged_content = NULL;
7684 *path_new_staged_content = NULL;
7686 err = got_object_id_str(&label1, blob_id);
7689 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7693 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7697 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7701 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7705 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7709 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7713 err = got_diff_files(&diffreg_result, f1, label1, f2,
7714 path2, 3, 0, 1, NULL);
7718 err = got_opentemp_named(path_unstaged_content, &outfile,
7719 "got-unstaged-content");
7722 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7723 "got-new-staged-content");
7727 if (fseek(f1, 0L, SEEK_SET) == -1) {
7728 err = got_ferror(f1, GOT_ERR_IO);
7731 if (fseek(f2, 0L, SEEK_SET) == -1) {
7732 err = got_ferror(f2, GOT_ERR_IO);
7735 /* Count the number of actual changes in the diff result. */
7736 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7737 struct diff_chunk_context cc = {};
7738 diff_chunk_context_load_change(&cc, &nchunks_used,
7739 diffreg_result->result, n, 0);
7742 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7744 err = apply_or_reject_change(&choice, &nchunks_used,
7745 diffreg_result->result, n, relpath, f1, f2,
7746 &line_cur1, &line_cur2,
7747 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7750 if (choice == GOT_PATCH_CHOICE_YES)
7753 have_rejected_content = 1;
7754 if (choice == GOT_PATCH_CHOICE_QUIT)
7757 if (have_content || have_rejected_content)
7758 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7759 outfile, rejectfile);
7763 got_object_blob_close(blob);
7765 got_object_blob_close(staged_blob);
7766 free_err = got_diffreg_result_free(diffreg_result);
7767 if (free_err && err == NULL)
7769 if (f1 && fclose(f1) == EOF && err == NULL)
7770 err = got_error_from_errno2("fclose", path1);
7771 if (f2 && fclose(f2) == EOF && err == NULL)
7772 err = got_error_from_errno2("fclose", path2);
7773 if (outfile && fclose(outfile) == EOF && err == NULL)
7774 err = got_error_from_errno2("fclose", *path_unstaged_content);
7775 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7776 err = got_error_from_errno2("fclose", *path_new_staged_content);
7777 if (path1 && unlink(path1) == -1 && err == NULL)
7778 err = got_error_from_errno2("unlink", path1);
7779 if (path2 && unlink(path2) == -1 && err == NULL)
7780 err = got_error_from_errno2("unlink", path2);
7781 if (err || !have_content) {
7782 if (*path_unstaged_content &&
7783 unlink(*path_unstaged_content) == -1 && err == NULL)
7784 err = got_error_from_errno2("unlink",
7785 *path_unstaged_content);
7786 free(*path_unstaged_content);
7787 *path_unstaged_content = NULL;
7789 if (err || !have_content || !have_rejected_content) {
7790 if (*path_new_staged_content &&
7791 unlink(*path_new_staged_content) == -1 && err == NULL)
7792 err = got_error_from_errno2("unlink",
7793 *path_new_staged_content);
7794 free(*path_new_staged_content);
7795 *path_new_staged_content = NULL;
7802 static const struct got_error *
7803 unstage_hunks(struct got_object_id *staged_blob_id,
7804 struct got_blob_object *blob_base,
7805 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7806 const char *ondisk_path, const char *label_orig,
7807 struct got_worktree *worktree, struct got_repository *repo,
7808 got_worktree_patch_cb patch_cb, void *patch_arg,
7809 got_worktree_checkout_cb progress_cb, void *progress_arg)
7811 const struct got_error *err = NULL;
7812 char *path_unstaged_content = NULL;
7813 char *path_new_staged_content = NULL;
7814 char *parent = NULL, *base_path = NULL;
7815 char *blob_base_path = NULL;
7816 struct got_object_id *new_staged_blob_id = NULL;
7817 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7820 err = create_unstaged_content(&path_unstaged_content,
7821 &path_new_staged_content, blob_id, staged_blob_id,
7822 ie->path, repo, patch_cb, patch_arg);
7826 if (path_unstaged_content == NULL)
7829 if (path_new_staged_content) {
7830 err = got_object_blob_create(&new_staged_blob_id,
7831 path_new_staged_content, repo);
7836 f = fopen(path_unstaged_content, "r");
7838 err = got_error_from_errno2("fopen",
7839 path_unstaged_content);
7842 if (fstat(fileno(f), &sb) == -1) {
7843 err = got_error_from_errno2("fstat", path_unstaged_content);
7846 if (got_fileindex_entry_staged_filetype_get(ie) ==
7847 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7848 char link_target[PATH_MAX];
7850 r = fread(link_target, 1, sizeof(link_target), f);
7851 if (r == 0 && ferror(f)) {
7852 err = got_error_from_errno("fread");
7855 if (r >= sizeof(link_target)) { /* should not happen */
7856 err = got_error(GOT_ERR_NO_SPACE);
7859 link_target[r] = '\0';
7860 err = merge_symlink(worktree, blob_base,
7861 ondisk_path, ie->path, label_orig, link_target,
7862 worktree->base_commit_id, repo, progress_cb,
7865 int local_changes_subsumed;
7867 err = got_path_dirname(&parent, ondisk_path);
7871 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7873 err = got_error_from_errno("asprintf");
7878 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7881 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7887 * In order the run a 3-way merge with a symlink we copy the symlink's
7888 * target path into a temporary file and use that file with diff3.
7890 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7891 err = dump_symlink_target_path_to_file(&f_deriv2,
7897 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7899 err = got_error_from_errno2("open", ondisk_path);
7902 f_deriv2 = fdopen(fd, "r");
7903 if (f_deriv2 == NULL) {
7904 err = got_error_from_errno2("fdopen", ondisk_path);
7910 err = merge_file(&local_changes_subsumed, worktree,
7911 f_base, f, f_deriv2, ondisk_path, ie->path,
7912 got_fileindex_perms_to_st(ie),
7913 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
7914 repo, progress_cb, progress_arg);
7919 if (new_staged_blob_id) {
7920 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7921 SHA1_DIGEST_LENGTH);
7923 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7924 got_fileindex_entry_staged_filetype_set(ie, 0);
7927 free(new_staged_blob_id);
7928 if (path_unstaged_content &&
7929 unlink(path_unstaged_content) == -1 && err == NULL)
7930 err = got_error_from_errno2("unlink", path_unstaged_content);
7931 if (path_new_staged_content &&
7932 unlink(path_new_staged_content) == -1 && err == NULL)
7933 err = got_error_from_errno2("unlink", path_new_staged_content);
7934 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7935 err = got_error_from_errno2("unlink", blob_base_path);
7936 if (f_base && fclose(f_base) == EOF && err == NULL)
7937 err = got_error_from_errno2("fclose", path_unstaged_content);
7938 if (f && fclose(f) == EOF && err == NULL)
7939 err = got_error_from_errno2("fclose", path_unstaged_content);
7940 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7941 err = got_error_from_errno2("fclose", ondisk_path);
7942 free(path_unstaged_content);
7943 free(path_new_staged_content);
7944 free(blob_base_path);
7950 static const struct got_error *
7951 unstage_path(void *arg, unsigned char status,
7952 unsigned char staged_status, const char *relpath,
7953 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7954 struct got_object_id *commit_id, int dirfd, const char *de_name)
7956 const struct got_error *err = NULL;
7957 struct unstage_path_arg *a = arg;
7958 struct got_fileindex_entry *ie;
7959 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7960 char *ondisk_path = NULL;
7961 char *id_str = NULL, *label_orig = NULL;
7962 int local_changes_subsumed;
7965 if (staged_status != GOT_STATUS_ADD &&
7966 staged_status != GOT_STATUS_MODIFY &&
7967 staged_status != GOT_STATUS_DELETE)
7970 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7972 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7974 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7976 return got_error_from_errno("asprintf");
7978 err = got_object_id_str(&id_str,
7979 commit_id ? commit_id : a->worktree->base_commit_id);
7982 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7984 err = got_error_from_errno("asprintf");
7988 switch (staged_status) {
7989 case GOT_STATUS_MODIFY:
7990 err = got_object_open_as_blob(&blob_base, a->repo,
7995 case GOT_STATUS_ADD:
7997 if (staged_status == GOT_STATUS_ADD) {
7998 int choice = GOT_PATCH_CHOICE_NONE;
7999 err = (*a->patch_cb)(&choice, a->patch_arg,
8000 staged_status, ie->path, NULL, 1, 1);
8003 if (choice != GOT_PATCH_CHOICE_YES)
8006 err = unstage_hunks(staged_blob_id,
8007 blob_base, blob_id, ie, ondisk_path,
8008 label_orig, a->worktree, a->repo,
8009 a->patch_cb, a->patch_arg,
8010 a->progress_cb, a->progress_arg);
8011 break; /* Done with this file. */
8014 err = got_object_open_as_blob(&blob_staged, a->repo,
8015 staged_blob_id, 8192);
8018 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8019 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8020 case GOT_FILEIDX_MODE_REGULAR_FILE:
8021 err = merge_blob(&local_changes_subsumed, a->worktree,
8022 blob_base, ondisk_path, relpath,
8023 got_fileindex_perms_to_st(ie), label_orig,
8024 blob_staged, commit_id ? commit_id :
8025 a->worktree->base_commit_id, a->repo,
8026 a->progress_cb, a->progress_arg);
8028 case GOT_FILEIDX_MODE_SYMLINK:
8029 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8030 char *staged_target;
8031 err = got_object_blob_read_to_str(
8032 &staged_target, blob_staged);
8035 err = merge_symlink(a->worktree, blob_base,
8036 ondisk_path, relpath, label_orig,
8037 staged_target, commit_id ? commit_id :
8038 a->worktree->base_commit_id,
8039 a->repo, a->progress_cb, a->progress_arg);
8040 free(staged_target);
8042 err = merge_blob(&local_changes_subsumed,
8043 a->worktree, blob_base, ondisk_path,
8044 relpath, got_fileindex_perms_to_st(ie),
8045 label_orig, blob_staged,
8046 commit_id ? commit_id :
8047 a->worktree->base_commit_id, a->repo,
8048 a->progress_cb, a->progress_arg);
8052 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8056 got_fileindex_entry_stage_set(ie,
8057 GOT_FILEIDX_STAGE_NONE);
8058 got_fileindex_entry_staged_filetype_set(ie, 0);
8061 case GOT_STATUS_DELETE:
8063 int choice = GOT_PATCH_CHOICE_NONE;
8064 err = (*a->patch_cb)(&choice, a->patch_arg,
8065 staged_status, ie->path, NULL, 1, 1);
8068 if (choice == GOT_PATCH_CHOICE_NO)
8070 if (choice != GOT_PATCH_CHOICE_YES) {
8071 err = got_error(GOT_ERR_PATCH_CHOICE);
8075 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8076 got_fileindex_entry_staged_filetype_set(ie, 0);
8077 err = get_file_status(&status, &sb, ie, ondisk_path,
8078 dirfd, de_name, a->repo);
8081 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8087 got_object_blob_close(blob_base);
8089 got_object_blob_close(blob_staged);
8095 const struct got_error *
8096 got_worktree_unstage(struct got_worktree *worktree,
8097 struct got_pathlist_head *paths,
8098 got_worktree_checkout_cb progress_cb, void *progress_arg,
8099 got_worktree_patch_cb patch_cb, void *patch_arg,
8100 struct got_repository *repo)
8102 const struct got_error *err = NULL, *sync_err, *unlockerr;
8103 struct got_pathlist_entry *pe;
8104 struct got_fileindex *fileindex = NULL;
8105 char *fileindex_path = NULL;
8106 struct unstage_path_arg upa;
8108 err = lock_worktree(worktree, LOCK_EX);
8112 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8116 upa.worktree = worktree;
8117 upa.fileindex = fileindex;
8119 upa.progress_cb = progress_cb;
8120 upa.progress_arg = progress_arg;
8121 upa.patch_cb = patch_cb;
8122 upa.patch_arg = patch_arg;
8123 TAILQ_FOREACH(pe, paths, entry) {
8124 err = worktree_status(worktree, pe->path, fileindex, repo,
8125 unstage_path, &upa, NULL, NULL, 0, 0);
8130 sync_err = sync_fileindex(fileindex, fileindex_path);
8131 if (sync_err && err == NULL)
8134 free(fileindex_path);
8136 got_fileindex_free(fileindex);
8137 unlockerr = lock_worktree(worktree, LOCK_SH);
8138 if (unlockerr && err == NULL)
8143 struct report_file_info_arg {
8144 struct got_worktree *worktree;
8145 got_worktree_path_info_cb info_cb;
8147 struct got_pathlist_head *paths;
8148 got_cancel_cb cancel_cb;
8152 static const struct got_error *
8153 report_file_info(void *arg, struct got_fileindex_entry *ie)
8155 struct report_file_info_arg *a = arg;
8156 struct got_pathlist_entry *pe;
8157 struct got_object_id blob_id, staged_blob_id, commit_id;
8158 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8159 struct got_object_id *commit_idp = NULL;
8162 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8163 return got_error(GOT_ERR_CANCELLED);
8165 TAILQ_FOREACH(pe, a->paths, entry) {
8166 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8167 got_path_is_child(ie->path, pe->path, pe->path_len))
8170 if (pe == NULL) /* not found */
8173 if (got_fileindex_entry_has_blob(ie)) {
8174 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8175 blob_idp = &blob_id;
8177 stage = got_fileindex_entry_stage_get(ie);
8178 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8179 stage == GOT_FILEIDX_STAGE_ADD) {
8180 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8181 SHA1_DIGEST_LENGTH);
8182 staged_blob_idp = &staged_blob_id;
8185 if (got_fileindex_entry_has_commit(ie)) {
8186 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8187 commit_idp = &commit_id;
8190 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8191 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8194 const struct got_error *
8195 got_worktree_path_info(struct got_worktree *worktree,
8196 struct got_pathlist_head *paths,
8197 got_worktree_path_info_cb info_cb, void *info_arg,
8198 got_cancel_cb cancel_cb, void *cancel_arg)
8201 const struct got_error *err = NULL, *unlockerr;
8202 struct got_fileindex *fileindex = NULL;
8203 char *fileindex_path = NULL;
8204 struct report_file_info_arg arg;
8206 err = lock_worktree(worktree, LOCK_SH);
8210 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8214 arg.worktree = worktree;
8215 arg.info_cb = info_cb;
8216 arg.info_arg = info_arg;
8218 arg.cancel_cb = cancel_cb;
8219 arg.cancel_arg = cancel_arg;
8220 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8223 free(fileindex_path);
8225 got_fileindex_free(fileindex);
8226 unlockerr = lock_worktree(worktree, LOCK_UN);
8227 if (unlockerr && err == NULL)