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"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
69 const struct got_error *err = NULL;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
83 const struct got_error *err = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 err = got_error_from_errno2("open", path);
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
170 (*content)[sb.st_size - 1] = '\0';
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
192 return got_error_from_errno("got_ref_to_str");
194 refstr = strdup(got_ref_get_name(head_ref));
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
210 uint32_t uuid_status;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
223 err = got_ref_resolve(&commit_id, repo, head_ref);
226 err = got_object_get_type(&obj_type, repo, commit_id);
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
421 got_repo_close(repo);
424 free(base_commit_id_str);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
434 (*worktree)->lockfd = fd;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
450 path = dirname(path);
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
535 err = write_head_ref(path_got, head_ref);
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
570 err = got_object_open(&obj, repo, commit_id);
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
595 got_object_close(obj);
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
641 size_t flen1 = 0, flen2 = 0;
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
660 } else if (flen2 == 0) {
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
684 FILE *f1 = NULL, *f2 = NULL;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
700 if (size1 != size2) {
705 f1 = fopen(f1_path, "r");
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
711 err = got_error_from_errno2("fopen", f2_path);
715 err = check_file_contents_equal(same, f1, f2);
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
745 char *symlink_path = NULL;
746 FILE *symlinkf = NULL;
748 *local_changes_subsumed = 0;
750 parent = dirname(ondisk_path);
752 return got_error_from_errno2("dirname", ondisk_path);
754 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
755 return got_error_from_errno("asprintf");
757 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
762 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
763 err = got_error_from_errno("asprintf");
768 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
772 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
778 * If the file has no blob, this is an "add vs add" conflict,
779 * and we simply use an empty ancestor file to make both files
780 * appear in the merged result in their entirety.
785 * In order the run a 3-way merge with a symlink we copy the symlink's
786 * target path into a temporary file and use that file with diff3.
788 if (S_ISLNK(st_mode)) {
789 char target_path[PATH_MAX];
794 if (asprintf(&base_path, "%s/got-symlink-merge",
796 err = got_error_from_errno("asprintf");
800 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
803 target_len = readlink(ondisk_path, target_path,
804 sizeof(target_path));
805 if (target_len == -1) {
806 err = got_error_from_errno2("readlink", ondisk_path);
809 n = fwrite(target_path, 1, target_len, symlinkf);
810 if (n != target_len) {
811 err = got_ferror(symlinkf, GOT_ERR_IO);
814 if (fflush(symlinkf) == EOF) {
815 err = got_error_from_errno2("fflush", symlink_path);
820 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
821 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
822 label_deriv, label_orig, NULL);
826 err = (*progress_cb)(progress_arg,
827 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
831 if (fsync(merged_fd) != 0) {
832 err = got_error_from_errno("fsync");
836 /* Check if a clean merge has subsumed all local changes. */
837 if (overlapcnt == 0) {
838 err = check_files_equal(local_changes_subsumed, deriv_path,
844 if (fchmod(merged_fd, st_mode) != 0) {
845 err = got_error_from_errno2("fchmod", merged_path);
849 if (rename(merged_path, ondisk_path) != 0) {
850 err = got_error_from_errno3("rename", merged_path,
860 if (unlink(symlink_path) == -1 && err == NULL)
861 err = got_error_from_errno2("unlink", symlink_path);
863 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
864 err = got_error_from_errno2("fclose", symlink_path);
866 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
867 err = got_error_from_errno("close");
868 if (f_orig && fclose(f_orig) != 0 && err == NULL)
869 err = got_error_from_errno("fclose");
872 if (blob_orig_path) {
873 unlink(blob_orig_path);
874 free(blob_orig_path);
879 static const struct got_error *
880 update_symlink(const char *ondisk_path, const char *target_path,
883 /* This is not atomic but matches what 'ln -sf' does. */
884 if (unlink(ondisk_path) == -1)
885 return got_error_from_errno2("unlink", ondisk_path);
886 if (symlink(target_path, ondisk_path) == -1)
887 return got_error_from_errno3("symlink", target_path,
893 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
894 * in the work tree with a file that contains conflict markers and the
895 * conflicting target paths of the original version, a "derived version"
896 * of a symlink from an incoming change, and a local version of the symlink.
898 * The original versions's target path can be NULL if it is not available,
899 * such as if both derived versions added a new symlink at the same path.
901 * The incoming derived symlink target is NULL in case the incoming change
902 * has deleted this symlink.
904 static const struct got_error *
905 install_symlink_conflict(const char *deriv_target,
906 struct got_object_id *deriv_base_commit_id, const char *orig_target,
907 const char *label_orig, const char *local_target, const char *ondisk_path)
909 const struct got_error *err;
910 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
913 err = got_object_id_str(&id_str, deriv_base_commit_id);
915 return got_error_from_errno("asprintf");
917 if (asprintf(&label_deriv, "%s: commit %s",
918 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
919 err = got_error_from_errno("asprintf");
923 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
927 if (fprintf(f, "%s: Could not install symbolic link because of merge "
928 "conflict.\nln(1) may be used to fix the situation. If this is "
929 "intended to be a\nregular file instead then its expected "
930 "contents may be filled in.\nThe following conflicting symlink "
931 "target paths were found:\n"
932 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
933 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
934 deriv_target ? deriv_target : "(symlink was deleted)",
935 orig_target ? label_orig : "",
936 orig_target ? "\n" : "",
937 orig_target ? orig_target : "",
938 orig_target ? "\n" : "",
939 GOT_DIFF_CONFLICT_MARKER_SEP,
940 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
941 err = got_error_from_errno2("fprintf", path);
945 if (unlink(ondisk_path) == -1) {
946 err = got_error_from_errno2("unlink", ondisk_path);
949 if (rename(path, ondisk_path) == -1) {
950 err = got_error_from_errno3("rename", path, ondisk_path);
953 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
954 err = got_error_from_errno2("chmod", ondisk_path);
958 if (f != NULL && fclose(f) == EOF && err == NULL)
959 err = got_error_from_errno2("fclose", path);
966 /* forward declaration */
967 static const struct got_error *
968 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
969 const char *, const char *, uint16_t, const char *,
970 struct got_blob_object *, struct got_object_id *,
971 struct got_repository *, got_worktree_checkout_cb, void *);
974 * Merge a symlink into the work tree, where blob_orig acts as the common
975 * ancestor, blob_deriv acts as the first derived version, and the symlink
976 * on disk acts as the second derived version.
977 * Assume that contents of both blobs represent symlinks.
979 static const struct got_error *
980 merge_symlink(struct got_worktree *worktree,
981 struct got_blob_object *blob_orig, const char *ondisk_path,
982 const char *path, uint16_t st_mode, const char *label_orig,
983 struct got_blob_object *blob_deriv,
984 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
985 got_worktree_checkout_cb progress_cb, void *progress_arg)
987 const struct got_error *err = NULL;
988 char *ancestor_target = NULL, *deriv_target = NULL;
990 ssize_t ondisk_len, deriv_len;
991 char ondisk_target[PATH_MAX];
992 int have_local_change = 0;
993 int have_incoming_change = 0;
995 if (lstat(ondisk_path, &sb) == -1)
996 return got_error_from_errno2("lstat", ondisk_path);
998 if (!S_ISLNK(sb.st_mode)) {
1000 * If there is a regular file on disk, merge the symlink
1001 * target path into this file, which will usually cause
1004 if (S_ISREG(sb.st_mode)) {
1005 int local_changes_subsumed;
1006 return merge_blob(&local_changes_subsumed, worktree,
1007 NULL, ondisk_path, path, sb.st_mode, label_orig,
1008 blob_deriv, deriv_base_commit_id,
1009 repo, progress_cb, progress_arg);
1012 /* TODO symlink is obstructed; do something */
1013 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
1016 ondisk_len = readlink(ondisk_path, ondisk_target,
1017 sizeof(ondisk_target));
1018 if (ondisk_len == -1) {
1019 err = got_error_from_errno2("readlink",
1023 ondisk_target[ondisk_len] = '\0';
1026 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1031 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
1035 if (ancestor_target == NULL ||
1036 (ondisk_len != strlen(ancestor_target) ||
1037 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1038 have_local_change = 1;
1040 deriv_len = strlen(deriv_target);
1041 if (ancestor_target == NULL ||
1042 (deriv_len != strlen(ancestor_target) ||
1043 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1044 have_incoming_change = 1;
1046 if (!have_local_change && !have_incoming_change) {
1047 if (ancestor_target) {
1048 /* Both sides made the same change. */
1049 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1051 } else if (deriv_len == ondisk_len &&
1052 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1053 /* Both sides added the same symlink. */
1054 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1057 /* Both sides added symlinks which don't match. */
1058 err = install_symlink_conflict(deriv_target,
1059 deriv_base_commit_id, ancestor_target,
1060 label_orig, ondisk_target, ondisk_path);
1063 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1066 } else if (!have_local_change && have_incoming_change) {
1067 /* Apply the incoming change. */
1068 err = update_symlink(ondisk_path, deriv_target,
1069 strlen(deriv_target));
1072 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1073 } else if (have_local_change && have_incoming_change) {
1074 if (deriv_len == ondisk_len &&
1075 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1076 /* Both sides made the same change. */
1077 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1080 err = install_symlink_conflict(deriv_target,
1081 deriv_base_commit_id, ancestor_target, label_orig,
1082 ondisk_target, ondisk_path);
1085 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1091 free(ancestor_target);
1097 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1098 * blob_deriv acts as the first derived version, and the file on disk
1099 * acts as the second derived version.
1101 static const struct got_error *
1102 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1103 struct got_blob_object *blob_orig, const char *ondisk_path,
1104 const char *path, uint16_t st_mode, const char *label_orig,
1105 struct got_blob_object *blob_deriv,
1106 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1107 got_worktree_checkout_cb progress_cb, void *progress_arg)
1109 const struct got_error *err = NULL;
1110 FILE *f_deriv = NULL;
1111 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1112 char *label_deriv = NULL, *parent;
1114 *local_changes_subsumed = 0;
1116 parent = dirname(ondisk_path);
1118 return got_error_from_errno2("dirname", ondisk_path);
1121 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1122 err = got_error_from_errno("asprintf");
1127 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1130 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1135 err = got_object_id_str(&id_str, deriv_base_commit_id);
1138 if (asprintf(&label_deriv, "%s: commit %s",
1139 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1140 err = got_error_from_errno("asprintf");
1144 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1145 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1146 label_deriv, repo, progress_cb, progress_arg);
1148 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1149 err = got_error_from_errno("fclose");
1151 if (blob_deriv_path) {
1152 unlink(blob_deriv_path);
1153 free(blob_deriv_path);
1160 static const struct got_error *
1161 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1162 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1163 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1165 const struct got_error *err = NULL;
1166 struct got_fileindex_entry *new_ie;
1170 err = got_fileindex_entry_alloc(&new_ie, path);
1174 err = got_fileindex_entry_update(new_ie, ondisk_path,
1175 blob_id->sha1, base_commit_id->sha1, 1);
1179 err = got_fileindex_entry_add(fileindex, new_ie);
1182 got_fileindex_entry_free(new_ie);
1189 get_ondisk_perms(int executable, mode_t st_mode)
1191 mode_t xbits = S_IXUSR;
1194 /* Map read bits to execute bits. */
1195 if (st_mode & S_IRGRP)
1197 if (st_mode & S_IROTH)
1199 return st_mode | xbits;
1202 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1205 /* forward declaration */
1206 static const struct got_error *
1207 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1208 const char *path, mode_t te_mode, mode_t st_mode,
1209 struct got_blob_object *blob, int restoring_missing_file,
1210 int reverting_versioned_file, int installing_bad_symlink,
1211 int path_is_unversioned, struct got_repository *repo,
1212 got_worktree_checkout_cb progress_cb, void *progress_arg);
1215 * This function assumes that the provided symlink target points at a
1216 * safe location in the work tree!
1218 static const struct got_error *
1219 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1222 const struct got_error *err = NULL;
1224 char etarget[PATH_MAX];
1228 * "Bad" symlinks (those pointing outside the work tree or into the
1229 * .got directory) are installed in the work tree as a regular file
1230 * which contains the bad symlink target path.
1231 * The new symlink target has already been checked for safety by our
1232 * caller. If we can successfully open a regular file then we simply
1233 * replace this file with a symlink below.
1235 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1238 return got_error_from_errno2("open", ondisk_path);
1240 /* We are updating an existing on-disk symlink. */
1241 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1243 return got_error_from_errno2("readlink", ondisk_path);
1245 if (elen == target_len &&
1246 memcmp(etarget, target_path, target_len) == 0)
1247 return NULL; /* nothing to do */
1250 err = update_symlink(ondisk_path, target_path, target_len);
1251 if (fd != -1 && close(fd) == -1 && err == NULL)
1252 err = got_error_from_errno2("close", ondisk_path);
1256 static const struct got_error *
1257 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1258 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1259 int restoring_missing_file, int reverting_versioned_file,
1260 int path_is_unversioned, struct got_repository *repo,
1261 got_worktree_checkout_cb progress_cb, void *progress_arg)
1263 const struct got_error *err = NULL;
1264 char target_path[PATH_MAX];
1265 char canonpath[PATH_MAX];
1266 size_t len, target_len = 0;
1267 char *path_got = NULL;
1268 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1269 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1271 *is_bad_symlink = 0;
1274 * Blob object content specifies the target path of the link.
1275 * If a symbolic link cannot be installed we instead create
1276 * a regular file which contains the link target path stored
1277 * in the blob object.
1280 err = got_object_blob_read_block(&len, blob);
1281 if (len + target_len >= sizeof(target_path)) {
1282 /* Path too long; install as a regular file. */
1283 *is_bad_symlink = 1;
1284 got_object_blob_rewind(blob);
1285 return install_blob(worktree, ondisk_path, path,
1286 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1287 restoring_missing_file, reverting_versioned_file,
1288 1, path_is_unversioned, repo, progress_cb,
1292 /* Skip blob object header first time around. */
1293 memcpy(target_path + target_len, buf + hdrlen,
1295 target_len += len - hdrlen;
1299 target_path[target_len] = '\0';
1302 * We do not use realpath(3) to resolve the symlink's target path
1303 * because we don't want to resolve symlinks recursively. Instead
1304 * we make the path absolute and then canonicalize it.
1305 * Relative symlink target lookup should begin at the directory
1306 * in which the blob object is being installed.
1308 if (!got_path_is_absolute(target_path)) {
1310 char *parent = dirname(ondisk_path);
1311 if (parent == NULL) {
1312 err = got_error_from_errno2("dirname", ondisk_path);
1315 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1316 err = got_error_from_errno("asprintf");
1319 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1324 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1329 /* Only allow symlinks pointing at paths within the work tree. */
1330 if (!got_path_is_child(canonpath, worktree->root_path,
1331 strlen(worktree->root_path))) {
1332 /* install as a regular file */
1333 *is_bad_symlink = 1;
1334 got_object_blob_rewind(blob);
1335 err = install_blob(worktree, ondisk_path, path,
1336 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1337 restoring_missing_file, reverting_versioned_file, 1,
1338 path_is_unversioned, repo, progress_cb, progress_arg);
1342 /* Do not allow symlinks pointing into the .got directory. */
1343 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1344 GOT_WORKTREE_GOT_DIR) == -1) {
1345 err = got_error_from_errno("asprintf");
1348 if (got_path_is_child(canonpath, path_got, strlen(path_got))) {
1349 /* install as a regular file */
1350 *is_bad_symlink = 1;
1351 got_object_blob_rewind(blob);
1352 err = install_blob(worktree, ondisk_path, path,
1353 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1354 restoring_missing_file, reverting_versioned_file, 1,
1355 path_is_unversioned, repo, progress_cb, progress_arg);
1359 if (symlink(target_path, ondisk_path) == -1) {
1360 if (errno == EEXIST) {
1361 if (path_is_unversioned) {
1362 err = (*progress_cb)(progress_arg,
1363 GOT_STATUS_UNVERSIONED, path);
1366 err = replace_existing_symlink(ondisk_path,
1367 target_path, target_len);
1371 err = (*progress_cb)(progress_arg,
1372 reverting_versioned_file ?
1373 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1376 goto done; /* Nothing else to do. */
1379 if (errno == ENOENT) {
1380 char *parent = dirname(ondisk_path);
1381 if (parent == NULL) {
1382 err = got_error_from_errno2("dirname",
1386 err = add_dir_on_disk(worktree, parent);
1390 * Retry, and fall through to error handling
1391 * below if this second attempt fails.
1393 if (symlink(target_path, ondisk_path) != -1) {
1394 err = NULL; /* success */
1399 /* Handle errors from first or second creation attempt. */
1400 if (errno == ENAMETOOLONG) {
1401 /* bad target path; install as a regular file */
1402 *is_bad_symlink = 1;
1403 got_object_blob_rewind(blob);
1404 err = install_blob(worktree, ondisk_path, path,
1405 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1406 restoring_missing_file, reverting_versioned_file, 1,
1407 path_is_unversioned, repo,
1408 progress_cb, progress_arg);
1409 } else if (errno == ENOTDIR) {
1410 err = got_error_path(ondisk_path,
1411 GOT_ERR_FILE_OBSTRUCTED);
1413 err = got_error_from_errno3("symlink",
1414 target_path, ondisk_path);
1416 } else if (progress_cb)
1417 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1418 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1424 static const struct got_error *
1425 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1426 const char *path, mode_t te_mode, mode_t st_mode,
1427 struct got_blob_object *blob, int restoring_missing_file,
1428 int reverting_versioned_file, int installing_bad_symlink,
1429 int path_is_unversioned, struct got_repository *repo,
1430 got_worktree_checkout_cb progress_cb, void *progress_arg)
1432 const struct got_error *err = NULL;
1436 char *tmppath = NULL;
1438 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1439 GOT_DEFAULT_FILE_MODE);
1441 if (errno == ENOENT) {
1442 char *parent = dirname(path);
1444 return got_error_from_errno2("dirname", path);
1445 err = add_dir_on_disk(worktree, parent);
1448 fd = open(ondisk_path,
1449 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1450 GOT_DEFAULT_FILE_MODE);
1452 return got_error_from_errno2("open",
1454 } else if (errno == EEXIST) {
1455 if (path_is_unversioned) {
1456 err = (*progress_cb)(progress_arg,
1457 GOT_STATUS_UNVERSIONED, path);
1460 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1461 /* TODO file is obstructed; do something */
1462 err = got_error_path(ondisk_path,
1463 GOT_ERR_FILE_OBSTRUCTED);
1466 err = got_opentemp_named_fd(&tmppath, &fd,
1473 return got_error_from_errno2("open", ondisk_path);
1477 if (restoring_missing_file)
1478 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1480 else if (reverting_versioned_file)
1481 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1484 err = (*progress_cb)(progress_arg,
1485 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1490 hdrlen = got_object_blob_get_hdrlen(blob);
1492 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1493 err = got_object_blob_read_block(&len, blob);
1497 /* Skip blob object header first time around. */
1498 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1500 err = got_error_from_errno("write");
1502 } else if (outlen != len - hdrlen) {
1503 err = got_error(GOT_ERR_IO);
1510 if (fsync(fd) != 0) {
1511 err = got_error_from_errno("fsync");
1516 if (rename(tmppath, ondisk_path) != 0) {
1517 err = got_error_from_errno3("rename", tmppath,
1524 if (chmod(ondisk_path,
1525 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1526 err = got_error_from_errno2("chmod", ondisk_path);
1531 if (fd != -1 && close(fd) != 0 && err == NULL)
1532 err = got_error_from_errno("close");
1537 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1538 static const struct got_error *
1539 get_modified_file_content_status(unsigned char *status, FILE *f)
1541 const struct got_error *err = NULL;
1542 const char *markers[3] = {
1543 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1544 GOT_DIFF_CONFLICT_MARKER_SEP,
1545 GOT_DIFF_CONFLICT_MARKER_END
1550 const char delim[3] = {'\0', '\0', '\0'};
1552 while (*status == GOT_STATUS_MODIFY) {
1553 line = fparseln(f, &len, NULL, delim, 0);
1557 err = got_ferror(f, GOT_ERR_IO);
1561 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1562 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1564 *status = GOT_STATUS_CONFLICT;
1574 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1576 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1577 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1581 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1583 return !(ie->ctime_sec == sb->st_ctime &&
1584 ie->ctime_nsec == sb->st_ctimensec &&
1585 ie->mtime_sec == sb->st_mtime &&
1586 ie->mtime_nsec == sb->st_mtimensec &&
1587 ie->size == (sb->st_size & 0xffffffff) &&
1588 !xbit_differs(ie, sb->st_mode));
1591 static unsigned char
1592 get_staged_status(struct got_fileindex_entry *ie)
1594 switch (got_fileindex_entry_stage_get(ie)) {
1595 case GOT_FILEIDX_STAGE_ADD:
1596 return GOT_STATUS_ADD;
1597 case GOT_FILEIDX_STAGE_DELETE:
1598 return GOT_STATUS_DELETE;
1599 case GOT_FILEIDX_STAGE_MODIFY:
1600 return GOT_STATUS_MODIFY;
1602 return GOT_STATUS_NO_CHANGE;
1606 static const struct got_error *
1607 get_symlink_status(unsigned char *status, struct stat *sb,
1608 struct got_fileindex_entry *ie, const char *abspath,
1609 int dirfd, const char *de_name, struct got_blob_object *blob)
1611 const struct got_error *err = NULL;
1612 char target_path[PATH_MAX];
1613 char etarget[PATH_MAX];
1615 size_t len, target_len = 0;
1616 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1617 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1619 *status = GOT_STATUS_NO_CHANGE;
1621 /* Blob object content specifies the target path of the link. */
1623 err = got_object_blob_read_block(&len, blob);
1626 if (len + target_len >= sizeof(target_path)) {
1628 * Should not happen. The blob contents were OK
1629 * when this symlink was installed.
1631 return got_error(GOT_ERR_NO_SPACE);
1634 /* Skip blob object header first time around. */
1635 memcpy(target_path + target_len, buf + hdrlen,
1637 target_len += len - hdrlen;
1641 target_path[target_len] = '\0';
1644 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1646 return got_error_from_errno2("readlinkat", abspath);
1648 elen = readlink(abspath, etarget, sizeof(etarget));
1650 return got_error_from_errno2("readlink", abspath);
1653 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1654 *status = GOT_STATUS_MODIFY;
1659 static const struct got_error *
1660 get_file_status(unsigned char *status, struct stat *sb,
1661 struct got_fileindex_entry *ie, const char *abspath,
1662 int dirfd, const char *de_name, struct got_repository *repo)
1664 const struct got_error *err = NULL;
1665 struct got_object_id id;
1670 struct got_blob_object *blob = NULL;
1672 unsigned char staged_status = get_staged_status(ie);
1674 *status = GOT_STATUS_NO_CHANGE;
1677 * Whenever the caller provides a directory descriptor and a
1678 * directory entry name for the file, use them! This prevents
1679 * race conditions if filesystem paths change beneath our feet.
1682 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1683 if (errno == ENOENT) {
1684 if (got_fileindex_entry_has_file_on_disk(ie))
1685 *status = GOT_STATUS_MISSING;
1687 *status = GOT_STATUS_DELETE;
1690 err = got_error_from_errno2("fstatat", abspath);
1694 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1695 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1696 return got_error_from_errno2("open", abspath);
1697 else if (fd == -1 && errno == ELOOP) {
1698 if (lstat(abspath, sb) == -1)
1699 return got_error_from_errno2("lstat", abspath);
1700 } else if (fd == -1 || fstat(fd, sb) == -1) {
1701 if (errno == ENOENT) {
1702 if (got_fileindex_entry_has_file_on_disk(ie))
1703 *status = GOT_STATUS_MISSING;
1705 *status = GOT_STATUS_DELETE;
1708 err = got_error_from_errno2("fstat", abspath);
1713 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1714 *status = GOT_STATUS_OBSTRUCTED;
1718 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1719 *status = GOT_STATUS_DELETE;
1721 } else if (!got_fileindex_entry_has_blob(ie) &&
1722 staged_status != GOT_STATUS_ADD) {
1723 *status = GOT_STATUS_ADD;
1727 if (!stat_info_differs(ie, sb))
1730 if (staged_status == GOT_STATUS_MODIFY ||
1731 staged_status == GOT_STATUS_ADD)
1732 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1734 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1736 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1740 if (S_ISLNK(sb->st_mode)) {
1741 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1748 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1750 err = got_error_from_errno2("openat", abspath);
1755 f = fdopen(fd, "r");
1757 err = got_error_from_errno2("fdopen", abspath);
1761 hdrlen = got_object_blob_get_hdrlen(blob);
1763 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1764 err = got_object_blob_read_block(&blen, blob);
1767 /* Skip length of blob object header first time around. */
1768 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1769 if (flen == 0 && ferror(f)) {
1770 err = got_error_from_errno("fread");
1775 *status = GOT_STATUS_MODIFY;
1777 } else if (flen == 0) {
1779 *status = GOT_STATUS_MODIFY;
1781 } else if (blen - hdrlen == flen) {
1782 /* Skip blob object header first time around. */
1783 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1784 *status = GOT_STATUS_MODIFY;
1788 *status = GOT_STATUS_MODIFY;
1794 if (*status == GOT_STATUS_MODIFY) {
1796 err = get_modified_file_content_status(status, f);
1797 } else if (xbit_differs(ie, sb->st_mode))
1798 *status = GOT_STATUS_MODE_CHANGE;
1801 got_object_blob_close(blob);
1802 if (f != NULL && fclose(f) == EOF && err == NULL)
1803 err = got_error_from_errno2("fclose", abspath);
1804 if (fd != -1 && close(fd) == -1 && err == NULL)
1805 err = got_error_from_errno2("close", abspath);
1810 * Update timestamps in the file index if a file is unmodified and
1811 * we had to run a full content comparison to find out.
1813 static const struct got_error *
1814 sync_timestamps(char *ondisk_path, unsigned char status,
1815 struct got_fileindex_entry *ie, struct stat *sb)
1817 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1818 return got_fileindex_entry_update(ie, ondisk_path,
1819 ie->blob_sha1, ie->commit_sha1, 1);
1824 static const struct got_error *
1825 update_blob(struct got_worktree *worktree,
1826 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1827 struct got_tree_entry *te, const char *path,
1828 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1831 const struct got_error *err = NULL;
1832 struct got_blob_object *blob = NULL;
1834 unsigned char status = GOT_STATUS_NO_CHANGE;
1837 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1838 return got_error_from_errno("asprintf");
1841 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1842 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1845 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1849 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1850 sb.st_mode = got_fileindex_perms_to_st(ie);
1852 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1853 status = GOT_STATUS_UNVERSIONED;
1856 if (status == GOT_STATUS_OBSTRUCTED) {
1857 err = (*progress_cb)(progress_arg, status, path);
1860 if (status == GOT_STATUS_CONFLICT) {
1861 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1866 if (ie && status != GOT_STATUS_MISSING &&
1867 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1868 if (got_fileindex_entry_has_commit(ie) &&
1869 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1870 SHA1_DIGEST_LENGTH) == 0) {
1871 err = sync_timestamps(ondisk_path, status, ie, &sb);
1874 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1878 if (got_fileindex_entry_has_blob(ie) &&
1879 memcmp(ie->blob_sha1, te->id.sha1,
1880 SHA1_DIGEST_LENGTH) == 0) {
1881 err = sync_timestamps(ondisk_path, status, ie, &sb);
1886 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 int update_timestamps;
1892 struct got_blob_object *blob2 = NULL;
1893 char *label_orig = NULL;
1894 if (got_fileindex_entry_has_blob(ie)) {
1895 struct got_object_id id2;
1896 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 if (got_fileindex_entry_has_commit(ie)) {
1902 char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 sizeof(id_str)) == NULL) {
1905 err = got_error_path(id_str,
1906 GOT_ERR_BAD_OBJ_ID_STR);
1909 if (asprintf(&label_orig, "%s: commit %s",
1910 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 err = got_error_from_errno("asprintf");
1915 if (S_ISLNK(te->mode)) {
1916 err = merge_symlink(worktree, blob2,
1917 ondisk_path, path, sb.st_mode, label_orig,
1918 blob, worktree->base_commit_id, repo,
1919 progress_cb, progress_arg);
1921 err = merge_blob(&update_timestamps, worktree, blob2,
1922 ondisk_path, path, sb.st_mode, label_orig, blob,
1923 worktree->base_commit_id, repo,
1924 progress_cb, progress_arg);
1928 got_object_blob_close(blob2);
1932 * Do not update timestamps of files with local changes.
1933 * Otherwise, a future status walk would treat them as
1934 * unmodified files again.
1936 err = got_fileindex_entry_update(ie, ondisk_path,
1937 blob->id.sha1, worktree->base_commit_id->sha1,
1939 } else if (status == GOT_STATUS_MODE_CHANGE) {
1940 err = got_fileindex_entry_update(ie, ondisk_path,
1941 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1942 } else if (status == GOT_STATUS_DELETE) {
1943 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1946 err = got_fileindex_entry_update(ie, ondisk_path,
1947 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1951 int is_bad_symlink = 0;
1952 if (S_ISLNK(te->mode)) {
1953 err = install_symlink(&is_bad_symlink, worktree,
1954 ondisk_path, path, blob,
1955 status == GOT_STATUS_MISSING, 0,
1956 status == GOT_STATUS_UNVERSIONED, repo,
1957 progress_cb, progress_arg);
1959 err = install_blob(worktree, ondisk_path, path,
1960 te->mode, sb.st_mode, blob,
1961 status == GOT_STATUS_MISSING, 0, 0,
1962 status == GOT_STATUS_UNVERSIONED, repo,
1963 progress_cb, progress_arg);
1969 err = got_fileindex_entry_update(ie, ondisk_path,
1970 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1972 err = create_fileindex_entry(&ie, fileindex,
1973 worktree->base_commit_id, ondisk_path, path,
1979 if (is_bad_symlink) {
1980 got_fileindex_entry_filetype_set(ie,
1981 GOT_FILEIDX_MODE_BAD_SYMLINK);
1984 got_object_blob_close(blob);
1990 static const struct got_error *
1991 remove_ondisk_file(const char *root_path, const char *path)
1993 const struct got_error *err = NULL;
1994 char *ondisk_path = NULL;
1996 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1997 return got_error_from_errno("asprintf");
1999 if (unlink(ondisk_path) == -1) {
2000 if (errno != ENOENT)
2001 err = got_error_from_errno2("unlink", ondisk_path);
2003 char *parent = dirname(ondisk_path);
2004 while (parent && strcmp(parent, root_path) != 0) {
2005 if (rmdir(parent) == -1) {
2006 if (errno != ENOTEMPTY)
2007 err = got_error_from_errno2("rmdir",
2011 parent = dirname(parent);
2018 static const struct got_error *
2019 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2020 struct got_fileindex_entry *ie, struct got_repository *repo,
2021 got_worktree_checkout_cb progress_cb, void *progress_arg)
2023 const struct got_error *err = NULL;
2024 unsigned char status;
2028 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2029 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2031 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2033 return got_error_from_errno("asprintf");
2035 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2039 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2040 char ondisk_target[PATH_MAX];
2041 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2042 sizeof(ondisk_target));
2043 if (ondisk_len == -1) {
2044 err = got_error_from_errno2("readlink", ondisk_path);
2047 ondisk_target[ondisk_len] = '\0';
2048 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2049 NULL, NULL, /* XXX pass common ancestor info? */
2050 ondisk_target, ondisk_path);
2053 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2058 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2059 status == GOT_STATUS_ADD) {
2060 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2064 * Preserve the working file and change the deleted blob's
2065 * entry into a schedule-add entry.
2067 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2070 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2073 if (status == GOT_STATUS_NO_CHANGE) {
2074 err = remove_ondisk_file(worktree->root_path, ie->path);
2078 got_fileindex_entry_remove(fileindex, ie);
2085 struct diff_cb_arg {
2086 struct got_fileindex *fileindex;
2087 struct got_worktree *worktree;
2088 struct got_repository *repo;
2089 got_worktree_checkout_cb progress_cb;
2091 got_cancel_cb cancel_cb;
2095 static const struct got_error *
2096 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2097 struct got_tree_entry *te, const char *parent_path)
2099 struct diff_cb_arg *a = arg;
2101 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2102 return got_error(GOT_ERR_CANCELLED);
2104 return update_blob(a->worktree, a->fileindex, ie, te,
2105 ie->path, a->repo, a->progress_cb, a->progress_arg);
2108 static const struct got_error *
2109 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2111 struct diff_cb_arg *a = arg;
2113 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2114 return got_error(GOT_ERR_CANCELLED);
2116 return delete_blob(a->worktree, a->fileindex, ie,
2117 a->repo, a->progress_cb, a->progress_arg);
2120 static const struct got_error *
2121 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2123 struct diff_cb_arg *a = arg;
2124 const struct got_error *err;
2127 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2128 return got_error(GOT_ERR_CANCELLED);
2130 if (got_object_tree_entry_is_submodule(te))
2133 if (asprintf(&path, "%s%s%s", parent_path,
2134 parent_path[0] ? "/" : "", te->name)
2136 return got_error_from_errno("asprintf");
2138 if (S_ISDIR(te->mode))
2139 err = add_dir_on_disk(a->worktree, path);
2141 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2142 a->repo, a->progress_cb, a->progress_arg);
2148 static const struct got_error *
2149 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2151 const struct got_error *err = NULL;
2152 char *uuidstr = NULL;
2153 uint32_t uuid_status;
2157 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2158 if (uuid_status != uuid_s_ok)
2159 return got_error_uuid(uuid_status, "uuid_to_string");
2161 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2163 err = got_error_from_errno("asprintf");
2170 const struct got_error *
2171 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2173 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2176 static const struct got_error *
2177 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2179 return get_ref_name(refname, worktree,
2180 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2183 static const struct got_error *
2184 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2186 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2189 static const struct got_error *
2190 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2192 return get_ref_name(refname, worktree,
2193 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2196 static const struct got_error *
2197 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2199 return get_ref_name(refname, worktree,
2200 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2203 static const struct got_error *
2204 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2206 return get_ref_name(refname, worktree,
2207 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2210 static const struct got_error *
2211 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2213 return get_ref_name(refname, worktree,
2214 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2217 static const struct got_error *
2218 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2220 return get_ref_name(refname, worktree,
2221 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2224 static const struct got_error *
2225 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2227 return get_ref_name(refname, worktree,
2228 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2231 const struct got_error *
2232 got_worktree_get_histedit_script_path(char **path,
2233 struct got_worktree *worktree)
2235 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2236 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2238 return got_error_from_errno("asprintf");
2244 * Prevent Git's garbage collector from deleting our base commit by
2245 * setting a reference to our base commit's ID.
2247 static const struct got_error *
2248 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2250 const struct got_error *err = NULL;
2251 struct got_reference *ref = NULL;
2254 err = got_worktree_get_base_ref_name(&refname, worktree);
2258 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2262 err = got_ref_write(ref, repo);
2270 static const struct got_error *
2271 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2273 const struct got_error *err = NULL;
2275 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2276 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2277 err = got_error_from_errno("asprintf");
2278 *fileindex_path = NULL;
2284 static const struct got_error *
2285 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2286 struct got_worktree *worktree)
2288 const struct got_error *err = NULL;
2291 *fileindex_path = NULL;
2292 *fileindex = got_fileindex_alloc();
2293 if (*fileindex == NULL)
2294 return got_error_from_errno("got_fileindex_alloc");
2296 err = get_fileindex_path(fileindex_path, worktree);
2300 index = fopen(*fileindex_path, "rb");
2301 if (index == NULL) {
2302 if (errno != ENOENT)
2303 err = got_error_from_errno2("fopen", *fileindex_path);
2305 err = got_fileindex_read(*fileindex, index);
2306 if (fclose(index) != 0 && err == NULL)
2307 err = got_error_from_errno("fclose");
2311 free(*fileindex_path);
2312 *fileindex_path = NULL;
2313 got_fileindex_free(*fileindex);
2319 struct bump_base_commit_id_arg {
2320 struct got_object_id *base_commit_id;
2323 const char *entry_name;
2324 got_worktree_checkout_cb progress_cb;
2328 /* Bump base commit ID of all files within an updated part of the work tree. */
2329 static const struct got_error *
2330 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2332 const struct got_error *err;
2333 struct bump_base_commit_id_arg *a = arg;
2335 if (a->entry_name) {
2336 if (strcmp(ie->path, a->path) != 0)
2338 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2341 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2342 SHA1_DIGEST_LENGTH) == 0)
2345 if (a->progress_cb) {
2346 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2351 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2355 static const struct got_error *
2356 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2358 const struct got_error *err = NULL;
2359 char *new_fileindex_path = NULL;
2360 FILE *new_index = NULL;
2361 struct timespec timeout;
2363 err = got_opentemp_named(&new_fileindex_path, &new_index,
2368 err = got_fileindex_write(fileindex, new_index);
2372 if (rename(new_fileindex_path, fileindex_path) != 0) {
2373 err = got_error_from_errno3("rename", new_fileindex_path,
2375 unlink(new_fileindex_path);
2379 * Sleep for a short amount of time to ensure that files modified after
2380 * this program exits have a different time stamp from the one which
2381 * was recorded in the file index.
2384 timeout.tv_nsec = 1;
2385 nanosleep(&timeout, NULL);
2389 free(new_fileindex_path);
2393 static const struct got_error *
2394 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2395 struct got_object_id **tree_id, const char *wt_relpath,
2396 struct got_worktree *worktree, struct got_repository *repo)
2398 const struct got_error *err = NULL;
2399 struct got_object_id *id = NULL;
2400 char *in_repo_path = NULL;
2401 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2403 *entry_type = GOT_OBJ_TYPE_ANY;
2404 *tree_relpath = NULL;
2407 if (wt_relpath[0] == '\0') {
2408 /* Check out all files within the work tree. */
2409 *entry_type = GOT_OBJ_TYPE_TREE;
2410 *tree_relpath = strdup("");
2411 if (*tree_relpath == NULL) {
2412 err = got_error_from_errno("strdup");
2415 err = got_object_id_by_path(tree_id, repo,
2416 worktree->base_commit_id, worktree->path_prefix);
2422 /* Check out a subset of files in the work tree. */
2424 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2425 is_root_wt ? "" : "/", wt_relpath) == -1) {
2426 err = got_error_from_errno("asprintf");
2430 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2436 in_repo_path = NULL;
2438 err = got_object_get_type(entry_type, repo, id);
2442 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2443 /* Check out a single file. */
2444 if (strchr(wt_relpath, '/') == NULL) {
2445 /* Check out a single file in work tree's root dir. */
2446 in_repo_path = strdup(worktree->path_prefix);
2447 if (in_repo_path == NULL) {
2448 err = got_error_from_errno("strdup");
2451 *tree_relpath = strdup("");
2452 if (*tree_relpath == NULL) {
2453 err = got_error_from_errno("strdup");
2457 /* Check out a single file in a subdirectory. */
2458 err = got_path_dirname(tree_relpath, wt_relpath);
2461 if (asprintf(&in_repo_path, "%s%s%s",
2462 worktree->path_prefix, is_root_wt ? "" : "/",
2463 *tree_relpath) == -1) {
2464 err = got_error_from_errno("asprintf");
2468 err = got_object_id_by_path(tree_id, repo,
2469 worktree->base_commit_id, in_repo_path);
2471 /* Check out all files within a subdirectory. */
2472 *tree_id = got_object_id_dup(id);
2473 if (*tree_id == NULL) {
2474 err = got_error_from_errno("got_object_id_dup");
2477 *tree_relpath = strdup(wt_relpath);
2478 if (*tree_relpath == NULL) {
2479 err = got_error_from_errno("strdup");
2487 *entry_type = GOT_OBJ_TYPE_ANY;
2488 free(*tree_relpath);
2489 *tree_relpath = NULL;
2496 static const struct got_error *
2497 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2498 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2499 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2500 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2502 const struct got_error *err = NULL;
2503 struct got_commit_object *commit = NULL;
2504 struct got_tree_object *tree = NULL;
2505 struct got_fileindex_diff_tree_cb diff_cb;
2506 struct diff_cb_arg arg;
2508 err = ref_base_commit(worktree, repo);
2510 if (!(err->code == GOT_ERR_ERRNO &&
2511 (errno == EACCES || errno == EROFS)))
2513 err = (*progress_cb)(progress_arg,
2514 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2519 err = got_object_open_as_commit(&commit, repo,
2520 worktree->base_commit_id);
2524 err = got_object_open_as_tree(&tree, repo, tree_id);
2529 got_object_tree_find_entry(tree, entry_name) == NULL) {
2530 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2534 diff_cb.diff_old_new = diff_old_new;
2535 diff_cb.diff_old = diff_old;
2536 diff_cb.diff_new = diff_new;
2537 arg.fileindex = fileindex;
2538 arg.worktree = worktree;
2540 arg.progress_cb = progress_cb;
2541 arg.progress_arg = progress_arg;
2542 arg.cancel_cb = cancel_cb;
2543 arg.cancel_arg = cancel_arg;
2544 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2545 entry_name, repo, &diff_cb, &arg);
2548 got_object_tree_close(tree);
2550 got_object_commit_close(commit);
2554 const struct got_error *
2555 got_worktree_checkout_files(struct got_worktree *worktree,
2556 struct got_pathlist_head *paths, struct got_repository *repo,
2557 got_worktree_checkout_cb progress_cb, void *progress_arg,
2558 got_cancel_cb cancel_cb, void *cancel_arg)
2560 const struct got_error *err = NULL, *sync_err, *unlockerr;
2561 struct got_commit_object *commit = NULL;
2562 struct got_tree_object *tree = NULL;
2563 struct got_fileindex *fileindex = NULL;
2564 char *fileindex_path = NULL;
2565 struct got_pathlist_entry *pe;
2566 struct tree_path_data {
2567 SIMPLEQ_ENTRY(tree_path_data) entry;
2568 struct got_object_id *tree_id;
2573 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2575 SIMPLEQ_INIT(&tree_paths);
2577 err = lock_worktree(worktree, LOCK_EX);
2581 /* Map all specified paths to in-repository trees. */
2582 TAILQ_FOREACH(pe, paths, entry) {
2583 tpd = malloc(sizeof(*tpd));
2585 err = got_error_from_errno("malloc");
2589 err = find_tree_entry_for_checkout(&tpd->entry_type,
2590 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2596 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2597 err = got_path_basename(&tpd->entry_name, pe->path);
2605 tpd->entry_name = NULL;
2607 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2611 * Read the file index.
2612 * Checking out files is supposed to be an idempotent operation.
2613 * If the on-disk file index is incomplete we will try to complete it.
2615 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2619 tpd = SIMPLEQ_FIRST(&tree_paths);
2620 TAILQ_FOREACH(pe, paths, entry) {
2621 struct bump_base_commit_id_arg bbc_arg;
2623 err = checkout_files(worktree, fileindex, tpd->relpath,
2624 tpd->tree_id, tpd->entry_name, repo,
2625 progress_cb, progress_arg, cancel_cb, cancel_arg);
2629 bbc_arg.base_commit_id = worktree->base_commit_id;
2630 bbc_arg.entry_name = tpd->entry_name;
2631 bbc_arg.path = pe->path;
2632 bbc_arg.path_len = pe->path_len;
2633 bbc_arg.progress_cb = progress_cb;
2634 bbc_arg.progress_arg = progress_arg;
2635 err = got_fileindex_for_each_entry_safe(fileindex,
2636 bump_base_commit_id, &bbc_arg);
2640 tpd = SIMPLEQ_NEXT(tpd, entry);
2642 sync_err = sync_fileindex(fileindex, fileindex_path);
2643 if (sync_err && err == NULL)
2646 free(fileindex_path);
2648 got_object_tree_close(tree);
2650 got_object_commit_close(commit);
2652 got_fileindex_free(fileindex);
2653 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2654 tpd = SIMPLEQ_FIRST(&tree_paths);
2655 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2660 unlockerr = lock_worktree(worktree, LOCK_SH);
2661 if (unlockerr && err == NULL)
2666 struct merge_file_cb_arg {
2667 struct got_worktree *worktree;
2668 struct got_fileindex *fileindex;
2669 got_worktree_checkout_cb progress_cb;
2671 got_cancel_cb cancel_cb;
2673 const char *label_orig;
2674 struct got_object_id *commit_id2;
2677 static const struct got_error *
2678 merge_file_cb(void *arg, struct got_blob_object *blob1,
2679 struct got_blob_object *blob2, struct got_object_id *id1,
2680 struct got_object_id *id2, const char *path1, const char *path2,
2681 mode_t mode1, mode_t mode2, struct got_repository *repo)
2683 static const struct got_error *err = NULL;
2684 struct merge_file_cb_arg *a = arg;
2685 struct got_fileindex_entry *ie;
2686 char *ondisk_path = NULL;
2688 unsigned char status;
2689 int local_changes_subsumed;
2691 if (blob1 && blob2) {
2692 ie = got_fileindex_entry_get(a->fileindex, path2,
2695 return (*a->progress_cb)(a->progress_arg,
2696 GOT_STATUS_MISSING, path2);
2698 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2700 return got_error_from_errno("asprintf");
2702 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2707 if (status == GOT_STATUS_DELETE) {
2708 err = (*a->progress_cb)(a->progress_arg,
2709 GOT_STATUS_MERGE, path2);
2712 if (status != GOT_STATUS_NO_CHANGE &&
2713 status != GOT_STATUS_MODIFY &&
2714 status != GOT_STATUS_CONFLICT &&
2715 status != GOT_STATUS_ADD) {
2716 err = (*a->progress_cb)(a->progress_arg, status, path2);
2720 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2721 err = merge_symlink(a->worktree, blob1,
2722 ondisk_path, path2, sb.st_mode, a->label_orig,
2723 blob2, a->commit_id2, repo, a->progress_cb,
2726 err = merge_blob(&local_changes_subsumed, a->worktree,
2727 blob1, ondisk_path, path2, sb.st_mode,
2728 a->label_orig, blob2, a->commit_id2, repo,
2729 a->progress_cb, a->progress_arg);
2732 ie = got_fileindex_entry_get(a->fileindex, path1,
2735 return (*a->progress_cb)(a->progress_arg,
2736 GOT_STATUS_MISSING, path1);
2738 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2740 return got_error_from_errno("asprintf");
2742 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2748 case GOT_STATUS_NO_CHANGE:
2749 err = (*a->progress_cb)(a->progress_arg,
2750 GOT_STATUS_DELETE, path1);
2753 err = remove_ondisk_file(a->worktree->root_path, path1);
2757 got_fileindex_entry_mark_deleted_from_disk(ie);
2759 case GOT_STATUS_DELETE:
2760 case GOT_STATUS_MISSING:
2761 err = (*a->progress_cb)(a->progress_arg,
2762 GOT_STATUS_DELETE, path1);
2766 got_fileindex_entry_mark_deleted_from_disk(ie);
2768 case GOT_STATUS_ADD:
2769 case GOT_STATUS_MODIFY:
2770 case GOT_STATUS_CONFLICT:
2771 err = (*a->progress_cb)(a->progress_arg,
2772 GOT_STATUS_CANNOT_DELETE, path1);
2776 case GOT_STATUS_OBSTRUCTED:
2777 err = (*a->progress_cb)(a->progress_arg, status, path1);
2785 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2787 return got_error_from_errno("asprintf");
2788 ie = got_fileindex_entry_get(a->fileindex, path2,
2791 err = get_file_status(&status, &sb, ie, ondisk_path,
2795 if (status != GOT_STATUS_NO_CHANGE &&
2796 status != GOT_STATUS_MODIFY &&
2797 status != GOT_STATUS_CONFLICT &&
2798 status != GOT_STATUS_ADD) {
2799 err = (*a->progress_cb)(a->progress_arg,
2803 if (S_ISLNK(mode2)) {
2804 err = merge_symlink(a->worktree, NULL,
2805 ondisk_path, path2, sb.st_mode,
2806 a->label_orig, blob2, a->commit_id2,
2807 repo, a->progress_cb, a->progress_arg);
2811 err = merge_blob(&local_changes_subsumed,
2812 a->worktree, NULL, ondisk_path, path2,
2813 sb.st_mode, a->label_orig, blob2,
2814 a->commit_id2, repo, a->progress_cb,
2819 if (status == GOT_STATUS_DELETE) {
2820 err = got_fileindex_entry_update(ie,
2821 ondisk_path, blob2->id.sha1,
2822 a->worktree->base_commit_id->sha1, 0);
2827 int is_bad_symlink = 0;
2828 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2829 if (S_ISLNK(mode2)) {
2830 err = install_symlink(&is_bad_symlink,
2831 a->worktree, ondisk_path, path2, blob2, 0,
2832 0, 1, repo, a->progress_cb, a->progress_arg);
2834 err = install_blob(a->worktree, ondisk_path, path2,
2835 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2836 a->progress_cb, a->progress_arg);
2840 err = got_fileindex_entry_alloc(&ie, path2);
2843 err = got_fileindex_entry_update(ie, ondisk_path,
2846 got_fileindex_entry_free(ie);
2849 err = got_fileindex_entry_add(a->fileindex, ie);
2851 got_fileindex_entry_free(ie);
2854 if (is_bad_symlink) {
2855 got_fileindex_entry_filetype_set(ie,
2856 GOT_FILEIDX_MODE_BAD_SYMLINK);
2865 struct check_merge_ok_arg {
2866 struct got_worktree *worktree;
2867 struct got_repository *repo;
2870 static const struct got_error *
2871 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2873 const struct got_error *err = NULL;
2874 struct check_merge_ok_arg *a = arg;
2875 unsigned char status;
2879 /* Reject merges into a work tree with mixed base commits. */
2880 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2881 SHA1_DIGEST_LENGTH))
2882 return got_error(GOT_ERR_MIXED_COMMITS);
2884 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2886 return got_error_from_errno("asprintf");
2888 /* Reject merges into a work tree with conflicted files. */
2889 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2892 if (status == GOT_STATUS_CONFLICT)
2893 return got_error(GOT_ERR_CONFLICTS);
2898 static const struct got_error *
2899 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2900 const char *fileindex_path, struct got_object_id *commit_id1,
2901 struct got_object_id *commit_id2, struct got_repository *repo,
2902 got_worktree_checkout_cb progress_cb, void *progress_arg,
2903 got_cancel_cb cancel_cb, void *cancel_arg)
2905 const struct got_error *err = NULL, *sync_err;
2906 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2907 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2908 struct merge_file_cb_arg arg;
2909 char *label_orig = NULL;
2914 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2915 worktree->path_prefix);
2919 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2923 err = got_object_id_str(&id_str, commit_id1);
2927 if (asprintf(&label_orig, "%s: commit %s",
2928 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2929 err = got_error_from_errno("asprintf");
2936 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2937 worktree->path_prefix);
2941 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2945 arg.worktree = worktree;
2946 arg.fileindex = fileindex;
2947 arg.progress_cb = progress_cb;
2948 arg.progress_arg = progress_arg;
2949 arg.cancel_cb = cancel_cb;
2950 arg.cancel_arg = cancel_arg;
2951 arg.label_orig = label_orig;
2952 arg.commit_id2 = commit_id2;
2953 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2954 sync_err = sync_fileindex(fileindex, fileindex_path);
2955 if (sync_err && err == NULL)
2959 got_object_tree_close(tree1);
2961 got_object_tree_close(tree2);
2966 const struct got_error *
2967 got_worktree_merge_files(struct got_worktree *worktree,
2968 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2969 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2970 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2972 const struct got_error *err, *unlockerr;
2973 char *fileindex_path = NULL;
2974 struct got_fileindex *fileindex = NULL;
2975 struct check_merge_ok_arg mok_arg;
2977 err = lock_worktree(worktree, LOCK_EX);
2981 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2985 mok_arg.worktree = worktree;
2986 mok_arg.repo = repo;
2987 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2992 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2993 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2996 got_fileindex_free(fileindex);
2997 free(fileindex_path);
2998 unlockerr = lock_worktree(worktree, LOCK_SH);
2999 if (unlockerr && err == NULL)
3004 struct diff_dir_cb_arg {
3005 struct got_fileindex *fileindex;
3006 struct got_worktree *worktree;
3007 const char *status_path;
3008 size_t status_path_len;
3009 struct got_repository *repo;
3010 got_worktree_status_cb status_cb;
3012 got_cancel_cb cancel_cb;
3014 /* A pathlist containing per-directory pathlists of ignore patterns. */
3015 struct got_pathlist_head ignores;
3016 int report_unchanged;
3020 static const struct got_error *
3021 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3022 int dirfd, const char *de_name,
3023 got_worktree_status_cb status_cb, void *status_arg,
3024 struct got_repository *repo, int report_unchanged)
3026 const struct got_error *err = NULL;
3027 unsigned char status = GOT_STATUS_NO_CHANGE;
3028 unsigned char staged_status = get_staged_status(ie);
3030 struct got_object_id blob_id, commit_id, staged_blob_id;
3031 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3032 struct got_object_id *staged_blob_idp = NULL;
3034 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3038 if (status == GOT_STATUS_NO_CHANGE &&
3039 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3042 if (got_fileindex_entry_has_blob(ie)) {
3043 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3044 blob_idp = &blob_id;
3046 if (got_fileindex_entry_has_commit(ie)) {
3047 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3048 commit_idp = &commit_id;
3050 if (staged_status == GOT_STATUS_ADD ||
3051 staged_status == GOT_STATUS_MODIFY) {
3052 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3053 SHA1_DIGEST_LENGTH);
3054 staged_blob_idp = &staged_blob_id;
3057 return (*status_cb)(status_arg, status, staged_status,
3058 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3061 static const struct got_error *
3062 status_old_new(void *arg, struct got_fileindex_entry *ie,
3063 struct dirent *de, const char *parent_path, int dirfd)
3065 const struct got_error *err = NULL;
3066 struct diff_dir_cb_arg *a = arg;
3069 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3070 return got_error(GOT_ERR_CANCELLED);
3072 if (got_path_cmp(parent_path, a->status_path,
3073 strlen(parent_path), a->status_path_len) != 0 &&
3074 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3077 if (parent_path[0]) {
3078 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3079 parent_path, de->d_name) == -1)
3080 return got_error_from_errno("asprintf");
3082 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3084 return got_error_from_errno("asprintf");
3087 err = report_file_status(ie, abspath, dirfd, de->d_name,
3088 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3093 static const struct got_error *
3094 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3096 struct diff_dir_cb_arg *a = arg;
3097 struct got_object_id blob_id, commit_id;
3098 unsigned char status;
3100 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3101 return got_error(GOT_ERR_CANCELLED);
3103 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3106 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3107 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3108 if (got_fileindex_entry_has_file_on_disk(ie))
3109 status = GOT_STATUS_MISSING;
3111 status = GOT_STATUS_DELETE;
3112 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3113 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3117 free_ignorelist(struct got_pathlist_head *ignorelist)
3119 struct got_pathlist_entry *pe;
3121 TAILQ_FOREACH(pe, ignorelist, entry)
3122 free((char *)pe->path);
3123 got_pathlist_free(ignorelist);
3127 free_ignores(struct got_pathlist_head *ignores)
3129 struct got_pathlist_entry *pe;
3131 TAILQ_FOREACH(pe, ignores, entry) {
3132 struct got_pathlist_head *ignorelist = pe->data;
3133 free_ignorelist(ignorelist);
3134 free((char *)pe->path);
3136 got_pathlist_free(ignores);
3139 static const struct got_error *
3140 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3142 const struct got_error *err = NULL;
3143 struct got_pathlist_entry *pe = NULL;
3144 struct got_pathlist_head *ignorelist;
3145 char *line = NULL, *pattern, *dirpath = NULL;
3146 size_t linesize = 0;
3149 ignorelist = calloc(1, sizeof(*ignorelist));
3150 if (ignorelist == NULL)
3151 return got_error_from_errno("calloc");
3152 TAILQ_INIT(ignorelist);
3154 while ((linelen = getline(&line, &linesize, f)) != -1) {
3155 if (linelen > 0 && line[linelen - 1] == '\n')
3156 line[linelen - 1] = '\0';
3158 /* Git's ignores may contain comments. */
3162 /* Git's negated patterns are not (yet?) supported. */
3166 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3168 err = got_error_from_errno("asprintf");
3171 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3176 err = got_error_from_errno("getline");
3180 dirpath = strdup(path);
3181 if (dirpath == NULL) {
3182 err = got_error_from_errno("strdup");
3185 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3188 if (err || pe == NULL) {
3190 free_ignorelist(ignorelist);
3196 match_ignores(struct got_pathlist_head *ignores, const char *path)
3198 struct got_pathlist_entry *pe;
3200 /* Handle patterns which match in all directories. */
3201 TAILQ_FOREACH(pe, ignores, entry) {
3202 struct got_pathlist_head *ignorelist = pe->data;
3203 struct got_pathlist_entry *pi;
3205 TAILQ_FOREACH(pi, ignorelist, entry) {
3206 const char *p, *pattern = pi->path;
3208 if (strncmp(pattern, "**/", 3) != 0)
3213 if (fnmatch(pattern, p,
3214 FNM_PATHNAME | FNM_LEADING_DIR)) {
3215 /* Retry in next directory. */
3216 while (*p && *p != '/')
3228 * The ignores pathlist contains ignore lists from children before
3229 * parents, so we can find the most specific ignorelist by walking
3230 * ignores backwards.
3232 pe = TAILQ_LAST(ignores, got_pathlist_head);
3234 if (got_path_is_child(path, pe->path, pe->path_len)) {
3235 struct got_pathlist_head *ignorelist = pe->data;
3236 struct got_pathlist_entry *pi;
3237 TAILQ_FOREACH(pi, ignorelist, entry) {
3238 const char *pattern = pi->path;
3239 int flags = FNM_LEADING_DIR;
3240 if (strstr(pattern, "/**/") == NULL)
3241 flags |= FNM_PATHNAME;
3242 if (fnmatch(pattern, path, flags))
3247 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3253 static const struct got_error *
3254 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3255 const char *path, int dirfd, const char *ignores_filename)
3257 const struct got_error *err = NULL;
3260 FILE *ignoresfile = NULL;
3262 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3263 path[0] ? "/" : "", ignores_filename) == -1)
3264 return got_error_from_errno("asprintf");
3267 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3269 if (errno != ENOENT && errno != EACCES)
3270 err = got_error_from_errno2("openat",
3273 ignoresfile = fdopen(fd, "r");
3274 if (ignoresfile == NULL)
3275 err = got_error_from_errno2("fdopen",
3279 err = read_ignores(ignores, path, ignoresfile);
3283 ignoresfile = fopen(ignorespath, "r");
3284 if (ignoresfile == NULL) {
3285 if (errno != ENOENT && errno != EACCES)
3286 err = got_error_from_errno2("fopen",
3289 err = read_ignores(ignores, path, ignoresfile);
3292 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3293 err = got_error_from_errno2("fclose", path);
3294 if (fd != -1 && close(fd) == -1 && err == NULL)
3295 err = got_error_from_errno2("close", path);
3300 static const struct got_error *
3301 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3303 const struct got_error *err = NULL;
3304 struct diff_dir_cb_arg *a = arg;
3307 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3308 return got_error(GOT_ERR_CANCELLED);
3310 if (parent_path[0]) {
3311 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3312 return got_error_from_errno("asprintf");
3317 if (de->d_type != DT_DIR &&
3318 got_path_is_child(path, a->status_path, a->status_path_len)
3319 && !match_ignores(&a->ignores, path))
3320 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3321 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3327 static const struct got_error *
3328 status_traverse(void *arg, const char *path, int dirfd)
3330 const struct got_error *err = NULL;
3331 struct diff_dir_cb_arg *a = arg;
3336 err = add_ignores(&a->ignores, a->worktree->root_path,
3337 path, dirfd, ".cvsignore");
3341 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3342 dirfd, ".gitignore");
3347 static const struct got_error *
3348 report_single_file_status(const char *path, const char *ondisk_path,
3349 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3350 void *status_arg, struct got_repository *repo, int report_unchanged)
3352 struct got_fileindex_entry *ie;
3355 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3357 return report_file_status(ie, ondisk_path, -1, NULL,
3358 status_cb, status_arg, repo, report_unchanged);
3360 if (lstat(ondisk_path, &sb) == -1) {
3361 if (errno != ENOENT)
3362 return got_error_from_errno2("lstat", ondisk_path);
3363 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3364 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3368 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3369 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3370 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3375 static const struct got_error *
3376 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3377 const char *root_path, const char *path)
3379 const struct got_error *err;
3380 char *parent_path, *next_parent_path = NULL;
3382 err = add_ignores(ignores, root_path, "", -1,
3387 err = add_ignores(ignores, root_path, "", -1,
3392 err = got_path_dirname(&parent_path, path);
3394 if (err->code == GOT_ERR_BAD_PATH)
3395 return NULL; /* cannot traverse parent */
3399 err = add_ignores(ignores, root_path, parent_path, -1,
3403 err = add_ignores(ignores, root_path, parent_path, -1,
3407 err = got_path_dirname(&next_parent_path, parent_path);
3409 if (err->code == GOT_ERR_BAD_PATH)
3410 err = NULL; /* traversed everything */
3414 parent_path = next_parent_path;
3415 next_parent_path = NULL;
3419 free(next_parent_path);
3423 static const struct got_error *
3424 worktree_status(struct got_worktree *worktree, const char *path,
3425 struct got_fileindex *fileindex, struct got_repository *repo,
3426 got_worktree_status_cb status_cb, void *status_arg,
3427 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3428 int report_unchanged)
3430 const struct got_error *err = NULL;
3432 struct got_fileindex_diff_dir_cb fdiff_cb;
3433 struct diff_dir_cb_arg arg;
3434 char *ondisk_path = NULL;
3436 TAILQ_INIT(&arg.ignores);
3438 if (asprintf(&ondisk_path, "%s%s%s",
3439 worktree->root_path, path[0] ? "/" : "", path) == -1)
3440 return got_error_from_errno("asprintf");
3442 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3444 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3446 err = got_error_from_errno2("open", ondisk_path);
3448 err = report_single_file_status(path, ondisk_path,
3449 fileindex, status_cb, status_arg, repo,
3452 fdiff_cb.diff_old_new = status_old_new;
3453 fdiff_cb.diff_old = status_old;
3454 fdiff_cb.diff_new = status_new;
3455 fdiff_cb.diff_traverse = status_traverse;
3456 arg.fileindex = fileindex;
3457 arg.worktree = worktree;
3458 arg.status_path = path;
3459 arg.status_path_len = strlen(path);
3461 arg.status_cb = status_cb;
3462 arg.status_arg = status_arg;
3463 arg.cancel_cb = cancel_cb;
3464 arg.cancel_arg = cancel_arg;
3465 arg.report_unchanged = report_unchanged;
3466 arg.no_ignores = no_ignores;
3468 err = add_ignores_from_parent_paths(&arg.ignores,
3469 worktree->root_path, path);
3473 err = got_fileindex_diff_dir(fileindex, fd,
3474 worktree->root_path, path, repo, &fdiff_cb, &arg);
3477 free_ignores(&arg.ignores);
3478 if (fd != -1 && close(fd) != 0 && err == NULL)
3479 err = got_error_from_errno("close");
3484 const struct got_error *
3485 got_worktree_status(struct got_worktree *worktree,
3486 struct got_pathlist_head *paths, struct got_repository *repo,
3487 got_worktree_status_cb status_cb, void *status_arg,
3488 got_cancel_cb cancel_cb, void *cancel_arg)
3490 const struct got_error *err = NULL;
3491 char *fileindex_path = NULL;
3492 struct got_fileindex *fileindex = NULL;
3493 struct got_pathlist_entry *pe;
3495 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3499 TAILQ_FOREACH(pe, paths, entry) {
3500 err = worktree_status(worktree, pe->path, fileindex, repo,
3501 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3505 free(fileindex_path);
3506 got_fileindex_free(fileindex);
3510 const struct got_error *
3511 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3514 const struct got_error *err = NULL;
3515 char *resolved = NULL, *cwd = NULL, *path = NULL;
3521 cwd = getcwd(NULL, 0);
3523 return got_error_from_errno("getcwd");
3525 if (lstat(arg, &sb) == -1) {
3526 if (errno != ENOENT) {
3527 err = got_error_from_errno2("lstat", arg);
3531 if (S_ISLNK(sb.st_mode)) {
3533 * We cannot use realpath(3) with symlinks since we want to
3534 * operate on the symlink itself.
3535 * But we can make the path absolute, assuming it is relative
3536 * to the current working directory, and then canonicalize it.
3538 char *abspath = NULL;
3539 char canonpath[PATH_MAX];
3540 if (!got_path_is_absolute(arg)) {
3541 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3542 err = got_error_from_errno("asprintf");
3547 err = got_canonpath(abspath ? abspath : arg, canonpath,
3551 resolved = strdup(canonpath);
3552 if (resolved == NULL) {
3553 err = got_error_from_errno("strdup");
3557 resolved = realpath(arg, NULL);
3558 if (resolved == NULL) {
3559 if (errno != ENOENT) {
3560 err = got_error_from_errno2("realpath", arg);
3563 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3564 err = got_error_from_errno("asprintf");
3570 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3571 strlen(got_worktree_get_root_path(worktree)))) {
3572 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3576 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3577 err = got_path_skip_common_ancestor(&path,
3578 got_worktree_get_root_path(worktree), resolved);
3584 err = got_error_from_errno("strdup");
3589 /* XXX status walk can't deal with trailing slash! */
3591 while (len > 0 && path[len - 1] == '/') {
3592 path[len - 1] = '\0';
3605 struct schedule_addition_args {
3606 struct got_worktree *worktree;
3607 struct got_fileindex *fileindex;
3608 got_worktree_checkout_cb progress_cb;
3610 struct got_repository *repo;
3613 static const struct got_error *
3614 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3615 const char *relpath, struct got_object_id *blob_id,
3616 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3617 int dirfd, const char *de_name)
3619 struct schedule_addition_args *a = arg;
3620 const struct got_error *err = NULL;
3621 struct got_fileindex_entry *ie;
3625 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3627 return got_error_from_errno("asprintf");
3629 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3631 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3635 /* Re-adding an existing entry is a no-op. */
3636 if (status == GOT_STATUS_ADD)
3638 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3643 if (status != GOT_STATUS_UNVERSIONED) {
3644 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3648 err = got_fileindex_entry_alloc(&ie, relpath);
3651 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3653 got_fileindex_entry_free(ie);
3656 err = got_fileindex_entry_add(a->fileindex, ie);
3658 got_fileindex_entry_free(ie);
3665 if (status == GOT_STATUS_ADD)
3667 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3670 const struct got_error *
3671 got_worktree_schedule_add(struct got_worktree *worktree,
3672 struct got_pathlist_head *paths,
3673 got_worktree_checkout_cb progress_cb, void *progress_arg,
3674 struct got_repository *repo, int no_ignores)
3676 struct got_fileindex *fileindex = NULL;
3677 char *fileindex_path = NULL;
3678 const struct got_error *err = NULL, *sync_err, *unlockerr;
3679 struct got_pathlist_entry *pe;
3680 struct schedule_addition_args saa;
3682 err = lock_worktree(worktree, LOCK_EX);
3686 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3690 saa.worktree = worktree;
3691 saa.fileindex = fileindex;
3692 saa.progress_cb = progress_cb;
3693 saa.progress_arg = progress_arg;
3696 TAILQ_FOREACH(pe, paths, entry) {
3697 err = worktree_status(worktree, pe->path, fileindex, repo,
3698 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3702 sync_err = sync_fileindex(fileindex, fileindex_path);
3703 if (sync_err && err == NULL)
3706 free(fileindex_path);
3708 got_fileindex_free(fileindex);
3709 unlockerr = lock_worktree(worktree, LOCK_SH);
3710 if (unlockerr && err == NULL)
3715 struct schedule_deletion_args {
3716 struct got_worktree *worktree;
3717 struct got_fileindex *fileindex;
3718 got_worktree_delete_cb progress_cb;
3720 struct got_repository *repo;
3721 int delete_local_mods;
3725 static const struct got_error *
3726 schedule_for_deletion(void *arg, unsigned char status,
3727 unsigned char staged_status, const char *relpath,
3728 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3729 struct got_object_id *commit_id, int dirfd, const char *de_name)
3731 struct schedule_deletion_args *a = arg;
3732 const struct got_error *err = NULL;
3733 struct got_fileindex_entry *ie = NULL;
3735 char *ondisk_path, *parent = NULL;
3737 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3739 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3741 staged_status = get_staged_status(ie);
3742 if (staged_status != GOT_STATUS_NO_CHANGE) {
3743 if (staged_status == GOT_STATUS_DELETE)
3745 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3748 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3750 return got_error_from_errno("asprintf");
3752 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3757 if (status != GOT_STATUS_NO_CHANGE) {
3758 if (status == GOT_STATUS_DELETE)
3760 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3761 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3764 if (status != GOT_STATUS_MODIFY &&
3765 status != GOT_STATUS_MISSING) {
3766 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3771 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3773 if (unlinkat(dirfd, de_name, 0) != 0) {
3774 err = got_error_from_errno2("unlinkat",
3778 } else if (unlink(ondisk_path) != 0) {
3779 err = got_error_from_errno2("unlink", ondisk_path);
3783 parent = dirname(ondisk_path);
3785 if (parent == NULL) {
3786 err = got_error_from_errno2("dirname", ondisk_path);
3789 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3790 if (rmdir(parent) == -1) {
3791 if (errno != ENOTEMPTY)
3792 err = got_error_from_errno2("rmdir",
3796 parent = dirname(parent);
3797 if (parent == NULL) {
3798 err = got_error_from_errno2("dirname", parent);
3804 got_fileindex_entry_mark_deleted_from_disk(ie);
3809 if (status == GOT_STATUS_DELETE)
3811 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3812 staged_status, relpath);
3815 const struct got_error *
3816 got_worktree_schedule_delete(struct got_worktree *worktree,
3817 struct got_pathlist_head *paths, int delete_local_mods,
3818 got_worktree_delete_cb progress_cb, void *progress_arg,
3819 struct got_repository *repo, int keep_on_disk)
3821 struct got_fileindex *fileindex = NULL;
3822 char *fileindex_path = NULL;
3823 const struct got_error *err = NULL, *sync_err, *unlockerr;
3824 struct got_pathlist_entry *pe;
3825 struct schedule_deletion_args sda;
3827 err = lock_worktree(worktree, LOCK_EX);
3831 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3835 sda.worktree = worktree;
3836 sda.fileindex = fileindex;
3837 sda.progress_cb = progress_cb;
3838 sda.progress_arg = progress_arg;
3840 sda.delete_local_mods = delete_local_mods;
3841 sda.keep_on_disk = keep_on_disk;
3843 TAILQ_FOREACH(pe, paths, entry) {
3844 err = worktree_status(worktree, pe->path, fileindex, repo,
3845 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3849 sync_err = sync_fileindex(fileindex, fileindex_path);
3850 if (sync_err && err == NULL)
3853 free(fileindex_path);
3855 got_fileindex_free(fileindex);
3856 unlockerr = lock_worktree(worktree, LOCK_SH);
3857 if (unlockerr && err == NULL)
3862 static const struct got_error *
3863 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3865 const struct got_error *err = NULL;
3867 size_t linesize = 0, n;
3870 linelen = getline(&line, &linesize, infile);
3871 if (linelen == -1) {
3872 if (ferror(infile)) {
3873 err = got_error_from_errno("getline");
3879 n = fwrite(line, 1, linelen, outfile);
3881 err = got_ferror(outfile, GOT_ERR_IO);
3886 n = fwrite(line, 1, linelen, rejectfile);
3888 err = got_ferror(outfile, GOT_ERR_IO);
3895 static const struct got_error *
3896 skip_one_line(FILE *f)
3899 size_t linesize = 0;
3902 linelen = getline(&line, &linesize, f);
3903 if (linelen == -1) {
3905 return got_error_from_errno("getline");
3912 static const struct got_error *
3913 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3914 int start_old, int end_old, int start_new, int end_new,
3915 FILE *outfile, FILE *rejectfile)
3917 const struct got_error *err;
3919 /* Copy old file's lines leading up to patch. */
3920 while (!feof(f1) && *line_cur1 < start_old) {
3921 err = copy_one_line(f1, outfile, NULL);
3926 /* Skip new file's lines leading up to patch. */
3927 while (!feof(f2) && *line_cur2 < start_new) {
3929 err = copy_one_line(f2, NULL, rejectfile);
3931 err = skip_one_line(f2);
3936 /* Copy patched lines. */
3937 while (!feof(f2) && *line_cur2 <= end_new) {
3938 err = copy_one_line(f2, outfile, NULL);
3943 /* Skip over old file's replaced lines. */
3944 while (!feof(f1) && *line_cur1 <= end_old) {
3946 err = copy_one_line(f1, NULL, rejectfile);
3948 err = skip_one_line(f1);
3957 static const struct got_error *
3958 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3959 FILE *outfile, FILE *rejectfile)
3961 const struct got_error *err;
3964 /* Copy old file's lines until EOF. */
3966 err = copy_one_line(f1, outfile, NULL);
3973 /* Copy new file's lines until EOF. */
3975 err = copy_one_line(f2, NULL, rejectfile);
3985 static const struct got_error *
3986 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3987 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3988 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3989 int *line_cur2, FILE *outfile, FILE *rejectfile,
3990 got_worktree_patch_cb patch_cb, void *patch_arg)
3992 const struct got_error *err = NULL;
3993 int start_old = change->cv.a;
3994 int end_old = change->cv.b;
3995 int start_new = change->cv.c;
3996 int end_new = change->cv.d;
4000 *choice = GOT_PATCH_CHOICE_NONE;
4002 hunkfile = got_opentemp();
4003 if (hunkfile == NULL)
4004 return got_error_from_errno("got_opentemp");
4009 /* XXX TODO needs error checking */
4010 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4012 if (fseek(f1, pos1, SEEK_SET) == -1) {
4013 err = got_ferror(f1, GOT_ERR_IO);
4016 if (fseek(f2, pos2, SEEK_SET) == -1) {
4017 err = got_ferror(f1, GOT_ERR_IO);
4020 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4021 err = got_ferror(hunkfile, GOT_ERR_IO);
4025 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4026 hunkfile, n, nchanges);
4031 case GOT_PATCH_CHOICE_YES:
4032 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4033 end_old, start_new, end_new, outfile, rejectfile);
4035 case GOT_PATCH_CHOICE_NO:
4036 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4037 end_old, start_new, end_new, rejectfile, outfile);
4039 case GOT_PATCH_CHOICE_QUIT:
4042 err = got_error(GOT_ERR_PATCH_CHOICE);
4046 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4047 err = got_error_from_errno("fclose");
4051 struct revert_file_args {
4052 struct got_worktree *worktree;
4053 struct got_fileindex *fileindex;
4054 got_worktree_checkout_cb progress_cb;
4056 got_worktree_patch_cb patch_cb;
4058 struct got_repository *repo;
4061 static const struct got_error *
4062 create_patched_content(char **path_outfile, int reverse_patch,
4063 struct got_object_id *blob_id, const char *path2,
4064 int dirfd2, const char *de_name2,
4065 const char *relpath, struct got_repository *repo,
4066 got_worktree_patch_cb patch_cb, void *patch_arg)
4068 const struct got_error *err;
4069 struct got_blob_object *blob = NULL;
4070 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4072 char link_target[PATH_MAX];
4073 ssize_t link_len = 0;
4074 char *path1 = NULL, *id_str = NULL;
4075 struct stat sb1, sb2;
4076 struct got_diff_changes *changes = NULL;
4077 struct got_diff_state *ds = NULL;
4078 struct got_diff_args *args = NULL;
4079 struct got_diff_change *change;
4080 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4083 *path_outfile = NULL;
4085 err = got_object_id_str(&id_str, blob_id);
4090 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4092 if (errno != ELOOP) {
4093 err = got_error_from_errno2("openat", path2);
4096 link_len = readlinkat(dirfd2, de_name2,
4097 link_target, sizeof(link_target));
4099 return got_error_from_errno2("readlinkat", path2);
4100 sb2.st_mode = S_IFLNK;
4101 sb2.st_size = link_len;
4104 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4106 if (errno != ELOOP) {
4107 err = got_error_from_errno2("open", path2);
4110 link_len = readlink(path2, link_target,
4111 sizeof(link_target));
4113 return got_error_from_errno2("readlink", path2);
4114 sb2.st_mode = S_IFLNK;
4115 sb2.st_size = link_len;
4119 if (fstat(fd2, &sb2) == -1) {
4120 err = got_error_from_errno2("fstat", path2);
4124 f2 = fdopen(fd2, "r");
4126 err = got_error_from_errno2("fdopen", path2);
4132 f2 = got_opentemp();
4134 err = got_error_from_errno2("got_opentemp", path2);
4137 n = fwrite(link_target, 1, link_len, f2);
4138 if (n != link_len) {
4139 err = got_ferror(f2, GOT_ERR_IO);
4142 if (fflush(f2) == EOF) {
4143 err = got_error_from_errno("fflush");
4149 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4153 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4157 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4161 if (stat(path1, &sb1) == -1) {
4162 err = got_error_from_errno2("stat", path1);
4166 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4167 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4171 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4175 if (fseek(f1, 0L, SEEK_SET) == -1)
4176 return got_ferror(f1, GOT_ERR_IO);
4177 if (fseek(f2, 0L, SEEK_SET) == -1)
4178 return got_ferror(f2, GOT_ERR_IO);
4179 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4181 err = apply_or_reject_change(&choice, change, ++n,
4182 changes->nchanges, ds, args, diff_flags, relpath,
4183 f1, f2, &line_cur1, &line_cur2,
4184 reverse_patch ? NULL : outfile,
4185 reverse_patch ? outfile : NULL,
4186 patch_cb, patch_arg);
4189 if (choice == GOT_PATCH_CHOICE_YES)
4191 else if (choice == GOT_PATCH_CHOICE_QUIT)
4195 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4196 reverse_patch ? NULL : outfile,
4197 reverse_patch ? outfile : NULL);
4201 if (!S_ISLNK(sb2.st_mode)) {
4202 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4203 err = got_error_from_errno2("chmod", path2);
4211 got_object_blob_close(blob);
4212 if (f1 && fclose(f1) == EOF && err == NULL)
4213 err = got_error_from_errno2("fclose", path1);
4214 if (f2 && fclose(f2) == EOF && err == NULL)
4215 err = got_error_from_errno2("fclose", path2);
4216 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4217 err = got_error_from_errno2("close", path2);
4218 if (outfile && fclose(outfile) == EOF && err == NULL)
4219 err = got_error_from_errno2("fclose", *path_outfile);
4220 if (path1 && unlink(path1) == -1 && err == NULL)
4221 err = got_error_from_errno2("unlink", path1);
4222 if (err || !have_content) {
4223 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4224 err = got_error_from_errno2("unlink", *path_outfile);
4225 free(*path_outfile);
4226 *path_outfile = NULL;
4230 got_diff_state_free(ds);
4234 got_diff_free_changes(changes);
4239 static const struct got_error *
4240 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4241 const char *relpath, struct got_object_id *blob_id,
4242 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4243 int dirfd, const char *de_name)
4245 struct revert_file_args *a = arg;
4246 const struct got_error *err = NULL;
4247 char *parent_path = NULL;
4248 struct got_fileindex_entry *ie;
4249 struct got_tree_object *tree = NULL;
4250 struct got_object_id *tree_id = NULL;
4251 const struct got_tree_entry *te = NULL;
4252 char *tree_path = NULL, *te_name;
4253 char *ondisk_path = NULL, *path_content = NULL;
4254 struct got_blob_object *blob = NULL;
4256 /* Reverting a staged deletion is a no-op. */
4257 if (status == GOT_STATUS_DELETE &&
4258 staged_status != GOT_STATUS_NO_CHANGE)
4261 if (status == GOT_STATUS_UNVERSIONED)
4262 return (*a->progress_cb)(a->progress_arg,
4263 GOT_STATUS_UNVERSIONED, relpath);
4265 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4267 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4269 /* Construct in-repository path of tree which contains this blob. */
4270 err = got_path_dirname(&parent_path, ie->path);
4272 if (err->code != GOT_ERR_BAD_PATH)
4274 parent_path = strdup("/");
4275 if (parent_path == NULL) {
4276 err = got_error_from_errno("strdup");
4280 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4281 tree_path = strdup(parent_path);
4282 if (tree_path == NULL) {
4283 err = got_error_from_errno("strdup");
4287 if (got_path_is_root_dir(parent_path)) {
4288 tree_path = strdup(a->worktree->path_prefix);
4289 if (tree_path == NULL) {
4290 err = got_error_from_errno("strdup");
4294 if (asprintf(&tree_path, "%s/%s",
4295 a->worktree->path_prefix, parent_path) == -1) {
4296 err = got_error_from_errno("asprintf");
4302 err = got_object_id_by_path(&tree_id, a->repo,
4303 a->worktree->base_commit_id, tree_path);
4305 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4306 (status == GOT_STATUS_ADD ||
4307 staged_status == GOT_STATUS_ADD)))
4310 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4314 te_name = basename(ie->path);
4315 if (te_name == NULL) {
4316 err = got_error_from_errno2("basename", ie->path);
4320 te = got_object_tree_find_entry(tree, te_name);
4321 if (te == NULL && status != GOT_STATUS_ADD &&
4322 staged_status != GOT_STATUS_ADD) {
4323 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4329 case GOT_STATUS_ADD:
4331 int choice = GOT_PATCH_CHOICE_NONE;
4332 err = (*a->patch_cb)(&choice, a->patch_arg,
4333 status, ie->path, NULL, 1, 1);
4336 if (choice != GOT_PATCH_CHOICE_YES)
4339 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4343 got_fileindex_entry_remove(a->fileindex, ie);
4345 case GOT_STATUS_DELETE:
4347 int choice = GOT_PATCH_CHOICE_NONE;
4348 err = (*a->patch_cb)(&choice, a->patch_arg,
4349 status, ie->path, NULL, 1, 1);
4352 if (choice != GOT_PATCH_CHOICE_YES)
4356 case GOT_STATUS_MODIFY:
4357 case GOT_STATUS_MODE_CHANGE:
4358 case GOT_STATUS_CONFLICT:
4359 case GOT_STATUS_MISSING: {
4360 struct got_object_id id;
4361 if (staged_status == GOT_STATUS_ADD ||
4362 staged_status == GOT_STATUS_MODIFY) {
4363 memcpy(id.sha1, ie->staged_blob_sha1,
4364 SHA1_DIGEST_LENGTH);
4366 memcpy(id.sha1, ie->blob_sha1,
4367 SHA1_DIGEST_LENGTH);
4368 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4372 if (asprintf(&ondisk_path, "%s/%s",
4373 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4374 err = got_error_from_errno("asprintf");
4378 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4379 status == GOT_STATUS_CONFLICT)) {
4380 int is_bad_symlink = 0;
4381 err = create_patched_content(&path_content, 1, &id,
4382 ondisk_path, dirfd, de_name, ie->path, a->repo,
4383 a->patch_cb, a->patch_arg);
4384 if (err || path_content == NULL)
4386 if (te && S_ISLNK(te->mode)) {
4387 if (unlink(path_content) == -1) {
4388 err = got_error_from_errno2("unlink",
4392 err = install_symlink(&is_bad_symlink,
4393 a->worktree, ondisk_path, ie->path,
4394 blob, 0, 1, 0, a->repo,
4395 a->progress_cb, a->progress_arg);
4397 if (rename(path_content, ondisk_path) == -1) {
4398 err = got_error_from_errno3("rename",
4399 path_content, ondisk_path);
4404 int is_bad_symlink = 0;
4405 if (te && S_ISLNK(te->mode)) {
4406 err = install_symlink(&is_bad_symlink,
4407 a->worktree, ondisk_path, ie->path,
4408 blob, 0, 1, 0, a->repo,
4409 a->progress_cb, a->progress_arg);
4411 err = install_blob(a->worktree, ondisk_path,
4413 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4414 got_fileindex_perms_to_st(ie), blob,
4415 0, 1, 0, 0, a->repo,
4416 a->progress_cb, a->progress_arg);
4420 if (status == GOT_STATUS_DELETE ||
4421 status == GOT_STATUS_MODE_CHANGE) {
4422 err = got_fileindex_entry_update(ie,
4423 ondisk_path, blob->id.sha1,
4424 a->worktree->base_commit_id->sha1, 1);
4428 if (is_bad_symlink) {
4429 got_fileindex_entry_filetype_set(ie,
4430 GOT_FILEIDX_MODE_BAD_SYMLINK);
4444 got_object_blob_close(blob);
4446 got_object_tree_close(tree);
4451 const struct got_error *
4452 got_worktree_revert(struct got_worktree *worktree,
4453 struct got_pathlist_head *paths,
4454 got_worktree_checkout_cb progress_cb, void *progress_arg,
4455 got_worktree_patch_cb patch_cb, void *patch_arg,
4456 struct got_repository *repo)
4458 struct got_fileindex *fileindex = NULL;
4459 char *fileindex_path = NULL;
4460 const struct got_error *err = NULL, *unlockerr = NULL;
4461 const struct got_error *sync_err = NULL;
4462 struct got_pathlist_entry *pe;
4463 struct revert_file_args rfa;
4465 err = lock_worktree(worktree, LOCK_EX);
4469 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4473 rfa.worktree = worktree;
4474 rfa.fileindex = fileindex;
4475 rfa.progress_cb = progress_cb;
4476 rfa.progress_arg = progress_arg;
4477 rfa.patch_cb = patch_cb;
4478 rfa.patch_arg = patch_arg;
4480 TAILQ_FOREACH(pe, paths, entry) {
4481 err = worktree_status(worktree, pe->path, fileindex, repo,
4482 revert_file, &rfa, NULL, NULL, 0, 0);
4486 sync_err = sync_fileindex(fileindex, fileindex_path);
4487 if (sync_err && err == NULL)
4490 free(fileindex_path);
4492 got_fileindex_free(fileindex);
4493 unlockerr = lock_worktree(worktree, LOCK_SH);
4494 if (unlockerr && err == NULL)
4500 free_commitable(struct got_commitable *ct)
4503 free(ct->in_repo_path);
4504 free(ct->ondisk_path);
4506 free(ct->base_blob_id);
4507 free(ct->staged_blob_id);
4508 free(ct->base_commit_id);
4512 struct collect_commitables_arg {
4513 struct got_pathlist_head *commitable_paths;
4514 struct got_repository *repo;
4515 struct got_worktree *worktree;
4516 struct got_fileindex *fileindex;
4517 int have_staged_files;
4520 static const struct got_error *
4521 collect_commitables(void *arg, unsigned char status,
4522 unsigned char staged_status, const char *relpath,
4523 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4524 struct got_object_id *commit_id, int dirfd, const char *de_name)
4526 struct collect_commitables_arg *a = arg;
4527 const struct got_error *err = NULL;
4528 struct got_commitable *ct = NULL;
4529 struct got_pathlist_entry *new = NULL;
4530 char *parent_path = NULL, *path = NULL;
4533 if (a->have_staged_files) {
4534 if (staged_status != GOT_STATUS_MODIFY &&
4535 staged_status != GOT_STATUS_ADD &&
4536 staged_status != GOT_STATUS_DELETE)
4539 if (status == GOT_STATUS_CONFLICT)
4540 return got_error(GOT_ERR_COMMIT_CONFLICT);
4542 if (status != GOT_STATUS_MODIFY &&
4543 status != GOT_STATUS_MODE_CHANGE &&
4544 status != GOT_STATUS_ADD &&
4545 status != GOT_STATUS_DELETE)
4549 if (asprintf(&path, "/%s", relpath) == -1) {
4550 err = got_error_from_errno("asprintf");
4553 if (strcmp(path, "/") == 0) {
4554 parent_path = strdup("");
4555 if (parent_path == NULL)
4556 return got_error_from_errno("strdup");
4558 err = got_path_dirname(&parent_path, path);
4563 ct = calloc(1, sizeof(*ct));
4565 err = got_error_from_errno("calloc");
4569 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4571 err = got_error_from_errno("asprintf");
4575 if (staged_status == GOT_STATUS_ADD ||
4576 staged_status == GOT_STATUS_MODIFY) {
4577 struct got_fileindex_entry *ie;
4578 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4579 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4580 case GOT_FILEIDX_MODE_REGULAR_FILE:
4581 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4584 case GOT_FILEIDX_MODE_SYMLINK:
4588 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4591 ct->mode |= got_fileindex_entry_perms_get(ie);
4592 } else if (status != GOT_STATUS_DELETE &&
4593 staged_status != GOT_STATUS_DELETE) {
4595 if (fstatat(dirfd, de_name, &sb,
4596 AT_SYMLINK_NOFOLLOW) == -1) {
4597 err = got_error_from_errno2("fstatat",
4601 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4602 err = got_error_from_errno2("lstat", ct->ondisk_path);
4605 ct->mode = sb.st_mode;
4608 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4609 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4611 err = got_error_from_errno("asprintf");
4615 ct->status = status;
4616 ct->staged_status = staged_status;
4617 ct->blob_id = NULL; /* will be filled in when blob gets created */
4618 if (ct->status != GOT_STATUS_ADD &&
4619 ct->staged_status != GOT_STATUS_ADD) {
4620 ct->base_blob_id = got_object_id_dup(blob_id);
4621 if (ct->base_blob_id == NULL) {
4622 err = got_error_from_errno("got_object_id_dup");
4625 ct->base_commit_id = got_object_id_dup(commit_id);
4626 if (ct->base_commit_id == NULL) {
4627 err = got_error_from_errno("got_object_id_dup");
4631 if (ct->staged_status == GOT_STATUS_ADD ||
4632 ct->staged_status == GOT_STATUS_MODIFY) {
4633 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4634 if (ct->staged_blob_id == NULL) {
4635 err = got_error_from_errno("got_object_id_dup");
4639 ct->path = strdup(path);
4640 if (ct->path == NULL) {
4641 err = got_error_from_errno("strdup");
4644 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4646 if (ct && (err || new == NULL))
4647 free_commitable(ct);
4653 static const struct got_error *write_tree(struct got_object_id **, int *,
4654 struct got_tree_object *, const char *, struct got_pathlist_head *,
4655 got_worktree_status_cb status_cb, void *status_arg,
4656 struct got_repository *);
4658 static const struct got_error *
4659 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4660 struct got_tree_entry *te, const char *parent_path,
4661 struct got_pathlist_head *commitable_paths,
4662 got_worktree_status_cb status_cb, void *status_arg,
4663 struct got_repository *repo)
4665 const struct got_error *err = NULL;
4666 struct got_tree_object *subtree;
4669 if (asprintf(&subpath, "%s%s%s", parent_path,
4670 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4671 return got_error_from_errno("asprintf");
4673 err = got_object_open_as_tree(&subtree, repo, &te->id);
4677 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4678 commitable_paths, status_cb, status_arg, repo);
4679 got_object_tree_close(subtree);
4684 static const struct got_error *
4685 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4687 const struct got_error *err = NULL;
4688 char *ct_parent_path = NULL;
4692 if (strchr(ct->in_repo_path, '/') == NULL) {
4693 *match = got_path_is_root_dir(path);
4697 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4700 *match = (strcmp(path, ct_parent_path) == 0);
4701 free(ct_parent_path);
4706 get_ct_file_mode(struct got_commitable *ct)
4708 if (S_ISLNK(ct->mode))
4711 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4714 static const struct got_error *
4715 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4716 struct got_tree_entry *te, struct got_commitable *ct)
4718 const struct got_error *err = NULL;
4722 err = got_object_tree_entry_dup(new_te, te);
4726 (*new_te)->mode = get_ct_file_mode(ct);
4728 if (ct->staged_status == GOT_STATUS_MODIFY)
4729 memcpy(&(*new_te)->id, ct->staged_blob_id,
4730 sizeof((*new_te)->id));
4732 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4734 if (err && *new_te) {
4741 static const struct got_error *
4742 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4743 struct got_commitable *ct)
4745 const struct got_error *err = NULL;
4750 *new_te = calloc(1, sizeof(**new_te));
4751 if (*new_te == NULL)
4752 return got_error_from_errno("calloc");
4754 ct_name = basename(ct->path);
4755 if (ct_name == NULL) {
4756 err = got_error_from_errno2("basename", ct->path);
4759 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4760 sizeof((*new_te)->name)) {
4761 err = got_error(GOT_ERR_NO_SPACE);
4765 (*new_te)->mode = get_ct_file_mode(ct);
4767 if (ct->staged_status == GOT_STATUS_ADD)
4768 memcpy(&(*new_te)->id, ct->staged_blob_id,
4769 sizeof((*new_te)->id));
4771 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4773 if (err && *new_te) {
4780 static const struct got_error *
4781 insert_tree_entry(struct got_tree_entry *new_te,
4782 struct got_pathlist_head *paths)
4784 const struct got_error *err = NULL;
4785 struct got_pathlist_entry *new_pe;
4787 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4791 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4795 static const struct got_error *
4796 report_ct_status(struct got_commitable *ct,
4797 got_worktree_status_cb status_cb, void *status_arg)
4799 const char *ct_path = ct->path;
4800 unsigned char status;
4802 while (ct_path[0] == '/')
4805 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4806 status = ct->staged_status;
4808 status = ct->status;
4810 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4811 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4814 static const struct got_error *
4815 match_modified_subtree(int *modified, struct got_tree_entry *te,
4816 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4818 const struct got_error *err = NULL;
4819 struct got_pathlist_entry *pe;
4824 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4825 got_path_is_root_dir(base_tree_path) ? "" : "/",
4827 return got_error_from_errno("asprintf");
4829 TAILQ_FOREACH(pe, commitable_paths, entry) {
4830 struct got_commitable *ct = pe->data;
4831 *modified = got_path_is_child(ct->in_repo_path, te_path,
4841 static const struct got_error *
4842 match_deleted_or_modified_ct(struct got_commitable **ctp,
4843 struct got_tree_entry *te, const char *base_tree_path,
4844 struct got_pathlist_head *commitable_paths)
4846 const struct got_error *err = NULL;
4847 struct got_pathlist_entry *pe;
4851 TAILQ_FOREACH(pe, commitable_paths, entry) {
4852 struct got_commitable *ct = pe->data;
4853 char *ct_name = NULL;
4856 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4857 if (ct->status != GOT_STATUS_MODIFY &&
4858 ct->status != GOT_STATUS_MODE_CHANGE &&
4859 ct->status != GOT_STATUS_DELETE)
4862 if (ct->staged_status != GOT_STATUS_MODIFY &&
4863 ct->staged_status != GOT_STATUS_DELETE)
4867 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4870 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4876 ct_name = basename(pe->path);
4877 if (ct_name == NULL)
4878 return got_error_from_errno2("basename", pe->path);
4880 if (strcmp(te->name, ct_name) != 0)
4890 static const struct got_error *
4891 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4892 const char *child_path, const char *path_base_tree,
4893 struct got_pathlist_head *commitable_paths,
4894 got_worktree_status_cb status_cb, void *status_arg,
4895 struct got_repository *repo)
4897 const struct got_error *err = NULL;
4898 struct got_tree_entry *new_te;
4900 struct got_object_id *id = NULL;
4905 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4906 got_path_is_root_dir(path_base_tree) ? "" : "/",
4908 return got_error_from_errno("asprintf");
4910 new_te = calloc(1, sizeof(*new_te));
4912 return got_error_from_errno("calloc");
4913 new_te->mode = S_IFDIR;
4915 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4916 sizeof(new_te->name)) {
4917 err = got_error(GOT_ERR_NO_SPACE);
4920 err = write_tree(&id, &nentries, NULL, subtree_path,
4921 commitable_paths, status_cb, status_arg, repo);
4926 memcpy(&new_te->id, id, sizeof(new_te->id));
4935 static const struct got_error *
4936 write_tree(struct got_object_id **new_tree_id, int *nentries,
4937 struct got_tree_object *base_tree, const char *path_base_tree,
4938 struct got_pathlist_head *commitable_paths,
4939 got_worktree_status_cb status_cb, void *status_arg,
4940 struct got_repository *repo)
4942 const struct got_error *err = NULL;
4943 struct got_pathlist_head paths;
4944 struct got_tree_entry *te, *new_te = NULL;
4945 struct got_pathlist_entry *pe;
4950 /* Insert, and recurse into, newly added entries first. */
4951 TAILQ_FOREACH(pe, commitable_paths, entry) {
4952 struct got_commitable *ct = pe->data;
4953 char *child_path = NULL, *slash;
4955 if ((ct->status != GOT_STATUS_ADD &&
4956 ct->staged_status != GOT_STATUS_ADD) ||
4957 (ct->flags & GOT_COMMITABLE_ADDED))
4960 if (!got_path_is_child(pe->path, path_base_tree,
4961 strlen(path_base_tree)))
4964 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4969 slash = strchr(child_path, '/');
4970 if (slash == NULL) {
4971 err = alloc_added_blob_tree_entry(&new_te, ct);
4974 err = report_ct_status(ct, status_cb, status_arg);
4977 ct->flags |= GOT_COMMITABLE_ADDED;
4978 err = insert_tree_entry(new_te, &paths);
4983 *slash = '\0'; /* trim trailing path components */
4984 if (base_tree == NULL ||
4985 got_object_tree_find_entry(base_tree, child_path)
4987 err = make_subtree_for_added_blob(&new_te,
4988 child_path, path_base_tree,
4989 commitable_paths, status_cb, status_arg,
4993 err = insert_tree_entry(new_te, &paths);
5002 int i, nbase_entries;
5003 /* Handle modified and deleted entries. */
5004 nbase_entries = got_object_tree_get_nentries(base_tree);
5005 for (i = 0; i < nbase_entries; i++) {
5006 struct got_commitable *ct = NULL;
5008 te = got_object_tree_get_entry(base_tree, i);
5009 if (got_object_tree_entry_is_submodule(te)) {
5010 /* Entry is a submodule; just copy it. */
5011 err = got_object_tree_entry_dup(&new_te, te);
5014 err = insert_tree_entry(new_te, &paths);
5021 if (S_ISDIR(te->mode)) {
5023 err = got_object_tree_entry_dup(&new_te, te);
5026 err = match_modified_subtree(&modified, te,
5027 path_base_tree, commitable_paths);
5030 /* Avoid recursion into unmodified subtrees. */
5032 struct got_object_id *new_id;
5034 err = write_subtree(&new_id,
5036 path_base_tree, commitable_paths,
5037 status_cb, status_arg, repo);
5040 if (nsubentries == 0) {
5041 /* All entries were deleted. */
5045 memcpy(&new_te->id, new_id,
5046 sizeof(new_te->id));
5049 err = insert_tree_entry(new_te, &paths);
5056 err = match_deleted_or_modified_ct(&ct, te,
5057 path_base_tree, commitable_paths);
5061 /* NB: Deleted entries get dropped here. */
5062 if (ct->status == GOT_STATUS_MODIFY ||
5063 ct->status == GOT_STATUS_MODE_CHANGE ||
5064 ct->staged_status == GOT_STATUS_MODIFY) {
5065 err = alloc_modified_blob_tree_entry(
5069 err = insert_tree_entry(new_te, &paths);
5074 err = report_ct_status(ct, status_cb,
5079 /* Entry is unchanged; just copy it. */
5080 err = got_object_tree_entry_dup(&new_te, te);
5083 err = insert_tree_entry(new_te, &paths);
5091 /* Write new list of entries; deleted entries have been dropped. */
5092 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5094 got_pathlist_free(&paths);
5098 static const struct got_error *
5099 reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
5100 struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
5101 struct got_fileindex_entry *ie, struct got_repository *repo)
5103 const struct got_error *err = NULL;
5104 struct got_blob_object *blob = NULL;
5105 struct got_object_id *tree_id = NULL;
5106 char *tree_path = NULL;
5107 struct got_tree_object *tree = NULL;
5108 struct got_tree_entry *te;
5110 unsigned char status;
5113 err = get_file_status(&status, &sb, ie, ct->ondisk_path,
5117 if (status != GOT_STATUS_NO_CHANGE)
5120 if (ct->staged_status == GOT_STATUS_ADD ||
5121 ct->staged_status == GOT_STATUS_MODIFY) {
5122 err = got_object_open_as_blob(&blob, repo, ct->staged_blob_id,
5127 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5133 err = got_path_dirname(&tree_path, ct->in_repo_path);
5135 if (err->code != GOT_ERR_BAD_PATH)
5137 err = got_object_id_by_path(&tree_id, repo,
5138 new_base_commit_id, "");
5142 err = got_object_id_by_path(&tree_id, repo,
5143 new_base_commit_id, tree_path);
5148 err = got_object_open_as_tree(&tree, repo, tree_id);
5152 entry_name = basename(ct->path);
5153 if (entry_name == NULL) {
5154 err = got_error_from_errno2("basename", ct->path);
5158 te = got_object_tree_find_entry(tree, entry_name);
5160 err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5164 err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5165 ct->path, blob, 0, 0, 0, repo, NULL, NULL);
5168 got_object_blob_close(blob);
5170 got_object_tree_close(tree);
5177 * After comitting a symlink we have a chance to convert "bad" symlinks
5178 * (those which point outside the work tree or into .got) to regular files.
5179 * This way, the post-commit work tree state matches a fresh checkout of
5180 * the tree which was just committed. We also mark such newly committed
5181 * symlinks as "bad" in the work tree's fileindex.
5183 static const struct got_error *
5184 reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5185 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5186 struct got_worktree *worktree, struct got_repository *repo)
5188 const struct got_error *err = NULL;
5189 struct got_pathlist_entry *pe;
5191 TAILQ_FOREACH(pe, commitable_paths, entry) {
5192 struct got_commitable *ct = pe->data;
5193 struct got_fileindex_entry *ie;
5194 int is_bad_symlink = 0;
5196 if (!S_ISLNK(get_ct_file_mode(ct)) ||
5197 ct->staged_status == GOT_STATUS_DELETE ||
5198 ct->status == GOT_STATUS_DELETE)
5201 ie = got_fileindex_entry_get(fileindex, ct->path,
5204 err = got_error_path(ct->path, GOT_ERR_BAD_PATH);
5207 err = reinstall_symlink_after_commit(&is_bad_symlink,
5208 ct, new_base_commit_id, worktree, ie, repo);
5211 if (ie && is_bad_symlink) {
5212 got_fileindex_entry_filetype_set(ie,
5213 GOT_FILEIDX_MODE_BAD_SYMLINK);
5220 static const struct got_error *
5221 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5222 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5223 int have_staged_files)
5225 const struct got_error *err = NULL;
5226 struct got_pathlist_entry *pe;
5228 TAILQ_FOREACH(pe, commitable_paths, entry) {
5229 struct got_fileindex_entry *ie;
5230 struct got_commitable *ct = pe->data;
5232 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5234 if (ct->status == GOT_STATUS_DELETE ||
5235 ct->staged_status == GOT_STATUS_DELETE) {
5236 got_fileindex_entry_remove(fileindex, ie);
5237 } else if (ct->staged_status == GOT_STATUS_ADD ||
5238 ct->staged_status == GOT_STATUS_MODIFY) {
5239 got_fileindex_entry_stage_set(ie,
5240 GOT_FILEIDX_STAGE_NONE);
5241 err = got_fileindex_entry_update(ie,
5242 ct->ondisk_path, ct->staged_blob_id->sha1,
5243 new_base_commit_id->sha1,
5244 !have_staged_files);
5246 err = got_fileindex_entry_update(ie,
5247 ct->ondisk_path, ct->blob_id->sha1,
5248 new_base_commit_id->sha1,
5249 !have_staged_files);
5251 err = got_fileindex_entry_alloc(&ie, pe->path);
5254 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5255 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5257 got_fileindex_entry_free(ie);
5260 err = got_fileindex_entry_add(fileindex, ie);
5262 got_fileindex_entry_free(ie);
5271 static const struct got_error *
5272 check_out_of_date(const char *in_repo_path, unsigned char status,
5273 unsigned char staged_status, struct got_object_id *base_blob_id,
5274 struct got_object_id *base_commit_id,
5275 struct got_object_id *head_commit_id, struct got_repository *repo,
5278 const struct got_error *err = NULL;
5279 struct got_object_id *id = NULL;
5281 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5282 /* Trivial case: base commit == head commit */
5283 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5286 * Ensure file content which local changes were based
5287 * on matches file content in the branch head.
5289 err = got_object_id_by_path(&id, repo, head_commit_id,
5292 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5293 err = got_error(ood_errcode);
5295 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5296 err = got_error(ood_errcode);
5298 /* Require that added files don't exist in the branch head. */
5299 err = got_object_id_by_path(&id, repo, head_commit_id,
5301 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5303 err = id ? got_error(ood_errcode) : NULL;
5310 const struct got_error *
5311 commit_worktree(struct got_object_id **new_commit_id,
5312 struct got_pathlist_head *commitable_paths,
5313 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5314 const char *author, const char *committer,
5315 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5316 got_worktree_status_cb status_cb, void *status_arg,
5317 struct got_repository *repo)
5319 const struct got_error *err = NULL, *unlockerr = NULL;
5320 struct got_pathlist_entry *pe;
5321 const char *head_ref_name = NULL;
5322 struct got_commit_object *head_commit = NULL;
5323 struct got_reference *head_ref2 = NULL;
5324 struct got_object_id *head_commit_id2 = NULL;
5325 struct got_tree_object *head_tree = NULL;
5326 struct got_object_id *new_tree_id = NULL;
5328 struct got_object_id_queue parent_ids;
5329 struct got_object_qid *pid = NULL;
5330 char *logmsg = NULL;
5332 *new_commit_id = NULL;
5334 SIMPLEQ_INIT(&parent_ids);
5336 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5340 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5344 if (commit_msg_cb != NULL) {
5345 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5350 if (logmsg == NULL || strlen(logmsg) == 0) {
5351 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5355 /* Create blobs from added and modified files and record their IDs. */
5356 TAILQ_FOREACH(pe, commitable_paths, entry) {
5357 struct got_commitable *ct = pe->data;
5360 /* Blobs for staged files already exist. */
5361 if (ct->staged_status == GOT_STATUS_ADD ||
5362 ct->staged_status == GOT_STATUS_MODIFY)
5365 if (ct->status != GOT_STATUS_ADD &&
5366 ct->status != GOT_STATUS_MODIFY &&
5367 ct->status != GOT_STATUS_MODE_CHANGE)
5370 if (asprintf(&ondisk_path, "%s/%s",
5371 worktree->root_path, pe->path) == -1) {
5372 err = got_error_from_errno("asprintf");
5375 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5381 /* Recursively write new tree objects. */
5382 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5383 commitable_paths, status_cb, status_arg, repo);
5387 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5390 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5391 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5392 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5393 got_object_qid_free(pid);
5399 /* Check if a concurrent commit to our branch has occurred. */
5400 head_ref_name = got_worktree_get_head_ref_name(worktree);
5401 if (head_ref_name == NULL) {
5402 err = got_error_from_errno("got_worktree_get_head_ref_name");
5405 /* Lock the reference here to prevent concurrent modification. */
5406 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5409 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5412 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5413 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5416 /* Update branch head in repository. */
5417 err = got_ref_change_ref(head_ref2, *new_commit_id);
5420 err = got_ref_write(head_ref2, repo);
5424 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5428 err = ref_base_commit(worktree, repo);
5433 got_object_tree_close(head_tree);
5435 got_object_commit_close(head_commit);
5436 free(head_commit_id2);
5438 unlockerr = got_ref_unlock(head_ref2);
5439 if (unlockerr && err == NULL)
5441 got_ref_close(head_ref2);
5446 static const struct got_error *
5447 check_path_is_commitable(const char *path,
5448 struct got_pathlist_head *commitable_paths)
5450 struct got_pathlist_entry *cpe = NULL;
5451 size_t path_len = strlen(path);
5453 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5454 struct got_commitable *ct = cpe->data;
5455 const char *ct_path = ct->path;
5457 while (ct_path[0] == '/')
5460 if (strcmp(path, ct_path) == 0 ||
5461 got_path_is_child(ct_path, path, path_len))
5466 return got_error_path(path, GOT_ERR_BAD_PATH);
5471 static const struct got_error *
5472 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5474 int *have_staged_files = arg;
5476 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5477 *have_staged_files = 1;
5478 return got_error(GOT_ERR_CANCELLED);
5484 static const struct got_error *
5485 check_non_staged_files(struct got_fileindex *fileindex,
5486 struct got_pathlist_head *paths)
5488 struct got_pathlist_entry *pe;
5489 struct got_fileindex_entry *ie;
5491 TAILQ_FOREACH(pe, paths, entry) {
5492 if (pe->path[0] == '\0')
5494 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5496 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5497 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5498 return got_error_path(pe->path,
5499 GOT_ERR_FILE_NOT_STAGED);
5505 const struct got_error *
5506 got_worktree_commit(struct got_object_id **new_commit_id,
5507 struct got_worktree *worktree, struct got_pathlist_head *paths,
5508 const char *author, const char *committer,
5509 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5510 got_worktree_status_cb status_cb, void *status_arg,
5511 struct got_repository *repo)
5513 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5514 struct got_fileindex *fileindex = NULL;
5515 char *fileindex_path = NULL;
5516 struct got_pathlist_head commitable_paths;
5517 struct collect_commitables_arg cc_arg;
5518 struct got_pathlist_entry *pe;
5519 struct got_reference *head_ref = NULL;
5520 struct got_object_id *head_commit_id = NULL;
5521 int have_staged_files = 0;
5523 *new_commit_id = NULL;
5525 TAILQ_INIT(&commitable_paths);
5527 err = lock_worktree(worktree, LOCK_EX);
5531 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5535 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5539 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5543 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5544 &have_staged_files);
5545 if (err && err->code != GOT_ERR_CANCELLED)
5547 if (have_staged_files) {
5548 err = check_non_staged_files(fileindex, paths);
5553 cc_arg.commitable_paths = &commitable_paths;
5554 cc_arg.worktree = worktree;
5555 cc_arg.fileindex = fileindex;
5557 cc_arg.have_staged_files = have_staged_files;
5558 TAILQ_FOREACH(pe, paths, entry) {
5559 err = worktree_status(worktree, pe->path, fileindex, repo,
5560 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5565 if (TAILQ_EMPTY(&commitable_paths)) {
5566 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5570 TAILQ_FOREACH(pe, paths, entry) {
5571 err = check_path_is_commitable(pe->path, &commitable_paths);
5576 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5577 struct got_commitable *ct = pe->data;
5578 const char *ct_path = ct->in_repo_path;
5580 while (ct_path[0] == '/')
5582 err = check_out_of_date(ct_path, ct->status,
5583 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5584 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5590 err = commit_worktree(new_commit_id, &commitable_paths,
5591 head_commit_id, worktree, author, committer,
5592 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5596 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5597 fileindex, have_staged_files);
5599 err = reinstall_symlinks_after_commit(&commitable_paths,
5600 *new_commit_id, fileindex, worktree, repo);
5602 sync_err = sync_fileindex(fileindex, fileindex_path);
5603 if (sync_err && err == NULL)
5607 got_fileindex_free(fileindex);
5608 free(fileindex_path);
5609 unlockerr = lock_worktree(worktree, LOCK_SH);
5610 if (unlockerr && err == NULL)
5612 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5613 struct got_commitable *ct = pe->data;
5614 free_commitable(ct);
5616 got_pathlist_free(&commitable_paths);
5621 got_commitable_get_path(struct got_commitable *ct)
5627 got_commitable_get_status(struct got_commitable *ct)
5632 struct check_rebase_ok_arg {
5633 struct got_worktree *worktree;
5634 struct got_repository *repo;
5637 static const struct got_error *
5638 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5640 const struct got_error *err = NULL;
5641 struct check_rebase_ok_arg *a = arg;
5642 unsigned char status;
5646 /* Reject rebase of a work tree with mixed base commits. */
5647 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5648 SHA1_DIGEST_LENGTH))
5649 return got_error(GOT_ERR_MIXED_COMMITS);
5651 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5653 return got_error_from_errno("asprintf");
5655 /* Reject rebase of a work tree with modified or staged files. */
5656 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5661 if (status != GOT_STATUS_NO_CHANGE)
5662 return got_error(GOT_ERR_MODIFIED);
5663 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5664 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5669 const struct got_error *
5670 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5671 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5672 struct got_worktree *worktree, struct got_reference *branch,
5673 struct got_repository *repo)
5675 const struct got_error *err = NULL;
5676 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5677 char *branch_ref_name = NULL;
5678 char *fileindex_path = NULL;
5679 struct check_rebase_ok_arg ok_arg;
5680 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5681 struct got_object_id *wt_branch_tip = NULL;
5683 *new_base_branch_ref = NULL;
5687 err = lock_worktree(worktree, LOCK_EX);
5691 err = open_fileindex(fileindex, &fileindex_path, worktree);
5695 ok_arg.worktree = worktree;
5697 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5702 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5706 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5710 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5714 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5719 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5722 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5723 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5727 err = got_ref_alloc_symref(new_base_branch_ref,
5728 new_base_branch_ref_name, wt_branch);
5731 err = got_ref_write(*new_base_branch_ref, repo);
5735 /* TODO Lock original branch's ref while rebasing? */
5737 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5741 err = got_ref_write(branch_ref, repo);
5745 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5746 worktree->base_commit_id);
5749 err = got_ref_write(*tmp_branch, repo);
5753 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5757 free(fileindex_path);
5758 free(tmp_branch_name);
5759 free(new_base_branch_ref_name);
5760 free(branch_ref_name);
5762 got_ref_close(branch_ref);
5764 got_ref_close(wt_branch);
5765 free(wt_branch_tip);
5767 if (*new_base_branch_ref) {
5768 got_ref_close(*new_base_branch_ref);
5769 *new_base_branch_ref = NULL;
5772 got_ref_close(*tmp_branch);
5776 got_fileindex_free(*fileindex);
5779 lock_worktree(worktree, LOCK_SH);
5784 const struct got_error *
5785 got_worktree_rebase_continue(struct got_object_id **commit_id,
5786 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5787 struct got_reference **branch, struct got_fileindex **fileindex,
5788 struct got_worktree *worktree, struct got_repository *repo)
5790 const struct got_error *err;
5791 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5792 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5793 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5794 char *fileindex_path = NULL;
5795 int have_staged_files = 0;
5798 *new_base_branch = NULL;
5803 err = lock_worktree(worktree, LOCK_EX);
5807 err = open_fileindex(fileindex, &fileindex_path, worktree);
5811 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5812 &have_staged_files);
5813 if (err && err->code != GOT_ERR_CANCELLED)
5815 if (have_staged_files) {
5816 err = got_error(GOT_ERR_STAGED_PATHS);
5820 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5824 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5828 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5832 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5836 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5840 err = got_ref_open(branch, repo,
5841 got_ref_get_symref_target(branch_ref), 0);
5845 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5849 err = got_ref_resolve(commit_id, repo, commit_ref);
5853 err = got_ref_open(new_base_branch, repo,
5854 new_base_branch_ref_name, 0);
5858 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5862 free(commit_ref_name);
5863 free(branch_ref_name);
5864 free(fileindex_path);
5866 got_ref_close(commit_ref);
5868 got_ref_close(branch_ref);
5873 got_ref_close(*tmp_branch);
5876 if (*new_base_branch) {
5877 got_ref_close(*new_base_branch);
5878 *new_base_branch = NULL;
5881 got_ref_close(*branch);
5885 got_fileindex_free(*fileindex);
5888 lock_worktree(worktree, LOCK_SH);
5893 const struct got_error *
5894 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5896 const struct got_error *err;
5897 char *tmp_branch_name = NULL;
5899 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5903 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5904 free(tmp_branch_name);
5908 static const struct got_error *
5909 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5910 char **logmsg, void *arg)
5916 static const struct got_error *
5917 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5918 const char *path, struct got_object_id *blob_id,
5919 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5920 int dirfd, const char *de_name)
5925 struct collect_merged_paths_arg {
5926 got_worktree_checkout_cb progress_cb;
5928 struct got_pathlist_head *merged_paths;
5931 static const struct got_error *
5932 collect_merged_paths(void *arg, unsigned char status, const char *path)
5934 const struct got_error *err;
5935 struct collect_merged_paths_arg *a = arg;
5937 struct got_pathlist_entry *new;
5939 err = (*a->progress_cb)(a->progress_arg, status, path);
5943 if (status != GOT_STATUS_MERGE &&
5944 status != GOT_STATUS_ADD &&
5945 status != GOT_STATUS_DELETE &&
5946 status != GOT_STATUS_CONFLICT)
5951 return got_error_from_errno("strdup");
5953 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5954 if (err || new == NULL)
5960 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5962 struct got_pathlist_entry *pe;
5964 TAILQ_FOREACH(pe, merged_paths, entry)
5965 free((char *)pe->path);
5967 got_pathlist_free(merged_paths);
5970 static const struct got_error *
5971 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5972 int is_rebase, struct got_repository *repo)
5974 const struct got_error *err;
5975 struct got_reference *commit_ref = NULL;
5977 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5979 if (err->code != GOT_ERR_NOT_REF)
5981 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5984 err = got_ref_write(commit_ref, repo);
5987 } else if (is_rebase) {
5988 struct got_object_id *stored_id;
5991 err = got_ref_resolve(&stored_id, repo, commit_ref);
5994 cmp = got_object_id_cmp(commit_id, stored_id);
5997 err = got_error(GOT_ERR_REBASE_COMMITID);
6003 got_ref_close(commit_ref);
6007 static const struct got_error *
6008 rebase_merge_files(struct got_pathlist_head *merged_paths,
6009 const char *commit_ref_name, struct got_worktree *worktree,
6010 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6011 struct got_object_id *commit_id, struct got_repository *repo,
6012 got_worktree_checkout_cb progress_cb, void *progress_arg,
6013 got_cancel_cb cancel_cb, void *cancel_arg)
6015 const struct got_error *err;
6016 struct got_reference *commit_ref = NULL;
6017 struct collect_merged_paths_arg cmp_arg;
6018 char *fileindex_path;
6020 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6022 err = get_fileindex_path(&fileindex_path, worktree);
6026 cmp_arg.progress_cb = progress_cb;
6027 cmp_arg.progress_arg = progress_arg;
6028 cmp_arg.merged_paths = merged_paths;
6029 err = merge_files(worktree, fileindex, fileindex_path,
6030 parent_commit_id, commit_id, repo, collect_merged_paths,
6031 &cmp_arg, cancel_cb, cancel_arg);
6033 got_ref_close(commit_ref);
6037 const struct got_error *
6038 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6039 struct got_worktree *worktree, struct got_fileindex *fileindex,
6040 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6041 struct got_repository *repo,
6042 got_worktree_checkout_cb progress_cb, void *progress_arg,
6043 got_cancel_cb cancel_cb, void *cancel_arg)
6045 const struct got_error *err;
6046 char *commit_ref_name;
6048 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6052 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6056 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6057 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6058 progress_arg, cancel_cb, cancel_arg);
6060 free(commit_ref_name);
6064 const struct got_error *
6065 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6066 struct got_worktree *worktree, struct got_fileindex *fileindex,
6067 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6068 struct got_repository *repo,
6069 got_worktree_checkout_cb progress_cb, void *progress_arg,
6070 got_cancel_cb cancel_cb, void *cancel_arg)
6072 const struct got_error *err;
6073 char *commit_ref_name;
6075 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6079 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6083 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6084 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6085 progress_arg, cancel_cb, cancel_arg);
6087 free(commit_ref_name);
6091 static const struct got_error *
6092 rebase_commit(struct got_object_id **new_commit_id,
6093 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6094 struct got_worktree *worktree, struct got_fileindex *fileindex,
6095 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6096 const char *new_logmsg, struct got_repository *repo)
6098 const struct got_error *err, *sync_err;
6099 struct got_pathlist_head commitable_paths;
6100 struct collect_commitables_arg cc_arg;
6101 char *fileindex_path = NULL;
6102 struct got_reference *head_ref = NULL;
6103 struct got_object_id *head_commit_id = NULL;
6104 char *logmsg = NULL;
6106 TAILQ_INIT(&commitable_paths);
6107 *new_commit_id = NULL;
6109 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6111 err = get_fileindex_path(&fileindex_path, worktree);
6115 cc_arg.commitable_paths = &commitable_paths;
6116 cc_arg.worktree = worktree;
6118 cc_arg.have_staged_files = 0;
6120 * If possible get the status of individual files directly to
6121 * avoid crawling the entire work tree once per rebased commit.
6122 * TODO: Ideally, merged_paths would contain a list of commitables
6123 * we could use so we could skip worktree_status() entirely.
6126 struct got_pathlist_entry *pe;
6127 TAILQ_FOREACH(pe, merged_paths, entry) {
6128 err = worktree_status(worktree, pe->path, fileindex,
6129 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6135 err = worktree_status(worktree, "", fileindex, repo,
6136 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6141 if (TAILQ_EMPTY(&commitable_paths)) {
6142 /* No-op change; commit will be elided. */
6143 err = got_ref_delete(commit_ref, repo);
6146 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6150 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6154 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6159 logmsg = strdup(new_logmsg);
6160 if (logmsg == NULL) {
6161 err = got_error_from_errno("strdup");
6165 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6170 /* NB: commit_worktree will call free(logmsg) */
6171 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6172 worktree, got_object_commit_get_author(orig_commit),
6173 got_object_commit_get_committer(orig_commit),
6174 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6178 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6182 err = got_ref_delete(commit_ref, repo);
6186 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6188 sync_err = sync_fileindex(fileindex, fileindex_path);
6189 if (sync_err && err == NULL)
6192 free(fileindex_path);
6193 free(head_commit_id);
6195 got_ref_close(head_ref);
6197 free(*new_commit_id);
6198 *new_commit_id = NULL;
6203 const struct got_error *
6204 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6205 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6206 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6207 struct got_commit_object *orig_commit,
6208 struct got_object_id *orig_commit_id, struct got_repository *repo)
6210 const struct got_error *err;
6211 char *commit_ref_name;
6212 struct got_reference *commit_ref = NULL;
6213 struct got_object_id *commit_id = NULL;
6215 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6219 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6222 err = got_ref_resolve(&commit_id, repo, commit_ref);
6225 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6226 err = got_error(GOT_ERR_REBASE_COMMITID);
6230 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6231 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6234 got_ref_close(commit_ref);
6235 free(commit_ref_name);
6240 const struct got_error *
6241 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6242 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6243 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6244 struct got_commit_object *orig_commit,
6245 struct got_object_id *orig_commit_id, const char *new_logmsg,
6246 struct got_repository *repo)
6248 const struct got_error *err;
6249 char *commit_ref_name;
6250 struct got_reference *commit_ref = NULL;
6252 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6256 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6260 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6261 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6264 got_ref_close(commit_ref);
6265 free(commit_ref_name);
6269 const struct got_error *
6270 got_worktree_rebase_postpone(struct got_worktree *worktree,
6271 struct got_fileindex *fileindex)
6274 got_fileindex_free(fileindex);
6275 return lock_worktree(worktree, LOCK_SH);
6278 static const struct got_error *
6279 delete_ref(const char *name, struct got_repository *repo)
6281 const struct got_error *err;
6282 struct got_reference *ref;
6284 err = got_ref_open(&ref, repo, name, 0);
6286 if (err->code == GOT_ERR_NOT_REF)
6291 err = got_ref_delete(ref, repo);
6296 static const struct got_error *
6297 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6299 const struct got_error *err;
6300 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6301 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6303 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6306 err = delete_ref(tmp_branch_name, repo);
6310 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6313 err = delete_ref(new_base_branch_ref_name, repo);
6317 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6320 err = delete_ref(branch_ref_name, repo);
6324 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6327 err = delete_ref(commit_ref_name, repo);
6332 free(tmp_branch_name);
6333 free(new_base_branch_ref_name);
6334 free(branch_ref_name);
6335 free(commit_ref_name);
6339 const struct got_error *
6340 got_worktree_rebase_complete(struct got_worktree *worktree,
6341 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6342 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6343 struct got_repository *repo)
6345 const struct got_error *err, *unlockerr;
6346 struct got_object_id *new_head_commit_id = NULL;
6348 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6352 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6356 err = got_ref_write(rebased_branch, repo);
6360 err = got_worktree_set_head_ref(worktree, rebased_branch);
6364 err = delete_rebase_refs(worktree, repo);
6367 got_fileindex_free(fileindex);
6368 free(new_head_commit_id);
6369 unlockerr = lock_worktree(worktree, LOCK_SH);
6370 if (unlockerr && err == NULL)
6375 const struct got_error *
6376 got_worktree_rebase_abort(struct got_worktree *worktree,
6377 struct got_fileindex *fileindex, struct got_repository *repo,
6378 struct got_reference *new_base_branch,
6379 got_worktree_checkout_cb progress_cb, void *progress_arg)
6381 const struct got_error *err, *unlockerr, *sync_err;
6382 struct got_reference *resolved = NULL;
6383 struct got_object_id *commit_id = NULL;
6384 char *fileindex_path = NULL;
6385 struct revert_file_args rfa;
6386 struct got_object_id *tree_id = NULL;
6388 err = lock_worktree(worktree, LOCK_EX);
6392 err = got_ref_open(&resolved, repo,
6393 got_ref_get_symref_target(new_base_branch), 0);
6397 err = got_worktree_set_head_ref(worktree, resolved);
6402 * XXX commits to the base branch could have happened while
6403 * we were busy rebasing; should we store the original commit ID
6404 * when rebase begins and read it back here?
6406 err = got_ref_resolve(&commit_id, repo, resolved);
6410 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6414 err = got_object_id_by_path(&tree_id, repo,
6415 worktree->base_commit_id, worktree->path_prefix);
6419 err = delete_rebase_refs(worktree, repo);
6423 err = get_fileindex_path(&fileindex_path, worktree);
6427 rfa.worktree = worktree;
6428 rfa.fileindex = fileindex;
6429 rfa.progress_cb = progress_cb;
6430 rfa.progress_arg = progress_arg;
6431 rfa.patch_cb = NULL;
6432 rfa.patch_arg = NULL;
6434 err = worktree_status(worktree, "", fileindex, repo,
6435 revert_file, &rfa, NULL, NULL, 0, 0);
6439 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6440 repo, progress_cb, progress_arg, NULL, NULL);
6442 sync_err = sync_fileindex(fileindex, fileindex_path);
6443 if (sync_err && err == NULL)
6446 got_ref_close(resolved);
6450 got_fileindex_free(fileindex);
6451 free(fileindex_path);
6453 unlockerr = lock_worktree(worktree, LOCK_SH);
6454 if (unlockerr && err == NULL)
6459 const struct got_error *
6460 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6461 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6462 struct got_fileindex **fileindex, struct got_worktree *worktree,
6463 struct got_repository *repo)
6465 const struct got_error *err = NULL;
6466 char *tmp_branch_name = NULL;
6467 char *branch_ref_name = NULL;
6468 char *base_commit_ref_name = NULL;
6469 char *fileindex_path = NULL;
6470 struct check_rebase_ok_arg ok_arg;
6471 struct got_reference *wt_branch = NULL;
6472 struct got_reference *base_commit_ref = NULL;
6476 *base_commit_id = NULL;
6479 err = lock_worktree(worktree, LOCK_EX);
6483 err = open_fileindex(fileindex, &fileindex_path, worktree);
6487 ok_arg.worktree = worktree;
6489 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6494 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6498 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6502 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6507 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6512 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6516 err = got_ref_write(*branch_ref, repo);
6520 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6521 worktree->base_commit_id);
6524 err = got_ref_write(base_commit_ref, repo);
6527 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6528 if (*base_commit_id == NULL) {
6529 err = got_error_from_errno("got_object_id_dup");
6533 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6534 worktree->base_commit_id);
6537 err = got_ref_write(*tmp_branch, repo);
6541 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6545 free(fileindex_path);
6546 free(tmp_branch_name);
6547 free(branch_ref_name);
6548 free(base_commit_ref_name);
6550 got_ref_close(wt_branch);
6553 got_ref_close(*branch_ref);
6557 got_ref_close(*tmp_branch);
6560 free(*base_commit_id);
6562 got_fileindex_free(*fileindex);
6565 lock_worktree(worktree, LOCK_SH);
6570 const struct got_error *
6571 got_worktree_histedit_postpone(struct got_worktree *worktree,
6572 struct got_fileindex *fileindex)
6575 got_fileindex_free(fileindex);
6576 return lock_worktree(worktree, LOCK_SH);
6579 const struct got_error *
6580 got_worktree_histedit_in_progress(int *in_progress,
6581 struct got_worktree *worktree)
6583 const struct got_error *err;
6584 char *tmp_branch_name = NULL;
6586 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6590 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6591 free(tmp_branch_name);
6595 const struct got_error *
6596 got_worktree_histedit_continue(struct got_object_id **commit_id,
6597 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6598 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6599 struct got_worktree *worktree, struct got_repository *repo)
6601 const struct got_error *err;
6602 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6603 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6604 struct got_reference *commit_ref = NULL;
6605 struct got_reference *base_commit_ref = NULL;
6606 char *fileindex_path = NULL;
6607 int have_staged_files = 0;
6611 *base_commit_id = NULL;
6614 err = lock_worktree(worktree, LOCK_EX);
6618 err = open_fileindex(fileindex, &fileindex_path, worktree);
6622 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6623 &have_staged_files);
6624 if (err && err->code != GOT_ERR_CANCELLED)
6626 if (have_staged_files) {
6627 err = got_error(GOT_ERR_STAGED_PATHS);
6631 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6635 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6639 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6643 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6648 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6652 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6655 err = got_ref_resolve(commit_id, repo, commit_ref);
6659 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6662 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6666 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6670 free(commit_ref_name);
6671 free(branch_ref_name);
6672 free(fileindex_path);
6674 got_ref_close(commit_ref);
6675 if (base_commit_ref)
6676 got_ref_close(base_commit_ref);
6680 free(*base_commit_id);
6681 *base_commit_id = NULL;
6683 got_ref_close(*tmp_branch);
6687 got_fileindex_free(*fileindex);
6690 lock_worktree(worktree, LOCK_EX);
6695 static const struct got_error *
6696 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6698 const struct got_error *err;
6699 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6700 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6702 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6705 err = delete_ref(tmp_branch_name, repo);
6709 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6713 err = delete_ref(base_commit_ref_name, repo);
6717 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6720 err = delete_ref(branch_ref_name, repo);
6724 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6727 err = delete_ref(commit_ref_name, repo);
6731 free(tmp_branch_name);
6732 free(base_commit_ref_name);
6733 free(branch_ref_name);
6734 free(commit_ref_name);
6738 const struct got_error *
6739 got_worktree_histedit_abort(struct got_worktree *worktree,
6740 struct got_fileindex *fileindex, struct got_repository *repo,
6741 struct got_reference *branch, struct got_object_id *base_commit_id,
6742 got_worktree_checkout_cb progress_cb, void *progress_arg)
6744 const struct got_error *err, *unlockerr, *sync_err;
6745 struct got_reference *resolved = NULL;
6746 char *fileindex_path = NULL;
6747 struct got_object_id *tree_id = NULL;
6748 struct revert_file_args rfa;
6750 err = lock_worktree(worktree, LOCK_EX);
6754 err = got_ref_open(&resolved, repo,
6755 got_ref_get_symref_target(branch), 0);
6759 err = got_worktree_set_head_ref(worktree, resolved);
6763 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6767 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6768 worktree->path_prefix);
6772 err = delete_histedit_refs(worktree, repo);
6776 err = get_fileindex_path(&fileindex_path, worktree);
6780 rfa.worktree = worktree;
6781 rfa.fileindex = fileindex;
6782 rfa.progress_cb = progress_cb;
6783 rfa.progress_arg = progress_arg;
6784 rfa.patch_cb = NULL;
6785 rfa.patch_arg = NULL;
6787 err = worktree_status(worktree, "", fileindex, repo,
6788 revert_file, &rfa, NULL, NULL, 0, 0);
6792 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6793 repo, progress_cb, progress_arg, NULL, NULL);
6795 sync_err = sync_fileindex(fileindex, fileindex_path);
6796 if (sync_err && err == NULL)
6799 got_ref_close(resolved);
6801 free(fileindex_path);
6803 unlockerr = lock_worktree(worktree, LOCK_SH);
6804 if (unlockerr && err == NULL)
6809 const struct got_error *
6810 got_worktree_histedit_complete(struct got_worktree *worktree,
6811 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6812 struct got_reference *edited_branch, struct got_repository *repo)
6814 const struct got_error *err, *unlockerr;
6815 struct got_object_id *new_head_commit_id = NULL;
6816 struct got_reference *resolved = NULL;
6818 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6822 err = got_ref_open(&resolved, repo,
6823 got_ref_get_symref_target(edited_branch), 0);
6827 err = got_ref_change_ref(resolved, new_head_commit_id);
6831 err = got_ref_write(resolved, repo);
6835 err = got_worktree_set_head_ref(worktree, resolved);
6839 err = delete_histedit_refs(worktree, repo);
6842 got_fileindex_free(fileindex);
6843 free(new_head_commit_id);
6844 unlockerr = lock_worktree(worktree, LOCK_SH);
6845 if (unlockerr && err == NULL)
6850 const struct got_error *
6851 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6852 struct got_object_id *commit_id, struct got_repository *repo)
6854 const struct got_error *err;
6855 char *commit_ref_name;
6857 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6861 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6865 err = delete_ref(commit_ref_name, repo);
6867 free(commit_ref_name);
6871 const struct got_error *
6872 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6873 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6874 struct got_worktree *worktree, const char *refname,
6875 struct got_repository *repo)
6877 const struct got_error *err = NULL;
6878 char *fileindex_path = NULL;
6879 struct check_rebase_ok_arg ok_arg;
6883 *base_branch_ref = NULL;
6885 err = lock_worktree(worktree, LOCK_EX);
6889 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6890 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6891 "cannot integrate a branch into itself; "
6892 "update -b or different branch name required");
6896 err = open_fileindex(fileindex, &fileindex_path, worktree);
6900 /* Preconditions are the same as for rebase. */
6901 ok_arg.worktree = worktree;
6903 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6908 err = got_ref_open(branch_ref, repo, refname, 1);
6912 err = got_ref_open(base_branch_ref, repo,
6913 got_worktree_get_head_ref_name(worktree), 1);
6917 got_ref_close(*branch_ref);
6920 if (*base_branch_ref) {
6921 got_ref_close(*base_branch_ref);
6922 *base_branch_ref = NULL;
6925 got_fileindex_free(*fileindex);
6928 lock_worktree(worktree, LOCK_SH);
6933 const struct got_error *
6934 got_worktree_integrate_continue(struct got_worktree *worktree,
6935 struct got_fileindex *fileindex, struct got_repository *repo,
6936 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6937 got_worktree_checkout_cb progress_cb, void *progress_arg,
6938 got_cancel_cb cancel_cb, void *cancel_arg)
6940 const struct got_error *err = NULL, *sync_err, *unlockerr;
6941 char *fileindex_path = NULL;
6942 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6944 err = get_fileindex_path(&fileindex_path, worktree);
6948 err = got_ref_resolve(&commit_id, repo, branch_ref);
6952 err = got_object_id_by_path(&tree_id, repo, commit_id,
6953 worktree->path_prefix);
6957 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6961 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6962 progress_cb, progress_arg, cancel_cb, cancel_arg);
6966 err = got_ref_change_ref(base_branch_ref, commit_id);
6970 err = got_ref_write(base_branch_ref, repo);
6972 sync_err = sync_fileindex(fileindex, fileindex_path);
6973 if (sync_err && err == NULL)
6977 unlockerr = got_ref_unlock(branch_ref);
6978 if (unlockerr && err == NULL)
6980 got_ref_close(branch_ref);
6982 unlockerr = got_ref_unlock(base_branch_ref);
6983 if (unlockerr && err == NULL)
6985 got_ref_close(base_branch_ref);
6987 got_fileindex_free(fileindex);
6988 free(fileindex_path);
6991 unlockerr = lock_worktree(worktree, LOCK_SH);
6992 if (unlockerr && err == NULL)
6997 const struct got_error *
6998 got_worktree_integrate_abort(struct got_worktree *worktree,
6999 struct got_fileindex *fileindex, struct got_repository *repo,
7000 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7002 const struct got_error *err = NULL, *unlockerr = NULL;
7004 got_fileindex_free(fileindex);
7006 err = lock_worktree(worktree, LOCK_SH);
7008 unlockerr = got_ref_unlock(branch_ref);
7009 if (unlockerr && err == NULL)
7011 got_ref_close(branch_ref);
7013 unlockerr = got_ref_unlock(base_branch_ref);
7014 if (unlockerr && err == NULL)
7016 got_ref_close(base_branch_ref);
7021 struct check_stage_ok_arg {
7022 struct got_object_id *head_commit_id;
7023 struct got_worktree *worktree;
7024 struct got_fileindex *fileindex;
7025 struct got_repository *repo;
7029 const struct got_error *
7030 check_stage_ok(void *arg, unsigned char status,
7031 unsigned char staged_status, const char *relpath,
7032 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7033 struct got_object_id *commit_id, int dirfd, const char *de_name)
7035 struct check_stage_ok_arg *a = arg;
7036 const struct got_error *err = NULL;
7037 struct got_fileindex_entry *ie;
7038 struct got_object_id base_commit_id;
7039 struct got_object_id *base_commit_idp = NULL;
7040 char *in_repo_path = NULL, *p;
7042 if (status == GOT_STATUS_UNVERSIONED ||
7043 status == GOT_STATUS_NO_CHANGE)
7045 if (status == GOT_STATUS_NONEXISTENT)
7046 return got_error_set_errno(ENOENT, relpath);
7048 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7050 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7052 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7053 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7055 return got_error_from_errno("asprintf");
7057 if (got_fileindex_entry_has_commit(ie)) {
7058 memcpy(base_commit_id.sha1, ie->commit_sha1,
7059 SHA1_DIGEST_LENGTH);
7060 base_commit_idp = &base_commit_id;
7063 if (status == GOT_STATUS_CONFLICT) {
7064 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7066 } else if (status != GOT_STATUS_ADD &&
7067 status != GOT_STATUS_MODIFY &&
7068 status != GOT_STATUS_DELETE) {
7069 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7073 a->have_changes = 1;
7078 err = check_out_of_date(p, status, staged_status,
7079 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7080 GOT_ERR_STAGE_OUT_OF_DATE);
7086 struct stage_path_arg {
7087 struct got_worktree *worktree;
7088 struct got_fileindex *fileindex;
7089 struct got_repository *repo;
7090 got_worktree_status_cb status_cb;
7092 got_worktree_patch_cb patch_cb;
7094 int staged_something;
7097 static const struct got_error *
7098 stage_path(void *arg, unsigned char status,
7099 unsigned char staged_status, const char *relpath,
7100 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7101 struct got_object_id *commit_id, int dirfd, const char *de_name)
7103 struct stage_path_arg *a = arg;
7104 const struct got_error *err = NULL;
7105 struct got_fileindex_entry *ie;
7106 char *ondisk_path = NULL, *path_content = NULL;
7108 struct got_object_id *new_staged_blob_id = NULL;
7111 if (status == GOT_STATUS_UNVERSIONED)
7114 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7116 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7118 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7120 return got_error_from_errno("asprintf");
7123 case GOT_STATUS_ADD:
7124 case GOT_STATUS_MODIFY:
7125 /* XXX could sb.st_mode be passed in by our caller? */
7126 if (lstat(ondisk_path, &sb) == -1) {
7127 err = got_error_from_errno2("lstat", ondisk_path);
7131 if (status == GOT_STATUS_ADD) {
7132 int choice = GOT_PATCH_CHOICE_NONE;
7133 err = (*a->patch_cb)(&choice, a->patch_arg,
7134 status, ie->path, NULL, 1, 1);
7137 if (choice != GOT_PATCH_CHOICE_YES)
7140 err = create_patched_content(&path_content, 0,
7141 staged_blob_id ? staged_blob_id : blob_id,
7142 ondisk_path, dirfd, de_name, ie->path,
7143 a->repo, a->patch_cb, a->patch_arg);
7144 if (err || path_content == NULL)
7148 err = got_object_blob_create(&new_staged_blob_id,
7149 path_content ? path_content : ondisk_path, a->repo);
7152 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7153 SHA1_DIGEST_LENGTH);
7154 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7155 stage = GOT_FILEIDX_STAGE_ADD;
7157 stage = GOT_FILEIDX_STAGE_MODIFY;
7158 got_fileindex_entry_stage_set(ie, stage);
7159 if (S_ISLNK(sb.st_mode)) {
7160 got_fileindex_entry_staged_filetype_set(ie,
7161 GOT_FILEIDX_MODE_SYMLINK);
7163 got_fileindex_entry_staged_filetype_set(ie,
7164 GOT_FILEIDX_MODE_REGULAR_FILE);
7166 a->staged_something = 1;
7167 if (a->status_cb == NULL)
7169 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7170 get_staged_status(ie), relpath, blob_id,
7171 new_staged_blob_id, NULL, dirfd, de_name);
7173 case GOT_STATUS_DELETE:
7174 if (staged_status == GOT_STATUS_DELETE)
7177 int choice = GOT_PATCH_CHOICE_NONE;
7178 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7179 ie->path, NULL, 1, 1);
7182 if (choice == GOT_PATCH_CHOICE_NO)
7184 if (choice != GOT_PATCH_CHOICE_YES) {
7185 err = got_error(GOT_ERR_PATCH_CHOICE);
7189 stage = GOT_FILEIDX_STAGE_DELETE;
7190 got_fileindex_entry_stage_set(ie, stage);
7191 a->staged_something = 1;
7192 if (a->status_cb == NULL)
7194 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7195 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7198 case GOT_STATUS_NO_CHANGE:
7200 case GOT_STATUS_CONFLICT:
7201 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7203 case GOT_STATUS_NONEXISTENT:
7204 err = got_error_set_errno(ENOENT, relpath);
7207 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7211 if (path_content && unlink(path_content) == -1 && err == NULL)
7212 err = got_error_from_errno2("unlink", path_content);
7215 free(new_staged_blob_id);
7219 const struct got_error *
7220 got_worktree_stage(struct got_worktree *worktree,
7221 struct got_pathlist_head *paths,
7222 got_worktree_status_cb status_cb, void *status_arg,
7223 got_worktree_patch_cb patch_cb, void *patch_arg,
7224 struct got_repository *repo)
7226 const struct got_error *err = NULL, *sync_err, *unlockerr;
7227 struct got_pathlist_entry *pe;
7228 struct got_fileindex *fileindex = NULL;
7229 char *fileindex_path = NULL;
7230 struct got_reference *head_ref = NULL;
7231 struct got_object_id *head_commit_id = NULL;
7232 struct check_stage_ok_arg oka;
7233 struct stage_path_arg spa;
7235 err = lock_worktree(worktree, LOCK_EX);
7239 err = got_ref_open(&head_ref, repo,
7240 got_worktree_get_head_ref_name(worktree), 0);
7243 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7246 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7250 /* Check pre-conditions before staging anything. */
7251 oka.head_commit_id = head_commit_id;
7252 oka.worktree = worktree;
7253 oka.fileindex = fileindex;
7255 oka.have_changes = 0;
7256 TAILQ_FOREACH(pe, paths, entry) {
7257 err = worktree_status(worktree, pe->path, fileindex, repo,
7258 check_stage_ok, &oka, NULL, NULL, 0, 0);
7262 if (!oka.have_changes) {
7263 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7267 spa.worktree = worktree;
7268 spa.fileindex = fileindex;
7270 spa.patch_cb = patch_cb;
7271 spa.patch_arg = patch_arg;
7272 spa.status_cb = status_cb;
7273 spa.status_arg = status_arg;
7274 spa.staged_something = 0;
7275 TAILQ_FOREACH(pe, paths, entry) {
7276 err = worktree_status(worktree, pe->path, fileindex, repo,
7277 stage_path, &spa, NULL, NULL, 0, 0);
7281 if (!spa.staged_something) {
7282 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7286 sync_err = sync_fileindex(fileindex, fileindex_path);
7287 if (sync_err && err == NULL)
7291 got_ref_close(head_ref);
7292 free(head_commit_id);
7293 free(fileindex_path);
7295 got_fileindex_free(fileindex);
7296 unlockerr = lock_worktree(worktree, LOCK_SH);
7297 if (unlockerr && err == NULL)
7302 struct unstage_path_arg {
7303 struct got_worktree *worktree;
7304 struct got_fileindex *fileindex;
7305 struct got_repository *repo;
7306 got_worktree_checkout_cb progress_cb;
7308 got_worktree_patch_cb patch_cb;
7312 static const struct got_error *
7313 create_unstaged_content(char **path_unstaged_content,
7314 char **path_new_staged_content, struct got_object_id *blob_id,
7315 struct got_object_id *staged_blob_id, const char *relpath,
7316 struct got_repository *repo,
7317 got_worktree_patch_cb patch_cb, void *patch_arg)
7319 const struct got_error *err;
7320 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7321 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7322 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7323 struct stat sb1, sb2;
7324 struct got_diff_changes *changes = NULL;
7325 struct got_diff_state *ds = NULL;
7326 struct got_diff_args *args = NULL;
7327 struct got_diff_change *change;
7328 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7329 int have_content = 0, have_rejected_content = 0;
7331 *path_unstaged_content = NULL;
7332 *path_new_staged_content = NULL;
7334 err = got_object_id_str(&label1, blob_id);
7337 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7341 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7345 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7349 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7353 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7357 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7361 if (stat(path1, &sb1) == -1) {
7362 err = got_error_from_errno2("stat", path1);
7366 if (stat(path2, &sb2) == -1) {
7367 err = got_error_from_errno2("stat", path2);
7371 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7372 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7376 err = got_opentemp_named(path_unstaged_content, &outfile,
7377 "got-unstaged-content");
7380 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7381 "got-new-staged-content");
7385 if (fseek(f1, 0L, SEEK_SET) == -1) {
7386 err = got_ferror(f1, GOT_ERR_IO);
7389 if (fseek(f2, 0L, SEEK_SET) == -1) {
7390 err = got_ferror(f2, GOT_ERR_IO);
7393 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7395 err = apply_or_reject_change(&choice, change, ++n,
7396 changes->nchanges, ds, args, diff_flags, relpath,
7397 f1, f2, &line_cur1, &line_cur2,
7398 outfile, rejectfile, patch_cb, patch_arg);
7401 if (choice == GOT_PATCH_CHOICE_YES)
7404 have_rejected_content = 1;
7405 if (choice == GOT_PATCH_CHOICE_QUIT)
7408 if (have_content || have_rejected_content)
7409 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7410 outfile, rejectfile);
7414 got_object_blob_close(blob);
7416 got_object_blob_close(staged_blob);
7417 if (f1 && fclose(f1) == EOF && err == NULL)
7418 err = got_error_from_errno2("fclose", path1);
7419 if (f2 && fclose(f2) == EOF && err == NULL)
7420 err = got_error_from_errno2("fclose", path2);
7421 if (outfile && fclose(outfile) == EOF && err == NULL)
7422 err = got_error_from_errno2("fclose", *path_unstaged_content);
7423 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7424 err = got_error_from_errno2("fclose", *path_new_staged_content);
7425 if (path1 && unlink(path1) == -1 && err == NULL)
7426 err = got_error_from_errno2("unlink", path1);
7427 if (path2 && unlink(path2) == -1 && err == NULL)
7428 err = got_error_from_errno2("unlink", path2);
7429 if (err || !have_content) {
7430 if (*path_unstaged_content &&
7431 unlink(*path_unstaged_content) == -1 && err == NULL)
7432 err = got_error_from_errno2("unlink",
7433 *path_unstaged_content);
7434 free(*path_unstaged_content);
7435 *path_unstaged_content = NULL;
7437 if (err || !have_rejected_content) {
7438 if (*path_new_staged_content &&
7439 unlink(*path_new_staged_content) == -1 && err == NULL)
7440 err = got_error_from_errno2("unlink",
7441 *path_new_staged_content);
7442 free(*path_new_staged_content);
7443 *path_new_staged_content = NULL;
7447 got_diff_state_free(ds);
7451 got_diff_free_changes(changes);
7457 static const struct got_error *
7458 unstage_path(void *arg, unsigned char status,
7459 unsigned char staged_status, const char *relpath,
7460 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7461 struct got_object_id *commit_id, int dirfd, const char *de_name)
7463 const struct got_error *err = NULL;
7464 struct unstage_path_arg *a = arg;
7465 struct got_fileindex_entry *ie;
7466 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7467 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7468 char *path_new_staged_content = NULL;
7469 char *id_str = NULL, *label_orig = NULL;
7470 int local_changes_subsumed;
7473 if (staged_status != GOT_STATUS_ADD &&
7474 staged_status != GOT_STATUS_MODIFY &&
7475 staged_status != GOT_STATUS_DELETE)
7478 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7480 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7482 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7484 return got_error_from_errno("asprintf");
7486 err = got_object_id_str(&id_str,
7487 commit_id ? commit_id : a->worktree->base_commit_id);
7490 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7492 err = got_error_from_errno("asprintf");
7496 switch (staged_status) {
7497 case GOT_STATUS_MODIFY:
7498 err = got_object_open_as_blob(&blob_base, a->repo,
7503 case GOT_STATUS_ADD:
7505 if (staged_status == GOT_STATUS_ADD) {
7506 int choice = GOT_PATCH_CHOICE_NONE;
7507 err = (*a->patch_cb)(&choice, a->patch_arg,
7508 staged_status, ie->path, NULL, 1, 1);
7511 if (choice != GOT_PATCH_CHOICE_YES)
7514 err = create_unstaged_content(
7515 &path_unstaged_content,
7516 &path_new_staged_content, blob_id,
7517 staged_blob_id, ie->path, a->repo,
7518 a->patch_cb, a->patch_arg);
7519 if (err || path_unstaged_content == NULL)
7521 if (path_new_staged_content) {
7522 err = got_object_blob_create(
7524 path_new_staged_content,
7528 memcpy(ie->staged_blob_sha1,
7529 staged_blob_id->sha1,
7530 SHA1_DIGEST_LENGTH);
7532 err = merge_file(&local_changes_subsumed,
7533 a->worktree, blob_base, ondisk_path,
7534 relpath, got_fileindex_perms_to_st(ie),
7535 path_unstaged_content, label_orig,
7536 "unstaged", a->repo, a->progress_cb,
7539 path_new_staged_content == NULL)
7540 got_fileindex_entry_stage_set(ie,
7541 GOT_FILEIDX_STAGE_NONE);
7542 break; /* Done with this file. */
7545 err = got_object_open_as_blob(&blob_staged, a->repo,
7546 staged_blob_id, 8192);
7549 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7550 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7551 case GOT_FILEIDX_MODE_REGULAR_FILE:
7552 err = merge_blob(&local_changes_subsumed, a->worktree,
7553 blob_base, ondisk_path, relpath,
7554 got_fileindex_perms_to_st(ie), label_orig,
7555 blob_staged, commit_id ? commit_id :
7556 a->worktree->base_commit_id, a->repo,
7557 a->progress_cb, a->progress_arg);
7559 case GOT_FILEIDX_MODE_SYMLINK:
7560 err = merge_symlink(a->worktree, blob_base, ondisk_path,
7561 relpath, got_fileindex_perms_to_st(ie), label_orig,
7562 blob_staged, a->worktree->base_commit_id, a->repo,
7563 a->progress_cb, a->progress_arg);
7566 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7570 got_fileindex_entry_stage_set(ie,
7571 GOT_FILEIDX_STAGE_NONE);
7573 case GOT_STATUS_DELETE:
7575 int choice = GOT_PATCH_CHOICE_NONE;
7576 err = (*a->patch_cb)(&choice, a->patch_arg,
7577 staged_status, ie->path, NULL, 1, 1);
7580 if (choice == GOT_PATCH_CHOICE_NO)
7582 if (choice != GOT_PATCH_CHOICE_YES) {
7583 err = got_error(GOT_ERR_PATCH_CHOICE);
7587 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7588 err = get_file_status(&status, &sb, ie, ondisk_path,
7589 dirfd, de_name, a->repo);
7592 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7597 if (path_unstaged_content &&
7598 unlink(path_unstaged_content) == -1 && err == NULL)
7599 err = got_error_from_errno2("unlink", path_unstaged_content);
7600 if (path_new_staged_content &&
7601 unlink(path_new_staged_content) == -1 && err == NULL)
7602 err = got_error_from_errno2("unlink", path_new_staged_content);
7603 free(path_unstaged_content);
7604 free(path_new_staged_content);
7606 got_object_blob_close(blob_base);
7608 got_object_blob_close(blob_staged);
7614 const struct got_error *
7615 got_worktree_unstage(struct got_worktree *worktree,
7616 struct got_pathlist_head *paths,
7617 got_worktree_checkout_cb progress_cb, void *progress_arg,
7618 got_worktree_patch_cb patch_cb, void *patch_arg,
7619 struct got_repository *repo)
7621 const struct got_error *err = NULL, *sync_err, *unlockerr;
7622 struct got_pathlist_entry *pe;
7623 struct got_fileindex *fileindex = NULL;
7624 char *fileindex_path = NULL;
7625 struct unstage_path_arg upa;
7627 err = lock_worktree(worktree, LOCK_EX);
7631 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7635 upa.worktree = worktree;
7636 upa.fileindex = fileindex;
7638 upa.progress_cb = progress_cb;
7639 upa.progress_arg = progress_arg;
7640 upa.patch_cb = patch_cb;
7641 upa.patch_arg = patch_arg;
7642 TAILQ_FOREACH(pe, paths, entry) {
7643 err = worktree_status(worktree, pe->path, fileindex, repo,
7644 unstage_path, &upa, NULL, NULL, 0, 0);
7649 sync_err = sync_fileindex(fileindex, fileindex_path);
7650 if (sync_err && err == NULL)
7653 free(fileindex_path);
7655 got_fileindex_free(fileindex);
7656 unlockerr = lock_worktree(worktree, LOCK_SH);
7657 if (unlockerr && err == NULL)