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 err = install_symlink_conflict(deriv_target,
1075 deriv_base_commit_id, ancestor_target, label_orig,
1076 ondisk_target, ondisk_path);
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1084 free(ancestor_target);
1090 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1091 * blob_deriv acts as the first derived version, and the file on disk
1092 * acts as the second derived version.
1094 static const struct got_error *
1095 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1096 struct got_blob_object *blob_orig, const char *ondisk_path,
1097 const char *path, uint16_t st_mode, const char *label_orig,
1098 struct got_blob_object *blob_deriv,
1099 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1100 got_worktree_checkout_cb progress_cb, void *progress_arg)
1102 const struct got_error *err = NULL;
1103 FILE *f_deriv = NULL;
1104 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1105 char *label_deriv = NULL, *parent;
1107 *local_changes_subsumed = 0;
1109 parent = dirname(ondisk_path);
1111 return got_error_from_errno2("dirname", ondisk_path);
1114 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1115 err = got_error_from_errno("asprintf");
1120 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1123 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1128 err = got_object_id_str(&id_str, deriv_base_commit_id);
1131 if (asprintf(&label_deriv, "%s: commit %s",
1132 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1133 err = got_error_from_errno("asprintf");
1137 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1138 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1139 label_deriv, repo, progress_cb, progress_arg);
1141 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1142 err = got_error_from_errno("fclose");
1144 if (blob_deriv_path) {
1145 unlink(blob_deriv_path);
1146 free(blob_deriv_path);
1153 static const struct got_error *
1154 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1155 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1156 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1158 const struct got_error *err = NULL;
1159 struct got_fileindex_entry *new_ie;
1163 err = got_fileindex_entry_alloc(&new_ie, path);
1167 err = got_fileindex_entry_update(new_ie, ondisk_path,
1168 blob_id->sha1, base_commit_id->sha1, 1);
1172 err = got_fileindex_entry_add(fileindex, new_ie);
1175 got_fileindex_entry_free(new_ie);
1182 get_ondisk_perms(int executable, mode_t st_mode)
1184 mode_t xbits = S_IXUSR;
1187 /* Map read bits to execute bits. */
1188 if (st_mode & S_IRGRP)
1190 if (st_mode & S_IROTH)
1192 return st_mode | xbits;
1195 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1198 /* forward declaration */
1199 static const struct got_error *
1200 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1201 const char *path, mode_t te_mode, mode_t st_mode,
1202 struct got_blob_object *blob, int restoring_missing_file,
1203 int reverting_versioned_file, int installing_bad_symlink,
1204 int path_is_unversioned, struct got_repository *repo,
1205 got_worktree_checkout_cb progress_cb, void *progress_arg);
1208 * This function assumes that the provided symlink target points at a
1209 * safe location in the work tree!
1211 static const struct got_error *
1212 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1215 const struct got_error *err = NULL;
1217 char etarget[PATH_MAX];
1221 * "Bad" symlinks (those pointing outside the work tree or into the
1222 * .got directory) are installed in the work tree as a regular file
1223 * which contains the bad symlink target path.
1224 * The new symlink target has already been checked for safety by our
1225 * caller. If we can successfully open a regular file then we simply
1226 * replace this file with a symlink below.
1228 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1231 return got_error_from_errno2("open", ondisk_path);
1233 /* We are updating an existing on-disk symlink. */
1234 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1236 return got_error_from_errno2("readlink", ondisk_path);
1238 if (elen == target_len &&
1239 memcmp(etarget, target_path, target_len) == 0)
1240 return NULL; /* nothing to do */
1243 err = update_symlink(ondisk_path, target_path, target_len);
1244 if (fd != -1 && close(fd) == -1 && err == NULL)
1245 err = got_error_from_errno2("close", ondisk_path);
1249 static const struct got_error *
1250 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1251 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1252 int restoring_missing_file, int reverting_versioned_file,
1253 int path_is_unversioned, struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *resolved_path = NULL, *abspath = NULL;
1260 char *path_got = NULL;
1261 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1262 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1264 *is_bad_symlink = 0;
1267 * Blob object content specifies the target path of the link.
1268 * If a symbolic link cannot be installed we instead create
1269 * a regular file which contains the link target path stored
1270 * in the blob object.
1273 err = got_object_blob_read_block(&len, blob);
1274 if (len + target_len >= sizeof(target_path)) {
1275 /* Path too long; install as a regular file. */
1276 *is_bad_symlink = 1;
1277 got_object_blob_rewind(blob);
1278 return install_blob(worktree, ondisk_path, path,
1279 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1280 restoring_missing_file, reverting_versioned_file,
1281 1, path_is_unversioned, repo, progress_cb,
1285 /* Skip blob object header first time around. */
1286 memcpy(target_path + target_len, buf + hdrlen,
1288 target_len += len - hdrlen;
1292 target_path[target_len] = '\0';
1295 * Relative symlink target lookup should begin at the directory
1296 * in which the blob object is being installed.
1298 if (!got_path_is_absolute(target_path)) {
1299 char *parent = dirname(ondisk_path);
1300 if (parent == NULL) {
1301 err = got_error_from_errno2("dirname", ondisk_path);
1304 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1305 err = got_error_from_errno("asprintf");
1311 * unveil(2) restricts our view of paths in the filesystem.
1312 * ENOENT will occur if a link target path does not exist or
1313 * if it points outside our unveiled path space.
1315 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1316 if (resolved_path == NULL) {
1317 if (errno != ENOENT) {
1318 err = got_error_from_errno2("realpath", target_path);
1323 /* Only allow symlinks pointing at paths within the work tree. */
1324 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1325 abspath : target_path), worktree->root_path,
1326 strlen(worktree->root_path))) {
1327 /* install as a regular file */
1328 *is_bad_symlink = 1;
1329 got_object_blob_rewind(blob);
1330 err = install_blob(worktree, ondisk_path, path,
1331 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1332 restoring_missing_file, reverting_versioned_file, 1,
1333 path_is_unversioned, repo, progress_cb, progress_arg);
1337 /* Do not allow symlinks pointing into the .got directory. */
1338 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1339 GOT_WORKTREE_GOT_DIR) == -1) {
1340 err = got_error_from_errno("asprintf");
1343 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1344 abspath : target_path), path_got, strlen(path_got))) {
1345 /* install as a regular file */
1346 *is_bad_symlink = 1;
1347 got_object_blob_rewind(blob);
1348 err = install_blob(worktree, ondisk_path, path,
1349 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1350 restoring_missing_file, reverting_versioned_file, 1,
1351 path_is_unversioned, repo, progress_cb, progress_arg);
1355 if (symlink(target_path, ondisk_path) == -1) {
1356 if (errno == EEXIST) {
1357 if (path_is_unversioned) {
1358 err = (*progress_cb)(progress_arg,
1359 GOT_STATUS_UNVERSIONED, path);
1362 err = replace_existing_symlink(ondisk_path,
1363 target_path, target_len);
1367 err = (*progress_cb)(progress_arg,
1368 GOT_STATUS_UPDATE, path);
1370 goto done; /* Nothing else to do. */
1373 if (errno == ENOENT) {
1374 char *parent = dirname(ondisk_path);
1375 if (parent == NULL) {
1376 err = got_error_from_errno2("dirname",
1380 err = add_dir_on_disk(worktree, parent);
1384 * Retry, and fall through to error handling
1385 * below if this second attempt fails.
1387 if (symlink(target_path, ondisk_path) != -1) {
1388 err = NULL; /* success */
1393 /* Handle errors from first or second creation attempt. */
1394 if (errno == ENAMETOOLONG) {
1395 /* bad target path; install as a regular file */
1396 *is_bad_symlink = 1;
1397 got_object_blob_rewind(blob);
1398 err = install_blob(worktree, ondisk_path, path,
1399 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1400 restoring_missing_file, reverting_versioned_file, 1,
1401 path_is_unversioned, repo,
1402 progress_cb, progress_arg);
1403 } else if (errno == ENOTDIR) {
1404 err = got_error_path(ondisk_path,
1405 GOT_ERR_FILE_OBSTRUCTED);
1407 err = got_error_from_errno3("symlink",
1408 target_path, ondisk_path);
1410 } else if (progress_cb)
1411 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1413 free(resolved_path);
1419 static const struct got_error *
1420 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1421 const char *path, mode_t te_mode, mode_t st_mode,
1422 struct got_blob_object *blob, int restoring_missing_file,
1423 int reverting_versioned_file, int installing_bad_symlink,
1424 int path_is_unversioned, struct got_repository *repo,
1425 got_worktree_checkout_cb progress_cb, void *progress_arg)
1427 const struct got_error *err = NULL;
1431 char *tmppath = NULL;
1433 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1434 GOT_DEFAULT_FILE_MODE);
1436 if (errno == ENOENT) {
1437 char *parent = dirname(path);
1439 return got_error_from_errno2("dirname", path);
1440 err = add_dir_on_disk(worktree, parent);
1443 fd = open(ondisk_path,
1444 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1445 GOT_DEFAULT_FILE_MODE);
1447 return got_error_from_errno2("open",
1449 } else if (errno == EEXIST) {
1450 if (path_is_unversioned) {
1451 err = (*progress_cb)(progress_arg,
1452 GOT_STATUS_UNVERSIONED, path);
1455 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1456 /* TODO file is obstructed; do something */
1457 err = got_error_path(ondisk_path,
1458 GOT_ERR_FILE_OBSTRUCTED);
1461 err = got_opentemp_named_fd(&tmppath, &fd,
1468 return got_error_from_errno2("open", ondisk_path);
1472 if (restoring_missing_file)
1473 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1475 else if (reverting_versioned_file)
1476 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1479 err = (*progress_cb)(progress_arg,
1480 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1485 hdrlen = got_object_blob_get_hdrlen(blob);
1487 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1488 err = got_object_blob_read_block(&len, blob);
1492 /* Skip blob object header first time around. */
1493 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1495 err = got_error_from_errno("write");
1497 } else if (outlen != len - hdrlen) {
1498 err = got_error(GOT_ERR_IO);
1505 if (fsync(fd) != 0) {
1506 err = got_error_from_errno("fsync");
1511 if (rename(tmppath, ondisk_path) != 0) {
1512 err = got_error_from_errno3("rename", tmppath,
1519 if (chmod(ondisk_path,
1520 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1521 err = got_error_from_errno2("chmod", ondisk_path);
1526 if (fd != -1 && close(fd) != 0 && err == NULL)
1527 err = got_error_from_errno("close");
1532 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1533 static const struct got_error *
1534 get_modified_file_content_status(unsigned char *status, FILE *f)
1536 const struct got_error *err = NULL;
1537 const char *markers[3] = {
1538 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1539 GOT_DIFF_CONFLICT_MARKER_SEP,
1540 GOT_DIFF_CONFLICT_MARKER_END
1545 const char delim[3] = {'\0', '\0', '\0'};
1547 while (*status == GOT_STATUS_MODIFY) {
1548 line = fparseln(f, &len, NULL, delim, 0);
1552 err = got_ferror(f, GOT_ERR_IO);
1556 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1557 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1559 *status = GOT_STATUS_CONFLICT;
1569 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1571 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1572 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1576 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1578 return !(ie->ctime_sec == sb->st_ctime &&
1579 ie->ctime_nsec == sb->st_ctimensec &&
1580 ie->mtime_sec == sb->st_mtime &&
1581 ie->mtime_nsec == sb->st_mtimensec &&
1582 ie->size == (sb->st_size & 0xffffffff) &&
1583 !xbit_differs(ie, sb->st_mode));
1586 static unsigned char
1587 get_staged_status(struct got_fileindex_entry *ie)
1589 switch (got_fileindex_entry_stage_get(ie)) {
1590 case GOT_FILEIDX_STAGE_ADD:
1591 return GOT_STATUS_ADD;
1592 case GOT_FILEIDX_STAGE_DELETE:
1593 return GOT_STATUS_DELETE;
1594 case GOT_FILEIDX_STAGE_MODIFY:
1595 return GOT_STATUS_MODIFY;
1597 return GOT_STATUS_NO_CHANGE;
1601 static const struct got_error *
1602 get_symlink_status(unsigned char *status, struct stat *sb,
1603 struct got_fileindex_entry *ie, const char *abspath,
1604 int dirfd, const char *de_name, struct got_blob_object *blob)
1606 const struct got_error *err = NULL;
1607 char target_path[PATH_MAX];
1608 char etarget[PATH_MAX];
1610 size_t len, target_len = 0;
1611 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1612 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1614 *status = GOT_STATUS_NO_CHANGE;
1616 /* Blob object content specifies the target path of the link. */
1618 err = got_object_blob_read_block(&len, blob);
1621 if (len + target_len >= sizeof(target_path)) {
1623 * Should not happen. The blob contents were OK
1624 * when this symlink was installed.
1626 return got_error(GOT_ERR_NO_SPACE);
1629 /* Skip blob object header first time around. */
1630 memcpy(target_path + target_len, buf + hdrlen,
1632 target_len += len - hdrlen;
1636 target_path[target_len] = '\0';
1639 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1641 return got_error_from_errno2("readlinkat", abspath);
1643 elen = readlink(abspath, etarget, sizeof(etarget));
1645 return got_error_from_errno2("readlinkat", abspath);
1648 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1649 *status = GOT_STATUS_MODIFY;
1654 static const struct got_error *
1655 get_file_status(unsigned char *status, struct stat *sb,
1656 struct got_fileindex_entry *ie, const char *abspath,
1657 int dirfd, const char *de_name, struct got_repository *repo)
1659 const struct got_error *err = NULL;
1660 struct got_object_id id;
1665 struct got_blob_object *blob = NULL;
1667 unsigned char staged_status = get_staged_status(ie);
1669 *status = GOT_STATUS_NO_CHANGE;
1672 * Whenever the caller provides a directory descriptor and a
1673 * directory entry name for the file, use them! This prevents
1674 * race conditions if filesystem paths change beneath our feet.
1677 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1678 if (errno == ENOENT) {
1679 if (got_fileindex_entry_has_file_on_disk(ie))
1680 *status = GOT_STATUS_MISSING;
1682 *status = GOT_STATUS_DELETE;
1685 err = got_error_from_errno2("fstatat", abspath);
1689 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1690 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1691 return got_error_from_errno2("open", abspath);
1692 else if (fd == -1 && errno == ELOOP) {
1693 if (lstat(abspath, sb) == -1)
1694 return got_error_from_errno2("lstat", abspath);
1695 } else if (fd == -1 || fstat(fd, sb) == -1) {
1696 if (errno == ENOENT) {
1697 if (got_fileindex_entry_has_file_on_disk(ie))
1698 *status = GOT_STATUS_MISSING;
1700 *status = GOT_STATUS_DELETE;
1703 err = got_error_from_errno2("fstat", abspath);
1708 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1709 *status = GOT_STATUS_OBSTRUCTED;
1713 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1714 *status = GOT_STATUS_DELETE;
1716 } else if (!got_fileindex_entry_has_blob(ie) &&
1717 staged_status != GOT_STATUS_ADD) {
1718 *status = GOT_STATUS_ADD;
1722 if (!stat_info_differs(ie, sb))
1725 if (staged_status == GOT_STATUS_MODIFY ||
1726 staged_status == GOT_STATUS_ADD)
1727 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1729 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1731 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1735 if (S_ISLNK(sb->st_mode)) {
1736 /* Staging changes to symlinks is not yet(?) supported. */
1737 if (staged_status != GOT_STATUS_NO_CHANGE) {
1738 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
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 err = got_fileindex_entry_filetype_set(ie,
1981 GOT_FILEIDX_MODE_BAD_SYMLINK);
1986 got_object_blob_close(blob);
1992 static const struct got_error *
1993 remove_ondisk_file(const char *root_path, const char *path)
1995 const struct got_error *err = NULL;
1996 char *ondisk_path = NULL;
1998 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1999 return got_error_from_errno("asprintf");
2001 if (unlink(ondisk_path) == -1) {
2002 if (errno != ENOENT)
2003 err = got_error_from_errno2("unlink", ondisk_path);
2005 char *parent = dirname(ondisk_path);
2006 while (parent && strcmp(parent, root_path) != 0) {
2007 if (rmdir(parent) == -1) {
2008 if (errno != ENOTEMPTY)
2009 err = got_error_from_errno2("rmdir",
2013 parent = dirname(parent);
2020 static const struct got_error *
2021 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2022 struct got_fileindex_entry *ie, struct got_repository *repo,
2023 got_worktree_checkout_cb progress_cb, void *progress_arg)
2025 const struct got_error *err = NULL;
2026 unsigned char status;
2030 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2031 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2033 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2035 return got_error_from_errno("asprintf");
2037 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2041 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2042 char ondisk_target[PATH_MAX];
2043 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2044 sizeof(ondisk_target));
2045 if (ondisk_len == -1) {
2046 err = got_error_from_errno2("readlink", ondisk_path);
2049 ondisk_target[ondisk_len] = '\0';
2050 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2051 NULL, NULL, /* XXX pass common ancestor info? */
2052 ondisk_target, ondisk_path);
2055 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2060 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2061 status == GOT_STATUS_ADD) {
2062 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2066 * Preserve the working file and change the deleted blob's
2067 * entry into a schedule-add entry.
2069 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2072 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2075 if (status == GOT_STATUS_NO_CHANGE) {
2076 err = remove_ondisk_file(worktree->root_path, ie->path);
2080 got_fileindex_entry_remove(fileindex, ie);
2087 struct diff_cb_arg {
2088 struct got_fileindex *fileindex;
2089 struct got_worktree *worktree;
2090 struct got_repository *repo;
2091 got_worktree_checkout_cb progress_cb;
2093 got_cancel_cb cancel_cb;
2097 static const struct got_error *
2098 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2099 struct got_tree_entry *te, const char *parent_path)
2101 struct diff_cb_arg *a = arg;
2103 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2104 return got_error(GOT_ERR_CANCELLED);
2106 return update_blob(a->worktree, a->fileindex, ie, te,
2107 ie->path, a->repo, a->progress_cb, a->progress_arg);
2110 static const struct got_error *
2111 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2113 struct diff_cb_arg *a = arg;
2115 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2116 return got_error(GOT_ERR_CANCELLED);
2118 return delete_blob(a->worktree, a->fileindex, ie,
2119 a->repo, a->progress_cb, a->progress_arg);
2122 static const struct got_error *
2123 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2125 struct diff_cb_arg *a = arg;
2126 const struct got_error *err;
2129 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2130 return got_error(GOT_ERR_CANCELLED);
2132 if (got_object_tree_entry_is_submodule(te))
2135 if (asprintf(&path, "%s%s%s", parent_path,
2136 parent_path[0] ? "/" : "", te->name)
2138 return got_error_from_errno("asprintf");
2140 if (S_ISDIR(te->mode))
2141 err = add_dir_on_disk(a->worktree, path);
2143 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2144 a->repo, a->progress_cb, a->progress_arg);
2150 static const struct got_error *
2151 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2153 const struct got_error *err = NULL;
2154 char *uuidstr = NULL;
2155 uint32_t uuid_status;
2159 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2160 if (uuid_status != uuid_s_ok)
2161 return got_error_uuid(uuid_status, "uuid_to_string");
2163 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2165 err = got_error_from_errno("asprintf");
2172 const struct got_error *
2173 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2175 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2178 static const struct got_error *
2179 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2181 return get_ref_name(refname, worktree,
2182 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2185 static const struct got_error *
2186 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2188 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2191 static const struct got_error *
2192 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2194 return get_ref_name(refname, worktree,
2195 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2198 static const struct got_error *
2199 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2201 return get_ref_name(refname, worktree,
2202 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2205 static const struct got_error *
2206 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2208 return get_ref_name(refname, worktree,
2209 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2212 static const struct got_error *
2213 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2215 return get_ref_name(refname, worktree,
2216 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2219 static const struct got_error *
2220 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2222 return get_ref_name(refname, worktree,
2223 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2226 static const struct got_error *
2227 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2229 return get_ref_name(refname, worktree,
2230 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2233 const struct got_error *
2234 got_worktree_get_histedit_script_path(char **path,
2235 struct got_worktree *worktree)
2237 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2238 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2240 return got_error_from_errno("asprintf");
2246 * Prevent Git's garbage collector from deleting our base commit by
2247 * setting a reference to our base commit's ID.
2249 static const struct got_error *
2250 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2252 const struct got_error *err = NULL;
2253 struct got_reference *ref = NULL;
2256 err = got_worktree_get_base_ref_name(&refname, worktree);
2260 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2264 err = got_ref_write(ref, repo);
2272 static const struct got_error *
2273 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2275 const struct got_error *err = NULL;
2277 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2278 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2279 err = got_error_from_errno("asprintf");
2280 *fileindex_path = NULL;
2286 static const struct got_error *
2287 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2288 struct got_worktree *worktree)
2290 const struct got_error *err = NULL;
2293 *fileindex_path = NULL;
2294 *fileindex = got_fileindex_alloc();
2295 if (*fileindex == NULL)
2296 return got_error_from_errno("got_fileindex_alloc");
2298 err = get_fileindex_path(fileindex_path, worktree);
2302 index = fopen(*fileindex_path, "rb");
2303 if (index == NULL) {
2304 if (errno != ENOENT)
2305 err = got_error_from_errno2("fopen", *fileindex_path);
2307 err = got_fileindex_read(*fileindex, index);
2308 if (fclose(index) != 0 && err == NULL)
2309 err = got_error_from_errno("fclose");
2313 free(*fileindex_path);
2314 *fileindex_path = NULL;
2315 got_fileindex_free(*fileindex);
2321 struct bump_base_commit_id_arg {
2322 struct got_object_id *base_commit_id;
2325 const char *entry_name;
2326 got_worktree_checkout_cb progress_cb;
2330 /* Bump base commit ID of all files within an updated part of the work tree. */
2331 static const struct got_error *
2332 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2334 const struct got_error *err;
2335 struct bump_base_commit_id_arg *a = arg;
2337 if (a->entry_name) {
2338 if (strcmp(ie->path, a->path) != 0)
2340 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2343 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2344 SHA1_DIGEST_LENGTH) == 0)
2347 if (a->progress_cb) {
2348 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2353 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2357 static const struct got_error *
2358 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2360 const struct got_error *err = NULL;
2361 char *new_fileindex_path = NULL;
2362 FILE *new_index = NULL;
2363 struct timespec timeout;
2365 err = got_opentemp_named(&new_fileindex_path, &new_index,
2370 err = got_fileindex_write(fileindex, new_index);
2374 if (rename(new_fileindex_path, fileindex_path) != 0) {
2375 err = got_error_from_errno3("rename", new_fileindex_path,
2377 unlink(new_fileindex_path);
2381 * Sleep for a short amount of time to ensure that files modified after
2382 * this program exits have a different time stamp from the one which
2383 * was recorded in the file index.
2386 timeout.tv_nsec = 1;
2387 nanosleep(&timeout, NULL);
2391 free(new_fileindex_path);
2395 static const struct got_error *
2396 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2397 struct got_object_id **tree_id, const char *wt_relpath,
2398 struct got_worktree *worktree, struct got_repository *repo)
2400 const struct got_error *err = NULL;
2401 struct got_object_id *id = NULL;
2402 char *in_repo_path = NULL;
2403 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2405 *entry_type = GOT_OBJ_TYPE_ANY;
2406 *tree_relpath = NULL;
2409 if (wt_relpath[0] == '\0') {
2410 /* Check out all files within the work tree. */
2411 *entry_type = GOT_OBJ_TYPE_TREE;
2412 *tree_relpath = strdup("");
2413 if (*tree_relpath == NULL) {
2414 err = got_error_from_errno("strdup");
2417 err = got_object_id_by_path(tree_id, repo,
2418 worktree->base_commit_id, worktree->path_prefix);
2424 /* Check out a subset of files in the work tree. */
2426 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2427 is_root_wt ? "" : "/", wt_relpath) == -1) {
2428 err = got_error_from_errno("asprintf");
2432 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2438 in_repo_path = NULL;
2440 err = got_object_get_type(entry_type, repo, id);
2444 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2445 /* Check out a single file. */
2446 if (strchr(wt_relpath, '/') == NULL) {
2447 /* Check out a single file in work tree's root dir. */
2448 in_repo_path = strdup(worktree->path_prefix);
2449 if (in_repo_path == NULL) {
2450 err = got_error_from_errno("strdup");
2453 *tree_relpath = strdup("");
2454 if (*tree_relpath == NULL) {
2455 err = got_error_from_errno("strdup");
2459 /* Check out a single file in a subdirectory. */
2460 err = got_path_dirname(tree_relpath, wt_relpath);
2463 if (asprintf(&in_repo_path, "%s%s%s",
2464 worktree->path_prefix, is_root_wt ? "" : "/",
2465 *tree_relpath) == -1) {
2466 err = got_error_from_errno("asprintf");
2470 err = got_object_id_by_path(tree_id, repo,
2471 worktree->base_commit_id, in_repo_path);
2473 /* Check out all files within a subdirectory. */
2474 *tree_id = got_object_id_dup(id);
2475 if (*tree_id == NULL) {
2476 err = got_error_from_errno("got_object_id_dup");
2479 *tree_relpath = strdup(wt_relpath);
2480 if (*tree_relpath == NULL) {
2481 err = got_error_from_errno("strdup");
2489 *entry_type = GOT_OBJ_TYPE_ANY;
2490 free(*tree_relpath);
2491 *tree_relpath = NULL;
2498 static const struct got_error *
2499 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2500 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2501 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2502 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2504 const struct got_error *err = NULL;
2505 struct got_commit_object *commit = NULL;
2506 struct got_tree_object *tree = NULL;
2507 struct got_fileindex_diff_tree_cb diff_cb;
2508 struct diff_cb_arg arg;
2510 err = ref_base_commit(worktree, repo);
2512 if (!(err->code == GOT_ERR_ERRNO &&
2513 (errno == EACCES || errno == EROFS)))
2515 err = (*progress_cb)(progress_arg,
2516 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2521 err = got_object_open_as_commit(&commit, repo,
2522 worktree->base_commit_id);
2526 err = got_object_open_as_tree(&tree, repo, tree_id);
2531 got_object_tree_find_entry(tree, entry_name) == NULL) {
2532 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2536 diff_cb.diff_old_new = diff_old_new;
2537 diff_cb.diff_old = diff_old;
2538 diff_cb.diff_new = diff_new;
2539 arg.fileindex = fileindex;
2540 arg.worktree = worktree;
2542 arg.progress_cb = progress_cb;
2543 arg.progress_arg = progress_arg;
2544 arg.cancel_cb = cancel_cb;
2545 arg.cancel_arg = cancel_arg;
2546 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2547 entry_name, repo, &diff_cb, &arg);
2550 got_object_tree_close(tree);
2552 got_object_commit_close(commit);
2556 const struct got_error *
2557 got_worktree_checkout_files(struct got_worktree *worktree,
2558 struct got_pathlist_head *paths, struct got_repository *repo,
2559 got_worktree_checkout_cb progress_cb, void *progress_arg,
2560 got_cancel_cb cancel_cb, void *cancel_arg)
2562 const struct got_error *err = NULL, *sync_err, *unlockerr;
2563 struct got_commit_object *commit = NULL;
2564 struct got_tree_object *tree = NULL;
2565 struct got_fileindex *fileindex = NULL;
2566 char *fileindex_path = NULL;
2567 struct got_pathlist_entry *pe;
2568 struct tree_path_data {
2569 SIMPLEQ_ENTRY(tree_path_data) entry;
2570 struct got_object_id *tree_id;
2575 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2577 SIMPLEQ_INIT(&tree_paths);
2579 err = lock_worktree(worktree, LOCK_EX);
2583 /* Map all specified paths to in-repository trees. */
2584 TAILQ_FOREACH(pe, paths, entry) {
2585 tpd = malloc(sizeof(*tpd));
2587 err = got_error_from_errno("malloc");
2591 err = find_tree_entry_for_checkout(&tpd->entry_type,
2592 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2598 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2599 err = got_path_basename(&tpd->entry_name, pe->path);
2607 tpd->entry_name = NULL;
2609 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2613 * Read the file index.
2614 * Checking out files is supposed to be an idempotent operation.
2615 * If the on-disk file index is incomplete we will try to complete it.
2617 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2621 tpd = SIMPLEQ_FIRST(&tree_paths);
2622 TAILQ_FOREACH(pe, paths, entry) {
2623 struct bump_base_commit_id_arg bbc_arg;
2625 err = checkout_files(worktree, fileindex, tpd->relpath,
2626 tpd->tree_id, tpd->entry_name, repo,
2627 progress_cb, progress_arg, cancel_cb, cancel_arg);
2631 bbc_arg.base_commit_id = worktree->base_commit_id;
2632 bbc_arg.entry_name = tpd->entry_name;
2633 bbc_arg.path = pe->path;
2634 bbc_arg.path_len = pe->path_len;
2635 bbc_arg.progress_cb = progress_cb;
2636 bbc_arg.progress_arg = progress_arg;
2637 err = got_fileindex_for_each_entry_safe(fileindex,
2638 bump_base_commit_id, &bbc_arg);
2642 tpd = SIMPLEQ_NEXT(tpd, entry);
2644 sync_err = sync_fileindex(fileindex, fileindex_path);
2645 if (sync_err && err == NULL)
2648 free(fileindex_path);
2650 got_object_tree_close(tree);
2652 got_object_commit_close(commit);
2654 got_fileindex_free(fileindex);
2655 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2656 tpd = SIMPLEQ_FIRST(&tree_paths);
2657 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2662 unlockerr = lock_worktree(worktree, LOCK_SH);
2663 if (unlockerr && err == NULL)
2668 struct merge_file_cb_arg {
2669 struct got_worktree *worktree;
2670 struct got_fileindex *fileindex;
2671 got_worktree_checkout_cb progress_cb;
2673 got_cancel_cb cancel_cb;
2675 const char *label_orig;
2676 struct got_object_id *commit_id2;
2679 static const struct got_error *
2680 merge_file_cb(void *arg, struct got_blob_object *blob1,
2681 struct got_blob_object *blob2, struct got_object_id *id1,
2682 struct got_object_id *id2, const char *path1, const char *path2,
2683 mode_t mode1, mode_t mode2, struct got_repository *repo)
2685 static const struct got_error *err = NULL;
2686 struct merge_file_cb_arg *a = arg;
2687 struct got_fileindex_entry *ie;
2688 char *ondisk_path = NULL;
2690 unsigned char status;
2691 int local_changes_subsumed;
2693 if (blob1 && blob2) {
2694 ie = got_fileindex_entry_get(a->fileindex, path2,
2697 return (*a->progress_cb)(a->progress_arg,
2698 GOT_STATUS_MISSING, path2);
2700 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2702 return got_error_from_errno("asprintf");
2704 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2709 if (status == GOT_STATUS_DELETE) {
2710 err = (*a->progress_cb)(a->progress_arg,
2711 GOT_STATUS_MERGE, path2);
2714 if (status != GOT_STATUS_NO_CHANGE &&
2715 status != GOT_STATUS_MODIFY &&
2716 status != GOT_STATUS_CONFLICT &&
2717 status != GOT_STATUS_ADD) {
2718 err = (*a->progress_cb)(a->progress_arg, status, path2);
2722 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2723 err = merge_symlink(a->worktree, blob1,
2724 ondisk_path, path2, sb.st_mode, a->label_orig,
2725 blob2, a->commit_id2, repo, a->progress_cb,
2728 err = merge_blob(&local_changes_subsumed, a->worktree,
2729 blob1, ondisk_path, path2, sb.st_mode,
2730 a->label_orig, blob2, a->commit_id2, repo,
2731 a->progress_cb, a->progress_arg);
2734 ie = got_fileindex_entry_get(a->fileindex, path1,
2737 return (*a->progress_cb)(a->progress_arg,
2738 GOT_STATUS_MISSING, path1);
2740 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2742 return got_error_from_errno("asprintf");
2744 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2750 case GOT_STATUS_NO_CHANGE:
2751 err = (*a->progress_cb)(a->progress_arg,
2752 GOT_STATUS_DELETE, path1);
2755 err = remove_ondisk_file(a->worktree->root_path, path1);
2759 got_fileindex_entry_mark_deleted_from_disk(ie);
2761 case GOT_STATUS_DELETE:
2762 case GOT_STATUS_MISSING:
2763 err = (*a->progress_cb)(a->progress_arg,
2764 GOT_STATUS_DELETE, path1);
2768 got_fileindex_entry_mark_deleted_from_disk(ie);
2770 case GOT_STATUS_ADD:
2771 case GOT_STATUS_MODIFY:
2772 case GOT_STATUS_CONFLICT:
2773 err = (*a->progress_cb)(a->progress_arg,
2774 GOT_STATUS_CANNOT_DELETE, path1);
2778 case GOT_STATUS_OBSTRUCTED:
2779 err = (*a->progress_cb)(a->progress_arg, status, path1);
2787 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2789 return got_error_from_errno("asprintf");
2790 ie = got_fileindex_entry_get(a->fileindex, path2,
2793 err = get_file_status(&status, &sb, ie, ondisk_path,
2797 if (status != GOT_STATUS_NO_CHANGE &&
2798 status != GOT_STATUS_MODIFY &&
2799 status != GOT_STATUS_CONFLICT &&
2800 status != GOT_STATUS_ADD) {
2801 err = (*a->progress_cb)(a->progress_arg,
2805 if (S_ISLNK(mode2)) {
2806 err = merge_symlink(a->worktree, NULL,
2807 ondisk_path, path2, sb.st_mode,
2808 a->label_orig, blob2, a->commit_id2,
2809 repo, a->progress_cb, a->progress_arg);
2813 err = merge_blob(&local_changes_subsumed,
2814 a->worktree, NULL, ondisk_path, path2,
2815 sb.st_mode, a->label_orig, blob2,
2816 a->commit_id2, repo, a->progress_cb,
2821 if (status == GOT_STATUS_DELETE) {
2822 err = got_fileindex_entry_update(ie,
2823 ondisk_path, blob2->id.sha1,
2824 a->worktree->base_commit_id->sha1, 0);
2829 int is_bad_symlink = 0;
2830 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2831 if (S_ISLNK(mode2)) {
2832 err = install_symlink(&is_bad_symlink,
2833 a->worktree, ondisk_path, path2, blob2, 0,
2834 0, 1, repo, a->progress_cb, a->progress_arg);
2836 err = install_blob(a->worktree, ondisk_path, path2,
2837 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2838 a->progress_cb, a->progress_arg);
2842 err = got_fileindex_entry_alloc(&ie, path2);
2845 err = got_fileindex_entry_update(ie, ondisk_path,
2848 got_fileindex_entry_free(ie);
2851 err = got_fileindex_entry_add(a->fileindex, ie);
2853 got_fileindex_entry_free(ie);
2856 if (is_bad_symlink) {
2857 err = got_fileindex_entry_filetype_set(ie,
2858 GOT_FILEIDX_MODE_BAD_SYMLINK);
2869 struct check_merge_ok_arg {
2870 struct got_worktree *worktree;
2871 struct got_repository *repo;
2874 static const struct got_error *
2875 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2877 const struct got_error *err = NULL;
2878 struct check_merge_ok_arg *a = arg;
2879 unsigned char status;
2883 /* Reject merges into a work tree with mixed base commits. */
2884 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2885 SHA1_DIGEST_LENGTH))
2886 return got_error(GOT_ERR_MIXED_COMMITS);
2888 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2890 return got_error_from_errno("asprintf");
2892 /* Reject merges into a work tree with conflicted files. */
2893 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2896 if (status == GOT_STATUS_CONFLICT)
2897 return got_error(GOT_ERR_CONFLICTS);
2902 static const struct got_error *
2903 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2904 const char *fileindex_path, struct got_object_id *commit_id1,
2905 struct got_object_id *commit_id2, struct got_repository *repo,
2906 got_worktree_checkout_cb progress_cb, void *progress_arg,
2907 got_cancel_cb cancel_cb, void *cancel_arg)
2909 const struct got_error *err = NULL, *sync_err;
2910 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2911 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2912 struct merge_file_cb_arg arg;
2913 char *label_orig = NULL;
2918 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2919 worktree->path_prefix);
2923 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2927 err = got_object_id_str(&id_str, commit_id1);
2931 if (asprintf(&label_orig, "%s: commit %s",
2932 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2933 err = got_error_from_errno("asprintf");
2940 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2941 worktree->path_prefix);
2945 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2949 arg.worktree = worktree;
2950 arg.fileindex = fileindex;
2951 arg.progress_cb = progress_cb;
2952 arg.progress_arg = progress_arg;
2953 arg.cancel_cb = cancel_cb;
2954 arg.cancel_arg = cancel_arg;
2955 arg.label_orig = label_orig;
2956 arg.commit_id2 = commit_id2;
2957 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2958 sync_err = sync_fileindex(fileindex, fileindex_path);
2959 if (sync_err && err == NULL)
2963 got_object_tree_close(tree1);
2965 got_object_tree_close(tree2);
2970 const struct got_error *
2971 got_worktree_merge_files(struct got_worktree *worktree,
2972 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2973 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2974 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2976 const struct got_error *err, *unlockerr;
2977 char *fileindex_path = NULL;
2978 struct got_fileindex *fileindex = NULL;
2979 struct check_merge_ok_arg mok_arg;
2981 err = lock_worktree(worktree, LOCK_EX);
2985 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2989 mok_arg.worktree = worktree;
2990 mok_arg.repo = repo;
2991 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2996 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2997 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3000 got_fileindex_free(fileindex);
3001 free(fileindex_path);
3002 unlockerr = lock_worktree(worktree, LOCK_SH);
3003 if (unlockerr && err == NULL)
3008 struct diff_dir_cb_arg {
3009 struct got_fileindex *fileindex;
3010 struct got_worktree *worktree;
3011 const char *status_path;
3012 size_t status_path_len;
3013 struct got_repository *repo;
3014 got_worktree_status_cb status_cb;
3016 got_cancel_cb cancel_cb;
3018 /* A pathlist containing per-directory pathlists of ignore patterns. */
3019 struct got_pathlist_head ignores;
3020 int report_unchanged;
3024 static const struct got_error *
3025 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3026 int dirfd, const char *de_name,
3027 got_worktree_status_cb status_cb, void *status_arg,
3028 struct got_repository *repo, int report_unchanged)
3030 const struct got_error *err = NULL;
3031 unsigned char status = GOT_STATUS_NO_CHANGE;
3032 unsigned char staged_status = get_staged_status(ie);
3034 struct got_object_id blob_id, commit_id, staged_blob_id;
3035 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3036 struct got_object_id *staged_blob_idp = NULL;
3038 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3042 if (status == GOT_STATUS_NO_CHANGE &&
3043 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3046 if (got_fileindex_entry_has_blob(ie)) {
3047 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3048 blob_idp = &blob_id;
3050 if (got_fileindex_entry_has_commit(ie)) {
3051 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3052 commit_idp = &commit_id;
3054 if (staged_status == GOT_STATUS_ADD ||
3055 staged_status == GOT_STATUS_MODIFY) {
3056 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3057 SHA1_DIGEST_LENGTH);
3058 staged_blob_idp = &staged_blob_id;
3061 return (*status_cb)(status_arg, status, staged_status,
3062 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3065 static const struct got_error *
3066 status_old_new(void *arg, struct got_fileindex_entry *ie,
3067 struct dirent *de, const char *parent_path, int dirfd)
3069 const struct got_error *err = NULL;
3070 struct diff_dir_cb_arg *a = arg;
3073 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3074 return got_error(GOT_ERR_CANCELLED);
3076 if (got_path_cmp(parent_path, a->status_path,
3077 strlen(parent_path), a->status_path_len) != 0 &&
3078 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3081 if (parent_path[0]) {
3082 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3083 parent_path, de->d_name) == -1)
3084 return got_error_from_errno("asprintf");
3086 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3088 return got_error_from_errno("asprintf");
3091 err = report_file_status(ie, abspath, dirfd, de->d_name,
3092 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3097 static const struct got_error *
3098 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3100 struct diff_dir_cb_arg *a = arg;
3101 struct got_object_id blob_id, commit_id;
3102 unsigned char status;
3104 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3105 return got_error(GOT_ERR_CANCELLED);
3107 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3110 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3111 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3112 if (got_fileindex_entry_has_file_on_disk(ie))
3113 status = GOT_STATUS_MISSING;
3115 status = GOT_STATUS_DELETE;
3116 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3117 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3121 free_ignorelist(struct got_pathlist_head *ignorelist)
3123 struct got_pathlist_entry *pe;
3125 TAILQ_FOREACH(pe, ignorelist, entry)
3126 free((char *)pe->path);
3127 got_pathlist_free(ignorelist);
3131 free_ignores(struct got_pathlist_head *ignores)
3133 struct got_pathlist_entry *pe;
3135 TAILQ_FOREACH(pe, ignores, entry) {
3136 struct got_pathlist_head *ignorelist = pe->data;
3137 free_ignorelist(ignorelist);
3138 free((char *)pe->path);
3140 got_pathlist_free(ignores);
3143 static const struct got_error *
3144 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3146 const struct got_error *err = NULL;
3147 struct got_pathlist_entry *pe = NULL;
3148 struct got_pathlist_head *ignorelist;
3149 char *line = NULL, *pattern, *dirpath = NULL;
3150 size_t linesize = 0;
3153 ignorelist = calloc(1, sizeof(*ignorelist));
3154 if (ignorelist == NULL)
3155 return got_error_from_errno("calloc");
3156 TAILQ_INIT(ignorelist);
3158 while ((linelen = getline(&line, &linesize, f)) != -1) {
3159 if (linelen > 0 && line[linelen - 1] == '\n')
3160 line[linelen - 1] = '\0';
3162 /* Git's ignores may contain comments. */
3166 /* Git's negated patterns are not (yet?) supported. */
3170 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3172 err = got_error_from_errno("asprintf");
3175 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3180 err = got_error_from_errno("getline");
3184 dirpath = strdup(path);
3185 if (dirpath == NULL) {
3186 err = got_error_from_errno("strdup");
3189 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3192 if (err || pe == NULL) {
3194 free_ignorelist(ignorelist);
3200 match_ignores(struct got_pathlist_head *ignores, const char *path)
3202 struct got_pathlist_entry *pe;
3204 /* Handle patterns which match in all directories. */
3205 TAILQ_FOREACH(pe, ignores, entry) {
3206 struct got_pathlist_head *ignorelist = pe->data;
3207 struct got_pathlist_entry *pi;
3209 TAILQ_FOREACH(pi, ignorelist, entry) {
3210 const char *p, *pattern = pi->path;
3212 if (strncmp(pattern, "**/", 3) != 0)
3217 if (fnmatch(pattern, p,
3218 FNM_PATHNAME | FNM_LEADING_DIR)) {
3219 /* Retry in next directory. */
3220 while (*p && *p != '/')
3232 * The ignores pathlist contains ignore lists from children before
3233 * parents, so we can find the most specific ignorelist by walking
3234 * ignores backwards.
3236 pe = TAILQ_LAST(ignores, got_pathlist_head);
3238 if (got_path_is_child(path, pe->path, pe->path_len)) {
3239 struct got_pathlist_head *ignorelist = pe->data;
3240 struct got_pathlist_entry *pi;
3241 TAILQ_FOREACH(pi, ignorelist, entry) {
3242 const char *pattern = pi->path;
3243 int flags = FNM_LEADING_DIR;
3244 if (strstr(pattern, "/**/") == NULL)
3245 flags |= FNM_PATHNAME;
3246 if (fnmatch(pattern, path, flags))
3251 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3257 static const struct got_error *
3258 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3259 const char *path, int dirfd, const char *ignores_filename)
3261 const struct got_error *err = NULL;
3264 FILE *ignoresfile = NULL;
3266 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3267 path[0] ? "/" : "", ignores_filename) == -1)
3268 return got_error_from_errno("asprintf");
3271 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3273 if (errno != ENOENT && errno != EACCES)
3274 err = got_error_from_errno2("openat",
3277 ignoresfile = fdopen(fd, "r");
3278 if (ignoresfile == NULL)
3279 err = got_error_from_errno2("fdopen",
3283 err = read_ignores(ignores, path, ignoresfile);
3287 ignoresfile = fopen(ignorespath, "r");
3288 if (ignoresfile == NULL) {
3289 if (errno != ENOENT && errno != EACCES)
3290 err = got_error_from_errno2("fopen",
3293 err = read_ignores(ignores, path, ignoresfile);
3296 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3297 err = got_error_from_errno2("fclose", path);
3298 if (fd != -1 && close(fd) == -1 && err == NULL)
3299 err = got_error_from_errno2("close", path);
3304 static const struct got_error *
3305 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3307 const struct got_error *err = NULL;
3308 struct diff_dir_cb_arg *a = arg;
3311 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3312 return got_error(GOT_ERR_CANCELLED);
3314 if (parent_path[0]) {
3315 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3316 return got_error_from_errno("asprintf");
3321 if (de->d_type != DT_DIR &&
3322 got_path_is_child(path, a->status_path, a->status_path_len)
3323 && !match_ignores(&a->ignores, path))
3324 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3325 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3331 static const struct got_error *
3332 status_traverse(void *arg, const char *path, int dirfd)
3334 const struct got_error *err = NULL;
3335 struct diff_dir_cb_arg *a = arg;
3340 err = add_ignores(&a->ignores, a->worktree->root_path,
3341 path, dirfd, ".cvsignore");
3345 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3346 dirfd, ".gitignore");
3351 static const struct got_error *
3352 report_single_file_status(const char *path, const char *ondisk_path,
3353 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3354 void *status_arg, struct got_repository *repo, int report_unchanged)
3356 struct got_fileindex_entry *ie;
3359 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3361 return report_file_status(ie, ondisk_path, -1, NULL,
3362 status_cb, status_arg, repo, report_unchanged);
3364 if (lstat(ondisk_path, &sb) == -1) {
3365 if (errno != ENOENT)
3366 return got_error_from_errno2("lstat", ondisk_path);
3367 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3368 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3372 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3373 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3374 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3379 static const struct got_error *
3380 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3381 const char *root_path, const char *path)
3383 const struct got_error *err;
3384 char *parent_path, *next_parent_path = NULL;
3386 err = add_ignores(ignores, root_path, "", -1,
3391 err = add_ignores(ignores, root_path, "", -1,
3396 err = got_path_dirname(&parent_path, path);
3398 if (err->code == GOT_ERR_BAD_PATH)
3399 return NULL; /* cannot traverse parent */
3403 err = add_ignores(ignores, root_path, parent_path, -1,
3407 err = add_ignores(ignores, root_path, parent_path, -1,
3411 err = got_path_dirname(&next_parent_path, parent_path);
3413 if (err->code == GOT_ERR_BAD_PATH)
3414 err = NULL; /* traversed everything */
3418 parent_path = next_parent_path;
3419 next_parent_path = NULL;
3423 free(next_parent_path);
3427 static const struct got_error *
3428 worktree_status(struct got_worktree *worktree, const char *path,
3429 struct got_fileindex *fileindex, struct got_repository *repo,
3430 got_worktree_status_cb status_cb, void *status_arg,
3431 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3432 int report_unchanged)
3434 const struct got_error *err = NULL;
3436 struct got_fileindex_diff_dir_cb fdiff_cb;
3437 struct diff_dir_cb_arg arg;
3438 char *ondisk_path = NULL;
3440 TAILQ_INIT(&arg.ignores);
3442 if (asprintf(&ondisk_path, "%s%s%s",
3443 worktree->root_path, path[0] ? "/" : "", path) == -1)
3444 return got_error_from_errno("asprintf");
3446 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3448 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3450 err = got_error_from_errno2("open", ondisk_path);
3452 err = report_single_file_status(path, ondisk_path,
3453 fileindex, status_cb, status_arg, repo,
3456 fdiff_cb.diff_old_new = status_old_new;
3457 fdiff_cb.diff_old = status_old;
3458 fdiff_cb.diff_new = status_new;
3459 fdiff_cb.diff_traverse = status_traverse;
3460 arg.fileindex = fileindex;
3461 arg.worktree = worktree;
3462 arg.status_path = path;
3463 arg.status_path_len = strlen(path);
3465 arg.status_cb = status_cb;
3466 arg.status_arg = status_arg;
3467 arg.cancel_cb = cancel_cb;
3468 arg.cancel_arg = cancel_arg;
3469 arg.report_unchanged = report_unchanged;
3470 arg.no_ignores = no_ignores;
3472 err = add_ignores_from_parent_paths(&arg.ignores,
3473 worktree->root_path, path);
3477 err = got_fileindex_diff_dir(fileindex, fd,
3478 worktree->root_path, path, repo, &fdiff_cb, &arg);
3481 free_ignores(&arg.ignores);
3482 if (fd != -1 && close(fd) != 0 && err == NULL)
3483 err = got_error_from_errno("close");
3488 const struct got_error *
3489 got_worktree_status(struct got_worktree *worktree,
3490 struct got_pathlist_head *paths, struct got_repository *repo,
3491 got_worktree_status_cb status_cb, void *status_arg,
3492 got_cancel_cb cancel_cb, void *cancel_arg)
3494 const struct got_error *err = NULL;
3495 char *fileindex_path = NULL;
3496 struct got_fileindex *fileindex = NULL;
3497 struct got_pathlist_entry *pe;
3499 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3503 TAILQ_FOREACH(pe, paths, entry) {
3504 err = worktree_status(worktree, pe->path, fileindex, repo,
3505 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3509 free(fileindex_path);
3510 got_fileindex_free(fileindex);
3514 const struct got_error *
3515 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3518 const struct got_error *err = NULL;
3519 char *resolved = NULL, *cwd = NULL, *path = NULL;
3525 cwd = getcwd(NULL, 0);
3527 return got_error_from_errno("getcwd");
3529 if (lstat(arg, &sb) == -1) {
3530 if (errno != ENOENT) {
3531 err = got_error_from_errno2("lstat", arg);
3535 if (S_ISLNK(sb.st_mode)) {
3537 * We cannot use realpath(3) with symlinks since we want to
3538 * operate on the symlink itself.
3539 * But we can make the path absolute, assuming it is relative
3540 * to the current working directory, and then canonicalize it.
3542 char *abspath = NULL;
3543 char canonpath[PATH_MAX];
3544 if (!got_path_is_absolute(arg)) {
3545 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3546 err = got_error_from_errno("asprintf");
3551 err = got_canonpath(abspath ? abspath : arg, canonpath,
3555 resolved = strdup(canonpath);
3556 if (resolved == NULL) {
3557 err = got_error_from_errno("strdup");
3561 resolved = realpath(arg, NULL);
3562 if (resolved == NULL) {
3563 if (errno != ENOENT) {
3564 err = got_error_from_errno2("realpath", arg);
3567 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3568 err = got_error_from_errno("asprintf");
3574 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3575 strlen(got_worktree_get_root_path(worktree)))) {
3576 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3580 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3581 err = got_path_skip_common_ancestor(&path,
3582 got_worktree_get_root_path(worktree), resolved);
3588 err = got_error_from_errno("strdup");
3593 /* XXX status walk can't deal with trailing slash! */
3595 while (len > 0 && path[len - 1] == '/') {
3596 path[len - 1] = '\0';
3609 struct schedule_addition_args {
3610 struct got_worktree *worktree;
3611 struct got_fileindex *fileindex;
3612 got_worktree_checkout_cb progress_cb;
3614 struct got_repository *repo;
3617 static const struct got_error *
3618 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3619 const char *relpath, struct got_object_id *blob_id,
3620 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3621 int dirfd, const char *de_name)
3623 struct schedule_addition_args *a = arg;
3624 const struct got_error *err = NULL;
3625 struct got_fileindex_entry *ie;
3629 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3631 return got_error_from_errno("asprintf");
3633 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3635 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3639 /* Re-adding an existing entry is a no-op. */
3640 if (status == GOT_STATUS_ADD)
3642 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3647 if (status != GOT_STATUS_UNVERSIONED) {
3648 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3652 err = got_fileindex_entry_alloc(&ie, relpath);
3655 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3657 got_fileindex_entry_free(ie);
3660 err = got_fileindex_entry_add(a->fileindex, ie);
3662 got_fileindex_entry_free(ie);
3669 if (status == GOT_STATUS_ADD)
3671 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3674 const struct got_error *
3675 got_worktree_schedule_add(struct got_worktree *worktree,
3676 struct got_pathlist_head *paths,
3677 got_worktree_checkout_cb progress_cb, void *progress_arg,
3678 struct got_repository *repo, int no_ignores)
3680 struct got_fileindex *fileindex = NULL;
3681 char *fileindex_path = NULL;
3682 const struct got_error *err = NULL, *sync_err, *unlockerr;
3683 struct got_pathlist_entry *pe;
3684 struct schedule_addition_args saa;
3686 err = lock_worktree(worktree, LOCK_EX);
3690 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3694 saa.worktree = worktree;
3695 saa.fileindex = fileindex;
3696 saa.progress_cb = progress_cb;
3697 saa.progress_arg = progress_arg;
3700 TAILQ_FOREACH(pe, paths, entry) {
3701 err = worktree_status(worktree, pe->path, fileindex, repo,
3702 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3706 sync_err = sync_fileindex(fileindex, fileindex_path);
3707 if (sync_err && err == NULL)
3710 free(fileindex_path);
3712 got_fileindex_free(fileindex);
3713 unlockerr = lock_worktree(worktree, LOCK_SH);
3714 if (unlockerr && err == NULL)
3719 struct schedule_deletion_args {
3720 struct got_worktree *worktree;
3721 struct got_fileindex *fileindex;
3722 got_worktree_delete_cb progress_cb;
3724 struct got_repository *repo;
3725 int delete_local_mods;
3729 static const struct got_error *
3730 schedule_for_deletion(void *arg, unsigned char status,
3731 unsigned char staged_status, const char *relpath,
3732 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3733 struct got_object_id *commit_id, int dirfd, const char *de_name)
3735 struct schedule_deletion_args *a = arg;
3736 const struct got_error *err = NULL;
3737 struct got_fileindex_entry *ie = NULL;
3739 char *ondisk_path, *parent = NULL;
3741 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3743 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3745 staged_status = get_staged_status(ie);
3746 if (staged_status != GOT_STATUS_NO_CHANGE) {
3747 if (staged_status == GOT_STATUS_DELETE)
3749 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3752 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3754 return got_error_from_errno("asprintf");
3756 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3761 if (status != GOT_STATUS_NO_CHANGE) {
3762 if (status == GOT_STATUS_DELETE)
3764 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3765 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3768 if (status != GOT_STATUS_MODIFY &&
3769 status != GOT_STATUS_MISSING) {
3770 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3775 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3777 if (unlinkat(dirfd, de_name, 0) != 0) {
3778 err = got_error_from_errno2("unlinkat",
3782 } else if (unlink(ondisk_path) != 0) {
3783 err = got_error_from_errno2("unlink", ondisk_path);
3787 parent = dirname(ondisk_path);
3789 if (parent == NULL) {
3790 err = got_error_from_errno2("dirname", ondisk_path);
3793 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3794 if (rmdir(parent) == -1) {
3795 if (errno != ENOTEMPTY)
3796 err = got_error_from_errno2("rmdir",
3800 parent = dirname(parent);
3801 if (parent == NULL) {
3802 err = got_error_from_errno2("dirname", parent);
3808 got_fileindex_entry_mark_deleted_from_disk(ie);
3813 if (status == GOT_STATUS_DELETE)
3815 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3816 staged_status, relpath);
3819 const struct got_error *
3820 got_worktree_schedule_delete(struct got_worktree *worktree,
3821 struct got_pathlist_head *paths, int delete_local_mods,
3822 got_worktree_delete_cb progress_cb, void *progress_arg,
3823 struct got_repository *repo, int keep_on_disk)
3825 struct got_fileindex *fileindex = NULL;
3826 char *fileindex_path = NULL;
3827 const struct got_error *err = NULL, *sync_err, *unlockerr;
3828 struct got_pathlist_entry *pe;
3829 struct schedule_deletion_args sda;
3831 err = lock_worktree(worktree, LOCK_EX);
3835 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3839 sda.worktree = worktree;
3840 sda.fileindex = fileindex;
3841 sda.progress_cb = progress_cb;
3842 sda.progress_arg = progress_arg;
3844 sda.delete_local_mods = delete_local_mods;
3845 sda.keep_on_disk = keep_on_disk;
3847 TAILQ_FOREACH(pe, paths, entry) {
3848 err = worktree_status(worktree, pe->path, fileindex, repo,
3849 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3853 sync_err = sync_fileindex(fileindex, fileindex_path);
3854 if (sync_err && err == NULL)
3857 free(fileindex_path);
3859 got_fileindex_free(fileindex);
3860 unlockerr = lock_worktree(worktree, LOCK_SH);
3861 if (unlockerr && err == NULL)
3866 static const struct got_error *
3867 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3869 const struct got_error *err = NULL;
3871 size_t linesize = 0, n;
3874 linelen = getline(&line, &linesize, infile);
3875 if (linelen == -1) {
3876 if (ferror(infile)) {
3877 err = got_error_from_errno("getline");
3883 n = fwrite(line, 1, linelen, outfile);
3885 err = got_ferror(outfile, GOT_ERR_IO);
3890 n = fwrite(line, 1, linelen, rejectfile);
3892 err = got_ferror(outfile, GOT_ERR_IO);
3899 static const struct got_error *
3900 skip_one_line(FILE *f)
3903 size_t linesize = 0;
3906 linelen = getline(&line, &linesize, f);
3907 if (linelen == -1) {
3909 return got_error_from_errno("getline");
3916 static const struct got_error *
3917 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3918 int start_old, int end_old, int start_new, int end_new,
3919 FILE *outfile, FILE *rejectfile)
3921 const struct got_error *err;
3923 /* Copy old file's lines leading up to patch. */
3924 while (!feof(f1) && *line_cur1 < start_old) {
3925 err = copy_one_line(f1, outfile, NULL);
3930 /* Skip new file's lines leading up to patch. */
3931 while (!feof(f2) && *line_cur2 < start_new) {
3933 err = copy_one_line(f2, NULL, rejectfile);
3935 err = skip_one_line(f2);
3940 /* Copy patched lines. */
3941 while (!feof(f2) && *line_cur2 <= end_new) {
3942 err = copy_one_line(f2, outfile, NULL);
3947 /* Skip over old file's replaced lines. */
3948 while (!feof(f1) && *line_cur1 <= end_old) {
3950 err = copy_one_line(f1, NULL, rejectfile);
3952 err = skip_one_line(f1);
3961 static const struct got_error *
3962 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3963 FILE *outfile, FILE *rejectfile)
3965 const struct got_error *err;
3968 /* Copy old file's lines until EOF. */
3970 err = copy_one_line(f1, outfile, NULL);
3977 /* Copy new file's lines until EOF. */
3979 err = copy_one_line(f2, NULL, rejectfile);
3989 static const struct got_error *
3990 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3991 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3992 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3993 int *line_cur2, FILE *outfile, FILE *rejectfile,
3994 got_worktree_patch_cb patch_cb, void *patch_arg)
3996 const struct got_error *err = NULL;
3997 int start_old = change->cv.a;
3998 int end_old = change->cv.b;
3999 int start_new = change->cv.c;
4000 int end_new = change->cv.d;
4004 *choice = GOT_PATCH_CHOICE_NONE;
4006 hunkfile = got_opentemp();
4007 if (hunkfile == NULL)
4008 return got_error_from_errno("got_opentemp");
4013 /* XXX TODO needs error checking */
4014 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4016 if (fseek(f1, pos1, SEEK_SET) == -1) {
4017 err = got_ferror(f1, GOT_ERR_IO);
4020 if (fseek(f2, pos2, SEEK_SET) == -1) {
4021 err = got_ferror(f1, GOT_ERR_IO);
4024 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4025 err = got_ferror(hunkfile, GOT_ERR_IO);
4029 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4030 hunkfile, n, nchanges);
4035 case GOT_PATCH_CHOICE_YES:
4036 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4037 end_old, start_new, end_new, outfile, rejectfile);
4039 case GOT_PATCH_CHOICE_NO:
4040 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4041 end_old, start_new, end_new, rejectfile, outfile);
4043 case GOT_PATCH_CHOICE_QUIT:
4046 err = got_error(GOT_ERR_PATCH_CHOICE);
4050 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4051 err = got_error_from_errno("fclose");
4055 struct revert_file_args {
4056 struct got_worktree *worktree;
4057 struct got_fileindex *fileindex;
4058 got_worktree_checkout_cb progress_cb;
4060 got_worktree_patch_cb patch_cb;
4062 struct got_repository *repo;
4065 static const struct got_error *
4066 create_patched_content(char **path_outfile, int reverse_patch,
4067 struct got_object_id *blob_id, const char *path2,
4068 int dirfd2, const char *de_name2,
4069 const char *relpath, struct got_repository *repo,
4070 got_worktree_patch_cb patch_cb, void *patch_arg)
4072 const struct got_error *err;
4073 struct got_blob_object *blob = NULL;
4074 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4076 char *path1 = NULL, *id_str = NULL;
4077 struct stat sb1, sb2;
4078 struct got_diff_changes *changes = NULL;
4079 struct got_diff_state *ds = NULL;
4080 struct got_diff_args *args = NULL;
4081 struct got_diff_change *change;
4082 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4085 *path_outfile = NULL;
4087 err = got_object_id_str(&id_str, blob_id);
4092 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4094 err = got_error_from_errno2("openat", path2);
4098 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4100 err = got_error_from_errno2("open", path2);
4104 if (fstat(fd2, &sb2) == -1) {
4105 err = got_error_from_errno2("fstat", path2);
4109 f2 = fdopen(fd2, "r");
4111 err = got_error_from_errno2("fdopen", path2);
4116 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4120 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4124 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4128 if (stat(path1, &sb1) == -1) {
4129 err = got_error_from_errno2("stat", path1);
4133 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4134 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4138 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4142 if (fseek(f1, 0L, SEEK_SET) == -1)
4143 return got_ferror(f1, GOT_ERR_IO);
4144 if (fseek(f2, 0L, SEEK_SET) == -1)
4145 return got_ferror(f2, GOT_ERR_IO);
4146 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4148 err = apply_or_reject_change(&choice, change, ++n,
4149 changes->nchanges, ds, args, diff_flags, relpath,
4150 f1, f2, &line_cur1, &line_cur2,
4151 reverse_patch ? NULL : outfile,
4152 reverse_patch ? outfile : NULL,
4153 patch_cb, patch_arg);
4156 if (choice == GOT_PATCH_CHOICE_YES)
4158 else if (choice == GOT_PATCH_CHOICE_QUIT)
4162 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4163 reverse_patch ? NULL : outfile,
4164 reverse_patch ? outfile : NULL);
4168 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4169 err = got_error_from_errno2("chmod", path2);
4176 got_object_blob_close(blob);
4177 if (f1 && fclose(f1) == EOF && err == NULL)
4178 err = got_error_from_errno2("fclose", path1);
4179 if (f2 && fclose(f2) == EOF && err == NULL)
4180 err = got_error_from_errno2("fclose", path2);
4181 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4182 err = got_error_from_errno2("close", path2);
4183 if (outfile && fclose(outfile) == EOF && err == NULL)
4184 err = got_error_from_errno2("fclose", *path_outfile);
4185 if (path1 && unlink(path1) == -1 && err == NULL)
4186 err = got_error_from_errno2("unlink", path1);
4187 if (err || !have_content) {
4188 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4189 err = got_error_from_errno2("unlink", *path_outfile);
4190 free(*path_outfile);
4191 *path_outfile = NULL;
4195 got_diff_state_free(ds);
4199 got_diff_free_changes(changes);
4204 static const struct got_error *
4205 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4206 const char *relpath, struct got_object_id *blob_id,
4207 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4208 int dirfd, const char *de_name)
4210 struct revert_file_args *a = arg;
4211 const struct got_error *err = NULL;
4212 char *parent_path = NULL;
4213 struct got_fileindex_entry *ie;
4214 struct got_tree_object *tree = NULL;
4215 struct got_object_id *tree_id = NULL;
4216 const struct got_tree_entry *te = NULL;
4217 char *tree_path = NULL, *te_name;
4218 char *ondisk_path = NULL, *path_content = NULL;
4219 struct got_blob_object *blob = NULL;
4221 /* Reverting a staged deletion is a no-op. */
4222 if (status == GOT_STATUS_DELETE &&
4223 staged_status != GOT_STATUS_NO_CHANGE)
4226 if (status == GOT_STATUS_UNVERSIONED)
4227 return (*a->progress_cb)(a->progress_arg,
4228 GOT_STATUS_UNVERSIONED, relpath);
4230 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4232 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4234 /* Construct in-repository path of tree which contains this blob. */
4235 err = got_path_dirname(&parent_path, ie->path);
4237 if (err->code != GOT_ERR_BAD_PATH)
4239 parent_path = strdup("/");
4240 if (parent_path == NULL) {
4241 err = got_error_from_errno("strdup");
4245 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4246 tree_path = strdup(parent_path);
4247 if (tree_path == NULL) {
4248 err = got_error_from_errno("strdup");
4252 if (got_path_is_root_dir(parent_path)) {
4253 tree_path = strdup(a->worktree->path_prefix);
4254 if (tree_path == NULL) {
4255 err = got_error_from_errno("strdup");
4259 if (asprintf(&tree_path, "%s/%s",
4260 a->worktree->path_prefix, parent_path) == -1) {
4261 err = got_error_from_errno("asprintf");
4267 err = got_object_id_by_path(&tree_id, a->repo,
4268 a->worktree->base_commit_id, tree_path);
4270 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4271 (status == GOT_STATUS_ADD ||
4272 staged_status == GOT_STATUS_ADD)))
4275 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4279 te_name = basename(ie->path);
4280 if (te_name == NULL) {
4281 err = got_error_from_errno2("basename", ie->path);
4285 te = got_object_tree_find_entry(tree, te_name);
4286 if (te == NULL && status != GOT_STATUS_ADD &&
4287 staged_status != GOT_STATUS_ADD) {
4288 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4294 case GOT_STATUS_ADD:
4296 int choice = GOT_PATCH_CHOICE_NONE;
4297 err = (*a->patch_cb)(&choice, a->patch_arg,
4298 status, ie->path, NULL, 1, 1);
4301 if (choice != GOT_PATCH_CHOICE_YES)
4304 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4308 got_fileindex_entry_remove(a->fileindex, ie);
4310 case GOT_STATUS_DELETE:
4312 int choice = GOT_PATCH_CHOICE_NONE;
4313 err = (*a->patch_cb)(&choice, a->patch_arg,
4314 status, ie->path, NULL, 1, 1);
4317 if (choice != GOT_PATCH_CHOICE_YES)
4321 case GOT_STATUS_MODIFY:
4322 case GOT_STATUS_MODE_CHANGE:
4323 case GOT_STATUS_CONFLICT:
4324 case GOT_STATUS_MISSING: {
4325 struct got_object_id id;
4326 if (staged_status == GOT_STATUS_ADD ||
4327 staged_status == GOT_STATUS_MODIFY) {
4328 memcpy(id.sha1, ie->staged_blob_sha1,
4329 SHA1_DIGEST_LENGTH);
4331 memcpy(id.sha1, ie->blob_sha1,
4332 SHA1_DIGEST_LENGTH);
4333 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4337 if (asprintf(&ondisk_path, "%s/%s",
4338 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4339 err = got_error_from_errno("asprintf");
4343 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4344 status == GOT_STATUS_CONFLICT)) {
4345 err = create_patched_content(&path_content, 1, &id,
4346 ondisk_path, dirfd, de_name, ie->path, a->repo,
4347 a->patch_cb, a->patch_arg);
4348 if (err || path_content == NULL)
4350 if (rename(path_content, ondisk_path) == -1) {
4351 err = got_error_from_errno3("rename",
4352 path_content, ondisk_path);
4356 int is_bad_symlink = 0;
4357 if (te && S_ISLNK(te->mode)) {
4358 err = install_symlink(&is_bad_symlink,
4359 a->worktree, ondisk_path, ie->path,
4360 blob, 0, 1, 0, a->repo,
4361 a->progress_cb, a->progress_arg);
4363 err = install_blob(a->worktree, ondisk_path,
4365 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4366 got_fileindex_perms_to_st(ie), blob,
4367 0, 1, 0, 0, a->repo,
4368 a->progress_cb, a->progress_arg);
4372 if (status == GOT_STATUS_DELETE ||
4373 status == GOT_STATUS_MODE_CHANGE) {
4374 err = got_fileindex_entry_update(ie,
4375 ondisk_path, blob->id.sha1,
4376 a->worktree->base_commit_id->sha1, 1);
4380 if (is_bad_symlink) {
4381 err = got_fileindex_entry_filetype_set(ie,
4382 GOT_FILEIDX_MODE_BAD_SYMLINK);
4398 got_object_blob_close(blob);
4400 got_object_tree_close(tree);
4405 const struct got_error *
4406 got_worktree_revert(struct got_worktree *worktree,
4407 struct got_pathlist_head *paths,
4408 got_worktree_checkout_cb progress_cb, void *progress_arg,
4409 got_worktree_patch_cb patch_cb, void *patch_arg,
4410 struct got_repository *repo)
4412 struct got_fileindex *fileindex = NULL;
4413 char *fileindex_path = NULL;
4414 const struct got_error *err = NULL, *unlockerr = NULL;
4415 const struct got_error *sync_err = NULL;
4416 struct got_pathlist_entry *pe;
4417 struct revert_file_args rfa;
4419 err = lock_worktree(worktree, LOCK_EX);
4423 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4427 rfa.worktree = worktree;
4428 rfa.fileindex = fileindex;
4429 rfa.progress_cb = progress_cb;
4430 rfa.progress_arg = progress_arg;
4431 rfa.patch_cb = patch_cb;
4432 rfa.patch_arg = patch_arg;
4434 TAILQ_FOREACH(pe, paths, entry) {
4435 err = worktree_status(worktree, pe->path, fileindex, repo,
4436 revert_file, &rfa, NULL, NULL, 0, 0);
4440 sync_err = sync_fileindex(fileindex, fileindex_path);
4441 if (sync_err && err == NULL)
4444 free(fileindex_path);
4446 got_fileindex_free(fileindex);
4447 unlockerr = lock_worktree(worktree, LOCK_SH);
4448 if (unlockerr && err == NULL)
4454 free_commitable(struct got_commitable *ct)
4457 free(ct->in_repo_path);
4458 free(ct->ondisk_path);
4460 free(ct->base_blob_id);
4461 free(ct->staged_blob_id);
4462 free(ct->base_commit_id);
4466 struct collect_commitables_arg {
4467 struct got_pathlist_head *commitable_paths;
4468 struct got_repository *repo;
4469 struct got_worktree *worktree;
4470 int have_staged_files;
4473 static const struct got_error *
4474 collect_commitables(void *arg, unsigned char status,
4475 unsigned char staged_status, const char *relpath,
4476 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4477 struct got_object_id *commit_id, int dirfd, const char *de_name)
4479 struct collect_commitables_arg *a = arg;
4480 const struct got_error *err = NULL;
4481 struct got_commitable *ct = NULL;
4482 struct got_pathlist_entry *new = NULL;
4483 char *parent_path = NULL, *path = NULL;
4486 if (a->have_staged_files) {
4487 if (staged_status != GOT_STATUS_MODIFY &&
4488 staged_status != GOT_STATUS_ADD &&
4489 staged_status != GOT_STATUS_DELETE)
4492 if (status == GOT_STATUS_CONFLICT)
4493 return got_error(GOT_ERR_COMMIT_CONFLICT);
4495 if (status != GOT_STATUS_MODIFY &&
4496 status != GOT_STATUS_MODE_CHANGE &&
4497 status != GOT_STATUS_ADD &&
4498 status != GOT_STATUS_DELETE)
4502 if (asprintf(&path, "/%s", relpath) == -1) {
4503 err = got_error_from_errno("asprintf");
4506 if (strcmp(path, "/") == 0) {
4507 parent_path = strdup("");
4508 if (parent_path == NULL)
4509 return got_error_from_errno("strdup");
4511 err = got_path_dirname(&parent_path, path);
4516 ct = calloc(1, sizeof(*ct));
4518 err = got_error_from_errno("calloc");
4522 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4524 err = got_error_from_errno("asprintf");
4527 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4528 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4531 if (fstatat(dirfd, de_name, &sb,
4532 AT_SYMLINK_NOFOLLOW) == -1) {
4533 err = got_error_from_errno2("fstatat",
4537 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4538 err = got_error_from_errno2("lstat", ct->ondisk_path);
4541 ct->mode = sb.st_mode;
4544 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4545 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4547 err = got_error_from_errno("asprintf");
4551 ct->status = status;
4552 ct->staged_status = staged_status;
4553 ct->blob_id = NULL; /* will be filled in when blob gets created */
4554 if (ct->status != GOT_STATUS_ADD &&
4555 ct->staged_status != GOT_STATUS_ADD) {
4556 ct->base_blob_id = got_object_id_dup(blob_id);
4557 if (ct->base_blob_id == NULL) {
4558 err = got_error_from_errno("got_object_id_dup");
4561 ct->base_commit_id = got_object_id_dup(commit_id);
4562 if (ct->base_commit_id == NULL) {
4563 err = got_error_from_errno("got_object_id_dup");
4567 if (ct->staged_status == GOT_STATUS_ADD ||
4568 ct->staged_status == GOT_STATUS_MODIFY) {
4569 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4570 if (ct->staged_blob_id == NULL) {
4571 err = got_error_from_errno("got_object_id_dup");
4575 ct->path = strdup(path);
4576 if (ct->path == NULL) {
4577 err = got_error_from_errno("strdup");
4580 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4582 if (ct && (err || new == NULL))
4583 free_commitable(ct);
4589 static const struct got_error *write_tree(struct got_object_id **, int *,
4590 struct got_tree_object *, const char *, struct got_pathlist_head *,
4591 got_worktree_status_cb status_cb, void *status_arg,
4592 struct got_repository *);
4594 static const struct got_error *
4595 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4596 struct got_tree_entry *te, const char *parent_path,
4597 struct got_pathlist_head *commitable_paths,
4598 got_worktree_status_cb status_cb, void *status_arg,
4599 struct got_repository *repo)
4601 const struct got_error *err = NULL;
4602 struct got_tree_object *subtree;
4605 if (asprintf(&subpath, "%s%s%s", parent_path,
4606 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4607 return got_error_from_errno("asprintf");
4609 err = got_object_open_as_tree(&subtree, repo, &te->id);
4613 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4614 commitable_paths, status_cb, status_arg, repo);
4615 got_object_tree_close(subtree);
4620 static const struct got_error *
4621 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4623 const struct got_error *err = NULL;
4624 char *ct_parent_path = NULL;
4628 if (strchr(ct->in_repo_path, '/') == NULL) {
4629 *match = got_path_is_root_dir(path);
4633 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4636 *match = (strcmp(path, ct_parent_path) == 0);
4637 free(ct_parent_path);
4642 get_ct_file_mode(struct got_commitable *ct)
4644 if (S_ISLNK(ct->mode))
4647 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4650 static const struct got_error *
4651 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4652 struct got_tree_entry *te, struct got_commitable *ct)
4654 const struct got_error *err = NULL;
4658 err = got_object_tree_entry_dup(new_te, te);
4662 (*new_te)->mode = get_ct_file_mode(ct);
4664 if (ct->staged_status == GOT_STATUS_MODIFY)
4665 memcpy(&(*new_te)->id, ct->staged_blob_id,
4666 sizeof((*new_te)->id));
4668 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4670 if (err && *new_te) {
4677 static const struct got_error *
4678 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4679 struct got_commitable *ct)
4681 const struct got_error *err = NULL;
4686 *new_te = calloc(1, sizeof(**new_te));
4687 if (*new_te == NULL)
4688 return got_error_from_errno("calloc");
4690 ct_name = basename(ct->path);
4691 if (ct_name == NULL) {
4692 err = got_error_from_errno2("basename", ct->path);
4695 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4696 sizeof((*new_te)->name)) {
4697 err = got_error(GOT_ERR_NO_SPACE);
4701 (*new_te)->mode = get_ct_file_mode(ct);
4703 if (ct->staged_status == GOT_STATUS_ADD)
4704 memcpy(&(*new_te)->id, ct->staged_blob_id,
4705 sizeof((*new_te)->id));
4707 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4709 if (err && *new_te) {
4716 static const struct got_error *
4717 insert_tree_entry(struct got_tree_entry *new_te,
4718 struct got_pathlist_head *paths)
4720 const struct got_error *err = NULL;
4721 struct got_pathlist_entry *new_pe;
4723 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4727 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4731 static const struct got_error *
4732 report_ct_status(struct got_commitable *ct,
4733 got_worktree_status_cb status_cb, void *status_arg)
4735 const char *ct_path = ct->path;
4736 unsigned char status;
4738 while (ct_path[0] == '/')
4741 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4742 status = ct->staged_status;
4744 status = ct->status;
4746 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4747 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4750 static const struct got_error *
4751 match_modified_subtree(int *modified, struct got_tree_entry *te,
4752 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4754 const struct got_error *err = NULL;
4755 struct got_pathlist_entry *pe;
4760 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4761 got_path_is_root_dir(base_tree_path) ? "" : "/",
4763 return got_error_from_errno("asprintf");
4765 TAILQ_FOREACH(pe, commitable_paths, entry) {
4766 struct got_commitable *ct = pe->data;
4767 *modified = got_path_is_child(ct->in_repo_path, te_path,
4777 static const struct got_error *
4778 match_deleted_or_modified_ct(struct got_commitable **ctp,
4779 struct got_tree_entry *te, const char *base_tree_path,
4780 struct got_pathlist_head *commitable_paths)
4782 const struct got_error *err = NULL;
4783 struct got_pathlist_entry *pe;
4787 TAILQ_FOREACH(pe, commitable_paths, entry) {
4788 struct got_commitable *ct = pe->data;
4789 char *ct_name = NULL;
4792 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4793 if (ct->status != GOT_STATUS_MODIFY &&
4794 ct->status != GOT_STATUS_MODE_CHANGE &&
4795 ct->status != GOT_STATUS_DELETE)
4798 if (ct->staged_status != GOT_STATUS_MODIFY &&
4799 ct->staged_status != GOT_STATUS_DELETE)
4803 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4806 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4812 ct_name = basename(pe->path);
4813 if (ct_name == NULL)
4814 return got_error_from_errno2("basename", pe->path);
4816 if (strcmp(te->name, ct_name) != 0)
4826 static const struct got_error *
4827 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4828 const char *child_path, const char *path_base_tree,
4829 struct got_pathlist_head *commitable_paths,
4830 got_worktree_status_cb status_cb, void *status_arg,
4831 struct got_repository *repo)
4833 const struct got_error *err = NULL;
4834 struct got_tree_entry *new_te;
4836 struct got_object_id *id = NULL;
4841 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4842 got_path_is_root_dir(path_base_tree) ? "" : "/",
4844 return got_error_from_errno("asprintf");
4846 new_te = calloc(1, sizeof(*new_te));
4848 return got_error_from_errno("calloc");
4849 new_te->mode = S_IFDIR;
4851 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4852 sizeof(new_te->name)) {
4853 err = got_error(GOT_ERR_NO_SPACE);
4856 err = write_tree(&id, &nentries, NULL, subtree_path,
4857 commitable_paths, status_cb, status_arg, repo);
4862 memcpy(&new_te->id, id, sizeof(new_te->id));
4871 static const struct got_error *
4872 write_tree(struct got_object_id **new_tree_id, int *nentries,
4873 struct got_tree_object *base_tree, const char *path_base_tree,
4874 struct got_pathlist_head *commitable_paths,
4875 got_worktree_status_cb status_cb, void *status_arg,
4876 struct got_repository *repo)
4878 const struct got_error *err = NULL;
4879 struct got_pathlist_head paths;
4880 struct got_tree_entry *te, *new_te = NULL;
4881 struct got_pathlist_entry *pe;
4886 /* Insert, and recurse into, newly added entries first. */
4887 TAILQ_FOREACH(pe, commitable_paths, entry) {
4888 struct got_commitable *ct = pe->data;
4889 char *child_path = NULL, *slash;
4891 if ((ct->status != GOT_STATUS_ADD &&
4892 ct->staged_status != GOT_STATUS_ADD) ||
4893 (ct->flags & GOT_COMMITABLE_ADDED))
4896 if (!got_path_is_child(pe->path, path_base_tree,
4897 strlen(path_base_tree)))
4900 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4905 slash = strchr(child_path, '/');
4906 if (slash == NULL) {
4907 err = alloc_added_blob_tree_entry(&new_te, ct);
4910 err = report_ct_status(ct, status_cb, status_arg);
4913 ct->flags |= GOT_COMMITABLE_ADDED;
4914 err = insert_tree_entry(new_te, &paths);
4919 *slash = '\0'; /* trim trailing path components */
4920 if (base_tree == NULL ||
4921 got_object_tree_find_entry(base_tree, child_path)
4923 err = make_subtree_for_added_blob(&new_te,
4924 child_path, path_base_tree,
4925 commitable_paths, status_cb, status_arg,
4929 err = insert_tree_entry(new_te, &paths);
4938 int i, nbase_entries;
4939 /* Handle modified and deleted entries. */
4940 nbase_entries = got_object_tree_get_nentries(base_tree);
4941 for (i = 0; i < nbase_entries; i++) {
4942 struct got_commitable *ct = NULL;
4944 te = got_object_tree_get_entry(base_tree, i);
4945 if (got_object_tree_entry_is_submodule(te)) {
4946 /* Entry is a submodule; just copy it. */
4947 err = got_object_tree_entry_dup(&new_te, te);
4950 err = insert_tree_entry(new_te, &paths);
4957 if (S_ISDIR(te->mode)) {
4959 err = got_object_tree_entry_dup(&new_te, te);
4962 err = match_modified_subtree(&modified, te,
4963 path_base_tree, commitable_paths);
4966 /* Avoid recursion into unmodified subtrees. */
4968 struct got_object_id *new_id;
4970 err = write_subtree(&new_id,
4972 path_base_tree, commitable_paths,
4973 status_cb, status_arg, repo);
4976 if (nsubentries == 0) {
4977 /* All entries were deleted. */
4981 memcpy(&new_te->id, new_id,
4982 sizeof(new_te->id));
4985 err = insert_tree_entry(new_te, &paths);
4992 err = match_deleted_or_modified_ct(&ct, te,
4993 path_base_tree, commitable_paths);
4997 /* NB: Deleted entries get dropped here. */
4998 if (ct->status == GOT_STATUS_MODIFY ||
4999 ct->status == GOT_STATUS_MODE_CHANGE ||
5000 ct->staged_status == GOT_STATUS_MODIFY) {
5001 err = alloc_modified_blob_tree_entry(
5005 err = insert_tree_entry(new_te, &paths);
5010 err = report_ct_status(ct, status_cb,
5015 /* Entry is unchanged; just copy it. */
5016 err = got_object_tree_entry_dup(&new_te, te);
5019 err = insert_tree_entry(new_te, &paths);
5027 /* Write new list of entries; deleted entries have been dropped. */
5028 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5030 got_pathlist_free(&paths);
5034 static const struct got_error *
5035 reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
5036 struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
5037 struct got_repository *repo)
5039 const struct got_error *err = NULL;
5040 struct got_blob_object *blob = NULL;
5041 struct got_object_id *tree_id = NULL;
5042 char *tree_path = NULL;
5043 struct got_tree_object *tree = NULL;
5044 struct got_tree_entry *te;
5047 err = got_object_open_as_blob(&blob, repo, ct->blob_id, PATH_MAX);
5051 err = got_path_dirname(&tree_path, ct->in_repo_path);
5053 if (err->code != GOT_ERR_BAD_PATH)
5055 err = got_object_id_by_path(&tree_id, repo,
5056 new_base_commit_id, "");
5060 err = got_object_id_by_path(&tree_id, repo,
5061 new_base_commit_id, tree_path);
5066 err = got_object_open_as_tree(&tree, repo, tree_id);
5070 entry_name = basename(ct->path);
5071 if (entry_name == NULL) {
5072 err = got_error_from_errno2("basename", ct->path);
5076 te = got_object_tree_find_entry(tree, entry_name);
5078 err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5082 err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5083 ct->path, blob, 0, 0, 0, repo, NULL, NULL);
5086 got_object_blob_close(blob);
5088 got_object_tree_close(tree);
5095 * After comitting a symlink we have a chance to convert "bad" symlinks
5096 * (those which point outside the work tree or into .got) to regular files.
5097 * This way, the post-commit work tree state matches a fresh checkout of
5098 * the tree which was just committed. We also mark such newly committed
5099 * symlinks as "bad" in the work tree's fileindex.
5101 static const struct got_error *
5102 reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5103 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5104 struct got_worktree *worktree, struct got_repository *repo)
5106 const struct got_error *err = NULL;
5107 struct got_pathlist_entry *pe;
5109 TAILQ_FOREACH(pe, commitable_paths, entry) {
5110 struct got_commitable *ct = pe->data;
5111 struct got_fileindex_entry *ie;
5112 int is_bad_symlink = 0;
5114 if (!S_ISLNK(get_ct_file_mode(ct)))
5117 err = reinstall_symlink_after_commit(&is_bad_symlink,
5118 ct, new_base_commit_id, worktree, repo);
5121 ie = got_fileindex_entry_get(fileindex, ct->path,
5123 if (ie && is_bad_symlink) {
5124 err = got_fileindex_entry_filetype_set(ie,
5125 GOT_FILEIDX_MODE_BAD_SYMLINK);
5134 static const struct got_error *
5135 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5136 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5137 int have_staged_files)
5139 const struct got_error *err = NULL;
5140 struct got_pathlist_entry *pe;
5142 TAILQ_FOREACH(pe, commitable_paths, entry) {
5143 struct got_fileindex_entry *ie;
5144 struct got_commitable *ct = pe->data;
5146 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5148 if (ct->status == GOT_STATUS_DELETE ||
5149 ct->staged_status == GOT_STATUS_DELETE) {
5150 got_fileindex_entry_remove(fileindex, ie);
5151 } else if (ct->staged_status == GOT_STATUS_ADD ||
5152 ct->staged_status == GOT_STATUS_MODIFY) {
5153 got_fileindex_entry_stage_set(ie,
5154 GOT_FILEIDX_STAGE_NONE);
5155 err = got_fileindex_entry_update(ie,
5156 ct->ondisk_path, ct->staged_blob_id->sha1,
5157 new_base_commit_id->sha1,
5158 !have_staged_files);
5160 err = got_fileindex_entry_update(ie,
5161 ct->ondisk_path, ct->blob_id->sha1,
5162 new_base_commit_id->sha1,
5163 !have_staged_files);
5165 err = got_fileindex_entry_alloc(&ie, pe->path);
5168 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5169 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5171 got_fileindex_entry_free(ie);
5174 err = got_fileindex_entry_add(fileindex, ie);
5176 got_fileindex_entry_free(ie);
5185 static const struct got_error *
5186 check_out_of_date(const char *in_repo_path, unsigned char status,
5187 unsigned char staged_status, struct got_object_id *base_blob_id,
5188 struct got_object_id *base_commit_id,
5189 struct got_object_id *head_commit_id, struct got_repository *repo,
5192 const struct got_error *err = NULL;
5193 struct got_object_id *id = NULL;
5195 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5196 /* Trivial case: base commit == head commit */
5197 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5200 * Ensure file content which local changes were based
5201 * on matches file content in the branch head.
5203 err = got_object_id_by_path(&id, repo, head_commit_id,
5206 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5207 err = got_error(ood_errcode);
5209 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5210 err = got_error(ood_errcode);
5212 /* Require that added files don't exist in the branch head. */
5213 err = got_object_id_by_path(&id, repo, head_commit_id,
5215 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5217 err = id ? got_error(ood_errcode) : NULL;
5224 const struct got_error *
5225 commit_worktree(struct got_object_id **new_commit_id,
5226 struct got_pathlist_head *commitable_paths,
5227 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5228 const char *author, const char *committer,
5229 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5230 got_worktree_status_cb status_cb, void *status_arg,
5231 struct got_repository *repo)
5233 const struct got_error *err = NULL, *unlockerr = NULL;
5234 struct got_pathlist_entry *pe;
5235 const char *head_ref_name = NULL;
5236 struct got_commit_object *head_commit = NULL;
5237 struct got_reference *head_ref2 = NULL;
5238 struct got_object_id *head_commit_id2 = NULL;
5239 struct got_tree_object *head_tree = NULL;
5240 struct got_object_id *new_tree_id = NULL;
5242 struct got_object_id_queue parent_ids;
5243 struct got_object_qid *pid = NULL;
5244 char *logmsg = NULL;
5246 *new_commit_id = NULL;
5248 SIMPLEQ_INIT(&parent_ids);
5250 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5254 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5258 if (commit_msg_cb != NULL) {
5259 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5264 if (logmsg == NULL || strlen(logmsg) == 0) {
5265 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5269 /* Create blobs from added and modified files and record their IDs. */
5270 TAILQ_FOREACH(pe, commitable_paths, entry) {
5271 struct got_commitable *ct = pe->data;
5274 /* Blobs for staged files already exist. */
5275 if (ct->staged_status == GOT_STATUS_ADD ||
5276 ct->staged_status == GOT_STATUS_MODIFY)
5279 if (ct->status != GOT_STATUS_ADD &&
5280 ct->status != GOT_STATUS_MODIFY &&
5281 ct->status != GOT_STATUS_MODE_CHANGE)
5284 if (asprintf(&ondisk_path, "%s/%s",
5285 worktree->root_path, pe->path) == -1) {
5286 err = got_error_from_errno("asprintf");
5289 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5295 /* Recursively write new tree objects. */
5296 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5297 commitable_paths, status_cb, status_arg, repo);
5301 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5304 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5305 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5306 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5307 got_object_qid_free(pid);
5313 /* Check if a concurrent commit to our branch has occurred. */
5314 head_ref_name = got_worktree_get_head_ref_name(worktree);
5315 if (head_ref_name == NULL) {
5316 err = got_error_from_errno("got_worktree_get_head_ref_name");
5319 /* Lock the reference here to prevent concurrent modification. */
5320 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5323 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5326 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5327 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5330 /* Update branch head in repository. */
5331 err = got_ref_change_ref(head_ref2, *new_commit_id);
5334 err = got_ref_write(head_ref2, repo);
5338 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5342 err = ref_base_commit(worktree, repo);
5347 got_object_tree_close(head_tree);
5349 got_object_commit_close(head_commit);
5350 free(head_commit_id2);
5352 unlockerr = got_ref_unlock(head_ref2);
5353 if (unlockerr && err == NULL)
5355 got_ref_close(head_ref2);
5360 static const struct got_error *
5361 check_path_is_commitable(const char *path,
5362 struct got_pathlist_head *commitable_paths)
5364 struct got_pathlist_entry *cpe = NULL;
5365 size_t path_len = strlen(path);
5367 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5368 struct got_commitable *ct = cpe->data;
5369 const char *ct_path = ct->path;
5371 while (ct_path[0] == '/')
5374 if (strcmp(path, ct_path) == 0 ||
5375 got_path_is_child(ct_path, path, path_len))
5380 return got_error_path(path, GOT_ERR_BAD_PATH);
5385 static const struct got_error *
5386 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5388 int *have_staged_files = arg;
5390 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5391 *have_staged_files = 1;
5392 return got_error(GOT_ERR_CANCELLED);
5398 static const struct got_error *
5399 check_non_staged_files(struct got_fileindex *fileindex,
5400 struct got_pathlist_head *paths)
5402 struct got_pathlist_entry *pe;
5403 struct got_fileindex_entry *ie;
5405 TAILQ_FOREACH(pe, paths, entry) {
5406 if (pe->path[0] == '\0')
5408 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5410 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5411 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5412 return got_error_path(pe->path,
5413 GOT_ERR_FILE_NOT_STAGED);
5419 const struct got_error *
5420 got_worktree_commit(struct got_object_id **new_commit_id,
5421 struct got_worktree *worktree, struct got_pathlist_head *paths,
5422 const char *author, const char *committer,
5423 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5424 got_worktree_status_cb status_cb, void *status_arg,
5425 struct got_repository *repo)
5427 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5428 struct got_fileindex *fileindex = NULL;
5429 char *fileindex_path = NULL;
5430 struct got_pathlist_head commitable_paths;
5431 struct collect_commitables_arg cc_arg;
5432 struct got_pathlist_entry *pe;
5433 struct got_reference *head_ref = NULL;
5434 struct got_object_id *head_commit_id = NULL;
5435 int have_staged_files = 0;
5437 *new_commit_id = NULL;
5439 TAILQ_INIT(&commitable_paths);
5441 err = lock_worktree(worktree, LOCK_EX);
5445 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5449 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5453 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5457 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5458 &have_staged_files);
5459 if (err && err->code != GOT_ERR_CANCELLED)
5461 if (have_staged_files) {
5462 err = check_non_staged_files(fileindex, paths);
5467 cc_arg.commitable_paths = &commitable_paths;
5468 cc_arg.worktree = worktree;
5470 cc_arg.have_staged_files = have_staged_files;
5471 TAILQ_FOREACH(pe, paths, entry) {
5472 err = worktree_status(worktree, pe->path, fileindex, repo,
5473 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5478 if (TAILQ_EMPTY(&commitable_paths)) {
5479 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5483 TAILQ_FOREACH(pe, paths, entry) {
5484 err = check_path_is_commitable(pe->path, &commitable_paths);
5489 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5490 struct got_commitable *ct = pe->data;
5491 const char *ct_path = ct->in_repo_path;
5493 while (ct_path[0] == '/')
5495 err = check_out_of_date(ct_path, ct->status,
5496 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5497 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5503 err = commit_worktree(new_commit_id, &commitable_paths,
5504 head_commit_id, worktree, author, committer,
5505 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5509 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5510 fileindex, have_staged_files);
5512 err = reinstall_symlinks_after_commit(&commitable_paths,
5513 *new_commit_id, fileindex, worktree, repo);
5515 sync_err = sync_fileindex(fileindex, fileindex_path);
5516 if (sync_err && err == NULL)
5520 got_fileindex_free(fileindex);
5521 free(fileindex_path);
5522 unlockerr = lock_worktree(worktree, LOCK_SH);
5523 if (unlockerr && err == NULL)
5525 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5526 struct got_commitable *ct = pe->data;
5527 free_commitable(ct);
5529 got_pathlist_free(&commitable_paths);
5534 got_commitable_get_path(struct got_commitable *ct)
5540 got_commitable_get_status(struct got_commitable *ct)
5545 struct check_rebase_ok_arg {
5546 struct got_worktree *worktree;
5547 struct got_repository *repo;
5550 static const struct got_error *
5551 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5553 const struct got_error *err = NULL;
5554 struct check_rebase_ok_arg *a = arg;
5555 unsigned char status;
5559 /* Reject rebase of a work tree with mixed base commits. */
5560 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5561 SHA1_DIGEST_LENGTH))
5562 return got_error(GOT_ERR_MIXED_COMMITS);
5564 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5566 return got_error_from_errno("asprintf");
5568 /* Reject rebase of a work tree with modified or staged files. */
5569 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5574 if (status != GOT_STATUS_NO_CHANGE)
5575 return got_error(GOT_ERR_MODIFIED);
5576 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5577 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5582 const struct got_error *
5583 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5584 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5585 struct got_worktree *worktree, struct got_reference *branch,
5586 struct got_repository *repo)
5588 const struct got_error *err = NULL;
5589 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5590 char *branch_ref_name = NULL;
5591 char *fileindex_path = NULL;
5592 struct check_rebase_ok_arg ok_arg;
5593 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5594 struct got_object_id *wt_branch_tip = NULL;
5596 *new_base_branch_ref = NULL;
5600 err = lock_worktree(worktree, LOCK_EX);
5604 err = open_fileindex(fileindex, &fileindex_path, worktree);
5608 ok_arg.worktree = worktree;
5610 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5615 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5619 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5623 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5627 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5632 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5635 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5636 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5640 err = got_ref_alloc_symref(new_base_branch_ref,
5641 new_base_branch_ref_name, wt_branch);
5644 err = got_ref_write(*new_base_branch_ref, repo);
5648 /* TODO Lock original branch's ref while rebasing? */
5650 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5654 err = got_ref_write(branch_ref, repo);
5658 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5659 worktree->base_commit_id);
5662 err = got_ref_write(*tmp_branch, repo);
5666 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5670 free(fileindex_path);
5671 free(tmp_branch_name);
5672 free(new_base_branch_ref_name);
5673 free(branch_ref_name);
5675 got_ref_close(branch_ref);
5677 got_ref_close(wt_branch);
5678 free(wt_branch_tip);
5680 if (*new_base_branch_ref) {
5681 got_ref_close(*new_base_branch_ref);
5682 *new_base_branch_ref = NULL;
5685 got_ref_close(*tmp_branch);
5689 got_fileindex_free(*fileindex);
5692 lock_worktree(worktree, LOCK_SH);
5697 const struct got_error *
5698 got_worktree_rebase_continue(struct got_object_id **commit_id,
5699 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5700 struct got_reference **branch, struct got_fileindex **fileindex,
5701 struct got_worktree *worktree, struct got_repository *repo)
5703 const struct got_error *err;
5704 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5705 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5706 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5707 char *fileindex_path = NULL;
5708 int have_staged_files = 0;
5711 *new_base_branch = NULL;
5716 err = lock_worktree(worktree, LOCK_EX);
5720 err = open_fileindex(fileindex, &fileindex_path, worktree);
5724 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5725 &have_staged_files);
5726 if (err && err->code != GOT_ERR_CANCELLED)
5728 if (have_staged_files) {
5729 err = got_error(GOT_ERR_STAGED_PATHS);
5733 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5737 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5741 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5745 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5749 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5753 err = got_ref_open(branch, repo,
5754 got_ref_get_symref_target(branch_ref), 0);
5758 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5762 err = got_ref_resolve(commit_id, repo, commit_ref);
5766 err = got_ref_open(new_base_branch, repo,
5767 new_base_branch_ref_name, 0);
5771 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5775 free(commit_ref_name);
5776 free(branch_ref_name);
5777 free(fileindex_path);
5779 got_ref_close(commit_ref);
5781 got_ref_close(branch_ref);
5786 got_ref_close(*tmp_branch);
5789 if (*new_base_branch) {
5790 got_ref_close(*new_base_branch);
5791 *new_base_branch = NULL;
5794 got_ref_close(*branch);
5798 got_fileindex_free(*fileindex);
5801 lock_worktree(worktree, LOCK_SH);
5806 const struct got_error *
5807 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5809 const struct got_error *err;
5810 char *tmp_branch_name = NULL;
5812 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5816 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5817 free(tmp_branch_name);
5821 static const struct got_error *
5822 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5823 char **logmsg, void *arg)
5829 static const struct got_error *
5830 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5831 const char *path, struct got_object_id *blob_id,
5832 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5833 int dirfd, const char *de_name)
5838 struct collect_merged_paths_arg {
5839 got_worktree_checkout_cb progress_cb;
5841 struct got_pathlist_head *merged_paths;
5844 static const struct got_error *
5845 collect_merged_paths(void *arg, unsigned char status, const char *path)
5847 const struct got_error *err;
5848 struct collect_merged_paths_arg *a = arg;
5850 struct got_pathlist_entry *new;
5852 err = (*a->progress_cb)(a->progress_arg, status, path);
5856 if (status != GOT_STATUS_MERGE &&
5857 status != GOT_STATUS_ADD &&
5858 status != GOT_STATUS_DELETE &&
5859 status != GOT_STATUS_CONFLICT)
5864 return got_error_from_errno("strdup");
5866 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5867 if (err || new == NULL)
5873 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5875 struct got_pathlist_entry *pe;
5877 TAILQ_FOREACH(pe, merged_paths, entry)
5878 free((char *)pe->path);
5880 got_pathlist_free(merged_paths);
5883 static const struct got_error *
5884 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5885 int is_rebase, struct got_repository *repo)
5887 const struct got_error *err;
5888 struct got_reference *commit_ref = NULL;
5890 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5892 if (err->code != GOT_ERR_NOT_REF)
5894 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5897 err = got_ref_write(commit_ref, repo);
5900 } else if (is_rebase) {
5901 struct got_object_id *stored_id;
5904 err = got_ref_resolve(&stored_id, repo, commit_ref);
5907 cmp = got_object_id_cmp(commit_id, stored_id);
5910 err = got_error(GOT_ERR_REBASE_COMMITID);
5916 got_ref_close(commit_ref);
5920 static const struct got_error *
5921 rebase_merge_files(struct got_pathlist_head *merged_paths,
5922 const char *commit_ref_name, struct got_worktree *worktree,
5923 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5924 struct got_object_id *commit_id, struct got_repository *repo,
5925 got_worktree_checkout_cb progress_cb, void *progress_arg,
5926 got_cancel_cb cancel_cb, void *cancel_arg)
5928 const struct got_error *err;
5929 struct got_reference *commit_ref = NULL;
5930 struct collect_merged_paths_arg cmp_arg;
5931 char *fileindex_path;
5933 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5935 err = get_fileindex_path(&fileindex_path, worktree);
5939 cmp_arg.progress_cb = progress_cb;
5940 cmp_arg.progress_arg = progress_arg;
5941 cmp_arg.merged_paths = merged_paths;
5942 err = merge_files(worktree, fileindex, fileindex_path,
5943 parent_commit_id, commit_id, repo, collect_merged_paths,
5944 &cmp_arg, cancel_cb, cancel_arg);
5946 got_ref_close(commit_ref);
5950 const struct got_error *
5951 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5952 struct got_worktree *worktree, struct got_fileindex *fileindex,
5953 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5954 struct got_repository *repo,
5955 got_worktree_checkout_cb progress_cb, void *progress_arg,
5956 got_cancel_cb cancel_cb, void *cancel_arg)
5958 const struct got_error *err;
5959 char *commit_ref_name;
5961 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5965 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5969 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5970 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5971 progress_arg, cancel_cb, cancel_arg);
5973 free(commit_ref_name);
5977 const struct got_error *
5978 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5979 struct got_worktree *worktree, struct got_fileindex *fileindex,
5980 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5981 struct got_repository *repo,
5982 got_worktree_checkout_cb progress_cb, void *progress_arg,
5983 got_cancel_cb cancel_cb, void *cancel_arg)
5985 const struct got_error *err;
5986 char *commit_ref_name;
5988 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5992 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5996 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5997 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5998 progress_arg, cancel_cb, cancel_arg);
6000 free(commit_ref_name);
6004 static const struct got_error *
6005 rebase_commit(struct got_object_id **new_commit_id,
6006 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6007 struct got_worktree *worktree, struct got_fileindex *fileindex,
6008 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6009 const char *new_logmsg, struct got_repository *repo)
6011 const struct got_error *err, *sync_err;
6012 struct got_pathlist_head commitable_paths;
6013 struct collect_commitables_arg cc_arg;
6014 char *fileindex_path = NULL;
6015 struct got_reference *head_ref = NULL;
6016 struct got_object_id *head_commit_id = NULL;
6017 char *logmsg = NULL;
6019 TAILQ_INIT(&commitable_paths);
6020 *new_commit_id = NULL;
6022 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6024 err = get_fileindex_path(&fileindex_path, worktree);
6028 cc_arg.commitable_paths = &commitable_paths;
6029 cc_arg.worktree = worktree;
6031 cc_arg.have_staged_files = 0;
6033 * If possible get the status of individual files directly to
6034 * avoid crawling the entire work tree once per rebased commit.
6035 * TODO: Ideally, merged_paths would contain a list of commitables
6036 * we could use so we could skip worktree_status() entirely.
6039 struct got_pathlist_entry *pe;
6040 TAILQ_FOREACH(pe, merged_paths, entry) {
6041 err = worktree_status(worktree, pe->path, fileindex,
6042 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6048 err = worktree_status(worktree, "", fileindex, repo,
6049 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6054 if (TAILQ_EMPTY(&commitable_paths)) {
6055 /* No-op change; commit will be elided. */
6056 err = got_ref_delete(commit_ref, repo);
6059 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6063 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6067 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6072 logmsg = strdup(new_logmsg);
6073 if (logmsg == NULL) {
6074 err = got_error_from_errno("strdup");
6078 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6083 /* NB: commit_worktree will call free(logmsg) */
6084 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6085 worktree, got_object_commit_get_author(orig_commit),
6086 got_object_commit_get_committer(orig_commit),
6087 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6091 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6095 err = got_ref_delete(commit_ref, repo);
6099 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6101 sync_err = sync_fileindex(fileindex, fileindex_path);
6102 if (sync_err && err == NULL)
6105 free(fileindex_path);
6106 free(head_commit_id);
6108 got_ref_close(head_ref);
6110 free(*new_commit_id);
6111 *new_commit_id = NULL;
6116 const struct got_error *
6117 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6118 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6119 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6120 struct got_commit_object *orig_commit,
6121 struct got_object_id *orig_commit_id, struct got_repository *repo)
6123 const struct got_error *err;
6124 char *commit_ref_name;
6125 struct got_reference *commit_ref = NULL;
6126 struct got_object_id *commit_id = NULL;
6128 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6132 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6135 err = got_ref_resolve(&commit_id, repo, commit_ref);
6138 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6139 err = got_error(GOT_ERR_REBASE_COMMITID);
6143 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6144 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6147 got_ref_close(commit_ref);
6148 free(commit_ref_name);
6153 const struct got_error *
6154 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6155 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6156 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6157 struct got_commit_object *orig_commit,
6158 struct got_object_id *orig_commit_id, const char *new_logmsg,
6159 struct got_repository *repo)
6161 const struct got_error *err;
6162 char *commit_ref_name;
6163 struct got_reference *commit_ref = NULL;
6165 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6169 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6173 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6174 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6177 got_ref_close(commit_ref);
6178 free(commit_ref_name);
6182 const struct got_error *
6183 got_worktree_rebase_postpone(struct got_worktree *worktree,
6184 struct got_fileindex *fileindex)
6187 got_fileindex_free(fileindex);
6188 return lock_worktree(worktree, LOCK_SH);
6191 static const struct got_error *
6192 delete_ref(const char *name, struct got_repository *repo)
6194 const struct got_error *err;
6195 struct got_reference *ref;
6197 err = got_ref_open(&ref, repo, name, 0);
6199 if (err->code == GOT_ERR_NOT_REF)
6204 err = got_ref_delete(ref, repo);
6209 static const struct got_error *
6210 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6212 const struct got_error *err;
6213 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6214 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6216 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6219 err = delete_ref(tmp_branch_name, repo);
6223 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6226 err = delete_ref(new_base_branch_ref_name, repo);
6230 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6233 err = delete_ref(branch_ref_name, repo);
6237 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6240 err = delete_ref(commit_ref_name, repo);
6245 free(tmp_branch_name);
6246 free(new_base_branch_ref_name);
6247 free(branch_ref_name);
6248 free(commit_ref_name);
6252 const struct got_error *
6253 got_worktree_rebase_complete(struct got_worktree *worktree,
6254 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6255 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6256 struct got_repository *repo)
6258 const struct got_error *err, *unlockerr;
6259 struct got_object_id *new_head_commit_id = NULL;
6261 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6265 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6269 err = got_ref_write(rebased_branch, repo);
6273 err = got_worktree_set_head_ref(worktree, rebased_branch);
6277 err = delete_rebase_refs(worktree, repo);
6280 got_fileindex_free(fileindex);
6281 free(new_head_commit_id);
6282 unlockerr = lock_worktree(worktree, LOCK_SH);
6283 if (unlockerr && err == NULL)
6288 const struct got_error *
6289 got_worktree_rebase_abort(struct got_worktree *worktree,
6290 struct got_fileindex *fileindex, struct got_repository *repo,
6291 struct got_reference *new_base_branch,
6292 got_worktree_checkout_cb progress_cb, void *progress_arg)
6294 const struct got_error *err, *unlockerr, *sync_err;
6295 struct got_reference *resolved = NULL;
6296 struct got_object_id *commit_id = NULL;
6297 char *fileindex_path = NULL;
6298 struct revert_file_args rfa;
6299 struct got_object_id *tree_id = NULL;
6301 err = lock_worktree(worktree, LOCK_EX);
6305 err = got_ref_open(&resolved, repo,
6306 got_ref_get_symref_target(new_base_branch), 0);
6310 err = got_worktree_set_head_ref(worktree, resolved);
6315 * XXX commits to the base branch could have happened while
6316 * we were busy rebasing; should we store the original commit ID
6317 * when rebase begins and read it back here?
6319 err = got_ref_resolve(&commit_id, repo, resolved);
6323 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6327 err = got_object_id_by_path(&tree_id, repo,
6328 worktree->base_commit_id, worktree->path_prefix);
6332 err = delete_rebase_refs(worktree, repo);
6336 err = get_fileindex_path(&fileindex_path, worktree);
6340 rfa.worktree = worktree;
6341 rfa.fileindex = fileindex;
6342 rfa.progress_cb = progress_cb;
6343 rfa.progress_arg = progress_arg;
6344 rfa.patch_cb = NULL;
6345 rfa.patch_arg = NULL;
6347 err = worktree_status(worktree, "", fileindex, repo,
6348 revert_file, &rfa, NULL, NULL, 0, 0);
6352 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6353 repo, progress_cb, progress_arg, NULL, NULL);
6355 sync_err = sync_fileindex(fileindex, fileindex_path);
6356 if (sync_err && err == NULL)
6359 got_ref_close(resolved);
6363 got_fileindex_free(fileindex);
6364 free(fileindex_path);
6366 unlockerr = lock_worktree(worktree, LOCK_SH);
6367 if (unlockerr && err == NULL)
6372 const struct got_error *
6373 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6374 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6375 struct got_fileindex **fileindex, struct got_worktree *worktree,
6376 struct got_repository *repo)
6378 const struct got_error *err = NULL;
6379 char *tmp_branch_name = NULL;
6380 char *branch_ref_name = NULL;
6381 char *base_commit_ref_name = NULL;
6382 char *fileindex_path = NULL;
6383 struct check_rebase_ok_arg ok_arg;
6384 struct got_reference *wt_branch = NULL;
6385 struct got_reference *base_commit_ref = NULL;
6389 *base_commit_id = NULL;
6392 err = lock_worktree(worktree, LOCK_EX);
6396 err = open_fileindex(fileindex, &fileindex_path, worktree);
6400 ok_arg.worktree = worktree;
6402 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6407 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6411 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6415 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6420 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6425 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6429 err = got_ref_write(*branch_ref, repo);
6433 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6434 worktree->base_commit_id);
6437 err = got_ref_write(base_commit_ref, repo);
6440 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6441 if (*base_commit_id == NULL) {
6442 err = got_error_from_errno("got_object_id_dup");
6446 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6447 worktree->base_commit_id);
6450 err = got_ref_write(*tmp_branch, repo);
6454 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6458 free(fileindex_path);
6459 free(tmp_branch_name);
6460 free(branch_ref_name);
6461 free(base_commit_ref_name);
6463 got_ref_close(wt_branch);
6466 got_ref_close(*branch_ref);
6470 got_ref_close(*tmp_branch);
6473 free(*base_commit_id);
6475 got_fileindex_free(*fileindex);
6478 lock_worktree(worktree, LOCK_SH);
6483 const struct got_error *
6484 got_worktree_histedit_postpone(struct got_worktree *worktree,
6485 struct got_fileindex *fileindex)
6488 got_fileindex_free(fileindex);
6489 return lock_worktree(worktree, LOCK_SH);
6492 const struct got_error *
6493 got_worktree_histedit_in_progress(int *in_progress,
6494 struct got_worktree *worktree)
6496 const struct got_error *err;
6497 char *tmp_branch_name = NULL;
6499 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6503 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6504 free(tmp_branch_name);
6508 const struct got_error *
6509 got_worktree_histedit_continue(struct got_object_id **commit_id,
6510 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6511 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6512 struct got_worktree *worktree, struct got_repository *repo)
6514 const struct got_error *err;
6515 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6516 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6517 struct got_reference *commit_ref = NULL;
6518 struct got_reference *base_commit_ref = NULL;
6519 char *fileindex_path = NULL;
6520 int have_staged_files = 0;
6524 *base_commit_id = NULL;
6527 err = lock_worktree(worktree, LOCK_EX);
6531 err = open_fileindex(fileindex, &fileindex_path, worktree);
6535 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6536 &have_staged_files);
6537 if (err && err->code != GOT_ERR_CANCELLED)
6539 if (have_staged_files) {
6540 err = got_error(GOT_ERR_STAGED_PATHS);
6544 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6548 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6552 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6556 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6561 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6565 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6568 err = got_ref_resolve(commit_id, repo, commit_ref);
6572 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6575 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6579 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6583 free(commit_ref_name);
6584 free(branch_ref_name);
6585 free(fileindex_path);
6587 got_ref_close(commit_ref);
6588 if (base_commit_ref)
6589 got_ref_close(base_commit_ref);
6593 free(*base_commit_id);
6594 *base_commit_id = NULL;
6596 got_ref_close(*tmp_branch);
6600 got_fileindex_free(*fileindex);
6603 lock_worktree(worktree, LOCK_EX);
6608 static const struct got_error *
6609 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6611 const struct got_error *err;
6612 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6613 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6615 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6618 err = delete_ref(tmp_branch_name, repo);
6622 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6626 err = delete_ref(base_commit_ref_name, repo);
6630 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6633 err = delete_ref(branch_ref_name, repo);
6637 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6640 err = delete_ref(commit_ref_name, repo);
6644 free(tmp_branch_name);
6645 free(base_commit_ref_name);
6646 free(branch_ref_name);
6647 free(commit_ref_name);
6651 const struct got_error *
6652 got_worktree_histedit_abort(struct got_worktree *worktree,
6653 struct got_fileindex *fileindex, struct got_repository *repo,
6654 struct got_reference *branch, struct got_object_id *base_commit_id,
6655 got_worktree_checkout_cb progress_cb, void *progress_arg)
6657 const struct got_error *err, *unlockerr, *sync_err;
6658 struct got_reference *resolved = NULL;
6659 char *fileindex_path = NULL;
6660 struct got_object_id *tree_id = NULL;
6661 struct revert_file_args rfa;
6663 err = lock_worktree(worktree, LOCK_EX);
6667 err = got_ref_open(&resolved, repo,
6668 got_ref_get_symref_target(branch), 0);
6672 err = got_worktree_set_head_ref(worktree, resolved);
6676 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6680 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6681 worktree->path_prefix);
6685 err = delete_histedit_refs(worktree, repo);
6689 err = get_fileindex_path(&fileindex_path, worktree);
6693 rfa.worktree = worktree;
6694 rfa.fileindex = fileindex;
6695 rfa.progress_cb = progress_cb;
6696 rfa.progress_arg = progress_arg;
6697 rfa.patch_cb = NULL;
6698 rfa.patch_arg = NULL;
6700 err = worktree_status(worktree, "", fileindex, repo,
6701 revert_file, &rfa, NULL, NULL, 0, 0);
6705 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6706 repo, progress_cb, progress_arg, NULL, NULL);
6708 sync_err = sync_fileindex(fileindex, fileindex_path);
6709 if (sync_err && err == NULL)
6712 got_ref_close(resolved);
6714 free(fileindex_path);
6716 unlockerr = lock_worktree(worktree, LOCK_SH);
6717 if (unlockerr && err == NULL)
6722 const struct got_error *
6723 got_worktree_histedit_complete(struct got_worktree *worktree,
6724 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6725 struct got_reference *edited_branch, struct got_repository *repo)
6727 const struct got_error *err, *unlockerr;
6728 struct got_object_id *new_head_commit_id = NULL;
6729 struct got_reference *resolved = NULL;
6731 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6735 err = got_ref_open(&resolved, repo,
6736 got_ref_get_symref_target(edited_branch), 0);
6740 err = got_ref_change_ref(resolved, new_head_commit_id);
6744 err = got_ref_write(resolved, repo);
6748 err = got_worktree_set_head_ref(worktree, resolved);
6752 err = delete_histedit_refs(worktree, repo);
6755 got_fileindex_free(fileindex);
6756 free(new_head_commit_id);
6757 unlockerr = lock_worktree(worktree, LOCK_SH);
6758 if (unlockerr && err == NULL)
6763 const struct got_error *
6764 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6765 struct got_object_id *commit_id, struct got_repository *repo)
6767 const struct got_error *err;
6768 char *commit_ref_name;
6770 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6774 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6778 err = delete_ref(commit_ref_name, repo);
6780 free(commit_ref_name);
6784 const struct got_error *
6785 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6786 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6787 struct got_worktree *worktree, const char *refname,
6788 struct got_repository *repo)
6790 const struct got_error *err = NULL;
6791 char *fileindex_path = NULL;
6792 struct check_rebase_ok_arg ok_arg;
6796 *base_branch_ref = NULL;
6798 err = lock_worktree(worktree, LOCK_EX);
6802 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6803 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6804 "cannot integrate a branch into itself; "
6805 "update -b or different branch name required");
6809 err = open_fileindex(fileindex, &fileindex_path, worktree);
6813 /* Preconditions are the same as for rebase. */
6814 ok_arg.worktree = worktree;
6816 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6821 err = got_ref_open(branch_ref, repo, refname, 1);
6825 err = got_ref_open(base_branch_ref, repo,
6826 got_worktree_get_head_ref_name(worktree), 1);
6830 got_ref_close(*branch_ref);
6833 if (*base_branch_ref) {
6834 got_ref_close(*base_branch_ref);
6835 *base_branch_ref = NULL;
6838 got_fileindex_free(*fileindex);
6841 lock_worktree(worktree, LOCK_SH);
6846 const struct got_error *
6847 got_worktree_integrate_continue(struct got_worktree *worktree,
6848 struct got_fileindex *fileindex, struct got_repository *repo,
6849 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6850 got_worktree_checkout_cb progress_cb, void *progress_arg,
6851 got_cancel_cb cancel_cb, void *cancel_arg)
6853 const struct got_error *err = NULL, *sync_err, *unlockerr;
6854 char *fileindex_path = NULL;
6855 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6857 err = get_fileindex_path(&fileindex_path, worktree);
6861 err = got_ref_resolve(&commit_id, repo, branch_ref);
6865 err = got_object_id_by_path(&tree_id, repo, commit_id,
6866 worktree->path_prefix);
6870 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6874 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6875 progress_cb, progress_arg, cancel_cb, cancel_arg);
6879 err = got_ref_change_ref(base_branch_ref, commit_id);
6883 err = got_ref_write(base_branch_ref, repo);
6885 sync_err = sync_fileindex(fileindex, fileindex_path);
6886 if (sync_err && err == NULL)
6890 unlockerr = got_ref_unlock(branch_ref);
6891 if (unlockerr && err == NULL)
6893 got_ref_close(branch_ref);
6895 unlockerr = got_ref_unlock(base_branch_ref);
6896 if (unlockerr && err == NULL)
6898 got_ref_close(base_branch_ref);
6900 got_fileindex_free(fileindex);
6901 free(fileindex_path);
6904 unlockerr = lock_worktree(worktree, LOCK_SH);
6905 if (unlockerr && err == NULL)
6910 const struct got_error *
6911 got_worktree_integrate_abort(struct got_worktree *worktree,
6912 struct got_fileindex *fileindex, struct got_repository *repo,
6913 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6915 const struct got_error *err = NULL, *unlockerr = NULL;
6917 got_fileindex_free(fileindex);
6919 err = lock_worktree(worktree, LOCK_SH);
6921 unlockerr = got_ref_unlock(branch_ref);
6922 if (unlockerr && err == NULL)
6924 got_ref_close(branch_ref);
6926 unlockerr = got_ref_unlock(base_branch_ref);
6927 if (unlockerr && err == NULL)
6929 got_ref_close(base_branch_ref);
6934 struct check_stage_ok_arg {
6935 struct got_object_id *head_commit_id;
6936 struct got_worktree *worktree;
6937 struct got_fileindex *fileindex;
6938 struct got_repository *repo;
6942 const struct got_error *
6943 check_stage_ok(void *arg, unsigned char status,
6944 unsigned char staged_status, const char *relpath,
6945 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6946 struct got_object_id *commit_id, int dirfd, const char *de_name)
6948 struct check_stage_ok_arg *a = arg;
6949 const struct got_error *err = NULL;
6950 struct got_fileindex_entry *ie;
6951 struct got_object_id base_commit_id;
6952 struct got_object_id *base_commit_idp = NULL;
6953 char *in_repo_path = NULL, *p;
6955 if (status == GOT_STATUS_UNVERSIONED ||
6956 status == GOT_STATUS_NO_CHANGE)
6958 if (status == GOT_STATUS_NONEXISTENT)
6959 return got_error_set_errno(ENOENT, relpath);
6961 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6963 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6965 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6966 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6968 return got_error_from_errno("asprintf");
6970 if (got_fileindex_entry_has_commit(ie)) {
6971 memcpy(base_commit_id.sha1, ie->commit_sha1,
6972 SHA1_DIGEST_LENGTH);
6973 base_commit_idp = &base_commit_id;
6976 if (status == GOT_STATUS_CONFLICT) {
6977 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6979 } else if (status != GOT_STATUS_ADD &&
6980 status != GOT_STATUS_MODIFY &&
6981 status != GOT_STATUS_DELETE) {
6982 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6986 a->have_changes = 1;
6991 err = check_out_of_date(p, status, staged_status,
6992 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6993 GOT_ERR_STAGE_OUT_OF_DATE);
6999 struct stage_path_arg {
7000 struct got_worktree *worktree;
7001 struct got_fileindex *fileindex;
7002 struct got_repository *repo;
7003 got_worktree_status_cb status_cb;
7005 got_worktree_patch_cb patch_cb;
7007 int staged_something;
7010 static const struct got_error *
7011 stage_path(void *arg, unsigned char status,
7012 unsigned char staged_status, const char *relpath,
7013 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7014 struct got_object_id *commit_id, int dirfd, const char *de_name)
7016 struct stage_path_arg *a = arg;
7017 const struct got_error *err = NULL;
7018 struct got_fileindex_entry *ie;
7019 char *ondisk_path = NULL, *path_content = NULL;
7021 struct got_object_id *new_staged_blob_id = NULL;
7023 if (status == GOT_STATUS_UNVERSIONED)
7026 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7028 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7030 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7032 return got_error_from_errno("asprintf");
7035 case GOT_STATUS_ADD:
7036 case GOT_STATUS_MODIFY:
7038 if (status == GOT_STATUS_ADD) {
7039 int choice = GOT_PATCH_CHOICE_NONE;
7040 err = (*a->patch_cb)(&choice, a->patch_arg,
7041 status, ie->path, NULL, 1, 1);
7044 if (choice != GOT_PATCH_CHOICE_YES)
7047 err = create_patched_content(&path_content, 0,
7048 staged_blob_id ? staged_blob_id : blob_id,
7049 ondisk_path, dirfd, de_name, ie->path,
7050 a->repo, a->patch_cb, a->patch_arg);
7051 if (err || path_content == NULL)
7055 err = got_object_blob_create(&new_staged_blob_id,
7056 path_content ? path_content : ondisk_path, a->repo);
7059 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7060 SHA1_DIGEST_LENGTH);
7061 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7062 stage = GOT_FILEIDX_STAGE_ADD;
7064 stage = GOT_FILEIDX_STAGE_MODIFY;
7065 got_fileindex_entry_stage_set(ie, stage);
7066 a->staged_something = 1;
7067 if (a->status_cb == NULL)
7069 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7070 get_staged_status(ie), relpath, blob_id,
7071 new_staged_blob_id, NULL, dirfd, de_name);
7073 case GOT_STATUS_DELETE:
7074 if (staged_status == GOT_STATUS_DELETE)
7077 int choice = GOT_PATCH_CHOICE_NONE;
7078 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7079 ie->path, NULL, 1, 1);
7082 if (choice == GOT_PATCH_CHOICE_NO)
7084 if (choice != GOT_PATCH_CHOICE_YES) {
7085 err = got_error(GOT_ERR_PATCH_CHOICE);
7089 stage = GOT_FILEIDX_STAGE_DELETE;
7090 got_fileindex_entry_stage_set(ie, stage);
7091 a->staged_something = 1;
7092 if (a->status_cb == NULL)
7094 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7095 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7098 case GOT_STATUS_NO_CHANGE:
7100 case GOT_STATUS_CONFLICT:
7101 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7103 case GOT_STATUS_NONEXISTENT:
7104 err = got_error_set_errno(ENOENT, relpath);
7107 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7111 if (path_content && unlink(path_content) == -1 && err == NULL)
7112 err = got_error_from_errno2("unlink", path_content);
7115 free(new_staged_blob_id);
7119 const struct got_error *
7120 got_worktree_stage(struct got_worktree *worktree,
7121 struct got_pathlist_head *paths,
7122 got_worktree_status_cb status_cb, void *status_arg,
7123 got_worktree_patch_cb patch_cb, void *patch_arg,
7124 struct got_repository *repo)
7126 const struct got_error *err = NULL, *sync_err, *unlockerr;
7127 struct got_pathlist_entry *pe;
7128 struct got_fileindex *fileindex = NULL;
7129 char *fileindex_path = NULL;
7130 struct got_reference *head_ref = NULL;
7131 struct got_object_id *head_commit_id = NULL;
7132 struct check_stage_ok_arg oka;
7133 struct stage_path_arg spa;
7135 err = lock_worktree(worktree, LOCK_EX);
7139 err = got_ref_open(&head_ref, repo,
7140 got_worktree_get_head_ref_name(worktree), 0);
7143 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7146 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7150 /* Check pre-conditions before staging anything. */
7151 oka.head_commit_id = head_commit_id;
7152 oka.worktree = worktree;
7153 oka.fileindex = fileindex;
7155 oka.have_changes = 0;
7156 TAILQ_FOREACH(pe, paths, entry) {
7157 err = worktree_status(worktree, pe->path, fileindex, repo,
7158 check_stage_ok, &oka, NULL, NULL, 0, 0);
7162 if (!oka.have_changes) {
7163 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7167 spa.worktree = worktree;
7168 spa.fileindex = fileindex;
7170 spa.patch_cb = patch_cb;
7171 spa.patch_arg = patch_arg;
7172 spa.status_cb = status_cb;
7173 spa.status_arg = status_arg;
7174 spa.staged_something = 0;
7175 TAILQ_FOREACH(pe, paths, entry) {
7176 err = worktree_status(worktree, pe->path, fileindex, repo,
7177 stage_path, &spa, NULL, NULL, 0, 0);
7181 if (!spa.staged_something) {
7182 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7186 sync_err = sync_fileindex(fileindex, fileindex_path);
7187 if (sync_err && err == NULL)
7191 got_ref_close(head_ref);
7192 free(head_commit_id);
7193 free(fileindex_path);
7195 got_fileindex_free(fileindex);
7196 unlockerr = lock_worktree(worktree, LOCK_SH);
7197 if (unlockerr && err == NULL)
7202 struct unstage_path_arg {
7203 struct got_worktree *worktree;
7204 struct got_fileindex *fileindex;
7205 struct got_repository *repo;
7206 got_worktree_checkout_cb progress_cb;
7208 got_worktree_patch_cb patch_cb;
7212 static const struct got_error *
7213 create_unstaged_content(char **path_unstaged_content,
7214 char **path_new_staged_content, struct got_object_id *blob_id,
7215 struct got_object_id *staged_blob_id, const char *relpath,
7216 struct got_repository *repo,
7217 got_worktree_patch_cb patch_cb, void *patch_arg)
7219 const struct got_error *err;
7220 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7221 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7222 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7223 struct stat sb1, sb2;
7224 struct got_diff_changes *changes = NULL;
7225 struct got_diff_state *ds = NULL;
7226 struct got_diff_args *args = NULL;
7227 struct got_diff_change *change;
7228 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7229 int have_content = 0, have_rejected_content = 0;
7231 *path_unstaged_content = NULL;
7232 *path_new_staged_content = NULL;
7234 err = got_object_id_str(&label1, blob_id);
7237 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7241 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7245 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7249 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7253 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7257 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7261 if (stat(path1, &sb1) == -1) {
7262 err = got_error_from_errno2("stat", path1);
7266 if (stat(path2, &sb2) == -1) {
7267 err = got_error_from_errno2("stat", path2);
7271 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7272 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7276 err = got_opentemp_named(path_unstaged_content, &outfile,
7277 "got-unstaged-content");
7280 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7281 "got-new-staged-content");
7285 if (fseek(f1, 0L, SEEK_SET) == -1) {
7286 err = got_ferror(f1, GOT_ERR_IO);
7289 if (fseek(f2, 0L, SEEK_SET) == -1) {
7290 err = got_ferror(f2, GOT_ERR_IO);
7293 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7295 err = apply_or_reject_change(&choice, change, ++n,
7296 changes->nchanges, ds, args, diff_flags, relpath,
7297 f1, f2, &line_cur1, &line_cur2,
7298 outfile, rejectfile, patch_cb, patch_arg);
7301 if (choice == GOT_PATCH_CHOICE_YES)
7304 have_rejected_content = 1;
7305 if (choice == GOT_PATCH_CHOICE_QUIT)
7308 if (have_content || have_rejected_content)
7309 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7310 outfile, rejectfile);
7314 got_object_blob_close(blob);
7316 got_object_blob_close(staged_blob);
7317 if (f1 && fclose(f1) == EOF && err == NULL)
7318 err = got_error_from_errno2("fclose", path1);
7319 if (f2 && fclose(f2) == EOF && err == NULL)
7320 err = got_error_from_errno2("fclose", path2);
7321 if (outfile && fclose(outfile) == EOF && err == NULL)
7322 err = got_error_from_errno2("fclose", *path_unstaged_content);
7323 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7324 err = got_error_from_errno2("fclose", *path_new_staged_content);
7325 if (path1 && unlink(path1) == -1 && err == NULL)
7326 err = got_error_from_errno2("unlink", path1);
7327 if (path2 && unlink(path2) == -1 && err == NULL)
7328 err = got_error_from_errno2("unlink", path2);
7329 if (err || !have_content) {
7330 if (*path_unstaged_content &&
7331 unlink(*path_unstaged_content) == -1 && err == NULL)
7332 err = got_error_from_errno2("unlink",
7333 *path_unstaged_content);
7334 free(*path_unstaged_content);
7335 *path_unstaged_content = NULL;
7337 if (err || !have_rejected_content) {
7338 if (*path_new_staged_content &&
7339 unlink(*path_new_staged_content) == -1 && err == NULL)
7340 err = got_error_from_errno2("unlink",
7341 *path_new_staged_content);
7342 free(*path_new_staged_content);
7343 *path_new_staged_content = NULL;
7347 got_diff_state_free(ds);
7351 got_diff_free_changes(changes);
7357 static const struct got_error *
7358 unstage_path(void *arg, unsigned char status,
7359 unsigned char staged_status, const char *relpath,
7360 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7361 struct got_object_id *commit_id, int dirfd, const char *de_name)
7363 const struct got_error *err = NULL;
7364 struct unstage_path_arg *a = arg;
7365 struct got_fileindex_entry *ie;
7366 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7367 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7368 char *path_new_staged_content = NULL;
7369 char *id_str = NULL, *label_orig = NULL;
7370 int local_changes_subsumed;
7373 if (staged_status != GOT_STATUS_ADD &&
7374 staged_status != GOT_STATUS_MODIFY &&
7375 staged_status != GOT_STATUS_DELETE)
7378 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7380 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7382 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7384 return got_error_from_errno("asprintf");
7386 err = got_object_id_str(&id_str,
7387 commit_id ? commit_id : a->worktree->base_commit_id);
7390 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7392 err = got_error_from_errno("asprintf");
7396 switch (staged_status) {
7397 case GOT_STATUS_MODIFY:
7398 err = got_object_open_as_blob(&blob_base, a->repo,
7403 case GOT_STATUS_ADD:
7405 if (staged_status == GOT_STATUS_ADD) {
7406 int choice = GOT_PATCH_CHOICE_NONE;
7407 err = (*a->patch_cb)(&choice, a->patch_arg,
7408 staged_status, ie->path, NULL, 1, 1);
7411 if (choice != GOT_PATCH_CHOICE_YES)
7414 err = create_unstaged_content(
7415 &path_unstaged_content,
7416 &path_new_staged_content, blob_id,
7417 staged_blob_id, ie->path, a->repo,
7418 a->patch_cb, a->patch_arg);
7419 if (err || path_unstaged_content == NULL)
7421 if (path_new_staged_content) {
7422 err = got_object_blob_create(
7424 path_new_staged_content,
7428 memcpy(ie->staged_blob_sha1,
7429 staged_blob_id->sha1,
7430 SHA1_DIGEST_LENGTH);
7432 err = merge_file(&local_changes_subsumed,
7433 a->worktree, blob_base, ondisk_path,
7434 relpath, got_fileindex_perms_to_st(ie),
7435 path_unstaged_content, label_orig,
7436 "unstaged", a->repo, a->progress_cb,
7439 path_new_staged_content == NULL)
7440 got_fileindex_entry_stage_set(ie,
7441 GOT_FILEIDX_STAGE_NONE);
7442 break; /* Done with this file. */
7445 err = got_object_open_as_blob(&blob_staged, a->repo,
7446 staged_blob_id, 8192);
7449 err = merge_blob(&local_changes_subsumed, a->worktree,
7450 blob_base, ondisk_path, relpath,
7451 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7452 commit_id ? commit_id : a->worktree->base_commit_id,
7453 a->repo, a->progress_cb, a->progress_arg);
7455 got_fileindex_entry_stage_set(ie,
7456 GOT_FILEIDX_STAGE_NONE);
7458 case GOT_STATUS_DELETE:
7460 int choice = GOT_PATCH_CHOICE_NONE;
7461 err = (*a->patch_cb)(&choice, a->patch_arg,
7462 staged_status, ie->path, NULL, 1, 1);
7465 if (choice == GOT_PATCH_CHOICE_NO)
7467 if (choice != GOT_PATCH_CHOICE_YES) {
7468 err = got_error(GOT_ERR_PATCH_CHOICE);
7472 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7473 err = get_file_status(&status, &sb, ie, ondisk_path,
7474 dirfd, de_name, a->repo);
7477 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7482 if (path_unstaged_content &&
7483 unlink(path_unstaged_content) == -1 && err == NULL)
7484 err = got_error_from_errno2("unlink", path_unstaged_content);
7485 if (path_new_staged_content &&
7486 unlink(path_new_staged_content) == -1 && err == NULL)
7487 err = got_error_from_errno2("unlink", path_new_staged_content);
7488 free(path_unstaged_content);
7489 free(path_new_staged_content);
7491 got_object_blob_close(blob_base);
7493 got_object_blob_close(blob_staged);
7499 const struct got_error *
7500 got_worktree_unstage(struct got_worktree *worktree,
7501 struct got_pathlist_head *paths,
7502 got_worktree_checkout_cb progress_cb, void *progress_arg,
7503 got_worktree_patch_cb patch_cb, void *patch_arg,
7504 struct got_repository *repo)
7506 const struct got_error *err = NULL, *sync_err, *unlockerr;
7507 struct got_pathlist_entry *pe;
7508 struct got_fileindex *fileindex = NULL;
7509 char *fileindex_path = NULL;
7510 struct unstage_path_arg upa;
7512 err = lock_worktree(worktree, LOCK_EX);
7516 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7520 upa.worktree = worktree;
7521 upa.fileindex = fileindex;
7523 upa.progress_cb = progress_cb;
7524 upa.progress_arg = progress_arg;
7525 upa.patch_cb = patch_cb;
7526 upa.patch_arg = patch_arg;
7527 TAILQ_FOREACH(pe, paths, entry) {
7528 err = worktree_status(worktree, pe->path, fileindex, repo,
7529 unstage_path, &upa, NULL, NULL, 0, 0);
7534 sync_err = sync_fileindex(fileindex, fileindex_path);
7535 if (sync_err && err == NULL)
7538 free(fileindex_path);
7540 got_fileindex_free(fileindex);
7541 unlockerr = lock_worktree(worktree, LOCK_SH);
7542 if (unlockerr && err == NULL)