2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
142 err = got_error_from_errno2("open", path);
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
171 (*content)[sb.st_size - 1] = '\0';
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
193 return got_error_from_errno("got_ref_to_str");
195 refstr = strdup(got_ref_get_name(head_ref));
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
211 uint32_t uuid_status;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
224 err = got_ref_resolve(&commit_id, repo, head_ref);
227 err = got_object_get_type(&obj_type, repo, commit_id);
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
433 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 if ((*worktree)->root_fd == -1) {
435 err = got_error_from_errno2("open", (*worktree)->root_path);
440 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;
503 if (worktree->lockfd != -1) {
504 if (close(worktree->lockfd) == -1)
505 err = got_error_from_errno2("close",
506 got_worktree_get_root_path(worktree));
508 if (close(worktree->root_fd) == -1 && err == NULL)
509 err = got_error_from_errno2("close",
510 got_worktree_get_root_path(worktree));
511 free(worktree->repo_path);
512 free(worktree->path_prefix);
513 free(worktree->base_commit_id);
514 free(worktree->head_ref_name);
515 free(worktree->root_path);
516 free(worktree->gotconfig_path);
517 got_gotconfig_free(worktree->gotconfig);
523 got_worktree_get_root_path(struct got_worktree *worktree)
525 return worktree->root_path;
529 got_worktree_get_repo_path(struct got_worktree *worktree)
531 return worktree->repo_path;
534 got_worktree_get_path_prefix(struct got_worktree *worktree)
536 return worktree->path_prefix;
539 const struct got_error *
540 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
541 const char *path_prefix)
543 char *absprefix = NULL;
545 if (!got_path_is_absolute(path_prefix)) {
546 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
547 return got_error_from_errno("asprintf");
549 *match = (strcmp(absprefix ? absprefix : path_prefix,
550 worktree->path_prefix) == 0);
556 got_worktree_get_head_ref_name(struct got_worktree *worktree)
558 return worktree->head_ref_name;
561 const struct got_error *
562 got_worktree_set_head_ref(struct got_worktree *worktree,
563 struct got_reference *head_ref)
565 const struct got_error *err = NULL;
566 char *path_got = NULL, *head_ref_name = NULL;
568 if (asprintf(&path_got, "%s/%s", worktree->root_path,
569 GOT_WORKTREE_GOT_DIR) == -1) {
570 err = got_error_from_errno("asprintf");
575 head_ref_name = strdup(got_ref_get_name(head_ref));
576 if (head_ref_name == NULL) {
577 err = got_error_from_errno("strdup");
581 err = write_head_ref(path_got, head_ref);
585 free(worktree->head_ref_name);
586 worktree->head_ref_name = head_ref_name;
594 struct got_object_id *
595 got_worktree_get_base_commit_id(struct got_worktree *worktree)
597 return worktree->base_commit_id;
600 const struct got_error *
601 got_worktree_set_base_commit_id(struct got_worktree *worktree,
602 struct got_repository *repo, struct got_object_id *commit_id)
604 const struct got_error *err;
605 struct got_object *obj = NULL;
607 char *path_got = NULL;
609 if (asprintf(&path_got, "%s/%s", worktree->root_path,
610 GOT_WORKTREE_GOT_DIR) == -1) {
611 err = got_error_from_errno("asprintf");
616 err = got_object_open(&obj, repo, commit_id);
620 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
621 err = got_error(GOT_ERR_OBJ_TYPE);
625 /* Record our base commit. */
626 err = got_object_id_str(&id_str, commit_id);
629 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
633 free(worktree->base_commit_id);
634 worktree->base_commit_id = got_object_id_dup(commit_id);
635 if (worktree->base_commit_id == NULL) {
636 err = got_error_from_errno("got_object_id_dup");
641 got_object_close(obj);
647 const struct got_gotconfig *
648 got_worktree_get_gotconfig(struct got_worktree *worktree)
650 return worktree->gotconfig;
653 static const struct got_error *
654 lock_worktree(struct got_worktree *worktree, int operation)
656 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
657 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
658 : got_error_from_errno2("flock",
659 got_worktree_get_root_path(worktree)));
663 static const struct got_error *
664 add_dir_on_disk(struct got_worktree *worktree, const char *path)
666 const struct got_error *err = NULL;
669 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
670 return got_error_from_errno("asprintf");
672 err = got_path_mkdir(abspath);
673 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
676 if (lstat(abspath, &sb) == -1) {
677 err = got_error_from_errno2("lstat", abspath);
678 } else if (!S_ISDIR(sb.st_mode)) {
679 /* TODO directory is obstructed; do something */
680 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
687 static const struct got_error *
688 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
690 const struct got_error *err = NULL;
693 size_t flen1 = 0, flen2 = 0;
698 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
699 if (flen1 == 0 && ferror(f1)) {
700 err = got_error_from_errno("fread");
703 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
704 if (flen2 == 0 && ferror(f2)) {
705 err = got_error_from_errno("fread");
712 } else if (flen2 == 0) {
716 } else if (flen1 == flen2) {
717 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
730 static const struct got_error *
731 check_files_equal(int *same, FILE *f1, FILE *f2)
738 if (fstat(fileno(f1), &sb) != 0)
739 return got_error_from_errno("fstat");
742 if (fstat(fileno(f2), &sb) != 0)
743 return got_error_from_errno("fstat");
746 if (size1 != size2) {
751 if (fseek(f1, 0L, SEEK_SET) == -1)
752 return got_ferror(f1, GOT_ERR_IO);
753 if (fseek(f2, 0L, SEEK_SET) == -1)
754 return got_ferror(f2, GOT_ERR_IO);
756 return check_file_contents_equal(same, f1, f2);
760 * Perform a 3-way merge where the file f_orig acts as the common
761 * ancestor, the file f_deriv acts as the first derived version,
762 * and the file at ondisk_path acts as both the target and the
763 * second derived version
765 static const struct got_error *
766 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
767 FILE *f_orig, const char *ondisk_path,
768 const char *path, uint16_t st_mode, FILE *f_deriv,
769 const char *label_orig, const char *label_deriv,
770 struct got_repository *repo,
771 got_worktree_checkout_cb progress_cb, void *progress_arg)
773 const struct got_error *err = NULL;
775 FILE *f_merged = NULL;
776 char *merged_path = NULL, *base_path = NULL;
781 *local_changes_subsumed = 0;
783 err = got_path_dirname(&parent, ondisk_path);
787 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
788 err = got_error_from_errno("asprintf");
792 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
797 * Open the merge target file.
798 * In order the run a 3-way merge with a symlink we copy the symlink's
799 * target path into a temporary file and use that file with diff3.
801 if (S_ISLNK(st_mode)) {
802 char target_path[PATH_MAX];
807 if (asprintf(&base_path, "%s/got-symlink-merge",
809 err = got_error_from_errno("asprintf");
815 err = got_error_from_errno("got_opentemp");
818 target_len = readlink(ondisk_path, target_path,
819 sizeof(target_path));
820 if (target_len == -1) {
821 err = got_error_from_errno2("readlink", ondisk_path);
824 n = fwrite(target_path, 1, target_len, f);
825 if (n != target_len) {
826 err = got_ferror(f, GOT_ERR_IO);
829 if (fflush(f) == EOF) {
830 err = got_error_from_errno("fflush");
833 if (fseek(f, 0L, SEEK_SET) == -1) {
834 err = got_ferror(f, GOT_ERR_IO);
839 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
841 err = got_error_from_errno2("open", ondisk_path);
846 err = got_error_from_errno2("fdopen", ondisk_path);
852 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig, f,
853 label_deriv, label_orig, NULL);
857 err = (*progress_cb)(progress_arg,
858 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
862 if (fsync(merged_fd) != 0) {
863 err = got_error_from_errno("fsync");
867 f_merged = fdopen(merged_fd, "r");
868 if (f_merged == NULL) {
869 err = got_error_from_errno("fdopen");
874 /* Check if a clean merge has subsumed all local changes. */
875 if (overlapcnt == 0) {
876 err = check_files_equal(local_changes_subsumed, f_deriv,
882 if (fchmod(fileno(f_merged), st_mode) != 0) {
883 err = got_error_from_errno2("fchmod", merged_path);
887 if (rename(merged_path, ondisk_path) != 0) {
888 err = got_error_from_errno3("rename", merged_path,
897 if (f && fclose(f) == EOF && err == NULL)
898 err = got_error_from_errno("fclose");
899 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
900 err = got_error_from_errno("close");
901 if (f_merged && fclose(f_merged) == EOF && err == NULL)
902 err = got_error_from_errno("fclose");
909 static const struct got_error *
910 update_symlink(const char *ondisk_path, const char *target_path,
913 /* This is not atomic but matches what 'ln -sf' does. */
914 if (unlink(ondisk_path) == -1)
915 return got_error_from_errno2("unlink", ondisk_path);
916 if (symlink(target_path, ondisk_path) == -1)
917 return got_error_from_errno3("symlink", target_path,
923 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
924 * in the work tree with a file that contains conflict markers and the
925 * conflicting target paths of the original version, a "derived version"
926 * of a symlink from an incoming change, and a local version of the symlink.
928 * The original versions's target path can be NULL if it is not available,
929 * such as if both derived versions added a new symlink at the same path.
931 * The incoming derived symlink target is NULL in case the incoming change
932 * has deleted this symlink.
934 static const struct got_error *
935 install_symlink_conflict(const char *deriv_target,
936 struct got_object_id *deriv_base_commit_id, const char *orig_target,
937 const char *label_orig, const char *local_target, const char *ondisk_path)
939 const struct got_error *err;
940 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
943 err = got_object_id_str(&id_str, deriv_base_commit_id);
945 return got_error_from_errno("asprintf");
947 if (asprintf(&label_deriv, "%s: commit %s",
948 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
949 err = got_error_from_errno("asprintf");
953 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
957 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
958 err = got_error_from_errno2("fchmod", path);
962 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
963 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
964 deriv_target ? deriv_target : "(symlink was deleted)",
965 orig_target ? label_orig : "",
966 orig_target ? "\n" : "",
967 orig_target ? orig_target : "",
968 orig_target ? "\n" : "",
969 GOT_DIFF_CONFLICT_MARKER_SEP,
970 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
971 err = got_error_from_errno2("fprintf", path);
975 if (unlink(ondisk_path) == -1) {
976 err = got_error_from_errno2("unlink", ondisk_path);
979 if (rename(path, ondisk_path) == -1) {
980 err = got_error_from_errno3("rename", path, ondisk_path);
984 if (f != NULL && fclose(f) == EOF && err == NULL)
985 err = got_error_from_errno2("fclose", path);
992 /* forward declaration */
993 static const struct got_error *
994 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
995 const char *, const char *, uint16_t, const char *,
996 struct got_blob_object *, struct got_object_id *,
997 struct got_repository *, got_worktree_checkout_cb, void *);
1000 * Merge a symlink into the work tree, where blob_orig acts as the common
1001 * ancestor, deriv_target is the link target of the first derived version,
1002 * and the symlink on disk acts as the second derived version.
1003 * Assume that contents of both blobs represent symlinks.
1005 static const struct got_error *
1006 merge_symlink(struct got_worktree *worktree,
1007 struct got_blob_object *blob_orig, const char *ondisk_path,
1008 const char *path, const char *label_orig, const char *deriv_target,
1009 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1010 got_worktree_checkout_cb progress_cb, void *progress_arg)
1012 const struct got_error *err = NULL;
1013 char *ancestor_target = NULL;
1015 ssize_t ondisk_len, deriv_len;
1016 char ondisk_target[PATH_MAX];
1017 int have_local_change = 0;
1018 int have_incoming_change = 0;
1020 if (lstat(ondisk_path, &sb) == -1)
1021 return got_error_from_errno2("lstat", ondisk_path);
1023 ondisk_len = readlink(ondisk_path, ondisk_target,
1024 sizeof(ondisk_target));
1025 if (ondisk_len == -1) {
1026 err = got_error_from_errno2("readlink",
1030 ondisk_target[ondisk_len] = '\0';
1033 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1038 if (ancestor_target == NULL ||
1039 (ondisk_len != strlen(ancestor_target) ||
1040 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1041 have_local_change = 1;
1043 deriv_len = strlen(deriv_target);
1044 if (ancestor_target == NULL ||
1045 (deriv_len != strlen(ancestor_target) ||
1046 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1047 have_incoming_change = 1;
1049 if (!have_local_change && !have_incoming_change) {
1050 if (ancestor_target) {
1051 /* Both sides made the same change. */
1052 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1054 } else if (deriv_len == ondisk_len &&
1055 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1056 /* Both sides added the same symlink. */
1057 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1060 /* Both sides added symlinks which don't match. */
1061 err = install_symlink_conflict(deriv_target,
1062 deriv_base_commit_id, ancestor_target,
1063 label_orig, ondisk_target, ondisk_path);
1066 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1069 } else if (!have_local_change && have_incoming_change) {
1070 /* Apply the incoming change. */
1071 err = update_symlink(ondisk_path, deriv_target,
1072 strlen(deriv_target));
1075 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1076 } else if (have_local_change && have_incoming_change) {
1077 if (deriv_len == ondisk_len &&
1078 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1079 /* Both sides made the same change. */
1080 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1083 err = install_symlink_conflict(deriv_target,
1084 deriv_base_commit_id, ancestor_target, label_orig,
1085 ondisk_target, ondisk_path);
1088 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1094 free(ancestor_target);
1099 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1100 * blob_deriv acts as the first derived version, and the file on disk
1101 * acts as the second derived version.
1103 static const struct got_error *
1104 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1105 struct got_blob_object *blob_orig, const char *ondisk_path,
1106 const char *path, uint16_t st_mode, const char *label_orig,
1107 struct got_blob_object *blob_deriv,
1108 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1109 got_worktree_checkout_cb progress_cb, void *progress_arg)
1111 const struct got_error *err = NULL;
1112 FILE *f_orig = NULL, *f_deriv = NULL;
1113 char *blob_orig_path = NULL;
1114 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1115 char *label_deriv = NULL, *parent = NULL;
1117 *local_changes_subsumed = 0;
1119 err = got_path_dirname(&parent, ondisk_path);
1124 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1126 err = got_error_from_errno("asprintf");
1131 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1134 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1141 * No common ancestor exists. This is an "add vs add" conflict
1142 * and we simply use an empty ancestor file to make both files
1143 * appear in the merged result in their entirety.
1145 f_orig = got_opentemp();
1146 if (f_orig == NULL) {
1147 err = got_error_from_errno("got_opentemp");
1152 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1153 err = got_error_from_errno("asprintf");
1158 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1161 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1166 err = got_object_id_str(&id_str, deriv_base_commit_id);
1169 if (asprintf(&label_deriv, "%s: commit %s",
1170 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1171 err = got_error_from_errno("asprintf");
1175 err = merge_file(local_changes_subsumed, worktree, f_orig,
1176 ondisk_path, path, st_mode, f_deriv, label_orig, label_deriv,
1177 repo, progress_cb, progress_arg);
1179 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1180 err = got_error_from_errno("fclose");
1181 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1182 err = got_error_from_errno("fclose");
1184 if (blob_orig_path) {
1185 unlink(blob_orig_path);
1186 free(blob_orig_path);
1188 if (blob_deriv_path) {
1189 unlink(blob_deriv_path);
1190 free(blob_deriv_path);
1198 static const struct got_error *
1199 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1200 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1201 int wt_fd, const char *path, struct got_object_id *blob_id)
1203 const struct got_error *err = NULL;
1204 struct got_fileindex_entry *new_ie;
1208 err = got_fileindex_entry_alloc(&new_ie, path);
1212 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1213 blob_id->sha1, base_commit_id->sha1, 1);
1217 err = got_fileindex_entry_add(fileindex, new_ie);
1220 got_fileindex_entry_free(new_ie);
1227 get_ondisk_perms(int executable, mode_t st_mode)
1229 mode_t xbits = S_IXUSR;
1232 /* Map read bits to execute bits. */
1233 if (st_mode & S_IRGRP)
1235 if (st_mode & S_IROTH)
1237 return st_mode | xbits;
1240 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1243 /* forward declaration */
1244 static const struct got_error *
1245 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1246 const char *path, mode_t te_mode, mode_t st_mode,
1247 struct got_blob_object *blob, int restoring_missing_file,
1248 int reverting_versioned_file, int installing_bad_symlink,
1249 int path_is_unversioned, struct got_repository *repo,
1250 got_worktree_checkout_cb progress_cb, void *progress_arg);
1253 * This function assumes that the provided symlink target points at a
1254 * safe location in the work tree!
1256 static const struct got_error *
1257 replace_existing_symlink(int *did_something, const char *ondisk_path,
1258 const char *target_path, size_t target_len)
1260 const struct got_error *err = NULL;
1262 char etarget[PATH_MAX];
1268 * "Bad" symlinks (those pointing outside the work tree or into the
1269 * .got directory) are installed in the work tree as a regular file
1270 * which contains the bad symlink target path.
1271 * The new symlink target has already been checked for safety by our
1272 * caller. If we can successfully open a regular file then we simply
1273 * replace this file with a symlink below.
1275 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1278 return got_error_from_errno2("open", ondisk_path);
1280 /* We are updating an existing on-disk symlink. */
1281 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1283 return got_error_from_errno2("readlink", ondisk_path);
1285 if (elen == target_len &&
1286 memcmp(etarget, target_path, target_len) == 0)
1287 return NULL; /* nothing to do */
1291 err = update_symlink(ondisk_path, target_path, target_len);
1292 if (fd != -1 && close(fd) == -1 && err == NULL)
1293 err = got_error_from_errno2("close", ondisk_path);
1297 static const struct got_error *
1298 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1299 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1301 const struct got_error *err = NULL;
1302 char canonpath[PATH_MAX];
1303 char *path_got = NULL;
1305 *is_bad_symlink = 0;
1307 if (target_len >= sizeof(canonpath)) {
1308 *is_bad_symlink = 1;
1313 * We do not use realpath(3) to resolve the symlink's target
1314 * path because we don't want to resolve symlinks recursively.
1315 * Instead we make the path absolute and then canonicalize it.
1316 * Relative symlink target lookup should begin at the directory
1317 * in which the blob object is being installed.
1319 if (!got_path_is_absolute(target_path)) {
1320 char *abspath, *parent;
1321 err = got_path_dirname(&parent, ondisk_path);
1324 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1326 return got_error_from_errno("asprintf");
1329 if (strlen(abspath) >= sizeof(canonpath)) {
1330 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1334 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1339 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1344 /* Only allow symlinks pointing at paths within the work tree. */
1345 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1346 *is_bad_symlink = 1;
1350 /* Do not allow symlinks pointing into the .got directory. */
1351 if (asprintf(&path_got, "%s/%s", wtroot_path,
1352 GOT_WORKTREE_GOT_DIR) == -1)
1353 return got_error_from_errno("asprintf");
1354 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1355 *is_bad_symlink = 1;
1361 static const struct got_error *
1362 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1363 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1364 int restoring_missing_file, int reverting_versioned_file,
1365 int path_is_unversioned, struct got_repository *repo,
1366 got_worktree_checkout_cb progress_cb, void *progress_arg)
1368 const struct got_error *err = NULL;
1369 char target_path[PATH_MAX];
1370 size_t len, target_len = 0;
1371 char *path_got = NULL;
1372 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1373 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1375 *is_bad_symlink = 0;
1378 * Blob object content specifies the target path of the link.
1379 * If a symbolic link cannot be installed we instead create
1380 * a regular file which contains the link target path stored
1381 * in the blob object.
1384 err = got_object_blob_read_block(&len, blob);
1385 if (len + target_len >= sizeof(target_path)) {
1386 /* Path too long; install as a regular file. */
1387 *is_bad_symlink = 1;
1388 got_object_blob_rewind(blob);
1389 return install_blob(worktree, ondisk_path, path,
1390 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1391 restoring_missing_file, reverting_versioned_file,
1392 1, path_is_unversioned, repo, progress_cb,
1396 /* Skip blob object header first time around. */
1397 memcpy(target_path + target_len, buf + hdrlen,
1399 target_len += len - hdrlen;
1403 target_path[target_len] = '\0';
1405 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1406 ondisk_path, worktree->root_path);
1410 if (*is_bad_symlink) {
1411 /* install as a regular file */
1412 got_object_blob_rewind(blob);
1413 err = install_blob(worktree, ondisk_path, path,
1414 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1415 restoring_missing_file, reverting_versioned_file, 1,
1416 path_is_unversioned, repo, progress_cb, progress_arg);
1420 if (symlink(target_path, ondisk_path) == -1) {
1421 if (errno == EEXIST) {
1422 int symlink_replaced;
1423 if (path_is_unversioned) {
1424 err = (*progress_cb)(progress_arg,
1425 GOT_STATUS_UNVERSIONED, path);
1428 err = replace_existing_symlink(&symlink_replaced,
1429 ondisk_path, target_path, target_len);
1433 if (symlink_replaced) {
1434 err = (*progress_cb)(progress_arg,
1435 reverting_versioned_file ?
1437 GOT_STATUS_UPDATE, path);
1439 err = (*progress_cb)(progress_arg,
1440 GOT_STATUS_EXISTS, path);
1443 goto done; /* Nothing else to do. */
1446 if (errno == ENOENT) {
1448 err = got_path_dirname(&parent, ondisk_path);
1451 err = add_dir_on_disk(worktree, parent);
1456 * Retry, and fall through to error handling
1457 * below if this second attempt fails.
1459 if (symlink(target_path, ondisk_path) != -1) {
1460 err = NULL; /* success */
1465 /* Handle errors from first or second creation attempt. */
1466 if (errno == ENAMETOOLONG) {
1467 /* bad target path; install as a regular file */
1468 *is_bad_symlink = 1;
1469 got_object_blob_rewind(blob);
1470 err = install_blob(worktree, ondisk_path, path,
1471 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1472 restoring_missing_file, reverting_versioned_file, 1,
1473 path_is_unversioned, repo,
1474 progress_cb, progress_arg);
1475 } else if (errno == ENOTDIR) {
1476 err = got_error_path(ondisk_path,
1477 GOT_ERR_FILE_OBSTRUCTED);
1479 err = got_error_from_errno3("symlink",
1480 target_path, ondisk_path);
1482 } else if (progress_cb)
1483 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1484 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1490 static const struct got_error *
1491 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1492 const char *path, mode_t te_mode, mode_t st_mode,
1493 struct got_blob_object *blob, int restoring_missing_file,
1494 int reverting_versioned_file, int installing_bad_symlink,
1495 int path_is_unversioned, struct got_repository *repo,
1496 got_worktree_checkout_cb progress_cb, void *progress_arg)
1498 const struct got_error *err = NULL;
1502 char *tmppath = NULL;
1504 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1505 GOT_DEFAULT_FILE_MODE);
1507 if (errno == ENOENT) {
1509 err = got_path_dirname(&parent, path);
1512 err = add_dir_on_disk(worktree, parent);
1516 fd = open(ondisk_path,
1517 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1518 GOT_DEFAULT_FILE_MODE);
1520 return got_error_from_errno2("open",
1522 } else if (errno == EEXIST) {
1523 if (path_is_unversioned) {
1524 err = (*progress_cb)(progress_arg,
1525 GOT_STATUS_UNVERSIONED, path);
1528 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1529 !S_ISREG(st_mode) && !installing_bad_symlink) {
1530 /* TODO file is obstructed; do something */
1531 err = got_error_path(ondisk_path,
1532 GOT_ERR_FILE_OBSTRUCTED);
1535 err = got_opentemp_named_fd(&tmppath, &fd,
1542 return got_error_from_errno2("open", ondisk_path);
1545 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1546 err = got_error_from_errno2("fchmod",
1547 update ? tmppath : ondisk_path);
1552 if (restoring_missing_file)
1553 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1555 else if (reverting_versioned_file)
1556 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1559 err = (*progress_cb)(progress_arg,
1560 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1565 hdrlen = got_object_blob_get_hdrlen(blob);
1567 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1568 err = got_object_blob_read_block(&len, blob);
1572 /* Skip blob object header first time around. */
1573 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1575 err = got_error_from_errno("write");
1577 } else if (outlen != len - hdrlen) {
1578 err = got_error(GOT_ERR_IO);
1585 if (fsync(fd) != 0) {
1586 err = got_error_from_errno("fsync");
1591 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1592 err = got_error_from_errno2("unlink", ondisk_path);
1595 if (rename(tmppath, ondisk_path) != 0) {
1596 err = got_error_from_errno3("rename", tmppath,
1605 if (fd != -1 && close(fd) == -1 && err == NULL)
1606 err = got_error_from_errno("close");
1607 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1608 err = got_error_from_errno2("unlink", tmppath);
1613 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1614 static const struct got_error *
1615 get_modified_file_content_status(unsigned char *status, FILE *f)
1617 const struct got_error *err = NULL;
1618 const char *markers[3] = {
1619 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1620 GOT_DIFF_CONFLICT_MARKER_SEP,
1621 GOT_DIFF_CONFLICT_MARKER_END
1625 size_t linesize = 0;
1628 while (*status == GOT_STATUS_MODIFY) {
1629 linelen = getline(&line, &linesize, f);
1630 if (linelen == -1) {
1633 err = got_ferror(f, GOT_ERR_IO);
1637 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1638 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1640 *status = GOT_STATUS_CONFLICT;
1651 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1653 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1654 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1658 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1660 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1661 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1662 ie->mtime_sec == sb->st_mtim.tv_sec &&
1663 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1664 ie->size == (sb->st_size & 0xffffffff) &&
1665 !xbit_differs(ie, sb->st_mode));
1668 static unsigned char
1669 get_staged_status(struct got_fileindex_entry *ie)
1671 switch (got_fileindex_entry_stage_get(ie)) {
1672 case GOT_FILEIDX_STAGE_ADD:
1673 return GOT_STATUS_ADD;
1674 case GOT_FILEIDX_STAGE_DELETE:
1675 return GOT_STATUS_DELETE;
1676 case GOT_FILEIDX_STAGE_MODIFY:
1677 return GOT_STATUS_MODIFY;
1679 return GOT_STATUS_NO_CHANGE;
1683 static const struct got_error *
1684 get_symlink_modification_status(unsigned char *status,
1685 struct got_fileindex_entry *ie, const char *abspath,
1686 int dirfd, const char *de_name, struct got_blob_object *blob)
1688 const struct got_error *err = NULL;
1689 char target_path[PATH_MAX];
1690 char etarget[PATH_MAX];
1692 size_t len, target_len = 0;
1693 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1694 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1696 *status = GOT_STATUS_NO_CHANGE;
1698 /* Blob object content specifies the target path of the link. */
1700 err = got_object_blob_read_block(&len, blob);
1703 if (len + target_len >= sizeof(target_path)) {
1705 * Should not happen. The blob contents were OK
1706 * when this symlink was installed.
1708 return got_error(GOT_ERR_NO_SPACE);
1711 /* Skip blob object header first time around. */
1712 memcpy(target_path + target_len, buf + hdrlen,
1714 target_len += len - hdrlen;
1718 target_path[target_len] = '\0';
1721 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1723 return got_error_from_errno2("readlinkat", abspath);
1725 elen = readlink(abspath, etarget, sizeof(etarget));
1727 return got_error_from_errno2("readlink", abspath);
1730 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1731 *status = GOT_STATUS_MODIFY;
1736 static const struct got_error *
1737 get_file_status(unsigned char *status, struct stat *sb,
1738 struct got_fileindex_entry *ie, const char *abspath,
1739 int dirfd, const char *de_name, struct got_repository *repo)
1741 const struct got_error *err = NULL;
1742 struct got_object_id id;
1747 struct got_blob_object *blob = NULL;
1749 unsigned char staged_status = get_staged_status(ie);
1751 *status = GOT_STATUS_NO_CHANGE;
1752 memset(sb, 0, sizeof(*sb));
1755 * Whenever the caller provides a directory descriptor and a
1756 * directory entry name for the file, use them! This prevents
1757 * race conditions if filesystem paths change beneath our feet.
1760 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1761 if (errno == ENOENT) {
1762 if (got_fileindex_entry_has_file_on_disk(ie))
1763 *status = GOT_STATUS_MISSING;
1765 *status = GOT_STATUS_DELETE;
1768 err = got_error_from_errno2("fstatat", abspath);
1772 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1773 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1774 return got_error_from_errno2("open", abspath);
1775 else if (fd == -1 && errno == ELOOP) {
1776 if (lstat(abspath, sb) == -1)
1777 return got_error_from_errno2("lstat", abspath);
1778 } else if (fd == -1 || fstat(fd, sb) == -1) {
1779 if (errno == ENOENT) {
1780 if (got_fileindex_entry_has_file_on_disk(ie))
1781 *status = GOT_STATUS_MISSING;
1783 *status = GOT_STATUS_DELETE;
1786 err = got_error_from_errno2("fstat", abspath);
1791 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1792 *status = GOT_STATUS_OBSTRUCTED;
1796 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1797 *status = GOT_STATUS_DELETE;
1799 } else if (!got_fileindex_entry_has_blob(ie) &&
1800 staged_status != GOT_STATUS_ADD) {
1801 *status = GOT_STATUS_ADD;
1805 if (!stat_info_differs(ie, sb))
1808 if (S_ISLNK(sb->st_mode) &&
1809 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1810 *status = GOT_STATUS_MODIFY;
1814 if (staged_status == GOT_STATUS_MODIFY ||
1815 staged_status == GOT_STATUS_ADD)
1816 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1818 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1820 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1824 if (S_ISLNK(sb->st_mode)) {
1825 err = get_symlink_modification_status(status, ie,
1826 abspath, dirfd, de_name, blob);
1831 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1833 err = got_error_from_errno2("openat", abspath);
1838 f = fdopen(fd, "r");
1840 err = got_error_from_errno2("fdopen", abspath);
1844 hdrlen = got_object_blob_get_hdrlen(blob);
1846 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1847 err = got_object_blob_read_block(&blen, blob);
1850 /* Skip length of blob object header first time around. */
1851 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1852 if (flen == 0 && ferror(f)) {
1853 err = got_error_from_errno("fread");
1856 if (blen - hdrlen == 0) {
1858 *status = GOT_STATUS_MODIFY;
1860 } else if (flen == 0) {
1861 if (blen - hdrlen != 0)
1862 *status = GOT_STATUS_MODIFY;
1864 } else if (blen - hdrlen == flen) {
1865 /* Skip blob object header first time around. */
1866 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1867 *status = GOT_STATUS_MODIFY;
1871 *status = GOT_STATUS_MODIFY;
1877 if (*status == GOT_STATUS_MODIFY) {
1879 err = get_modified_file_content_status(status, f);
1880 } else if (xbit_differs(ie, sb->st_mode))
1881 *status = GOT_STATUS_MODE_CHANGE;
1884 got_object_blob_close(blob);
1885 if (f != NULL && fclose(f) == EOF && err == NULL)
1886 err = got_error_from_errno2("fclose", abspath);
1887 if (fd != -1 && close(fd) == -1 && err == NULL)
1888 err = got_error_from_errno2("close", abspath);
1893 * Update timestamps in the file index if a file is unmodified and
1894 * we had to run a full content comparison to find out.
1896 static const struct got_error *
1897 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1898 struct got_fileindex_entry *ie, struct stat *sb)
1900 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1901 return got_fileindex_entry_update(ie, wt_fd, path,
1902 ie->blob_sha1, ie->commit_sha1, 1);
1907 static const struct got_error *
1908 update_blob(struct got_worktree *worktree,
1909 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1910 struct got_tree_entry *te, const char *path,
1911 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1914 const struct got_error *err = NULL;
1915 struct got_blob_object *blob = NULL;
1917 unsigned char status = GOT_STATUS_NO_CHANGE;
1920 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1921 return got_error_from_errno("asprintf");
1924 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1925 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1928 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1932 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1933 sb.st_mode = got_fileindex_perms_to_st(ie);
1935 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1936 status = GOT_STATUS_UNVERSIONED;
1939 if (status == GOT_STATUS_OBSTRUCTED) {
1940 err = (*progress_cb)(progress_arg, status, path);
1943 if (status == GOT_STATUS_CONFLICT) {
1944 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1949 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1950 (S_ISLNK(te->mode) ||
1951 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1953 * This is a regular file or an installed bad symlink.
1954 * If the file index indicates that this file is already
1955 * up-to-date with respect to the repository we can skip
1956 * updating contents of this file.
1958 if (got_fileindex_entry_has_commit(ie) &&
1959 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1960 SHA1_DIGEST_LENGTH) == 0) {
1962 err = sync_timestamps(worktree->root_fd,
1963 path, status, ie, &sb);
1966 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1970 if (got_fileindex_entry_has_blob(ie) &&
1971 memcmp(ie->blob_sha1, te->id.sha1,
1972 SHA1_DIGEST_LENGTH) == 0) {
1973 /* Different commit but the same blob. */
1974 err = sync_timestamps(worktree->root_fd,
1975 path, status, ie, &sb);
1978 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1984 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1988 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1989 int update_timestamps;
1990 struct got_blob_object *blob2 = NULL;
1991 char *label_orig = NULL;
1992 if (got_fileindex_entry_has_blob(ie)) {
1993 struct got_object_id id2;
1994 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1995 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1999 if (got_fileindex_entry_has_commit(ie)) {
2000 char id_str[SHA1_DIGEST_STRING_LENGTH];
2001 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2002 sizeof(id_str)) == NULL) {
2003 err = got_error_path(id_str,
2004 GOT_ERR_BAD_OBJ_ID_STR);
2007 if (asprintf(&label_orig, "%s: commit %s",
2008 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2009 err = got_error_from_errno("asprintf");
2013 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2015 err = got_object_blob_read_to_str(&link_target, blob);
2018 err = merge_symlink(worktree, blob2, ondisk_path, path,
2019 label_orig, link_target, worktree->base_commit_id,
2020 repo, progress_cb, progress_arg);
2023 err = merge_blob(&update_timestamps, worktree, blob2,
2024 ondisk_path, path, sb.st_mode, label_orig, blob,
2025 worktree->base_commit_id, repo,
2026 progress_cb, progress_arg);
2030 got_object_blob_close(blob2);
2034 * Do not update timestamps of files with local changes.
2035 * Otherwise, a future status walk would treat them as
2036 * unmodified files again.
2038 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2039 blob->id.sha1, worktree->base_commit_id->sha1,
2041 } else if (status == GOT_STATUS_MODE_CHANGE) {
2042 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2043 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2044 } else if (status == GOT_STATUS_DELETE) {
2045 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2048 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2049 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2053 int is_bad_symlink = 0;
2054 if (S_ISLNK(te->mode)) {
2055 err = install_symlink(&is_bad_symlink, worktree,
2056 ondisk_path, path, blob,
2057 status == GOT_STATUS_MISSING, 0,
2058 status == GOT_STATUS_UNVERSIONED, repo,
2059 progress_cb, progress_arg);
2061 err = install_blob(worktree, ondisk_path, path,
2062 te->mode, sb.st_mode, blob,
2063 status == GOT_STATUS_MISSING, 0, 0,
2064 status == GOT_STATUS_UNVERSIONED, repo,
2065 progress_cb, progress_arg);
2071 err = got_fileindex_entry_update(ie,
2072 worktree->root_fd, path, blob->id.sha1,
2073 worktree->base_commit_id->sha1, 1);
2075 err = create_fileindex_entry(&ie, fileindex,
2076 worktree->base_commit_id, worktree->root_fd, path,
2082 if (is_bad_symlink) {
2083 got_fileindex_entry_filetype_set(ie,
2084 GOT_FILEIDX_MODE_BAD_SYMLINK);
2087 got_object_blob_close(blob);
2093 static const struct got_error *
2094 remove_ondisk_file(const char *root_path, const char *path)
2096 const struct got_error *err = NULL;
2097 char *ondisk_path = NULL;
2099 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2100 return got_error_from_errno("asprintf");
2102 if (unlink(ondisk_path) == -1) {
2103 if (errno != ENOENT)
2104 err = got_error_from_errno2("unlink", ondisk_path);
2106 size_t root_len = strlen(root_path);
2109 err = got_path_dirname(&parent, ondisk_path);
2113 ondisk_path = parent;
2114 if (rmdir(ondisk_path) == -1) {
2115 if (errno != ENOTEMPTY)
2116 err = got_error_from_errno2("rmdir",
2120 } while (got_path_cmp(ondisk_path, root_path,
2121 strlen(ondisk_path), root_len) != 0);
2127 static const struct got_error *
2128 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2129 struct got_fileindex_entry *ie, struct got_repository *repo,
2130 got_worktree_checkout_cb progress_cb, void *progress_arg)
2132 const struct got_error *err = NULL;
2133 unsigned char status;
2137 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2138 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2140 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2142 return got_error_from_errno("asprintf");
2144 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2148 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2149 char ondisk_target[PATH_MAX];
2150 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2151 sizeof(ondisk_target));
2152 if (ondisk_len == -1) {
2153 err = got_error_from_errno2("readlink", ondisk_path);
2156 ondisk_target[ondisk_len] = '\0';
2157 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2158 NULL, NULL, /* XXX pass common ancestor info? */
2159 ondisk_target, ondisk_path);
2162 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2167 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2168 status == GOT_STATUS_ADD) {
2169 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2173 * Preserve the working file and change the deleted blob's
2174 * entry into a schedule-add entry.
2176 err = got_fileindex_entry_update(ie, worktree->root_fd,
2177 ie->path, NULL, NULL, 0);
2179 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2182 if (status == GOT_STATUS_NO_CHANGE) {
2183 err = remove_ondisk_file(worktree->root_path, ie->path);
2187 got_fileindex_entry_remove(fileindex, ie);
2194 struct diff_cb_arg {
2195 struct got_fileindex *fileindex;
2196 struct got_worktree *worktree;
2197 struct got_repository *repo;
2198 got_worktree_checkout_cb progress_cb;
2200 got_cancel_cb cancel_cb;
2204 static const struct got_error *
2205 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2206 struct got_tree_entry *te, const char *parent_path)
2208 struct diff_cb_arg *a = arg;
2210 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2211 return got_error(GOT_ERR_CANCELLED);
2213 return update_blob(a->worktree, a->fileindex, ie, te,
2214 ie->path, a->repo, a->progress_cb, a->progress_arg);
2217 static const struct got_error *
2218 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2220 struct diff_cb_arg *a = arg;
2222 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2223 return got_error(GOT_ERR_CANCELLED);
2225 return delete_blob(a->worktree, a->fileindex, ie,
2226 a->repo, a->progress_cb, a->progress_arg);
2229 static const struct got_error *
2230 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2232 struct diff_cb_arg *a = arg;
2233 const struct got_error *err;
2236 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2237 return got_error(GOT_ERR_CANCELLED);
2239 if (got_object_tree_entry_is_submodule(te))
2242 if (asprintf(&path, "%s%s%s", parent_path,
2243 parent_path[0] ? "/" : "", te->name)
2245 return got_error_from_errno("asprintf");
2247 if (S_ISDIR(te->mode))
2248 err = add_dir_on_disk(a->worktree, path);
2250 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2251 a->repo, a->progress_cb, a->progress_arg);
2257 const struct got_error *
2258 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2260 uint32_t uuid_status;
2262 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2263 if (uuid_status != uuid_s_ok) {
2265 return got_error_uuid(uuid_status, "uuid_to_string");
2271 static const struct got_error *
2272 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2274 const struct got_error *err = NULL;
2275 char *uuidstr = NULL;
2279 err = got_worktree_get_uuid(&uuidstr, worktree);
2283 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2284 err = got_error_from_errno("asprintf");
2291 const struct got_error *
2292 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2294 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2297 static const struct got_error *
2298 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2300 return get_ref_name(refname, worktree,
2301 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2304 static const struct got_error *
2305 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2307 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2310 static const struct got_error *
2311 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2313 return get_ref_name(refname, worktree,
2314 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2317 static const struct got_error *
2318 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2320 return get_ref_name(refname, worktree,
2321 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2324 static const struct got_error *
2325 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2327 return get_ref_name(refname, worktree,
2328 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2331 static const struct got_error *
2332 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2334 return get_ref_name(refname, worktree,
2335 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2338 static const struct got_error *
2339 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2341 return get_ref_name(refname, worktree,
2342 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2345 static const struct got_error *
2346 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2348 return get_ref_name(refname, worktree,
2349 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2352 const struct got_error *
2353 got_worktree_get_histedit_script_path(char **path,
2354 struct got_worktree *worktree)
2356 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2357 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2359 return got_error_from_errno("asprintf");
2365 * Prevent Git's garbage collector from deleting our base commit by
2366 * setting a reference to our base commit's ID.
2368 static const struct got_error *
2369 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2371 const struct got_error *err = NULL;
2372 struct got_reference *ref = NULL;
2375 err = got_worktree_get_base_ref_name(&refname, worktree);
2379 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2383 err = got_ref_write(ref, repo);
2391 static const struct got_error *
2392 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2394 const struct got_error *err = NULL;
2396 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2397 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2398 err = got_error_from_errno("asprintf");
2399 *fileindex_path = NULL;
2405 static const struct got_error *
2406 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2407 struct got_worktree *worktree)
2409 const struct got_error *err = NULL;
2412 *fileindex_path = NULL;
2413 *fileindex = got_fileindex_alloc();
2414 if (*fileindex == NULL)
2415 return got_error_from_errno("got_fileindex_alloc");
2417 err = get_fileindex_path(fileindex_path, worktree);
2421 index = fopen(*fileindex_path, "rb");
2422 if (index == NULL) {
2423 if (errno != ENOENT)
2424 err = got_error_from_errno2("fopen", *fileindex_path);
2426 err = got_fileindex_read(*fileindex, index);
2427 if (fclose(index) == EOF && err == NULL)
2428 err = got_error_from_errno("fclose");
2432 free(*fileindex_path);
2433 *fileindex_path = NULL;
2434 got_fileindex_free(*fileindex);
2440 struct bump_base_commit_id_arg {
2441 struct got_object_id *base_commit_id;
2444 const char *entry_name;
2445 got_worktree_checkout_cb progress_cb;
2449 /* Bump base commit ID of all files within an updated part of the work tree. */
2450 static const struct got_error *
2451 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2453 const struct got_error *err;
2454 struct bump_base_commit_id_arg *a = arg;
2456 if (a->entry_name) {
2457 if (strcmp(ie->path, a->path) != 0)
2459 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2462 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2463 SHA1_DIGEST_LENGTH) == 0)
2466 if (a->progress_cb) {
2467 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2472 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2476 static const struct got_error *
2477 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2478 struct got_fileindex *fileindex,
2479 got_worktree_checkout_cb progress_cb, void *progress_arg)
2481 struct bump_base_commit_id_arg bbc_arg;
2483 bbc_arg.base_commit_id = worktree->base_commit_id;
2484 bbc_arg.entry_name = NULL;
2486 bbc_arg.path_len = 0;
2487 bbc_arg.progress_cb = progress_cb;
2488 bbc_arg.progress_arg = progress_arg;
2490 return got_fileindex_for_each_entry_safe(fileindex,
2491 bump_base_commit_id, &bbc_arg);
2494 static const struct got_error *
2495 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2497 const struct got_error *err = NULL;
2498 char *new_fileindex_path = NULL;
2499 FILE *new_index = NULL;
2500 struct timespec timeout;
2502 err = got_opentemp_named(&new_fileindex_path, &new_index,
2507 err = got_fileindex_write(fileindex, new_index);
2511 if (rename(new_fileindex_path, fileindex_path) != 0) {
2512 err = got_error_from_errno3("rename", new_fileindex_path,
2514 unlink(new_fileindex_path);
2518 * Sleep for a short amount of time to ensure that files modified after
2519 * this program exits have a different time stamp from the one which
2520 * was recorded in the file index.
2523 timeout.tv_nsec = 1;
2524 nanosleep(&timeout, NULL);
2528 free(new_fileindex_path);
2532 static const struct got_error *
2533 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2534 struct got_object_id **tree_id, const char *wt_relpath,
2535 struct got_worktree *worktree, struct got_repository *repo)
2537 const struct got_error *err = NULL;
2538 struct got_object_id *id = NULL;
2539 char *in_repo_path = NULL;
2540 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2542 *entry_type = GOT_OBJ_TYPE_ANY;
2543 *tree_relpath = NULL;
2546 if (wt_relpath[0] == '\0') {
2547 /* Check out all files within the work tree. */
2548 *entry_type = GOT_OBJ_TYPE_TREE;
2549 *tree_relpath = strdup("");
2550 if (*tree_relpath == NULL) {
2551 err = got_error_from_errno("strdup");
2554 err = got_object_id_by_path(tree_id, repo,
2555 worktree->base_commit_id, worktree->path_prefix);
2561 /* Check out a subset of files in the work tree. */
2563 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2564 is_root_wt ? "" : "/", wt_relpath) == -1) {
2565 err = got_error_from_errno("asprintf");
2569 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2575 in_repo_path = NULL;
2577 err = got_object_get_type(entry_type, repo, id);
2581 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2582 /* Check out a single file. */
2583 if (strchr(wt_relpath, '/') == NULL) {
2584 /* Check out a single file in work tree's root dir. */
2585 in_repo_path = strdup(worktree->path_prefix);
2586 if (in_repo_path == NULL) {
2587 err = got_error_from_errno("strdup");
2590 *tree_relpath = strdup("");
2591 if (*tree_relpath == NULL) {
2592 err = got_error_from_errno("strdup");
2596 /* Check out a single file in a subdirectory. */
2597 err = got_path_dirname(tree_relpath, wt_relpath);
2600 if (asprintf(&in_repo_path, "%s%s%s",
2601 worktree->path_prefix, is_root_wt ? "" : "/",
2602 *tree_relpath) == -1) {
2603 err = got_error_from_errno("asprintf");
2607 err = got_object_id_by_path(tree_id, repo,
2608 worktree->base_commit_id, in_repo_path);
2610 /* Check out all files within a subdirectory. */
2611 *tree_id = got_object_id_dup(id);
2612 if (*tree_id == NULL) {
2613 err = got_error_from_errno("got_object_id_dup");
2616 *tree_relpath = strdup(wt_relpath);
2617 if (*tree_relpath == NULL) {
2618 err = got_error_from_errno("strdup");
2626 *entry_type = GOT_OBJ_TYPE_ANY;
2627 free(*tree_relpath);
2628 *tree_relpath = NULL;
2635 static const struct got_error *
2636 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2637 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2638 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2639 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2641 const struct got_error *err = NULL;
2642 struct got_commit_object *commit = NULL;
2643 struct got_tree_object *tree = NULL;
2644 struct got_fileindex_diff_tree_cb diff_cb;
2645 struct diff_cb_arg arg;
2647 err = ref_base_commit(worktree, repo);
2649 if (!(err->code == GOT_ERR_ERRNO &&
2650 (errno == EACCES || errno == EROFS)))
2652 err = (*progress_cb)(progress_arg,
2653 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2658 err = got_object_open_as_commit(&commit, repo,
2659 worktree->base_commit_id);
2663 err = got_object_open_as_tree(&tree, repo, tree_id);
2668 got_object_tree_find_entry(tree, entry_name) == NULL) {
2669 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2673 diff_cb.diff_old_new = diff_old_new;
2674 diff_cb.diff_old = diff_old;
2675 diff_cb.diff_new = diff_new;
2676 arg.fileindex = fileindex;
2677 arg.worktree = worktree;
2679 arg.progress_cb = progress_cb;
2680 arg.progress_arg = progress_arg;
2681 arg.cancel_cb = cancel_cb;
2682 arg.cancel_arg = cancel_arg;
2683 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2684 entry_name, repo, &diff_cb, &arg);
2687 got_object_tree_close(tree);
2689 got_object_commit_close(commit);
2693 const struct got_error *
2694 got_worktree_checkout_files(struct got_worktree *worktree,
2695 struct got_pathlist_head *paths, struct got_repository *repo,
2696 got_worktree_checkout_cb progress_cb, void *progress_arg,
2697 got_cancel_cb cancel_cb, void *cancel_arg)
2699 const struct got_error *err = NULL, *sync_err, *unlockerr;
2700 struct got_commit_object *commit = NULL;
2701 struct got_tree_object *tree = NULL;
2702 struct got_fileindex *fileindex = NULL;
2703 char *fileindex_path = NULL;
2704 struct got_pathlist_entry *pe;
2705 struct tree_path_data {
2706 SIMPLEQ_ENTRY(tree_path_data) entry;
2707 struct got_object_id *tree_id;
2712 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2714 SIMPLEQ_INIT(&tree_paths);
2716 err = lock_worktree(worktree, LOCK_EX);
2720 /* Map all specified paths to in-repository trees. */
2721 TAILQ_FOREACH(pe, paths, entry) {
2722 tpd = malloc(sizeof(*tpd));
2724 err = got_error_from_errno("malloc");
2728 err = find_tree_entry_for_checkout(&tpd->entry_type,
2729 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2735 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2736 err = got_path_basename(&tpd->entry_name, pe->path);
2744 tpd->entry_name = NULL;
2746 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2750 * Read the file index.
2751 * Checking out files is supposed to be an idempotent operation.
2752 * If the on-disk file index is incomplete we will try to complete it.
2754 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2758 tpd = SIMPLEQ_FIRST(&tree_paths);
2759 TAILQ_FOREACH(pe, paths, entry) {
2760 struct bump_base_commit_id_arg bbc_arg;
2762 err = checkout_files(worktree, fileindex, tpd->relpath,
2763 tpd->tree_id, tpd->entry_name, repo,
2764 progress_cb, progress_arg, cancel_cb, cancel_arg);
2768 bbc_arg.base_commit_id = worktree->base_commit_id;
2769 bbc_arg.entry_name = tpd->entry_name;
2770 bbc_arg.path = pe->path;
2771 bbc_arg.path_len = pe->path_len;
2772 bbc_arg.progress_cb = progress_cb;
2773 bbc_arg.progress_arg = progress_arg;
2774 err = got_fileindex_for_each_entry_safe(fileindex,
2775 bump_base_commit_id, &bbc_arg);
2779 tpd = SIMPLEQ_NEXT(tpd, entry);
2781 sync_err = sync_fileindex(fileindex, fileindex_path);
2782 if (sync_err && err == NULL)
2785 free(fileindex_path);
2787 got_object_tree_close(tree);
2789 got_object_commit_close(commit);
2791 got_fileindex_free(fileindex);
2792 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2793 tpd = SIMPLEQ_FIRST(&tree_paths);
2794 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2799 unlockerr = lock_worktree(worktree, LOCK_SH);
2800 if (unlockerr && err == NULL)
2805 struct merge_file_cb_arg {
2806 struct got_worktree *worktree;
2807 struct got_fileindex *fileindex;
2808 got_worktree_checkout_cb progress_cb;
2810 got_cancel_cb cancel_cb;
2812 const char *label_orig;
2813 struct got_object_id *commit_id2;
2816 static const struct got_error *
2817 merge_file_cb(void *arg, struct got_blob_object *blob1,
2818 struct got_blob_object *blob2, struct got_object_id *id1,
2819 struct got_object_id *id2, const char *path1, const char *path2,
2820 mode_t mode1, mode_t mode2, struct got_repository *repo)
2822 static const struct got_error *err = NULL;
2823 struct merge_file_cb_arg *a = arg;
2824 struct got_fileindex_entry *ie;
2825 char *ondisk_path = NULL;
2827 unsigned char status;
2828 int local_changes_subsumed;
2830 if (blob1 && blob2) {
2831 ie = got_fileindex_entry_get(a->fileindex, path2,
2834 return (*a->progress_cb)(a->progress_arg,
2835 GOT_STATUS_MISSING, path2);
2837 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2839 return got_error_from_errno("asprintf");
2841 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2846 if (status == GOT_STATUS_DELETE) {
2847 err = (*a->progress_cb)(a->progress_arg,
2848 GOT_STATUS_MERGE, path2);
2851 if (status != GOT_STATUS_NO_CHANGE &&
2852 status != GOT_STATUS_MODIFY &&
2853 status != GOT_STATUS_CONFLICT &&
2854 status != GOT_STATUS_ADD) {
2855 err = (*a->progress_cb)(a->progress_arg, status, path2);
2859 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2861 err = got_object_blob_read_to_str(&link_target2, blob2);
2864 err = merge_symlink(a->worktree, blob1, ondisk_path,
2865 path2, a->label_orig, link_target2, a->commit_id2,
2866 repo, a->progress_cb, a->progress_arg);
2869 err = merge_blob(&local_changes_subsumed, a->worktree,
2870 blob1, ondisk_path, path2, sb.st_mode,
2871 a->label_orig, blob2, a->commit_id2, repo,
2872 a->progress_cb, a->progress_arg);
2875 ie = got_fileindex_entry_get(a->fileindex, path1,
2878 return (*a->progress_cb)(a->progress_arg,
2879 GOT_STATUS_MISSING, path1);
2881 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2883 return got_error_from_errno("asprintf");
2885 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2891 case GOT_STATUS_NO_CHANGE:
2892 err = (*a->progress_cb)(a->progress_arg,
2893 GOT_STATUS_DELETE, path1);
2896 err = remove_ondisk_file(a->worktree->root_path, path1);
2900 got_fileindex_entry_mark_deleted_from_disk(ie);
2902 case GOT_STATUS_DELETE:
2903 case GOT_STATUS_MISSING:
2904 err = (*a->progress_cb)(a->progress_arg,
2905 GOT_STATUS_DELETE, path1);
2909 got_fileindex_entry_mark_deleted_from_disk(ie);
2911 case GOT_STATUS_ADD: {
2912 struct got_object_id *id;
2915 * Delete the added file only if its content already
2916 * exists in the repository.
2918 err = got_object_blob_file_create(&id, &blob1_f, path1);
2921 if (got_object_id_cmp(id, id1) == 0) {
2922 err = (*a->progress_cb)(a->progress_arg,
2923 GOT_STATUS_DELETE, path1);
2926 err = remove_ondisk_file(a->worktree->root_path,
2931 got_fileindex_entry_remove(a->fileindex,
2934 err = (*a->progress_cb)(a->progress_arg,
2935 GOT_STATUS_CANNOT_DELETE, path1);
2937 if (fclose(blob1_f) == EOF && err == NULL)
2938 err = got_error_from_errno("fclose");
2944 case GOT_STATUS_MODIFY:
2945 case GOT_STATUS_CONFLICT:
2946 err = (*a->progress_cb)(a->progress_arg,
2947 GOT_STATUS_CANNOT_DELETE, path1);
2951 case GOT_STATUS_OBSTRUCTED:
2952 err = (*a->progress_cb)(a->progress_arg, status, path1);
2960 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2962 return got_error_from_errno("asprintf");
2963 ie = got_fileindex_entry_get(a->fileindex, path2,
2966 err = get_file_status(&status, &sb, ie, ondisk_path,
2970 if (status != GOT_STATUS_NO_CHANGE &&
2971 status != GOT_STATUS_MODIFY &&
2972 status != GOT_STATUS_CONFLICT &&
2973 status != GOT_STATUS_ADD) {
2974 err = (*a->progress_cb)(a->progress_arg,
2978 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2980 err = got_object_blob_read_to_str(&link_target2,
2984 err = merge_symlink(a->worktree, NULL,
2985 ondisk_path, path2, a->label_orig,
2986 link_target2, a->commit_id2, repo,
2987 a->progress_cb, a->progress_arg);
2989 } else if (S_ISREG(sb.st_mode)) {
2990 err = merge_blob(&local_changes_subsumed,
2991 a->worktree, NULL, ondisk_path, path2,
2992 sb.st_mode, a->label_orig, blob2,
2993 a->commit_id2, repo, a->progress_cb,
2996 err = got_error_path(ondisk_path,
2997 GOT_ERR_FILE_OBSTRUCTED);
3001 if (status == GOT_STATUS_DELETE) {
3002 err = got_fileindex_entry_update(ie,
3003 a->worktree->root_fd, path2, blob2->id.sha1,
3004 a->worktree->base_commit_id->sha1, 0);
3009 int is_bad_symlink = 0;
3010 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3011 if (S_ISLNK(mode2)) {
3012 err = install_symlink(&is_bad_symlink,
3013 a->worktree, ondisk_path, path2, blob2, 0,
3014 0, 1, repo, a->progress_cb, a->progress_arg);
3016 err = install_blob(a->worktree, ondisk_path, path2,
3017 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3018 a->progress_cb, a->progress_arg);
3022 err = got_fileindex_entry_alloc(&ie, path2);
3025 err = got_fileindex_entry_update(ie,
3026 a->worktree->root_fd, path2, NULL, NULL, 1);
3028 got_fileindex_entry_free(ie);
3031 err = got_fileindex_entry_add(a->fileindex, ie);
3033 got_fileindex_entry_free(ie);
3036 if (is_bad_symlink) {
3037 got_fileindex_entry_filetype_set(ie,
3038 GOT_FILEIDX_MODE_BAD_SYMLINK);
3047 struct check_merge_ok_arg {
3048 struct got_worktree *worktree;
3049 struct got_repository *repo;
3052 static const struct got_error *
3053 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3055 const struct got_error *err = NULL;
3056 struct check_merge_ok_arg *a = arg;
3057 unsigned char status;
3061 /* Reject merges into a work tree with mixed base commits. */
3062 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3063 SHA1_DIGEST_LENGTH))
3064 return got_error(GOT_ERR_MIXED_COMMITS);
3066 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3068 return got_error_from_errno("asprintf");
3070 /* Reject merges into a work tree with conflicted files. */
3071 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3074 if (status == GOT_STATUS_CONFLICT)
3075 return got_error(GOT_ERR_CONFLICTS);
3080 static const struct got_error *
3081 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3082 const char *fileindex_path, struct got_object_id *commit_id1,
3083 struct got_object_id *commit_id2, struct got_repository *repo,
3084 got_worktree_checkout_cb progress_cb, void *progress_arg,
3085 got_cancel_cb cancel_cb, void *cancel_arg)
3087 const struct got_error *err = NULL, *sync_err;
3088 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3089 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3090 struct merge_file_cb_arg arg;
3091 char *label_orig = NULL;
3094 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3095 worktree->path_prefix);
3096 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3102 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3106 err = got_object_id_str(&id_str, commit_id1);
3110 if (asprintf(&label_orig, "%s: commit %s",
3111 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3112 err = got_error_from_errno("asprintf");
3119 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3120 worktree->path_prefix);
3124 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3128 arg.worktree = worktree;
3129 arg.fileindex = fileindex;
3130 arg.progress_cb = progress_cb;
3131 arg.progress_arg = progress_arg;
3132 arg.cancel_cb = cancel_cb;
3133 arg.cancel_arg = cancel_arg;
3134 arg.label_orig = label_orig;
3135 arg.commit_id2 = commit_id2;
3136 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3137 sync_err = sync_fileindex(fileindex, fileindex_path);
3138 if (sync_err && err == NULL)
3142 got_object_tree_close(tree1);
3144 got_object_tree_close(tree2);
3149 const struct got_error *
3150 got_worktree_merge_files(struct got_worktree *worktree,
3151 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3152 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3153 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3155 const struct got_error *err, *unlockerr;
3156 char *fileindex_path = NULL;
3157 struct got_fileindex *fileindex = NULL;
3158 struct check_merge_ok_arg mok_arg;
3160 err = lock_worktree(worktree, LOCK_EX);
3164 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3168 mok_arg.worktree = worktree;
3169 mok_arg.repo = repo;
3170 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3175 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3176 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3179 got_fileindex_free(fileindex);
3180 free(fileindex_path);
3181 unlockerr = lock_worktree(worktree, LOCK_SH);
3182 if (unlockerr && err == NULL)
3187 struct diff_dir_cb_arg {
3188 struct got_fileindex *fileindex;
3189 struct got_worktree *worktree;
3190 const char *status_path;
3191 size_t status_path_len;
3192 struct got_repository *repo;
3193 got_worktree_status_cb status_cb;
3195 got_cancel_cb cancel_cb;
3197 /* A pathlist containing per-directory pathlists of ignore patterns. */
3198 struct got_pathlist_head ignores;
3199 int report_unchanged;
3203 static const struct got_error *
3204 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3205 int dirfd, const char *de_name,
3206 got_worktree_status_cb status_cb, void *status_arg,
3207 struct got_repository *repo, int report_unchanged)
3209 const struct got_error *err = NULL;
3210 unsigned char status = GOT_STATUS_NO_CHANGE;
3211 unsigned char staged_status = get_staged_status(ie);
3213 struct got_object_id blob_id, commit_id, staged_blob_id;
3214 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3215 struct got_object_id *staged_blob_idp = NULL;
3217 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3221 if (status == GOT_STATUS_NO_CHANGE &&
3222 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3225 if (got_fileindex_entry_has_blob(ie)) {
3226 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3227 blob_idp = &blob_id;
3229 if (got_fileindex_entry_has_commit(ie)) {
3230 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3231 commit_idp = &commit_id;
3233 if (staged_status == GOT_STATUS_ADD ||
3234 staged_status == GOT_STATUS_MODIFY) {
3235 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3236 SHA1_DIGEST_LENGTH);
3237 staged_blob_idp = &staged_blob_id;
3240 return (*status_cb)(status_arg, status, staged_status,
3241 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3244 static const struct got_error *
3245 status_old_new(void *arg, struct got_fileindex_entry *ie,
3246 struct dirent *de, const char *parent_path, int dirfd)
3248 const struct got_error *err = NULL;
3249 struct diff_dir_cb_arg *a = arg;
3252 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3253 return got_error(GOT_ERR_CANCELLED);
3255 if (got_path_cmp(parent_path, a->status_path,
3256 strlen(parent_path), a->status_path_len) != 0 &&
3257 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3260 if (parent_path[0]) {
3261 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3262 parent_path, de->d_name) == -1)
3263 return got_error_from_errno("asprintf");
3265 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3267 return got_error_from_errno("asprintf");
3270 err = report_file_status(ie, abspath, dirfd, de->d_name,
3271 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3276 static const struct got_error *
3277 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3279 struct diff_dir_cb_arg *a = arg;
3280 struct got_object_id blob_id, commit_id;
3281 unsigned char status;
3283 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3284 return got_error(GOT_ERR_CANCELLED);
3286 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3289 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3290 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3291 if (got_fileindex_entry_has_file_on_disk(ie))
3292 status = GOT_STATUS_MISSING;
3294 status = GOT_STATUS_DELETE;
3295 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3296 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3300 free_ignorelist(struct got_pathlist_head *ignorelist)
3302 struct got_pathlist_entry *pe;
3304 TAILQ_FOREACH(pe, ignorelist, entry)
3305 free((char *)pe->path);
3306 got_pathlist_free(ignorelist);
3310 free_ignores(struct got_pathlist_head *ignores)
3312 struct got_pathlist_entry *pe;
3314 TAILQ_FOREACH(pe, ignores, entry) {
3315 struct got_pathlist_head *ignorelist = pe->data;
3316 free_ignorelist(ignorelist);
3317 free((char *)pe->path);
3319 got_pathlist_free(ignores);
3322 static const struct got_error *
3323 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3325 const struct got_error *err = NULL;
3326 struct got_pathlist_entry *pe = NULL;
3327 struct got_pathlist_head *ignorelist;
3328 char *line = NULL, *pattern, *dirpath = NULL;
3329 size_t linesize = 0;
3332 ignorelist = calloc(1, sizeof(*ignorelist));
3333 if (ignorelist == NULL)
3334 return got_error_from_errno("calloc");
3335 TAILQ_INIT(ignorelist);
3337 while ((linelen = getline(&line, &linesize, f)) != -1) {
3338 if (linelen > 0 && line[linelen - 1] == '\n')
3339 line[linelen - 1] = '\0';
3341 /* Git's ignores may contain comments. */
3345 /* Git's negated patterns are not (yet?) supported. */
3349 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3351 err = got_error_from_errno("asprintf");
3354 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3359 err = got_error_from_errno("getline");
3363 dirpath = strdup(path);
3364 if (dirpath == NULL) {
3365 err = got_error_from_errno("strdup");
3368 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3371 if (err || pe == NULL) {
3373 free_ignorelist(ignorelist);
3379 match_ignores(struct got_pathlist_head *ignores, const char *path)
3381 struct got_pathlist_entry *pe;
3383 /* Handle patterns which match in all directories. */
3384 TAILQ_FOREACH(pe, ignores, entry) {
3385 struct got_pathlist_head *ignorelist = pe->data;
3386 struct got_pathlist_entry *pi;
3388 TAILQ_FOREACH(pi, ignorelist, entry) {
3389 const char *p, *pattern = pi->path;
3391 if (strncmp(pattern, "**/", 3) != 0)
3396 if (fnmatch(pattern, p,
3397 FNM_PATHNAME | FNM_LEADING_DIR)) {
3398 /* Retry in next directory. */
3399 while (*p && *p != '/')
3411 * The ignores pathlist contains ignore lists from children before
3412 * parents, so we can find the most specific ignorelist by walking
3413 * ignores backwards.
3415 pe = TAILQ_LAST(ignores, got_pathlist_head);
3417 if (got_path_is_child(path, pe->path, pe->path_len)) {
3418 struct got_pathlist_head *ignorelist = pe->data;
3419 struct got_pathlist_entry *pi;
3420 TAILQ_FOREACH(pi, ignorelist, entry) {
3421 const char *pattern = pi->path;
3422 int flags = FNM_LEADING_DIR;
3423 if (strstr(pattern, "/**/") == NULL)
3424 flags |= FNM_PATHNAME;
3425 if (fnmatch(pattern, path, flags))
3430 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3436 static const struct got_error *
3437 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3438 const char *path, int dirfd, const char *ignores_filename)
3440 const struct got_error *err = NULL;
3443 FILE *ignoresfile = NULL;
3445 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3446 path[0] ? "/" : "", ignores_filename) == -1)
3447 return got_error_from_errno("asprintf");
3450 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3452 if (errno != ENOENT && errno != EACCES)
3453 err = got_error_from_errno2("openat",
3456 ignoresfile = fdopen(fd, "r");
3457 if (ignoresfile == NULL)
3458 err = got_error_from_errno2("fdopen",
3462 err = read_ignores(ignores, path, ignoresfile);
3466 ignoresfile = fopen(ignorespath, "r");
3467 if (ignoresfile == NULL) {
3468 if (errno != ENOENT && errno != EACCES)
3469 err = got_error_from_errno2("fopen",
3472 err = read_ignores(ignores, path, ignoresfile);
3475 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3476 err = got_error_from_errno2("fclose", path);
3477 if (fd != -1 && close(fd) == -1 && err == NULL)
3478 err = got_error_from_errno2("close", path);
3483 static const struct got_error *
3484 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3486 const struct got_error *err = NULL;
3487 struct diff_dir_cb_arg *a = arg;
3490 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3491 return got_error(GOT_ERR_CANCELLED);
3493 if (parent_path[0]) {
3494 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3495 return got_error_from_errno("asprintf");
3500 if (de->d_type != DT_DIR &&
3501 got_path_is_child(path, a->status_path, a->status_path_len)
3502 && !match_ignores(&a->ignores, path))
3503 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3504 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3510 static const struct got_error *
3511 status_traverse(void *arg, const char *path, int dirfd)
3513 const struct got_error *err = NULL;
3514 struct diff_dir_cb_arg *a = arg;
3519 err = add_ignores(&a->ignores, a->worktree->root_path,
3520 path, dirfd, ".cvsignore");
3524 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3525 dirfd, ".gitignore");
3530 static const struct got_error *
3531 report_single_file_status(const char *path, const char *ondisk_path,
3532 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3533 void *status_arg, struct got_repository *repo, int report_unchanged)
3535 struct got_fileindex_entry *ie;
3538 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3540 return report_file_status(ie, ondisk_path, -1, NULL,
3541 status_cb, status_arg, repo, report_unchanged);
3543 if (lstat(ondisk_path, &sb) == -1) {
3544 if (errno != ENOENT)
3545 return got_error_from_errno2("lstat", ondisk_path);
3546 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3547 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3551 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3552 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3553 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3558 static const struct got_error *
3559 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3560 const char *root_path, const char *path)
3562 const struct got_error *err;
3563 char *parent_path, *next_parent_path = NULL;
3565 err = add_ignores(ignores, root_path, "", -1,
3570 err = add_ignores(ignores, root_path, "", -1,
3575 err = got_path_dirname(&parent_path, path);
3577 if (err->code == GOT_ERR_BAD_PATH)
3578 return NULL; /* cannot traverse parent */
3582 err = add_ignores(ignores, root_path, parent_path, -1,
3586 err = add_ignores(ignores, root_path, parent_path, -1,
3590 err = got_path_dirname(&next_parent_path, parent_path);
3592 if (err->code == GOT_ERR_BAD_PATH)
3593 err = NULL; /* traversed everything */
3597 parent_path = next_parent_path;
3598 next_parent_path = NULL;
3602 free(next_parent_path);
3606 static const struct got_error *
3607 worktree_status(struct got_worktree *worktree, const char *path,
3608 struct got_fileindex *fileindex, struct got_repository *repo,
3609 got_worktree_status_cb status_cb, void *status_arg,
3610 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3611 int report_unchanged)
3613 const struct got_error *err = NULL;
3615 struct got_fileindex_diff_dir_cb fdiff_cb;
3616 struct diff_dir_cb_arg arg;
3617 char *ondisk_path = NULL;
3619 TAILQ_INIT(&arg.ignores);
3621 if (asprintf(&ondisk_path, "%s%s%s",
3622 worktree->root_path, path[0] ? "/" : "", path) == -1)
3623 return got_error_from_errno("asprintf");
3625 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3627 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3629 err = got_error_from_errno2("open", ondisk_path);
3631 err = report_single_file_status(path, ondisk_path,
3632 fileindex, status_cb, status_arg, repo,
3635 fdiff_cb.diff_old_new = status_old_new;
3636 fdiff_cb.diff_old = status_old;
3637 fdiff_cb.diff_new = status_new;
3638 fdiff_cb.diff_traverse = status_traverse;
3639 arg.fileindex = fileindex;
3640 arg.worktree = worktree;
3641 arg.status_path = path;
3642 arg.status_path_len = strlen(path);
3644 arg.status_cb = status_cb;
3645 arg.status_arg = status_arg;
3646 arg.cancel_cb = cancel_cb;
3647 arg.cancel_arg = cancel_arg;
3648 arg.report_unchanged = report_unchanged;
3649 arg.no_ignores = no_ignores;
3651 err = add_ignores_from_parent_paths(&arg.ignores,
3652 worktree->root_path, path);
3656 err = got_fileindex_diff_dir(fileindex, fd,
3657 worktree->root_path, path, repo, &fdiff_cb, &arg);
3660 free_ignores(&arg.ignores);
3661 if (fd != -1 && close(fd) == -1 && err == NULL)
3662 err = got_error_from_errno("close");
3667 const struct got_error *
3668 got_worktree_status(struct got_worktree *worktree,
3669 struct got_pathlist_head *paths, struct got_repository *repo,
3670 got_worktree_status_cb status_cb, void *status_arg,
3671 got_cancel_cb cancel_cb, void *cancel_arg)
3673 const struct got_error *err = NULL;
3674 char *fileindex_path = NULL;
3675 struct got_fileindex *fileindex = NULL;
3676 struct got_pathlist_entry *pe;
3678 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3682 TAILQ_FOREACH(pe, paths, entry) {
3683 err = worktree_status(worktree, pe->path, fileindex, repo,
3684 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3688 free(fileindex_path);
3689 got_fileindex_free(fileindex);
3693 const struct got_error *
3694 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3697 const struct got_error *err = NULL;
3698 char *resolved = NULL, *cwd = NULL, *path = NULL;
3701 char *abspath = NULL;
3702 char canonpath[PATH_MAX];
3706 cwd = getcwd(NULL, 0);
3708 return got_error_from_errno("getcwd");
3710 if (lstat(arg, &sb) == -1) {
3711 if (errno != ENOENT) {
3712 err = got_error_from_errno2("lstat", arg);
3717 if (S_ISLNK(sb.st_mode)) {
3719 * We cannot use realpath(3) with symlinks since we want to
3720 * operate on the symlink itself.
3721 * But we can make the path absolute, assuming it is relative
3722 * to the current working directory, and then canonicalize it.
3724 if (!got_path_is_absolute(arg)) {
3725 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3726 err = got_error_from_errno("asprintf");
3731 err = got_canonpath(abspath ? abspath : arg, canonpath,
3735 resolved = strdup(canonpath);
3736 if (resolved == NULL) {
3737 err = got_error_from_errno("strdup");
3741 resolved = realpath(arg, NULL);
3742 if (resolved == NULL) {
3743 if (errno != ENOENT) {
3744 err = got_error_from_errno2("realpath", arg);
3747 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3748 err = got_error_from_errno("asprintf");
3751 err = got_canonpath(abspath, canonpath,
3755 resolved = strdup(canonpath);
3756 if (resolved == NULL) {
3757 err = got_error_from_errno("strdup");
3763 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3764 strlen(got_worktree_get_root_path(worktree)))) {
3765 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3769 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3770 err = got_path_skip_common_ancestor(&path,
3771 got_worktree_get_root_path(worktree), resolved);
3777 err = got_error_from_errno("strdup");
3782 /* XXX status walk can't deal with trailing slash! */
3784 while (len > 0 && path[len - 1] == '/') {
3785 path[len - 1] = '\0';
3799 struct schedule_addition_args {
3800 struct got_worktree *worktree;
3801 struct got_fileindex *fileindex;
3802 got_worktree_checkout_cb progress_cb;
3804 struct got_repository *repo;
3807 static const struct got_error *
3808 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3809 const char *relpath, struct got_object_id *blob_id,
3810 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3811 int dirfd, const char *de_name)
3813 struct schedule_addition_args *a = arg;
3814 const struct got_error *err = NULL;
3815 struct got_fileindex_entry *ie;
3819 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3821 return got_error_from_errno("asprintf");
3823 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3825 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3829 /* Re-adding an existing entry is a no-op. */
3830 if (status == GOT_STATUS_ADD)
3832 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3837 if (status != GOT_STATUS_UNVERSIONED) {
3838 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3842 err = got_fileindex_entry_alloc(&ie, relpath);
3845 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3846 relpath, NULL, NULL, 1);
3848 got_fileindex_entry_free(ie);
3851 err = got_fileindex_entry_add(a->fileindex, ie);
3853 got_fileindex_entry_free(ie);
3860 if (status == GOT_STATUS_ADD)
3862 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3865 const struct got_error *
3866 got_worktree_schedule_add(struct got_worktree *worktree,
3867 struct got_pathlist_head *paths,
3868 got_worktree_checkout_cb progress_cb, void *progress_arg,
3869 struct got_repository *repo, int no_ignores)
3871 struct got_fileindex *fileindex = NULL;
3872 char *fileindex_path = NULL;
3873 const struct got_error *err = NULL, *sync_err, *unlockerr;
3874 struct got_pathlist_entry *pe;
3875 struct schedule_addition_args saa;
3877 err = lock_worktree(worktree, LOCK_EX);
3881 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3885 saa.worktree = worktree;
3886 saa.fileindex = fileindex;
3887 saa.progress_cb = progress_cb;
3888 saa.progress_arg = progress_arg;
3891 TAILQ_FOREACH(pe, paths, entry) {
3892 err = worktree_status(worktree, pe->path, fileindex, repo,
3893 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3897 sync_err = sync_fileindex(fileindex, fileindex_path);
3898 if (sync_err && err == NULL)
3901 free(fileindex_path);
3903 got_fileindex_free(fileindex);
3904 unlockerr = lock_worktree(worktree, LOCK_SH);
3905 if (unlockerr && err == NULL)
3910 struct schedule_deletion_args {
3911 struct got_worktree *worktree;
3912 struct got_fileindex *fileindex;
3913 got_worktree_delete_cb progress_cb;
3915 struct got_repository *repo;
3916 int delete_local_mods;
3918 const char *status_codes;
3921 static const struct got_error *
3922 schedule_for_deletion(void *arg, unsigned char status,
3923 unsigned char staged_status, const char *relpath,
3924 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3925 struct got_object_id *commit_id, int dirfd, const char *de_name)
3927 struct schedule_deletion_args *a = arg;
3928 const struct got_error *err = NULL;
3929 struct got_fileindex_entry *ie = NULL;
3933 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3935 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3937 staged_status = get_staged_status(ie);
3938 if (staged_status != GOT_STATUS_NO_CHANGE) {
3939 if (staged_status == GOT_STATUS_DELETE)
3941 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3944 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3946 return got_error_from_errno("asprintf");
3948 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3953 if (a->status_codes) {
3954 size_t ncodes = strlen(a->status_codes);
3956 for (i = 0; i < ncodes ; i++) {
3957 if (status == a->status_codes[i])
3961 /* Do not delete files in non-matching status. */
3965 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3966 a->status_codes[i] != GOT_STATUS_MISSING) {
3967 static char msg[64];
3968 snprintf(msg, sizeof(msg),
3969 "invalid status code '%c'", a->status_codes[i]);
3970 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3975 if (status != GOT_STATUS_NO_CHANGE) {
3976 if (status == GOT_STATUS_DELETE)
3978 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3979 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3982 if (status != GOT_STATUS_MODIFY &&
3983 status != GOT_STATUS_MISSING) {
3984 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3989 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3993 if (unlinkat(dirfd, de_name, 0) != 0) {
3994 err = got_error_from_errno2("unlinkat",
3998 } else if (unlink(ondisk_path) != 0) {
3999 err = got_error_from_errno2("unlink", ondisk_path);
4003 root_len = strlen(a->worktree->root_path);
4006 err = got_path_dirname(&parent, ondisk_path);
4010 ondisk_path = parent;
4011 if (rmdir(ondisk_path) == -1) {
4012 if (errno != ENOTEMPTY)
4013 err = got_error_from_errno2("rmdir",
4017 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4018 strlen(ondisk_path), root_len) != 0);
4021 got_fileindex_entry_mark_deleted_from_disk(ie);
4026 if (status == GOT_STATUS_DELETE)
4028 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4029 staged_status, relpath);
4032 const struct got_error *
4033 got_worktree_schedule_delete(struct got_worktree *worktree,
4034 struct got_pathlist_head *paths, int delete_local_mods,
4035 const char *status_codes,
4036 got_worktree_delete_cb progress_cb, void *progress_arg,
4037 struct got_repository *repo, int keep_on_disk)
4039 struct got_fileindex *fileindex = NULL;
4040 char *fileindex_path = NULL;
4041 const struct got_error *err = NULL, *sync_err, *unlockerr;
4042 struct got_pathlist_entry *pe;
4043 struct schedule_deletion_args sda;
4045 err = lock_worktree(worktree, LOCK_EX);
4049 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4053 sda.worktree = worktree;
4054 sda.fileindex = fileindex;
4055 sda.progress_cb = progress_cb;
4056 sda.progress_arg = progress_arg;
4058 sda.delete_local_mods = delete_local_mods;
4059 sda.keep_on_disk = keep_on_disk;
4060 sda.status_codes = status_codes;
4062 TAILQ_FOREACH(pe, paths, entry) {
4063 err = worktree_status(worktree, pe->path, fileindex, repo,
4064 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4068 sync_err = sync_fileindex(fileindex, fileindex_path);
4069 if (sync_err && err == NULL)
4072 free(fileindex_path);
4074 got_fileindex_free(fileindex);
4075 unlockerr = lock_worktree(worktree, LOCK_SH);
4076 if (unlockerr && err == NULL)
4081 static const struct got_error *
4082 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4084 const struct got_error *err = NULL;
4086 size_t linesize = 0, n;
4089 linelen = getline(&line, &linesize, infile);
4090 if (linelen == -1) {
4091 if (ferror(infile)) {
4092 err = got_error_from_errno("getline");
4098 n = fwrite(line, 1, linelen, outfile);
4100 err = got_ferror(outfile, GOT_ERR_IO);
4105 n = fwrite(line, 1, linelen, rejectfile);
4107 err = got_ferror(outfile, GOT_ERR_IO);
4114 static const struct got_error *
4115 skip_one_line(FILE *f)
4118 size_t linesize = 0;
4121 linelen = getline(&line, &linesize, f);
4122 if (linelen == -1) {
4124 return got_error_from_errno("getline");
4131 static const struct got_error *
4132 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4133 int start_old, int end_old, int start_new, int end_new,
4134 FILE *outfile, FILE *rejectfile)
4136 const struct got_error *err;
4138 /* Copy old file's lines leading up to patch. */
4139 while (!feof(f1) && *line_cur1 < start_old) {
4140 err = copy_one_line(f1, outfile, NULL);
4145 /* Skip new file's lines leading up to patch. */
4146 while (!feof(f2) && *line_cur2 < start_new) {
4148 err = copy_one_line(f2, NULL, rejectfile);
4150 err = skip_one_line(f2);
4155 /* Copy patched lines. */
4156 while (!feof(f2) && *line_cur2 <= end_new) {
4157 err = copy_one_line(f2, outfile, NULL);
4162 /* Skip over old file's replaced lines. */
4163 while (!feof(f1) && *line_cur1 <= end_old) {
4165 err = copy_one_line(f1, NULL, rejectfile);
4167 err = skip_one_line(f1);
4176 static const struct got_error *
4177 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4178 FILE *outfile, FILE *rejectfile)
4180 const struct got_error *err;
4183 /* Copy old file's lines until EOF. */
4185 err = copy_one_line(f1, outfile, NULL);
4192 /* Copy new file's lines until EOF. */
4194 err = copy_one_line(f2, NULL, rejectfile);
4204 static const struct got_error *
4205 apply_or_reject_change(int *choice, int *nchunks_used,
4206 struct diff_result *diff_result, int n,
4207 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4208 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4209 got_worktree_patch_cb patch_cb, void *patch_arg)
4211 const struct got_error *err = NULL;
4212 struct diff_chunk_context cc = {};
4213 int start_old, end_old, start_new, end_new;
4215 struct diff_output_unidiff_state *diff_state;
4216 struct diff_input_info diff_info;
4219 *choice = GOT_PATCH_CHOICE_NONE;
4221 /* Get changed line numbers without context lines for copy_change(). */
4222 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4223 start_old = cc.left.start;
4224 end_old = cc.left.end;
4225 start_new = cc.right.start;
4226 end_new = cc.right.end;
4228 /* Get the same change with context lines for display. */
4229 memset(&cc, 0, sizeof(cc));
4230 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4232 memset(&diff_info, 0, sizeof(diff_info));
4233 diff_info.left_path = relpath;
4234 diff_info.right_path = relpath;
4236 diff_state = diff_output_unidiff_state_alloc();
4237 if (diff_state == NULL)
4238 return got_error_set_errno(ENOMEM,
4239 "diff_output_unidiff_state_alloc");
4241 hunkfile = got_opentemp();
4242 if (hunkfile == NULL) {
4243 err = got_error_from_errno("got_opentemp");
4247 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4249 if (rc != DIFF_RC_OK) {
4250 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4254 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4255 err = got_ferror(hunkfile, GOT_ERR_IO);
4259 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4260 hunkfile, changeno, nchanges);
4265 case GOT_PATCH_CHOICE_YES:
4266 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4267 end_old, start_new, end_new, outfile, rejectfile);
4269 case GOT_PATCH_CHOICE_NO:
4270 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4271 end_old, start_new, end_new, rejectfile, outfile);
4273 case GOT_PATCH_CHOICE_QUIT:
4276 err = got_error(GOT_ERR_PATCH_CHOICE);
4280 diff_output_unidiff_state_free(diff_state);
4281 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4282 err = got_error_from_errno("fclose");
4286 struct revert_file_args {
4287 struct got_worktree *worktree;
4288 struct got_fileindex *fileindex;
4289 got_worktree_checkout_cb progress_cb;
4291 got_worktree_patch_cb patch_cb;
4293 struct got_repository *repo;
4296 static const struct got_error *
4297 create_patched_content(char **path_outfile, int reverse_patch,
4298 struct got_object_id *blob_id, const char *path2,
4299 int dirfd2, const char *de_name2,
4300 const char *relpath, struct got_repository *repo,
4301 got_worktree_patch_cb patch_cb, void *patch_arg)
4303 const struct got_error *err, *free_err;
4304 struct got_blob_object *blob = NULL;
4305 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4307 char link_target[PATH_MAX];
4308 ssize_t link_len = 0;
4309 char *path1 = NULL, *id_str = NULL;
4311 struct got_diffreg_result *diffreg_result = NULL;
4312 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4313 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4315 *path_outfile = NULL;
4317 err = got_object_id_str(&id_str, blob_id);
4322 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4324 if (errno != ELOOP) {
4325 err = got_error_from_errno2("openat", path2);
4328 link_len = readlinkat(dirfd2, de_name2,
4329 link_target, sizeof(link_target));
4331 return got_error_from_errno2("readlinkat", path2);
4332 sb2.st_mode = S_IFLNK;
4333 sb2.st_size = link_len;
4336 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4338 if (errno != ELOOP) {
4339 err = got_error_from_errno2("open", path2);
4342 link_len = readlink(path2, link_target,
4343 sizeof(link_target));
4345 return got_error_from_errno2("readlink", path2);
4346 sb2.st_mode = S_IFLNK;
4347 sb2.st_size = link_len;
4351 if (fstat(fd2, &sb2) == -1) {
4352 err = got_error_from_errno2("fstat", path2);
4356 f2 = fdopen(fd2, "r");
4358 err = got_error_from_errno2("fdopen", path2);
4364 f2 = got_opentemp();
4366 err = got_error_from_errno2("got_opentemp", path2);
4369 n = fwrite(link_target, 1, link_len, f2);
4370 if (n != link_len) {
4371 err = got_ferror(f2, GOT_ERR_IO);
4374 if (fflush(f2) == EOF) {
4375 err = got_error_from_errno("fflush");
4381 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4385 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4389 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4393 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4398 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4402 if (fseek(f1, 0L, SEEK_SET) == -1)
4403 return got_ferror(f1, GOT_ERR_IO);
4404 if (fseek(f2, 0L, SEEK_SET) == -1)
4405 return got_ferror(f2, GOT_ERR_IO);
4407 /* Count the number of actual changes in the diff result. */
4408 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4409 struct diff_chunk_context cc = {};
4410 diff_chunk_context_load_change(&cc, &nchunks_used,
4411 diffreg_result->result, n, 0);
4414 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4416 err = apply_or_reject_change(&choice, &nchunks_used,
4417 diffreg_result->result, n, relpath, f1, f2,
4418 &line_cur1, &line_cur2,
4419 reverse_patch ? NULL : outfile,
4420 reverse_patch ? outfile : NULL,
4421 ++i, nchanges, patch_cb, patch_arg);
4424 if (choice == GOT_PATCH_CHOICE_YES)
4426 else if (choice == GOT_PATCH_CHOICE_QUIT)
4430 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4431 reverse_patch ? NULL : outfile,
4432 reverse_patch ? outfile : NULL);
4436 if (!S_ISLNK(sb2.st_mode)) {
4437 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4438 err = got_error_from_errno2("fchmod", path2);
4446 got_object_blob_close(blob);
4447 free_err = got_diffreg_result_free(diffreg_result);
4450 if (f1 && fclose(f1) == EOF && err == NULL)
4451 err = got_error_from_errno2("fclose", path1);
4452 if (f2 && fclose(f2) == EOF && err == NULL)
4453 err = got_error_from_errno2("fclose", path2);
4454 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4455 err = got_error_from_errno2("close", path2);
4456 if (outfile && fclose(outfile) == EOF && err == NULL)
4457 err = got_error_from_errno2("fclose", *path_outfile);
4458 if (path1 && unlink(path1) == -1 && err == NULL)
4459 err = got_error_from_errno2("unlink", path1);
4460 if (err || !have_content) {
4461 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4462 err = got_error_from_errno2("unlink", *path_outfile);
4463 free(*path_outfile);
4464 *path_outfile = NULL;
4470 static const struct got_error *
4471 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4472 const char *relpath, struct got_object_id *blob_id,
4473 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4474 int dirfd, const char *de_name)
4476 struct revert_file_args *a = arg;
4477 const struct got_error *err = NULL;
4478 char *parent_path = NULL;
4479 struct got_fileindex_entry *ie;
4480 struct got_tree_object *tree = NULL;
4481 struct got_object_id *tree_id = NULL;
4482 const struct got_tree_entry *te = NULL;
4483 char *tree_path = NULL, *te_name;
4484 char *ondisk_path = NULL, *path_content = NULL;
4485 struct got_blob_object *blob = NULL;
4487 /* Reverting a staged deletion is a no-op. */
4488 if (status == GOT_STATUS_DELETE &&
4489 staged_status != GOT_STATUS_NO_CHANGE)
4492 if (status == GOT_STATUS_UNVERSIONED)
4493 return (*a->progress_cb)(a->progress_arg,
4494 GOT_STATUS_UNVERSIONED, relpath);
4496 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4498 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4500 /* Construct in-repository path of tree which contains this blob. */
4501 err = got_path_dirname(&parent_path, ie->path);
4503 if (err->code != GOT_ERR_BAD_PATH)
4505 parent_path = strdup("/");
4506 if (parent_path == NULL) {
4507 err = got_error_from_errno("strdup");
4511 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4512 tree_path = strdup(parent_path);
4513 if (tree_path == NULL) {
4514 err = got_error_from_errno("strdup");
4518 if (got_path_is_root_dir(parent_path)) {
4519 tree_path = strdup(a->worktree->path_prefix);
4520 if (tree_path == NULL) {
4521 err = got_error_from_errno("strdup");
4525 if (asprintf(&tree_path, "%s/%s",
4526 a->worktree->path_prefix, parent_path) == -1) {
4527 err = got_error_from_errno("asprintf");
4533 err = got_object_id_by_path(&tree_id, a->repo,
4534 a->worktree->base_commit_id, tree_path);
4536 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4537 (status == GOT_STATUS_ADD ||
4538 staged_status == GOT_STATUS_ADD)))
4541 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4545 err = got_path_basename(&te_name, ie->path);
4549 te = got_object_tree_find_entry(tree, te_name);
4551 if (te == NULL && status != GOT_STATUS_ADD &&
4552 staged_status != GOT_STATUS_ADD) {
4553 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4559 case GOT_STATUS_ADD:
4561 int choice = GOT_PATCH_CHOICE_NONE;
4562 err = (*a->patch_cb)(&choice, a->patch_arg,
4563 status, ie->path, NULL, 1, 1);
4566 if (choice != GOT_PATCH_CHOICE_YES)
4569 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4573 got_fileindex_entry_remove(a->fileindex, ie);
4575 case GOT_STATUS_DELETE:
4577 int choice = GOT_PATCH_CHOICE_NONE;
4578 err = (*a->patch_cb)(&choice, a->patch_arg,
4579 status, ie->path, NULL, 1, 1);
4582 if (choice != GOT_PATCH_CHOICE_YES)
4586 case GOT_STATUS_MODIFY:
4587 case GOT_STATUS_MODE_CHANGE:
4588 case GOT_STATUS_CONFLICT:
4589 case GOT_STATUS_MISSING: {
4590 struct got_object_id id;
4591 if (staged_status == GOT_STATUS_ADD ||
4592 staged_status == GOT_STATUS_MODIFY) {
4593 memcpy(id.sha1, ie->staged_blob_sha1,
4594 SHA1_DIGEST_LENGTH);
4596 memcpy(id.sha1, ie->blob_sha1,
4597 SHA1_DIGEST_LENGTH);
4598 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4602 if (asprintf(&ondisk_path, "%s/%s",
4603 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4604 err = got_error_from_errno("asprintf");
4608 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4609 status == GOT_STATUS_CONFLICT)) {
4610 int is_bad_symlink = 0;
4611 err = create_patched_content(&path_content, 1, &id,
4612 ondisk_path, dirfd, de_name, ie->path, a->repo,
4613 a->patch_cb, a->patch_arg);
4614 if (err || path_content == NULL)
4616 if (te && S_ISLNK(te->mode)) {
4617 if (unlink(path_content) == -1) {
4618 err = got_error_from_errno2("unlink",
4622 err = install_symlink(&is_bad_symlink,
4623 a->worktree, ondisk_path, ie->path,
4624 blob, 0, 1, 0, a->repo,
4625 a->progress_cb, a->progress_arg);
4627 if (rename(path_content, ondisk_path) == -1) {
4628 err = got_error_from_errno3("rename",
4629 path_content, ondisk_path);
4634 int is_bad_symlink = 0;
4635 if (te && S_ISLNK(te->mode)) {
4636 err = install_symlink(&is_bad_symlink,
4637 a->worktree, ondisk_path, ie->path,
4638 blob, 0, 1, 0, a->repo,
4639 a->progress_cb, a->progress_arg);
4641 err = install_blob(a->worktree, ondisk_path,
4643 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4644 got_fileindex_perms_to_st(ie), blob,
4645 0, 1, 0, 0, a->repo,
4646 a->progress_cb, a->progress_arg);
4650 if (status == GOT_STATUS_DELETE ||
4651 status == GOT_STATUS_MODE_CHANGE) {
4652 err = got_fileindex_entry_update(ie,
4653 a->worktree->root_fd, relpath,
4655 a->worktree->base_commit_id->sha1, 1);
4659 if (is_bad_symlink) {
4660 got_fileindex_entry_filetype_set(ie,
4661 GOT_FILEIDX_MODE_BAD_SYMLINK);
4675 got_object_blob_close(blob);
4677 got_object_tree_close(tree);
4682 const struct got_error *
4683 got_worktree_revert(struct got_worktree *worktree,
4684 struct got_pathlist_head *paths,
4685 got_worktree_checkout_cb progress_cb, void *progress_arg,
4686 got_worktree_patch_cb patch_cb, void *patch_arg,
4687 struct got_repository *repo)
4689 struct got_fileindex *fileindex = NULL;
4690 char *fileindex_path = NULL;
4691 const struct got_error *err = NULL, *unlockerr = NULL;
4692 const struct got_error *sync_err = NULL;
4693 struct got_pathlist_entry *pe;
4694 struct revert_file_args rfa;
4696 err = lock_worktree(worktree, LOCK_EX);
4700 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4704 rfa.worktree = worktree;
4705 rfa.fileindex = fileindex;
4706 rfa.progress_cb = progress_cb;
4707 rfa.progress_arg = progress_arg;
4708 rfa.patch_cb = patch_cb;
4709 rfa.patch_arg = patch_arg;
4711 TAILQ_FOREACH(pe, paths, entry) {
4712 err = worktree_status(worktree, pe->path, fileindex, repo,
4713 revert_file, &rfa, NULL, NULL, 0, 0);
4717 sync_err = sync_fileindex(fileindex, fileindex_path);
4718 if (sync_err && err == NULL)
4721 free(fileindex_path);
4723 got_fileindex_free(fileindex);
4724 unlockerr = lock_worktree(worktree, LOCK_SH);
4725 if (unlockerr && err == NULL)
4731 free_commitable(struct got_commitable *ct)
4734 free(ct->in_repo_path);
4735 free(ct->ondisk_path);
4737 free(ct->base_blob_id);
4738 free(ct->staged_blob_id);
4739 free(ct->base_commit_id);
4743 struct collect_commitables_arg {
4744 struct got_pathlist_head *commitable_paths;
4745 struct got_repository *repo;
4746 struct got_worktree *worktree;
4747 struct got_fileindex *fileindex;
4748 int have_staged_files;
4749 int allow_bad_symlinks;
4752 static const struct got_error *
4753 collect_commitables(void *arg, unsigned char status,
4754 unsigned char staged_status, const char *relpath,
4755 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4756 struct got_object_id *commit_id, int dirfd, const char *de_name)
4758 struct collect_commitables_arg *a = arg;
4759 const struct got_error *err = NULL;
4760 struct got_commitable *ct = NULL;
4761 struct got_pathlist_entry *new = NULL;
4762 char *parent_path = NULL, *path = NULL;
4765 if (a->have_staged_files) {
4766 if (staged_status != GOT_STATUS_MODIFY &&
4767 staged_status != GOT_STATUS_ADD &&
4768 staged_status != GOT_STATUS_DELETE)
4771 if (status == GOT_STATUS_CONFLICT)
4772 return got_error(GOT_ERR_COMMIT_CONFLICT);
4774 if (status != GOT_STATUS_MODIFY &&
4775 status != GOT_STATUS_MODE_CHANGE &&
4776 status != GOT_STATUS_ADD &&
4777 status != GOT_STATUS_DELETE)
4781 if (asprintf(&path, "/%s", relpath) == -1) {
4782 err = got_error_from_errno("asprintf");
4785 if (strcmp(path, "/") == 0) {
4786 parent_path = strdup("");
4787 if (parent_path == NULL)
4788 return got_error_from_errno("strdup");
4790 err = got_path_dirname(&parent_path, path);
4795 ct = calloc(1, sizeof(*ct));
4797 err = got_error_from_errno("calloc");
4801 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4803 err = got_error_from_errno("asprintf");
4807 if (staged_status == GOT_STATUS_ADD ||
4808 staged_status == GOT_STATUS_MODIFY) {
4809 struct got_fileindex_entry *ie;
4810 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4811 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4812 case GOT_FILEIDX_MODE_REGULAR_FILE:
4813 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4816 case GOT_FILEIDX_MODE_SYMLINK:
4820 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4823 ct->mode |= got_fileindex_entry_perms_get(ie);
4824 } else if (status != GOT_STATUS_DELETE &&
4825 staged_status != GOT_STATUS_DELETE) {
4827 if (fstatat(dirfd, de_name, &sb,
4828 AT_SYMLINK_NOFOLLOW) == -1) {
4829 err = got_error_from_errno2("fstatat",
4833 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4834 err = got_error_from_errno2("lstat", ct->ondisk_path);
4837 ct->mode = sb.st_mode;
4840 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4841 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4843 err = got_error_from_errno("asprintf");
4847 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4848 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4850 char target_path[PATH_MAX];
4852 target_len = readlink(ct->ondisk_path, target_path,
4853 sizeof(target_path));
4854 if (target_len == -1) {
4855 err = got_error_from_errno2("readlink",
4859 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4860 target_len, ct->ondisk_path, a->worktree->root_path);
4863 if (is_bad_symlink) {
4864 err = got_error_path(ct->ondisk_path,
4865 GOT_ERR_BAD_SYMLINK);
4871 ct->status = status;
4872 ct->staged_status = staged_status;
4873 ct->blob_id = NULL; /* will be filled in when blob gets created */
4874 if (ct->status != GOT_STATUS_ADD &&
4875 ct->staged_status != GOT_STATUS_ADD) {
4876 ct->base_blob_id = got_object_id_dup(blob_id);
4877 if (ct->base_blob_id == NULL) {
4878 err = got_error_from_errno("got_object_id_dup");
4881 ct->base_commit_id = got_object_id_dup(commit_id);
4882 if (ct->base_commit_id == NULL) {
4883 err = got_error_from_errno("got_object_id_dup");
4887 if (ct->staged_status == GOT_STATUS_ADD ||
4888 ct->staged_status == GOT_STATUS_MODIFY) {
4889 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4890 if (ct->staged_blob_id == NULL) {
4891 err = got_error_from_errno("got_object_id_dup");
4895 ct->path = strdup(path);
4896 if (ct->path == NULL) {
4897 err = got_error_from_errno("strdup");
4900 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4902 if (ct && (err || new == NULL))
4903 free_commitable(ct);
4909 static const struct got_error *write_tree(struct got_object_id **, int *,
4910 struct got_tree_object *, const char *, struct got_pathlist_head *,
4911 got_worktree_status_cb status_cb, void *status_arg,
4912 struct got_repository *);
4914 static const struct got_error *
4915 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4916 struct got_tree_entry *te, const char *parent_path,
4917 struct got_pathlist_head *commitable_paths,
4918 got_worktree_status_cb status_cb, void *status_arg,
4919 struct got_repository *repo)
4921 const struct got_error *err = NULL;
4922 struct got_tree_object *subtree;
4925 if (asprintf(&subpath, "%s%s%s", parent_path,
4926 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4927 return got_error_from_errno("asprintf");
4929 err = got_object_open_as_tree(&subtree, repo, &te->id);
4933 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4934 commitable_paths, status_cb, status_arg, repo);
4935 got_object_tree_close(subtree);
4940 static const struct got_error *
4941 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4943 const struct got_error *err = NULL;
4944 char *ct_parent_path = NULL;
4948 if (strchr(ct->in_repo_path, '/') == NULL) {
4949 *match = got_path_is_root_dir(path);
4953 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4956 *match = (strcmp(path, ct_parent_path) == 0);
4957 free(ct_parent_path);
4962 get_ct_file_mode(struct got_commitable *ct)
4964 if (S_ISLNK(ct->mode))
4967 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4970 static const struct got_error *
4971 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4972 struct got_tree_entry *te, struct got_commitable *ct)
4974 const struct got_error *err = NULL;
4978 err = got_object_tree_entry_dup(new_te, te);
4982 (*new_te)->mode = get_ct_file_mode(ct);
4984 if (ct->staged_status == GOT_STATUS_MODIFY)
4985 memcpy(&(*new_te)->id, ct->staged_blob_id,
4986 sizeof((*new_te)->id));
4988 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4990 if (err && *new_te) {
4997 static const struct got_error *
4998 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4999 struct got_commitable *ct)
5001 const struct got_error *err = NULL;
5002 char *ct_name = NULL;
5006 *new_te = calloc(1, sizeof(**new_te));
5007 if (*new_te == NULL)
5008 return got_error_from_errno("calloc");
5010 err = got_path_basename(&ct_name, ct->path);
5013 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5014 sizeof((*new_te)->name)) {
5015 err = got_error(GOT_ERR_NO_SPACE);
5019 (*new_te)->mode = get_ct_file_mode(ct);
5021 if (ct->staged_status == GOT_STATUS_ADD)
5022 memcpy(&(*new_te)->id, ct->staged_blob_id,
5023 sizeof((*new_te)->id));
5025 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5028 if (err && *new_te) {
5035 static const struct got_error *
5036 insert_tree_entry(struct got_tree_entry *new_te,
5037 struct got_pathlist_head *paths)
5039 const struct got_error *err = NULL;
5040 struct got_pathlist_entry *new_pe;
5042 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5046 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5050 static const struct got_error *
5051 report_ct_status(struct got_commitable *ct,
5052 got_worktree_status_cb status_cb, void *status_arg)
5054 const char *ct_path = ct->path;
5055 unsigned char status;
5057 while (ct_path[0] == '/')
5060 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5061 status = ct->staged_status;
5063 status = ct->status;
5065 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5066 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5069 static const struct got_error *
5070 match_modified_subtree(int *modified, struct got_tree_entry *te,
5071 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5073 const struct got_error *err = NULL;
5074 struct got_pathlist_entry *pe;
5079 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5080 got_path_is_root_dir(base_tree_path) ? "" : "/",
5082 return got_error_from_errno("asprintf");
5084 TAILQ_FOREACH(pe, commitable_paths, entry) {
5085 struct got_commitable *ct = pe->data;
5086 *modified = got_path_is_child(ct->in_repo_path, te_path,
5096 static const struct got_error *
5097 match_deleted_or_modified_ct(struct got_commitable **ctp,
5098 struct got_tree_entry *te, const char *base_tree_path,
5099 struct got_pathlist_head *commitable_paths)
5101 const struct got_error *err = NULL;
5102 struct got_pathlist_entry *pe;
5106 TAILQ_FOREACH(pe, commitable_paths, entry) {
5107 struct got_commitable *ct = pe->data;
5108 char *ct_name = NULL;
5111 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5112 if (ct->status != GOT_STATUS_MODIFY &&
5113 ct->status != GOT_STATUS_MODE_CHANGE &&
5114 ct->status != GOT_STATUS_DELETE)
5117 if (ct->staged_status != GOT_STATUS_MODIFY &&
5118 ct->staged_status != GOT_STATUS_DELETE)
5122 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5125 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5131 err = got_path_basename(&ct_name, pe->path);
5135 if (strcmp(te->name, ct_name) != 0) {
5148 static const struct got_error *
5149 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5150 const char *child_path, const char *path_base_tree,
5151 struct got_pathlist_head *commitable_paths,
5152 got_worktree_status_cb status_cb, void *status_arg,
5153 struct got_repository *repo)
5155 const struct got_error *err = NULL;
5156 struct got_tree_entry *new_te;
5158 struct got_object_id *id = NULL;
5163 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5164 got_path_is_root_dir(path_base_tree) ? "" : "/",
5166 return got_error_from_errno("asprintf");
5168 new_te = calloc(1, sizeof(*new_te));
5170 return got_error_from_errno("calloc");
5171 new_te->mode = S_IFDIR;
5173 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5174 sizeof(new_te->name)) {
5175 err = got_error(GOT_ERR_NO_SPACE);
5178 err = write_tree(&id, &nentries, NULL, subtree_path,
5179 commitable_paths, status_cb, status_arg, repo);
5184 memcpy(&new_te->id, id, sizeof(new_te->id));
5193 static const struct got_error *
5194 write_tree(struct got_object_id **new_tree_id, int *nentries,
5195 struct got_tree_object *base_tree, const char *path_base_tree,
5196 struct got_pathlist_head *commitable_paths,
5197 got_worktree_status_cb status_cb, void *status_arg,
5198 struct got_repository *repo)
5200 const struct got_error *err = NULL;
5201 struct got_pathlist_head paths;
5202 struct got_tree_entry *te, *new_te = NULL;
5203 struct got_pathlist_entry *pe;
5208 /* Insert, and recurse into, newly added entries first. */
5209 TAILQ_FOREACH(pe, commitable_paths, entry) {
5210 struct got_commitable *ct = pe->data;
5211 char *child_path = NULL, *slash;
5213 if ((ct->status != GOT_STATUS_ADD &&
5214 ct->staged_status != GOT_STATUS_ADD) ||
5215 (ct->flags & GOT_COMMITABLE_ADDED))
5218 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5219 strlen(path_base_tree)))
5222 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5227 slash = strchr(child_path, '/');
5228 if (slash == NULL) {
5229 err = alloc_added_blob_tree_entry(&new_te, ct);
5232 err = report_ct_status(ct, status_cb, status_arg);
5235 ct->flags |= GOT_COMMITABLE_ADDED;
5236 err = insert_tree_entry(new_te, &paths);
5241 *slash = '\0'; /* trim trailing path components */
5242 if (base_tree == NULL ||
5243 got_object_tree_find_entry(base_tree, child_path)
5245 err = make_subtree_for_added_blob(&new_te,
5246 child_path, path_base_tree,
5247 commitable_paths, status_cb, status_arg,
5251 err = insert_tree_entry(new_te, &paths);
5260 int i, nbase_entries;
5261 /* Handle modified and deleted entries. */
5262 nbase_entries = got_object_tree_get_nentries(base_tree);
5263 for (i = 0; i < nbase_entries; i++) {
5264 struct got_commitable *ct = NULL;
5266 te = got_object_tree_get_entry(base_tree, i);
5267 if (got_object_tree_entry_is_submodule(te)) {
5268 /* Entry is a submodule; just copy it. */
5269 err = got_object_tree_entry_dup(&new_te, te);
5272 err = insert_tree_entry(new_te, &paths);
5279 if (S_ISDIR(te->mode)) {
5281 err = got_object_tree_entry_dup(&new_te, te);
5284 err = match_modified_subtree(&modified, te,
5285 path_base_tree, commitable_paths);
5288 /* Avoid recursion into unmodified subtrees. */
5290 struct got_object_id *new_id;
5292 err = write_subtree(&new_id,
5294 path_base_tree, commitable_paths,
5295 status_cb, status_arg, repo);
5298 if (nsubentries == 0) {
5299 /* All entries were deleted. */
5303 memcpy(&new_te->id, new_id,
5304 sizeof(new_te->id));
5307 err = insert_tree_entry(new_te, &paths);
5314 err = match_deleted_or_modified_ct(&ct, te,
5315 path_base_tree, commitable_paths);
5319 /* NB: Deleted entries get dropped here. */
5320 if (ct->status == GOT_STATUS_MODIFY ||
5321 ct->status == GOT_STATUS_MODE_CHANGE ||
5322 ct->staged_status == GOT_STATUS_MODIFY) {
5323 err = alloc_modified_blob_tree_entry(
5327 err = insert_tree_entry(new_te, &paths);
5332 err = report_ct_status(ct, status_cb,
5337 /* Entry is unchanged; just copy it. */
5338 err = got_object_tree_entry_dup(&new_te, te);
5341 err = insert_tree_entry(new_te, &paths);
5349 /* Write new list of entries; deleted entries have been dropped. */
5350 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5352 got_pathlist_free(&paths);
5356 static const struct got_error *
5357 update_fileindex_after_commit(struct got_worktree *worktree,
5358 struct got_pathlist_head *commitable_paths,
5359 struct got_object_id *new_base_commit_id,
5360 struct got_fileindex *fileindex, int have_staged_files)
5362 const struct got_error *err = NULL;
5363 struct got_pathlist_entry *pe;
5364 char *relpath = NULL;
5366 TAILQ_FOREACH(pe, commitable_paths, entry) {
5367 struct got_fileindex_entry *ie;
5368 struct got_commitable *ct = pe->data;
5370 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5372 err = got_path_skip_common_ancestor(&relpath,
5373 worktree->root_path, ct->ondisk_path);
5378 if (ct->status == GOT_STATUS_DELETE ||
5379 ct->staged_status == GOT_STATUS_DELETE) {
5380 got_fileindex_entry_remove(fileindex, ie);
5381 } else if (ct->staged_status == GOT_STATUS_ADD ||
5382 ct->staged_status == GOT_STATUS_MODIFY) {
5383 got_fileindex_entry_stage_set(ie,
5384 GOT_FILEIDX_STAGE_NONE);
5385 got_fileindex_entry_staged_filetype_set(ie, 0);
5387 err = got_fileindex_entry_update(ie,
5388 worktree->root_fd, relpath,
5389 ct->staged_blob_id->sha1,
5390 new_base_commit_id->sha1,
5391 !have_staged_files);
5393 err = got_fileindex_entry_update(ie,
5394 worktree->root_fd, relpath,
5396 new_base_commit_id->sha1,
5397 !have_staged_files);
5399 err = got_fileindex_entry_alloc(&ie, pe->path);
5402 err = got_fileindex_entry_update(ie,
5403 worktree->root_fd, relpath, ct->blob_id->sha1,
5404 new_base_commit_id->sha1, 1);
5406 got_fileindex_entry_free(ie);
5409 err = got_fileindex_entry_add(fileindex, ie);
5411 got_fileindex_entry_free(ie);
5424 static const struct got_error *
5425 check_out_of_date(const char *in_repo_path, unsigned char status,
5426 unsigned char staged_status, struct got_object_id *base_blob_id,
5427 struct got_object_id *base_commit_id,
5428 struct got_object_id *head_commit_id, struct got_repository *repo,
5431 const struct got_error *err = NULL;
5432 struct got_object_id *id = NULL;
5434 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5435 /* Trivial case: base commit == head commit */
5436 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5439 * Ensure file content which local changes were based
5440 * on matches file content in the branch head.
5442 err = got_object_id_by_path(&id, repo, head_commit_id,
5445 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5446 err = got_error(ood_errcode);
5448 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5449 err = got_error(ood_errcode);
5451 /* Require that added files don't exist in the branch head. */
5452 err = got_object_id_by_path(&id, repo, head_commit_id,
5454 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5456 err = id ? got_error(ood_errcode) : NULL;
5463 const struct got_error *
5464 commit_worktree(struct got_object_id **new_commit_id,
5465 struct got_pathlist_head *commitable_paths,
5466 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5467 const char *author, const char *committer,
5468 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5469 got_worktree_status_cb status_cb, void *status_arg,
5470 struct got_repository *repo)
5472 const struct got_error *err = NULL, *unlockerr = NULL;
5473 struct got_pathlist_entry *pe;
5474 const char *head_ref_name = NULL;
5475 struct got_commit_object *head_commit = NULL;
5476 struct got_reference *head_ref2 = NULL;
5477 struct got_object_id *head_commit_id2 = NULL;
5478 struct got_tree_object *head_tree = NULL;
5479 struct got_object_id *new_tree_id = NULL;
5481 struct got_object_id_queue parent_ids;
5482 struct got_object_qid *pid = NULL;
5483 char *logmsg = NULL;
5485 *new_commit_id = NULL;
5487 SIMPLEQ_INIT(&parent_ids);
5489 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5493 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5497 if (commit_msg_cb != NULL) {
5498 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5503 if (logmsg == NULL || strlen(logmsg) == 0) {
5504 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5508 /* Create blobs from added and modified files and record their IDs. */
5509 TAILQ_FOREACH(pe, commitable_paths, entry) {
5510 struct got_commitable *ct = pe->data;
5513 /* Blobs for staged files already exist. */
5514 if (ct->staged_status == GOT_STATUS_ADD ||
5515 ct->staged_status == GOT_STATUS_MODIFY)
5518 if (ct->status != GOT_STATUS_ADD &&
5519 ct->status != GOT_STATUS_MODIFY &&
5520 ct->status != GOT_STATUS_MODE_CHANGE)
5523 if (asprintf(&ondisk_path, "%s/%s",
5524 worktree->root_path, pe->path) == -1) {
5525 err = got_error_from_errno("asprintf");
5528 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5534 /* Recursively write new tree objects. */
5535 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5536 commitable_paths, status_cb, status_arg, repo);
5540 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5543 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5544 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5545 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5546 got_object_qid_free(pid);
5552 /* Check if a concurrent commit to our branch has occurred. */
5553 head_ref_name = got_worktree_get_head_ref_name(worktree);
5554 if (head_ref_name == NULL) {
5555 err = got_error_from_errno("got_worktree_get_head_ref_name");
5558 /* Lock the reference here to prevent concurrent modification. */
5559 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5562 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5565 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5566 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5569 /* Update branch head in repository. */
5570 err = got_ref_change_ref(head_ref2, *new_commit_id);
5573 err = got_ref_write(head_ref2, repo);
5577 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5581 err = ref_base_commit(worktree, repo);
5586 got_object_tree_close(head_tree);
5588 got_object_commit_close(head_commit);
5589 free(head_commit_id2);
5591 unlockerr = got_ref_unlock(head_ref2);
5592 if (unlockerr && err == NULL)
5594 got_ref_close(head_ref2);
5599 static const struct got_error *
5600 check_path_is_commitable(const char *path,
5601 struct got_pathlist_head *commitable_paths)
5603 struct got_pathlist_entry *cpe = NULL;
5604 size_t path_len = strlen(path);
5606 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5607 struct got_commitable *ct = cpe->data;
5608 const char *ct_path = ct->path;
5610 while (ct_path[0] == '/')
5613 if (strcmp(path, ct_path) == 0 ||
5614 got_path_is_child(ct_path, path, path_len))
5619 return got_error_path(path, GOT_ERR_BAD_PATH);
5624 static const struct got_error *
5625 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5627 int *have_staged_files = arg;
5629 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5630 *have_staged_files = 1;
5631 return got_error(GOT_ERR_CANCELLED);
5637 static const struct got_error *
5638 check_non_staged_files(struct got_fileindex *fileindex,
5639 struct got_pathlist_head *paths)
5641 struct got_pathlist_entry *pe;
5642 struct got_fileindex_entry *ie;
5644 TAILQ_FOREACH(pe, paths, entry) {
5645 if (pe->path[0] == '\0')
5647 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5649 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5650 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5651 return got_error_path(pe->path,
5652 GOT_ERR_FILE_NOT_STAGED);
5658 const struct got_error *
5659 got_worktree_commit(struct got_object_id **new_commit_id,
5660 struct got_worktree *worktree, struct got_pathlist_head *paths,
5661 const char *author, const char *committer, int allow_bad_symlinks,
5662 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5663 got_worktree_status_cb status_cb, void *status_arg,
5664 struct got_repository *repo)
5666 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5667 struct got_fileindex *fileindex = NULL;
5668 char *fileindex_path = NULL;
5669 struct got_pathlist_head commitable_paths;
5670 struct collect_commitables_arg cc_arg;
5671 struct got_pathlist_entry *pe;
5672 struct got_reference *head_ref = NULL;
5673 struct got_object_id *head_commit_id = NULL;
5674 int have_staged_files = 0;
5676 *new_commit_id = NULL;
5678 TAILQ_INIT(&commitable_paths);
5680 err = lock_worktree(worktree, LOCK_EX);
5684 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5688 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5692 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5696 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5697 &have_staged_files);
5698 if (err && err->code != GOT_ERR_CANCELLED)
5700 if (have_staged_files) {
5701 err = check_non_staged_files(fileindex, paths);
5706 cc_arg.commitable_paths = &commitable_paths;
5707 cc_arg.worktree = worktree;
5708 cc_arg.fileindex = fileindex;
5710 cc_arg.have_staged_files = have_staged_files;
5711 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5712 TAILQ_FOREACH(pe, paths, entry) {
5713 err = worktree_status(worktree, pe->path, fileindex, repo,
5714 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5719 if (TAILQ_EMPTY(&commitable_paths)) {
5720 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5724 TAILQ_FOREACH(pe, paths, entry) {
5725 err = check_path_is_commitable(pe->path, &commitable_paths);
5730 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5731 struct got_commitable *ct = pe->data;
5732 const char *ct_path = ct->in_repo_path;
5734 while (ct_path[0] == '/')
5736 err = check_out_of_date(ct_path, ct->status,
5737 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5738 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5744 err = commit_worktree(new_commit_id, &commitable_paths,
5745 head_commit_id, worktree, author, committer,
5746 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5750 err = update_fileindex_after_commit(worktree, &commitable_paths,
5751 *new_commit_id, fileindex, have_staged_files);
5752 sync_err = sync_fileindex(fileindex, fileindex_path);
5753 if (sync_err && err == NULL)
5757 got_fileindex_free(fileindex);
5758 free(fileindex_path);
5759 unlockerr = lock_worktree(worktree, LOCK_SH);
5760 if (unlockerr && err == NULL)
5762 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5763 struct got_commitable *ct = pe->data;
5764 free_commitable(ct);
5766 got_pathlist_free(&commitable_paths);
5771 got_commitable_get_path(struct got_commitable *ct)
5777 got_commitable_get_status(struct got_commitable *ct)
5782 struct check_rebase_ok_arg {
5783 struct got_worktree *worktree;
5784 struct got_repository *repo;
5787 static const struct got_error *
5788 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5790 const struct got_error *err = NULL;
5791 struct check_rebase_ok_arg *a = arg;
5792 unsigned char status;
5796 /* Reject rebase of a work tree with mixed base commits. */
5797 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5798 SHA1_DIGEST_LENGTH))
5799 return got_error(GOT_ERR_MIXED_COMMITS);
5801 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5803 return got_error_from_errno("asprintf");
5805 /* Reject rebase of a work tree with modified or staged files. */
5806 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5811 if (status != GOT_STATUS_NO_CHANGE)
5812 return got_error(GOT_ERR_MODIFIED);
5813 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5814 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5819 const struct got_error *
5820 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5821 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5822 struct got_worktree *worktree, struct got_reference *branch,
5823 struct got_repository *repo)
5825 const struct got_error *err = NULL;
5826 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5827 char *branch_ref_name = NULL;
5828 char *fileindex_path = NULL;
5829 struct check_rebase_ok_arg ok_arg;
5830 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5831 struct got_object_id *wt_branch_tip = NULL;
5833 *new_base_branch_ref = NULL;
5837 err = lock_worktree(worktree, LOCK_EX);
5841 err = open_fileindex(fileindex, &fileindex_path, worktree);
5845 ok_arg.worktree = worktree;
5847 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5852 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5856 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5860 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5864 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5869 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5872 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5873 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5877 err = got_ref_alloc_symref(new_base_branch_ref,
5878 new_base_branch_ref_name, wt_branch);
5881 err = got_ref_write(*new_base_branch_ref, repo);
5885 /* TODO Lock original branch's ref while rebasing? */
5887 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5891 err = got_ref_write(branch_ref, repo);
5895 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5896 worktree->base_commit_id);
5899 err = got_ref_write(*tmp_branch, repo);
5903 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5907 free(fileindex_path);
5908 free(tmp_branch_name);
5909 free(new_base_branch_ref_name);
5910 free(branch_ref_name);
5912 got_ref_close(branch_ref);
5914 got_ref_close(wt_branch);
5915 free(wt_branch_tip);
5917 if (*new_base_branch_ref) {
5918 got_ref_close(*new_base_branch_ref);
5919 *new_base_branch_ref = NULL;
5922 got_ref_close(*tmp_branch);
5926 got_fileindex_free(*fileindex);
5929 lock_worktree(worktree, LOCK_SH);
5934 const struct got_error *
5935 got_worktree_rebase_continue(struct got_object_id **commit_id,
5936 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5937 struct got_reference **branch, struct got_fileindex **fileindex,
5938 struct got_worktree *worktree, struct got_repository *repo)
5940 const struct got_error *err;
5941 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5942 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5943 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5944 char *fileindex_path = NULL;
5945 int have_staged_files = 0;
5948 *new_base_branch = NULL;
5953 err = lock_worktree(worktree, LOCK_EX);
5957 err = open_fileindex(fileindex, &fileindex_path, worktree);
5961 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5962 &have_staged_files);
5963 if (err && err->code != GOT_ERR_CANCELLED)
5965 if (have_staged_files) {
5966 err = got_error(GOT_ERR_STAGED_PATHS);
5970 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5974 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5978 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5982 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5986 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5990 err = got_ref_open(branch, repo,
5991 got_ref_get_symref_target(branch_ref), 0);
5995 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5999 err = got_ref_resolve(commit_id, repo, commit_ref);
6003 err = got_ref_open(new_base_branch, repo,
6004 new_base_branch_ref_name, 0);
6008 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6012 free(commit_ref_name);
6013 free(branch_ref_name);
6014 free(fileindex_path);
6016 got_ref_close(commit_ref);
6018 got_ref_close(branch_ref);
6023 got_ref_close(*tmp_branch);
6026 if (*new_base_branch) {
6027 got_ref_close(*new_base_branch);
6028 *new_base_branch = NULL;
6031 got_ref_close(*branch);
6035 got_fileindex_free(*fileindex);
6038 lock_worktree(worktree, LOCK_SH);
6043 const struct got_error *
6044 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6046 const struct got_error *err;
6047 char *tmp_branch_name = NULL;
6049 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6053 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6054 free(tmp_branch_name);
6058 static const struct got_error *
6059 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6060 char **logmsg, void *arg)
6066 static const struct got_error *
6067 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6068 const char *path, struct got_object_id *blob_id,
6069 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6070 int dirfd, const char *de_name)
6075 struct collect_merged_paths_arg {
6076 got_worktree_checkout_cb progress_cb;
6078 struct got_pathlist_head *merged_paths;
6081 static const struct got_error *
6082 collect_merged_paths(void *arg, unsigned char status, const char *path)
6084 const struct got_error *err;
6085 struct collect_merged_paths_arg *a = arg;
6087 struct got_pathlist_entry *new;
6089 err = (*a->progress_cb)(a->progress_arg, status, path);
6093 if (status != GOT_STATUS_MERGE &&
6094 status != GOT_STATUS_ADD &&
6095 status != GOT_STATUS_DELETE &&
6096 status != GOT_STATUS_CONFLICT)
6101 return got_error_from_errno("strdup");
6103 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6104 if (err || new == NULL)
6110 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6112 struct got_pathlist_entry *pe;
6114 TAILQ_FOREACH(pe, merged_paths, entry)
6115 free((char *)pe->path);
6117 got_pathlist_free(merged_paths);
6120 static const struct got_error *
6121 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6122 int is_rebase, struct got_repository *repo)
6124 const struct got_error *err;
6125 struct got_reference *commit_ref = NULL;
6127 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6129 if (err->code != GOT_ERR_NOT_REF)
6131 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6134 err = got_ref_write(commit_ref, repo);
6137 } else if (is_rebase) {
6138 struct got_object_id *stored_id;
6141 err = got_ref_resolve(&stored_id, repo, commit_ref);
6144 cmp = got_object_id_cmp(commit_id, stored_id);
6147 err = got_error(GOT_ERR_REBASE_COMMITID);
6153 got_ref_close(commit_ref);
6157 static const struct got_error *
6158 rebase_merge_files(struct got_pathlist_head *merged_paths,
6159 const char *commit_ref_name, struct got_worktree *worktree,
6160 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6161 struct got_object_id *commit_id, struct got_repository *repo,
6162 got_worktree_checkout_cb progress_cb, void *progress_arg,
6163 got_cancel_cb cancel_cb, void *cancel_arg)
6165 const struct got_error *err;
6166 struct got_reference *commit_ref = NULL;
6167 struct collect_merged_paths_arg cmp_arg;
6168 char *fileindex_path;
6170 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6172 err = get_fileindex_path(&fileindex_path, worktree);
6176 cmp_arg.progress_cb = progress_cb;
6177 cmp_arg.progress_arg = progress_arg;
6178 cmp_arg.merged_paths = merged_paths;
6179 err = merge_files(worktree, fileindex, fileindex_path,
6180 parent_commit_id, commit_id, repo, collect_merged_paths,
6181 &cmp_arg, cancel_cb, cancel_arg);
6183 got_ref_close(commit_ref);
6187 const struct got_error *
6188 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6189 struct got_worktree *worktree, struct got_fileindex *fileindex,
6190 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6191 struct got_repository *repo,
6192 got_worktree_checkout_cb progress_cb, void *progress_arg,
6193 got_cancel_cb cancel_cb, void *cancel_arg)
6195 const struct got_error *err;
6196 char *commit_ref_name;
6198 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6202 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6206 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6207 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6208 progress_arg, cancel_cb, cancel_arg);
6210 free(commit_ref_name);
6214 const struct got_error *
6215 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6216 struct got_worktree *worktree, struct got_fileindex *fileindex,
6217 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6218 struct got_repository *repo,
6219 got_worktree_checkout_cb progress_cb, void *progress_arg,
6220 got_cancel_cb cancel_cb, void *cancel_arg)
6222 const struct got_error *err;
6223 char *commit_ref_name;
6225 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6229 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6233 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6234 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6235 progress_arg, cancel_cb, cancel_arg);
6237 free(commit_ref_name);
6241 static const struct got_error *
6242 rebase_commit(struct got_object_id **new_commit_id,
6243 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6244 struct got_worktree *worktree, struct got_fileindex *fileindex,
6245 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6246 const char *new_logmsg, struct got_repository *repo)
6248 const struct got_error *err, *sync_err;
6249 struct got_pathlist_head commitable_paths;
6250 struct collect_commitables_arg cc_arg;
6251 char *fileindex_path = NULL;
6252 struct got_reference *head_ref = NULL;
6253 struct got_object_id *head_commit_id = NULL;
6254 char *logmsg = NULL;
6256 TAILQ_INIT(&commitable_paths);
6257 *new_commit_id = NULL;
6259 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6261 err = get_fileindex_path(&fileindex_path, worktree);
6265 cc_arg.commitable_paths = &commitable_paths;
6266 cc_arg.worktree = worktree;
6268 cc_arg.have_staged_files = 0;
6270 * If possible get the status of individual files directly to
6271 * avoid crawling the entire work tree once per rebased commit.
6272 * TODO: Ideally, merged_paths would contain a list of commitables
6273 * we could use so we could skip worktree_status() entirely.
6276 struct got_pathlist_entry *pe;
6277 TAILQ_FOREACH(pe, merged_paths, entry) {
6278 err = worktree_status(worktree, pe->path, fileindex,
6279 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6285 err = worktree_status(worktree, "", fileindex, repo,
6286 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6291 if (TAILQ_EMPTY(&commitable_paths)) {
6292 /* No-op change; commit will be elided. */
6293 err = got_ref_delete(commit_ref, repo);
6296 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6300 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6304 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6309 logmsg = strdup(new_logmsg);
6310 if (logmsg == NULL) {
6311 err = got_error_from_errno("strdup");
6315 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6320 /* NB: commit_worktree will call free(logmsg) */
6321 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6322 worktree, got_object_commit_get_author(orig_commit),
6323 got_object_commit_get_committer(orig_commit),
6324 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6328 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6332 err = got_ref_delete(commit_ref, repo);
6336 err = update_fileindex_after_commit(worktree, &commitable_paths,
6337 *new_commit_id, fileindex, 0);
6338 sync_err = sync_fileindex(fileindex, fileindex_path);
6339 if (sync_err && err == NULL)
6342 free(fileindex_path);
6343 free(head_commit_id);
6345 got_ref_close(head_ref);
6347 free(*new_commit_id);
6348 *new_commit_id = NULL;
6353 const struct got_error *
6354 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6355 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6356 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6357 struct got_commit_object *orig_commit,
6358 struct got_object_id *orig_commit_id, struct got_repository *repo)
6360 const struct got_error *err;
6361 char *commit_ref_name;
6362 struct got_reference *commit_ref = NULL;
6363 struct got_object_id *commit_id = NULL;
6365 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6369 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6372 err = got_ref_resolve(&commit_id, repo, commit_ref);
6375 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6376 err = got_error(GOT_ERR_REBASE_COMMITID);
6380 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6381 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6384 got_ref_close(commit_ref);
6385 free(commit_ref_name);
6390 const struct got_error *
6391 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6392 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6393 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6394 struct got_commit_object *orig_commit,
6395 struct got_object_id *orig_commit_id, const char *new_logmsg,
6396 struct got_repository *repo)
6398 const struct got_error *err;
6399 char *commit_ref_name;
6400 struct got_reference *commit_ref = NULL;
6402 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6406 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6410 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6411 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6414 got_ref_close(commit_ref);
6415 free(commit_ref_name);
6419 const struct got_error *
6420 got_worktree_rebase_postpone(struct got_worktree *worktree,
6421 struct got_fileindex *fileindex)
6424 got_fileindex_free(fileindex);
6425 return lock_worktree(worktree, LOCK_SH);
6428 static const struct got_error *
6429 delete_ref(const char *name, struct got_repository *repo)
6431 const struct got_error *err;
6432 struct got_reference *ref;
6434 err = got_ref_open(&ref, repo, name, 0);
6436 if (err->code == GOT_ERR_NOT_REF)
6441 err = got_ref_delete(ref, repo);
6446 static const struct got_error *
6447 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6449 const struct got_error *err;
6450 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6451 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6453 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6456 err = delete_ref(tmp_branch_name, repo);
6460 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6463 err = delete_ref(new_base_branch_ref_name, repo);
6467 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6470 err = delete_ref(branch_ref_name, repo);
6474 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6477 err = delete_ref(commit_ref_name, repo);
6482 free(tmp_branch_name);
6483 free(new_base_branch_ref_name);
6484 free(branch_ref_name);
6485 free(commit_ref_name);
6489 const struct got_error *
6490 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6491 struct got_object_id *new_commit_id, struct got_repository *repo)
6493 const struct got_error *err;
6494 struct got_reference *ref = NULL;
6495 struct got_object_id *old_commit_id = NULL;
6496 const char *branch_name = NULL;
6497 char *new_id_str = NULL;
6498 char *refname = NULL;
6500 branch_name = got_ref_get_name(branch);
6501 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6502 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6505 err = got_object_id_str(&new_id_str, new_commit_id);
6509 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6510 new_id_str) == -1) {
6511 err = got_error_from_errno("asprintf");
6515 err = got_ref_resolve(&old_commit_id, repo, branch);
6519 err = got_ref_alloc(&ref, refname, old_commit_id);
6523 err = got_ref_write(ref, repo);
6527 free(old_commit_id);
6533 const struct got_error *
6534 got_worktree_rebase_complete(struct got_worktree *worktree,
6535 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6536 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6537 struct got_repository *repo, int create_backup)
6539 const struct got_error *err, *unlockerr, *sync_err;
6540 struct got_object_id *new_head_commit_id = NULL;
6541 char *fileindex_path = NULL;
6543 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6547 if (create_backup) {
6548 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6549 rebased_branch, new_head_commit_id, repo);
6554 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6558 err = got_ref_write(rebased_branch, repo);
6562 err = got_worktree_set_head_ref(worktree, rebased_branch);
6566 err = delete_rebase_refs(worktree, repo);
6570 err = get_fileindex_path(&fileindex_path, worktree);
6573 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6574 sync_err = sync_fileindex(fileindex, fileindex_path);
6575 if (sync_err && err == NULL)
6578 got_fileindex_free(fileindex);
6579 free(fileindex_path);
6580 free(new_head_commit_id);
6581 unlockerr = lock_worktree(worktree, LOCK_SH);
6582 if (unlockerr && err == NULL)
6587 const struct got_error *
6588 got_worktree_rebase_abort(struct got_worktree *worktree,
6589 struct got_fileindex *fileindex, struct got_repository *repo,
6590 struct got_reference *new_base_branch,
6591 got_worktree_checkout_cb progress_cb, void *progress_arg)
6593 const struct got_error *err, *unlockerr, *sync_err;
6594 struct got_reference *resolved = NULL;
6595 struct got_object_id *commit_id = NULL;
6596 char *fileindex_path = NULL;
6597 struct revert_file_args rfa;
6598 struct got_object_id *tree_id = NULL;
6600 err = lock_worktree(worktree, LOCK_EX);
6604 err = got_ref_open(&resolved, repo,
6605 got_ref_get_symref_target(new_base_branch), 0);
6609 err = got_worktree_set_head_ref(worktree, resolved);
6614 * XXX commits to the base branch could have happened while
6615 * we were busy rebasing; should we store the original commit ID
6616 * when rebase begins and read it back here?
6618 err = got_ref_resolve(&commit_id, repo, resolved);
6622 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6626 err = got_object_id_by_path(&tree_id, repo,
6627 worktree->base_commit_id, worktree->path_prefix);
6631 err = delete_rebase_refs(worktree, repo);
6635 err = get_fileindex_path(&fileindex_path, worktree);
6639 rfa.worktree = worktree;
6640 rfa.fileindex = fileindex;
6641 rfa.progress_cb = progress_cb;
6642 rfa.progress_arg = progress_arg;
6643 rfa.patch_cb = NULL;
6644 rfa.patch_arg = NULL;
6646 err = worktree_status(worktree, "", fileindex, repo,
6647 revert_file, &rfa, NULL, NULL, 0, 0);
6651 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6652 repo, progress_cb, progress_arg, NULL, NULL);
6654 sync_err = sync_fileindex(fileindex, fileindex_path);
6655 if (sync_err && err == NULL)
6658 got_ref_close(resolved);
6662 got_fileindex_free(fileindex);
6663 free(fileindex_path);
6665 unlockerr = lock_worktree(worktree, LOCK_SH);
6666 if (unlockerr && err == NULL)
6671 const struct got_error *
6672 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6673 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6674 struct got_fileindex **fileindex, struct got_worktree *worktree,
6675 struct got_repository *repo)
6677 const struct got_error *err = NULL;
6678 char *tmp_branch_name = NULL;
6679 char *branch_ref_name = NULL;
6680 char *base_commit_ref_name = NULL;
6681 char *fileindex_path = NULL;
6682 struct check_rebase_ok_arg ok_arg;
6683 struct got_reference *wt_branch = NULL;
6684 struct got_reference *base_commit_ref = NULL;
6688 *base_commit_id = NULL;
6691 err = lock_worktree(worktree, LOCK_EX);
6695 err = open_fileindex(fileindex, &fileindex_path, worktree);
6699 ok_arg.worktree = worktree;
6701 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6706 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6710 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6714 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6719 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6724 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6728 err = got_ref_write(*branch_ref, repo);
6732 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6733 worktree->base_commit_id);
6736 err = got_ref_write(base_commit_ref, repo);
6739 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6740 if (*base_commit_id == NULL) {
6741 err = got_error_from_errno("got_object_id_dup");
6745 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6746 worktree->base_commit_id);
6749 err = got_ref_write(*tmp_branch, repo);
6753 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6757 free(fileindex_path);
6758 free(tmp_branch_name);
6759 free(branch_ref_name);
6760 free(base_commit_ref_name);
6762 got_ref_close(wt_branch);
6765 got_ref_close(*branch_ref);
6769 got_ref_close(*tmp_branch);
6772 free(*base_commit_id);
6774 got_fileindex_free(*fileindex);
6777 lock_worktree(worktree, LOCK_SH);
6782 const struct got_error *
6783 got_worktree_histedit_postpone(struct got_worktree *worktree,
6784 struct got_fileindex *fileindex)
6787 got_fileindex_free(fileindex);
6788 return lock_worktree(worktree, LOCK_SH);
6791 const struct got_error *
6792 got_worktree_histedit_in_progress(int *in_progress,
6793 struct got_worktree *worktree)
6795 const struct got_error *err;
6796 char *tmp_branch_name = NULL;
6798 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6802 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6803 free(tmp_branch_name);
6807 const struct got_error *
6808 got_worktree_histedit_continue(struct got_object_id **commit_id,
6809 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6810 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6811 struct got_worktree *worktree, struct got_repository *repo)
6813 const struct got_error *err;
6814 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6815 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6816 struct got_reference *commit_ref = NULL;
6817 struct got_reference *base_commit_ref = NULL;
6818 char *fileindex_path = NULL;
6819 int have_staged_files = 0;
6823 *base_commit_id = NULL;
6826 err = lock_worktree(worktree, LOCK_EX);
6830 err = open_fileindex(fileindex, &fileindex_path, worktree);
6834 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6835 &have_staged_files);
6836 if (err && err->code != GOT_ERR_CANCELLED)
6838 if (have_staged_files) {
6839 err = got_error(GOT_ERR_STAGED_PATHS);
6843 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6847 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6851 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6855 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6860 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6864 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6867 err = got_ref_resolve(commit_id, repo, commit_ref);
6871 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6874 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6878 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6882 free(commit_ref_name);
6883 free(branch_ref_name);
6884 free(fileindex_path);
6886 got_ref_close(commit_ref);
6887 if (base_commit_ref)
6888 got_ref_close(base_commit_ref);
6892 free(*base_commit_id);
6893 *base_commit_id = NULL;
6895 got_ref_close(*tmp_branch);
6899 got_fileindex_free(*fileindex);
6902 lock_worktree(worktree, LOCK_EX);
6907 static const struct got_error *
6908 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6910 const struct got_error *err;
6911 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6912 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6914 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6917 err = delete_ref(tmp_branch_name, repo);
6921 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6925 err = delete_ref(base_commit_ref_name, repo);
6929 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6932 err = delete_ref(branch_ref_name, repo);
6936 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6939 err = delete_ref(commit_ref_name, repo);
6943 free(tmp_branch_name);
6944 free(base_commit_ref_name);
6945 free(branch_ref_name);
6946 free(commit_ref_name);
6950 const struct got_error *
6951 got_worktree_histedit_abort(struct got_worktree *worktree,
6952 struct got_fileindex *fileindex, struct got_repository *repo,
6953 struct got_reference *branch, struct got_object_id *base_commit_id,
6954 got_worktree_checkout_cb progress_cb, void *progress_arg)
6956 const struct got_error *err, *unlockerr, *sync_err;
6957 struct got_reference *resolved = NULL;
6958 char *fileindex_path = NULL;
6959 struct got_object_id *tree_id = NULL;
6960 struct revert_file_args rfa;
6962 err = lock_worktree(worktree, LOCK_EX);
6966 err = got_ref_open(&resolved, repo,
6967 got_ref_get_symref_target(branch), 0);
6971 err = got_worktree_set_head_ref(worktree, resolved);
6975 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6979 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6980 worktree->path_prefix);
6984 err = delete_histedit_refs(worktree, repo);
6988 err = get_fileindex_path(&fileindex_path, worktree);
6992 rfa.worktree = worktree;
6993 rfa.fileindex = fileindex;
6994 rfa.progress_cb = progress_cb;
6995 rfa.progress_arg = progress_arg;
6996 rfa.patch_cb = NULL;
6997 rfa.patch_arg = NULL;
6999 err = worktree_status(worktree, "", fileindex, repo,
7000 revert_file, &rfa, NULL, NULL, 0, 0);
7004 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7005 repo, progress_cb, progress_arg, NULL, NULL);
7007 sync_err = sync_fileindex(fileindex, fileindex_path);
7008 if (sync_err && err == NULL)
7011 got_ref_close(resolved);
7013 free(fileindex_path);
7015 unlockerr = lock_worktree(worktree, LOCK_SH);
7016 if (unlockerr && err == NULL)
7021 const struct got_error *
7022 got_worktree_histedit_complete(struct got_worktree *worktree,
7023 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7024 struct got_reference *edited_branch, struct got_repository *repo)
7026 const struct got_error *err, *unlockerr, *sync_err;
7027 struct got_object_id *new_head_commit_id = NULL;
7028 struct got_reference *resolved = NULL;
7029 char *fileindex_path = NULL;
7031 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7035 err = got_ref_open(&resolved, repo,
7036 got_ref_get_symref_target(edited_branch), 0);
7040 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7041 resolved, new_head_commit_id, repo);
7045 err = got_ref_change_ref(resolved, new_head_commit_id);
7049 err = got_ref_write(resolved, repo);
7053 err = got_worktree_set_head_ref(worktree, resolved);
7057 err = delete_histedit_refs(worktree, repo);
7061 err = get_fileindex_path(&fileindex_path, worktree);
7064 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7065 sync_err = sync_fileindex(fileindex, fileindex_path);
7066 if (sync_err && err == NULL)
7069 got_fileindex_free(fileindex);
7070 free(fileindex_path);
7071 free(new_head_commit_id);
7072 unlockerr = lock_worktree(worktree, LOCK_SH);
7073 if (unlockerr && err == NULL)
7078 const struct got_error *
7079 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7080 struct got_object_id *commit_id, struct got_repository *repo)
7082 const struct got_error *err;
7083 char *commit_ref_name;
7085 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7089 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7093 err = delete_ref(commit_ref_name, repo);
7095 free(commit_ref_name);
7099 const struct got_error *
7100 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7101 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7102 struct got_worktree *worktree, const char *refname,
7103 struct got_repository *repo)
7105 const struct got_error *err = NULL;
7106 char *fileindex_path = NULL;
7107 struct check_rebase_ok_arg ok_arg;
7111 *base_branch_ref = NULL;
7113 err = lock_worktree(worktree, LOCK_EX);
7117 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7118 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7119 "cannot integrate a branch into itself; "
7120 "update -b or different branch name required");
7124 err = open_fileindex(fileindex, &fileindex_path, worktree);
7128 /* Preconditions are the same as for rebase. */
7129 ok_arg.worktree = worktree;
7131 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7136 err = got_ref_open(branch_ref, repo, refname, 1);
7140 err = got_ref_open(base_branch_ref, repo,
7141 got_worktree_get_head_ref_name(worktree), 1);
7145 got_ref_close(*branch_ref);
7148 if (*base_branch_ref) {
7149 got_ref_close(*base_branch_ref);
7150 *base_branch_ref = NULL;
7153 got_fileindex_free(*fileindex);
7156 lock_worktree(worktree, LOCK_SH);
7161 const struct got_error *
7162 got_worktree_integrate_continue(struct got_worktree *worktree,
7163 struct got_fileindex *fileindex, struct got_repository *repo,
7164 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7165 got_worktree_checkout_cb progress_cb, void *progress_arg,
7166 got_cancel_cb cancel_cb, void *cancel_arg)
7168 const struct got_error *err = NULL, *sync_err, *unlockerr;
7169 char *fileindex_path = NULL;
7170 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7172 err = get_fileindex_path(&fileindex_path, worktree);
7176 err = got_ref_resolve(&commit_id, repo, branch_ref);
7180 err = got_object_id_by_path(&tree_id, repo, commit_id,
7181 worktree->path_prefix);
7185 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7189 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7190 progress_cb, progress_arg, cancel_cb, cancel_arg);
7194 err = got_ref_change_ref(base_branch_ref, commit_id);
7198 err = got_ref_write(base_branch_ref, repo);
7202 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7204 sync_err = sync_fileindex(fileindex, fileindex_path);
7205 if (sync_err && err == NULL)
7209 unlockerr = got_ref_unlock(branch_ref);
7210 if (unlockerr && err == NULL)
7212 got_ref_close(branch_ref);
7214 unlockerr = got_ref_unlock(base_branch_ref);
7215 if (unlockerr && err == NULL)
7217 got_ref_close(base_branch_ref);
7219 got_fileindex_free(fileindex);
7220 free(fileindex_path);
7223 unlockerr = lock_worktree(worktree, LOCK_SH);
7224 if (unlockerr && err == NULL)
7229 const struct got_error *
7230 got_worktree_integrate_abort(struct got_worktree *worktree,
7231 struct got_fileindex *fileindex, struct got_repository *repo,
7232 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7234 const struct got_error *err = NULL, *unlockerr = NULL;
7236 got_fileindex_free(fileindex);
7238 err = lock_worktree(worktree, LOCK_SH);
7240 unlockerr = got_ref_unlock(branch_ref);
7241 if (unlockerr && err == NULL)
7243 got_ref_close(branch_ref);
7245 unlockerr = got_ref_unlock(base_branch_ref);
7246 if (unlockerr && err == NULL)
7248 got_ref_close(base_branch_ref);
7253 struct check_stage_ok_arg {
7254 struct got_object_id *head_commit_id;
7255 struct got_worktree *worktree;
7256 struct got_fileindex *fileindex;
7257 struct got_repository *repo;
7261 const struct got_error *
7262 check_stage_ok(void *arg, unsigned char status,
7263 unsigned char staged_status, const char *relpath,
7264 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7265 struct got_object_id *commit_id, int dirfd, const char *de_name)
7267 struct check_stage_ok_arg *a = arg;
7268 const struct got_error *err = NULL;
7269 struct got_fileindex_entry *ie;
7270 struct got_object_id base_commit_id;
7271 struct got_object_id *base_commit_idp = NULL;
7272 char *in_repo_path = NULL, *p;
7274 if (status == GOT_STATUS_UNVERSIONED ||
7275 status == GOT_STATUS_NO_CHANGE)
7277 if (status == GOT_STATUS_NONEXISTENT)
7278 return got_error_set_errno(ENOENT, relpath);
7280 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7282 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7284 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7285 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7287 return got_error_from_errno("asprintf");
7289 if (got_fileindex_entry_has_commit(ie)) {
7290 memcpy(base_commit_id.sha1, ie->commit_sha1,
7291 SHA1_DIGEST_LENGTH);
7292 base_commit_idp = &base_commit_id;
7295 if (status == GOT_STATUS_CONFLICT) {
7296 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7298 } else if (status != GOT_STATUS_ADD &&
7299 status != GOT_STATUS_MODIFY &&
7300 status != GOT_STATUS_DELETE) {
7301 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7305 a->have_changes = 1;
7310 err = check_out_of_date(p, status, staged_status,
7311 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7312 GOT_ERR_STAGE_OUT_OF_DATE);
7318 struct stage_path_arg {
7319 struct got_worktree *worktree;
7320 struct got_fileindex *fileindex;
7321 struct got_repository *repo;
7322 got_worktree_status_cb status_cb;
7324 got_worktree_patch_cb patch_cb;
7326 int staged_something;
7327 int allow_bad_symlinks;
7330 static const struct got_error *
7331 stage_path(void *arg, unsigned char status,
7332 unsigned char staged_status, const char *relpath,
7333 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7334 struct got_object_id *commit_id, int dirfd, const char *de_name)
7336 struct stage_path_arg *a = arg;
7337 const struct got_error *err = NULL;
7338 struct got_fileindex_entry *ie;
7339 char *ondisk_path = NULL, *path_content = NULL;
7341 struct got_object_id *new_staged_blob_id = NULL;
7344 if (status == GOT_STATUS_UNVERSIONED)
7347 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7349 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7351 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7353 return got_error_from_errno("asprintf");
7356 case GOT_STATUS_ADD:
7357 case GOT_STATUS_MODIFY:
7358 /* XXX could sb.st_mode be passed in by our caller? */
7359 if (lstat(ondisk_path, &sb) == -1) {
7360 err = got_error_from_errno2("lstat", ondisk_path);
7364 if (status == GOT_STATUS_ADD) {
7365 int choice = GOT_PATCH_CHOICE_NONE;
7366 err = (*a->patch_cb)(&choice, a->patch_arg,
7367 status, ie->path, NULL, 1, 1);
7370 if (choice != GOT_PATCH_CHOICE_YES)
7373 err = create_patched_content(&path_content, 0,
7374 staged_blob_id ? staged_blob_id : blob_id,
7375 ondisk_path, dirfd, de_name, ie->path,
7376 a->repo, a->patch_cb, a->patch_arg);
7377 if (err || path_content == NULL)
7381 err = got_object_blob_create(&new_staged_blob_id,
7382 path_content ? path_content : ondisk_path, a->repo);
7385 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7386 SHA1_DIGEST_LENGTH);
7387 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7388 stage = GOT_FILEIDX_STAGE_ADD;
7390 stage = GOT_FILEIDX_STAGE_MODIFY;
7391 got_fileindex_entry_stage_set(ie, stage);
7392 if (S_ISLNK(sb.st_mode)) {
7393 int is_bad_symlink = 0;
7394 if (!a->allow_bad_symlinks) {
7395 char target_path[PATH_MAX];
7397 target_len = readlink(ondisk_path, target_path,
7398 sizeof(target_path));
7399 if (target_len == -1) {
7400 err = got_error_from_errno2("readlink",
7404 err = is_bad_symlink_target(&is_bad_symlink,
7405 target_path, target_len, ondisk_path,
7406 a->worktree->root_path);
7409 if (is_bad_symlink) {
7410 err = got_error_path(ondisk_path,
7411 GOT_ERR_BAD_SYMLINK);
7416 got_fileindex_entry_staged_filetype_set(ie,
7417 GOT_FILEIDX_MODE_BAD_SYMLINK);
7419 got_fileindex_entry_staged_filetype_set(ie,
7420 GOT_FILEIDX_MODE_SYMLINK);
7422 got_fileindex_entry_staged_filetype_set(ie,
7423 GOT_FILEIDX_MODE_REGULAR_FILE);
7425 a->staged_something = 1;
7426 if (a->status_cb == NULL)
7428 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7429 get_staged_status(ie), relpath, blob_id,
7430 new_staged_blob_id, NULL, dirfd, de_name);
7432 case GOT_STATUS_DELETE:
7433 if (staged_status == GOT_STATUS_DELETE)
7436 int choice = GOT_PATCH_CHOICE_NONE;
7437 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7438 ie->path, NULL, 1, 1);
7441 if (choice == GOT_PATCH_CHOICE_NO)
7443 if (choice != GOT_PATCH_CHOICE_YES) {
7444 err = got_error(GOT_ERR_PATCH_CHOICE);
7448 stage = GOT_FILEIDX_STAGE_DELETE;
7449 got_fileindex_entry_stage_set(ie, stage);
7450 a->staged_something = 1;
7451 if (a->status_cb == NULL)
7453 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7454 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7457 case GOT_STATUS_NO_CHANGE:
7459 case GOT_STATUS_CONFLICT:
7460 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7462 case GOT_STATUS_NONEXISTENT:
7463 err = got_error_set_errno(ENOENT, relpath);
7466 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7470 if (path_content && unlink(path_content) == -1 && err == NULL)
7471 err = got_error_from_errno2("unlink", path_content);
7474 free(new_staged_blob_id);
7478 const struct got_error *
7479 got_worktree_stage(struct got_worktree *worktree,
7480 struct got_pathlist_head *paths,
7481 got_worktree_status_cb status_cb, void *status_arg,
7482 got_worktree_patch_cb patch_cb, void *patch_arg,
7483 int allow_bad_symlinks, struct got_repository *repo)
7485 const struct got_error *err = NULL, *sync_err, *unlockerr;
7486 struct got_pathlist_entry *pe;
7487 struct got_fileindex *fileindex = NULL;
7488 char *fileindex_path = NULL;
7489 struct got_reference *head_ref = NULL;
7490 struct got_object_id *head_commit_id = NULL;
7491 struct check_stage_ok_arg oka;
7492 struct stage_path_arg spa;
7494 err = lock_worktree(worktree, LOCK_EX);
7498 err = got_ref_open(&head_ref, repo,
7499 got_worktree_get_head_ref_name(worktree), 0);
7502 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7505 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7509 /* Check pre-conditions before staging anything. */
7510 oka.head_commit_id = head_commit_id;
7511 oka.worktree = worktree;
7512 oka.fileindex = fileindex;
7514 oka.have_changes = 0;
7515 TAILQ_FOREACH(pe, paths, entry) {
7516 err = worktree_status(worktree, pe->path, fileindex, repo,
7517 check_stage_ok, &oka, NULL, NULL, 0, 0);
7521 if (!oka.have_changes) {
7522 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7526 spa.worktree = worktree;
7527 spa.fileindex = fileindex;
7529 spa.patch_cb = patch_cb;
7530 spa.patch_arg = patch_arg;
7531 spa.status_cb = status_cb;
7532 spa.status_arg = status_arg;
7533 spa.staged_something = 0;
7534 spa.allow_bad_symlinks = allow_bad_symlinks;
7535 TAILQ_FOREACH(pe, paths, entry) {
7536 err = worktree_status(worktree, pe->path, fileindex, repo,
7537 stage_path, &spa, NULL, NULL, 0, 0);
7541 if (!spa.staged_something) {
7542 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7546 sync_err = sync_fileindex(fileindex, fileindex_path);
7547 if (sync_err && err == NULL)
7551 got_ref_close(head_ref);
7552 free(head_commit_id);
7553 free(fileindex_path);
7555 got_fileindex_free(fileindex);
7556 unlockerr = lock_worktree(worktree, LOCK_SH);
7557 if (unlockerr && err == NULL)
7562 struct unstage_path_arg {
7563 struct got_worktree *worktree;
7564 struct got_fileindex *fileindex;
7565 struct got_repository *repo;
7566 got_worktree_checkout_cb progress_cb;
7568 got_worktree_patch_cb patch_cb;
7572 static const struct got_error *
7573 create_unstaged_content(char **path_unstaged_content,
7574 char **path_new_staged_content, struct got_object_id *blob_id,
7575 struct got_object_id *staged_blob_id, const char *relpath,
7576 struct got_repository *repo,
7577 got_worktree_patch_cb patch_cb, void *patch_arg)
7579 const struct got_error *err, *free_err;
7580 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7581 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7582 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7583 struct got_diffreg_result *diffreg_result = NULL;
7584 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7585 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7587 *path_unstaged_content = NULL;
7588 *path_new_staged_content = NULL;
7590 err = got_object_id_str(&label1, blob_id);
7593 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7597 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7601 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7605 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7609 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7613 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7617 err = got_diff_files(&diffreg_result, f1, label1, f2,
7618 path2, 3, 0, 1, NULL);
7622 err = got_opentemp_named(path_unstaged_content, &outfile,
7623 "got-unstaged-content");
7626 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7627 "got-new-staged-content");
7631 if (fseek(f1, 0L, SEEK_SET) == -1) {
7632 err = got_ferror(f1, GOT_ERR_IO);
7635 if (fseek(f2, 0L, SEEK_SET) == -1) {
7636 err = got_ferror(f2, GOT_ERR_IO);
7639 /* Count the number of actual changes in the diff result. */
7640 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7641 struct diff_chunk_context cc = {};
7642 diff_chunk_context_load_change(&cc, &nchunks_used,
7643 diffreg_result->result, n, 0);
7646 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7648 err = apply_or_reject_change(&choice, &nchunks_used,
7649 diffreg_result->result, n, relpath, f1, f2,
7650 &line_cur1, &line_cur2,
7651 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7654 if (choice == GOT_PATCH_CHOICE_YES)
7657 have_rejected_content = 1;
7658 if (choice == GOT_PATCH_CHOICE_QUIT)
7661 if (have_content || have_rejected_content)
7662 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7663 outfile, rejectfile);
7667 got_object_blob_close(blob);
7669 got_object_blob_close(staged_blob);
7670 free_err = got_diffreg_result_free(diffreg_result);
7671 if (free_err && err == NULL)
7673 if (f1 && fclose(f1) == EOF && err == NULL)
7674 err = got_error_from_errno2("fclose", path1);
7675 if (f2 && fclose(f2) == EOF && err == NULL)
7676 err = got_error_from_errno2("fclose", path2);
7677 if (outfile && fclose(outfile) == EOF && err == NULL)
7678 err = got_error_from_errno2("fclose", *path_unstaged_content);
7679 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7680 err = got_error_from_errno2("fclose", *path_new_staged_content);
7681 if (path1 && unlink(path1) == -1 && err == NULL)
7682 err = got_error_from_errno2("unlink", path1);
7683 if (path2 && unlink(path2) == -1 && err == NULL)
7684 err = got_error_from_errno2("unlink", path2);
7685 if (err || !have_content) {
7686 if (*path_unstaged_content &&
7687 unlink(*path_unstaged_content) == -1 && err == NULL)
7688 err = got_error_from_errno2("unlink",
7689 *path_unstaged_content);
7690 free(*path_unstaged_content);
7691 *path_unstaged_content = NULL;
7693 if (err || !have_content || !have_rejected_content) {
7694 if (*path_new_staged_content &&
7695 unlink(*path_new_staged_content) == -1 && err == NULL)
7696 err = got_error_from_errno2("unlink",
7697 *path_new_staged_content);
7698 free(*path_new_staged_content);
7699 *path_new_staged_content = NULL;
7706 static const struct got_error *
7707 unstage_hunks(struct got_object_id *staged_blob_id,
7708 struct got_blob_object *blob_base,
7709 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7710 const char *ondisk_path, const char *label_orig,
7711 struct got_worktree *worktree, struct got_repository *repo,
7712 got_worktree_patch_cb patch_cb, void *patch_arg,
7713 got_worktree_checkout_cb progress_cb, void *progress_arg)
7715 const struct got_error *err = NULL;
7716 char *path_unstaged_content = NULL;
7717 char *path_new_staged_content = NULL;
7718 char *parent = NULL, *base_path = NULL;
7719 char *blob_base_path = NULL;
7720 struct got_object_id *new_staged_blob_id = NULL;
7721 FILE *f = NULL, *f_base = NULL;
7724 err = create_unstaged_content(&path_unstaged_content,
7725 &path_new_staged_content, blob_id, staged_blob_id,
7726 ie->path, repo, patch_cb, patch_arg);
7730 if (path_unstaged_content == NULL)
7733 if (path_new_staged_content) {
7734 err = got_object_blob_create(&new_staged_blob_id,
7735 path_new_staged_content, repo);
7740 f = fopen(path_unstaged_content, "r");
7742 err = got_error_from_errno2("fopen",
7743 path_unstaged_content);
7746 if (fstat(fileno(f), &sb) == -1) {
7747 err = got_error_from_errno2("fstat", path_unstaged_content);
7750 if (got_fileindex_entry_staged_filetype_get(ie) ==
7751 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7752 char link_target[PATH_MAX];
7754 r = fread(link_target, 1, sizeof(link_target), f);
7755 if (r == 0 && ferror(f)) {
7756 err = got_error_from_errno("fread");
7759 if (r >= sizeof(link_target)) { /* should not happen */
7760 err = got_error(GOT_ERR_NO_SPACE);
7763 link_target[r] = '\0';
7764 err = merge_symlink(worktree, blob_base,
7765 ondisk_path, ie->path, label_orig, link_target,
7766 worktree->base_commit_id, repo, progress_cb,
7769 int local_changes_subsumed;
7771 err = got_path_dirname(&parent, ondisk_path);
7775 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7777 err = got_error_from_errno("asprintf");
7782 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7785 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7790 err = merge_file(&local_changes_subsumed, worktree,
7791 f_base, ondisk_path, ie->path,
7792 got_fileindex_perms_to_st(ie),
7793 f, label_orig, "unstaged",
7794 repo, progress_cb, progress_arg);
7799 if (new_staged_blob_id) {
7800 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7801 SHA1_DIGEST_LENGTH);
7803 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7804 got_fileindex_entry_staged_filetype_set(ie, 0);
7807 free(new_staged_blob_id);
7808 if (path_unstaged_content &&
7809 unlink(path_unstaged_content) == -1 && err == NULL)
7810 err = got_error_from_errno2("unlink", path_unstaged_content);
7811 if (path_new_staged_content &&
7812 unlink(path_new_staged_content) == -1 && err == NULL)
7813 err = got_error_from_errno2("unlink", path_new_staged_content);
7814 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7815 err = got_error_from_errno2("unlink", blob_base_path);
7816 if (f_base && fclose(f_base) == EOF && err == NULL)
7817 err = got_error_from_errno2("fclose", path_unstaged_content);
7818 if (f && fclose(f) == EOF && err == NULL)
7819 err = got_error_from_errno2("fclose", path_unstaged_content);
7820 free(path_unstaged_content);
7821 free(path_new_staged_content);
7822 free(blob_base_path);
7828 static const struct got_error *
7829 unstage_path(void *arg, unsigned char status,
7830 unsigned char staged_status, const char *relpath,
7831 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7832 struct got_object_id *commit_id, int dirfd, const char *de_name)
7834 const struct got_error *err = NULL;
7835 struct unstage_path_arg *a = arg;
7836 struct got_fileindex_entry *ie;
7837 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7838 char *ondisk_path = NULL;
7839 char *id_str = NULL, *label_orig = NULL;
7840 int local_changes_subsumed;
7843 if (staged_status != GOT_STATUS_ADD &&
7844 staged_status != GOT_STATUS_MODIFY &&
7845 staged_status != GOT_STATUS_DELETE)
7848 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7850 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7852 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7854 return got_error_from_errno("asprintf");
7856 err = got_object_id_str(&id_str,
7857 commit_id ? commit_id : a->worktree->base_commit_id);
7860 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7862 err = got_error_from_errno("asprintf");
7866 switch (staged_status) {
7867 case GOT_STATUS_MODIFY:
7868 err = got_object_open_as_blob(&blob_base, a->repo,
7873 case GOT_STATUS_ADD:
7875 if (staged_status == GOT_STATUS_ADD) {
7876 int choice = GOT_PATCH_CHOICE_NONE;
7877 err = (*a->patch_cb)(&choice, a->patch_arg,
7878 staged_status, ie->path, NULL, 1, 1);
7881 if (choice != GOT_PATCH_CHOICE_YES)
7884 err = unstage_hunks(staged_blob_id,
7885 blob_base, blob_id, ie, ondisk_path,
7886 label_orig, a->worktree, a->repo,
7887 a->patch_cb, a->patch_arg,
7888 a->progress_cb, a->progress_arg);
7889 break; /* Done with this file. */
7892 err = got_object_open_as_blob(&blob_staged, a->repo,
7893 staged_blob_id, 8192);
7896 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7897 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7898 case GOT_FILEIDX_MODE_REGULAR_FILE:
7899 err = merge_blob(&local_changes_subsumed, a->worktree,
7900 blob_base, ondisk_path, relpath,
7901 got_fileindex_perms_to_st(ie), label_orig,
7902 blob_staged, commit_id ? commit_id :
7903 a->worktree->base_commit_id, a->repo,
7904 a->progress_cb, a->progress_arg);
7906 case GOT_FILEIDX_MODE_SYMLINK:
7907 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7908 char *staged_target;
7909 err = got_object_blob_read_to_str(
7910 &staged_target, blob_staged);
7913 err = merge_symlink(a->worktree, blob_base,
7914 ondisk_path, relpath, label_orig,
7915 staged_target, commit_id ? commit_id :
7916 a->worktree->base_commit_id,
7917 a->repo, a->progress_cb, a->progress_arg);
7918 free(staged_target);
7920 err = merge_blob(&local_changes_subsumed,
7921 a->worktree, blob_base, ondisk_path,
7922 relpath, got_fileindex_perms_to_st(ie),
7923 label_orig, blob_staged,
7924 commit_id ? commit_id :
7925 a->worktree->base_commit_id, a->repo,
7926 a->progress_cb, a->progress_arg);
7930 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7934 got_fileindex_entry_stage_set(ie,
7935 GOT_FILEIDX_STAGE_NONE);
7936 got_fileindex_entry_staged_filetype_set(ie, 0);
7939 case GOT_STATUS_DELETE:
7941 int choice = GOT_PATCH_CHOICE_NONE;
7942 err = (*a->patch_cb)(&choice, a->patch_arg,
7943 staged_status, ie->path, NULL, 1, 1);
7946 if (choice == GOT_PATCH_CHOICE_NO)
7948 if (choice != GOT_PATCH_CHOICE_YES) {
7949 err = got_error(GOT_ERR_PATCH_CHOICE);
7953 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7954 got_fileindex_entry_staged_filetype_set(ie, 0);
7955 err = get_file_status(&status, &sb, ie, ondisk_path,
7956 dirfd, de_name, a->repo);
7959 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7965 got_object_blob_close(blob_base);
7967 got_object_blob_close(blob_staged);
7973 const struct got_error *
7974 got_worktree_unstage(struct got_worktree *worktree,
7975 struct got_pathlist_head *paths,
7976 got_worktree_checkout_cb progress_cb, void *progress_arg,
7977 got_worktree_patch_cb patch_cb, void *patch_arg,
7978 struct got_repository *repo)
7980 const struct got_error *err = NULL, *sync_err, *unlockerr;
7981 struct got_pathlist_entry *pe;
7982 struct got_fileindex *fileindex = NULL;
7983 char *fileindex_path = NULL;
7984 struct unstage_path_arg upa;
7986 err = lock_worktree(worktree, LOCK_EX);
7990 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7994 upa.worktree = worktree;
7995 upa.fileindex = fileindex;
7997 upa.progress_cb = progress_cb;
7998 upa.progress_arg = progress_arg;
7999 upa.patch_cb = patch_cb;
8000 upa.patch_arg = patch_arg;
8001 TAILQ_FOREACH(pe, paths, entry) {
8002 err = worktree_status(worktree, pe->path, fileindex, repo,
8003 unstage_path, &upa, NULL, NULL, 0, 0);
8008 sync_err = sync_fileindex(fileindex, fileindex_path);
8009 if (sync_err && err == NULL)
8012 free(fileindex_path);
8014 got_fileindex_free(fileindex);
8015 unlockerr = lock_worktree(worktree, LOCK_SH);
8016 if (unlockerr && err == NULL)
8021 struct report_file_info_arg {
8022 struct got_worktree *worktree;
8023 got_worktree_path_info_cb info_cb;
8025 struct got_pathlist_head *paths;
8026 got_cancel_cb cancel_cb;
8030 static const struct got_error *
8031 report_file_info(void *arg, struct got_fileindex_entry *ie)
8033 struct report_file_info_arg *a = arg;
8034 struct got_pathlist_entry *pe;
8035 struct got_object_id blob_id, staged_blob_id, commit_id;
8036 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8037 struct got_object_id *commit_idp = NULL;
8040 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8041 return got_error(GOT_ERR_CANCELLED);
8043 TAILQ_FOREACH(pe, a->paths, entry) {
8044 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8045 got_path_is_child(ie->path, pe->path, pe->path_len))
8048 if (pe == NULL) /* not found */
8051 if (got_fileindex_entry_has_blob(ie)) {
8052 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8053 blob_idp = &blob_id;
8055 stage = got_fileindex_entry_stage_get(ie);
8056 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8057 stage == GOT_FILEIDX_STAGE_ADD) {
8058 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8059 SHA1_DIGEST_LENGTH);
8060 staged_blob_idp = &staged_blob_id;
8063 if (got_fileindex_entry_has_commit(ie)) {
8064 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8065 commit_idp = &commit_id;
8068 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8069 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8072 const struct got_error *
8073 got_worktree_path_info(struct got_worktree *worktree,
8074 struct got_pathlist_head *paths,
8075 got_worktree_path_info_cb info_cb, void *info_arg,
8076 got_cancel_cb cancel_cb, void *cancel_arg)
8079 const struct got_error *err = NULL, *unlockerr;
8080 struct got_fileindex *fileindex = NULL;
8081 char *fileindex_path = NULL;
8082 struct report_file_info_arg arg;
8084 err = lock_worktree(worktree, LOCK_SH);
8088 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8092 arg.worktree = worktree;
8093 arg.info_cb = info_cb;
8094 arg.info_arg = info_arg;
8096 arg.cancel_cb = cancel_cb;
8097 arg.cancel_arg = cancel_arg;
8098 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8101 free(fileindex_path);
8103 got_fileindex_free(fileindex);
8104 unlockerr = lock_worktree(worktree, LOCK_UN);
8105 if (unlockerr && err == NULL)