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) != 0 && 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 got_repo_close(repo);
443 free(base_commit_id_str);
449 if (*worktree != NULL)
450 got_worktree_close(*worktree);
453 (*worktree)->lockfd = fd;
458 const struct got_error *
459 got_worktree_open(struct got_worktree **worktree, const char *path)
461 const struct got_error *err = NULL;
464 worktree_path = strdup(path);
465 if (worktree_path == NULL)
466 return got_error_from_errno("strdup");
471 err = open_worktree(worktree, worktree_path);
472 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
480 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
482 err = got_path_dirname(&parent_path, worktree_path);
484 if (err->code != GOT_ERR_BAD_PATH) {
491 worktree_path = parent_path;
495 return got_error(GOT_ERR_NOT_WORKTREE);
498 const struct got_error *
499 got_worktree_close(struct got_worktree *worktree)
501 const struct got_error *err = NULL;
502 free(worktree->repo_path);
503 free(worktree->path_prefix);
504 free(worktree->base_commit_id);
505 free(worktree->head_ref_name);
506 if (worktree->lockfd != -1)
507 if (close(worktree->lockfd) != 0)
508 err = got_error_from_errno2("close",
509 got_worktree_get_root_path(worktree));
510 free(worktree->root_path);
511 free(worktree->gotconfig_path);
512 got_gotconfig_free(worktree->gotconfig);
514 close(worktree->root_fd);
519 got_worktree_get_root_path(struct got_worktree *worktree)
521 return worktree->root_path;
525 got_worktree_get_repo_path(struct got_worktree *worktree)
527 return worktree->repo_path;
530 got_worktree_get_path_prefix(struct got_worktree *worktree)
532 return worktree->path_prefix;
535 const struct got_error *
536 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
537 const char *path_prefix)
539 char *absprefix = NULL;
541 if (!got_path_is_absolute(path_prefix)) {
542 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
543 return got_error_from_errno("asprintf");
545 *match = (strcmp(absprefix ? absprefix : path_prefix,
546 worktree->path_prefix) == 0);
552 got_worktree_get_head_ref_name(struct got_worktree *worktree)
554 return worktree->head_ref_name;
557 const struct got_error *
558 got_worktree_set_head_ref(struct got_worktree *worktree,
559 struct got_reference *head_ref)
561 const struct got_error *err = NULL;
562 char *path_got = NULL, *head_ref_name = NULL;
564 if (asprintf(&path_got, "%s/%s", worktree->root_path,
565 GOT_WORKTREE_GOT_DIR) == -1) {
566 err = got_error_from_errno("asprintf");
571 head_ref_name = strdup(got_ref_get_name(head_ref));
572 if (head_ref_name == NULL) {
573 err = got_error_from_errno("strdup");
577 err = write_head_ref(path_got, head_ref);
581 free(worktree->head_ref_name);
582 worktree->head_ref_name = head_ref_name;
590 struct got_object_id *
591 got_worktree_get_base_commit_id(struct got_worktree *worktree)
593 return worktree->base_commit_id;
596 const struct got_error *
597 got_worktree_set_base_commit_id(struct got_worktree *worktree,
598 struct got_repository *repo, struct got_object_id *commit_id)
600 const struct got_error *err;
601 struct got_object *obj = NULL;
603 char *path_got = NULL;
605 if (asprintf(&path_got, "%s/%s", worktree->root_path,
606 GOT_WORKTREE_GOT_DIR) == -1) {
607 err = got_error_from_errno("asprintf");
612 err = got_object_open(&obj, repo, commit_id);
616 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
617 err = got_error(GOT_ERR_OBJ_TYPE);
621 /* Record our base commit. */
622 err = got_object_id_str(&id_str, commit_id);
625 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
629 free(worktree->base_commit_id);
630 worktree->base_commit_id = got_object_id_dup(commit_id);
631 if (worktree->base_commit_id == NULL) {
632 err = got_error_from_errno("got_object_id_dup");
637 got_object_close(obj);
643 const struct got_gotconfig *
644 got_worktree_get_gotconfig(struct got_worktree *worktree)
646 return worktree->gotconfig;
649 static const struct got_error *
650 lock_worktree(struct got_worktree *worktree, int operation)
652 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
653 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
654 : got_error_from_errno2("flock",
655 got_worktree_get_root_path(worktree)));
659 static const struct got_error *
660 add_dir_on_disk(struct got_worktree *worktree, const char *path)
662 const struct got_error *err = NULL;
665 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
666 return got_error_from_errno("asprintf");
668 err = got_path_mkdir(abspath);
669 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
672 if (lstat(abspath, &sb) == -1) {
673 err = got_error_from_errno2("lstat", abspath);
674 } else if (!S_ISDIR(sb.st_mode)) {
675 /* TODO directory is obstructed; do something */
676 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
683 static const struct got_error *
684 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
686 const struct got_error *err = NULL;
689 size_t flen1 = 0, flen2 = 0;
694 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
695 if (flen1 == 0 && ferror(f1)) {
696 err = got_error_from_errno("fread");
699 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
700 if (flen2 == 0 && ferror(f2)) {
701 err = got_error_from_errno("fread");
708 } else if (flen2 == 0) {
712 } else if (flen1 == flen2) {
713 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
726 static const struct got_error *
727 check_files_equal(int *same, const char *f1_path, const char *f2_path)
729 const struct got_error *err = NULL;
732 FILE *f1 = NULL, *f2 = NULL;
736 if (lstat(f1_path, &sb) != 0) {
737 err = got_error_from_errno2("lstat", f1_path);
742 if (lstat(f2_path, &sb) != 0) {
743 err = got_error_from_errno2("lstat", f2_path);
748 if (size1 != size2) {
753 f1 = fopen(f1_path, "r");
755 return got_error_from_errno2("fopen", f1_path);
757 f2 = fopen(f2_path, "r");
759 err = got_error_from_errno2("fopen", f2_path);
763 err = check_file_contents_equal(same, f1, f2);
765 if (f1 && fclose(f1) != 0 && err == NULL)
766 err = got_error_from_errno("fclose");
767 if (f2 && fclose(f2) != 0 && err == NULL)
768 err = got_error_from_errno("fclose");
774 * Perform a 3-way merge where blob_orig acts as the common ancestor,
775 * the file at deriv_path acts as the first derived version, and the
776 * file on disk acts as the second derived version.
778 static const struct got_error *
779 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
780 struct got_blob_object *blob_orig, const char *ondisk_path,
781 const char *path, uint16_t st_mode, const char *deriv_path,
782 const char *label_orig, const char *label_deriv,
783 struct got_repository *repo,
784 got_worktree_checkout_cb progress_cb, void *progress_arg)
786 const struct got_error *err = NULL;
789 char *blob_orig_path = NULL;
790 char *merged_path = NULL, *base_path = NULL;
793 char *symlink_path = NULL;
794 FILE *symlinkf = NULL;
796 *local_changes_subsumed = 0;
798 err = got_path_dirname(&parent, ondisk_path);
802 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
803 err = got_error_from_errno("asprintf");
807 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
812 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
813 err = got_error_from_errno("asprintf");
818 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
822 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
828 * If the file has no blob, this is an "add vs add" conflict,
829 * and we simply use an empty ancestor file to make both files
830 * appear in the merged result in their entirety.
835 * In order the run a 3-way merge with a symlink we copy the symlink's
836 * target path into a temporary file and use that file with diff3.
838 if (S_ISLNK(st_mode)) {
839 char target_path[PATH_MAX];
844 if (asprintf(&base_path, "%s/got-symlink-merge",
846 err = got_error_from_errno("asprintf");
850 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
853 target_len = readlink(ondisk_path, target_path,
854 sizeof(target_path));
855 if (target_len == -1) {
856 err = got_error_from_errno2("readlink", ondisk_path);
859 n = fwrite(target_path, 1, target_len, symlinkf);
860 if (n != target_len) {
861 err = got_ferror(symlinkf, GOT_ERR_IO);
864 if (fflush(symlinkf) == EOF) {
865 err = got_error_from_errno2("fflush", symlink_path);
870 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
871 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
872 label_deriv, label_orig, NULL);
876 err = (*progress_cb)(progress_arg,
877 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
881 if (fsync(merged_fd) != 0) {
882 err = got_error_from_errno("fsync");
886 /* Check if a clean merge has subsumed all local changes. */
887 if (overlapcnt == 0) {
888 err = check_files_equal(local_changes_subsumed, deriv_path,
894 if (fchmod(merged_fd, st_mode) != 0) {
895 err = got_error_from_errno2("fchmod", merged_path);
899 if (rename(merged_path, ondisk_path) != 0) {
900 err = got_error_from_errno3("rename", merged_path,
910 if (unlink(symlink_path) == -1 && err == NULL)
911 err = got_error_from_errno2("unlink", symlink_path);
913 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
914 err = got_error_from_errno2("fclose", symlink_path);
916 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
917 err = got_error_from_errno("close");
918 if (f_orig && fclose(f_orig) != 0 && err == NULL)
919 err = got_error_from_errno("fclose");
922 if (blob_orig_path) {
923 unlink(blob_orig_path);
924 free(blob_orig_path);
930 static const struct got_error *
931 update_symlink(const char *ondisk_path, const char *target_path,
934 /* This is not atomic but matches what 'ln -sf' does. */
935 if (unlink(ondisk_path) == -1)
936 return got_error_from_errno2("unlink", ondisk_path);
937 if (symlink(target_path, ondisk_path) == -1)
938 return got_error_from_errno3("symlink", target_path,
944 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
945 * in the work tree with a file that contains conflict markers and the
946 * conflicting target paths of the original version, a "derived version"
947 * of a symlink from an incoming change, and a local version of the symlink.
949 * The original versions's target path can be NULL if it is not available,
950 * such as if both derived versions added a new symlink at the same path.
952 * The incoming derived symlink target is NULL in case the incoming change
953 * has deleted this symlink.
955 static const struct got_error *
956 install_symlink_conflict(const char *deriv_target,
957 struct got_object_id *deriv_base_commit_id, const char *orig_target,
958 const char *label_orig, const char *local_target, const char *ondisk_path)
960 const struct got_error *err;
961 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
964 err = got_object_id_str(&id_str, deriv_base_commit_id);
966 return got_error_from_errno("asprintf");
968 if (asprintf(&label_deriv, "%s: commit %s",
969 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
970 err = got_error_from_errno("asprintf");
974 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
978 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
979 err = got_error_from_errno2("fchmod", path);
983 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
984 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
985 deriv_target ? deriv_target : "(symlink was deleted)",
986 orig_target ? label_orig : "",
987 orig_target ? "\n" : "",
988 orig_target ? orig_target : "",
989 orig_target ? "\n" : "",
990 GOT_DIFF_CONFLICT_MARKER_SEP,
991 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
992 err = got_error_from_errno2("fprintf", path);
996 if (unlink(ondisk_path) == -1) {
997 err = got_error_from_errno2("unlink", ondisk_path);
1000 if (rename(path, ondisk_path) == -1) {
1001 err = got_error_from_errno3("rename", path, ondisk_path);
1005 if (f != NULL && fclose(f) == EOF && err == NULL)
1006 err = got_error_from_errno2("fclose", path);
1013 /* forward declaration */
1014 static const struct got_error *
1015 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1016 const char *, const char *, uint16_t, const char *,
1017 struct got_blob_object *, struct got_object_id *,
1018 struct got_repository *, got_worktree_checkout_cb, void *);
1021 * Merge a symlink into the work tree, where blob_orig acts as the common
1022 * ancestor, deriv_target is the link target of the first derived version,
1023 * and the symlink on disk acts as the second derived version.
1024 * Assume that contents of both blobs represent symlinks.
1026 static const struct got_error *
1027 merge_symlink(struct got_worktree *worktree,
1028 struct got_blob_object *blob_orig, const char *ondisk_path,
1029 const char *path, const char *label_orig, const char *deriv_target,
1030 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1031 got_worktree_checkout_cb progress_cb, void *progress_arg)
1033 const struct got_error *err = NULL;
1034 char *ancestor_target = NULL;
1036 ssize_t ondisk_len, deriv_len;
1037 char ondisk_target[PATH_MAX];
1038 int have_local_change = 0;
1039 int have_incoming_change = 0;
1041 if (lstat(ondisk_path, &sb) == -1)
1042 return got_error_from_errno2("lstat", ondisk_path);
1044 ondisk_len = readlink(ondisk_path, ondisk_target,
1045 sizeof(ondisk_target));
1046 if (ondisk_len == -1) {
1047 err = got_error_from_errno2("readlink",
1051 ondisk_target[ondisk_len] = '\0';
1054 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1059 if (ancestor_target == NULL ||
1060 (ondisk_len != strlen(ancestor_target) ||
1061 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1062 have_local_change = 1;
1064 deriv_len = strlen(deriv_target);
1065 if (ancestor_target == NULL ||
1066 (deriv_len != strlen(ancestor_target) ||
1067 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1068 have_incoming_change = 1;
1070 if (!have_local_change && !have_incoming_change) {
1071 if (ancestor_target) {
1072 /* Both sides made the same change. */
1073 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1075 } else if (deriv_len == ondisk_len &&
1076 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1077 /* Both sides added the same symlink. */
1078 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1081 /* Both sides added symlinks which don't match. */
1082 err = install_symlink_conflict(deriv_target,
1083 deriv_base_commit_id, ancestor_target,
1084 label_orig, ondisk_target, ondisk_path);
1087 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1090 } else if (!have_local_change && have_incoming_change) {
1091 /* Apply the incoming change. */
1092 err = update_symlink(ondisk_path, deriv_target,
1093 strlen(deriv_target));
1096 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1097 } else if (have_local_change && have_incoming_change) {
1098 if (deriv_len == ondisk_len &&
1099 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1100 /* Both sides made the same change. */
1101 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1104 err = install_symlink_conflict(deriv_target,
1105 deriv_base_commit_id, ancestor_target, label_orig,
1106 ondisk_target, ondisk_path);
1109 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1115 free(ancestor_target);
1120 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1121 * blob_deriv acts as the first derived version, and the file on disk
1122 * acts as the second derived version.
1124 static const struct got_error *
1125 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1126 struct got_blob_object *blob_orig, const char *ondisk_path,
1127 const char *path, uint16_t st_mode, const char *label_orig,
1128 struct got_blob_object *blob_deriv,
1129 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1130 got_worktree_checkout_cb progress_cb, void *progress_arg)
1132 const struct got_error *err = NULL;
1133 FILE *f_deriv = NULL;
1134 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1135 char *label_deriv = NULL, *parent = NULL;
1137 *local_changes_subsumed = 0;
1139 err = got_path_dirname(&parent, ondisk_path);
1144 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1145 err = got_error_from_errno("asprintf");
1150 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1153 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1158 err = got_object_id_str(&id_str, deriv_base_commit_id);
1161 if (asprintf(&label_deriv, "%s: commit %s",
1162 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1163 err = got_error_from_errno("asprintf");
1167 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1168 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1169 label_deriv, repo, progress_cb, progress_arg);
1171 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1172 err = got_error_from_errno("fclose");
1174 if (blob_deriv_path) {
1175 unlink(blob_deriv_path);
1176 free(blob_deriv_path);
1184 static const struct got_error *
1185 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1186 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1187 int wt_fd, const char *path, struct got_object_id *blob_id)
1189 const struct got_error *err = NULL;
1190 struct got_fileindex_entry *new_ie;
1194 err = got_fileindex_entry_alloc(&new_ie, path);
1198 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1199 blob_id->sha1, base_commit_id->sha1, 1);
1203 err = got_fileindex_entry_add(fileindex, new_ie);
1206 got_fileindex_entry_free(new_ie);
1213 get_ondisk_perms(int executable, mode_t st_mode)
1215 mode_t xbits = S_IXUSR;
1218 /* Map read bits to execute bits. */
1219 if (st_mode & S_IRGRP)
1221 if (st_mode & S_IROTH)
1223 return st_mode | xbits;
1226 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1229 /* forward declaration */
1230 static const struct got_error *
1231 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1232 const char *path, mode_t te_mode, mode_t st_mode,
1233 struct got_blob_object *blob, int restoring_missing_file,
1234 int reverting_versioned_file, int installing_bad_symlink,
1235 int path_is_unversioned, struct got_repository *repo,
1236 got_worktree_checkout_cb progress_cb, void *progress_arg);
1239 * This function assumes that the provided symlink target points at a
1240 * safe location in the work tree!
1242 static const struct got_error *
1243 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1246 const struct got_error *err = NULL;
1248 char etarget[PATH_MAX];
1252 * "Bad" symlinks (those pointing outside the work tree or into the
1253 * .got directory) are installed in the work tree as a regular file
1254 * which contains the bad symlink target path.
1255 * The new symlink target has already been checked for safety by our
1256 * caller. If we can successfully open a regular file then we simply
1257 * replace this file with a symlink below.
1259 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1262 return got_error_from_errno2("open", ondisk_path);
1264 /* We are updating an existing on-disk symlink. */
1265 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1267 return got_error_from_errno2("readlink", ondisk_path);
1269 if (elen == target_len &&
1270 memcmp(etarget, target_path, target_len) == 0)
1271 return NULL; /* nothing to do */
1274 err = update_symlink(ondisk_path, target_path, target_len);
1275 if (fd != -1 && close(fd) == -1 && err == NULL)
1276 err = got_error_from_errno2("close", ondisk_path);
1280 static const struct got_error *
1281 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1282 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1284 const struct got_error *err = NULL;
1285 char canonpath[PATH_MAX];
1286 char *path_got = NULL;
1288 *is_bad_symlink = 0;
1290 if (target_len >= sizeof(canonpath)) {
1291 *is_bad_symlink = 1;
1296 * We do not use realpath(3) to resolve the symlink's target
1297 * path because we don't want to resolve symlinks recursively.
1298 * Instead we make the path absolute and then canonicalize it.
1299 * Relative symlink target lookup should begin at the directory
1300 * in which the blob object is being installed.
1302 if (!got_path_is_absolute(target_path)) {
1303 char *abspath, *parent;
1304 err = got_path_dirname(&parent, ondisk_path);
1307 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1309 return got_error_from_errno("asprintf");
1312 if (strlen(abspath) >= sizeof(canonpath)) {
1313 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1317 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1322 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1327 /* Only allow symlinks pointing at paths within the work tree. */
1328 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1329 *is_bad_symlink = 1;
1333 /* Do not allow symlinks pointing into the .got directory. */
1334 if (asprintf(&path_got, "%s/%s", wtroot_path,
1335 GOT_WORKTREE_GOT_DIR) == -1)
1336 return got_error_from_errno("asprintf");
1337 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1338 *is_bad_symlink = 1;
1344 static const struct got_error *
1345 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1346 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1347 int restoring_missing_file, int reverting_versioned_file,
1348 int path_is_unversioned, struct got_repository *repo,
1349 got_worktree_checkout_cb progress_cb, void *progress_arg)
1351 const struct got_error *err = NULL;
1352 char target_path[PATH_MAX];
1353 size_t len, target_len = 0;
1354 char *path_got = NULL;
1355 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1356 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1358 *is_bad_symlink = 0;
1361 * Blob object content specifies the target path of the link.
1362 * If a symbolic link cannot be installed we instead create
1363 * a regular file which contains the link target path stored
1364 * in the blob object.
1367 err = got_object_blob_read_block(&len, blob);
1368 if (len + target_len >= sizeof(target_path)) {
1369 /* Path too long; install as a regular file. */
1370 *is_bad_symlink = 1;
1371 got_object_blob_rewind(blob);
1372 return install_blob(worktree, ondisk_path, path,
1373 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 restoring_missing_file, reverting_versioned_file,
1375 1, path_is_unversioned, repo, progress_cb,
1379 /* Skip blob object header first time around. */
1380 memcpy(target_path + target_len, buf + hdrlen,
1382 target_len += len - hdrlen;
1386 target_path[target_len] = '\0';
1388 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1389 ondisk_path, worktree->root_path);
1393 if (*is_bad_symlink) {
1394 /* install as a regular file */
1395 *is_bad_symlink = 1;
1396 got_object_blob_rewind(blob);
1397 err = install_blob(worktree, ondisk_path, path,
1398 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1399 restoring_missing_file, reverting_versioned_file, 1,
1400 path_is_unversioned, repo, progress_cb, progress_arg);
1404 if (symlink(target_path, ondisk_path) == -1) {
1405 if (errno == EEXIST) {
1406 if (path_is_unversioned) {
1407 err = (*progress_cb)(progress_arg,
1408 GOT_STATUS_UNVERSIONED, path);
1411 err = replace_existing_symlink(ondisk_path,
1412 target_path, target_len);
1416 err = (*progress_cb)(progress_arg,
1417 reverting_versioned_file ?
1418 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1421 goto done; /* Nothing else to do. */
1424 if (errno == ENOENT) {
1426 err = got_path_dirname(&parent, ondisk_path);
1429 err = add_dir_on_disk(worktree, parent);
1434 * Retry, and fall through to error handling
1435 * below if this second attempt fails.
1437 if (symlink(target_path, ondisk_path) != -1) {
1438 err = NULL; /* success */
1443 /* Handle errors from first or second creation attempt. */
1444 if (errno == ENAMETOOLONG) {
1445 /* bad target path; install as a regular file */
1446 *is_bad_symlink = 1;
1447 got_object_blob_rewind(blob);
1448 err = install_blob(worktree, ondisk_path, path,
1449 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1450 restoring_missing_file, reverting_versioned_file, 1,
1451 path_is_unversioned, repo,
1452 progress_cb, progress_arg);
1453 } else if (errno == ENOTDIR) {
1454 err = got_error_path(ondisk_path,
1455 GOT_ERR_FILE_OBSTRUCTED);
1457 err = got_error_from_errno3("symlink",
1458 target_path, ondisk_path);
1460 } else if (progress_cb)
1461 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1462 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1468 static const struct got_error *
1469 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1470 const char *path, mode_t te_mode, mode_t st_mode,
1471 struct got_blob_object *blob, int restoring_missing_file,
1472 int reverting_versioned_file, int installing_bad_symlink,
1473 int path_is_unversioned, struct got_repository *repo,
1474 got_worktree_checkout_cb progress_cb, void *progress_arg)
1476 const struct got_error *err = NULL;
1480 char *tmppath = NULL;
1482 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1483 GOT_DEFAULT_FILE_MODE);
1485 if (errno == ENOENT) {
1487 err = got_path_dirname(&parent, path);
1490 err = add_dir_on_disk(worktree, parent);
1494 fd = open(ondisk_path,
1495 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1496 GOT_DEFAULT_FILE_MODE);
1498 return got_error_from_errno2("open",
1500 } else if (errno == EEXIST) {
1501 if (path_is_unversioned) {
1502 err = (*progress_cb)(progress_arg,
1503 GOT_STATUS_UNVERSIONED, path);
1506 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1507 !S_ISREG(st_mode) && !installing_bad_symlink) {
1508 /* TODO file is obstructed; do something */
1509 err = got_error_path(ondisk_path,
1510 GOT_ERR_FILE_OBSTRUCTED);
1513 err = got_opentemp_named_fd(&tmppath, &fd,
1520 return got_error_from_errno2("open", ondisk_path);
1523 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1524 err = got_error_from_errno2("fchmod",
1525 update ? tmppath : ondisk_path);
1530 if (restoring_missing_file)
1531 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1533 else if (reverting_versioned_file)
1534 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1537 err = (*progress_cb)(progress_arg,
1538 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1543 hdrlen = got_object_blob_get_hdrlen(blob);
1545 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1546 err = got_object_blob_read_block(&len, blob);
1550 /* Skip blob object header first time around. */
1551 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1553 err = got_error_from_errno("write");
1555 } else if (outlen != len - hdrlen) {
1556 err = got_error(GOT_ERR_IO);
1563 if (fsync(fd) != 0) {
1564 err = got_error_from_errno("fsync");
1569 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1570 err = got_error_from_errno2("unlink", ondisk_path);
1573 if (rename(tmppath, ondisk_path) != 0) {
1574 err = got_error_from_errno3("rename", tmppath,
1583 if (fd != -1 && close(fd) != 0 && err == NULL)
1584 err = got_error_from_errno("close");
1585 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1586 err = got_error_from_errno2("unlink", tmppath);
1591 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1592 static const struct got_error *
1593 get_modified_file_content_status(unsigned char *status, FILE *f)
1595 const struct got_error *err = NULL;
1596 const char *markers[3] = {
1597 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1598 GOT_DIFF_CONFLICT_MARKER_SEP,
1599 GOT_DIFF_CONFLICT_MARKER_END
1603 size_t linesize = 0;
1606 while (*status == GOT_STATUS_MODIFY) {
1607 linelen = getline(&line, &linesize, f);
1608 if (linelen == -1) {
1611 err = got_ferror(f, GOT_ERR_IO);
1615 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1616 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1618 *status = GOT_STATUS_CONFLICT;
1629 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1631 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1632 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1636 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1638 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1639 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1640 ie->mtime_sec == sb->st_mtim.tv_sec &&
1641 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1642 ie->size == (sb->st_size & 0xffffffff) &&
1643 !xbit_differs(ie, sb->st_mode));
1646 static unsigned char
1647 get_staged_status(struct got_fileindex_entry *ie)
1649 switch (got_fileindex_entry_stage_get(ie)) {
1650 case GOT_FILEIDX_STAGE_ADD:
1651 return GOT_STATUS_ADD;
1652 case GOT_FILEIDX_STAGE_DELETE:
1653 return GOT_STATUS_DELETE;
1654 case GOT_FILEIDX_STAGE_MODIFY:
1655 return GOT_STATUS_MODIFY;
1657 return GOT_STATUS_NO_CHANGE;
1661 static const struct got_error *
1662 get_symlink_modification_status(unsigned char *status,
1663 struct got_fileindex_entry *ie, const char *abspath,
1664 int dirfd, const char *de_name, struct got_blob_object *blob)
1666 const struct got_error *err = NULL;
1667 char target_path[PATH_MAX];
1668 char etarget[PATH_MAX];
1670 size_t len, target_len = 0;
1671 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1672 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1674 *status = GOT_STATUS_NO_CHANGE;
1676 /* Blob object content specifies the target path of the link. */
1678 err = got_object_blob_read_block(&len, blob);
1681 if (len + target_len >= sizeof(target_path)) {
1683 * Should not happen. The blob contents were OK
1684 * when this symlink was installed.
1686 return got_error(GOT_ERR_NO_SPACE);
1689 /* Skip blob object header first time around. */
1690 memcpy(target_path + target_len, buf + hdrlen,
1692 target_len += len - hdrlen;
1696 target_path[target_len] = '\0';
1699 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1701 return got_error_from_errno2("readlinkat", abspath);
1703 elen = readlink(abspath, etarget, sizeof(etarget));
1705 return got_error_from_errno2("readlink", abspath);
1708 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1709 *status = GOT_STATUS_MODIFY;
1714 static const struct got_error *
1715 get_file_status(unsigned char *status, struct stat *sb,
1716 struct got_fileindex_entry *ie, const char *abspath,
1717 int dirfd, const char *de_name, struct got_repository *repo)
1719 const struct got_error *err = NULL;
1720 struct got_object_id id;
1725 struct got_blob_object *blob = NULL;
1727 unsigned char staged_status = get_staged_status(ie);
1729 *status = GOT_STATUS_NO_CHANGE;
1732 * Whenever the caller provides a directory descriptor and a
1733 * directory entry name for the file, use them! This prevents
1734 * race conditions if filesystem paths change beneath our feet.
1737 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1738 if (errno == ENOENT) {
1739 if (got_fileindex_entry_has_file_on_disk(ie))
1740 *status = GOT_STATUS_MISSING;
1742 *status = GOT_STATUS_DELETE;
1745 err = got_error_from_errno2("fstatat", abspath);
1749 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1750 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1751 return got_error_from_errno2("open", abspath);
1752 else if (fd == -1 && errno == ELOOP) {
1753 if (lstat(abspath, sb) == -1)
1754 return got_error_from_errno2("lstat", abspath);
1755 } else if (fd == -1 || fstat(fd, sb) == -1) {
1756 if (errno == ENOENT) {
1757 if (got_fileindex_entry_has_file_on_disk(ie))
1758 *status = GOT_STATUS_MISSING;
1760 *status = GOT_STATUS_DELETE;
1763 err = got_error_from_errno2("fstat", abspath);
1768 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1769 *status = GOT_STATUS_OBSTRUCTED;
1773 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1774 *status = GOT_STATUS_DELETE;
1776 } else if (!got_fileindex_entry_has_blob(ie) &&
1777 staged_status != GOT_STATUS_ADD) {
1778 *status = GOT_STATUS_ADD;
1782 if (!stat_info_differs(ie, sb))
1785 if (S_ISLNK(sb->st_mode) &&
1786 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1787 *status = GOT_STATUS_MODIFY;
1791 if (staged_status == GOT_STATUS_MODIFY ||
1792 staged_status == GOT_STATUS_ADD)
1793 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1795 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1797 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1801 if (S_ISLNK(sb->st_mode)) {
1802 err = get_symlink_modification_status(status, ie,
1803 abspath, dirfd, de_name, blob);
1808 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1810 err = got_error_from_errno2("openat", abspath);
1815 f = fdopen(fd, "r");
1817 err = got_error_from_errno2("fdopen", abspath);
1821 hdrlen = got_object_blob_get_hdrlen(blob);
1823 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1824 err = got_object_blob_read_block(&blen, blob);
1827 /* Skip length of blob object header first time around. */
1828 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1829 if (flen == 0 && ferror(f)) {
1830 err = got_error_from_errno("fread");
1833 if (blen - hdrlen == 0) {
1835 *status = GOT_STATUS_MODIFY;
1837 } else if (flen == 0) {
1838 if (blen - hdrlen != 0)
1839 *status = GOT_STATUS_MODIFY;
1841 } else if (blen - hdrlen == flen) {
1842 /* Skip blob object header first time around. */
1843 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1844 *status = GOT_STATUS_MODIFY;
1848 *status = GOT_STATUS_MODIFY;
1854 if (*status == GOT_STATUS_MODIFY) {
1856 err = get_modified_file_content_status(status, f);
1857 } else if (xbit_differs(ie, sb->st_mode))
1858 *status = GOT_STATUS_MODE_CHANGE;
1861 got_object_blob_close(blob);
1862 if (f != NULL && fclose(f) == EOF && err == NULL)
1863 err = got_error_from_errno2("fclose", abspath);
1864 if (fd != -1 && close(fd) == -1 && err == NULL)
1865 err = got_error_from_errno2("close", abspath);
1870 * Update timestamps in the file index if a file is unmodified and
1871 * we had to run a full content comparison to find out.
1873 static const struct got_error *
1874 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1875 struct got_fileindex_entry *ie, struct stat *sb)
1877 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1878 return got_fileindex_entry_update(ie, wt_fd, path,
1879 ie->blob_sha1, ie->commit_sha1, 1);
1884 static const struct got_error *
1885 update_blob(struct got_worktree *worktree,
1886 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1887 struct got_tree_entry *te, const char *path,
1888 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1891 const struct got_error *err = NULL;
1892 struct got_blob_object *blob = NULL;
1894 unsigned char status = GOT_STATUS_NO_CHANGE;
1897 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1898 return got_error_from_errno("asprintf");
1901 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1902 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1905 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1909 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1910 sb.st_mode = got_fileindex_perms_to_st(ie);
1912 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1913 status = GOT_STATUS_UNVERSIONED;
1916 if (status == GOT_STATUS_OBSTRUCTED) {
1917 err = (*progress_cb)(progress_arg, status, path);
1920 if (status == GOT_STATUS_CONFLICT) {
1921 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1926 if (ie && status != GOT_STATUS_MISSING &&
1927 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1928 if (got_fileindex_entry_has_commit(ie) &&
1929 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1930 SHA1_DIGEST_LENGTH) == 0) {
1931 err = sync_timestamps(worktree->root_fd,
1932 path, status, ie, &sb);
1935 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1939 if (got_fileindex_entry_has_blob(ie) &&
1940 memcmp(ie->blob_sha1, te->id.sha1,
1941 SHA1_DIGEST_LENGTH) == 0) {
1942 err = sync_timestamps(worktree->root_fd,
1943 path, status, ie, &sb);
1948 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1952 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1953 int update_timestamps;
1954 struct got_blob_object *blob2 = NULL;
1955 char *label_orig = NULL;
1956 if (got_fileindex_entry_has_blob(ie)) {
1957 struct got_object_id id2;
1958 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1959 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1963 if (got_fileindex_entry_has_commit(ie)) {
1964 char id_str[SHA1_DIGEST_STRING_LENGTH];
1965 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1966 sizeof(id_str)) == NULL) {
1967 err = got_error_path(id_str,
1968 GOT_ERR_BAD_OBJ_ID_STR);
1971 if (asprintf(&label_orig, "%s: commit %s",
1972 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1973 err = got_error_from_errno("asprintf");
1977 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1979 err = got_object_blob_read_to_str(&link_target, blob);
1982 err = merge_symlink(worktree, blob2, ondisk_path, path,
1983 label_orig, link_target, worktree->base_commit_id,
1984 repo, progress_cb, progress_arg);
1987 err = merge_blob(&update_timestamps, worktree, blob2,
1988 ondisk_path, path, sb.st_mode, label_orig, blob,
1989 worktree->base_commit_id, repo,
1990 progress_cb, progress_arg);
1994 got_object_blob_close(blob2);
1998 * Do not update timestamps of files with local changes.
1999 * Otherwise, a future status walk would treat them as
2000 * unmodified files again.
2002 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2003 blob->id.sha1, worktree->base_commit_id->sha1,
2005 } else if (status == GOT_STATUS_MODE_CHANGE) {
2006 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2007 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2008 } else if (status == GOT_STATUS_DELETE) {
2009 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2012 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2013 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2017 int is_bad_symlink = 0;
2018 if (S_ISLNK(te->mode)) {
2019 err = install_symlink(&is_bad_symlink, worktree,
2020 ondisk_path, path, blob,
2021 status == GOT_STATUS_MISSING, 0,
2022 status == GOT_STATUS_UNVERSIONED, repo,
2023 progress_cb, progress_arg);
2025 err = install_blob(worktree, ondisk_path, path,
2026 te->mode, sb.st_mode, blob,
2027 status == GOT_STATUS_MISSING, 0, 0,
2028 status == GOT_STATUS_UNVERSIONED, repo,
2029 progress_cb, progress_arg);
2035 err = got_fileindex_entry_update(ie,
2036 worktree->root_fd, path, blob->id.sha1,
2037 worktree->base_commit_id->sha1, 1);
2039 err = create_fileindex_entry(&ie, fileindex,
2040 worktree->base_commit_id, worktree->root_fd, path,
2046 if (is_bad_symlink) {
2047 got_fileindex_entry_filetype_set(ie,
2048 GOT_FILEIDX_MODE_BAD_SYMLINK);
2051 got_object_blob_close(blob);
2057 static const struct got_error *
2058 remove_ondisk_file(const char *root_path, const char *path)
2060 const struct got_error *err = NULL;
2061 char *ondisk_path = NULL;
2063 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2064 return got_error_from_errno("asprintf");
2066 if (unlink(ondisk_path) == -1) {
2067 if (errno != ENOENT)
2068 err = got_error_from_errno2("unlink", ondisk_path);
2070 size_t root_len = strlen(root_path);
2073 err = got_path_dirname(&parent, ondisk_path);
2077 ondisk_path = parent;
2078 if (rmdir(ondisk_path) == -1) {
2079 if (errno != ENOTEMPTY)
2080 err = got_error_from_errno2("rmdir",
2084 } while (got_path_cmp(ondisk_path, root_path,
2085 strlen(ondisk_path), root_len) != 0);
2091 static const struct got_error *
2092 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2093 struct got_fileindex_entry *ie, struct got_repository *repo,
2094 got_worktree_checkout_cb progress_cb, void *progress_arg)
2096 const struct got_error *err = NULL;
2097 unsigned char status;
2101 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2102 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2104 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2106 return got_error_from_errno("asprintf");
2108 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2112 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2113 char ondisk_target[PATH_MAX];
2114 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2115 sizeof(ondisk_target));
2116 if (ondisk_len == -1) {
2117 err = got_error_from_errno2("readlink", ondisk_path);
2120 ondisk_target[ondisk_len] = '\0';
2121 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2122 NULL, NULL, /* XXX pass common ancestor info? */
2123 ondisk_target, ondisk_path);
2126 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2131 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2132 status == GOT_STATUS_ADD) {
2133 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2137 * Preserve the working file and change the deleted blob's
2138 * entry into a schedule-add entry.
2140 err = got_fileindex_entry_update(ie, worktree->root_fd,
2141 ie->path, NULL, NULL, 0);
2143 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2146 if (status == GOT_STATUS_NO_CHANGE) {
2147 err = remove_ondisk_file(worktree->root_path, ie->path);
2151 got_fileindex_entry_remove(fileindex, ie);
2158 struct diff_cb_arg {
2159 struct got_fileindex *fileindex;
2160 struct got_worktree *worktree;
2161 struct got_repository *repo;
2162 got_worktree_checkout_cb progress_cb;
2164 got_cancel_cb cancel_cb;
2168 static const struct got_error *
2169 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2170 struct got_tree_entry *te, const char *parent_path)
2172 struct diff_cb_arg *a = arg;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 return update_blob(a->worktree, a->fileindex, ie, te,
2178 ie->path, a->repo, a->progress_cb, a->progress_arg);
2181 static const struct got_error *
2182 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2184 struct diff_cb_arg *a = arg;
2186 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2187 return got_error(GOT_ERR_CANCELLED);
2189 return delete_blob(a->worktree, a->fileindex, ie,
2190 a->repo, a->progress_cb, a->progress_arg);
2193 static const struct got_error *
2194 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2196 struct diff_cb_arg *a = arg;
2197 const struct got_error *err;
2200 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2201 return got_error(GOT_ERR_CANCELLED);
2203 if (got_object_tree_entry_is_submodule(te))
2206 if (asprintf(&path, "%s%s%s", parent_path,
2207 parent_path[0] ? "/" : "", te->name)
2209 return got_error_from_errno("asprintf");
2211 if (S_ISDIR(te->mode))
2212 err = add_dir_on_disk(a->worktree, path);
2214 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2215 a->repo, a->progress_cb, a->progress_arg);
2221 const struct got_error *
2222 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2224 uint32_t uuid_status;
2226 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2227 if (uuid_status != uuid_s_ok) {
2229 return got_error_uuid(uuid_status, "uuid_to_string");
2235 static const struct got_error *
2236 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2238 const struct got_error *err = NULL;
2239 char *uuidstr = NULL;
2243 err = got_worktree_get_uuid(&uuidstr, worktree);
2247 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2248 err = got_error_from_errno("asprintf");
2255 const struct got_error *
2256 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2261 static const struct got_error *
2262 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2264 return get_ref_name(refname, worktree,
2265 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2268 static const struct got_error *
2269 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2271 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2274 static const struct got_error *
2275 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2277 return get_ref_name(refname, worktree,
2278 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2281 static const struct got_error *
2282 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2284 return get_ref_name(refname, worktree,
2285 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2288 static const struct got_error *
2289 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2291 return get_ref_name(refname, worktree,
2292 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2295 static const struct got_error *
2296 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2298 return get_ref_name(refname, worktree,
2299 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2302 static const struct got_error *
2303 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2305 return get_ref_name(refname, worktree,
2306 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2309 static const struct got_error *
2310 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2312 return get_ref_name(refname, worktree,
2313 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2316 const struct got_error *
2317 got_worktree_get_histedit_script_path(char **path,
2318 struct got_worktree *worktree)
2320 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2321 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2323 return got_error_from_errno("asprintf");
2329 * Prevent Git's garbage collector from deleting our base commit by
2330 * setting a reference to our base commit's ID.
2332 static const struct got_error *
2333 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2335 const struct got_error *err = NULL;
2336 struct got_reference *ref = NULL;
2339 err = got_worktree_get_base_ref_name(&refname, worktree);
2343 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2347 err = got_ref_write(ref, repo);
2355 static const struct got_error *
2356 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2358 const struct got_error *err = NULL;
2360 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2361 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2362 err = got_error_from_errno("asprintf");
2363 *fileindex_path = NULL;
2369 static const struct got_error *
2370 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2371 struct got_worktree *worktree)
2373 const struct got_error *err = NULL;
2376 *fileindex_path = NULL;
2377 *fileindex = got_fileindex_alloc();
2378 if (*fileindex == NULL)
2379 return got_error_from_errno("got_fileindex_alloc");
2381 err = get_fileindex_path(fileindex_path, worktree);
2385 index = fopen(*fileindex_path, "rb");
2386 if (index == NULL) {
2387 if (errno != ENOENT)
2388 err = got_error_from_errno2("fopen", *fileindex_path);
2390 err = got_fileindex_read(*fileindex, index);
2391 if (fclose(index) != 0 && err == NULL)
2392 err = got_error_from_errno("fclose");
2396 free(*fileindex_path);
2397 *fileindex_path = NULL;
2398 got_fileindex_free(*fileindex);
2404 struct bump_base_commit_id_arg {
2405 struct got_object_id *base_commit_id;
2408 const char *entry_name;
2409 got_worktree_checkout_cb progress_cb;
2413 /* Bump base commit ID of all files within an updated part of the work tree. */
2414 static const struct got_error *
2415 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2417 const struct got_error *err;
2418 struct bump_base_commit_id_arg *a = arg;
2420 if (a->entry_name) {
2421 if (strcmp(ie->path, a->path) != 0)
2423 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2426 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2427 SHA1_DIGEST_LENGTH) == 0)
2430 if (a->progress_cb) {
2431 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2436 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2440 static const struct got_error *
2441 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2442 struct got_fileindex *fileindex,
2443 got_worktree_checkout_cb progress_cb, void *progress_arg)
2445 struct bump_base_commit_id_arg bbc_arg;
2447 bbc_arg.base_commit_id = worktree->base_commit_id;
2448 bbc_arg.entry_name = NULL;
2450 bbc_arg.path_len = 0;
2451 bbc_arg.progress_cb = progress_cb;
2452 bbc_arg.progress_arg = progress_arg;
2454 return got_fileindex_for_each_entry_safe(fileindex,
2455 bump_base_commit_id, &bbc_arg);
2458 static const struct got_error *
2459 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2461 const struct got_error *err = NULL;
2462 char *new_fileindex_path = NULL;
2463 FILE *new_index = NULL;
2464 struct timespec timeout;
2466 err = got_opentemp_named(&new_fileindex_path, &new_index,
2471 err = got_fileindex_write(fileindex, new_index);
2475 if (rename(new_fileindex_path, fileindex_path) != 0) {
2476 err = got_error_from_errno3("rename", new_fileindex_path,
2478 unlink(new_fileindex_path);
2482 * Sleep for a short amount of time to ensure that files modified after
2483 * this program exits have a different time stamp from the one which
2484 * was recorded in the file index.
2487 timeout.tv_nsec = 1;
2488 nanosleep(&timeout, NULL);
2492 free(new_fileindex_path);
2496 static const struct got_error *
2497 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2498 struct got_object_id **tree_id, const char *wt_relpath,
2499 struct got_worktree *worktree, struct got_repository *repo)
2501 const struct got_error *err = NULL;
2502 struct got_object_id *id = NULL;
2503 char *in_repo_path = NULL;
2504 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2506 *entry_type = GOT_OBJ_TYPE_ANY;
2507 *tree_relpath = NULL;
2510 if (wt_relpath[0] == '\0') {
2511 /* Check out all files within the work tree. */
2512 *entry_type = GOT_OBJ_TYPE_TREE;
2513 *tree_relpath = strdup("");
2514 if (*tree_relpath == NULL) {
2515 err = got_error_from_errno("strdup");
2518 err = got_object_id_by_path(tree_id, repo,
2519 worktree->base_commit_id, worktree->path_prefix);
2525 /* Check out a subset of files in the work tree. */
2527 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2528 is_root_wt ? "" : "/", wt_relpath) == -1) {
2529 err = got_error_from_errno("asprintf");
2533 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2539 in_repo_path = NULL;
2541 err = got_object_get_type(entry_type, repo, id);
2545 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2546 /* Check out a single file. */
2547 if (strchr(wt_relpath, '/') == NULL) {
2548 /* Check out a single file in work tree's root dir. */
2549 in_repo_path = strdup(worktree->path_prefix);
2550 if (in_repo_path == NULL) {
2551 err = got_error_from_errno("strdup");
2554 *tree_relpath = strdup("");
2555 if (*tree_relpath == NULL) {
2556 err = got_error_from_errno("strdup");
2560 /* Check out a single file in a subdirectory. */
2561 err = got_path_dirname(tree_relpath, wt_relpath);
2564 if (asprintf(&in_repo_path, "%s%s%s",
2565 worktree->path_prefix, is_root_wt ? "" : "/",
2566 *tree_relpath) == -1) {
2567 err = got_error_from_errno("asprintf");
2571 err = got_object_id_by_path(tree_id, repo,
2572 worktree->base_commit_id, in_repo_path);
2574 /* Check out all files within a subdirectory. */
2575 *tree_id = got_object_id_dup(id);
2576 if (*tree_id == NULL) {
2577 err = got_error_from_errno("got_object_id_dup");
2580 *tree_relpath = strdup(wt_relpath);
2581 if (*tree_relpath == NULL) {
2582 err = got_error_from_errno("strdup");
2590 *entry_type = GOT_OBJ_TYPE_ANY;
2591 free(*tree_relpath);
2592 *tree_relpath = NULL;
2599 static const struct got_error *
2600 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2601 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2602 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2603 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2605 const struct got_error *err = NULL;
2606 struct got_commit_object *commit = NULL;
2607 struct got_tree_object *tree = NULL;
2608 struct got_fileindex_diff_tree_cb diff_cb;
2609 struct diff_cb_arg arg;
2611 err = ref_base_commit(worktree, repo);
2613 if (!(err->code == GOT_ERR_ERRNO &&
2614 (errno == EACCES || errno == EROFS)))
2616 err = (*progress_cb)(progress_arg,
2617 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2622 err = got_object_open_as_commit(&commit, repo,
2623 worktree->base_commit_id);
2627 err = got_object_open_as_tree(&tree, repo, tree_id);
2632 got_object_tree_find_entry(tree, entry_name) == NULL) {
2633 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2637 diff_cb.diff_old_new = diff_old_new;
2638 diff_cb.diff_old = diff_old;
2639 diff_cb.diff_new = diff_new;
2640 arg.fileindex = fileindex;
2641 arg.worktree = worktree;
2643 arg.progress_cb = progress_cb;
2644 arg.progress_arg = progress_arg;
2645 arg.cancel_cb = cancel_cb;
2646 arg.cancel_arg = cancel_arg;
2647 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2648 entry_name, repo, &diff_cb, &arg);
2651 got_object_tree_close(tree);
2653 got_object_commit_close(commit);
2657 const struct got_error *
2658 got_worktree_checkout_files(struct got_worktree *worktree,
2659 struct got_pathlist_head *paths, struct got_repository *repo,
2660 got_worktree_checkout_cb progress_cb, void *progress_arg,
2661 got_cancel_cb cancel_cb, void *cancel_arg)
2663 const struct got_error *err = NULL, *sync_err, *unlockerr;
2664 struct got_commit_object *commit = NULL;
2665 struct got_tree_object *tree = NULL;
2666 struct got_fileindex *fileindex = NULL;
2667 char *fileindex_path = NULL;
2668 struct got_pathlist_entry *pe;
2669 struct tree_path_data {
2670 SIMPLEQ_ENTRY(tree_path_data) entry;
2671 struct got_object_id *tree_id;
2676 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2678 SIMPLEQ_INIT(&tree_paths);
2680 err = lock_worktree(worktree, LOCK_EX);
2684 /* Map all specified paths to in-repository trees. */
2685 TAILQ_FOREACH(pe, paths, entry) {
2686 tpd = malloc(sizeof(*tpd));
2688 err = got_error_from_errno("malloc");
2692 err = find_tree_entry_for_checkout(&tpd->entry_type,
2693 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2699 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2700 err = got_path_basename(&tpd->entry_name, pe->path);
2708 tpd->entry_name = NULL;
2710 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2714 * Read the file index.
2715 * Checking out files is supposed to be an idempotent operation.
2716 * If the on-disk file index is incomplete we will try to complete it.
2718 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2722 tpd = SIMPLEQ_FIRST(&tree_paths);
2723 TAILQ_FOREACH(pe, paths, entry) {
2724 struct bump_base_commit_id_arg bbc_arg;
2726 err = checkout_files(worktree, fileindex, tpd->relpath,
2727 tpd->tree_id, tpd->entry_name, repo,
2728 progress_cb, progress_arg, cancel_cb, cancel_arg);
2732 bbc_arg.base_commit_id = worktree->base_commit_id;
2733 bbc_arg.entry_name = tpd->entry_name;
2734 bbc_arg.path = pe->path;
2735 bbc_arg.path_len = pe->path_len;
2736 bbc_arg.progress_cb = progress_cb;
2737 bbc_arg.progress_arg = progress_arg;
2738 err = got_fileindex_for_each_entry_safe(fileindex,
2739 bump_base_commit_id, &bbc_arg);
2743 tpd = SIMPLEQ_NEXT(tpd, entry);
2745 sync_err = sync_fileindex(fileindex, fileindex_path);
2746 if (sync_err && err == NULL)
2749 free(fileindex_path);
2751 got_object_tree_close(tree);
2753 got_object_commit_close(commit);
2755 got_fileindex_free(fileindex);
2756 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2757 tpd = SIMPLEQ_FIRST(&tree_paths);
2758 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2763 unlockerr = lock_worktree(worktree, LOCK_SH);
2764 if (unlockerr && err == NULL)
2769 struct merge_file_cb_arg {
2770 struct got_worktree *worktree;
2771 struct got_fileindex *fileindex;
2772 got_worktree_checkout_cb progress_cb;
2774 got_cancel_cb cancel_cb;
2776 const char *label_orig;
2777 struct got_object_id *commit_id2;
2780 static const struct got_error *
2781 merge_file_cb(void *arg, struct got_blob_object *blob1,
2782 struct got_blob_object *blob2, struct got_object_id *id1,
2783 struct got_object_id *id2, const char *path1, const char *path2,
2784 mode_t mode1, mode_t mode2, struct got_repository *repo)
2786 static const struct got_error *err = NULL;
2787 struct merge_file_cb_arg *a = arg;
2788 struct got_fileindex_entry *ie;
2789 char *ondisk_path = NULL;
2791 unsigned char status;
2792 int local_changes_subsumed;
2794 if (blob1 && blob2) {
2795 ie = got_fileindex_entry_get(a->fileindex, path2,
2798 return (*a->progress_cb)(a->progress_arg,
2799 GOT_STATUS_MISSING, path2);
2801 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2803 return got_error_from_errno("asprintf");
2805 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2810 if (status == GOT_STATUS_DELETE) {
2811 err = (*a->progress_cb)(a->progress_arg,
2812 GOT_STATUS_MERGE, path2);
2815 if (status != GOT_STATUS_NO_CHANGE &&
2816 status != GOT_STATUS_MODIFY &&
2817 status != GOT_STATUS_CONFLICT &&
2818 status != GOT_STATUS_ADD) {
2819 err = (*a->progress_cb)(a->progress_arg, status, path2);
2823 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2825 err = got_object_blob_read_to_str(&link_target2, blob2);
2828 err = merge_symlink(a->worktree, blob1, ondisk_path,
2829 path2, a->label_orig, link_target2, a->commit_id2,
2830 repo, a->progress_cb, a->progress_arg);
2833 err = merge_blob(&local_changes_subsumed, a->worktree,
2834 blob1, ondisk_path, path2, sb.st_mode,
2835 a->label_orig, blob2, a->commit_id2, repo,
2836 a->progress_cb, a->progress_arg);
2839 ie = got_fileindex_entry_get(a->fileindex, path1,
2842 return (*a->progress_cb)(a->progress_arg,
2843 GOT_STATUS_MISSING, path1);
2845 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2847 return got_error_from_errno("asprintf");
2849 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2855 case GOT_STATUS_NO_CHANGE:
2856 err = (*a->progress_cb)(a->progress_arg,
2857 GOT_STATUS_DELETE, path1);
2860 err = remove_ondisk_file(a->worktree->root_path, path1);
2864 got_fileindex_entry_mark_deleted_from_disk(ie);
2866 case GOT_STATUS_DELETE:
2867 case GOT_STATUS_MISSING:
2868 err = (*a->progress_cb)(a->progress_arg,
2869 GOT_STATUS_DELETE, path1);
2873 got_fileindex_entry_mark_deleted_from_disk(ie);
2875 case GOT_STATUS_ADD: {
2876 struct got_object_id *id;
2879 * Delete the added file only if its content already
2880 * exists in the repository.
2882 err = got_object_blob_file_create(&id, &blob1_f, path1);
2885 if (got_object_id_cmp(id, id1) == 0) {
2886 err = (*a->progress_cb)(a->progress_arg,
2887 GOT_STATUS_DELETE, path1);
2890 err = remove_ondisk_file(a->worktree->root_path,
2895 got_fileindex_entry_remove(a->fileindex,
2898 err = (*a->progress_cb)(a->progress_arg,
2899 GOT_STATUS_CANNOT_DELETE, path1);
2901 if (fclose(blob1_f) == EOF && err == NULL)
2902 err = got_error_from_errno("fclose");
2908 case GOT_STATUS_MODIFY:
2909 case GOT_STATUS_CONFLICT:
2910 err = (*a->progress_cb)(a->progress_arg,
2911 GOT_STATUS_CANNOT_DELETE, path1);
2915 case GOT_STATUS_OBSTRUCTED:
2916 err = (*a->progress_cb)(a->progress_arg, status, path1);
2924 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2926 return got_error_from_errno("asprintf");
2927 ie = got_fileindex_entry_get(a->fileindex, path2,
2930 err = get_file_status(&status, &sb, ie, ondisk_path,
2934 if (status != GOT_STATUS_NO_CHANGE &&
2935 status != GOT_STATUS_MODIFY &&
2936 status != GOT_STATUS_CONFLICT &&
2937 status != GOT_STATUS_ADD) {
2938 err = (*a->progress_cb)(a->progress_arg,
2942 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2944 err = got_object_blob_read_to_str(&link_target2,
2948 err = merge_symlink(a->worktree, NULL,
2949 ondisk_path, path2, a->label_orig,
2950 link_target2, a->commit_id2, repo,
2951 a->progress_cb, a->progress_arg);
2953 } else if (S_ISREG(sb.st_mode)) {
2954 err = merge_blob(&local_changes_subsumed,
2955 a->worktree, NULL, ondisk_path, path2,
2956 sb.st_mode, a->label_orig, blob2,
2957 a->commit_id2, repo, a->progress_cb,
2960 err = got_error_path(ondisk_path,
2961 GOT_ERR_FILE_OBSTRUCTED);
2965 if (status == GOT_STATUS_DELETE) {
2966 err = got_fileindex_entry_update(ie,
2967 a->worktree->root_fd, path2, blob2->id.sha1,
2968 a->worktree->base_commit_id->sha1, 0);
2973 int is_bad_symlink = 0;
2974 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2975 if (S_ISLNK(mode2)) {
2976 err = install_symlink(&is_bad_symlink,
2977 a->worktree, ondisk_path, path2, blob2, 0,
2978 0, 1, repo, a->progress_cb, a->progress_arg);
2980 err = install_blob(a->worktree, ondisk_path, path2,
2981 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2982 a->progress_cb, a->progress_arg);
2986 err = got_fileindex_entry_alloc(&ie, path2);
2989 err = got_fileindex_entry_update(ie,
2990 a->worktree->root_fd, path2, NULL, NULL, 1);
2992 got_fileindex_entry_free(ie);
2995 err = got_fileindex_entry_add(a->fileindex, ie);
2997 got_fileindex_entry_free(ie);
3000 if (is_bad_symlink) {
3001 got_fileindex_entry_filetype_set(ie,
3002 GOT_FILEIDX_MODE_BAD_SYMLINK);
3011 struct check_merge_ok_arg {
3012 struct got_worktree *worktree;
3013 struct got_repository *repo;
3016 static const struct got_error *
3017 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3019 const struct got_error *err = NULL;
3020 struct check_merge_ok_arg *a = arg;
3021 unsigned char status;
3025 /* Reject merges into a work tree with mixed base commits. */
3026 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3027 SHA1_DIGEST_LENGTH))
3028 return got_error(GOT_ERR_MIXED_COMMITS);
3030 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3032 return got_error_from_errno("asprintf");
3034 /* Reject merges into a work tree with conflicted files. */
3035 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3038 if (status == GOT_STATUS_CONFLICT)
3039 return got_error(GOT_ERR_CONFLICTS);
3044 static const struct got_error *
3045 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3046 const char *fileindex_path, struct got_object_id *commit_id1,
3047 struct got_object_id *commit_id2, struct got_repository *repo,
3048 got_worktree_checkout_cb progress_cb, void *progress_arg,
3049 got_cancel_cb cancel_cb, void *cancel_arg)
3051 const struct got_error *err = NULL, *sync_err;
3052 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3053 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3054 struct merge_file_cb_arg arg;
3055 char *label_orig = NULL;
3058 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3059 worktree->path_prefix);
3060 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3066 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3070 err = got_object_id_str(&id_str, commit_id1);
3074 if (asprintf(&label_orig, "%s: commit %s",
3075 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3076 err = got_error_from_errno("asprintf");
3083 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3084 worktree->path_prefix);
3088 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3092 arg.worktree = worktree;
3093 arg.fileindex = fileindex;
3094 arg.progress_cb = progress_cb;
3095 arg.progress_arg = progress_arg;
3096 arg.cancel_cb = cancel_cb;
3097 arg.cancel_arg = cancel_arg;
3098 arg.label_orig = label_orig;
3099 arg.commit_id2 = commit_id2;
3100 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3101 sync_err = sync_fileindex(fileindex, fileindex_path);
3102 if (sync_err && err == NULL)
3106 got_object_tree_close(tree1);
3108 got_object_tree_close(tree2);
3113 const struct got_error *
3114 got_worktree_merge_files(struct got_worktree *worktree,
3115 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3116 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3117 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3119 const struct got_error *err, *unlockerr;
3120 char *fileindex_path = NULL;
3121 struct got_fileindex *fileindex = NULL;
3122 struct check_merge_ok_arg mok_arg;
3124 err = lock_worktree(worktree, LOCK_EX);
3128 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3132 mok_arg.worktree = worktree;
3133 mok_arg.repo = repo;
3134 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3139 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3140 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3143 got_fileindex_free(fileindex);
3144 free(fileindex_path);
3145 unlockerr = lock_worktree(worktree, LOCK_SH);
3146 if (unlockerr && err == NULL)
3151 struct diff_dir_cb_arg {
3152 struct got_fileindex *fileindex;
3153 struct got_worktree *worktree;
3154 const char *status_path;
3155 size_t status_path_len;
3156 struct got_repository *repo;
3157 got_worktree_status_cb status_cb;
3159 got_cancel_cb cancel_cb;
3161 /* A pathlist containing per-directory pathlists of ignore patterns. */
3162 struct got_pathlist_head ignores;
3163 int report_unchanged;
3167 static const struct got_error *
3168 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3169 int dirfd, const char *de_name,
3170 got_worktree_status_cb status_cb, void *status_arg,
3171 struct got_repository *repo, int report_unchanged)
3173 const struct got_error *err = NULL;
3174 unsigned char status = GOT_STATUS_NO_CHANGE;
3175 unsigned char staged_status = get_staged_status(ie);
3177 struct got_object_id blob_id, commit_id, staged_blob_id;
3178 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3179 struct got_object_id *staged_blob_idp = NULL;
3181 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3185 if (status == GOT_STATUS_NO_CHANGE &&
3186 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3189 if (got_fileindex_entry_has_blob(ie)) {
3190 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3191 blob_idp = &blob_id;
3193 if (got_fileindex_entry_has_commit(ie)) {
3194 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3195 commit_idp = &commit_id;
3197 if (staged_status == GOT_STATUS_ADD ||
3198 staged_status == GOT_STATUS_MODIFY) {
3199 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3200 SHA1_DIGEST_LENGTH);
3201 staged_blob_idp = &staged_blob_id;
3204 return (*status_cb)(status_arg, status, staged_status,
3205 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3208 static const struct got_error *
3209 status_old_new(void *arg, struct got_fileindex_entry *ie,
3210 struct dirent *de, const char *parent_path, int dirfd)
3212 const struct got_error *err = NULL;
3213 struct diff_dir_cb_arg *a = arg;
3216 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3217 return got_error(GOT_ERR_CANCELLED);
3219 if (got_path_cmp(parent_path, a->status_path,
3220 strlen(parent_path), a->status_path_len) != 0 &&
3221 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3224 if (parent_path[0]) {
3225 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3226 parent_path, de->d_name) == -1)
3227 return got_error_from_errno("asprintf");
3229 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3231 return got_error_from_errno("asprintf");
3234 err = report_file_status(ie, abspath, dirfd, de->d_name,
3235 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3240 static const struct got_error *
3241 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3243 struct diff_dir_cb_arg *a = arg;
3244 struct got_object_id blob_id, commit_id;
3245 unsigned char status;
3247 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3248 return got_error(GOT_ERR_CANCELLED);
3250 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3253 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3254 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3255 if (got_fileindex_entry_has_file_on_disk(ie))
3256 status = GOT_STATUS_MISSING;
3258 status = GOT_STATUS_DELETE;
3259 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3260 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3264 free_ignorelist(struct got_pathlist_head *ignorelist)
3266 struct got_pathlist_entry *pe;
3268 TAILQ_FOREACH(pe, ignorelist, entry)
3269 free((char *)pe->path);
3270 got_pathlist_free(ignorelist);
3274 free_ignores(struct got_pathlist_head *ignores)
3276 struct got_pathlist_entry *pe;
3278 TAILQ_FOREACH(pe, ignores, entry) {
3279 struct got_pathlist_head *ignorelist = pe->data;
3280 free_ignorelist(ignorelist);
3281 free((char *)pe->path);
3283 got_pathlist_free(ignores);
3286 static const struct got_error *
3287 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3289 const struct got_error *err = NULL;
3290 struct got_pathlist_entry *pe = NULL;
3291 struct got_pathlist_head *ignorelist;
3292 char *line = NULL, *pattern, *dirpath = NULL;
3293 size_t linesize = 0;
3296 ignorelist = calloc(1, sizeof(*ignorelist));
3297 if (ignorelist == NULL)
3298 return got_error_from_errno("calloc");
3299 TAILQ_INIT(ignorelist);
3301 while ((linelen = getline(&line, &linesize, f)) != -1) {
3302 if (linelen > 0 && line[linelen - 1] == '\n')
3303 line[linelen - 1] = '\0';
3305 /* Git's ignores may contain comments. */
3309 /* Git's negated patterns are not (yet?) supported. */
3313 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3315 err = got_error_from_errno("asprintf");
3318 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3323 err = got_error_from_errno("getline");
3327 dirpath = strdup(path);
3328 if (dirpath == NULL) {
3329 err = got_error_from_errno("strdup");
3332 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3335 if (err || pe == NULL) {
3337 free_ignorelist(ignorelist);
3343 match_ignores(struct got_pathlist_head *ignores, const char *path)
3345 struct got_pathlist_entry *pe;
3347 /* Handle patterns which match in all directories. */
3348 TAILQ_FOREACH(pe, ignores, entry) {
3349 struct got_pathlist_head *ignorelist = pe->data;
3350 struct got_pathlist_entry *pi;
3352 TAILQ_FOREACH(pi, ignorelist, entry) {
3353 const char *p, *pattern = pi->path;
3355 if (strncmp(pattern, "**/", 3) != 0)
3360 if (fnmatch(pattern, p,
3361 FNM_PATHNAME | FNM_LEADING_DIR)) {
3362 /* Retry in next directory. */
3363 while (*p && *p != '/')
3375 * The ignores pathlist contains ignore lists from children before
3376 * parents, so we can find the most specific ignorelist by walking
3377 * ignores backwards.
3379 pe = TAILQ_LAST(ignores, got_pathlist_head);
3381 if (got_path_is_child(path, pe->path, pe->path_len)) {
3382 struct got_pathlist_head *ignorelist = pe->data;
3383 struct got_pathlist_entry *pi;
3384 TAILQ_FOREACH(pi, ignorelist, entry) {
3385 const char *pattern = pi->path;
3386 int flags = FNM_LEADING_DIR;
3387 if (strstr(pattern, "/**/") == NULL)
3388 flags |= FNM_PATHNAME;
3389 if (fnmatch(pattern, path, flags))
3394 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3400 static const struct got_error *
3401 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3402 const char *path, int dirfd, const char *ignores_filename)
3404 const struct got_error *err = NULL;
3407 FILE *ignoresfile = NULL;
3409 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3410 path[0] ? "/" : "", ignores_filename) == -1)
3411 return got_error_from_errno("asprintf");
3414 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3416 if (errno != ENOENT && errno != EACCES)
3417 err = got_error_from_errno2("openat",
3420 ignoresfile = fdopen(fd, "r");
3421 if (ignoresfile == NULL)
3422 err = got_error_from_errno2("fdopen",
3426 err = read_ignores(ignores, path, ignoresfile);
3430 ignoresfile = fopen(ignorespath, "r");
3431 if (ignoresfile == NULL) {
3432 if (errno != ENOENT && errno != EACCES)
3433 err = got_error_from_errno2("fopen",
3436 err = read_ignores(ignores, path, ignoresfile);
3439 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3440 err = got_error_from_errno2("fclose", path);
3441 if (fd != -1 && close(fd) == -1 && err == NULL)
3442 err = got_error_from_errno2("close", path);
3447 static const struct got_error *
3448 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3450 const struct got_error *err = NULL;
3451 struct diff_dir_cb_arg *a = arg;
3454 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3455 return got_error(GOT_ERR_CANCELLED);
3457 if (parent_path[0]) {
3458 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3459 return got_error_from_errno("asprintf");
3464 if (de->d_type != DT_DIR &&
3465 got_path_is_child(path, a->status_path, a->status_path_len)
3466 && !match_ignores(&a->ignores, path))
3467 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3468 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3474 static const struct got_error *
3475 status_traverse(void *arg, const char *path, int dirfd)
3477 const struct got_error *err = NULL;
3478 struct diff_dir_cb_arg *a = arg;
3483 err = add_ignores(&a->ignores, a->worktree->root_path,
3484 path, dirfd, ".cvsignore");
3488 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3489 dirfd, ".gitignore");
3494 static const struct got_error *
3495 report_single_file_status(const char *path, const char *ondisk_path,
3496 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3497 void *status_arg, struct got_repository *repo, int report_unchanged)
3499 struct got_fileindex_entry *ie;
3502 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3504 return report_file_status(ie, ondisk_path, -1, NULL,
3505 status_cb, status_arg, repo, report_unchanged);
3507 if (lstat(ondisk_path, &sb) == -1) {
3508 if (errno != ENOENT)
3509 return got_error_from_errno2("lstat", ondisk_path);
3510 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3511 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3515 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3516 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3517 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3522 static const struct got_error *
3523 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3524 const char *root_path, const char *path)
3526 const struct got_error *err;
3527 char *parent_path, *next_parent_path = NULL;
3529 err = add_ignores(ignores, root_path, "", -1,
3534 err = add_ignores(ignores, root_path, "", -1,
3539 err = got_path_dirname(&parent_path, path);
3541 if (err->code == GOT_ERR_BAD_PATH)
3542 return NULL; /* cannot traverse parent */
3546 err = add_ignores(ignores, root_path, parent_path, -1,
3550 err = add_ignores(ignores, root_path, parent_path, -1,
3554 err = got_path_dirname(&next_parent_path, parent_path);
3556 if (err->code == GOT_ERR_BAD_PATH)
3557 err = NULL; /* traversed everything */
3561 parent_path = next_parent_path;
3562 next_parent_path = NULL;
3566 free(next_parent_path);
3570 static const struct got_error *
3571 worktree_status(struct got_worktree *worktree, const char *path,
3572 struct got_fileindex *fileindex, struct got_repository *repo,
3573 got_worktree_status_cb status_cb, void *status_arg,
3574 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3575 int report_unchanged)
3577 const struct got_error *err = NULL;
3579 struct got_fileindex_diff_dir_cb fdiff_cb;
3580 struct diff_dir_cb_arg arg;
3581 char *ondisk_path = NULL;
3583 TAILQ_INIT(&arg.ignores);
3585 if (asprintf(&ondisk_path, "%s%s%s",
3586 worktree->root_path, path[0] ? "/" : "", path) == -1)
3587 return got_error_from_errno("asprintf");
3589 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3591 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3593 err = got_error_from_errno2("open", ondisk_path);
3595 err = report_single_file_status(path, ondisk_path,
3596 fileindex, status_cb, status_arg, repo,
3599 fdiff_cb.diff_old_new = status_old_new;
3600 fdiff_cb.diff_old = status_old;
3601 fdiff_cb.diff_new = status_new;
3602 fdiff_cb.diff_traverse = status_traverse;
3603 arg.fileindex = fileindex;
3604 arg.worktree = worktree;
3605 arg.status_path = path;
3606 arg.status_path_len = strlen(path);
3608 arg.status_cb = status_cb;
3609 arg.status_arg = status_arg;
3610 arg.cancel_cb = cancel_cb;
3611 arg.cancel_arg = cancel_arg;
3612 arg.report_unchanged = report_unchanged;
3613 arg.no_ignores = no_ignores;
3615 err = add_ignores_from_parent_paths(&arg.ignores,
3616 worktree->root_path, path);
3620 err = got_fileindex_diff_dir(fileindex, fd,
3621 worktree->root_path, path, repo, &fdiff_cb, &arg);
3624 free_ignores(&arg.ignores);
3625 if (fd != -1 && close(fd) != 0 && err == NULL)
3626 err = got_error_from_errno("close");
3631 const struct got_error *
3632 got_worktree_status(struct got_worktree *worktree,
3633 struct got_pathlist_head *paths, struct got_repository *repo,
3634 got_worktree_status_cb status_cb, void *status_arg,
3635 got_cancel_cb cancel_cb, void *cancel_arg)
3637 const struct got_error *err = NULL;
3638 char *fileindex_path = NULL;
3639 struct got_fileindex *fileindex = NULL;
3640 struct got_pathlist_entry *pe;
3642 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3646 TAILQ_FOREACH(pe, paths, entry) {
3647 err = worktree_status(worktree, pe->path, fileindex, repo,
3648 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3652 free(fileindex_path);
3653 got_fileindex_free(fileindex);
3657 const struct got_error *
3658 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3661 const struct got_error *err = NULL;
3662 char *resolved = NULL, *cwd = NULL, *path = NULL;
3665 char *abspath = NULL;
3666 char canonpath[PATH_MAX];
3670 cwd = getcwd(NULL, 0);
3672 return got_error_from_errno("getcwd");
3674 if (lstat(arg, &sb) == -1) {
3675 if (errno != ENOENT) {
3676 err = got_error_from_errno2("lstat", arg);
3681 if (S_ISLNK(sb.st_mode)) {
3683 * We cannot use realpath(3) with symlinks since we want to
3684 * operate on the symlink itself.
3685 * But we can make the path absolute, assuming it is relative
3686 * to the current working directory, and then canonicalize it.
3688 if (!got_path_is_absolute(arg)) {
3689 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3690 err = got_error_from_errno("asprintf");
3695 err = got_canonpath(abspath ? abspath : arg, canonpath,
3699 resolved = strdup(canonpath);
3700 if (resolved == NULL) {
3701 err = got_error_from_errno("strdup");
3705 resolved = realpath(arg, NULL);
3706 if (resolved == NULL) {
3707 if (errno != ENOENT) {
3708 err = got_error_from_errno2("realpath", arg);
3711 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3712 err = got_error_from_errno("asprintf");
3715 err = got_canonpath(abspath, canonpath,
3719 resolved = strdup(canonpath);
3720 if (resolved == NULL) {
3721 err = got_error_from_errno("strdup");
3727 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3728 strlen(got_worktree_get_root_path(worktree)))) {
3729 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3733 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3734 err = got_path_skip_common_ancestor(&path,
3735 got_worktree_get_root_path(worktree), resolved);
3741 err = got_error_from_errno("strdup");
3746 /* XXX status walk can't deal with trailing slash! */
3748 while (len > 0 && path[len - 1] == '/') {
3749 path[len - 1] = '\0';
3763 struct schedule_addition_args {
3764 struct got_worktree *worktree;
3765 struct got_fileindex *fileindex;
3766 got_worktree_checkout_cb progress_cb;
3768 struct got_repository *repo;
3771 static const struct got_error *
3772 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3773 const char *relpath, struct got_object_id *blob_id,
3774 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3775 int dirfd, const char *de_name)
3777 struct schedule_addition_args *a = arg;
3778 const struct got_error *err = NULL;
3779 struct got_fileindex_entry *ie;
3783 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3785 return got_error_from_errno("asprintf");
3787 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3789 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3793 /* Re-adding an existing entry is a no-op. */
3794 if (status == GOT_STATUS_ADD)
3796 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3801 if (status != GOT_STATUS_UNVERSIONED) {
3802 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3806 err = got_fileindex_entry_alloc(&ie, relpath);
3809 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3810 relpath, NULL, NULL, 1);
3812 got_fileindex_entry_free(ie);
3815 err = got_fileindex_entry_add(a->fileindex, ie);
3817 got_fileindex_entry_free(ie);
3824 if (status == GOT_STATUS_ADD)
3826 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3829 const struct got_error *
3830 got_worktree_schedule_add(struct got_worktree *worktree,
3831 struct got_pathlist_head *paths,
3832 got_worktree_checkout_cb progress_cb, void *progress_arg,
3833 struct got_repository *repo, int no_ignores)
3835 struct got_fileindex *fileindex = NULL;
3836 char *fileindex_path = NULL;
3837 const struct got_error *err = NULL, *sync_err, *unlockerr;
3838 struct got_pathlist_entry *pe;
3839 struct schedule_addition_args saa;
3841 err = lock_worktree(worktree, LOCK_EX);
3845 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3849 saa.worktree = worktree;
3850 saa.fileindex = fileindex;
3851 saa.progress_cb = progress_cb;
3852 saa.progress_arg = progress_arg;
3855 TAILQ_FOREACH(pe, paths, entry) {
3856 err = worktree_status(worktree, pe->path, fileindex, repo,
3857 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3861 sync_err = sync_fileindex(fileindex, fileindex_path);
3862 if (sync_err && err == NULL)
3865 free(fileindex_path);
3867 got_fileindex_free(fileindex);
3868 unlockerr = lock_worktree(worktree, LOCK_SH);
3869 if (unlockerr && err == NULL)
3874 struct schedule_deletion_args {
3875 struct got_worktree *worktree;
3876 struct got_fileindex *fileindex;
3877 got_worktree_delete_cb progress_cb;
3879 struct got_repository *repo;
3880 int delete_local_mods;
3882 const char *status_codes;
3885 static const struct got_error *
3886 schedule_for_deletion(void *arg, unsigned char status,
3887 unsigned char staged_status, const char *relpath,
3888 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3889 struct got_object_id *commit_id, int dirfd, const char *de_name)
3891 struct schedule_deletion_args *a = arg;
3892 const struct got_error *err = NULL;
3893 struct got_fileindex_entry *ie = NULL;
3897 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3899 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3901 staged_status = get_staged_status(ie);
3902 if (staged_status != GOT_STATUS_NO_CHANGE) {
3903 if (staged_status == GOT_STATUS_DELETE)
3905 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3908 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3910 return got_error_from_errno("asprintf");
3912 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3917 if (a->status_codes) {
3918 size_t ncodes = strlen(a->status_codes);
3920 for (i = 0; i < ncodes ; i++) {
3921 if (status == a->status_codes[i])
3925 /* Do not delete files in non-matching status. */
3929 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3930 a->status_codes[i] != GOT_STATUS_MISSING) {
3931 static char msg[64];
3932 snprintf(msg, sizeof(msg),
3933 "invalid status code '%c'", a->status_codes[i]);
3934 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3939 if (status != GOT_STATUS_NO_CHANGE) {
3940 if (status == GOT_STATUS_DELETE)
3942 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3943 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3946 if (status != GOT_STATUS_MODIFY &&
3947 status != GOT_STATUS_MISSING) {
3948 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3953 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3957 if (unlinkat(dirfd, de_name, 0) != 0) {
3958 err = got_error_from_errno2("unlinkat",
3962 } else if (unlink(ondisk_path) != 0) {
3963 err = got_error_from_errno2("unlink", ondisk_path);
3967 root_len = strlen(a->worktree->root_path);
3970 err = got_path_dirname(&parent, ondisk_path);
3974 ondisk_path = parent;
3975 if (rmdir(ondisk_path) == -1) {
3976 if (errno != ENOTEMPTY)
3977 err = got_error_from_errno2("rmdir",
3981 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3982 strlen(ondisk_path), root_len) != 0);
3985 got_fileindex_entry_mark_deleted_from_disk(ie);
3990 if (status == GOT_STATUS_DELETE)
3992 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3993 staged_status, relpath);
3996 const struct got_error *
3997 got_worktree_schedule_delete(struct got_worktree *worktree,
3998 struct got_pathlist_head *paths, int delete_local_mods,
3999 const char *status_codes,
4000 got_worktree_delete_cb progress_cb, void *progress_arg,
4001 struct got_repository *repo, int keep_on_disk)
4003 struct got_fileindex *fileindex = NULL;
4004 char *fileindex_path = NULL;
4005 const struct got_error *err = NULL, *sync_err, *unlockerr;
4006 struct got_pathlist_entry *pe;
4007 struct schedule_deletion_args sda;
4009 err = lock_worktree(worktree, LOCK_EX);
4013 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4017 sda.worktree = worktree;
4018 sda.fileindex = fileindex;
4019 sda.progress_cb = progress_cb;
4020 sda.progress_arg = progress_arg;
4022 sda.delete_local_mods = delete_local_mods;
4023 sda.keep_on_disk = keep_on_disk;
4024 sda.status_codes = status_codes;
4026 TAILQ_FOREACH(pe, paths, entry) {
4027 err = worktree_status(worktree, pe->path, fileindex, repo,
4028 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4032 sync_err = sync_fileindex(fileindex, fileindex_path);
4033 if (sync_err && err == NULL)
4036 free(fileindex_path);
4038 got_fileindex_free(fileindex);
4039 unlockerr = lock_worktree(worktree, LOCK_SH);
4040 if (unlockerr && err == NULL)
4045 static const struct got_error *
4046 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4048 const struct got_error *err = NULL;
4050 size_t linesize = 0, n;
4053 linelen = getline(&line, &linesize, infile);
4054 if (linelen == -1) {
4055 if (ferror(infile)) {
4056 err = got_error_from_errno("getline");
4062 n = fwrite(line, 1, linelen, outfile);
4064 err = got_ferror(outfile, GOT_ERR_IO);
4069 n = fwrite(line, 1, linelen, rejectfile);
4071 err = got_ferror(outfile, GOT_ERR_IO);
4078 static const struct got_error *
4079 skip_one_line(FILE *f)
4082 size_t linesize = 0;
4085 linelen = getline(&line, &linesize, f);
4086 if (linelen == -1) {
4088 return got_error_from_errno("getline");
4095 static const struct got_error *
4096 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4097 int start_old, int end_old, int start_new, int end_new,
4098 FILE *outfile, FILE *rejectfile)
4100 const struct got_error *err;
4102 /* Copy old file's lines leading up to patch. */
4103 while (!feof(f1) && *line_cur1 < start_old) {
4104 err = copy_one_line(f1, outfile, NULL);
4109 /* Skip new file's lines leading up to patch. */
4110 while (!feof(f2) && *line_cur2 < start_new) {
4112 err = copy_one_line(f2, NULL, rejectfile);
4114 err = skip_one_line(f2);
4119 /* Copy patched lines. */
4120 while (!feof(f2) && *line_cur2 <= end_new) {
4121 err = copy_one_line(f2, outfile, NULL);
4126 /* Skip over old file's replaced lines. */
4127 while (!feof(f1) && *line_cur1 <= end_old) {
4129 err = copy_one_line(f1, NULL, rejectfile);
4131 err = skip_one_line(f1);
4140 static const struct got_error *
4141 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4142 FILE *outfile, FILE *rejectfile)
4144 const struct got_error *err;
4147 /* Copy old file's lines until EOF. */
4149 err = copy_one_line(f1, outfile, NULL);
4156 /* Copy new file's lines until EOF. */
4158 err = copy_one_line(f2, NULL, rejectfile);
4168 static const struct got_error *
4169 apply_or_reject_change(int *choice, int *nchunks_used,
4170 struct diff_result *diff_result, int n,
4171 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4172 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4173 got_worktree_patch_cb patch_cb, void *patch_arg)
4175 const struct got_error *err = NULL;
4176 struct diff_chunk_context cc = {};
4177 int start_old, end_old, start_new, end_new;
4179 struct diff_output_unidiff_state *diff_state;
4180 struct diff_input_info diff_info;
4183 *choice = GOT_PATCH_CHOICE_NONE;
4185 /* Get changed line numbers without context lines for copy_change(). */
4186 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4187 start_old = cc.left.start;
4188 end_old = cc.left.end;
4189 start_new = cc.right.start;
4190 end_new = cc.right.end;
4192 /* Get the same change with context lines for display. */
4193 memset(&cc, 0, sizeof(cc));
4194 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4196 memset(&diff_info, 0, sizeof(diff_info));
4197 diff_info.left_path = relpath;
4198 diff_info.right_path = relpath;
4200 diff_state = diff_output_unidiff_state_alloc();
4201 if (diff_state == NULL)
4202 return got_error_set_errno(ENOMEM,
4203 "diff_output_unidiff_state_alloc");
4205 hunkfile = got_opentemp();
4206 if (hunkfile == NULL) {
4207 err = got_error_from_errno("got_opentemp");
4211 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4213 if (rc != DIFF_RC_OK) {
4214 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4218 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4219 err = got_ferror(hunkfile, GOT_ERR_IO);
4223 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4224 hunkfile, changeno, nchanges);
4229 case GOT_PATCH_CHOICE_YES:
4230 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4231 end_old, start_new, end_new, outfile, rejectfile);
4233 case GOT_PATCH_CHOICE_NO:
4234 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4235 end_old, start_new, end_new, rejectfile, outfile);
4237 case GOT_PATCH_CHOICE_QUIT:
4240 err = got_error(GOT_ERR_PATCH_CHOICE);
4244 diff_output_unidiff_state_free(diff_state);
4245 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4246 err = got_error_from_errno("fclose");
4250 struct revert_file_args {
4251 struct got_worktree *worktree;
4252 struct got_fileindex *fileindex;
4253 got_worktree_checkout_cb progress_cb;
4255 got_worktree_patch_cb patch_cb;
4257 struct got_repository *repo;
4260 static const struct got_error *
4261 create_patched_content(char **path_outfile, int reverse_patch,
4262 struct got_object_id *blob_id, const char *path2,
4263 int dirfd2, const char *de_name2,
4264 const char *relpath, struct got_repository *repo,
4265 got_worktree_patch_cb patch_cb, void *patch_arg)
4267 const struct got_error *err, *free_err;
4268 struct got_blob_object *blob = NULL;
4269 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4271 char link_target[PATH_MAX];
4272 ssize_t link_len = 0;
4273 char *path1 = NULL, *id_str = NULL;
4275 struct got_diffreg_result *diffreg_result = NULL;
4276 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4277 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4279 *path_outfile = NULL;
4281 err = got_object_id_str(&id_str, blob_id);
4286 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4288 if (errno != ELOOP) {
4289 err = got_error_from_errno2("openat", path2);
4292 link_len = readlinkat(dirfd2, de_name2,
4293 link_target, sizeof(link_target));
4295 return got_error_from_errno2("readlinkat", path2);
4296 sb2.st_mode = S_IFLNK;
4297 sb2.st_size = link_len;
4300 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4302 if (errno != ELOOP) {
4303 err = got_error_from_errno2("open", path2);
4306 link_len = readlink(path2, link_target,
4307 sizeof(link_target));
4309 return got_error_from_errno2("readlink", path2);
4310 sb2.st_mode = S_IFLNK;
4311 sb2.st_size = link_len;
4315 if (fstat(fd2, &sb2) == -1) {
4316 err = got_error_from_errno2("fstat", path2);
4320 f2 = fdopen(fd2, "r");
4322 err = got_error_from_errno2("fdopen", path2);
4328 f2 = got_opentemp();
4330 err = got_error_from_errno2("got_opentemp", path2);
4333 n = fwrite(link_target, 1, link_len, f2);
4334 if (n != link_len) {
4335 err = got_ferror(f2, GOT_ERR_IO);
4338 if (fflush(f2) == EOF) {
4339 err = got_error_from_errno("fflush");
4345 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4349 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4353 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4357 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4362 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4366 if (fseek(f1, 0L, SEEK_SET) == -1)
4367 return got_ferror(f1, GOT_ERR_IO);
4368 if (fseek(f2, 0L, SEEK_SET) == -1)
4369 return got_ferror(f2, GOT_ERR_IO);
4371 /* Count the number of actual changes in the diff result. */
4372 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4373 struct diff_chunk_context cc = {};
4374 diff_chunk_context_load_change(&cc, &nchunks_used,
4375 diffreg_result->result, n, 0);
4378 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4380 err = apply_or_reject_change(&choice, &nchunks_used,
4381 diffreg_result->result, n, relpath, f1, f2,
4382 &line_cur1, &line_cur2,
4383 reverse_patch ? NULL : outfile,
4384 reverse_patch ? outfile : NULL,
4385 ++i, nchanges, patch_cb, patch_arg);
4388 if (choice == GOT_PATCH_CHOICE_YES)
4390 else if (choice == GOT_PATCH_CHOICE_QUIT)
4394 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4395 reverse_patch ? NULL : outfile,
4396 reverse_patch ? outfile : NULL);
4400 if (!S_ISLNK(sb2.st_mode)) {
4401 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4402 err = got_error_from_errno2("fchmod", path2);
4410 got_object_blob_close(blob);
4411 free_err = got_diffreg_result_free(diffreg_result);
4414 if (f1 && fclose(f1) == EOF && err == NULL)
4415 err = got_error_from_errno2("fclose", path1);
4416 if (f2 && fclose(f2) == EOF && err == NULL)
4417 err = got_error_from_errno2("fclose", path2);
4418 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4419 err = got_error_from_errno2("close", path2);
4420 if (outfile && fclose(outfile) == EOF && err == NULL)
4421 err = got_error_from_errno2("fclose", *path_outfile);
4422 if (path1 && unlink(path1) == -1 && err == NULL)
4423 err = got_error_from_errno2("unlink", path1);
4424 if (err || !have_content) {
4425 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4426 err = got_error_from_errno2("unlink", *path_outfile);
4427 free(*path_outfile);
4428 *path_outfile = NULL;
4434 static const struct got_error *
4435 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4436 const char *relpath, struct got_object_id *blob_id,
4437 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4438 int dirfd, const char *de_name)
4440 struct revert_file_args *a = arg;
4441 const struct got_error *err = NULL;
4442 char *parent_path = NULL;
4443 struct got_fileindex_entry *ie;
4444 struct got_tree_object *tree = NULL;
4445 struct got_object_id *tree_id = NULL;
4446 const struct got_tree_entry *te = NULL;
4447 char *tree_path = NULL, *te_name;
4448 char *ondisk_path = NULL, *path_content = NULL;
4449 struct got_blob_object *blob = NULL;
4451 /* Reverting a staged deletion is a no-op. */
4452 if (status == GOT_STATUS_DELETE &&
4453 staged_status != GOT_STATUS_NO_CHANGE)
4456 if (status == GOT_STATUS_UNVERSIONED)
4457 return (*a->progress_cb)(a->progress_arg,
4458 GOT_STATUS_UNVERSIONED, relpath);
4460 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4462 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4464 /* Construct in-repository path of tree which contains this blob. */
4465 err = got_path_dirname(&parent_path, ie->path);
4467 if (err->code != GOT_ERR_BAD_PATH)
4469 parent_path = strdup("/");
4470 if (parent_path == NULL) {
4471 err = got_error_from_errno("strdup");
4475 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4476 tree_path = strdup(parent_path);
4477 if (tree_path == NULL) {
4478 err = got_error_from_errno("strdup");
4482 if (got_path_is_root_dir(parent_path)) {
4483 tree_path = strdup(a->worktree->path_prefix);
4484 if (tree_path == NULL) {
4485 err = got_error_from_errno("strdup");
4489 if (asprintf(&tree_path, "%s/%s",
4490 a->worktree->path_prefix, parent_path) == -1) {
4491 err = got_error_from_errno("asprintf");
4497 err = got_object_id_by_path(&tree_id, a->repo,
4498 a->worktree->base_commit_id, tree_path);
4500 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4501 (status == GOT_STATUS_ADD ||
4502 staged_status == GOT_STATUS_ADD)))
4505 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4509 err = got_path_basename(&te_name, ie->path);
4513 te = got_object_tree_find_entry(tree, te_name);
4515 if (te == NULL && status != GOT_STATUS_ADD &&
4516 staged_status != GOT_STATUS_ADD) {
4517 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4523 case GOT_STATUS_ADD:
4525 int choice = GOT_PATCH_CHOICE_NONE;
4526 err = (*a->patch_cb)(&choice, a->patch_arg,
4527 status, ie->path, NULL, 1, 1);
4530 if (choice != GOT_PATCH_CHOICE_YES)
4533 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4537 got_fileindex_entry_remove(a->fileindex, ie);
4539 case GOT_STATUS_DELETE:
4541 int choice = GOT_PATCH_CHOICE_NONE;
4542 err = (*a->patch_cb)(&choice, a->patch_arg,
4543 status, ie->path, NULL, 1, 1);
4546 if (choice != GOT_PATCH_CHOICE_YES)
4550 case GOT_STATUS_MODIFY:
4551 case GOT_STATUS_MODE_CHANGE:
4552 case GOT_STATUS_CONFLICT:
4553 case GOT_STATUS_MISSING: {
4554 struct got_object_id id;
4555 if (staged_status == GOT_STATUS_ADD ||
4556 staged_status == GOT_STATUS_MODIFY) {
4557 memcpy(id.sha1, ie->staged_blob_sha1,
4558 SHA1_DIGEST_LENGTH);
4560 memcpy(id.sha1, ie->blob_sha1,
4561 SHA1_DIGEST_LENGTH);
4562 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4566 if (asprintf(&ondisk_path, "%s/%s",
4567 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4568 err = got_error_from_errno("asprintf");
4572 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4573 status == GOT_STATUS_CONFLICT)) {
4574 int is_bad_symlink = 0;
4575 err = create_patched_content(&path_content, 1, &id,
4576 ondisk_path, dirfd, de_name, ie->path, a->repo,
4577 a->patch_cb, a->patch_arg);
4578 if (err || path_content == NULL)
4580 if (te && S_ISLNK(te->mode)) {
4581 if (unlink(path_content) == -1) {
4582 err = got_error_from_errno2("unlink",
4586 err = install_symlink(&is_bad_symlink,
4587 a->worktree, ondisk_path, ie->path,
4588 blob, 0, 1, 0, a->repo,
4589 a->progress_cb, a->progress_arg);
4591 if (rename(path_content, ondisk_path) == -1) {
4592 err = got_error_from_errno3("rename",
4593 path_content, ondisk_path);
4598 int is_bad_symlink = 0;
4599 if (te && S_ISLNK(te->mode)) {
4600 err = install_symlink(&is_bad_symlink,
4601 a->worktree, ondisk_path, ie->path,
4602 blob, 0, 1, 0, a->repo,
4603 a->progress_cb, a->progress_arg);
4605 err = install_blob(a->worktree, ondisk_path,
4607 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4608 got_fileindex_perms_to_st(ie), blob,
4609 0, 1, 0, 0, a->repo,
4610 a->progress_cb, a->progress_arg);
4614 if (status == GOT_STATUS_DELETE ||
4615 status == GOT_STATUS_MODE_CHANGE) {
4616 err = got_fileindex_entry_update(ie,
4617 a->worktree->root_fd, relpath,
4619 a->worktree->base_commit_id->sha1, 1);
4623 if (is_bad_symlink) {
4624 got_fileindex_entry_filetype_set(ie,
4625 GOT_FILEIDX_MODE_BAD_SYMLINK);
4639 got_object_blob_close(blob);
4641 got_object_tree_close(tree);
4646 const struct got_error *
4647 got_worktree_revert(struct got_worktree *worktree,
4648 struct got_pathlist_head *paths,
4649 got_worktree_checkout_cb progress_cb, void *progress_arg,
4650 got_worktree_patch_cb patch_cb, void *patch_arg,
4651 struct got_repository *repo)
4653 struct got_fileindex *fileindex = NULL;
4654 char *fileindex_path = NULL;
4655 const struct got_error *err = NULL, *unlockerr = NULL;
4656 const struct got_error *sync_err = NULL;
4657 struct got_pathlist_entry *pe;
4658 struct revert_file_args rfa;
4660 err = lock_worktree(worktree, LOCK_EX);
4664 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4668 rfa.worktree = worktree;
4669 rfa.fileindex = fileindex;
4670 rfa.progress_cb = progress_cb;
4671 rfa.progress_arg = progress_arg;
4672 rfa.patch_cb = patch_cb;
4673 rfa.patch_arg = patch_arg;
4675 TAILQ_FOREACH(pe, paths, entry) {
4676 err = worktree_status(worktree, pe->path, fileindex, repo,
4677 revert_file, &rfa, NULL, NULL, 0, 0);
4681 sync_err = sync_fileindex(fileindex, fileindex_path);
4682 if (sync_err && err == NULL)
4685 free(fileindex_path);
4687 got_fileindex_free(fileindex);
4688 unlockerr = lock_worktree(worktree, LOCK_SH);
4689 if (unlockerr && err == NULL)
4695 free_commitable(struct got_commitable *ct)
4698 free(ct->in_repo_path);
4699 free(ct->ondisk_path);
4701 free(ct->base_blob_id);
4702 free(ct->staged_blob_id);
4703 free(ct->base_commit_id);
4707 struct collect_commitables_arg {
4708 struct got_pathlist_head *commitable_paths;
4709 struct got_repository *repo;
4710 struct got_worktree *worktree;
4711 struct got_fileindex *fileindex;
4712 int have_staged_files;
4713 int allow_bad_symlinks;
4716 static const struct got_error *
4717 collect_commitables(void *arg, unsigned char status,
4718 unsigned char staged_status, const char *relpath,
4719 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4720 struct got_object_id *commit_id, int dirfd, const char *de_name)
4722 struct collect_commitables_arg *a = arg;
4723 const struct got_error *err = NULL;
4724 struct got_commitable *ct = NULL;
4725 struct got_pathlist_entry *new = NULL;
4726 char *parent_path = NULL, *path = NULL;
4729 if (a->have_staged_files) {
4730 if (staged_status != GOT_STATUS_MODIFY &&
4731 staged_status != GOT_STATUS_ADD &&
4732 staged_status != GOT_STATUS_DELETE)
4735 if (status == GOT_STATUS_CONFLICT)
4736 return got_error(GOT_ERR_COMMIT_CONFLICT);
4738 if (status != GOT_STATUS_MODIFY &&
4739 status != GOT_STATUS_MODE_CHANGE &&
4740 status != GOT_STATUS_ADD &&
4741 status != GOT_STATUS_DELETE)
4745 if (asprintf(&path, "/%s", relpath) == -1) {
4746 err = got_error_from_errno("asprintf");
4749 if (strcmp(path, "/") == 0) {
4750 parent_path = strdup("");
4751 if (parent_path == NULL)
4752 return got_error_from_errno("strdup");
4754 err = got_path_dirname(&parent_path, path);
4759 ct = calloc(1, sizeof(*ct));
4761 err = got_error_from_errno("calloc");
4765 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4767 err = got_error_from_errno("asprintf");
4771 if (staged_status == GOT_STATUS_ADD ||
4772 staged_status == GOT_STATUS_MODIFY) {
4773 struct got_fileindex_entry *ie;
4774 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4775 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4776 case GOT_FILEIDX_MODE_REGULAR_FILE:
4777 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4780 case GOT_FILEIDX_MODE_SYMLINK:
4784 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4787 ct->mode |= got_fileindex_entry_perms_get(ie);
4788 } else if (status != GOT_STATUS_DELETE &&
4789 staged_status != GOT_STATUS_DELETE) {
4791 if (fstatat(dirfd, de_name, &sb,
4792 AT_SYMLINK_NOFOLLOW) == -1) {
4793 err = got_error_from_errno2("fstatat",
4797 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4798 err = got_error_from_errno2("lstat", ct->ondisk_path);
4801 ct->mode = sb.st_mode;
4804 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4805 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4807 err = got_error_from_errno("asprintf");
4811 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4812 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4814 char target_path[PATH_MAX];
4816 target_len = readlink(ct->ondisk_path, target_path,
4817 sizeof(target_path));
4818 if (target_len == -1) {
4819 err = got_error_from_errno2("readlink",
4823 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4824 target_len, ct->ondisk_path, a->worktree->root_path);
4827 if (is_bad_symlink) {
4828 err = got_error_path(ct->ondisk_path,
4829 GOT_ERR_BAD_SYMLINK);
4835 ct->status = status;
4836 ct->staged_status = staged_status;
4837 ct->blob_id = NULL; /* will be filled in when blob gets created */
4838 if (ct->status != GOT_STATUS_ADD &&
4839 ct->staged_status != GOT_STATUS_ADD) {
4840 ct->base_blob_id = got_object_id_dup(blob_id);
4841 if (ct->base_blob_id == NULL) {
4842 err = got_error_from_errno("got_object_id_dup");
4845 ct->base_commit_id = got_object_id_dup(commit_id);
4846 if (ct->base_commit_id == NULL) {
4847 err = got_error_from_errno("got_object_id_dup");
4851 if (ct->staged_status == GOT_STATUS_ADD ||
4852 ct->staged_status == GOT_STATUS_MODIFY) {
4853 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4854 if (ct->staged_blob_id == NULL) {
4855 err = got_error_from_errno("got_object_id_dup");
4859 ct->path = strdup(path);
4860 if (ct->path == NULL) {
4861 err = got_error_from_errno("strdup");
4864 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4866 if (ct && (err || new == NULL))
4867 free_commitable(ct);
4873 static const struct got_error *write_tree(struct got_object_id **, int *,
4874 struct got_tree_object *, const char *, struct got_pathlist_head *,
4875 got_worktree_status_cb status_cb, void *status_arg,
4876 struct got_repository *);
4878 static const struct got_error *
4879 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4880 struct got_tree_entry *te, const char *parent_path,
4881 struct got_pathlist_head *commitable_paths,
4882 got_worktree_status_cb status_cb, void *status_arg,
4883 struct got_repository *repo)
4885 const struct got_error *err = NULL;
4886 struct got_tree_object *subtree;
4889 if (asprintf(&subpath, "%s%s%s", parent_path,
4890 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4891 return got_error_from_errno("asprintf");
4893 err = got_object_open_as_tree(&subtree, repo, &te->id);
4897 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4898 commitable_paths, status_cb, status_arg, repo);
4899 got_object_tree_close(subtree);
4904 static const struct got_error *
4905 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4907 const struct got_error *err = NULL;
4908 char *ct_parent_path = NULL;
4912 if (strchr(ct->in_repo_path, '/') == NULL) {
4913 *match = got_path_is_root_dir(path);
4917 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4920 *match = (strcmp(path, ct_parent_path) == 0);
4921 free(ct_parent_path);
4926 get_ct_file_mode(struct got_commitable *ct)
4928 if (S_ISLNK(ct->mode))
4931 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4934 static const struct got_error *
4935 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4936 struct got_tree_entry *te, struct got_commitable *ct)
4938 const struct got_error *err = NULL;
4942 err = got_object_tree_entry_dup(new_te, te);
4946 (*new_te)->mode = get_ct_file_mode(ct);
4948 if (ct->staged_status == GOT_STATUS_MODIFY)
4949 memcpy(&(*new_te)->id, ct->staged_blob_id,
4950 sizeof((*new_te)->id));
4952 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4954 if (err && *new_te) {
4961 static const struct got_error *
4962 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4963 struct got_commitable *ct)
4965 const struct got_error *err = NULL;
4966 char *ct_name = NULL;
4970 *new_te = calloc(1, sizeof(**new_te));
4971 if (*new_te == NULL)
4972 return got_error_from_errno("calloc");
4974 err = got_path_basename(&ct_name, ct->path);
4977 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4978 sizeof((*new_te)->name)) {
4979 err = got_error(GOT_ERR_NO_SPACE);
4983 (*new_te)->mode = get_ct_file_mode(ct);
4985 if (ct->staged_status == GOT_STATUS_ADD)
4986 memcpy(&(*new_te)->id, ct->staged_blob_id,
4987 sizeof((*new_te)->id));
4989 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4992 if (err && *new_te) {
4999 static const struct got_error *
5000 insert_tree_entry(struct got_tree_entry *new_te,
5001 struct got_pathlist_head *paths)
5003 const struct got_error *err = NULL;
5004 struct got_pathlist_entry *new_pe;
5006 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5010 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5014 static const struct got_error *
5015 report_ct_status(struct got_commitable *ct,
5016 got_worktree_status_cb status_cb, void *status_arg)
5018 const char *ct_path = ct->path;
5019 unsigned char status;
5021 while (ct_path[0] == '/')
5024 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5025 status = ct->staged_status;
5027 status = ct->status;
5029 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5030 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5033 static const struct got_error *
5034 match_modified_subtree(int *modified, struct got_tree_entry *te,
5035 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5037 const struct got_error *err = NULL;
5038 struct got_pathlist_entry *pe;
5043 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5044 got_path_is_root_dir(base_tree_path) ? "" : "/",
5046 return got_error_from_errno("asprintf");
5048 TAILQ_FOREACH(pe, commitable_paths, entry) {
5049 struct got_commitable *ct = pe->data;
5050 *modified = got_path_is_child(ct->in_repo_path, te_path,
5060 static const struct got_error *
5061 match_deleted_or_modified_ct(struct got_commitable **ctp,
5062 struct got_tree_entry *te, const char *base_tree_path,
5063 struct got_pathlist_head *commitable_paths)
5065 const struct got_error *err = NULL;
5066 struct got_pathlist_entry *pe;
5070 TAILQ_FOREACH(pe, commitable_paths, entry) {
5071 struct got_commitable *ct = pe->data;
5072 char *ct_name = NULL;
5075 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5076 if (ct->status != GOT_STATUS_MODIFY &&
5077 ct->status != GOT_STATUS_MODE_CHANGE &&
5078 ct->status != GOT_STATUS_DELETE)
5081 if (ct->staged_status != GOT_STATUS_MODIFY &&
5082 ct->staged_status != GOT_STATUS_DELETE)
5086 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5089 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5095 err = got_path_basename(&ct_name, pe->path);
5099 if (strcmp(te->name, ct_name) != 0) {
5112 static const struct got_error *
5113 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5114 const char *child_path, const char *path_base_tree,
5115 struct got_pathlist_head *commitable_paths,
5116 got_worktree_status_cb status_cb, void *status_arg,
5117 struct got_repository *repo)
5119 const struct got_error *err = NULL;
5120 struct got_tree_entry *new_te;
5122 struct got_object_id *id = NULL;
5127 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5128 got_path_is_root_dir(path_base_tree) ? "" : "/",
5130 return got_error_from_errno("asprintf");
5132 new_te = calloc(1, sizeof(*new_te));
5134 return got_error_from_errno("calloc");
5135 new_te->mode = S_IFDIR;
5137 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5138 sizeof(new_te->name)) {
5139 err = got_error(GOT_ERR_NO_SPACE);
5142 err = write_tree(&id, &nentries, NULL, subtree_path,
5143 commitable_paths, status_cb, status_arg, repo);
5148 memcpy(&new_te->id, id, sizeof(new_te->id));
5157 static const struct got_error *
5158 write_tree(struct got_object_id **new_tree_id, int *nentries,
5159 struct got_tree_object *base_tree, const char *path_base_tree,
5160 struct got_pathlist_head *commitable_paths,
5161 got_worktree_status_cb status_cb, void *status_arg,
5162 struct got_repository *repo)
5164 const struct got_error *err = NULL;
5165 struct got_pathlist_head paths;
5166 struct got_tree_entry *te, *new_te = NULL;
5167 struct got_pathlist_entry *pe;
5172 /* Insert, and recurse into, newly added entries first. */
5173 TAILQ_FOREACH(pe, commitable_paths, entry) {
5174 struct got_commitable *ct = pe->data;
5175 char *child_path = NULL, *slash;
5177 if ((ct->status != GOT_STATUS_ADD &&
5178 ct->staged_status != GOT_STATUS_ADD) ||
5179 (ct->flags & GOT_COMMITABLE_ADDED))
5182 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5183 strlen(path_base_tree)))
5186 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5191 slash = strchr(child_path, '/');
5192 if (slash == NULL) {
5193 err = alloc_added_blob_tree_entry(&new_te, ct);
5196 err = report_ct_status(ct, status_cb, status_arg);
5199 ct->flags |= GOT_COMMITABLE_ADDED;
5200 err = insert_tree_entry(new_te, &paths);
5205 *slash = '\0'; /* trim trailing path components */
5206 if (base_tree == NULL ||
5207 got_object_tree_find_entry(base_tree, child_path)
5209 err = make_subtree_for_added_blob(&new_te,
5210 child_path, path_base_tree,
5211 commitable_paths, status_cb, status_arg,
5215 err = insert_tree_entry(new_te, &paths);
5224 int i, nbase_entries;
5225 /* Handle modified and deleted entries. */
5226 nbase_entries = got_object_tree_get_nentries(base_tree);
5227 for (i = 0; i < nbase_entries; i++) {
5228 struct got_commitable *ct = NULL;
5230 te = got_object_tree_get_entry(base_tree, i);
5231 if (got_object_tree_entry_is_submodule(te)) {
5232 /* Entry is a submodule; just copy it. */
5233 err = got_object_tree_entry_dup(&new_te, te);
5236 err = insert_tree_entry(new_te, &paths);
5243 if (S_ISDIR(te->mode)) {
5245 err = got_object_tree_entry_dup(&new_te, te);
5248 err = match_modified_subtree(&modified, te,
5249 path_base_tree, commitable_paths);
5252 /* Avoid recursion into unmodified subtrees. */
5254 struct got_object_id *new_id;
5256 err = write_subtree(&new_id,
5258 path_base_tree, commitable_paths,
5259 status_cb, status_arg, repo);
5262 if (nsubentries == 0) {
5263 /* All entries were deleted. */
5267 memcpy(&new_te->id, new_id,
5268 sizeof(new_te->id));
5271 err = insert_tree_entry(new_te, &paths);
5278 err = match_deleted_or_modified_ct(&ct, te,
5279 path_base_tree, commitable_paths);
5283 /* NB: Deleted entries get dropped here. */
5284 if (ct->status == GOT_STATUS_MODIFY ||
5285 ct->status == GOT_STATUS_MODE_CHANGE ||
5286 ct->staged_status == GOT_STATUS_MODIFY) {
5287 err = alloc_modified_blob_tree_entry(
5291 err = insert_tree_entry(new_te, &paths);
5296 err = report_ct_status(ct, status_cb,
5301 /* Entry is unchanged; just copy it. */
5302 err = got_object_tree_entry_dup(&new_te, te);
5305 err = insert_tree_entry(new_te, &paths);
5313 /* Write new list of entries; deleted entries have been dropped. */
5314 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5316 got_pathlist_free(&paths);
5320 static const struct got_error *
5321 update_fileindex_after_commit(struct got_worktree *worktree,
5322 struct got_pathlist_head *commitable_paths,
5323 struct got_object_id *new_base_commit_id,
5324 struct got_fileindex *fileindex, int have_staged_files)
5326 const struct got_error *err = NULL;
5327 struct got_pathlist_entry *pe;
5328 char *relpath = NULL;
5330 TAILQ_FOREACH(pe, commitable_paths, entry) {
5331 struct got_fileindex_entry *ie;
5332 struct got_commitable *ct = pe->data;
5334 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5336 err = got_path_skip_common_ancestor(&relpath,
5337 worktree->root_path, ct->ondisk_path);
5342 if (ct->status == GOT_STATUS_DELETE ||
5343 ct->staged_status == GOT_STATUS_DELETE) {
5344 got_fileindex_entry_remove(fileindex, ie);
5345 } else if (ct->staged_status == GOT_STATUS_ADD ||
5346 ct->staged_status == GOT_STATUS_MODIFY) {
5347 got_fileindex_entry_stage_set(ie,
5348 GOT_FILEIDX_STAGE_NONE);
5349 got_fileindex_entry_staged_filetype_set(ie, 0);
5351 err = got_fileindex_entry_update(ie,
5352 worktree->root_fd, relpath,
5353 ct->staged_blob_id->sha1,
5354 new_base_commit_id->sha1,
5355 !have_staged_files);
5357 err = got_fileindex_entry_update(ie,
5358 worktree->root_fd, relpath,
5360 new_base_commit_id->sha1,
5361 !have_staged_files);
5363 err = got_fileindex_entry_alloc(&ie, pe->path);
5366 err = got_fileindex_entry_update(ie,
5367 worktree->root_fd, relpath, ct->blob_id->sha1,
5368 new_base_commit_id->sha1, 1);
5370 got_fileindex_entry_free(ie);
5373 err = got_fileindex_entry_add(fileindex, ie);
5375 got_fileindex_entry_free(ie);
5388 static const struct got_error *
5389 check_out_of_date(const char *in_repo_path, unsigned char status,
5390 unsigned char staged_status, struct got_object_id *base_blob_id,
5391 struct got_object_id *base_commit_id,
5392 struct got_object_id *head_commit_id, struct got_repository *repo,
5395 const struct got_error *err = NULL;
5396 struct got_object_id *id = NULL;
5398 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5399 /* Trivial case: base commit == head commit */
5400 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5403 * Ensure file content which local changes were based
5404 * on matches file content in the branch head.
5406 err = got_object_id_by_path(&id, repo, head_commit_id,
5409 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5410 err = got_error(ood_errcode);
5412 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5413 err = got_error(ood_errcode);
5415 /* Require that added files don't exist in the branch head. */
5416 err = got_object_id_by_path(&id, repo, head_commit_id,
5418 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5420 err = id ? got_error(ood_errcode) : NULL;
5427 const struct got_error *
5428 commit_worktree(struct got_object_id **new_commit_id,
5429 struct got_pathlist_head *commitable_paths,
5430 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5431 const char *author, const char *committer,
5432 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5433 got_worktree_status_cb status_cb, void *status_arg,
5434 struct got_repository *repo)
5436 const struct got_error *err = NULL, *unlockerr = NULL;
5437 struct got_pathlist_entry *pe;
5438 const char *head_ref_name = NULL;
5439 struct got_commit_object *head_commit = NULL;
5440 struct got_reference *head_ref2 = NULL;
5441 struct got_object_id *head_commit_id2 = NULL;
5442 struct got_tree_object *head_tree = NULL;
5443 struct got_object_id *new_tree_id = NULL;
5445 struct got_object_id_queue parent_ids;
5446 struct got_object_qid *pid = NULL;
5447 char *logmsg = NULL;
5449 *new_commit_id = NULL;
5451 SIMPLEQ_INIT(&parent_ids);
5453 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5457 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5461 if (commit_msg_cb != NULL) {
5462 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5467 if (logmsg == NULL || strlen(logmsg) == 0) {
5468 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5472 /* Create blobs from added and modified files and record their IDs. */
5473 TAILQ_FOREACH(pe, commitable_paths, entry) {
5474 struct got_commitable *ct = pe->data;
5477 /* Blobs for staged files already exist. */
5478 if (ct->staged_status == GOT_STATUS_ADD ||
5479 ct->staged_status == GOT_STATUS_MODIFY)
5482 if (ct->status != GOT_STATUS_ADD &&
5483 ct->status != GOT_STATUS_MODIFY &&
5484 ct->status != GOT_STATUS_MODE_CHANGE)
5487 if (asprintf(&ondisk_path, "%s/%s",
5488 worktree->root_path, pe->path) == -1) {
5489 err = got_error_from_errno("asprintf");
5492 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5498 /* Recursively write new tree objects. */
5499 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5500 commitable_paths, status_cb, status_arg, repo);
5504 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5507 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5508 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5509 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5510 got_object_qid_free(pid);
5516 /* Check if a concurrent commit to our branch has occurred. */
5517 head_ref_name = got_worktree_get_head_ref_name(worktree);
5518 if (head_ref_name == NULL) {
5519 err = got_error_from_errno("got_worktree_get_head_ref_name");
5522 /* Lock the reference here to prevent concurrent modification. */
5523 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5526 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5529 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5530 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5533 /* Update branch head in repository. */
5534 err = got_ref_change_ref(head_ref2, *new_commit_id);
5537 err = got_ref_write(head_ref2, repo);
5541 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5545 err = ref_base_commit(worktree, repo);
5550 got_object_tree_close(head_tree);
5552 got_object_commit_close(head_commit);
5553 free(head_commit_id2);
5555 unlockerr = got_ref_unlock(head_ref2);
5556 if (unlockerr && err == NULL)
5558 got_ref_close(head_ref2);
5563 static const struct got_error *
5564 check_path_is_commitable(const char *path,
5565 struct got_pathlist_head *commitable_paths)
5567 struct got_pathlist_entry *cpe = NULL;
5568 size_t path_len = strlen(path);
5570 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5571 struct got_commitable *ct = cpe->data;
5572 const char *ct_path = ct->path;
5574 while (ct_path[0] == '/')
5577 if (strcmp(path, ct_path) == 0 ||
5578 got_path_is_child(ct_path, path, path_len))
5583 return got_error_path(path, GOT_ERR_BAD_PATH);
5588 static const struct got_error *
5589 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5591 int *have_staged_files = arg;
5593 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5594 *have_staged_files = 1;
5595 return got_error(GOT_ERR_CANCELLED);
5601 static const struct got_error *
5602 check_non_staged_files(struct got_fileindex *fileindex,
5603 struct got_pathlist_head *paths)
5605 struct got_pathlist_entry *pe;
5606 struct got_fileindex_entry *ie;
5608 TAILQ_FOREACH(pe, paths, entry) {
5609 if (pe->path[0] == '\0')
5611 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5613 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5614 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5615 return got_error_path(pe->path,
5616 GOT_ERR_FILE_NOT_STAGED);
5622 const struct got_error *
5623 got_worktree_commit(struct got_object_id **new_commit_id,
5624 struct got_worktree *worktree, struct got_pathlist_head *paths,
5625 const char *author, const char *committer, int allow_bad_symlinks,
5626 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5627 got_worktree_status_cb status_cb, void *status_arg,
5628 struct got_repository *repo)
5630 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5631 struct got_fileindex *fileindex = NULL;
5632 char *fileindex_path = NULL;
5633 struct got_pathlist_head commitable_paths;
5634 struct collect_commitables_arg cc_arg;
5635 struct got_pathlist_entry *pe;
5636 struct got_reference *head_ref = NULL;
5637 struct got_object_id *head_commit_id = NULL;
5638 int have_staged_files = 0;
5640 *new_commit_id = NULL;
5642 TAILQ_INIT(&commitable_paths);
5644 err = lock_worktree(worktree, LOCK_EX);
5648 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5652 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5656 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5660 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5661 &have_staged_files);
5662 if (err && err->code != GOT_ERR_CANCELLED)
5664 if (have_staged_files) {
5665 err = check_non_staged_files(fileindex, paths);
5670 cc_arg.commitable_paths = &commitable_paths;
5671 cc_arg.worktree = worktree;
5672 cc_arg.fileindex = fileindex;
5674 cc_arg.have_staged_files = have_staged_files;
5675 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5676 TAILQ_FOREACH(pe, paths, entry) {
5677 err = worktree_status(worktree, pe->path, fileindex, repo,
5678 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5683 if (TAILQ_EMPTY(&commitable_paths)) {
5684 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5688 TAILQ_FOREACH(pe, paths, entry) {
5689 err = check_path_is_commitable(pe->path, &commitable_paths);
5694 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5695 struct got_commitable *ct = pe->data;
5696 const char *ct_path = ct->in_repo_path;
5698 while (ct_path[0] == '/')
5700 err = check_out_of_date(ct_path, ct->status,
5701 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5702 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5708 err = commit_worktree(new_commit_id, &commitable_paths,
5709 head_commit_id, worktree, author, committer,
5710 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5714 err = update_fileindex_after_commit(worktree, &commitable_paths,
5715 *new_commit_id, fileindex, have_staged_files);
5716 sync_err = sync_fileindex(fileindex, fileindex_path);
5717 if (sync_err && err == NULL)
5721 got_fileindex_free(fileindex);
5722 free(fileindex_path);
5723 unlockerr = lock_worktree(worktree, LOCK_SH);
5724 if (unlockerr && err == NULL)
5726 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5727 struct got_commitable *ct = pe->data;
5728 free_commitable(ct);
5730 got_pathlist_free(&commitable_paths);
5735 got_commitable_get_path(struct got_commitable *ct)
5741 got_commitable_get_status(struct got_commitable *ct)
5746 struct check_rebase_ok_arg {
5747 struct got_worktree *worktree;
5748 struct got_repository *repo;
5751 static const struct got_error *
5752 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5754 const struct got_error *err = NULL;
5755 struct check_rebase_ok_arg *a = arg;
5756 unsigned char status;
5760 /* Reject rebase of a work tree with mixed base commits. */
5761 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5762 SHA1_DIGEST_LENGTH))
5763 return got_error(GOT_ERR_MIXED_COMMITS);
5765 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5767 return got_error_from_errno("asprintf");
5769 /* Reject rebase of a work tree with modified or staged files. */
5770 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5775 if (status != GOT_STATUS_NO_CHANGE)
5776 return got_error(GOT_ERR_MODIFIED);
5777 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5778 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5783 const struct got_error *
5784 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5785 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5786 struct got_worktree *worktree, struct got_reference *branch,
5787 struct got_repository *repo)
5789 const struct got_error *err = NULL;
5790 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5791 char *branch_ref_name = NULL;
5792 char *fileindex_path = NULL;
5793 struct check_rebase_ok_arg ok_arg;
5794 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5795 struct got_object_id *wt_branch_tip = NULL;
5797 *new_base_branch_ref = NULL;
5801 err = lock_worktree(worktree, LOCK_EX);
5805 err = open_fileindex(fileindex, &fileindex_path, worktree);
5809 ok_arg.worktree = worktree;
5811 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5816 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5820 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5824 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5828 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5833 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5836 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5837 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5841 err = got_ref_alloc_symref(new_base_branch_ref,
5842 new_base_branch_ref_name, wt_branch);
5845 err = got_ref_write(*new_base_branch_ref, repo);
5849 /* TODO Lock original branch's ref while rebasing? */
5851 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5855 err = got_ref_write(branch_ref, repo);
5859 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5860 worktree->base_commit_id);
5863 err = got_ref_write(*tmp_branch, repo);
5867 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5871 free(fileindex_path);
5872 free(tmp_branch_name);
5873 free(new_base_branch_ref_name);
5874 free(branch_ref_name);
5876 got_ref_close(branch_ref);
5878 got_ref_close(wt_branch);
5879 free(wt_branch_tip);
5881 if (*new_base_branch_ref) {
5882 got_ref_close(*new_base_branch_ref);
5883 *new_base_branch_ref = NULL;
5886 got_ref_close(*tmp_branch);
5890 got_fileindex_free(*fileindex);
5893 lock_worktree(worktree, LOCK_SH);
5898 const struct got_error *
5899 got_worktree_rebase_continue(struct got_object_id **commit_id,
5900 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5901 struct got_reference **branch, struct got_fileindex **fileindex,
5902 struct got_worktree *worktree, struct got_repository *repo)
5904 const struct got_error *err;
5905 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5906 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5907 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5908 char *fileindex_path = NULL;
5909 int have_staged_files = 0;
5912 *new_base_branch = NULL;
5917 err = lock_worktree(worktree, LOCK_EX);
5921 err = open_fileindex(fileindex, &fileindex_path, worktree);
5925 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5926 &have_staged_files);
5927 if (err && err->code != GOT_ERR_CANCELLED)
5929 if (have_staged_files) {
5930 err = got_error(GOT_ERR_STAGED_PATHS);
5934 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5938 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5942 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5946 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5950 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5954 err = got_ref_open(branch, repo,
5955 got_ref_get_symref_target(branch_ref), 0);
5959 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5963 err = got_ref_resolve(commit_id, repo, commit_ref);
5967 err = got_ref_open(new_base_branch, repo,
5968 new_base_branch_ref_name, 0);
5972 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5976 free(commit_ref_name);
5977 free(branch_ref_name);
5978 free(fileindex_path);
5980 got_ref_close(commit_ref);
5982 got_ref_close(branch_ref);
5987 got_ref_close(*tmp_branch);
5990 if (*new_base_branch) {
5991 got_ref_close(*new_base_branch);
5992 *new_base_branch = NULL;
5995 got_ref_close(*branch);
5999 got_fileindex_free(*fileindex);
6002 lock_worktree(worktree, LOCK_SH);
6007 const struct got_error *
6008 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6010 const struct got_error *err;
6011 char *tmp_branch_name = NULL;
6013 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6017 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6018 free(tmp_branch_name);
6022 static const struct got_error *
6023 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6024 char **logmsg, void *arg)
6030 static const struct got_error *
6031 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6032 const char *path, struct got_object_id *blob_id,
6033 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6034 int dirfd, const char *de_name)
6039 struct collect_merged_paths_arg {
6040 got_worktree_checkout_cb progress_cb;
6042 struct got_pathlist_head *merged_paths;
6045 static const struct got_error *
6046 collect_merged_paths(void *arg, unsigned char status, const char *path)
6048 const struct got_error *err;
6049 struct collect_merged_paths_arg *a = arg;
6051 struct got_pathlist_entry *new;
6053 err = (*a->progress_cb)(a->progress_arg, status, path);
6057 if (status != GOT_STATUS_MERGE &&
6058 status != GOT_STATUS_ADD &&
6059 status != GOT_STATUS_DELETE &&
6060 status != GOT_STATUS_CONFLICT)
6065 return got_error_from_errno("strdup");
6067 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6068 if (err || new == NULL)
6074 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6076 struct got_pathlist_entry *pe;
6078 TAILQ_FOREACH(pe, merged_paths, entry)
6079 free((char *)pe->path);
6081 got_pathlist_free(merged_paths);
6084 static const struct got_error *
6085 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6086 int is_rebase, struct got_repository *repo)
6088 const struct got_error *err;
6089 struct got_reference *commit_ref = NULL;
6091 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6093 if (err->code != GOT_ERR_NOT_REF)
6095 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6098 err = got_ref_write(commit_ref, repo);
6101 } else if (is_rebase) {
6102 struct got_object_id *stored_id;
6105 err = got_ref_resolve(&stored_id, repo, commit_ref);
6108 cmp = got_object_id_cmp(commit_id, stored_id);
6111 err = got_error(GOT_ERR_REBASE_COMMITID);
6117 got_ref_close(commit_ref);
6121 static const struct got_error *
6122 rebase_merge_files(struct got_pathlist_head *merged_paths,
6123 const char *commit_ref_name, struct got_worktree *worktree,
6124 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6125 struct got_object_id *commit_id, struct got_repository *repo,
6126 got_worktree_checkout_cb progress_cb, void *progress_arg,
6127 got_cancel_cb cancel_cb, void *cancel_arg)
6129 const struct got_error *err;
6130 struct got_reference *commit_ref = NULL;
6131 struct collect_merged_paths_arg cmp_arg;
6132 char *fileindex_path;
6134 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6136 err = get_fileindex_path(&fileindex_path, worktree);
6140 cmp_arg.progress_cb = progress_cb;
6141 cmp_arg.progress_arg = progress_arg;
6142 cmp_arg.merged_paths = merged_paths;
6143 err = merge_files(worktree, fileindex, fileindex_path,
6144 parent_commit_id, commit_id, repo, collect_merged_paths,
6145 &cmp_arg, cancel_cb, cancel_arg);
6147 got_ref_close(commit_ref);
6151 const struct got_error *
6152 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6153 struct got_worktree *worktree, struct got_fileindex *fileindex,
6154 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6155 struct got_repository *repo,
6156 got_worktree_checkout_cb progress_cb, void *progress_arg,
6157 got_cancel_cb cancel_cb, void *cancel_arg)
6159 const struct got_error *err;
6160 char *commit_ref_name;
6162 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6166 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6170 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6171 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6172 progress_arg, cancel_cb, cancel_arg);
6174 free(commit_ref_name);
6178 const struct got_error *
6179 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6180 struct got_worktree *worktree, struct got_fileindex *fileindex,
6181 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6182 struct got_repository *repo,
6183 got_worktree_checkout_cb progress_cb, void *progress_arg,
6184 got_cancel_cb cancel_cb, void *cancel_arg)
6186 const struct got_error *err;
6187 char *commit_ref_name;
6189 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6193 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6197 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6198 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6199 progress_arg, cancel_cb, cancel_arg);
6201 free(commit_ref_name);
6205 static const struct got_error *
6206 rebase_commit(struct got_object_id **new_commit_id,
6207 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6208 struct got_worktree *worktree, struct got_fileindex *fileindex,
6209 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6210 const char *new_logmsg, struct got_repository *repo)
6212 const struct got_error *err, *sync_err;
6213 struct got_pathlist_head commitable_paths;
6214 struct collect_commitables_arg cc_arg;
6215 char *fileindex_path = NULL;
6216 struct got_reference *head_ref = NULL;
6217 struct got_object_id *head_commit_id = NULL;
6218 char *logmsg = NULL;
6220 TAILQ_INIT(&commitable_paths);
6221 *new_commit_id = NULL;
6223 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6225 err = get_fileindex_path(&fileindex_path, worktree);
6229 cc_arg.commitable_paths = &commitable_paths;
6230 cc_arg.worktree = worktree;
6232 cc_arg.have_staged_files = 0;
6234 * If possible get the status of individual files directly to
6235 * avoid crawling the entire work tree once per rebased commit.
6236 * TODO: Ideally, merged_paths would contain a list of commitables
6237 * we could use so we could skip worktree_status() entirely.
6240 struct got_pathlist_entry *pe;
6241 TAILQ_FOREACH(pe, merged_paths, entry) {
6242 err = worktree_status(worktree, pe->path, fileindex,
6243 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6249 err = worktree_status(worktree, "", fileindex, repo,
6250 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6255 if (TAILQ_EMPTY(&commitable_paths)) {
6256 /* No-op change; commit will be elided. */
6257 err = got_ref_delete(commit_ref, repo);
6260 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6264 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6268 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6273 logmsg = strdup(new_logmsg);
6274 if (logmsg == NULL) {
6275 err = got_error_from_errno("strdup");
6279 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6284 /* NB: commit_worktree will call free(logmsg) */
6285 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6286 worktree, got_object_commit_get_author(orig_commit),
6287 got_object_commit_get_committer(orig_commit),
6288 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6292 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6296 err = got_ref_delete(commit_ref, repo);
6300 err = update_fileindex_after_commit(worktree, &commitable_paths,
6301 *new_commit_id, fileindex, 0);
6302 sync_err = sync_fileindex(fileindex, fileindex_path);
6303 if (sync_err && err == NULL)
6306 free(fileindex_path);
6307 free(head_commit_id);
6309 got_ref_close(head_ref);
6311 free(*new_commit_id);
6312 *new_commit_id = NULL;
6317 const struct got_error *
6318 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6319 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6320 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6321 struct got_commit_object *orig_commit,
6322 struct got_object_id *orig_commit_id, struct got_repository *repo)
6324 const struct got_error *err;
6325 char *commit_ref_name;
6326 struct got_reference *commit_ref = NULL;
6327 struct got_object_id *commit_id = NULL;
6329 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6333 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6336 err = got_ref_resolve(&commit_id, repo, commit_ref);
6339 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6340 err = got_error(GOT_ERR_REBASE_COMMITID);
6344 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6345 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6348 got_ref_close(commit_ref);
6349 free(commit_ref_name);
6354 const struct got_error *
6355 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6356 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6357 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6358 struct got_commit_object *orig_commit,
6359 struct got_object_id *orig_commit_id, const char *new_logmsg,
6360 struct got_repository *repo)
6362 const struct got_error *err;
6363 char *commit_ref_name;
6364 struct got_reference *commit_ref = NULL;
6366 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6370 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6374 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6375 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6378 got_ref_close(commit_ref);
6379 free(commit_ref_name);
6383 const struct got_error *
6384 got_worktree_rebase_postpone(struct got_worktree *worktree,
6385 struct got_fileindex *fileindex)
6388 got_fileindex_free(fileindex);
6389 return lock_worktree(worktree, LOCK_SH);
6392 static const struct got_error *
6393 delete_ref(const char *name, struct got_repository *repo)
6395 const struct got_error *err;
6396 struct got_reference *ref;
6398 err = got_ref_open(&ref, repo, name, 0);
6400 if (err->code == GOT_ERR_NOT_REF)
6405 err = got_ref_delete(ref, repo);
6410 static const struct got_error *
6411 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6413 const struct got_error *err;
6414 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6415 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6417 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6420 err = delete_ref(tmp_branch_name, repo);
6424 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6427 err = delete_ref(new_base_branch_ref_name, repo);
6431 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6434 err = delete_ref(branch_ref_name, repo);
6438 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6441 err = delete_ref(commit_ref_name, repo);
6446 free(tmp_branch_name);
6447 free(new_base_branch_ref_name);
6448 free(branch_ref_name);
6449 free(commit_ref_name);
6453 const struct got_error *
6454 got_worktree_rebase_complete(struct got_worktree *worktree,
6455 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6456 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6457 struct got_repository *repo)
6459 const struct got_error *err, *unlockerr, *sync_err;
6460 struct got_object_id *new_head_commit_id = NULL;
6461 char *fileindex_path = NULL;
6463 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6467 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6471 err = got_ref_write(rebased_branch, repo);
6475 err = got_worktree_set_head_ref(worktree, rebased_branch);
6479 err = delete_rebase_refs(worktree, repo);
6483 err = get_fileindex_path(&fileindex_path, worktree);
6486 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6487 sync_err = sync_fileindex(fileindex, fileindex_path);
6488 if (sync_err && err == NULL)
6491 got_fileindex_free(fileindex);
6492 free(fileindex_path);
6493 free(new_head_commit_id);
6494 unlockerr = lock_worktree(worktree, LOCK_SH);
6495 if (unlockerr && err == NULL)
6500 const struct got_error *
6501 got_worktree_rebase_abort(struct got_worktree *worktree,
6502 struct got_fileindex *fileindex, struct got_repository *repo,
6503 struct got_reference *new_base_branch,
6504 got_worktree_checkout_cb progress_cb, void *progress_arg)
6506 const struct got_error *err, *unlockerr, *sync_err;
6507 struct got_reference *resolved = NULL;
6508 struct got_object_id *commit_id = NULL;
6509 char *fileindex_path = NULL;
6510 struct revert_file_args rfa;
6511 struct got_object_id *tree_id = NULL;
6513 err = lock_worktree(worktree, LOCK_EX);
6517 err = got_ref_open(&resolved, repo,
6518 got_ref_get_symref_target(new_base_branch), 0);
6522 err = got_worktree_set_head_ref(worktree, resolved);
6527 * XXX commits to the base branch could have happened while
6528 * we were busy rebasing; should we store the original commit ID
6529 * when rebase begins and read it back here?
6531 err = got_ref_resolve(&commit_id, repo, resolved);
6535 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6539 err = got_object_id_by_path(&tree_id, repo,
6540 worktree->base_commit_id, worktree->path_prefix);
6544 err = delete_rebase_refs(worktree, repo);
6548 err = get_fileindex_path(&fileindex_path, worktree);
6552 rfa.worktree = worktree;
6553 rfa.fileindex = fileindex;
6554 rfa.progress_cb = progress_cb;
6555 rfa.progress_arg = progress_arg;
6556 rfa.patch_cb = NULL;
6557 rfa.patch_arg = NULL;
6559 err = worktree_status(worktree, "", fileindex, repo,
6560 revert_file, &rfa, NULL, NULL, 0, 0);
6564 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6565 repo, progress_cb, progress_arg, NULL, NULL);
6567 sync_err = sync_fileindex(fileindex, fileindex_path);
6568 if (sync_err && err == NULL)
6571 got_ref_close(resolved);
6575 got_fileindex_free(fileindex);
6576 free(fileindex_path);
6578 unlockerr = lock_worktree(worktree, LOCK_SH);
6579 if (unlockerr && err == NULL)
6584 const struct got_error *
6585 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6586 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6587 struct got_fileindex **fileindex, struct got_worktree *worktree,
6588 struct got_repository *repo)
6590 const struct got_error *err = NULL;
6591 char *tmp_branch_name = NULL;
6592 char *branch_ref_name = NULL;
6593 char *base_commit_ref_name = NULL;
6594 char *fileindex_path = NULL;
6595 struct check_rebase_ok_arg ok_arg;
6596 struct got_reference *wt_branch = NULL;
6597 struct got_reference *base_commit_ref = NULL;
6601 *base_commit_id = NULL;
6604 err = lock_worktree(worktree, LOCK_EX);
6608 err = open_fileindex(fileindex, &fileindex_path, worktree);
6612 ok_arg.worktree = worktree;
6614 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6619 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6623 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6627 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6632 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6637 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6641 err = got_ref_write(*branch_ref, repo);
6645 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6646 worktree->base_commit_id);
6649 err = got_ref_write(base_commit_ref, repo);
6652 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6653 if (*base_commit_id == NULL) {
6654 err = got_error_from_errno("got_object_id_dup");
6658 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6659 worktree->base_commit_id);
6662 err = got_ref_write(*tmp_branch, repo);
6666 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6670 free(fileindex_path);
6671 free(tmp_branch_name);
6672 free(branch_ref_name);
6673 free(base_commit_ref_name);
6675 got_ref_close(wt_branch);
6678 got_ref_close(*branch_ref);
6682 got_ref_close(*tmp_branch);
6685 free(*base_commit_id);
6687 got_fileindex_free(*fileindex);
6690 lock_worktree(worktree, LOCK_SH);
6695 const struct got_error *
6696 got_worktree_histedit_postpone(struct got_worktree *worktree,
6697 struct got_fileindex *fileindex)
6700 got_fileindex_free(fileindex);
6701 return lock_worktree(worktree, LOCK_SH);
6704 const struct got_error *
6705 got_worktree_histedit_in_progress(int *in_progress,
6706 struct got_worktree *worktree)
6708 const struct got_error *err;
6709 char *tmp_branch_name = NULL;
6711 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6715 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6716 free(tmp_branch_name);
6720 const struct got_error *
6721 got_worktree_histedit_continue(struct got_object_id **commit_id,
6722 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6723 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6724 struct got_worktree *worktree, struct got_repository *repo)
6726 const struct got_error *err;
6727 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6728 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6729 struct got_reference *commit_ref = NULL;
6730 struct got_reference *base_commit_ref = NULL;
6731 char *fileindex_path = NULL;
6732 int have_staged_files = 0;
6736 *base_commit_id = NULL;
6739 err = lock_worktree(worktree, LOCK_EX);
6743 err = open_fileindex(fileindex, &fileindex_path, worktree);
6747 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6748 &have_staged_files);
6749 if (err && err->code != GOT_ERR_CANCELLED)
6751 if (have_staged_files) {
6752 err = got_error(GOT_ERR_STAGED_PATHS);
6756 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6760 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6764 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6768 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6773 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6777 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6780 err = got_ref_resolve(commit_id, repo, commit_ref);
6784 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6787 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6791 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6795 free(commit_ref_name);
6796 free(branch_ref_name);
6797 free(fileindex_path);
6799 got_ref_close(commit_ref);
6800 if (base_commit_ref)
6801 got_ref_close(base_commit_ref);
6805 free(*base_commit_id);
6806 *base_commit_id = NULL;
6808 got_ref_close(*tmp_branch);
6812 got_fileindex_free(*fileindex);
6815 lock_worktree(worktree, LOCK_EX);
6820 static const struct got_error *
6821 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6823 const struct got_error *err;
6824 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6825 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6827 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6830 err = delete_ref(tmp_branch_name, repo);
6834 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6838 err = delete_ref(base_commit_ref_name, repo);
6842 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6845 err = delete_ref(branch_ref_name, repo);
6849 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6852 err = delete_ref(commit_ref_name, repo);
6856 free(tmp_branch_name);
6857 free(base_commit_ref_name);
6858 free(branch_ref_name);
6859 free(commit_ref_name);
6863 const struct got_error *
6864 got_worktree_histedit_abort(struct got_worktree *worktree,
6865 struct got_fileindex *fileindex, struct got_repository *repo,
6866 struct got_reference *branch, struct got_object_id *base_commit_id,
6867 got_worktree_checkout_cb progress_cb, void *progress_arg)
6869 const struct got_error *err, *unlockerr, *sync_err;
6870 struct got_reference *resolved = NULL;
6871 char *fileindex_path = NULL;
6872 struct got_object_id *tree_id = NULL;
6873 struct revert_file_args rfa;
6875 err = lock_worktree(worktree, LOCK_EX);
6879 err = got_ref_open(&resolved, repo,
6880 got_ref_get_symref_target(branch), 0);
6884 err = got_worktree_set_head_ref(worktree, resolved);
6888 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6892 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6893 worktree->path_prefix);
6897 err = delete_histedit_refs(worktree, repo);
6901 err = get_fileindex_path(&fileindex_path, worktree);
6905 rfa.worktree = worktree;
6906 rfa.fileindex = fileindex;
6907 rfa.progress_cb = progress_cb;
6908 rfa.progress_arg = progress_arg;
6909 rfa.patch_cb = NULL;
6910 rfa.patch_arg = NULL;
6912 err = worktree_status(worktree, "", fileindex, repo,
6913 revert_file, &rfa, NULL, NULL, 0, 0);
6917 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6918 repo, progress_cb, progress_arg, NULL, NULL);
6920 sync_err = sync_fileindex(fileindex, fileindex_path);
6921 if (sync_err && err == NULL)
6924 got_ref_close(resolved);
6926 free(fileindex_path);
6928 unlockerr = lock_worktree(worktree, LOCK_SH);
6929 if (unlockerr && err == NULL)
6934 const struct got_error *
6935 got_worktree_histedit_complete(struct got_worktree *worktree,
6936 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6937 struct got_reference *edited_branch, struct got_repository *repo)
6939 const struct got_error *err, *unlockerr, *sync_err;
6940 struct got_object_id *new_head_commit_id = NULL;
6941 struct got_reference *resolved = NULL;
6942 char *fileindex_path = NULL;
6944 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6948 err = got_ref_open(&resolved, repo,
6949 got_ref_get_symref_target(edited_branch), 0);
6953 err = got_ref_change_ref(resolved, new_head_commit_id);
6957 err = got_ref_write(resolved, repo);
6961 err = got_worktree_set_head_ref(worktree, resolved);
6965 err = delete_histedit_refs(worktree, repo);
6969 err = get_fileindex_path(&fileindex_path, worktree);
6972 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6973 sync_err = sync_fileindex(fileindex, fileindex_path);
6974 if (sync_err && err == NULL)
6977 got_fileindex_free(fileindex);
6978 free(fileindex_path);
6979 free(new_head_commit_id);
6980 unlockerr = lock_worktree(worktree, LOCK_SH);
6981 if (unlockerr && err == NULL)
6986 const struct got_error *
6987 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6988 struct got_object_id *commit_id, struct got_repository *repo)
6990 const struct got_error *err;
6991 char *commit_ref_name;
6993 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6997 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7001 err = delete_ref(commit_ref_name, repo);
7003 free(commit_ref_name);
7007 const struct got_error *
7008 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7009 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7010 struct got_worktree *worktree, const char *refname,
7011 struct got_repository *repo)
7013 const struct got_error *err = NULL;
7014 char *fileindex_path = NULL;
7015 struct check_rebase_ok_arg ok_arg;
7019 *base_branch_ref = NULL;
7021 err = lock_worktree(worktree, LOCK_EX);
7025 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7026 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7027 "cannot integrate a branch into itself; "
7028 "update -b or different branch name required");
7032 err = open_fileindex(fileindex, &fileindex_path, worktree);
7036 /* Preconditions are the same as for rebase. */
7037 ok_arg.worktree = worktree;
7039 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7044 err = got_ref_open(branch_ref, repo, refname, 1);
7048 err = got_ref_open(base_branch_ref, repo,
7049 got_worktree_get_head_ref_name(worktree), 1);
7053 got_ref_close(*branch_ref);
7056 if (*base_branch_ref) {
7057 got_ref_close(*base_branch_ref);
7058 *base_branch_ref = NULL;
7061 got_fileindex_free(*fileindex);
7064 lock_worktree(worktree, LOCK_SH);
7069 const struct got_error *
7070 got_worktree_integrate_continue(struct got_worktree *worktree,
7071 struct got_fileindex *fileindex, struct got_repository *repo,
7072 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7073 got_worktree_checkout_cb progress_cb, void *progress_arg,
7074 got_cancel_cb cancel_cb, void *cancel_arg)
7076 const struct got_error *err = NULL, *sync_err, *unlockerr;
7077 char *fileindex_path = NULL;
7078 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7080 err = get_fileindex_path(&fileindex_path, worktree);
7084 err = got_ref_resolve(&commit_id, repo, branch_ref);
7088 err = got_object_id_by_path(&tree_id, repo, commit_id,
7089 worktree->path_prefix);
7093 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7097 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7098 progress_cb, progress_arg, cancel_cb, cancel_arg);
7102 err = got_ref_change_ref(base_branch_ref, commit_id);
7106 err = got_ref_write(base_branch_ref, repo);
7108 sync_err = sync_fileindex(fileindex, fileindex_path);
7109 if (sync_err && err == NULL)
7113 unlockerr = got_ref_unlock(branch_ref);
7114 if (unlockerr && err == NULL)
7116 got_ref_close(branch_ref);
7118 unlockerr = got_ref_unlock(base_branch_ref);
7119 if (unlockerr && err == NULL)
7121 got_ref_close(base_branch_ref);
7123 got_fileindex_free(fileindex);
7124 free(fileindex_path);
7127 unlockerr = lock_worktree(worktree, LOCK_SH);
7128 if (unlockerr && err == NULL)
7133 const struct got_error *
7134 got_worktree_integrate_abort(struct got_worktree *worktree,
7135 struct got_fileindex *fileindex, struct got_repository *repo,
7136 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7138 const struct got_error *err = NULL, *unlockerr = NULL;
7140 got_fileindex_free(fileindex);
7142 err = lock_worktree(worktree, LOCK_SH);
7144 unlockerr = got_ref_unlock(branch_ref);
7145 if (unlockerr && err == NULL)
7147 got_ref_close(branch_ref);
7149 unlockerr = got_ref_unlock(base_branch_ref);
7150 if (unlockerr && err == NULL)
7152 got_ref_close(base_branch_ref);
7157 struct check_stage_ok_arg {
7158 struct got_object_id *head_commit_id;
7159 struct got_worktree *worktree;
7160 struct got_fileindex *fileindex;
7161 struct got_repository *repo;
7165 const struct got_error *
7166 check_stage_ok(void *arg, unsigned char status,
7167 unsigned char staged_status, const char *relpath,
7168 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7169 struct got_object_id *commit_id, int dirfd, const char *de_name)
7171 struct check_stage_ok_arg *a = arg;
7172 const struct got_error *err = NULL;
7173 struct got_fileindex_entry *ie;
7174 struct got_object_id base_commit_id;
7175 struct got_object_id *base_commit_idp = NULL;
7176 char *in_repo_path = NULL, *p;
7178 if (status == GOT_STATUS_UNVERSIONED ||
7179 status == GOT_STATUS_NO_CHANGE)
7181 if (status == GOT_STATUS_NONEXISTENT)
7182 return got_error_set_errno(ENOENT, relpath);
7184 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7186 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7188 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7189 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7191 return got_error_from_errno("asprintf");
7193 if (got_fileindex_entry_has_commit(ie)) {
7194 memcpy(base_commit_id.sha1, ie->commit_sha1,
7195 SHA1_DIGEST_LENGTH);
7196 base_commit_idp = &base_commit_id;
7199 if (status == GOT_STATUS_CONFLICT) {
7200 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7202 } else if (status != GOT_STATUS_ADD &&
7203 status != GOT_STATUS_MODIFY &&
7204 status != GOT_STATUS_DELETE) {
7205 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7209 a->have_changes = 1;
7214 err = check_out_of_date(p, status, staged_status,
7215 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7216 GOT_ERR_STAGE_OUT_OF_DATE);
7222 struct stage_path_arg {
7223 struct got_worktree *worktree;
7224 struct got_fileindex *fileindex;
7225 struct got_repository *repo;
7226 got_worktree_status_cb status_cb;
7228 got_worktree_patch_cb patch_cb;
7230 int staged_something;
7231 int allow_bad_symlinks;
7234 static const struct got_error *
7235 stage_path(void *arg, unsigned char status,
7236 unsigned char staged_status, const char *relpath,
7237 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7238 struct got_object_id *commit_id, int dirfd, const char *de_name)
7240 struct stage_path_arg *a = arg;
7241 const struct got_error *err = NULL;
7242 struct got_fileindex_entry *ie;
7243 char *ondisk_path = NULL, *path_content = NULL;
7245 struct got_object_id *new_staged_blob_id = NULL;
7248 if (status == GOT_STATUS_UNVERSIONED)
7251 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7253 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7255 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7257 return got_error_from_errno("asprintf");
7260 case GOT_STATUS_ADD:
7261 case GOT_STATUS_MODIFY:
7262 /* XXX could sb.st_mode be passed in by our caller? */
7263 if (lstat(ondisk_path, &sb) == -1) {
7264 err = got_error_from_errno2("lstat", ondisk_path);
7268 if (status == GOT_STATUS_ADD) {
7269 int choice = GOT_PATCH_CHOICE_NONE;
7270 err = (*a->patch_cb)(&choice, a->patch_arg,
7271 status, ie->path, NULL, 1, 1);
7274 if (choice != GOT_PATCH_CHOICE_YES)
7277 err = create_patched_content(&path_content, 0,
7278 staged_blob_id ? staged_blob_id : blob_id,
7279 ondisk_path, dirfd, de_name, ie->path,
7280 a->repo, a->patch_cb, a->patch_arg);
7281 if (err || path_content == NULL)
7285 err = got_object_blob_create(&new_staged_blob_id,
7286 path_content ? path_content : ondisk_path, a->repo);
7289 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7290 SHA1_DIGEST_LENGTH);
7291 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7292 stage = GOT_FILEIDX_STAGE_ADD;
7294 stage = GOT_FILEIDX_STAGE_MODIFY;
7295 got_fileindex_entry_stage_set(ie, stage);
7296 if (S_ISLNK(sb.st_mode)) {
7297 int is_bad_symlink = 0;
7298 if (!a->allow_bad_symlinks) {
7299 char target_path[PATH_MAX];
7301 target_len = readlink(ondisk_path, target_path,
7302 sizeof(target_path));
7303 if (target_len == -1) {
7304 err = got_error_from_errno2("readlink",
7308 err = is_bad_symlink_target(&is_bad_symlink,
7309 target_path, target_len, ondisk_path,
7310 a->worktree->root_path);
7313 if (is_bad_symlink) {
7314 err = got_error_path(ondisk_path,
7315 GOT_ERR_BAD_SYMLINK);
7320 got_fileindex_entry_staged_filetype_set(ie,
7321 GOT_FILEIDX_MODE_BAD_SYMLINK);
7323 got_fileindex_entry_staged_filetype_set(ie,
7324 GOT_FILEIDX_MODE_SYMLINK);
7326 got_fileindex_entry_staged_filetype_set(ie,
7327 GOT_FILEIDX_MODE_REGULAR_FILE);
7329 a->staged_something = 1;
7330 if (a->status_cb == NULL)
7332 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7333 get_staged_status(ie), relpath, blob_id,
7334 new_staged_blob_id, NULL, dirfd, de_name);
7336 case GOT_STATUS_DELETE:
7337 if (staged_status == GOT_STATUS_DELETE)
7340 int choice = GOT_PATCH_CHOICE_NONE;
7341 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7342 ie->path, NULL, 1, 1);
7345 if (choice == GOT_PATCH_CHOICE_NO)
7347 if (choice != GOT_PATCH_CHOICE_YES) {
7348 err = got_error(GOT_ERR_PATCH_CHOICE);
7352 stage = GOT_FILEIDX_STAGE_DELETE;
7353 got_fileindex_entry_stage_set(ie, stage);
7354 a->staged_something = 1;
7355 if (a->status_cb == NULL)
7357 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7358 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7361 case GOT_STATUS_NO_CHANGE:
7363 case GOT_STATUS_CONFLICT:
7364 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7366 case GOT_STATUS_NONEXISTENT:
7367 err = got_error_set_errno(ENOENT, relpath);
7370 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7374 if (path_content && unlink(path_content) == -1 && err == NULL)
7375 err = got_error_from_errno2("unlink", path_content);
7378 free(new_staged_blob_id);
7382 const struct got_error *
7383 got_worktree_stage(struct got_worktree *worktree,
7384 struct got_pathlist_head *paths,
7385 got_worktree_status_cb status_cb, void *status_arg,
7386 got_worktree_patch_cb patch_cb, void *patch_arg,
7387 int allow_bad_symlinks, struct got_repository *repo)
7389 const struct got_error *err = NULL, *sync_err, *unlockerr;
7390 struct got_pathlist_entry *pe;
7391 struct got_fileindex *fileindex = NULL;
7392 char *fileindex_path = NULL;
7393 struct got_reference *head_ref = NULL;
7394 struct got_object_id *head_commit_id = NULL;
7395 struct check_stage_ok_arg oka;
7396 struct stage_path_arg spa;
7398 err = lock_worktree(worktree, LOCK_EX);
7402 err = got_ref_open(&head_ref, repo,
7403 got_worktree_get_head_ref_name(worktree), 0);
7406 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7409 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7413 /* Check pre-conditions before staging anything. */
7414 oka.head_commit_id = head_commit_id;
7415 oka.worktree = worktree;
7416 oka.fileindex = fileindex;
7418 oka.have_changes = 0;
7419 TAILQ_FOREACH(pe, paths, entry) {
7420 err = worktree_status(worktree, pe->path, fileindex, repo,
7421 check_stage_ok, &oka, NULL, NULL, 0, 0);
7425 if (!oka.have_changes) {
7426 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7430 spa.worktree = worktree;
7431 spa.fileindex = fileindex;
7433 spa.patch_cb = patch_cb;
7434 spa.patch_arg = patch_arg;
7435 spa.status_cb = status_cb;
7436 spa.status_arg = status_arg;
7437 spa.staged_something = 0;
7438 spa.allow_bad_symlinks = allow_bad_symlinks;
7439 TAILQ_FOREACH(pe, paths, entry) {
7440 err = worktree_status(worktree, pe->path, fileindex, repo,
7441 stage_path, &spa, NULL, NULL, 0, 0);
7445 if (!spa.staged_something) {
7446 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7450 sync_err = sync_fileindex(fileindex, fileindex_path);
7451 if (sync_err && err == NULL)
7455 got_ref_close(head_ref);
7456 free(head_commit_id);
7457 free(fileindex_path);
7459 got_fileindex_free(fileindex);
7460 unlockerr = lock_worktree(worktree, LOCK_SH);
7461 if (unlockerr && err == NULL)
7466 struct unstage_path_arg {
7467 struct got_worktree *worktree;
7468 struct got_fileindex *fileindex;
7469 struct got_repository *repo;
7470 got_worktree_checkout_cb progress_cb;
7472 got_worktree_patch_cb patch_cb;
7476 static const struct got_error *
7477 create_unstaged_content(char **path_unstaged_content,
7478 char **path_new_staged_content, struct got_object_id *blob_id,
7479 struct got_object_id *staged_blob_id, const char *relpath,
7480 struct got_repository *repo,
7481 got_worktree_patch_cb patch_cb, void *patch_arg)
7483 const struct got_error *err, *free_err;
7484 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7485 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7486 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7487 struct got_diffreg_result *diffreg_result = NULL;
7488 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7489 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7491 *path_unstaged_content = NULL;
7492 *path_new_staged_content = NULL;
7494 err = got_object_id_str(&label1, blob_id);
7497 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7501 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7505 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7509 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7513 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7517 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7521 err = got_diff_files(&diffreg_result, f1, label1, f2,
7522 path2, 3, 0, 1, NULL);
7526 err = got_opentemp_named(path_unstaged_content, &outfile,
7527 "got-unstaged-content");
7530 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7531 "got-new-staged-content");
7535 if (fseek(f1, 0L, SEEK_SET) == -1) {
7536 err = got_ferror(f1, GOT_ERR_IO);
7539 if (fseek(f2, 0L, SEEK_SET) == -1) {
7540 err = got_ferror(f2, GOT_ERR_IO);
7543 /* Count the number of actual changes in the diff result. */
7544 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7545 struct diff_chunk_context cc = {};
7546 diff_chunk_context_load_change(&cc, &nchunks_used,
7547 diffreg_result->result, n, 0);
7550 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7552 err = apply_or_reject_change(&choice, &nchunks_used,
7553 diffreg_result->result, n, relpath, f1, f2,
7554 &line_cur1, &line_cur2,
7555 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7558 if (choice == GOT_PATCH_CHOICE_YES)
7561 have_rejected_content = 1;
7562 if (choice == GOT_PATCH_CHOICE_QUIT)
7565 if (have_content || have_rejected_content)
7566 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7567 outfile, rejectfile);
7571 got_object_blob_close(blob);
7573 got_object_blob_close(staged_blob);
7574 free_err = got_diffreg_result_free(diffreg_result);
7575 if (free_err && err == NULL)
7577 if (f1 && fclose(f1) == EOF && err == NULL)
7578 err = got_error_from_errno2("fclose", path1);
7579 if (f2 && fclose(f2) == EOF && err == NULL)
7580 err = got_error_from_errno2("fclose", path2);
7581 if (outfile && fclose(outfile) == EOF && err == NULL)
7582 err = got_error_from_errno2("fclose", *path_unstaged_content);
7583 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7584 err = got_error_from_errno2("fclose", *path_new_staged_content);
7585 if (path1 && unlink(path1) == -1 && err == NULL)
7586 err = got_error_from_errno2("unlink", path1);
7587 if (path2 && unlink(path2) == -1 && err == NULL)
7588 err = got_error_from_errno2("unlink", path2);
7589 if (err || !have_content) {
7590 if (*path_unstaged_content &&
7591 unlink(*path_unstaged_content) == -1 && err == NULL)
7592 err = got_error_from_errno2("unlink",
7593 *path_unstaged_content);
7594 free(*path_unstaged_content);
7595 *path_unstaged_content = NULL;
7597 if (err || !have_content || !have_rejected_content) {
7598 if (*path_new_staged_content &&
7599 unlink(*path_new_staged_content) == -1 && err == NULL)
7600 err = got_error_from_errno2("unlink",
7601 *path_new_staged_content);
7602 free(*path_new_staged_content);
7603 *path_new_staged_content = NULL;
7610 static const struct got_error *
7611 unstage_hunks(struct got_object_id *staged_blob_id,
7612 struct got_blob_object *blob_base,
7613 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7614 const char *ondisk_path, const char *label_orig,
7615 struct got_worktree *worktree, struct got_repository *repo,
7616 got_worktree_patch_cb patch_cb, void *patch_arg,
7617 got_worktree_checkout_cb progress_cb, void *progress_arg)
7619 const struct got_error *err = NULL;
7620 char *path_unstaged_content = NULL;
7621 char *path_new_staged_content = NULL;
7622 struct got_object_id *new_staged_blob_id = NULL;
7626 err = create_unstaged_content(&path_unstaged_content,
7627 &path_new_staged_content, blob_id, staged_blob_id,
7628 ie->path, repo, patch_cb, patch_arg);
7632 if (path_unstaged_content == NULL)
7635 if (path_new_staged_content) {
7636 err = got_object_blob_create(&new_staged_blob_id,
7637 path_new_staged_content, repo);
7642 f = fopen(path_unstaged_content, "r");
7644 err = got_error_from_errno2("fopen",
7645 path_unstaged_content);
7648 if (fstat(fileno(f), &sb) == -1) {
7649 err = got_error_from_errno2("fstat", path_unstaged_content);
7652 if (got_fileindex_entry_staged_filetype_get(ie) ==
7653 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7654 char link_target[PATH_MAX];
7656 r = fread(link_target, 1, sizeof(link_target), f);
7657 if (r == 0 && ferror(f)) {
7658 err = got_error_from_errno("fread");
7661 if (r >= sizeof(link_target)) { /* should not happen */
7662 err = got_error(GOT_ERR_NO_SPACE);
7665 link_target[r] = '\0';
7666 err = merge_symlink(worktree, blob_base,
7667 ondisk_path, ie->path, label_orig, link_target,
7668 worktree->base_commit_id, repo, progress_cb,
7671 int local_changes_subsumed;
7672 err = merge_file(&local_changes_subsumed, worktree,
7673 blob_base, ondisk_path, ie->path,
7674 got_fileindex_perms_to_st(ie),
7675 path_unstaged_content, label_orig, "unstaged",
7676 repo, progress_cb, progress_arg);
7681 if (new_staged_blob_id) {
7682 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7683 SHA1_DIGEST_LENGTH);
7685 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7686 got_fileindex_entry_staged_filetype_set(ie, 0);
7689 free(new_staged_blob_id);
7690 if (path_unstaged_content &&
7691 unlink(path_unstaged_content) == -1 && err == NULL)
7692 err = got_error_from_errno2("unlink", path_unstaged_content);
7693 if (path_new_staged_content &&
7694 unlink(path_new_staged_content) == -1 && err == NULL)
7695 err = got_error_from_errno2("unlink", path_new_staged_content);
7696 if (f && fclose(f) != 0 && err == NULL)
7697 err = got_error_from_errno2("fclose", path_unstaged_content);
7698 free(path_unstaged_content);
7699 free(path_new_staged_content);
7703 static const struct got_error *
7704 unstage_path(void *arg, unsigned char status,
7705 unsigned char staged_status, const char *relpath,
7706 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7707 struct got_object_id *commit_id, int dirfd, const char *de_name)
7709 const struct got_error *err = NULL;
7710 struct unstage_path_arg *a = arg;
7711 struct got_fileindex_entry *ie;
7712 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7713 char *ondisk_path = NULL;
7714 char *id_str = NULL, *label_orig = NULL;
7715 int local_changes_subsumed;
7718 if (staged_status != GOT_STATUS_ADD &&
7719 staged_status != GOT_STATUS_MODIFY &&
7720 staged_status != GOT_STATUS_DELETE)
7723 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7725 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7727 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7729 return got_error_from_errno("asprintf");
7731 err = got_object_id_str(&id_str,
7732 commit_id ? commit_id : a->worktree->base_commit_id);
7735 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7737 err = got_error_from_errno("asprintf");
7741 switch (staged_status) {
7742 case GOT_STATUS_MODIFY:
7743 err = got_object_open_as_blob(&blob_base, a->repo,
7748 case GOT_STATUS_ADD:
7750 if (staged_status == GOT_STATUS_ADD) {
7751 int choice = GOT_PATCH_CHOICE_NONE;
7752 err = (*a->patch_cb)(&choice, a->patch_arg,
7753 staged_status, ie->path, NULL, 1, 1);
7756 if (choice != GOT_PATCH_CHOICE_YES)
7759 err = unstage_hunks(staged_blob_id,
7760 blob_base, blob_id, ie, ondisk_path,
7761 label_orig, a->worktree, a->repo,
7762 a->patch_cb, a->patch_arg,
7763 a->progress_cb, a->progress_arg);
7764 break; /* Done with this file. */
7767 err = got_object_open_as_blob(&blob_staged, a->repo,
7768 staged_blob_id, 8192);
7771 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7772 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7773 case GOT_FILEIDX_MODE_REGULAR_FILE:
7774 err = merge_blob(&local_changes_subsumed, a->worktree,
7775 blob_base, ondisk_path, relpath,
7776 got_fileindex_perms_to_st(ie), label_orig,
7777 blob_staged, commit_id ? commit_id :
7778 a->worktree->base_commit_id, a->repo,
7779 a->progress_cb, a->progress_arg);
7781 case GOT_FILEIDX_MODE_SYMLINK:
7782 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7783 char *staged_target;
7784 err = got_object_blob_read_to_str(
7785 &staged_target, blob_staged);
7788 err = merge_symlink(a->worktree, blob_base,
7789 ondisk_path, relpath, label_orig,
7790 staged_target, commit_id ? commit_id :
7791 a->worktree->base_commit_id,
7792 a->repo, a->progress_cb, a->progress_arg);
7793 free(staged_target);
7795 err = merge_blob(&local_changes_subsumed,
7796 a->worktree, blob_base, ondisk_path,
7797 relpath, got_fileindex_perms_to_st(ie),
7798 label_orig, blob_staged,
7799 commit_id ? commit_id :
7800 a->worktree->base_commit_id, a->repo,
7801 a->progress_cb, a->progress_arg);
7805 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7809 got_fileindex_entry_stage_set(ie,
7810 GOT_FILEIDX_STAGE_NONE);
7811 got_fileindex_entry_staged_filetype_set(ie, 0);
7814 case GOT_STATUS_DELETE:
7816 int choice = GOT_PATCH_CHOICE_NONE;
7817 err = (*a->patch_cb)(&choice, a->patch_arg,
7818 staged_status, ie->path, NULL, 1, 1);
7821 if (choice == GOT_PATCH_CHOICE_NO)
7823 if (choice != GOT_PATCH_CHOICE_YES) {
7824 err = got_error(GOT_ERR_PATCH_CHOICE);
7828 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7829 got_fileindex_entry_staged_filetype_set(ie, 0);
7830 err = get_file_status(&status, &sb, ie, ondisk_path,
7831 dirfd, de_name, a->repo);
7834 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7840 got_object_blob_close(blob_base);
7842 got_object_blob_close(blob_staged);
7848 const struct got_error *
7849 got_worktree_unstage(struct got_worktree *worktree,
7850 struct got_pathlist_head *paths,
7851 got_worktree_checkout_cb progress_cb, void *progress_arg,
7852 got_worktree_patch_cb patch_cb, void *patch_arg,
7853 struct got_repository *repo)
7855 const struct got_error *err = NULL, *sync_err, *unlockerr;
7856 struct got_pathlist_entry *pe;
7857 struct got_fileindex *fileindex = NULL;
7858 char *fileindex_path = NULL;
7859 struct unstage_path_arg upa;
7861 err = lock_worktree(worktree, LOCK_EX);
7865 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7869 upa.worktree = worktree;
7870 upa.fileindex = fileindex;
7872 upa.progress_cb = progress_cb;
7873 upa.progress_arg = progress_arg;
7874 upa.patch_cb = patch_cb;
7875 upa.patch_arg = patch_arg;
7876 TAILQ_FOREACH(pe, paths, entry) {
7877 err = worktree_status(worktree, pe->path, fileindex, repo,
7878 unstage_path, &upa, NULL, NULL, 0, 0);
7883 sync_err = sync_fileindex(fileindex, fileindex_path);
7884 if (sync_err && err == NULL)
7887 free(fileindex_path);
7889 got_fileindex_free(fileindex);
7890 unlockerr = lock_worktree(worktree, LOCK_SH);
7891 if (unlockerr && err == NULL)
7896 struct report_file_info_arg {
7897 struct got_worktree *worktree;
7898 got_worktree_path_info_cb info_cb;
7900 struct got_pathlist_head *paths;
7901 got_cancel_cb cancel_cb;
7905 static const struct got_error *
7906 report_file_info(void *arg, struct got_fileindex_entry *ie)
7908 struct report_file_info_arg *a = arg;
7909 struct got_pathlist_entry *pe;
7910 struct got_object_id blob_id, staged_blob_id, commit_id;
7911 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7912 struct got_object_id *commit_idp = NULL;
7915 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7916 return got_error(GOT_ERR_CANCELLED);
7918 TAILQ_FOREACH(pe, a->paths, entry) {
7919 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7920 got_path_is_child(ie->path, pe->path, pe->path_len))
7923 if (pe == NULL) /* not found */
7926 if (got_fileindex_entry_has_blob(ie)) {
7927 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7928 blob_idp = &blob_id;
7930 stage = got_fileindex_entry_stage_get(ie);
7931 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7932 stage == GOT_FILEIDX_STAGE_ADD) {
7933 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7934 SHA1_DIGEST_LENGTH);
7935 staged_blob_idp = &staged_blob_id;
7938 if (got_fileindex_entry_has_commit(ie)) {
7939 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7940 commit_idp = &commit_id;
7943 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7944 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7947 const struct got_error *
7948 got_worktree_path_info(struct got_worktree *worktree,
7949 struct got_pathlist_head *paths,
7950 got_worktree_path_info_cb info_cb, void *info_arg,
7951 got_cancel_cb cancel_cb, void *cancel_arg)
7954 const struct got_error *err = NULL, *unlockerr;
7955 struct got_fileindex *fileindex = NULL;
7956 char *fileindex_path = NULL;
7957 struct report_file_info_arg arg;
7959 err = lock_worktree(worktree, LOCK_SH);
7963 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7967 arg.worktree = worktree;
7968 arg.info_cb = info_cb;
7969 arg.info_arg = info_arg;
7971 arg.cancel_cb = cancel_cb;
7972 arg.cancel_arg = cancel_arg;
7973 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7976 free(fileindex_path);
7978 got_fileindex_free(fileindex);
7979 unlockerr = lock_worktree(worktree, LOCK_UN);
7980 if (unlockerr && err == NULL)