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, deriv_target is the link target of the first derived version,
976 * and the symlink 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, const char *label_orig, const char *deriv_target,
983 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
984 got_worktree_checkout_cb progress_cb, void *progress_arg)
986 const struct got_error *err = NULL;
987 char *ancestor_target = NULL;
989 ssize_t ondisk_len, deriv_len;
990 char ondisk_target[PATH_MAX];
991 int have_local_change = 0;
992 int have_incoming_change = 0;
994 if (lstat(ondisk_path, &sb) == -1)
995 return got_error_from_errno2("lstat", ondisk_path);
997 ondisk_len = readlink(ondisk_path, ondisk_target,
998 sizeof(ondisk_target));
999 if (ondisk_len == -1) {
1000 err = got_error_from_errno2("readlink",
1004 ondisk_target[ondisk_len] = '\0';
1007 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1012 if (ancestor_target == NULL ||
1013 (ondisk_len != strlen(ancestor_target) ||
1014 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1015 have_local_change = 1;
1017 deriv_len = strlen(deriv_target);
1018 if (ancestor_target == NULL ||
1019 (deriv_len != strlen(ancestor_target) ||
1020 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1021 have_incoming_change = 1;
1023 if (!have_local_change && !have_incoming_change) {
1024 if (ancestor_target) {
1025 /* Both sides made the same change. */
1026 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1028 } else if (deriv_len == ondisk_len &&
1029 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1030 /* Both sides added the same symlink. */
1031 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1034 /* Both sides added symlinks which don't match. */
1035 err = install_symlink_conflict(deriv_target,
1036 deriv_base_commit_id, ancestor_target,
1037 label_orig, ondisk_target, ondisk_path);
1040 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1043 } else if (!have_local_change && have_incoming_change) {
1044 /* Apply the incoming change. */
1045 err = update_symlink(ondisk_path, deriv_target,
1046 strlen(deriv_target));
1049 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1050 } else if (have_local_change && have_incoming_change) {
1051 if (deriv_len == ondisk_len &&
1052 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1053 /* Both sides made the same change. */
1054 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1057 err = install_symlink_conflict(deriv_target,
1058 deriv_base_commit_id, ancestor_target, label_orig,
1059 ondisk_target, ondisk_path);
1062 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1068 free(ancestor_target);
1073 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1074 * blob_deriv acts as the first derived version, and the file on disk
1075 * acts as the second derived version.
1077 static const struct got_error *
1078 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1079 struct got_blob_object *blob_orig, const char *ondisk_path,
1080 const char *path, uint16_t st_mode, const char *label_orig,
1081 struct got_blob_object *blob_deriv,
1082 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1083 got_worktree_checkout_cb progress_cb, void *progress_arg)
1085 const struct got_error *err = NULL;
1086 FILE *f_deriv = NULL;
1087 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1088 char *label_deriv = NULL, *parent;
1090 *local_changes_subsumed = 0;
1092 parent = dirname(ondisk_path);
1094 return got_error_from_errno2("dirname", ondisk_path);
1097 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1098 err = got_error_from_errno("asprintf");
1103 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1106 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1111 err = got_object_id_str(&id_str, deriv_base_commit_id);
1114 if (asprintf(&label_deriv, "%s: commit %s",
1115 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1116 err = got_error_from_errno("asprintf");
1120 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1121 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1122 label_deriv, repo, progress_cb, progress_arg);
1124 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1125 err = got_error_from_errno("fclose");
1127 if (blob_deriv_path) {
1128 unlink(blob_deriv_path);
1129 free(blob_deriv_path);
1136 static const struct got_error *
1137 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1138 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1139 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1141 const struct got_error *err = NULL;
1142 struct got_fileindex_entry *new_ie;
1146 err = got_fileindex_entry_alloc(&new_ie, path);
1150 err = got_fileindex_entry_update(new_ie, ondisk_path,
1151 blob_id->sha1, base_commit_id->sha1, 1);
1155 err = got_fileindex_entry_add(fileindex, new_ie);
1158 got_fileindex_entry_free(new_ie);
1165 get_ondisk_perms(int executable, mode_t st_mode)
1167 mode_t xbits = S_IXUSR;
1170 /* Map read bits to execute bits. */
1171 if (st_mode & S_IRGRP)
1173 if (st_mode & S_IROTH)
1175 return st_mode | xbits;
1178 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1181 /* forward declaration */
1182 static const struct got_error *
1183 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1184 const char *path, mode_t te_mode, mode_t st_mode,
1185 struct got_blob_object *blob, int restoring_missing_file,
1186 int reverting_versioned_file, int installing_bad_symlink,
1187 int path_is_unversioned, struct got_repository *repo,
1188 got_worktree_checkout_cb progress_cb, void *progress_arg);
1191 * This function assumes that the provided symlink target points at a
1192 * safe location in the work tree!
1194 static const struct got_error *
1195 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1198 const struct got_error *err = NULL;
1200 char etarget[PATH_MAX];
1204 * "Bad" symlinks (those pointing outside the work tree or into the
1205 * .got directory) are installed in the work tree as a regular file
1206 * which contains the bad symlink target path.
1207 * The new symlink target has already been checked for safety by our
1208 * caller. If we can successfully open a regular file then we simply
1209 * replace this file with a symlink below.
1211 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1214 return got_error_from_errno2("open", ondisk_path);
1216 /* We are updating an existing on-disk symlink. */
1217 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1219 return got_error_from_errno2("readlink", ondisk_path);
1221 if (elen == target_len &&
1222 memcmp(etarget, target_path, target_len) == 0)
1223 return NULL; /* nothing to do */
1226 err = update_symlink(ondisk_path, target_path, target_len);
1227 if (fd != -1 && close(fd) == -1 && err == NULL)
1228 err = got_error_from_errno2("close", ondisk_path);
1232 static const struct got_error *
1233 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1234 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1236 const struct got_error *err = NULL;
1237 char canonpath[PATH_MAX];
1238 char *path_got = NULL;
1240 *is_bad_symlink = 0;
1242 if (target_len >= sizeof(canonpath)) {
1243 *is_bad_symlink = 1;
1248 * We do not use realpath(3) to resolve the symlink's target
1249 * path because we don't want to resolve symlinks recursively.
1250 * Instead we make the path absolute and then canonicalize it.
1251 * Relative symlink target lookup should begin at the directory
1252 * in which the blob object is being installed.
1254 if (!got_path_is_absolute(target_path)) {
1256 char *parent = dirname(ondisk_path);
1258 return got_error_from_errno2("dirname", ondisk_path);
1259 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1260 return got_error_from_errno("asprintf");
1261 if (strlen(abspath) >= sizeof(canonpath)) {
1262 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1266 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1271 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1276 /* Only allow symlinks pointing at paths within the work tree. */
1277 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1278 *is_bad_symlink = 1;
1282 /* Do not allow symlinks pointing into the .got directory. */
1283 if (asprintf(&path_got, "%s/%s", wtroot_path,
1284 GOT_WORKTREE_GOT_DIR) == -1)
1285 return got_error_from_errno("asprintf");
1286 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1287 *is_bad_symlink = 1;
1293 static const struct got_error *
1294 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1295 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1296 int restoring_missing_file, int reverting_versioned_file,
1297 int path_is_unversioned, struct got_repository *repo,
1298 got_worktree_checkout_cb progress_cb, void *progress_arg)
1300 const struct got_error *err = NULL;
1301 char target_path[PATH_MAX];
1302 size_t len, target_len = 0;
1303 char *path_got = NULL;
1304 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1305 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1307 *is_bad_symlink = 0;
1310 * Blob object content specifies the target path of the link.
1311 * If a symbolic link cannot be installed we instead create
1312 * a regular file which contains the link target path stored
1313 * in the blob object.
1316 err = got_object_blob_read_block(&len, blob);
1317 if (len + target_len >= sizeof(target_path)) {
1318 /* Path too long; install as a regular file. */
1319 *is_bad_symlink = 1;
1320 got_object_blob_rewind(blob);
1321 return install_blob(worktree, ondisk_path, path,
1322 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1323 restoring_missing_file, reverting_versioned_file,
1324 1, path_is_unversioned, repo, progress_cb,
1328 /* Skip blob object header first time around. */
1329 memcpy(target_path + target_len, buf + hdrlen,
1331 target_len += len - hdrlen;
1335 target_path[target_len] = '\0';
1337 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1338 ondisk_path, worktree->root_path);
1342 if (*is_bad_symlink) {
1343 /* install as a regular file */
1344 *is_bad_symlink = 1;
1345 got_object_blob_rewind(blob);
1346 err = install_blob(worktree, ondisk_path, path,
1347 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1348 restoring_missing_file, reverting_versioned_file, 1,
1349 path_is_unversioned, repo, progress_cb, progress_arg);
1353 if (symlink(target_path, ondisk_path) == -1) {
1354 if (errno == EEXIST) {
1355 if (path_is_unversioned) {
1356 err = (*progress_cb)(progress_arg,
1357 GOT_STATUS_UNVERSIONED, path);
1360 err = replace_existing_symlink(ondisk_path,
1361 target_path, target_len);
1365 err = (*progress_cb)(progress_arg,
1366 reverting_versioned_file ?
1367 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
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, reverting_versioned_file ?
1412 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1418 static const struct got_error *
1419 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1420 const char *path, mode_t te_mode, mode_t st_mode,
1421 struct got_blob_object *blob, int restoring_missing_file,
1422 int reverting_versioned_file, int installing_bad_symlink,
1423 int path_is_unversioned, struct got_repository *repo,
1424 got_worktree_checkout_cb progress_cb, void *progress_arg)
1426 const struct got_error *err = NULL;
1430 char *tmppath = NULL;
1432 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1433 GOT_DEFAULT_FILE_MODE);
1435 if (errno == ENOENT) {
1436 char *parent = dirname(path);
1438 return got_error_from_errno2("dirname", path);
1439 err = add_dir_on_disk(worktree, parent);
1442 fd = open(ondisk_path,
1443 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1444 GOT_DEFAULT_FILE_MODE);
1446 return got_error_from_errno2("open",
1448 } else if (errno == EEXIST) {
1449 if (path_is_unversioned) {
1450 err = (*progress_cb)(progress_arg,
1451 GOT_STATUS_UNVERSIONED, path);
1454 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1455 /* TODO file is obstructed; do something */
1456 err = got_error_path(ondisk_path,
1457 GOT_ERR_FILE_OBSTRUCTED);
1460 err = got_opentemp_named_fd(&tmppath, &fd,
1467 return got_error_from_errno2("open", ondisk_path);
1471 if (restoring_missing_file)
1472 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1474 else if (reverting_versioned_file)
1475 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1478 err = (*progress_cb)(progress_arg,
1479 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1484 hdrlen = got_object_blob_get_hdrlen(blob);
1486 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1487 err = got_object_blob_read_block(&len, blob);
1491 /* Skip blob object header first time around. */
1492 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1494 err = got_error_from_errno("write");
1496 } else if (outlen != len - hdrlen) {
1497 err = got_error(GOT_ERR_IO);
1504 if (fsync(fd) != 0) {
1505 err = got_error_from_errno("fsync");
1510 if (rename(tmppath, ondisk_path) != 0) {
1511 err = got_error_from_errno3("rename", tmppath,
1518 if (chmod(ondisk_path,
1519 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1520 err = got_error_from_errno2("chmod", ondisk_path);
1525 if (fd != -1 && close(fd) != 0 && err == NULL)
1526 err = got_error_from_errno("close");
1531 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1532 static const struct got_error *
1533 get_modified_file_content_status(unsigned char *status, FILE *f)
1535 const struct got_error *err = NULL;
1536 const char *markers[3] = {
1537 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1538 GOT_DIFF_CONFLICT_MARKER_SEP,
1539 GOT_DIFF_CONFLICT_MARKER_END
1544 const char delim[3] = {'\0', '\0', '\0'};
1546 while (*status == GOT_STATUS_MODIFY) {
1547 line = fparseln(f, &len, NULL, delim, 0);
1551 err = got_ferror(f, GOT_ERR_IO);
1555 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1556 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1558 *status = GOT_STATUS_CONFLICT;
1568 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1570 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1571 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1575 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1577 return !(ie->ctime_sec == sb->st_ctime &&
1578 ie->ctime_nsec == sb->st_ctimensec &&
1579 ie->mtime_sec == sb->st_mtime &&
1580 ie->mtime_nsec == sb->st_mtimensec &&
1581 ie->size == (sb->st_size & 0xffffffff) &&
1582 !xbit_differs(ie, sb->st_mode));
1585 static unsigned char
1586 get_staged_status(struct got_fileindex_entry *ie)
1588 switch (got_fileindex_entry_stage_get(ie)) {
1589 case GOT_FILEIDX_STAGE_ADD:
1590 return GOT_STATUS_ADD;
1591 case GOT_FILEIDX_STAGE_DELETE:
1592 return GOT_STATUS_DELETE;
1593 case GOT_FILEIDX_STAGE_MODIFY:
1594 return GOT_STATUS_MODIFY;
1596 return GOT_STATUS_NO_CHANGE;
1600 static const struct got_error *
1601 get_symlink_modification_status(unsigned char *status,
1602 struct got_fileindex_entry *ie, const char *abspath,
1603 int dirfd, const char *de_name, struct got_blob_object *blob)
1605 const struct got_error *err = NULL;
1606 char target_path[PATH_MAX];
1607 char etarget[PATH_MAX];
1609 size_t len, target_len = 0;
1610 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1611 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1613 *status = GOT_STATUS_NO_CHANGE;
1615 /* Blob object content specifies the target path of the link. */
1617 err = got_object_blob_read_block(&len, blob);
1620 if (len + target_len >= sizeof(target_path)) {
1622 * Should not happen. The blob contents were OK
1623 * when this symlink was installed.
1625 return got_error(GOT_ERR_NO_SPACE);
1628 /* Skip blob object header first time around. */
1629 memcpy(target_path + target_len, buf + hdrlen,
1631 target_len += len - hdrlen;
1635 target_path[target_len] = '\0';
1638 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1640 return got_error_from_errno2("readlinkat", abspath);
1642 elen = readlink(abspath, etarget, sizeof(etarget));
1644 return got_error_from_errno2("readlink", abspath);
1647 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1648 *status = GOT_STATUS_MODIFY;
1653 static const struct got_error *
1654 get_file_status(unsigned char *status, struct stat *sb,
1655 struct got_fileindex_entry *ie, const char *abspath,
1656 int dirfd, const char *de_name, struct got_repository *repo)
1658 const struct got_error *err = NULL;
1659 struct got_object_id id;
1664 struct got_blob_object *blob = NULL;
1666 unsigned char staged_status = get_staged_status(ie);
1668 *status = GOT_STATUS_NO_CHANGE;
1671 * Whenever the caller provides a directory descriptor and a
1672 * directory entry name for the file, use them! This prevents
1673 * race conditions if filesystem paths change beneath our feet.
1676 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1677 if (errno == ENOENT) {
1678 if (got_fileindex_entry_has_file_on_disk(ie))
1679 *status = GOT_STATUS_MISSING;
1681 *status = GOT_STATUS_DELETE;
1684 err = got_error_from_errno2("fstatat", abspath);
1688 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1689 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1690 return got_error_from_errno2("open", abspath);
1691 else if (fd == -1 && errno == ELOOP) {
1692 if (lstat(abspath, sb) == -1)
1693 return got_error_from_errno2("lstat", abspath);
1694 } else if (fd == -1 || fstat(fd, sb) == -1) {
1695 if (errno == ENOENT) {
1696 if (got_fileindex_entry_has_file_on_disk(ie))
1697 *status = GOT_STATUS_MISSING;
1699 *status = GOT_STATUS_DELETE;
1702 err = got_error_from_errno2("fstat", abspath);
1707 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1708 *status = GOT_STATUS_OBSTRUCTED;
1712 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1713 *status = GOT_STATUS_DELETE;
1715 } else if (!got_fileindex_entry_has_blob(ie) &&
1716 staged_status != GOT_STATUS_ADD) {
1717 *status = GOT_STATUS_ADD;
1721 if (!stat_info_differs(ie, sb))
1724 if (S_ISLNK(sb->st_mode) &&
1725 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1726 *status = GOT_STATUS_MODIFY;
1730 if (staged_status == GOT_STATUS_MODIFY ||
1731 staged_status == GOT_STATUS_ADD)
1732 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1734 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1736 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1740 if (S_ISLNK(sb->st_mode)) {
1741 err = get_symlink_modification_status(status, ie,
1742 abspath, dirfd, de_name, blob);
1747 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1749 err = got_error_from_errno2("openat", abspath);
1754 f = fdopen(fd, "r");
1756 err = got_error_from_errno2("fdopen", abspath);
1760 hdrlen = got_object_blob_get_hdrlen(blob);
1762 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1763 err = got_object_blob_read_block(&blen, blob);
1766 /* Skip length of blob object header first time around. */
1767 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1768 if (flen == 0 && ferror(f)) {
1769 err = got_error_from_errno("fread");
1774 *status = GOT_STATUS_MODIFY;
1776 } else if (flen == 0) {
1778 *status = GOT_STATUS_MODIFY;
1780 } else if (blen - hdrlen == flen) {
1781 /* Skip blob object header first time around. */
1782 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1783 *status = GOT_STATUS_MODIFY;
1787 *status = GOT_STATUS_MODIFY;
1793 if (*status == GOT_STATUS_MODIFY) {
1795 err = get_modified_file_content_status(status, f);
1796 } else if (xbit_differs(ie, sb->st_mode))
1797 *status = GOT_STATUS_MODE_CHANGE;
1800 got_object_blob_close(blob);
1801 if (f != NULL && fclose(f) == EOF && err == NULL)
1802 err = got_error_from_errno2("fclose", abspath);
1803 if (fd != -1 && close(fd) == -1 && err == NULL)
1804 err = got_error_from_errno2("close", abspath);
1809 * Update timestamps in the file index if a file is unmodified and
1810 * we had to run a full content comparison to find out.
1812 static const struct got_error *
1813 sync_timestamps(char *ondisk_path, unsigned char status,
1814 struct got_fileindex_entry *ie, struct stat *sb)
1816 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1817 return got_fileindex_entry_update(ie, ondisk_path,
1818 ie->blob_sha1, ie->commit_sha1, 1);
1823 static const struct got_error *
1824 update_blob(struct got_worktree *worktree,
1825 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1826 struct got_tree_entry *te, const char *path,
1827 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1830 const struct got_error *err = NULL;
1831 struct got_blob_object *blob = NULL;
1833 unsigned char status = GOT_STATUS_NO_CHANGE;
1836 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1837 return got_error_from_errno("asprintf");
1840 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1841 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1844 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1848 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1849 sb.st_mode = got_fileindex_perms_to_st(ie);
1851 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1852 status = GOT_STATUS_UNVERSIONED;
1855 if (status == GOT_STATUS_OBSTRUCTED) {
1856 err = (*progress_cb)(progress_arg, status, path);
1859 if (status == GOT_STATUS_CONFLICT) {
1860 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1865 if (ie && status != GOT_STATUS_MISSING &&
1866 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1867 if (got_fileindex_entry_has_commit(ie) &&
1868 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1869 SHA1_DIGEST_LENGTH) == 0) {
1870 err = sync_timestamps(ondisk_path, status, ie, &sb);
1873 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1877 if (got_fileindex_entry_has_blob(ie) &&
1878 memcmp(ie->blob_sha1, te->id.sha1,
1879 SHA1_DIGEST_LENGTH) == 0) {
1880 err = sync_timestamps(ondisk_path, status, ie, &sb);
1885 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1889 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1890 int update_timestamps;
1891 struct got_blob_object *blob2 = NULL;
1892 char *label_orig = NULL;
1893 if (got_fileindex_entry_has_blob(ie)) {
1894 struct got_object_id id2;
1895 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1896 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1900 if (got_fileindex_entry_has_commit(ie)) {
1901 char id_str[SHA1_DIGEST_STRING_LENGTH];
1902 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1903 sizeof(id_str)) == NULL) {
1904 err = got_error_path(id_str,
1905 GOT_ERR_BAD_OBJ_ID_STR);
1908 if (asprintf(&label_orig, "%s: commit %s",
1909 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1910 err = got_error_from_errno("asprintf");
1914 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1916 err = got_object_blob_read_to_str(&link_target, blob);
1919 err = merge_symlink(worktree, blob2, ondisk_path, path,
1920 label_orig, link_target, worktree->base_commit_id,
1921 repo, progress_cb, progress_arg);
1924 err = merge_blob(&update_timestamps, worktree, blob2,
1925 ondisk_path, path, sb.st_mode, label_orig, blob,
1926 worktree->base_commit_id, repo,
1927 progress_cb, progress_arg);
1931 got_object_blob_close(blob2);
1935 * Do not update timestamps of files with local changes.
1936 * Otherwise, a future status walk would treat them as
1937 * unmodified files again.
1939 err = got_fileindex_entry_update(ie, ondisk_path,
1940 blob->id.sha1, worktree->base_commit_id->sha1,
1942 } else if (status == GOT_STATUS_MODE_CHANGE) {
1943 err = got_fileindex_entry_update(ie, ondisk_path,
1944 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1945 } else if (status == GOT_STATUS_DELETE) {
1946 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1949 err = got_fileindex_entry_update(ie, ondisk_path,
1950 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1954 int is_bad_symlink = 0;
1955 if (S_ISLNK(te->mode)) {
1956 err = install_symlink(&is_bad_symlink, worktree,
1957 ondisk_path, path, blob,
1958 status == GOT_STATUS_MISSING, 0,
1959 status == GOT_STATUS_UNVERSIONED, repo,
1960 progress_cb, progress_arg);
1962 err = install_blob(worktree, ondisk_path, path,
1963 te->mode, sb.st_mode, blob,
1964 status == GOT_STATUS_MISSING, 0, 0,
1965 status == GOT_STATUS_UNVERSIONED, repo,
1966 progress_cb, progress_arg);
1972 err = got_fileindex_entry_update(ie, ondisk_path,
1973 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1975 err = create_fileindex_entry(&ie, fileindex,
1976 worktree->base_commit_id, ondisk_path, path,
1982 if (is_bad_symlink) {
1983 got_fileindex_entry_filetype_set(ie,
1984 GOT_FILEIDX_MODE_BAD_SYMLINK);
1987 got_object_blob_close(blob);
1993 static const struct got_error *
1994 remove_ondisk_file(const char *root_path, const char *path)
1996 const struct got_error *err = NULL;
1997 char *ondisk_path = NULL;
1999 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2000 return got_error_from_errno("asprintf");
2002 if (unlink(ondisk_path) == -1) {
2003 if (errno != ENOENT)
2004 err = got_error_from_errno2("unlink", ondisk_path);
2006 char *parent = dirname(ondisk_path);
2007 while (parent && strcmp(parent, root_path) != 0) {
2008 if (rmdir(parent) == -1) {
2009 if (errno != ENOTEMPTY)
2010 err = got_error_from_errno2("rmdir",
2014 parent = dirname(parent);
2021 static const struct got_error *
2022 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2023 struct got_fileindex_entry *ie, struct got_repository *repo,
2024 got_worktree_checkout_cb progress_cb, void *progress_arg)
2026 const struct got_error *err = NULL;
2027 unsigned char status;
2031 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2032 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2034 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2036 return got_error_from_errno("asprintf");
2038 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2042 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2043 char ondisk_target[PATH_MAX];
2044 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2045 sizeof(ondisk_target));
2046 if (ondisk_len == -1) {
2047 err = got_error_from_errno2("readlink", ondisk_path);
2050 ondisk_target[ondisk_len] = '\0';
2051 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2052 NULL, NULL, /* XXX pass common ancestor info? */
2053 ondisk_target, ondisk_path);
2056 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2061 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2062 status == GOT_STATUS_ADD) {
2063 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2067 * Preserve the working file and change the deleted blob's
2068 * entry into a schedule-add entry.
2070 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2073 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2076 if (status == GOT_STATUS_NO_CHANGE) {
2077 err = remove_ondisk_file(worktree->root_path, ie->path);
2081 got_fileindex_entry_remove(fileindex, ie);
2088 struct diff_cb_arg {
2089 struct got_fileindex *fileindex;
2090 struct got_worktree *worktree;
2091 struct got_repository *repo;
2092 got_worktree_checkout_cb progress_cb;
2094 got_cancel_cb cancel_cb;
2098 static const struct got_error *
2099 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2100 struct got_tree_entry *te, const char *parent_path)
2102 struct diff_cb_arg *a = arg;
2104 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2105 return got_error(GOT_ERR_CANCELLED);
2107 return update_blob(a->worktree, a->fileindex, ie, te,
2108 ie->path, a->repo, a->progress_cb, a->progress_arg);
2111 static const struct got_error *
2112 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2114 struct diff_cb_arg *a = arg;
2116 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2117 return got_error(GOT_ERR_CANCELLED);
2119 return delete_blob(a->worktree, a->fileindex, ie,
2120 a->repo, a->progress_cb, a->progress_arg);
2123 static const struct got_error *
2124 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2126 struct diff_cb_arg *a = arg;
2127 const struct got_error *err;
2130 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2131 return got_error(GOT_ERR_CANCELLED);
2133 if (got_object_tree_entry_is_submodule(te))
2136 if (asprintf(&path, "%s%s%s", parent_path,
2137 parent_path[0] ? "/" : "", te->name)
2139 return got_error_from_errno("asprintf");
2141 if (S_ISDIR(te->mode))
2142 err = add_dir_on_disk(a->worktree, path);
2144 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2145 a->repo, a->progress_cb, a->progress_arg);
2151 static const struct got_error *
2152 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2154 const struct got_error *err = NULL;
2155 char *uuidstr = NULL;
2156 uint32_t uuid_status;
2160 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2161 if (uuid_status != uuid_s_ok)
2162 return got_error_uuid(uuid_status, "uuid_to_string");
2164 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2166 err = got_error_from_errno("asprintf");
2173 const struct got_error *
2174 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2176 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2179 static const struct got_error *
2180 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2182 return get_ref_name(refname, worktree,
2183 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2186 static const struct got_error *
2187 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2189 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2192 static const struct got_error *
2193 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2195 return get_ref_name(refname, worktree,
2196 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2199 static const struct got_error *
2200 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2202 return get_ref_name(refname, worktree,
2203 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2206 static const struct got_error *
2207 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2209 return get_ref_name(refname, worktree,
2210 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2213 static const struct got_error *
2214 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2216 return get_ref_name(refname, worktree,
2217 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2220 static const struct got_error *
2221 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2223 return get_ref_name(refname, worktree,
2224 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2227 static const struct got_error *
2228 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2234 const struct got_error *
2235 got_worktree_get_histedit_script_path(char **path,
2236 struct got_worktree *worktree)
2238 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2239 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2241 return got_error_from_errno("asprintf");
2247 * Prevent Git's garbage collector from deleting our base commit by
2248 * setting a reference to our base commit's ID.
2250 static const struct got_error *
2251 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2253 const struct got_error *err = NULL;
2254 struct got_reference *ref = NULL;
2257 err = got_worktree_get_base_ref_name(&refname, worktree);
2261 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2265 err = got_ref_write(ref, repo);
2273 static const struct got_error *
2274 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2276 const struct got_error *err = NULL;
2278 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2279 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2280 err = got_error_from_errno("asprintf");
2281 *fileindex_path = NULL;
2287 static const struct got_error *
2288 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2289 struct got_worktree *worktree)
2291 const struct got_error *err = NULL;
2294 *fileindex_path = NULL;
2295 *fileindex = got_fileindex_alloc();
2296 if (*fileindex == NULL)
2297 return got_error_from_errno("got_fileindex_alloc");
2299 err = get_fileindex_path(fileindex_path, worktree);
2303 index = fopen(*fileindex_path, "rb");
2304 if (index == NULL) {
2305 if (errno != ENOENT)
2306 err = got_error_from_errno2("fopen", *fileindex_path);
2308 err = got_fileindex_read(*fileindex, index);
2309 if (fclose(index) != 0 && err == NULL)
2310 err = got_error_from_errno("fclose");
2314 free(*fileindex_path);
2315 *fileindex_path = NULL;
2316 got_fileindex_free(*fileindex);
2322 struct bump_base_commit_id_arg {
2323 struct got_object_id *base_commit_id;
2326 const char *entry_name;
2327 got_worktree_checkout_cb progress_cb;
2331 /* Bump base commit ID of all files within an updated part of the work tree. */
2332 static const struct got_error *
2333 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2335 const struct got_error *err;
2336 struct bump_base_commit_id_arg *a = arg;
2338 if (a->entry_name) {
2339 if (strcmp(ie->path, a->path) != 0)
2341 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2344 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2345 SHA1_DIGEST_LENGTH) == 0)
2348 if (a->progress_cb) {
2349 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2354 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2358 static const struct got_error *
2359 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2361 const struct got_error *err = NULL;
2362 char *new_fileindex_path = NULL;
2363 FILE *new_index = NULL;
2364 struct timespec timeout;
2366 err = got_opentemp_named(&new_fileindex_path, &new_index,
2371 err = got_fileindex_write(fileindex, new_index);
2375 if (rename(new_fileindex_path, fileindex_path) != 0) {
2376 err = got_error_from_errno3("rename", new_fileindex_path,
2378 unlink(new_fileindex_path);
2382 * Sleep for a short amount of time to ensure that files modified after
2383 * this program exits have a different time stamp from the one which
2384 * was recorded in the file index.
2387 timeout.tv_nsec = 1;
2388 nanosleep(&timeout, NULL);
2392 free(new_fileindex_path);
2396 static const struct got_error *
2397 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2398 struct got_object_id **tree_id, const char *wt_relpath,
2399 struct got_worktree *worktree, struct got_repository *repo)
2401 const struct got_error *err = NULL;
2402 struct got_object_id *id = NULL;
2403 char *in_repo_path = NULL;
2404 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2406 *entry_type = GOT_OBJ_TYPE_ANY;
2407 *tree_relpath = NULL;
2410 if (wt_relpath[0] == '\0') {
2411 /* Check out all files within the work tree. */
2412 *entry_type = GOT_OBJ_TYPE_TREE;
2413 *tree_relpath = strdup("");
2414 if (*tree_relpath == NULL) {
2415 err = got_error_from_errno("strdup");
2418 err = got_object_id_by_path(tree_id, repo,
2419 worktree->base_commit_id, worktree->path_prefix);
2425 /* Check out a subset of files in the work tree. */
2427 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2428 is_root_wt ? "" : "/", wt_relpath) == -1) {
2429 err = got_error_from_errno("asprintf");
2433 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2439 in_repo_path = NULL;
2441 err = got_object_get_type(entry_type, repo, id);
2445 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2446 /* Check out a single file. */
2447 if (strchr(wt_relpath, '/') == NULL) {
2448 /* Check out a single file in work tree's root dir. */
2449 in_repo_path = strdup(worktree->path_prefix);
2450 if (in_repo_path == NULL) {
2451 err = got_error_from_errno("strdup");
2454 *tree_relpath = strdup("");
2455 if (*tree_relpath == NULL) {
2456 err = got_error_from_errno("strdup");
2460 /* Check out a single file in a subdirectory. */
2461 err = got_path_dirname(tree_relpath, wt_relpath);
2464 if (asprintf(&in_repo_path, "%s%s%s",
2465 worktree->path_prefix, is_root_wt ? "" : "/",
2466 *tree_relpath) == -1) {
2467 err = got_error_from_errno("asprintf");
2471 err = got_object_id_by_path(tree_id, repo,
2472 worktree->base_commit_id, in_repo_path);
2474 /* Check out all files within a subdirectory. */
2475 *tree_id = got_object_id_dup(id);
2476 if (*tree_id == NULL) {
2477 err = got_error_from_errno("got_object_id_dup");
2480 *tree_relpath = strdup(wt_relpath);
2481 if (*tree_relpath == NULL) {
2482 err = got_error_from_errno("strdup");
2490 *entry_type = GOT_OBJ_TYPE_ANY;
2491 free(*tree_relpath);
2492 *tree_relpath = NULL;
2499 static const struct got_error *
2500 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2501 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2502 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2503 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2505 const struct got_error *err = NULL;
2506 struct got_commit_object *commit = NULL;
2507 struct got_tree_object *tree = NULL;
2508 struct got_fileindex_diff_tree_cb diff_cb;
2509 struct diff_cb_arg arg;
2511 err = ref_base_commit(worktree, repo);
2513 if (!(err->code == GOT_ERR_ERRNO &&
2514 (errno == EACCES || errno == EROFS)))
2516 err = (*progress_cb)(progress_arg,
2517 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2522 err = got_object_open_as_commit(&commit, repo,
2523 worktree->base_commit_id);
2527 err = got_object_open_as_tree(&tree, repo, tree_id);
2532 got_object_tree_find_entry(tree, entry_name) == NULL) {
2533 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2537 diff_cb.diff_old_new = diff_old_new;
2538 diff_cb.diff_old = diff_old;
2539 diff_cb.diff_new = diff_new;
2540 arg.fileindex = fileindex;
2541 arg.worktree = worktree;
2543 arg.progress_cb = progress_cb;
2544 arg.progress_arg = progress_arg;
2545 arg.cancel_cb = cancel_cb;
2546 arg.cancel_arg = cancel_arg;
2547 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2548 entry_name, repo, &diff_cb, &arg);
2551 got_object_tree_close(tree);
2553 got_object_commit_close(commit);
2557 const struct got_error *
2558 got_worktree_checkout_files(struct got_worktree *worktree,
2559 struct got_pathlist_head *paths, struct got_repository *repo,
2560 got_worktree_checkout_cb progress_cb, void *progress_arg,
2561 got_cancel_cb cancel_cb, void *cancel_arg)
2563 const struct got_error *err = NULL, *sync_err, *unlockerr;
2564 struct got_commit_object *commit = NULL;
2565 struct got_tree_object *tree = NULL;
2566 struct got_fileindex *fileindex = NULL;
2567 char *fileindex_path = NULL;
2568 struct got_pathlist_entry *pe;
2569 struct tree_path_data {
2570 SIMPLEQ_ENTRY(tree_path_data) entry;
2571 struct got_object_id *tree_id;
2576 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2578 SIMPLEQ_INIT(&tree_paths);
2580 err = lock_worktree(worktree, LOCK_EX);
2584 /* Map all specified paths to in-repository trees. */
2585 TAILQ_FOREACH(pe, paths, entry) {
2586 tpd = malloc(sizeof(*tpd));
2588 err = got_error_from_errno("malloc");
2592 err = find_tree_entry_for_checkout(&tpd->entry_type,
2593 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2599 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2600 err = got_path_basename(&tpd->entry_name, pe->path);
2608 tpd->entry_name = NULL;
2610 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2614 * Read the file index.
2615 * Checking out files is supposed to be an idempotent operation.
2616 * If the on-disk file index is incomplete we will try to complete it.
2618 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2622 tpd = SIMPLEQ_FIRST(&tree_paths);
2623 TAILQ_FOREACH(pe, paths, entry) {
2624 struct bump_base_commit_id_arg bbc_arg;
2626 err = checkout_files(worktree, fileindex, tpd->relpath,
2627 tpd->tree_id, tpd->entry_name, repo,
2628 progress_cb, progress_arg, cancel_cb, cancel_arg);
2632 bbc_arg.base_commit_id = worktree->base_commit_id;
2633 bbc_arg.entry_name = tpd->entry_name;
2634 bbc_arg.path = pe->path;
2635 bbc_arg.path_len = pe->path_len;
2636 bbc_arg.progress_cb = progress_cb;
2637 bbc_arg.progress_arg = progress_arg;
2638 err = got_fileindex_for_each_entry_safe(fileindex,
2639 bump_base_commit_id, &bbc_arg);
2643 tpd = SIMPLEQ_NEXT(tpd, entry);
2645 sync_err = sync_fileindex(fileindex, fileindex_path);
2646 if (sync_err && err == NULL)
2649 free(fileindex_path);
2651 got_object_tree_close(tree);
2653 got_object_commit_close(commit);
2655 got_fileindex_free(fileindex);
2656 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2657 tpd = SIMPLEQ_FIRST(&tree_paths);
2658 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2663 unlockerr = lock_worktree(worktree, LOCK_SH);
2664 if (unlockerr && err == NULL)
2669 struct merge_file_cb_arg {
2670 struct got_worktree *worktree;
2671 struct got_fileindex *fileindex;
2672 got_worktree_checkout_cb progress_cb;
2674 got_cancel_cb cancel_cb;
2676 const char *label_orig;
2677 struct got_object_id *commit_id2;
2680 static const struct got_error *
2681 merge_file_cb(void *arg, struct got_blob_object *blob1,
2682 struct got_blob_object *blob2, struct got_object_id *id1,
2683 struct got_object_id *id2, const char *path1, const char *path2,
2684 mode_t mode1, mode_t mode2, struct got_repository *repo)
2686 static const struct got_error *err = NULL;
2687 struct merge_file_cb_arg *a = arg;
2688 struct got_fileindex_entry *ie;
2689 char *ondisk_path = NULL;
2691 unsigned char status;
2692 int local_changes_subsumed;
2694 if (blob1 && blob2) {
2695 ie = got_fileindex_entry_get(a->fileindex, path2,
2698 return (*a->progress_cb)(a->progress_arg,
2699 GOT_STATUS_MISSING, path2);
2701 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2703 return got_error_from_errno("asprintf");
2705 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2710 if (status == GOT_STATUS_DELETE) {
2711 err = (*a->progress_cb)(a->progress_arg,
2712 GOT_STATUS_MERGE, path2);
2715 if (status != GOT_STATUS_NO_CHANGE &&
2716 status != GOT_STATUS_MODIFY &&
2717 status != GOT_STATUS_CONFLICT &&
2718 status != GOT_STATUS_ADD) {
2719 err = (*a->progress_cb)(a->progress_arg, status, path2);
2723 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2725 err = got_object_blob_read_to_str(&link_target2, blob2);
2728 err = merge_symlink(a->worktree, blob1, ondisk_path,
2729 path2, a->label_orig, link_target2, a->commit_id2,
2730 repo, a->progress_cb, a->progress_arg);
2733 err = merge_blob(&local_changes_subsumed, a->worktree,
2734 blob1, ondisk_path, path2, sb.st_mode,
2735 a->label_orig, blob2, a->commit_id2, repo,
2736 a->progress_cb, a->progress_arg);
2739 ie = got_fileindex_entry_get(a->fileindex, path1,
2742 return (*a->progress_cb)(a->progress_arg,
2743 GOT_STATUS_MISSING, path1);
2745 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2747 return got_error_from_errno("asprintf");
2749 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2755 case GOT_STATUS_NO_CHANGE:
2756 err = (*a->progress_cb)(a->progress_arg,
2757 GOT_STATUS_DELETE, path1);
2760 err = remove_ondisk_file(a->worktree->root_path, path1);
2764 got_fileindex_entry_mark_deleted_from_disk(ie);
2766 case GOT_STATUS_DELETE:
2767 case GOT_STATUS_MISSING:
2768 err = (*a->progress_cb)(a->progress_arg,
2769 GOT_STATUS_DELETE, path1);
2773 got_fileindex_entry_mark_deleted_from_disk(ie);
2775 case GOT_STATUS_ADD:
2776 case GOT_STATUS_MODIFY:
2777 case GOT_STATUS_CONFLICT:
2778 err = (*a->progress_cb)(a->progress_arg,
2779 GOT_STATUS_CANNOT_DELETE, path1);
2783 case GOT_STATUS_OBSTRUCTED:
2784 err = (*a->progress_cb)(a->progress_arg, status, path1);
2792 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2794 return got_error_from_errno("asprintf");
2795 ie = got_fileindex_entry_get(a->fileindex, path2,
2798 err = get_file_status(&status, &sb, ie, ondisk_path,
2802 if (status != GOT_STATUS_NO_CHANGE &&
2803 status != GOT_STATUS_MODIFY &&
2804 status != GOT_STATUS_CONFLICT &&
2805 status != GOT_STATUS_ADD) {
2806 err = (*a->progress_cb)(a->progress_arg,
2810 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2812 err = got_object_blob_read_to_str(&link_target2,
2816 err = merge_symlink(a->worktree, NULL,
2817 ondisk_path, path2, a->label_orig,
2818 link_target2, a->commit_id2, repo,
2819 a->progress_cb, a->progress_arg);
2821 } else if (S_ISREG(sb.st_mode)) {
2822 err = merge_blob(&local_changes_subsumed,
2823 a->worktree, NULL, ondisk_path, path2,
2824 sb.st_mode, a->label_orig, blob2,
2825 a->commit_id2, repo, a->progress_cb,
2828 err = got_error_path(ondisk_path,
2829 GOT_ERR_FILE_OBSTRUCTED);
2833 if (status == GOT_STATUS_DELETE) {
2834 err = got_fileindex_entry_update(ie,
2835 ondisk_path, blob2->id.sha1,
2836 a->worktree->base_commit_id->sha1, 0);
2841 int is_bad_symlink = 0;
2842 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2843 if (S_ISLNK(mode2)) {
2844 err = install_symlink(&is_bad_symlink,
2845 a->worktree, ondisk_path, path2, blob2, 0,
2846 0, 1, repo, a->progress_cb, a->progress_arg);
2848 err = install_blob(a->worktree, ondisk_path, path2,
2849 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2850 a->progress_cb, a->progress_arg);
2854 err = got_fileindex_entry_alloc(&ie, path2);
2857 err = got_fileindex_entry_update(ie, ondisk_path,
2860 got_fileindex_entry_free(ie);
2863 err = got_fileindex_entry_add(a->fileindex, ie);
2865 got_fileindex_entry_free(ie);
2868 if (is_bad_symlink) {
2869 got_fileindex_entry_filetype_set(ie,
2870 GOT_FILEIDX_MODE_BAD_SYMLINK);
2879 struct check_merge_ok_arg {
2880 struct got_worktree *worktree;
2881 struct got_repository *repo;
2884 static const struct got_error *
2885 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2887 const struct got_error *err = NULL;
2888 struct check_merge_ok_arg *a = arg;
2889 unsigned char status;
2893 /* Reject merges into a work tree with mixed base commits. */
2894 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2895 SHA1_DIGEST_LENGTH))
2896 return got_error(GOT_ERR_MIXED_COMMITS);
2898 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2900 return got_error_from_errno("asprintf");
2902 /* Reject merges into a work tree with conflicted files. */
2903 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2906 if (status == GOT_STATUS_CONFLICT)
2907 return got_error(GOT_ERR_CONFLICTS);
2912 static const struct got_error *
2913 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2914 const char *fileindex_path, struct got_object_id *commit_id1,
2915 struct got_object_id *commit_id2, struct got_repository *repo,
2916 got_worktree_checkout_cb progress_cb, void *progress_arg,
2917 got_cancel_cb cancel_cb, void *cancel_arg)
2919 const struct got_error *err = NULL, *sync_err;
2920 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2921 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2922 struct merge_file_cb_arg arg;
2923 char *label_orig = NULL;
2928 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2929 worktree->path_prefix);
2933 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2937 err = got_object_id_str(&id_str, commit_id1);
2941 if (asprintf(&label_orig, "%s: commit %s",
2942 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2943 err = got_error_from_errno("asprintf");
2950 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2951 worktree->path_prefix);
2955 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2959 arg.worktree = worktree;
2960 arg.fileindex = fileindex;
2961 arg.progress_cb = progress_cb;
2962 arg.progress_arg = progress_arg;
2963 arg.cancel_cb = cancel_cb;
2964 arg.cancel_arg = cancel_arg;
2965 arg.label_orig = label_orig;
2966 arg.commit_id2 = commit_id2;
2967 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2968 sync_err = sync_fileindex(fileindex, fileindex_path);
2969 if (sync_err && err == NULL)
2973 got_object_tree_close(tree1);
2975 got_object_tree_close(tree2);
2980 const struct got_error *
2981 got_worktree_merge_files(struct got_worktree *worktree,
2982 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2983 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2984 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2986 const struct got_error *err, *unlockerr;
2987 char *fileindex_path = NULL;
2988 struct got_fileindex *fileindex = NULL;
2989 struct check_merge_ok_arg mok_arg;
2991 err = lock_worktree(worktree, LOCK_EX);
2995 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2999 mok_arg.worktree = worktree;
3000 mok_arg.repo = repo;
3001 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3006 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3007 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3010 got_fileindex_free(fileindex);
3011 free(fileindex_path);
3012 unlockerr = lock_worktree(worktree, LOCK_SH);
3013 if (unlockerr && err == NULL)
3018 struct diff_dir_cb_arg {
3019 struct got_fileindex *fileindex;
3020 struct got_worktree *worktree;
3021 const char *status_path;
3022 size_t status_path_len;
3023 struct got_repository *repo;
3024 got_worktree_status_cb status_cb;
3026 got_cancel_cb cancel_cb;
3028 /* A pathlist containing per-directory pathlists of ignore patterns. */
3029 struct got_pathlist_head ignores;
3030 int report_unchanged;
3034 static const struct got_error *
3035 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3036 int dirfd, const char *de_name,
3037 got_worktree_status_cb status_cb, void *status_arg,
3038 struct got_repository *repo, int report_unchanged)
3040 const struct got_error *err = NULL;
3041 unsigned char status = GOT_STATUS_NO_CHANGE;
3042 unsigned char staged_status = get_staged_status(ie);
3044 struct got_object_id blob_id, commit_id, staged_blob_id;
3045 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3046 struct got_object_id *staged_blob_idp = NULL;
3048 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3052 if (status == GOT_STATUS_NO_CHANGE &&
3053 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3056 if (got_fileindex_entry_has_blob(ie)) {
3057 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3058 blob_idp = &blob_id;
3060 if (got_fileindex_entry_has_commit(ie)) {
3061 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3062 commit_idp = &commit_id;
3064 if (staged_status == GOT_STATUS_ADD ||
3065 staged_status == GOT_STATUS_MODIFY) {
3066 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3067 SHA1_DIGEST_LENGTH);
3068 staged_blob_idp = &staged_blob_id;
3071 return (*status_cb)(status_arg, status, staged_status,
3072 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3075 static const struct got_error *
3076 status_old_new(void *arg, struct got_fileindex_entry *ie,
3077 struct dirent *de, const char *parent_path, int dirfd)
3079 const struct got_error *err = NULL;
3080 struct diff_dir_cb_arg *a = arg;
3083 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3084 return got_error(GOT_ERR_CANCELLED);
3086 if (got_path_cmp(parent_path, a->status_path,
3087 strlen(parent_path), a->status_path_len) != 0 &&
3088 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3091 if (parent_path[0]) {
3092 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3093 parent_path, de->d_name) == -1)
3094 return got_error_from_errno("asprintf");
3096 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3098 return got_error_from_errno("asprintf");
3101 err = report_file_status(ie, abspath, dirfd, de->d_name,
3102 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3107 static const struct got_error *
3108 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3110 struct diff_dir_cb_arg *a = arg;
3111 struct got_object_id blob_id, commit_id;
3112 unsigned char status;
3114 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3115 return got_error(GOT_ERR_CANCELLED);
3117 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3120 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3121 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3122 if (got_fileindex_entry_has_file_on_disk(ie))
3123 status = GOT_STATUS_MISSING;
3125 status = GOT_STATUS_DELETE;
3126 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3127 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3131 free_ignorelist(struct got_pathlist_head *ignorelist)
3133 struct got_pathlist_entry *pe;
3135 TAILQ_FOREACH(pe, ignorelist, entry)
3136 free((char *)pe->path);
3137 got_pathlist_free(ignorelist);
3141 free_ignores(struct got_pathlist_head *ignores)
3143 struct got_pathlist_entry *pe;
3145 TAILQ_FOREACH(pe, ignores, entry) {
3146 struct got_pathlist_head *ignorelist = pe->data;
3147 free_ignorelist(ignorelist);
3148 free((char *)pe->path);
3150 got_pathlist_free(ignores);
3153 static const struct got_error *
3154 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3156 const struct got_error *err = NULL;
3157 struct got_pathlist_entry *pe = NULL;
3158 struct got_pathlist_head *ignorelist;
3159 char *line = NULL, *pattern, *dirpath = NULL;
3160 size_t linesize = 0;
3163 ignorelist = calloc(1, sizeof(*ignorelist));
3164 if (ignorelist == NULL)
3165 return got_error_from_errno("calloc");
3166 TAILQ_INIT(ignorelist);
3168 while ((linelen = getline(&line, &linesize, f)) != -1) {
3169 if (linelen > 0 && line[linelen - 1] == '\n')
3170 line[linelen - 1] = '\0';
3172 /* Git's ignores may contain comments. */
3176 /* Git's negated patterns are not (yet?) supported. */
3180 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3182 err = got_error_from_errno("asprintf");
3185 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3190 err = got_error_from_errno("getline");
3194 dirpath = strdup(path);
3195 if (dirpath == NULL) {
3196 err = got_error_from_errno("strdup");
3199 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3202 if (err || pe == NULL) {
3204 free_ignorelist(ignorelist);
3210 match_ignores(struct got_pathlist_head *ignores, const char *path)
3212 struct got_pathlist_entry *pe;
3214 /* Handle patterns which match in all directories. */
3215 TAILQ_FOREACH(pe, ignores, entry) {
3216 struct got_pathlist_head *ignorelist = pe->data;
3217 struct got_pathlist_entry *pi;
3219 TAILQ_FOREACH(pi, ignorelist, entry) {
3220 const char *p, *pattern = pi->path;
3222 if (strncmp(pattern, "**/", 3) != 0)
3227 if (fnmatch(pattern, p,
3228 FNM_PATHNAME | FNM_LEADING_DIR)) {
3229 /* Retry in next directory. */
3230 while (*p && *p != '/')
3242 * The ignores pathlist contains ignore lists from children before
3243 * parents, so we can find the most specific ignorelist by walking
3244 * ignores backwards.
3246 pe = TAILQ_LAST(ignores, got_pathlist_head);
3248 if (got_path_is_child(path, pe->path, pe->path_len)) {
3249 struct got_pathlist_head *ignorelist = pe->data;
3250 struct got_pathlist_entry *pi;
3251 TAILQ_FOREACH(pi, ignorelist, entry) {
3252 const char *pattern = pi->path;
3253 int flags = FNM_LEADING_DIR;
3254 if (strstr(pattern, "/**/") == NULL)
3255 flags |= FNM_PATHNAME;
3256 if (fnmatch(pattern, path, flags))
3261 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3267 static const struct got_error *
3268 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3269 const char *path, int dirfd, const char *ignores_filename)
3271 const struct got_error *err = NULL;
3274 FILE *ignoresfile = NULL;
3276 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3277 path[0] ? "/" : "", ignores_filename) == -1)
3278 return got_error_from_errno("asprintf");
3281 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3283 if (errno != ENOENT && errno != EACCES)
3284 err = got_error_from_errno2("openat",
3287 ignoresfile = fdopen(fd, "r");
3288 if (ignoresfile == NULL)
3289 err = got_error_from_errno2("fdopen",
3293 err = read_ignores(ignores, path, ignoresfile);
3297 ignoresfile = fopen(ignorespath, "r");
3298 if (ignoresfile == NULL) {
3299 if (errno != ENOENT && errno != EACCES)
3300 err = got_error_from_errno2("fopen",
3303 err = read_ignores(ignores, path, ignoresfile);
3306 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3307 err = got_error_from_errno2("fclose", path);
3308 if (fd != -1 && close(fd) == -1 && err == NULL)
3309 err = got_error_from_errno2("close", path);
3314 static const struct got_error *
3315 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3317 const struct got_error *err = NULL;
3318 struct diff_dir_cb_arg *a = arg;
3321 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3322 return got_error(GOT_ERR_CANCELLED);
3324 if (parent_path[0]) {
3325 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3326 return got_error_from_errno("asprintf");
3331 if (de->d_type != DT_DIR &&
3332 got_path_is_child(path, a->status_path, a->status_path_len)
3333 && !match_ignores(&a->ignores, path))
3334 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3335 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3341 static const struct got_error *
3342 status_traverse(void *arg, const char *path, int dirfd)
3344 const struct got_error *err = NULL;
3345 struct diff_dir_cb_arg *a = arg;
3350 err = add_ignores(&a->ignores, a->worktree->root_path,
3351 path, dirfd, ".cvsignore");
3355 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3356 dirfd, ".gitignore");
3361 static const struct got_error *
3362 report_single_file_status(const char *path, const char *ondisk_path,
3363 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3364 void *status_arg, struct got_repository *repo, int report_unchanged)
3366 struct got_fileindex_entry *ie;
3369 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3371 return report_file_status(ie, ondisk_path, -1, NULL,
3372 status_cb, status_arg, repo, report_unchanged);
3374 if (lstat(ondisk_path, &sb) == -1) {
3375 if (errno != ENOENT)
3376 return got_error_from_errno2("lstat", ondisk_path);
3377 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3378 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3382 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3383 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3384 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3389 static const struct got_error *
3390 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3391 const char *root_path, const char *path)
3393 const struct got_error *err;
3394 char *parent_path, *next_parent_path = NULL;
3396 err = add_ignores(ignores, root_path, "", -1,
3401 err = add_ignores(ignores, root_path, "", -1,
3406 err = got_path_dirname(&parent_path, path);
3408 if (err->code == GOT_ERR_BAD_PATH)
3409 return NULL; /* cannot traverse parent */
3413 err = add_ignores(ignores, root_path, parent_path, -1,
3417 err = add_ignores(ignores, root_path, parent_path, -1,
3421 err = got_path_dirname(&next_parent_path, parent_path);
3423 if (err->code == GOT_ERR_BAD_PATH)
3424 err = NULL; /* traversed everything */
3428 parent_path = next_parent_path;
3429 next_parent_path = NULL;
3433 free(next_parent_path);
3437 static const struct got_error *
3438 worktree_status(struct got_worktree *worktree, const char *path,
3439 struct got_fileindex *fileindex, struct got_repository *repo,
3440 got_worktree_status_cb status_cb, void *status_arg,
3441 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3442 int report_unchanged)
3444 const struct got_error *err = NULL;
3446 struct got_fileindex_diff_dir_cb fdiff_cb;
3447 struct diff_dir_cb_arg arg;
3448 char *ondisk_path = NULL;
3450 TAILQ_INIT(&arg.ignores);
3452 if (asprintf(&ondisk_path, "%s%s%s",
3453 worktree->root_path, path[0] ? "/" : "", path) == -1)
3454 return got_error_from_errno("asprintf");
3456 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3458 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3460 err = got_error_from_errno2("open", ondisk_path);
3462 err = report_single_file_status(path, ondisk_path,
3463 fileindex, status_cb, status_arg, repo,
3466 fdiff_cb.diff_old_new = status_old_new;
3467 fdiff_cb.diff_old = status_old;
3468 fdiff_cb.diff_new = status_new;
3469 fdiff_cb.diff_traverse = status_traverse;
3470 arg.fileindex = fileindex;
3471 arg.worktree = worktree;
3472 arg.status_path = path;
3473 arg.status_path_len = strlen(path);
3475 arg.status_cb = status_cb;
3476 arg.status_arg = status_arg;
3477 arg.cancel_cb = cancel_cb;
3478 arg.cancel_arg = cancel_arg;
3479 arg.report_unchanged = report_unchanged;
3480 arg.no_ignores = no_ignores;
3482 err = add_ignores_from_parent_paths(&arg.ignores,
3483 worktree->root_path, path);
3487 err = got_fileindex_diff_dir(fileindex, fd,
3488 worktree->root_path, path, repo, &fdiff_cb, &arg);
3491 free_ignores(&arg.ignores);
3492 if (fd != -1 && close(fd) != 0 && err == NULL)
3493 err = got_error_from_errno("close");
3498 const struct got_error *
3499 got_worktree_status(struct got_worktree *worktree,
3500 struct got_pathlist_head *paths, struct got_repository *repo,
3501 got_worktree_status_cb status_cb, void *status_arg,
3502 got_cancel_cb cancel_cb, void *cancel_arg)
3504 const struct got_error *err = NULL;
3505 char *fileindex_path = NULL;
3506 struct got_fileindex *fileindex = NULL;
3507 struct got_pathlist_entry *pe;
3509 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3513 TAILQ_FOREACH(pe, paths, entry) {
3514 err = worktree_status(worktree, pe->path, fileindex, repo,
3515 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3519 free(fileindex_path);
3520 got_fileindex_free(fileindex);
3524 const struct got_error *
3525 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3528 const struct got_error *err = NULL;
3529 char *resolved = NULL, *cwd = NULL, *path = NULL;
3535 cwd = getcwd(NULL, 0);
3537 return got_error_from_errno("getcwd");
3539 if (lstat(arg, &sb) == -1) {
3540 if (errno != ENOENT) {
3541 err = got_error_from_errno2("lstat", arg);
3545 if (S_ISLNK(sb.st_mode)) {
3547 * We cannot use realpath(3) with symlinks since we want to
3548 * operate on the symlink itself.
3549 * But we can make the path absolute, assuming it is relative
3550 * to the current working directory, and then canonicalize it.
3552 char *abspath = NULL;
3553 char canonpath[PATH_MAX];
3554 if (!got_path_is_absolute(arg)) {
3555 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3556 err = got_error_from_errno("asprintf");
3561 err = got_canonpath(abspath ? abspath : arg, canonpath,
3565 resolved = strdup(canonpath);
3566 if (resolved == NULL) {
3567 err = got_error_from_errno("strdup");
3571 resolved = realpath(arg, NULL);
3572 if (resolved == NULL) {
3573 if (errno != ENOENT) {
3574 err = got_error_from_errno2("realpath", arg);
3577 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3578 err = got_error_from_errno("asprintf");
3584 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3585 strlen(got_worktree_get_root_path(worktree)))) {
3586 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3590 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3591 err = got_path_skip_common_ancestor(&path,
3592 got_worktree_get_root_path(worktree), resolved);
3598 err = got_error_from_errno("strdup");
3603 /* XXX status walk can't deal with trailing slash! */
3605 while (len > 0 && path[len - 1] == '/') {
3606 path[len - 1] = '\0';
3619 struct schedule_addition_args {
3620 struct got_worktree *worktree;
3621 struct got_fileindex *fileindex;
3622 got_worktree_checkout_cb progress_cb;
3624 struct got_repository *repo;
3627 static const struct got_error *
3628 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3629 const char *relpath, struct got_object_id *blob_id,
3630 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3631 int dirfd, const char *de_name)
3633 struct schedule_addition_args *a = arg;
3634 const struct got_error *err = NULL;
3635 struct got_fileindex_entry *ie;
3639 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3641 return got_error_from_errno("asprintf");
3643 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3645 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3649 /* Re-adding an existing entry is a no-op. */
3650 if (status == GOT_STATUS_ADD)
3652 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3657 if (status != GOT_STATUS_UNVERSIONED) {
3658 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3662 err = got_fileindex_entry_alloc(&ie, relpath);
3665 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3667 got_fileindex_entry_free(ie);
3670 err = got_fileindex_entry_add(a->fileindex, ie);
3672 got_fileindex_entry_free(ie);
3679 if (status == GOT_STATUS_ADD)
3681 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3684 const struct got_error *
3685 got_worktree_schedule_add(struct got_worktree *worktree,
3686 struct got_pathlist_head *paths,
3687 got_worktree_checkout_cb progress_cb, void *progress_arg,
3688 struct got_repository *repo, int no_ignores)
3690 struct got_fileindex *fileindex = NULL;
3691 char *fileindex_path = NULL;
3692 const struct got_error *err = NULL, *sync_err, *unlockerr;
3693 struct got_pathlist_entry *pe;
3694 struct schedule_addition_args saa;
3696 err = lock_worktree(worktree, LOCK_EX);
3700 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3704 saa.worktree = worktree;
3705 saa.fileindex = fileindex;
3706 saa.progress_cb = progress_cb;
3707 saa.progress_arg = progress_arg;
3710 TAILQ_FOREACH(pe, paths, entry) {
3711 err = worktree_status(worktree, pe->path, fileindex, repo,
3712 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3716 sync_err = sync_fileindex(fileindex, fileindex_path);
3717 if (sync_err && err == NULL)
3720 free(fileindex_path);
3722 got_fileindex_free(fileindex);
3723 unlockerr = lock_worktree(worktree, LOCK_SH);
3724 if (unlockerr && err == NULL)
3729 struct schedule_deletion_args {
3730 struct got_worktree *worktree;
3731 struct got_fileindex *fileindex;
3732 got_worktree_delete_cb progress_cb;
3734 struct got_repository *repo;
3735 int delete_local_mods;
3739 static const struct got_error *
3740 schedule_for_deletion(void *arg, unsigned char status,
3741 unsigned char staged_status, const char *relpath,
3742 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3743 struct got_object_id *commit_id, int dirfd, const char *de_name)
3745 struct schedule_deletion_args *a = arg;
3746 const struct got_error *err = NULL;
3747 struct got_fileindex_entry *ie = NULL;
3749 char *ondisk_path, *parent = NULL;
3751 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3753 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3755 staged_status = get_staged_status(ie);
3756 if (staged_status != GOT_STATUS_NO_CHANGE) {
3757 if (staged_status == GOT_STATUS_DELETE)
3759 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3762 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3764 return got_error_from_errno("asprintf");
3766 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3771 if (status != GOT_STATUS_NO_CHANGE) {
3772 if (status == GOT_STATUS_DELETE)
3774 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3775 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3778 if (status != GOT_STATUS_MODIFY &&
3779 status != GOT_STATUS_MISSING) {
3780 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3785 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3787 if (unlinkat(dirfd, de_name, 0) != 0) {
3788 err = got_error_from_errno2("unlinkat",
3792 } else if (unlink(ondisk_path) != 0) {
3793 err = got_error_from_errno2("unlink", ondisk_path);
3797 parent = dirname(ondisk_path);
3799 if (parent == NULL) {
3800 err = got_error_from_errno2("dirname", ondisk_path);
3803 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3804 if (rmdir(parent) == -1) {
3805 if (errno != ENOTEMPTY)
3806 err = got_error_from_errno2("rmdir",
3810 parent = dirname(parent);
3811 if (parent == NULL) {
3812 err = got_error_from_errno2("dirname", parent);
3818 got_fileindex_entry_mark_deleted_from_disk(ie);
3823 if (status == GOT_STATUS_DELETE)
3825 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3826 staged_status, relpath);
3829 const struct got_error *
3830 got_worktree_schedule_delete(struct got_worktree *worktree,
3831 struct got_pathlist_head *paths, int delete_local_mods,
3832 got_worktree_delete_cb progress_cb, void *progress_arg,
3833 struct got_repository *repo, int keep_on_disk)
3835 struct got_fileindex *fileindex = NULL;
3836 char *fileindex_path = NULL;
3837 const struct got_error *err = NULL, *sync_err, *unlockerr;
3838 struct got_pathlist_entry *pe;
3839 struct schedule_deletion_args sda;
3841 err = lock_worktree(worktree, LOCK_EX);
3845 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3849 sda.worktree = worktree;
3850 sda.fileindex = fileindex;
3851 sda.progress_cb = progress_cb;
3852 sda.progress_arg = progress_arg;
3854 sda.delete_local_mods = delete_local_mods;
3855 sda.keep_on_disk = keep_on_disk;
3857 TAILQ_FOREACH(pe, paths, entry) {
3858 err = worktree_status(worktree, pe->path, fileindex, repo,
3859 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3863 sync_err = sync_fileindex(fileindex, fileindex_path);
3864 if (sync_err && err == NULL)
3867 free(fileindex_path);
3869 got_fileindex_free(fileindex);
3870 unlockerr = lock_worktree(worktree, LOCK_SH);
3871 if (unlockerr && err == NULL)
3876 static const struct got_error *
3877 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3879 const struct got_error *err = NULL;
3881 size_t linesize = 0, n;
3884 linelen = getline(&line, &linesize, infile);
3885 if (linelen == -1) {
3886 if (ferror(infile)) {
3887 err = got_error_from_errno("getline");
3893 n = fwrite(line, 1, linelen, outfile);
3895 err = got_ferror(outfile, GOT_ERR_IO);
3900 n = fwrite(line, 1, linelen, rejectfile);
3902 err = got_ferror(outfile, GOT_ERR_IO);
3909 static const struct got_error *
3910 skip_one_line(FILE *f)
3913 size_t linesize = 0;
3916 linelen = getline(&line, &linesize, f);
3917 if (linelen == -1) {
3919 return got_error_from_errno("getline");
3926 static const struct got_error *
3927 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3928 int start_old, int end_old, int start_new, int end_new,
3929 FILE *outfile, FILE *rejectfile)
3931 const struct got_error *err;
3933 /* Copy old file's lines leading up to patch. */
3934 while (!feof(f1) && *line_cur1 < start_old) {
3935 err = copy_one_line(f1, outfile, NULL);
3940 /* Skip new file's lines leading up to patch. */
3941 while (!feof(f2) && *line_cur2 < start_new) {
3943 err = copy_one_line(f2, NULL, rejectfile);
3945 err = skip_one_line(f2);
3950 /* Copy patched lines. */
3951 while (!feof(f2) && *line_cur2 <= end_new) {
3952 err = copy_one_line(f2, outfile, NULL);
3957 /* Skip over old file's replaced lines. */
3958 while (!feof(f1) && *line_cur1 <= end_old) {
3960 err = copy_one_line(f1, NULL, rejectfile);
3962 err = skip_one_line(f1);
3971 static const struct got_error *
3972 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3973 FILE *outfile, FILE *rejectfile)
3975 const struct got_error *err;
3978 /* Copy old file's lines until EOF. */
3980 err = copy_one_line(f1, outfile, NULL);
3987 /* Copy new file's lines until EOF. */
3989 err = copy_one_line(f2, NULL, rejectfile);
3999 static const struct got_error *
4000 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4001 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4002 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4003 int *line_cur2, FILE *outfile, FILE *rejectfile,
4004 got_worktree_patch_cb patch_cb, void *patch_arg)
4006 const struct got_error *err = NULL;
4007 int start_old = change->cv.a;
4008 int end_old = change->cv.b;
4009 int start_new = change->cv.c;
4010 int end_new = change->cv.d;
4014 *choice = GOT_PATCH_CHOICE_NONE;
4016 hunkfile = got_opentemp();
4017 if (hunkfile == NULL)
4018 return got_error_from_errno("got_opentemp");
4023 /* XXX TODO needs error checking */
4024 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4026 if (fseek(f1, pos1, SEEK_SET) == -1) {
4027 err = got_ferror(f1, GOT_ERR_IO);
4030 if (fseek(f2, pos2, SEEK_SET) == -1) {
4031 err = got_ferror(f1, GOT_ERR_IO);
4034 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4035 err = got_ferror(hunkfile, GOT_ERR_IO);
4039 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4040 hunkfile, n, nchanges);
4045 case GOT_PATCH_CHOICE_YES:
4046 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4047 end_old, start_new, end_new, outfile, rejectfile);
4049 case GOT_PATCH_CHOICE_NO:
4050 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4051 end_old, start_new, end_new, rejectfile, outfile);
4053 case GOT_PATCH_CHOICE_QUIT:
4056 err = got_error(GOT_ERR_PATCH_CHOICE);
4060 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4061 err = got_error_from_errno("fclose");
4065 struct revert_file_args {
4066 struct got_worktree *worktree;
4067 struct got_fileindex *fileindex;
4068 got_worktree_checkout_cb progress_cb;
4070 got_worktree_patch_cb patch_cb;
4072 struct got_repository *repo;
4075 static const struct got_error *
4076 create_patched_content(char **path_outfile, int reverse_patch,
4077 struct got_object_id *blob_id, const char *path2,
4078 int dirfd2, const char *de_name2,
4079 const char *relpath, struct got_repository *repo,
4080 got_worktree_patch_cb patch_cb, void *patch_arg)
4082 const struct got_error *err;
4083 struct got_blob_object *blob = NULL;
4084 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4086 char link_target[PATH_MAX];
4087 ssize_t link_len = 0;
4088 char *path1 = NULL, *id_str = NULL;
4089 struct stat sb1, sb2;
4090 struct got_diff_changes *changes = NULL;
4091 struct got_diff_state *ds = NULL;
4092 struct got_diff_args *args = NULL;
4093 struct got_diff_change *change;
4094 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4097 *path_outfile = NULL;
4099 err = got_object_id_str(&id_str, blob_id);
4104 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4106 if (errno != ELOOP) {
4107 err = got_error_from_errno2("openat", path2);
4110 link_len = readlinkat(dirfd2, de_name2,
4111 link_target, sizeof(link_target));
4113 return got_error_from_errno2("readlinkat", path2);
4114 sb2.st_mode = S_IFLNK;
4115 sb2.st_size = link_len;
4118 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4120 if (errno != ELOOP) {
4121 err = got_error_from_errno2("open", path2);
4124 link_len = readlink(path2, link_target,
4125 sizeof(link_target));
4127 return got_error_from_errno2("readlink", path2);
4128 sb2.st_mode = S_IFLNK;
4129 sb2.st_size = link_len;
4133 if (fstat(fd2, &sb2) == -1) {
4134 err = got_error_from_errno2("fstat", path2);
4138 f2 = fdopen(fd2, "r");
4140 err = got_error_from_errno2("fdopen", path2);
4146 f2 = got_opentemp();
4148 err = got_error_from_errno2("got_opentemp", path2);
4151 n = fwrite(link_target, 1, link_len, f2);
4152 if (n != link_len) {
4153 err = got_ferror(f2, GOT_ERR_IO);
4156 if (fflush(f2) == EOF) {
4157 err = got_error_from_errno("fflush");
4163 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4167 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4171 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4175 if (stat(path1, &sb1) == -1) {
4176 err = got_error_from_errno2("stat", path1);
4180 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4181 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4185 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4189 if (fseek(f1, 0L, SEEK_SET) == -1)
4190 return got_ferror(f1, GOT_ERR_IO);
4191 if (fseek(f2, 0L, SEEK_SET) == -1)
4192 return got_ferror(f2, GOT_ERR_IO);
4193 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4195 err = apply_or_reject_change(&choice, change, ++n,
4196 changes->nchanges, ds, args, diff_flags, relpath,
4197 f1, f2, &line_cur1, &line_cur2,
4198 reverse_patch ? NULL : outfile,
4199 reverse_patch ? outfile : NULL,
4200 patch_cb, patch_arg);
4203 if (choice == GOT_PATCH_CHOICE_YES)
4205 else if (choice == GOT_PATCH_CHOICE_QUIT)
4209 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4210 reverse_patch ? NULL : outfile,
4211 reverse_patch ? outfile : NULL);
4215 if (!S_ISLNK(sb2.st_mode)) {
4216 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4217 err = got_error_from_errno2("chmod", path2);
4225 got_object_blob_close(blob);
4226 if (f1 && fclose(f1) == EOF && err == NULL)
4227 err = got_error_from_errno2("fclose", path1);
4228 if (f2 && fclose(f2) == EOF && err == NULL)
4229 err = got_error_from_errno2("fclose", path2);
4230 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4231 err = got_error_from_errno2("close", path2);
4232 if (outfile && fclose(outfile) == EOF && err == NULL)
4233 err = got_error_from_errno2("fclose", *path_outfile);
4234 if (path1 && unlink(path1) == -1 && err == NULL)
4235 err = got_error_from_errno2("unlink", path1);
4236 if (err || !have_content) {
4237 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4238 err = got_error_from_errno2("unlink", *path_outfile);
4239 free(*path_outfile);
4240 *path_outfile = NULL;
4244 got_diff_state_free(ds);
4248 got_diff_free_changes(changes);
4253 static const struct got_error *
4254 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4255 const char *relpath, struct got_object_id *blob_id,
4256 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4257 int dirfd, const char *de_name)
4259 struct revert_file_args *a = arg;
4260 const struct got_error *err = NULL;
4261 char *parent_path = NULL;
4262 struct got_fileindex_entry *ie;
4263 struct got_tree_object *tree = NULL;
4264 struct got_object_id *tree_id = NULL;
4265 const struct got_tree_entry *te = NULL;
4266 char *tree_path = NULL, *te_name;
4267 char *ondisk_path = NULL, *path_content = NULL;
4268 struct got_blob_object *blob = NULL;
4270 /* Reverting a staged deletion is a no-op. */
4271 if (status == GOT_STATUS_DELETE &&
4272 staged_status != GOT_STATUS_NO_CHANGE)
4275 if (status == GOT_STATUS_UNVERSIONED)
4276 return (*a->progress_cb)(a->progress_arg,
4277 GOT_STATUS_UNVERSIONED, relpath);
4279 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4281 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4283 /* Construct in-repository path of tree which contains this blob. */
4284 err = got_path_dirname(&parent_path, ie->path);
4286 if (err->code != GOT_ERR_BAD_PATH)
4288 parent_path = strdup("/");
4289 if (parent_path == NULL) {
4290 err = got_error_from_errno("strdup");
4294 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4295 tree_path = strdup(parent_path);
4296 if (tree_path == NULL) {
4297 err = got_error_from_errno("strdup");
4301 if (got_path_is_root_dir(parent_path)) {
4302 tree_path = strdup(a->worktree->path_prefix);
4303 if (tree_path == NULL) {
4304 err = got_error_from_errno("strdup");
4308 if (asprintf(&tree_path, "%s/%s",
4309 a->worktree->path_prefix, parent_path) == -1) {
4310 err = got_error_from_errno("asprintf");
4316 err = got_object_id_by_path(&tree_id, a->repo,
4317 a->worktree->base_commit_id, tree_path);
4319 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4320 (status == GOT_STATUS_ADD ||
4321 staged_status == GOT_STATUS_ADD)))
4324 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4328 te_name = basename(ie->path);
4329 if (te_name == NULL) {
4330 err = got_error_from_errno2("basename", ie->path);
4334 te = got_object_tree_find_entry(tree, te_name);
4335 if (te == NULL && status != GOT_STATUS_ADD &&
4336 staged_status != GOT_STATUS_ADD) {
4337 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4343 case GOT_STATUS_ADD:
4345 int choice = GOT_PATCH_CHOICE_NONE;
4346 err = (*a->patch_cb)(&choice, a->patch_arg,
4347 status, ie->path, NULL, 1, 1);
4350 if (choice != GOT_PATCH_CHOICE_YES)
4353 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4357 got_fileindex_entry_remove(a->fileindex, ie);
4359 case GOT_STATUS_DELETE:
4361 int choice = GOT_PATCH_CHOICE_NONE;
4362 err = (*a->patch_cb)(&choice, a->patch_arg,
4363 status, ie->path, NULL, 1, 1);
4366 if (choice != GOT_PATCH_CHOICE_YES)
4370 case GOT_STATUS_MODIFY:
4371 case GOT_STATUS_MODE_CHANGE:
4372 case GOT_STATUS_CONFLICT:
4373 case GOT_STATUS_MISSING: {
4374 struct got_object_id id;
4375 if (staged_status == GOT_STATUS_ADD ||
4376 staged_status == GOT_STATUS_MODIFY) {
4377 memcpy(id.sha1, ie->staged_blob_sha1,
4378 SHA1_DIGEST_LENGTH);
4380 memcpy(id.sha1, ie->blob_sha1,
4381 SHA1_DIGEST_LENGTH);
4382 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4386 if (asprintf(&ondisk_path, "%s/%s",
4387 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4388 err = got_error_from_errno("asprintf");
4392 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4393 status == GOT_STATUS_CONFLICT)) {
4394 int is_bad_symlink = 0;
4395 err = create_patched_content(&path_content, 1, &id,
4396 ondisk_path, dirfd, de_name, ie->path, a->repo,
4397 a->patch_cb, a->patch_arg);
4398 if (err || path_content == NULL)
4400 if (te && S_ISLNK(te->mode)) {
4401 if (unlink(path_content) == -1) {
4402 err = got_error_from_errno2("unlink",
4406 err = install_symlink(&is_bad_symlink,
4407 a->worktree, ondisk_path, ie->path,
4408 blob, 0, 1, 0, a->repo,
4409 a->progress_cb, a->progress_arg);
4411 if (rename(path_content, ondisk_path) == -1) {
4412 err = got_error_from_errno3("rename",
4413 path_content, ondisk_path);
4418 int is_bad_symlink = 0;
4419 if (te && S_ISLNK(te->mode)) {
4420 err = install_symlink(&is_bad_symlink,
4421 a->worktree, ondisk_path, ie->path,
4422 blob, 0, 1, 0, a->repo,
4423 a->progress_cb, a->progress_arg);
4425 err = install_blob(a->worktree, ondisk_path,
4427 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4428 got_fileindex_perms_to_st(ie), blob,
4429 0, 1, 0, 0, a->repo,
4430 a->progress_cb, a->progress_arg);
4434 if (status == GOT_STATUS_DELETE ||
4435 status == GOT_STATUS_MODE_CHANGE) {
4436 err = got_fileindex_entry_update(ie,
4437 ondisk_path, blob->id.sha1,
4438 a->worktree->base_commit_id->sha1, 1);
4442 if (is_bad_symlink) {
4443 got_fileindex_entry_filetype_set(ie,
4444 GOT_FILEIDX_MODE_BAD_SYMLINK);
4458 got_object_blob_close(blob);
4460 got_object_tree_close(tree);
4465 const struct got_error *
4466 got_worktree_revert(struct got_worktree *worktree,
4467 struct got_pathlist_head *paths,
4468 got_worktree_checkout_cb progress_cb, void *progress_arg,
4469 got_worktree_patch_cb patch_cb, void *patch_arg,
4470 struct got_repository *repo)
4472 struct got_fileindex *fileindex = NULL;
4473 char *fileindex_path = NULL;
4474 const struct got_error *err = NULL, *unlockerr = NULL;
4475 const struct got_error *sync_err = NULL;
4476 struct got_pathlist_entry *pe;
4477 struct revert_file_args rfa;
4479 err = lock_worktree(worktree, LOCK_EX);
4483 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4487 rfa.worktree = worktree;
4488 rfa.fileindex = fileindex;
4489 rfa.progress_cb = progress_cb;
4490 rfa.progress_arg = progress_arg;
4491 rfa.patch_cb = patch_cb;
4492 rfa.patch_arg = patch_arg;
4494 TAILQ_FOREACH(pe, paths, entry) {
4495 err = worktree_status(worktree, pe->path, fileindex, repo,
4496 revert_file, &rfa, NULL, NULL, 0, 0);
4500 sync_err = sync_fileindex(fileindex, fileindex_path);
4501 if (sync_err && err == NULL)
4504 free(fileindex_path);
4506 got_fileindex_free(fileindex);
4507 unlockerr = lock_worktree(worktree, LOCK_SH);
4508 if (unlockerr && err == NULL)
4514 free_commitable(struct got_commitable *ct)
4517 free(ct->in_repo_path);
4518 free(ct->ondisk_path);
4520 free(ct->base_blob_id);
4521 free(ct->staged_blob_id);
4522 free(ct->base_commit_id);
4526 struct collect_commitables_arg {
4527 struct got_pathlist_head *commitable_paths;
4528 struct got_repository *repo;
4529 struct got_worktree *worktree;
4530 struct got_fileindex *fileindex;
4531 int have_staged_files;
4532 int allow_bad_symlinks;
4535 static const struct got_error *
4536 collect_commitables(void *arg, unsigned char status,
4537 unsigned char staged_status, const char *relpath,
4538 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4539 struct got_object_id *commit_id, int dirfd, const char *de_name)
4541 struct collect_commitables_arg *a = arg;
4542 const struct got_error *err = NULL;
4543 struct got_commitable *ct = NULL;
4544 struct got_pathlist_entry *new = NULL;
4545 char *parent_path = NULL, *path = NULL;
4548 if (a->have_staged_files) {
4549 if (staged_status != GOT_STATUS_MODIFY &&
4550 staged_status != GOT_STATUS_ADD &&
4551 staged_status != GOT_STATUS_DELETE)
4554 if (status == GOT_STATUS_CONFLICT)
4555 return got_error(GOT_ERR_COMMIT_CONFLICT);
4557 if (status != GOT_STATUS_MODIFY &&
4558 status != GOT_STATUS_MODE_CHANGE &&
4559 status != GOT_STATUS_ADD &&
4560 status != GOT_STATUS_DELETE)
4564 if (asprintf(&path, "/%s", relpath) == -1) {
4565 err = got_error_from_errno("asprintf");
4568 if (strcmp(path, "/") == 0) {
4569 parent_path = strdup("");
4570 if (parent_path == NULL)
4571 return got_error_from_errno("strdup");
4573 err = got_path_dirname(&parent_path, path);
4578 ct = calloc(1, sizeof(*ct));
4580 err = got_error_from_errno("calloc");
4584 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4586 err = got_error_from_errno("asprintf");
4590 if (staged_status == GOT_STATUS_ADD ||
4591 staged_status == GOT_STATUS_MODIFY) {
4592 struct got_fileindex_entry *ie;
4593 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4594 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4595 case GOT_FILEIDX_MODE_REGULAR_FILE:
4596 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4599 case GOT_FILEIDX_MODE_SYMLINK:
4603 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4606 ct->mode |= got_fileindex_entry_perms_get(ie);
4607 } else if (status != GOT_STATUS_DELETE &&
4608 staged_status != GOT_STATUS_DELETE) {
4610 if (fstatat(dirfd, de_name, &sb,
4611 AT_SYMLINK_NOFOLLOW) == -1) {
4612 err = got_error_from_errno2("fstatat",
4616 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4617 err = got_error_from_errno2("lstat", ct->ondisk_path);
4620 ct->mode = sb.st_mode;
4623 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4624 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4626 err = got_error_from_errno("asprintf");
4630 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4631 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4633 char target_path[PATH_MAX];
4635 target_len = readlink(ct->ondisk_path, target_path,
4636 sizeof(target_path));
4637 if (target_len == -1) {
4638 err = got_error_from_errno2("readlink",
4642 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4643 target_len, ct->ondisk_path, a->worktree->root_path);
4646 if (is_bad_symlink) {
4647 err = got_error_path(ct->ondisk_path,
4648 GOT_ERR_BAD_SYMLINK);
4654 ct->status = status;
4655 ct->staged_status = staged_status;
4656 ct->blob_id = NULL; /* will be filled in when blob gets created */
4657 if (ct->status != GOT_STATUS_ADD &&
4658 ct->staged_status != GOT_STATUS_ADD) {
4659 ct->base_blob_id = got_object_id_dup(blob_id);
4660 if (ct->base_blob_id == NULL) {
4661 err = got_error_from_errno("got_object_id_dup");
4664 ct->base_commit_id = got_object_id_dup(commit_id);
4665 if (ct->base_commit_id == NULL) {
4666 err = got_error_from_errno("got_object_id_dup");
4670 if (ct->staged_status == GOT_STATUS_ADD ||
4671 ct->staged_status == GOT_STATUS_MODIFY) {
4672 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4673 if (ct->staged_blob_id == NULL) {
4674 err = got_error_from_errno("got_object_id_dup");
4678 ct->path = strdup(path);
4679 if (ct->path == NULL) {
4680 err = got_error_from_errno("strdup");
4683 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4685 if (ct && (err || new == NULL))
4686 free_commitable(ct);
4692 static const struct got_error *write_tree(struct got_object_id **, int *,
4693 struct got_tree_object *, const char *, struct got_pathlist_head *,
4694 got_worktree_status_cb status_cb, void *status_arg,
4695 struct got_repository *);
4697 static const struct got_error *
4698 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4699 struct got_tree_entry *te, const char *parent_path,
4700 struct got_pathlist_head *commitable_paths,
4701 got_worktree_status_cb status_cb, void *status_arg,
4702 struct got_repository *repo)
4704 const struct got_error *err = NULL;
4705 struct got_tree_object *subtree;
4708 if (asprintf(&subpath, "%s%s%s", parent_path,
4709 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4710 return got_error_from_errno("asprintf");
4712 err = got_object_open_as_tree(&subtree, repo, &te->id);
4716 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4717 commitable_paths, status_cb, status_arg, repo);
4718 got_object_tree_close(subtree);
4723 static const struct got_error *
4724 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4726 const struct got_error *err = NULL;
4727 char *ct_parent_path = NULL;
4731 if (strchr(ct->in_repo_path, '/') == NULL) {
4732 *match = got_path_is_root_dir(path);
4736 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4739 *match = (strcmp(path, ct_parent_path) == 0);
4740 free(ct_parent_path);
4745 get_ct_file_mode(struct got_commitable *ct)
4747 if (S_ISLNK(ct->mode))
4750 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4753 static const struct got_error *
4754 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4755 struct got_tree_entry *te, struct got_commitable *ct)
4757 const struct got_error *err = NULL;
4761 err = got_object_tree_entry_dup(new_te, te);
4765 (*new_te)->mode = get_ct_file_mode(ct);
4767 if (ct->staged_status == GOT_STATUS_MODIFY)
4768 memcpy(&(*new_te)->id, ct->staged_blob_id,
4769 sizeof((*new_te)->id));
4771 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4773 if (err && *new_te) {
4780 static const struct got_error *
4781 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4782 struct got_commitable *ct)
4784 const struct got_error *err = NULL;
4789 *new_te = calloc(1, sizeof(**new_te));
4790 if (*new_te == NULL)
4791 return got_error_from_errno("calloc");
4793 ct_name = basename(ct->path);
4794 if (ct_name == NULL) {
4795 err = got_error_from_errno2("basename", ct->path);
4798 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4799 sizeof((*new_te)->name)) {
4800 err = got_error(GOT_ERR_NO_SPACE);
4804 (*new_te)->mode = get_ct_file_mode(ct);
4806 if (ct->staged_status == GOT_STATUS_ADD)
4807 memcpy(&(*new_te)->id, ct->staged_blob_id,
4808 sizeof((*new_te)->id));
4810 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4812 if (err && *new_te) {
4819 static const struct got_error *
4820 insert_tree_entry(struct got_tree_entry *new_te,
4821 struct got_pathlist_head *paths)
4823 const struct got_error *err = NULL;
4824 struct got_pathlist_entry *new_pe;
4826 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4830 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4834 static const struct got_error *
4835 report_ct_status(struct got_commitable *ct,
4836 got_worktree_status_cb status_cb, void *status_arg)
4838 const char *ct_path = ct->path;
4839 unsigned char status;
4841 while (ct_path[0] == '/')
4844 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4845 status = ct->staged_status;
4847 status = ct->status;
4849 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4850 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4853 static const struct got_error *
4854 match_modified_subtree(int *modified, struct got_tree_entry *te,
4855 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4857 const struct got_error *err = NULL;
4858 struct got_pathlist_entry *pe;
4863 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4864 got_path_is_root_dir(base_tree_path) ? "" : "/",
4866 return got_error_from_errno("asprintf");
4868 TAILQ_FOREACH(pe, commitable_paths, entry) {
4869 struct got_commitable *ct = pe->data;
4870 *modified = got_path_is_child(ct->in_repo_path, te_path,
4880 static const struct got_error *
4881 match_deleted_or_modified_ct(struct got_commitable **ctp,
4882 struct got_tree_entry *te, const char *base_tree_path,
4883 struct got_pathlist_head *commitable_paths)
4885 const struct got_error *err = NULL;
4886 struct got_pathlist_entry *pe;
4890 TAILQ_FOREACH(pe, commitable_paths, entry) {
4891 struct got_commitable *ct = pe->data;
4892 char *ct_name = NULL;
4895 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4896 if (ct->status != GOT_STATUS_MODIFY &&
4897 ct->status != GOT_STATUS_MODE_CHANGE &&
4898 ct->status != GOT_STATUS_DELETE)
4901 if (ct->staged_status != GOT_STATUS_MODIFY &&
4902 ct->staged_status != GOT_STATUS_DELETE)
4906 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4909 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4915 ct_name = basename(pe->path);
4916 if (ct_name == NULL)
4917 return got_error_from_errno2("basename", pe->path);
4919 if (strcmp(te->name, ct_name) != 0)
4929 static const struct got_error *
4930 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4931 const char *child_path, const char *path_base_tree,
4932 struct got_pathlist_head *commitable_paths,
4933 got_worktree_status_cb status_cb, void *status_arg,
4934 struct got_repository *repo)
4936 const struct got_error *err = NULL;
4937 struct got_tree_entry *new_te;
4939 struct got_object_id *id = NULL;
4944 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4945 got_path_is_root_dir(path_base_tree) ? "" : "/",
4947 return got_error_from_errno("asprintf");
4949 new_te = calloc(1, sizeof(*new_te));
4951 return got_error_from_errno("calloc");
4952 new_te->mode = S_IFDIR;
4954 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4955 sizeof(new_te->name)) {
4956 err = got_error(GOT_ERR_NO_SPACE);
4959 err = write_tree(&id, &nentries, NULL, subtree_path,
4960 commitable_paths, status_cb, status_arg, repo);
4965 memcpy(&new_te->id, id, sizeof(new_te->id));
4974 static const struct got_error *
4975 write_tree(struct got_object_id **new_tree_id, int *nentries,
4976 struct got_tree_object *base_tree, const char *path_base_tree,
4977 struct got_pathlist_head *commitable_paths,
4978 got_worktree_status_cb status_cb, void *status_arg,
4979 struct got_repository *repo)
4981 const struct got_error *err = NULL;
4982 struct got_pathlist_head paths;
4983 struct got_tree_entry *te, *new_te = NULL;
4984 struct got_pathlist_entry *pe;
4989 /* Insert, and recurse into, newly added entries first. */
4990 TAILQ_FOREACH(pe, commitable_paths, entry) {
4991 struct got_commitable *ct = pe->data;
4992 char *child_path = NULL, *slash;
4994 if ((ct->status != GOT_STATUS_ADD &&
4995 ct->staged_status != GOT_STATUS_ADD) ||
4996 (ct->flags & GOT_COMMITABLE_ADDED))
4999 if (!got_path_is_child(pe->path, path_base_tree,
5000 strlen(path_base_tree)))
5003 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5008 slash = strchr(child_path, '/');
5009 if (slash == NULL) {
5010 err = alloc_added_blob_tree_entry(&new_te, ct);
5013 err = report_ct_status(ct, status_cb, status_arg);
5016 ct->flags |= GOT_COMMITABLE_ADDED;
5017 err = insert_tree_entry(new_te, &paths);
5022 *slash = '\0'; /* trim trailing path components */
5023 if (base_tree == NULL ||
5024 got_object_tree_find_entry(base_tree, child_path)
5026 err = make_subtree_for_added_blob(&new_te,
5027 child_path, path_base_tree,
5028 commitable_paths, status_cb, status_arg,
5032 err = insert_tree_entry(new_te, &paths);
5041 int i, nbase_entries;
5042 /* Handle modified and deleted entries. */
5043 nbase_entries = got_object_tree_get_nentries(base_tree);
5044 for (i = 0; i < nbase_entries; i++) {
5045 struct got_commitable *ct = NULL;
5047 te = got_object_tree_get_entry(base_tree, i);
5048 if (got_object_tree_entry_is_submodule(te)) {
5049 /* Entry is a submodule; just copy it. */
5050 err = got_object_tree_entry_dup(&new_te, te);
5053 err = insert_tree_entry(new_te, &paths);
5060 if (S_ISDIR(te->mode)) {
5062 err = got_object_tree_entry_dup(&new_te, te);
5065 err = match_modified_subtree(&modified, te,
5066 path_base_tree, commitable_paths);
5069 /* Avoid recursion into unmodified subtrees. */
5071 struct got_object_id *new_id;
5073 err = write_subtree(&new_id,
5075 path_base_tree, commitable_paths,
5076 status_cb, status_arg, repo);
5079 if (nsubentries == 0) {
5080 /* All entries were deleted. */
5084 memcpy(&new_te->id, new_id,
5085 sizeof(new_te->id));
5088 err = insert_tree_entry(new_te, &paths);
5095 err = match_deleted_or_modified_ct(&ct, te,
5096 path_base_tree, commitable_paths);
5100 /* NB: Deleted entries get dropped here. */
5101 if (ct->status == GOT_STATUS_MODIFY ||
5102 ct->status == GOT_STATUS_MODE_CHANGE ||
5103 ct->staged_status == GOT_STATUS_MODIFY) {
5104 err = alloc_modified_blob_tree_entry(
5108 err = insert_tree_entry(new_te, &paths);
5113 err = report_ct_status(ct, status_cb,
5118 /* Entry is unchanged; just copy it. */
5119 err = got_object_tree_entry_dup(&new_te, te);
5122 err = insert_tree_entry(new_te, &paths);
5130 /* Write new list of entries; deleted entries have been dropped. */
5131 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5133 got_pathlist_free(&paths);
5137 static const struct got_error *
5138 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5139 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5140 int have_staged_files)
5142 const struct got_error *err = NULL;
5143 struct got_pathlist_entry *pe;
5145 TAILQ_FOREACH(pe, commitable_paths, entry) {
5146 struct got_fileindex_entry *ie;
5147 struct got_commitable *ct = pe->data;
5149 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5151 if (ct->status == GOT_STATUS_DELETE ||
5152 ct->staged_status == GOT_STATUS_DELETE) {
5153 got_fileindex_entry_remove(fileindex, ie);
5154 } else if (ct->staged_status == GOT_STATUS_ADD ||
5155 ct->staged_status == GOT_STATUS_MODIFY) {
5156 got_fileindex_entry_stage_set(ie,
5157 GOT_FILEIDX_STAGE_NONE);
5158 err = got_fileindex_entry_update(ie,
5159 ct->ondisk_path, ct->staged_blob_id->sha1,
5160 new_base_commit_id->sha1,
5161 !have_staged_files);
5163 err = got_fileindex_entry_update(ie,
5164 ct->ondisk_path, ct->blob_id->sha1,
5165 new_base_commit_id->sha1,
5166 !have_staged_files);
5168 err = got_fileindex_entry_alloc(&ie, pe->path);
5171 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5172 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5174 got_fileindex_entry_free(ie);
5177 err = got_fileindex_entry_add(fileindex, ie);
5179 got_fileindex_entry_free(ie);
5188 static const struct got_error *
5189 check_out_of_date(const char *in_repo_path, unsigned char status,
5190 unsigned char staged_status, struct got_object_id *base_blob_id,
5191 struct got_object_id *base_commit_id,
5192 struct got_object_id *head_commit_id, struct got_repository *repo,
5195 const struct got_error *err = NULL;
5196 struct got_object_id *id = NULL;
5198 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5199 /* Trivial case: base commit == head commit */
5200 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5203 * Ensure file content which local changes were based
5204 * on matches file content in the branch head.
5206 err = got_object_id_by_path(&id, repo, head_commit_id,
5209 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5210 err = got_error(ood_errcode);
5212 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5213 err = got_error(ood_errcode);
5215 /* Require that added files don't exist in the branch head. */
5216 err = got_object_id_by_path(&id, repo, head_commit_id,
5218 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5220 err = id ? got_error(ood_errcode) : NULL;
5227 const struct got_error *
5228 commit_worktree(struct got_object_id **new_commit_id,
5229 struct got_pathlist_head *commitable_paths,
5230 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5231 const char *author, const char *committer,
5232 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5233 got_worktree_status_cb status_cb, void *status_arg,
5234 struct got_repository *repo)
5236 const struct got_error *err = NULL, *unlockerr = NULL;
5237 struct got_pathlist_entry *pe;
5238 const char *head_ref_name = NULL;
5239 struct got_commit_object *head_commit = NULL;
5240 struct got_reference *head_ref2 = NULL;
5241 struct got_object_id *head_commit_id2 = NULL;
5242 struct got_tree_object *head_tree = NULL;
5243 struct got_object_id *new_tree_id = NULL;
5245 struct got_object_id_queue parent_ids;
5246 struct got_object_qid *pid = NULL;
5247 char *logmsg = NULL;
5249 *new_commit_id = NULL;
5251 SIMPLEQ_INIT(&parent_ids);
5253 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5257 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5261 if (commit_msg_cb != NULL) {
5262 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5267 if (logmsg == NULL || strlen(logmsg) == 0) {
5268 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5272 /* Create blobs from added and modified files and record their IDs. */
5273 TAILQ_FOREACH(pe, commitable_paths, entry) {
5274 struct got_commitable *ct = pe->data;
5277 /* Blobs for staged files already exist. */
5278 if (ct->staged_status == GOT_STATUS_ADD ||
5279 ct->staged_status == GOT_STATUS_MODIFY)
5282 if (ct->status != GOT_STATUS_ADD &&
5283 ct->status != GOT_STATUS_MODIFY &&
5284 ct->status != GOT_STATUS_MODE_CHANGE)
5287 if (asprintf(&ondisk_path, "%s/%s",
5288 worktree->root_path, pe->path) == -1) {
5289 err = got_error_from_errno("asprintf");
5292 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5298 /* Recursively write new tree objects. */
5299 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5300 commitable_paths, status_cb, status_arg, repo);
5304 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5307 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5308 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5309 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5310 got_object_qid_free(pid);
5316 /* Check if a concurrent commit to our branch has occurred. */
5317 head_ref_name = got_worktree_get_head_ref_name(worktree);
5318 if (head_ref_name == NULL) {
5319 err = got_error_from_errno("got_worktree_get_head_ref_name");
5322 /* Lock the reference here to prevent concurrent modification. */
5323 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5326 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5329 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5330 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5333 /* Update branch head in repository. */
5334 err = got_ref_change_ref(head_ref2, *new_commit_id);
5337 err = got_ref_write(head_ref2, repo);
5341 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5345 err = ref_base_commit(worktree, repo);
5350 got_object_tree_close(head_tree);
5352 got_object_commit_close(head_commit);
5353 free(head_commit_id2);
5355 unlockerr = got_ref_unlock(head_ref2);
5356 if (unlockerr && err == NULL)
5358 got_ref_close(head_ref2);
5363 static const struct got_error *
5364 check_path_is_commitable(const char *path,
5365 struct got_pathlist_head *commitable_paths)
5367 struct got_pathlist_entry *cpe = NULL;
5368 size_t path_len = strlen(path);
5370 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5371 struct got_commitable *ct = cpe->data;
5372 const char *ct_path = ct->path;
5374 while (ct_path[0] == '/')
5377 if (strcmp(path, ct_path) == 0 ||
5378 got_path_is_child(ct_path, path, path_len))
5383 return got_error_path(path, GOT_ERR_BAD_PATH);
5388 static const struct got_error *
5389 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5391 int *have_staged_files = arg;
5393 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5394 *have_staged_files = 1;
5395 return got_error(GOT_ERR_CANCELLED);
5401 static const struct got_error *
5402 check_non_staged_files(struct got_fileindex *fileindex,
5403 struct got_pathlist_head *paths)
5405 struct got_pathlist_entry *pe;
5406 struct got_fileindex_entry *ie;
5408 TAILQ_FOREACH(pe, paths, entry) {
5409 if (pe->path[0] == '\0')
5411 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5413 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5414 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5415 return got_error_path(pe->path,
5416 GOT_ERR_FILE_NOT_STAGED);
5422 const struct got_error *
5423 got_worktree_commit(struct got_object_id **new_commit_id,
5424 struct got_worktree *worktree, struct got_pathlist_head *paths,
5425 const char *author, const char *committer, int allow_bad_symlinks,
5426 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5427 got_worktree_status_cb status_cb, void *status_arg,
5428 struct got_repository *repo)
5430 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5431 struct got_fileindex *fileindex = NULL;
5432 char *fileindex_path = NULL;
5433 struct got_pathlist_head commitable_paths;
5434 struct collect_commitables_arg cc_arg;
5435 struct got_pathlist_entry *pe;
5436 struct got_reference *head_ref = NULL;
5437 struct got_object_id *head_commit_id = NULL;
5438 int have_staged_files = 0;
5440 *new_commit_id = NULL;
5442 TAILQ_INIT(&commitable_paths);
5444 err = lock_worktree(worktree, LOCK_EX);
5448 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5452 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5456 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5460 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5461 &have_staged_files);
5462 if (err && err->code != GOT_ERR_CANCELLED)
5464 if (have_staged_files) {
5465 err = check_non_staged_files(fileindex, paths);
5470 cc_arg.commitable_paths = &commitable_paths;
5471 cc_arg.worktree = worktree;
5472 cc_arg.fileindex = fileindex;
5474 cc_arg.have_staged_files = have_staged_files;
5475 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5476 TAILQ_FOREACH(pe, paths, entry) {
5477 err = worktree_status(worktree, pe->path, fileindex, repo,
5478 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5483 if (TAILQ_EMPTY(&commitable_paths)) {
5484 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5488 TAILQ_FOREACH(pe, paths, entry) {
5489 err = check_path_is_commitable(pe->path, &commitable_paths);
5494 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5495 struct got_commitable *ct = pe->data;
5496 const char *ct_path = ct->in_repo_path;
5498 while (ct_path[0] == '/')
5500 err = check_out_of_date(ct_path, ct->status,
5501 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5502 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5508 err = commit_worktree(new_commit_id, &commitable_paths,
5509 head_commit_id, worktree, author, committer,
5510 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5514 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5515 fileindex, have_staged_files);
5516 sync_err = sync_fileindex(fileindex, fileindex_path);
5517 if (sync_err && err == NULL)
5521 got_fileindex_free(fileindex);
5522 free(fileindex_path);
5523 unlockerr = lock_worktree(worktree, LOCK_SH);
5524 if (unlockerr && err == NULL)
5526 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5527 struct got_commitable *ct = pe->data;
5528 free_commitable(ct);
5530 got_pathlist_free(&commitable_paths);
5535 got_commitable_get_path(struct got_commitable *ct)
5541 got_commitable_get_status(struct got_commitable *ct)
5546 struct check_rebase_ok_arg {
5547 struct got_worktree *worktree;
5548 struct got_repository *repo;
5551 static const struct got_error *
5552 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5554 const struct got_error *err = NULL;
5555 struct check_rebase_ok_arg *a = arg;
5556 unsigned char status;
5560 /* Reject rebase of a work tree with mixed base commits. */
5561 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5562 SHA1_DIGEST_LENGTH))
5563 return got_error(GOT_ERR_MIXED_COMMITS);
5565 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5567 return got_error_from_errno("asprintf");
5569 /* Reject rebase of a work tree with modified or staged files. */
5570 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5575 if (status != GOT_STATUS_NO_CHANGE)
5576 return got_error(GOT_ERR_MODIFIED);
5577 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5578 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5583 const struct got_error *
5584 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5585 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5586 struct got_worktree *worktree, struct got_reference *branch,
5587 struct got_repository *repo)
5589 const struct got_error *err = NULL;
5590 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5591 char *branch_ref_name = NULL;
5592 char *fileindex_path = NULL;
5593 struct check_rebase_ok_arg ok_arg;
5594 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5595 struct got_object_id *wt_branch_tip = NULL;
5597 *new_base_branch_ref = NULL;
5601 err = lock_worktree(worktree, LOCK_EX);
5605 err = open_fileindex(fileindex, &fileindex_path, worktree);
5609 ok_arg.worktree = worktree;
5611 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5616 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5620 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5624 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5628 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5633 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5636 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5637 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5641 err = got_ref_alloc_symref(new_base_branch_ref,
5642 new_base_branch_ref_name, wt_branch);
5645 err = got_ref_write(*new_base_branch_ref, repo);
5649 /* TODO Lock original branch's ref while rebasing? */
5651 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5655 err = got_ref_write(branch_ref, repo);
5659 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5660 worktree->base_commit_id);
5663 err = got_ref_write(*tmp_branch, repo);
5667 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5671 free(fileindex_path);
5672 free(tmp_branch_name);
5673 free(new_base_branch_ref_name);
5674 free(branch_ref_name);
5676 got_ref_close(branch_ref);
5678 got_ref_close(wt_branch);
5679 free(wt_branch_tip);
5681 if (*new_base_branch_ref) {
5682 got_ref_close(*new_base_branch_ref);
5683 *new_base_branch_ref = NULL;
5686 got_ref_close(*tmp_branch);
5690 got_fileindex_free(*fileindex);
5693 lock_worktree(worktree, LOCK_SH);
5698 const struct got_error *
5699 got_worktree_rebase_continue(struct got_object_id **commit_id,
5700 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5701 struct got_reference **branch, struct got_fileindex **fileindex,
5702 struct got_worktree *worktree, struct got_repository *repo)
5704 const struct got_error *err;
5705 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5706 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5707 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5708 char *fileindex_path = NULL;
5709 int have_staged_files = 0;
5712 *new_base_branch = NULL;
5717 err = lock_worktree(worktree, LOCK_EX);
5721 err = open_fileindex(fileindex, &fileindex_path, worktree);
5725 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5726 &have_staged_files);
5727 if (err && err->code != GOT_ERR_CANCELLED)
5729 if (have_staged_files) {
5730 err = got_error(GOT_ERR_STAGED_PATHS);
5734 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5738 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5742 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5746 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5750 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5754 err = got_ref_open(branch, repo,
5755 got_ref_get_symref_target(branch_ref), 0);
5759 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5763 err = got_ref_resolve(commit_id, repo, commit_ref);
5767 err = got_ref_open(new_base_branch, repo,
5768 new_base_branch_ref_name, 0);
5772 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5776 free(commit_ref_name);
5777 free(branch_ref_name);
5778 free(fileindex_path);
5780 got_ref_close(commit_ref);
5782 got_ref_close(branch_ref);
5787 got_ref_close(*tmp_branch);
5790 if (*new_base_branch) {
5791 got_ref_close(*new_base_branch);
5792 *new_base_branch = NULL;
5795 got_ref_close(*branch);
5799 got_fileindex_free(*fileindex);
5802 lock_worktree(worktree, LOCK_SH);
5807 const struct got_error *
5808 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5810 const struct got_error *err;
5811 char *tmp_branch_name = NULL;
5813 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5817 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5818 free(tmp_branch_name);
5822 static const struct got_error *
5823 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5824 char **logmsg, void *arg)
5830 static const struct got_error *
5831 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5832 const char *path, struct got_object_id *blob_id,
5833 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5834 int dirfd, const char *de_name)
5839 struct collect_merged_paths_arg {
5840 got_worktree_checkout_cb progress_cb;
5842 struct got_pathlist_head *merged_paths;
5845 static const struct got_error *
5846 collect_merged_paths(void *arg, unsigned char status, const char *path)
5848 const struct got_error *err;
5849 struct collect_merged_paths_arg *a = arg;
5851 struct got_pathlist_entry *new;
5853 err = (*a->progress_cb)(a->progress_arg, status, path);
5857 if (status != GOT_STATUS_MERGE &&
5858 status != GOT_STATUS_ADD &&
5859 status != GOT_STATUS_DELETE &&
5860 status != GOT_STATUS_CONFLICT)
5865 return got_error_from_errno("strdup");
5867 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5868 if (err || new == NULL)
5874 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5876 struct got_pathlist_entry *pe;
5878 TAILQ_FOREACH(pe, merged_paths, entry)
5879 free((char *)pe->path);
5881 got_pathlist_free(merged_paths);
5884 static const struct got_error *
5885 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5886 int is_rebase, struct got_repository *repo)
5888 const struct got_error *err;
5889 struct got_reference *commit_ref = NULL;
5891 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5893 if (err->code != GOT_ERR_NOT_REF)
5895 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5898 err = got_ref_write(commit_ref, repo);
5901 } else if (is_rebase) {
5902 struct got_object_id *stored_id;
5905 err = got_ref_resolve(&stored_id, repo, commit_ref);
5908 cmp = got_object_id_cmp(commit_id, stored_id);
5911 err = got_error(GOT_ERR_REBASE_COMMITID);
5917 got_ref_close(commit_ref);
5921 static const struct got_error *
5922 rebase_merge_files(struct got_pathlist_head *merged_paths,
5923 const char *commit_ref_name, struct got_worktree *worktree,
5924 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5925 struct got_object_id *commit_id, struct got_repository *repo,
5926 got_worktree_checkout_cb progress_cb, void *progress_arg,
5927 got_cancel_cb cancel_cb, void *cancel_arg)
5929 const struct got_error *err;
5930 struct got_reference *commit_ref = NULL;
5931 struct collect_merged_paths_arg cmp_arg;
5932 char *fileindex_path;
5934 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5936 err = get_fileindex_path(&fileindex_path, worktree);
5940 cmp_arg.progress_cb = progress_cb;
5941 cmp_arg.progress_arg = progress_arg;
5942 cmp_arg.merged_paths = merged_paths;
5943 err = merge_files(worktree, fileindex, fileindex_path,
5944 parent_commit_id, commit_id, repo, collect_merged_paths,
5945 &cmp_arg, cancel_cb, cancel_arg);
5947 got_ref_close(commit_ref);
5951 const struct got_error *
5952 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5953 struct got_worktree *worktree, struct got_fileindex *fileindex,
5954 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5955 struct got_repository *repo,
5956 got_worktree_checkout_cb progress_cb, void *progress_arg,
5957 got_cancel_cb cancel_cb, void *cancel_arg)
5959 const struct got_error *err;
5960 char *commit_ref_name;
5962 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5966 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5970 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5971 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5972 progress_arg, cancel_cb, cancel_arg);
5974 free(commit_ref_name);
5978 const struct got_error *
5979 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5980 struct got_worktree *worktree, struct got_fileindex *fileindex,
5981 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5982 struct got_repository *repo,
5983 got_worktree_checkout_cb progress_cb, void *progress_arg,
5984 got_cancel_cb cancel_cb, void *cancel_arg)
5986 const struct got_error *err;
5987 char *commit_ref_name;
5989 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5993 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5997 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5998 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5999 progress_arg, cancel_cb, cancel_arg);
6001 free(commit_ref_name);
6005 static const struct got_error *
6006 rebase_commit(struct got_object_id **new_commit_id,
6007 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6008 struct got_worktree *worktree, struct got_fileindex *fileindex,
6009 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6010 const char *new_logmsg, struct got_repository *repo)
6012 const struct got_error *err, *sync_err;
6013 struct got_pathlist_head commitable_paths;
6014 struct collect_commitables_arg cc_arg;
6015 char *fileindex_path = NULL;
6016 struct got_reference *head_ref = NULL;
6017 struct got_object_id *head_commit_id = NULL;
6018 char *logmsg = NULL;
6020 TAILQ_INIT(&commitable_paths);
6021 *new_commit_id = NULL;
6023 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6025 err = get_fileindex_path(&fileindex_path, worktree);
6029 cc_arg.commitable_paths = &commitable_paths;
6030 cc_arg.worktree = worktree;
6032 cc_arg.have_staged_files = 0;
6034 * If possible get the status of individual files directly to
6035 * avoid crawling the entire work tree once per rebased commit.
6036 * TODO: Ideally, merged_paths would contain a list of commitables
6037 * we could use so we could skip worktree_status() entirely.
6040 struct got_pathlist_entry *pe;
6041 TAILQ_FOREACH(pe, merged_paths, entry) {
6042 err = worktree_status(worktree, pe->path, fileindex,
6043 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6049 err = worktree_status(worktree, "", fileindex, repo,
6050 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6055 if (TAILQ_EMPTY(&commitable_paths)) {
6056 /* No-op change; commit will be elided. */
6057 err = got_ref_delete(commit_ref, repo);
6060 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6064 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6068 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6073 logmsg = strdup(new_logmsg);
6074 if (logmsg == NULL) {
6075 err = got_error_from_errno("strdup");
6079 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6084 /* NB: commit_worktree will call free(logmsg) */
6085 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6086 worktree, got_object_commit_get_author(orig_commit),
6087 got_object_commit_get_committer(orig_commit),
6088 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6092 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6096 err = got_ref_delete(commit_ref, repo);
6100 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6102 sync_err = sync_fileindex(fileindex, fileindex_path);
6103 if (sync_err && err == NULL)
6106 free(fileindex_path);
6107 free(head_commit_id);
6109 got_ref_close(head_ref);
6111 free(*new_commit_id);
6112 *new_commit_id = NULL;
6117 const struct got_error *
6118 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6119 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6120 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6121 struct got_commit_object *orig_commit,
6122 struct got_object_id *orig_commit_id, struct got_repository *repo)
6124 const struct got_error *err;
6125 char *commit_ref_name;
6126 struct got_reference *commit_ref = NULL;
6127 struct got_object_id *commit_id = NULL;
6129 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6133 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6136 err = got_ref_resolve(&commit_id, repo, commit_ref);
6139 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6140 err = got_error(GOT_ERR_REBASE_COMMITID);
6144 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6145 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6148 got_ref_close(commit_ref);
6149 free(commit_ref_name);
6154 const struct got_error *
6155 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6156 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6157 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6158 struct got_commit_object *orig_commit,
6159 struct got_object_id *orig_commit_id, const char *new_logmsg,
6160 struct got_repository *repo)
6162 const struct got_error *err;
6163 char *commit_ref_name;
6164 struct got_reference *commit_ref = NULL;
6166 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6170 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6174 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6175 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6178 got_ref_close(commit_ref);
6179 free(commit_ref_name);
6183 const struct got_error *
6184 got_worktree_rebase_postpone(struct got_worktree *worktree,
6185 struct got_fileindex *fileindex)
6188 got_fileindex_free(fileindex);
6189 return lock_worktree(worktree, LOCK_SH);
6192 static const struct got_error *
6193 delete_ref(const char *name, struct got_repository *repo)
6195 const struct got_error *err;
6196 struct got_reference *ref;
6198 err = got_ref_open(&ref, repo, name, 0);
6200 if (err->code == GOT_ERR_NOT_REF)
6205 err = got_ref_delete(ref, repo);
6210 static const struct got_error *
6211 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6213 const struct got_error *err;
6214 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6215 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6217 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6220 err = delete_ref(tmp_branch_name, repo);
6224 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6227 err = delete_ref(new_base_branch_ref_name, repo);
6231 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6234 err = delete_ref(branch_ref_name, repo);
6238 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6241 err = delete_ref(commit_ref_name, repo);
6246 free(tmp_branch_name);
6247 free(new_base_branch_ref_name);
6248 free(branch_ref_name);
6249 free(commit_ref_name);
6253 const struct got_error *
6254 got_worktree_rebase_complete(struct got_worktree *worktree,
6255 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6256 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6257 struct got_repository *repo)
6259 const struct got_error *err, *unlockerr;
6260 struct got_object_id *new_head_commit_id = NULL;
6262 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6266 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6270 err = got_ref_write(rebased_branch, repo);
6274 err = got_worktree_set_head_ref(worktree, rebased_branch);
6278 err = delete_rebase_refs(worktree, repo);
6281 got_fileindex_free(fileindex);
6282 free(new_head_commit_id);
6283 unlockerr = lock_worktree(worktree, LOCK_SH);
6284 if (unlockerr && err == NULL)
6289 const struct got_error *
6290 got_worktree_rebase_abort(struct got_worktree *worktree,
6291 struct got_fileindex *fileindex, struct got_repository *repo,
6292 struct got_reference *new_base_branch,
6293 got_worktree_checkout_cb progress_cb, void *progress_arg)
6295 const struct got_error *err, *unlockerr, *sync_err;
6296 struct got_reference *resolved = NULL;
6297 struct got_object_id *commit_id = NULL;
6298 char *fileindex_path = NULL;
6299 struct revert_file_args rfa;
6300 struct got_object_id *tree_id = NULL;
6302 err = lock_worktree(worktree, LOCK_EX);
6306 err = got_ref_open(&resolved, repo,
6307 got_ref_get_symref_target(new_base_branch), 0);
6311 err = got_worktree_set_head_ref(worktree, resolved);
6316 * XXX commits to the base branch could have happened while
6317 * we were busy rebasing; should we store the original commit ID
6318 * when rebase begins and read it back here?
6320 err = got_ref_resolve(&commit_id, repo, resolved);
6324 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6328 err = got_object_id_by_path(&tree_id, repo,
6329 worktree->base_commit_id, worktree->path_prefix);
6333 err = delete_rebase_refs(worktree, repo);
6337 err = get_fileindex_path(&fileindex_path, worktree);
6341 rfa.worktree = worktree;
6342 rfa.fileindex = fileindex;
6343 rfa.progress_cb = progress_cb;
6344 rfa.progress_arg = progress_arg;
6345 rfa.patch_cb = NULL;
6346 rfa.patch_arg = NULL;
6348 err = worktree_status(worktree, "", fileindex, repo,
6349 revert_file, &rfa, NULL, NULL, 0, 0);
6353 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6354 repo, progress_cb, progress_arg, NULL, NULL);
6356 sync_err = sync_fileindex(fileindex, fileindex_path);
6357 if (sync_err && err == NULL)
6360 got_ref_close(resolved);
6364 got_fileindex_free(fileindex);
6365 free(fileindex_path);
6367 unlockerr = lock_worktree(worktree, LOCK_SH);
6368 if (unlockerr && err == NULL)
6373 const struct got_error *
6374 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6375 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6376 struct got_fileindex **fileindex, struct got_worktree *worktree,
6377 struct got_repository *repo)
6379 const struct got_error *err = NULL;
6380 char *tmp_branch_name = NULL;
6381 char *branch_ref_name = NULL;
6382 char *base_commit_ref_name = NULL;
6383 char *fileindex_path = NULL;
6384 struct check_rebase_ok_arg ok_arg;
6385 struct got_reference *wt_branch = NULL;
6386 struct got_reference *base_commit_ref = NULL;
6390 *base_commit_id = NULL;
6393 err = lock_worktree(worktree, LOCK_EX);
6397 err = open_fileindex(fileindex, &fileindex_path, worktree);
6401 ok_arg.worktree = worktree;
6403 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6408 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6412 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6416 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6421 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6426 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6430 err = got_ref_write(*branch_ref, repo);
6434 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6435 worktree->base_commit_id);
6438 err = got_ref_write(base_commit_ref, repo);
6441 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6442 if (*base_commit_id == NULL) {
6443 err = got_error_from_errno("got_object_id_dup");
6447 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6448 worktree->base_commit_id);
6451 err = got_ref_write(*tmp_branch, repo);
6455 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6459 free(fileindex_path);
6460 free(tmp_branch_name);
6461 free(branch_ref_name);
6462 free(base_commit_ref_name);
6464 got_ref_close(wt_branch);
6467 got_ref_close(*branch_ref);
6471 got_ref_close(*tmp_branch);
6474 free(*base_commit_id);
6476 got_fileindex_free(*fileindex);
6479 lock_worktree(worktree, LOCK_SH);
6484 const struct got_error *
6485 got_worktree_histedit_postpone(struct got_worktree *worktree,
6486 struct got_fileindex *fileindex)
6489 got_fileindex_free(fileindex);
6490 return lock_worktree(worktree, LOCK_SH);
6493 const struct got_error *
6494 got_worktree_histedit_in_progress(int *in_progress,
6495 struct got_worktree *worktree)
6497 const struct got_error *err;
6498 char *tmp_branch_name = NULL;
6500 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6504 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6505 free(tmp_branch_name);
6509 const struct got_error *
6510 got_worktree_histedit_continue(struct got_object_id **commit_id,
6511 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6512 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6513 struct got_worktree *worktree, struct got_repository *repo)
6515 const struct got_error *err;
6516 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6517 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6518 struct got_reference *commit_ref = NULL;
6519 struct got_reference *base_commit_ref = NULL;
6520 char *fileindex_path = NULL;
6521 int have_staged_files = 0;
6525 *base_commit_id = NULL;
6528 err = lock_worktree(worktree, LOCK_EX);
6532 err = open_fileindex(fileindex, &fileindex_path, worktree);
6536 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6537 &have_staged_files);
6538 if (err && err->code != GOT_ERR_CANCELLED)
6540 if (have_staged_files) {
6541 err = got_error(GOT_ERR_STAGED_PATHS);
6545 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6549 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6553 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6557 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6562 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6566 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6569 err = got_ref_resolve(commit_id, repo, commit_ref);
6573 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6576 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6580 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6584 free(commit_ref_name);
6585 free(branch_ref_name);
6586 free(fileindex_path);
6588 got_ref_close(commit_ref);
6589 if (base_commit_ref)
6590 got_ref_close(base_commit_ref);
6594 free(*base_commit_id);
6595 *base_commit_id = NULL;
6597 got_ref_close(*tmp_branch);
6601 got_fileindex_free(*fileindex);
6604 lock_worktree(worktree, LOCK_EX);
6609 static const struct got_error *
6610 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6612 const struct got_error *err;
6613 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6614 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6616 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6619 err = delete_ref(tmp_branch_name, repo);
6623 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6627 err = delete_ref(base_commit_ref_name, repo);
6631 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6634 err = delete_ref(branch_ref_name, repo);
6638 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6641 err = delete_ref(commit_ref_name, repo);
6645 free(tmp_branch_name);
6646 free(base_commit_ref_name);
6647 free(branch_ref_name);
6648 free(commit_ref_name);
6652 const struct got_error *
6653 got_worktree_histedit_abort(struct got_worktree *worktree,
6654 struct got_fileindex *fileindex, struct got_repository *repo,
6655 struct got_reference *branch, struct got_object_id *base_commit_id,
6656 got_worktree_checkout_cb progress_cb, void *progress_arg)
6658 const struct got_error *err, *unlockerr, *sync_err;
6659 struct got_reference *resolved = NULL;
6660 char *fileindex_path = NULL;
6661 struct got_object_id *tree_id = NULL;
6662 struct revert_file_args rfa;
6664 err = lock_worktree(worktree, LOCK_EX);
6668 err = got_ref_open(&resolved, repo,
6669 got_ref_get_symref_target(branch), 0);
6673 err = got_worktree_set_head_ref(worktree, resolved);
6677 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6681 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6682 worktree->path_prefix);
6686 err = delete_histedit_refs(worktree, repo);
6690 err = get_fileindex_path(&fileindex_path, worktree);
6694 rfa.worktree = worktree;
6695 rfa.fileindex = fileindex;
6696 rfa.progress_cb = progress_cb;
6697 rfa.progress_arg = progress_arg;
6698 rfa.patch_cb = NULL;
6699 rfa.patch_arg = NULL;
6701 err = worktree_status(worktree, "", fileindex, repo,
6702 revert_file, &rfa, NULL, NULL, 0, 0);
6706 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6707 repo, progress_cb, progress_arg, NULL, NULL);
6709 sync_err = sync_fileindex(fileindex, fileindex_path);
6710 if (sync_err && err == NULL)
6713 got_ref_close(resolved);
6715 free(fileindex_path);
6717 unlockerr = lock_worktree(worktree, LOCK_SH);
6718 if (unlockerr && err == NULL)
6723 const struct got_error *
6724 got_worktree_histedit_complete(struct got_worktree *worktree,
6725 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6726 struct got_reference *edited_branch, struct got_repository *repo)
6728 const struct got_error *err, *unlockerr;
6729 struct got_object_id *new_head_commit_id = NULL;
6730 struct got_reference *resolved = NULL;
6732 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6736 err = got_ref_open(&resolved, repo,
6737 got_ref_get_symref_target(edited_branch), 0);
6741 err = got_ref_change_ref(resolved, new_head_commit_id);
6745 err = got_ref_write(resolved, repo);
6749 err = got_worktree_set_head_ref(worktree, resolved);
6753 err = delete_histedit_refs(worktree, repo);
6756 got_fileindex_free(fileindex);
6757 free(new_head_commit_id);
6758 unlockerr = lock_worktree(worktree, LOCK_SH);
6759 if (unlockerr && err == NULL)
6764 const struct got_error *
6765 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6766 struct got_object_id *commit_id, struct got_repository *repo)
6768 const struct got_error *err;
6769 char *commit_ref_name;
6771 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6775 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6779 err = delete_ref(commit_ref_name, repo);
6781 free(commit_ref_name);
6785 const struct got_error *
6786 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6787 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6788 struct got_worktree *worktree, const char *refname,
6789 struct got_repository *repo)
6791 const struct got_error *err = NULL;
6792 char *fileindex_path = NULL;
6793 struct check_rebase_ok_arg ok_arg;
6797 *base_branch_ref = NULL;
6799 err = lock_worktree(worktree, LOCK_EX);
6803 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6804 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6805 "cannot integrate a branch into itself; "
6806 "update -b or different branch name required");
6810 err = open_fileindex(fileindex, &fileindex_path, worktree);
6814 /* Preconditions are the same as for rebase. */
6815 ok_arg.worktree = worktree;
6817 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6822 err = got_ref_open(branch_ref, repo, refname, 1);
6826 err = got_ref_open(base_branch_ref, repo,
6827 got_worktree_get_head_ref_name(worktree), 1);
6831 got_ref_close(*branch_ref);
6834 if (*base_branch_ref) {
6835 got_ref_close(*base_branch_ref);
6836 *base_branch_ref = NULL;
6839 got_fileindex_free(*fileindex);
6842 lock_worktree(worktree, LOCK_SH);
6847 const struct got_error *
6848 got_worktree_integrate_continue(struct got_worktree *worktree,
6849 struct got_fileindex *fileindex, struct got_repository *repo,
6850 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6851 got_worktree_checkout_cb progress_cb, void *progress_arg,
6852 got_cancel_cb cancel_cb, void *cancel_arg)
6854 const struct got_error *err = NULL, *sync_err, *unlockerr;
6855 char *fileindex_path = NULL;
6856 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6858 err = get_fileindex_path(&fileindex_path, worktree);
6862 err = got_ref_resolve(&commit_id, repo, branch_ref);
6866 err = got_object_id_by_path(&tree_id, repo, commit_id,
6867 worktree->path_prefix);
6871 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6875 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6876 progress_cb, progress_arg, cancel_cb, cancel_arg);
6880 err = got_ref_change_ref(base_branch_ref, commit_id);
6884 err = got_ref_write(base_branch_ref, repo);
6886 sync_err = sync_fileindex(fileindex, fileindex_path);
6887 if (sync_err && err == NULL)
6891 unlockerr = got_ref_unlock(branch_ref);
6892 if (unlockerr && err == NULL)
6894 got_ref_close(branch_ref);
6896 unlockerr = got_ref_unlock(base_branch_ref);
6897 if (unlockerr && err == NULL)
6899 got_ref_close(base_branch_ref);
6901 got_fileindex_free(fileindex);
6902 free(fileindex_path);
6905 unlockerr = lock_worktree(worktree, LOCK_SH);
6906 if (unlockerr && err == NULL)
6911 const struct got_error *
6912 got_worktree_integrate_abort(struct got_worktree *worktree,
6913 struct got_fileindex *fileindex, struct got_repository *repo,
6914 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6916 const struct got_error *err = NULL, *unlockerr = NULL;
6918 got_fileindex_free(fileindex);
6920 err = lock_worktree(worktree, LOCK_SH);
6922 unlockerr = got_ref_unlock(branch_ref);
6923 if (unlockerr && err == NULL)
6925 got_ref_close(branch_ref);
6927 unlockerr = got_ref_unlock(base_branch_ref);
6928 if (unlockerr && err == NULL)
6930 got_ref_close(base_branch_ref);
6935 struct check_stage_ok_arg {
6936 struct got_object_id *head_commit_id;
6937 struct got_worktree *worktree;
6938 struct got_fileindex *fileindex;
6939 struct got_repository *repo;
6943 const struct got_error *
6944 check_stage_ok(void *arg, unsigned char status,
6945 unsigned char staged_status, const char *relpath,
6946 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6947 struct got_object_id *commit_id, int dirfd, const char *de_name)
6949 struct check_stage_ok_arg *a = arg;
6950 const struct got_error *err = NULL;
6951 struct got_fileindex_entry *ie;
6952 struct got_object_id base_commit_id;
6953 struct got_object_id *base_commit_idp = NULL;
6954 char *in_repo_path = NULL, *p;
6956 if (status == GOT_STATUS_UNVERSIONED ||
6957 status == GOT_STATUS_NO_CHANGE)
6959 if (status == GOT_STATUS_NONEXISTENT)
6960 return got_error_set_errno(ENOENT, relpath);
6962 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6964 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6966 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6967 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6969 return got_error_from_errno("asprintf");
6971 if (got_fileindex_entry_has_commit(ie)) {
6972 memcpy(base_commit_id.sha1, ie->commit_sha1,
6973 SHA1_DIGEST_LENGTH);
6974 base_commit_idp = &base_commit_id;
6977 if (status == GOT_STATUS_CONFLICT) {
6978 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6980 } else if (status != GOT_STATUS_ADD &&
6981 status != GOT_STATUS_MODIFY &&
6982 status != GOT_STATUS_DELETE) {
6983 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6987 a->have_changes = 1;
6992 err = check_out_of_date(p, status, staged_status,
6993 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6994 GOT_ERR_STAGE_OUT_OF_DATE);
7000 struct stage_path_arg {
7001 struct got_worktree *worktree;
7002 struct got_fileindex *fileindex;
7003 struct got_repository *repo;
7004 got_worktree_status_cb status_cb;
7006 got_worktree_patch_cb patch_cb;
7008 int staged_something;
7009 int allow_bad_symlinks;
7012 static const struct got_error *
7013 stage_path(void *arg, unsigned char status,
7014 unsigned char staged_status, const char *relpath,
7015 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7016 struct got_object_id *commit_id, int dirfd, const char *de_name)
7018 struct stage_path_arg *a = arg;
7019 const struct got_error *err = NULL;
7020 struct got_fileindex_entry *ie;
7021 char *ondisk_path = NULL, *path_content = NULL;
7023 struct got_object_id *new_staged_blob_id = NULL;
7026 if (status == GOT_STATUS_UNVERSIONED)
7029 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7031 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7033 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7035 return got_error_from_errno("asprintf");
7038 case GOT_STATUS_ADD:
7039 case GOT_STATUS_MODIFY:
7040 /* XXX could sb.st_mode be passed in by our caller? */
7041 if (lstat(ondisk_path, &sb) == -1) {
7042 err = got_error_from_errno2("lstat", ondisk_path);
7046 if (status == GOT_STATUS_ADD) {
7047 int choice = GOT_PATCH_CHOICE_NONE;
7048 err = (*a->patch_cb)(&choice, a->patch_arg,
7049 status, ie->path, NULL, 1, 1);
7052 if (choice != GOT_PATCH_CHOICE_YES)
7055 err = create_patched_content(&path_content, 0,
7056 staged_blob_id ? staged_blob_id : blob_id,
7057 ondisk_path, dirfd, de_name, ie->path,
7058 a->repo, a->patch_cb, a->patch_arg);
7059 if (err || path_content == NULL)
7063 err = got_object_blob_create(&new_staged_blob_id,
7064 path_content ? path_content : ondisk_path, a->repo);
7067 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7068 SHA1_DIGEST_LENGTH);
7069 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7070 stage = GOT_FILEIDX_STAGE_ADD;
7072 stage = GOT_FILEIDX_STAGE_MODIFY;
7073 got_fileindex_entry_stage_set(ie, stage);
7074 if (S_ISLNK(sb.st_mode)) {
7075 int is_bad_symlink = 0;
7076 if (!a->allow_bad_symlinks) {
7077 char target_path[PATH_MAX];
7079 target_len = readlink(ondisk_path, target_path,
7080 sizeof(target_path));
7081 if (target_len == -1) {
7082 err = got_error_from_errno2("readlink",
7086 err = is_bad_symlink_target(&is_bad_symlink,
7087 target_path, target_len, ondisk_path,
7088 a->worktree->root_path);
7091 if (is_bad_symlink) {
7092 err = got_error_path(ondisk_path,
7093 GOT_ERR_BAD_SYMLINK);
7098 got_fileindex_entry_staged_filetype_set(ie,
7099 GOT_FILEIDX_MODE_BAD_SYMLINK);
7101 got_fileindex_entry_staged_filetype_set(ie,
7102 GOT_FILEIDX_MODE_SYMLINK);
7104 got_fileindex_entry_staged_filetype_set(ie,
7105 GOT_FILEIDX_MODE_REGULAR_FILE);
7107 a->staged_something = 1;
7108 if (a->status_cb == NULL)
7110 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7111 get_staged_status(ie), relpath, blob_id,
7112 new_staged_blob_id, NULL, dirfd, de_name);
7114 case GOT_STATUS_DELETE:
7115 if (staged_status == GOT_STATUS_DELETE)
7118 int choice = GOT_PATCH_CHOICE_NONE;
7119 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7120 ie->path, NULL, 1, 1);
7123 if (choice == GOT_PATCH_CHOICE_NO)
7125 if (choice != GOT_PATCH_CHOICE_YES) {
7126 err = got_error(GOT_ERR_PATCH_CHOICE);
7130 stage = GOT_FILEIDX_STAGE_DELETE;
7131 got_fileindex_entry_stage_set(ie, stage);
7132 a->staged_something = 1;
7133 if (a->status_cb == NULL)
7135 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7136 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7139 case GOT_STATUS_NO_CHANGE:
7141 case GOT_STATUS_CONFLICT:
7142 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7144 case GOT_STATUS_NONEXISTENT:
7145 err = got_error_set_errno(ENOENT, relpath);
7148 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7152 if (path_content && unlink(path_content) == -1 && err == NULL)
7153 err = got_error_from_errno2("unlink", path_content);
7156 free(new_staged_blob_id);
7160 const struct got_error *
7161 got_worktree_stage(struct got_worktree *worktree,
7162 struct got_pathlist_head *paths,
7163 got_worktree_status_cb status_cb, void *status_arg,
7164 got_worktree_patch_cb patch_cb, void *patch_arg,
7165 int allow_bad_symlinks, struct got_repository *repo)
7167 const struct got_error *err = NULL, *sync_err, *unlockerr;
7168 struct got_pathlist_entry *pe;
7169 struct got_fileindex *fileindex = NULL;
7170 char *fileindex_path = NULL;
7171 struct got_reference *head_ref = NULL;
7172 struct got_object_id *head_commit_id = NULL;
7173 struct check_stage_ok_arg oka;
7174 struct stage_path_arg spa;
7176 err = lock_worktree(worktree, LOCK_EX);
7180 err = got_ref_open(&head_ref, repo,
7181 got_worktree_get_head_ref_name(worktree), 0);
7184 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7187 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7191 /* Check pre-conditions before staging anything. */
7192 oka.head_commit_id = head_commit_id;
7193 oka.worktree = worktree;
7194 oka.fileindex = fileindex;
7196 oka.have_changes = 0;
7197 TAILQ_FOREACH(pe, paths, entry) {
7198 err = worktree_status(worktree, pe->path, fileindex, repo,
7199 check_stage_ok, &oka, NULL, NULL, 0, 0);
7203 if (!oka.have_changes) {
7204 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7208 spa.worktree = worktree;
7209 spa.fileindex = fileindex;
7211 spa.patch_cb = patch_cb;
7212 spa.patch_arg = patch_arg;
7213 spa.status_cb = status_cb;
7214 spa.status_arg = status_arg;
7215 spa.staged_something = 0;
7216 spa.allow_bad_symlinks = allow_bad_symlinks;
7217 TAILQ_FOREACH(pe, paths, entry) {
7218 err = worktree_status(worktree, pe->path, fileindex, repo,
7219 stage_path, &spa, NULL, NULL, 0, 0);
7223 if (!spa.staged_something) {
7224 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7228 sync_err = sync_fileindex(fileindex, fileindex_path);
7229 if (sync_err && err == NULL)
7233 got_ref_close(head_ref);
7234 free(head_commit_id);
7235 free(fileindex_path);
7237 got_fileindex_free(fileindex);
7238 unlockerr = lock_worktree(worktree, LOCK_SH);
7239 if (unlockerr && err == NULL)
7244 struct unstage_path_arg {
7245 struct got_worktree *worktree;
7246 struct got_fileindex *fileindex;
7247 struct got_repository *repo;
7248 got_worktree_checkout_cb progress_cb;
7250 got_worktree_patch_cb patch_cb;
7254 static const struct got_error *
7255 create_unstaged_content(char **path_unstaged_content,
7256 char **path_new_staged_content, struct got_object_id *blob_id,
7257 struct got_object_id *staged_blob_id, const char *relpath,
7258 struct got_repository *repo,
7259 got_worktree_patch_cb patch_cb, void *patch_arg)
7261 const struct got_error *err;
7262 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7263 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7264 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7265 struct stat sb1, sb2;
7266 struct got_diff_changes *changes = NULL;
7267 struct got_diff_state *ds = NULL;
7268 struct got_diff_args *args = NULL;
7269 struct got_diff_change *change;
7270 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7271 int have_content = 0, have_rejected_content = 0;
7273 *path_unstaged_content = NULL;
7274 *path_new_staged_content = NULL;
7276 err = got_object_id_str(&label1, blob_id);
7279 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7283 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7287 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7291 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7295 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7299 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7303 if (stat(path1, &sb1) == -1) {
7304 err = got_error_from_errno2("stat", path1);
7308 if (stat(path2, &sb2) == -1) {
7309 err = got_error_from_errno2("stat", path2);
7313 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7314 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7318 err = got_opentemp_named(path_unstaged_content, &outfile,
7319 "got-unstaged-content");
7322 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7323 "got-new-staged-content");
7327 if (fseek(f1, 0L, SEEK_SET) == -1) {
7328 err = got_ferror(f1, GOT_ERR_IO);
7331 if (fseek(f2, 0L, SEEK_SET) == -1) {
7332 err = got_ferror(f2, GOT_ERR_IO);
7335 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7337 err = apply_or_reject_change(&choice, change, ++n,
7338 changes->nchanges, ds, args, diff_flags, relpath,
7339 f1, f2, &line_cur1, &line_cur2,
7340 outfile, rejectfile, patch_cb, patch_arg);
7343 if (choice == GOT_PATCH_CHOICE_YES)
7346 have_rejected_content = 1;
7347 if (choice == GOT_PATCH_CHOICE_QUIT)
7350 if (have_content || have_rejected_content)
7351 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7352 outfile, rejectfile);
7356 got_object_blob_close(blob);
7358 got_object_blob_close(staged_blob);
7359 if (f1 && fclose(f1) == EOF && err == NULL)
7360 err = got_error_from_errno2("fclose", path1);
7361 if (f2 && fclose(f2) == EOF && err == NULL)
7362 err = got_error_from_errno2("fclose", path2);
7363 if (outfile && fclose(outfile) == EOF && err == NULL)
7364 err = got_error_from_errno2("fclose", *path_unstaged_content);
7365 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7366 err = got_error_from_errno2("fclose", *path_new_staged_content);
7367 if (path1 && unlink(path1) == -1 && err == NULL)
7368 err = got_error_from_errno2("unlink", path1);
7369 if (path2 && unlink(path2) == -1 && err == NULL)
7370 err = got_error_from_errno2("unlink", path2);
7371 if (err || !have_content) {
7372 if (*path_unstaged_content &&
7373 unlink(*path_unstaged_content) == -1 && err == NULL)
7374 err = got_error_from_errno2("unlink",
7375 *path_unstaged_content);
7376 free(*path_unstaged_content);
7377 *path_unstaged_content = NULL;
7379 if (err || !have_content || !have_rejected_content) {
7380 if (*path_new_staged_content &&
7381 unlink(*path_new_staged_content) == -1 && err == NULL)
7382 err = got_error_from_errno2("unlink",
7383 *path_new_staged_content);
7384 free(*path_new_staged_content);
7385 *path_new_staged_content = NULL;
7389 got_diff_state_free(ds);
7393 got_diff_free_changes(changes);
7399 static const struct got_error *
7400 unstage_hunks(struct got_object_id *staged_blob_id,
7401 struct got_blob_object *blob_base,
7402 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7403 const char *ondisk_path, const char *label_orig,
7404 struct got_worktree *worktree, struct got_repository *repo,
7405 got_worktree_patch_cb patch_cb, void *patch_arg,
7406 got_worktree_checkout_cb progress_cb, void *progress_arg)
7408 const struct got_error *err = NULL;
7409 char *path_unstaged_content = NULL;
7410 char *path_new_staged_content = NULL;
7411 struct got_object_id *new_staged_blob_id = NULL;
7415 err = create_unstaged_content(&path_unstaged_content,
7416 &path_new_staged_content, blob_id, staged_blob_id,
7417 ie->path, repo, patch_cb, patch_arg);
7421 if (path_unstaged_content == NULL)
7424 if (path_new_staged_content) {
7425 err = got_object_blob_create(&new_staged_blob_id,
7426 path_new_staged_content, repo);
7431 f = fopen(path_unstaged_content, "r");
7433 err = got_error_from_errno2("fopen",
7434 path_unstaged_content);
7437 if (fstat(fileno(f), &sb) == -1) {
7438 err = got_error_from_errno2("fstat", path_unstaged_content);
7441 if (got_fileindex_entry_staged_filetype_get(ie) ==
7442 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7443 char link_target[PATH_MAX];
7445 r = fread(link_target, 1, sizeof(link_target), f);
7446 if (r == 0 && ferror(f)) {
7447 err = got_error_from_errno("fread");
7450 if (r >= sizeof(link_target)) { /* should not happen */
7451 err = got_error(GOT_ERR_NO_SPACE);
7454 link_target[r] = '\0';
7455 err = merge_symlink(worktree, blob_base,
7456 ondisk_path, ie->path, label_orig, link_target,
7457 worktree->base_commit_id, repo, progress_cb,
7460 int local_changes_subsumed;
7461 err = merge_file(&local_changes_subsumed, worktree,
7462 blob_base, ondisk_path, ie->path,
7463 got_fileindex_perms_to_st(ie),
7464 path_unstaged_content, label_orig, "unstaged",
7465 repo, progress_cb, progress_arg);
7470 if (new_staged_blob_id) {
7471 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7472 SHA1_DIGEST_LENGTH);
7474 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7476 free(new_staged_blob_id);
7477 if (path_unstaged_content &&
7478 unlink(path_unstaged_content) == -1 && err == NULL)
7479 err = got_error_from_errno2("unlink", path_unstaged_content);
7480 if (path_new_staged_content &&
7481 unlink(path_new_staged_content) == -1 && err == NULL)
7482 err = got_error_from_errno2("unlink", path_new_staged_content);
7483 if (f && fclose(f) != 0 && err == NULL)
7484 err = got_error_from_errno2("fclose", path_unstaged_content);
7485 free(path_unstaged_content);
7486 free(path_new_staged_content);
7490 static const struct got_error *
7491 unstage_path(void *arg, unsigned char status,
7492 unsigned char staged_status, const char *relpath,
7493 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7494 struct got_object_id *commit_id, int dirfd, const char *de_name)
7496 const struct got_error *err = NULL;
7497 struct unstage_path_arg *a = arg;
7498 struct got_fileindex_entry *ie;
7499 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7500 char *ondisk_path = NULL;
7501 char *id_str = NULL, *label_orig = NULL;
7502 int local_changes_subsumed;
7505 if (staged_status != GOT_STATUS_ADD &&
7506 staged_status != GOT_STATUS_MODIFY &&
7507 staged_status != GOT_STATUS_DELETE)
7510 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7512 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7514 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7516 return got_error_from_errno("asprintf");
7518 err = got_object_id_str(&id_str,
7519 commit_id ? commit_id : a->worktree->base_commit_id);
7522 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7524 err = got_error_from_errno("asprintf");
7528 switch (staged_status) {
7529 case GOT_STATUS_MODIFY:
7530 err = got_object_open_as_blob(&blob_base, a->repo,
7535 case GOT_STATUS_ADD:
7537 if (staged_status == GOT_STATUS_ADD) {
7538 int choice = GOT_PATCH_CHOICE_NONE;
7539 err = (*a->patch_cb)(&choice, a->patch_arg,
7540 staged_status, ie->path, NULL, 1, 1);
7543 if (choice != GOT_PATCH_CHOICE_YES)
7546 err = unstage_hunks(staged_blob_id,
7547 blob_base, blob_id, ie, ondisk_path,
7548 label_orig, a->worktree, a->repo,
7549 a->patch_cb, a->patch_arg,
7550 a->progress_cb, a->progress_arg);
7551 break; /* Done with this file. */
7554 err = got_object_open_as_blob(&blob_staged, a->repo,
7555 staged_blob_id, 8192);
7558 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7559 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7560 case GOT_FILEIDX_MODE_REGULAR_FILE:
7561 err = merge_blob(&local_changes_subsumed, a->worktree,
7562 blob_base, ondisk_path, relpath,
7563 got_fileindex_perms_to_st(ie), label_orig,
7564 blob_staged, commit_id ? commit_id :
7565 a->worktree->base_commit_id, a->repo,
7566 a->progress_cb, a->progress_arg);
7568 case GOT_FILEIDX_MODE_SYMLINK:
7569 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7570 char *staged_target;
7571 err = got_object_blob_read_to_str(
7572 &staged_target, blob_staged);
7575 err = merge_symlink(a->worktree, blob_base,
7576 ondisk_path, relpath, label_orig,
7577 staged_target, commit_id ? commit_id :
7578 a->worktree->base_commit_id,
7579 a->repo, a->progress_cb, a->progress_arg);
7580 free(staged_target);
7582 err = merge_blob(&local_changes_subsumed,
7583 a->worktree, blob_base, ondisk_path,
7584 relpath, got_fileindex_perms_to_st(ie),
7585 label_orig, blob_staged,
7586 commit_id ? commit_id :
7587 a->worktree->base_commit_id, a->repo,
7588 a->progress_cb, a->progress_arg);
7592 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7596 got_fileindex_entry_stage_set(ie,
7597 GOT_FILEIDX_STAGE_NONE);
7599 case GOT_STATUS_DELETE:
7601 int choice = GOT_PATCH_CHOICE_NONE;
7602 err = (*a->patch_cb)(&choice, a->patch_arg,
7603 staged_status, ie->path, NULL, 1, 1);
7606 if (choice == GOT_PATCH_CHOICE_NO)
7608 if (choice != GOT_PATCH_CHOICE_YES) {
7609 err = got_error(GOT_ERR_PATCH_CHOICE);
7613 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7614 err = get_file_status(&status, &sb, ie, ondisk_path,
7615 dirfd, de_name, a->repo);
7618 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7624 got_object_blob_close(blob_base);
7626 got_object_blob_close(blob_staged);
7632 const struct got_error *
7633 got_worktree_unstage(struct got_worktree *worktree,
7634 struct got_pathlist_head *paths,
7635 got_worktree_checkout_cb progress_cb, void *progress_arg,
7636 got_worktree_patch_cb patch_cb, void *patch_arg,
7637 struct got_repository *repo)
7639 const struct got_error *err = NULL, *sync_err, *unlockerr;
7640 struct got_pathlist_entry *pe;
7641 struct got_fileindex *fileindex = NULL;
7642 char *fileindex_path = NULL;
7643 struct unstage_path_arg upa;
7645 err = lock_worktree(worktree, LOCK_EX);
7649 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7653 upa.worktree = worktree;
7654 upa.fileindex = fileindex;
7656 upa.progress_cb = progress_cb;
7657 upa.progress_arg = progress_arg;
7658 upa.patch_cb = patch_cb;
7659 upa.patch_arg = patch_arg;
7660 TAILQ_FOREACH(pe, paths, entry) {
7661 err = worktree_status(worktree, pe->path, fileindex, repo,
7662 unstage_path, &upa, NULL, NULL, 0, 0);
7667 sync_err = sync_fileindex(fileindex, fileindex_path);
7668 if (sync_err && err == NULL)
7671 free(fileindex_path);
7673 got_fileindex_free(fileindex);
7674 unlockerr = lock_worktree(worktree, LOCK_SH);
7675 if (unlockerr && err == NULL)