2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
69 const struct got_error *err = NULL;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
83 const struct got_error *err = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 err = got_error_from_errno2("open", path);
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
170 (*content)[sb.st_size - 1] = '\0';
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
192 return got_error_from_errno("got_ref_to_str");
194 refstr = strdup(got_ref_get_name(head_ref));
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
210 uint32_t uuid_status;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
223 err = got_ref_resolve(&commit_id, repo, head_ref);
226 err = got_object_get_type(&obj_type, repo, commit_id);
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
421 got_repo_close(repo);
424 free(base_commit_id_str);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
434 (*worktree)->lockfd = fd;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
450 path = dirname(path);
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
535 err = write_head_ref(path_got, head_ref);
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
570 err = got_object_open(&obj, repo, commit_id);
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
595 got_object_close(obj);
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
641 size_t flen1 = 0, flen2 = 0;
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
660 } else if (flen2 == 0) {
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
684 FILE *f1 = NULL, *f2 = NULL;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
700 if (size1 != size2) {
705 f1 = fopen(f1_path, "r");
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
711 err = got_error_from_errno2("fopen", f2_path);
715 err = check_file_contents_equal(same, f1, f2);
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
745 char *symlink_path = NULL;
746 FILE *symlinkf = NULL;
748 *local_changes_subsumed = 0;
750 parent = dirname(ondisk_path);
752 return got_error_from_errno2("dirname", ondisk_path);
754 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
755 return got_error_from_errno("asprintf");
757 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
762 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
763 err = got_error_from_errno("asprintf");
768 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
772 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
778 * If the file has no blob, this is an "add vs add" conflict,
779 * and we simply use an empty ancestor file to make both files
780 * appear in the merged result in their entirety.
785 * In order the run a 3-way merge with a symlink we copy the symlink's
786 * target path into a temporary file and use that file with diff3.
788 if (S_ISLNK(st_mode)) {
789 char target_path[PATH_MAX];
794 if (asprintf(&base_path, "%s/got-symlink-merge",
796 err = got_error_from_errno("asprintf");
800 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
803 target_len = readlink(ondisk_path, target_path,
804 sizeof(target_path));
805 if (target_len == -1) {
806 err = got_error_from_errno2("readlink", ondisk_path);
809 n = fwrite(target_path, 1, target_len, symlinkf);
810 if (n != target_len) {
811 err = got_ferror(symlinkf, GOT_ERR_IO);
814 if (fflush(symlinkf) == EOF) {
815 err = got_error_from_errno2("fflush", symlink_path);
820 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
821 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
822 label_deriv, label_orig, NULL);
826 err = (*progress_cb)(progress_arg,
827 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
831 if (fsync(merged_fd) != 0) {
832 err = got_error_from_errno("fsync");
836 /* Check if a clean merge has subsumed all local changes. */
837 if (overlapcnt == 0) {
838 err = check_files_equal(local_changes_subsumed, deriv_path,
844 if (fchmod(merged_fd, st_mode) != 0) {
845 err = got_error_from_errno2("fchmod", merged_path);
849 if (rename(merged_path, ondisk_path) != 0) {
850 err = got_error_from_errno3("rename", merged_path,
860 if (unlink(symlink_path) == -1 && err == NULL)
861 err = got_error_from_errno2("unlink", symlink_path);
863 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
864 err = got_error_from_errno2("fclose", symlink_path);
866 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
867 err = got_error_from_errno("close");
868 if (f_orig && fclose(f_orig) != 0 && err == NULL)
869 err = got_error_from_errno("fclose");
872 if (blob_orig_path) {
873 unlink(blob_orig_path);
874 free(blob_orig_path);
879 static const struct got_error *
880 update_symlink(const char *ondisk_path, const char *target_path,
883 /* This is not atomic but matches what 'ln -sf' does. */
884 if (unlink(ondisk_path) == -1)
885 return got_error_from_errno2("unlink", ondisk_path);
886 if (symlink(target_path, ondisk_path) == -1)
887 return got_error_from_errno3("symlink", target_path,
893 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
894 * in the work tree with a file that contains conflict markers and the
895 * conflicting target paths of the original version, a "derived version"
896 * of a symlink from an incoming change, and a local version of the symlink.
898 * The original versions's target path can be NULL if it is not available,
899 * such as if both derived versions added a new symlink at the same path.
901 * The incoming derived symlink target is NULL in case the incoming change
902 * has deleted this symlink.
904 static const struct got_error *
905 install_symlink_conflict(const char *deriv_target,
906 struct got_object_id *deriv_base_commit_id, const char *orig_target,
907 const char *label_orig, const char *local_target, const char *ondisk_path)
909 const struct got_error *err;
910 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
913 err = got_object_id_str(&id_str, deriv_base_commit_id);
915 return got_error_from_errno("asprintf");
917 if (asprintf(&label_deriv, "%s: commit %s",
918 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
919 err = got_error_from_errno("asprintf");
923 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
927 if (fprintf(f, "%s: Could not install symbolic link because of merge "
928 "conflict.\nln(1) may be used to fix the situation. If this is "
929 "intended to be a\nregular file instead then its expected "
930 "contents may be filled in.\nThe following conflicting symlink "
931 "target paths were found:\n"
932 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
933 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
934 deriv_target ? deriv_target : "(symlink was deleted)",
935 orig_target ? label_orig : "",
936 orig_target ? "\n" : "",
937 orig_target ? orig_target : "",
938 orig_target ? "\n" : "",
939 GOT_DIFF_CONFLICT_MARKER_SEP,
940 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
941 err = got_error_from_errno2("fprintf", path);
945 if (unlink(ondisk_path) == -1) {
946 err = got_error_from_errno2("unlink", ondisk_path);
949 if (rename(path, ondisk_path) == -1) {
950 err = got_error_from_errno3("rename", path, ondisk_path);
953 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
954 err = got_error_from_errno2("chmod", ondisk_path);
958 if (f != NULL && fclose(f) == EOF && err == NULL)
959 err = got_error_from_errno2("fclose", path);
966 /* forward declaration */
967 static const struct got_error *
968 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
969 const char *, const char *, uint16_t, const char *,
970 struct got_blob_object *, struct got_object_id *,
971 struct got_repository *, got_worktree_checkout_cb, void *);
974 * Merge a symlink into the work tree, where blob_orig acts as the common
975 * ancestor, blob_deriv acts as the first derived version, and the symlink
976 * on disk acts as the second derived version.
977 * Assume that contents of both blobs represent symlinks.
979 static const struct got_error *
980 merge_symlink(struct got_worktree *worktree,
981 struct got_blob_object *blob_orig, const char *ondisk_path,
982 const char *path, const char *label_orig,
983 struct got_blob_object *blob_deriv,
984 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
985 got_worktree_checkout_cb progress_cb, void *progress_arg)
987 const struct got_error *err = NULL;
988 char *ancestor_target = NULL, *deriv_target = NULL;
990 ssize_t ondisk_len, deriv_len;
991 char ondisk_target[PATH_MAX];
992 int have_local_change = 0;
993 int have_incoming_change = 0;
995 if (lstat(ondisk_path, &sb) == -1)
996 return got_error_from_errno2("lstat", ondisk_path);
998 ondisk_len = readlink(ondisk_path, ondisk_target,
999 sizeof(ondisk_target));
1000 if (ondisk_len == -1) {
1001 err = got_error_from_errno2("readlink",
1005 ondisk_target[ondisk_len] = '\0';
1008 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1013 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
1017 if (ancestor_target == NULL ||
1018 (ondisk_len != strlen(ancestor_target) ||
1019 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1020 have_local_change = 1;
1022 deriv_len = strlen(deriv_target);
1023 if (ancestor_target == NULL ||
1024 (deriv_len != strlen(ancestor_target) ||
1025 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1026 have_incoming_change = 1;
1028 if (!have_local_change && !have_incoming_change) {
1029 if (ancestor_target) {
1030 /* Both sides made the same change. */
1031 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1033 } else if (deriv_len == ondisk_len &&
1034 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1035 /* Both sides added the same symlink. */
1036 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1039 /* Both sides added symlinks which don't match. */
1040 err = install_symlink_conflict(deriv_target,
1041 deriv_base_commit_id, ancestor_target,
1042 label_orig, ondisk_target, ondisk_path);
1045 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1048 } else if (!have_local_change && have_incoming_change) {
1049 /* Apply the incoming change. */
1050 err = update_symlink(ondisk_path, deriv_target,
1051 strlen(deriv_target));
1054 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1055 } else if (have_local_change && have_incoming_change) {
1056 if (deriv_len == ondisk_len &&
1057 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1058 /* Both sides made the same change. */
1059 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1062 err = install_symlink_conflict(deriv_target,
1063 deriv_base_commit_id, ancestor_target, label_orig,
1064 ondisk_target, ondisk_path);
1067 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1073 free(ancestor_target);
1079 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1080 * blob_deriv acts as the first derived version, and the file on disk
1081 * acts as the second derived version.
1083 static const struct got_error *
1084 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1085 struct got_blob_object *blob_orig, const char *ondisk_path,
1086 const char *path, uint16_t st_mode, const char *label_orig,
1087 struct got_blob_object *blob_deriv,
1088 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1089 got_worktree_checkout_cb progress_cb, void *progress_arg)
1091 const struct got_error *err = NULL;
1092 FILE *f_deriv = NULL;
1093 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1094 char *label_deriv = NULL, *parent;
1096 *local_changes_subsumed = 0;
1098 parent = dirname(ondisk_path);
1100 return got_error_from_errno2("dirname", ondisk_path);
1103 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1104 err = got_error_from_errno("asprintf");
1109 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1112 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1117 err = got_object_id_str(&id_str, deriv_base_commit_id);
1120 if (asprintf(&label_deriv, "%s: commit %s",
1121 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1122 err = got_error_from_errno("asprintf");
1126 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1127 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1128 label_deriv, repo, progress_cb, progress_arg);
1130 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1131 err = got_error_from_errno("fclose");
1133 if (blob_deriv_path) {
1134 unlink(blob_deriv_path);
1135 free(blob_deriv_path);
1142 static const struct got_error *
1143 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1144 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1145 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1147 const struct got_error *err = NULL;
1148 struct got_fileindex_entry *new_ie;
1152 err = got_fileindex_entry_alloc(&new_ie, path);
1156 err = got_fileindex_entry_update(new_ie, ondisk_path,
1157 blob_id->sha1, base_commit_id->sha1, 1);
1161 err = got_fileindex_entry_add(fileindex, new_ie);
1164 got_fileindex_entry_free(new_ie);
1171 get_ondisk_perms(int executable, mode_t st_mode)
1173 mode_t xbits = S_IXUSR;
1176 /* Map read bits to execute bits. */
1177 if (st_mode & S_IRGRP)
1179 if (st_mode & S_IROTH)
1181 return st_mode | xbits;
1184 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1187 /* forward declaration */
1188 static const struct got_error *
1189 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1190 const char *path, mode_t te_mode, mode_t st_mode,
1191 struct got_blob_object *blob, int restoring_missing_file,
1192 int reverting_versioned_file, int installing_bad_symlink,
1193 int path_is_unversioned, struct got_repository *repo,
1194 got_worktree_checkout_cb progress_cb, void *progress_arg);
1197 * This function assumes that the provided symlink target points at a
1198 * safe location in the work tree!
1200 static const struct got_error *
1201 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1204 const struct got_error *err = NULL;
1206 char etarget[PATH_MAX];
1210 * "Bad" symlinks (those pointing outside the work tree or into the
1211 * .got directory) are installed in the work tree as a regular file
1212 * which contains the bad symlink target path.
1213 * The new symlink target has already been checked for safety by our
1214 * caller. If we can successfully open a regular file then we simply
1215 * replace this file with a symlink below.
1217 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1220 return got_error_from_errno2("open", ondisk_path);
1222 /* We are updating an existing on-disk symlink. */
1223 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1225 return got_error_from_errno2("readlink", ondisk_path);
1227 if (elen == target_len &&
1228 memcmp(etarget, target_path, target_len) == 0)
1229 return NULL; /* nothing to do */
1232 err = update_symlink(ondisk_path, target_path, target_len);
1233 if (fd != -1 && close(fd) == -1 && err == NULL)
1234 err = got_error_from_errno2("close", ondisk_path);
1238 static const struct got_error *
1239 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1240 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1242 const struct got_error *err = NULL;
1243 char canonpath[PATH_MAX];
1244 char *path_got = NULL;
1246 *is_bad_symlink = 0;
1248 if (target_len >= sizeof(canonpath)) {
1249 *is_bad_symlink = 1;
1254 * We do not use realpath(3) to resolve the symlink's target
1255 * path because we don't want to resolve symlinks recursively.
1256 * Instead we make the path absolute and then canonicalize it.
1257 * Relative symlink target lookup should begin at the directory
1258 * in which the blob object is being installed.
1260 if (!got_path_is_absolute(target_path)) {
1262 char *parent = dirname(ondisk_path);
1264 return got_error_from_errno2("dirname", ondisk_path);
1265 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1266 return got_error_from_errno("asprintf");
1267 if (strlen(abspath) >= sizeof(canonpath)) {
1268 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1272 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1277 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1282 /* Only allow symlinks pointing at paths within the work tree. */
1283 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1284 *is_bad_symlink = 1;
1288 /* Do not allow symlinks pointing into the .got directory. */
1289 if (asprintf(&path_got, "%s/%s", wtroot_path,
1290 GOT_WORKTREE_GOT_DIR) == -1)
1291 return got_error_from_errno("asprintf");
1292 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1293 *is_bad_symlink = 1;
1299 static const struct got_error *
1300 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1301 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1302 int restoring_missing_file, int reverting_versioned_file,
1303 int path_is_unversioned, struct got_repository *repo,
1304 got_worktree_checkout_cb progress_cb, void *progress_arg)
1306 const struct got_error *err = NULL;
1307 char target_path[PATH_MAX];
1308 size_t len, target_len = 0;
1309 char *path_got = NULL;
1310 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1311 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1313 *is_bad_symlink = 0;
1316 * Blob object content specifies the target path of the link.
1317 * If a symbolic link cannot be installed we instead create
1318 * a regular file which contains the link target path stored
1319 * in the blob object.
1322 err = got_object_blob_read_block(&len, blob);
1323 if (len + target_len >= sizeof(target_path)) {
1324 /* Path too long; install as a regular file. */
1325 *is_bad_symlink = 1;
1326 got_object_blob_rewind(blob);
1327 return install_blob(worktree, ondisk_path, path,
1328 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1329 restoring_missing_file, reverting_versioned_file,
1330 1, path_is_unversioned, repo, progress_cb,
1334 /* Skip blob object header first time around. */
1335 memcpy(target_path + target_len, buf + hdrlen,
1337 target_len += len - hdrlen;
1341 target_path[target_len] = '\0';
1343 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1344 ondisk_path, worktree->root_path);
1348 if (*is_bad_symlink) {
1349 /* install as a regular file */
1350 *is_bad_symlink = 1;
1351 got_object_blob_rewind(blob);
1352 err = install_blob(worktree, ondisk_path, path,
1353 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1354 restoring_missing_file, reverting_versioned_file, 1,
1355 path_is_unversioned, repo, progress_cb, progress_arg);
1359 if (symlink(target_path, ondisk_path) == -1) {
1360 if (errno == EEXIST) {
1361 if (path_is_unversioned) {
1362 err = (*progress_cb)(progress_arg,
1363 GOT_STATUS_UNVERSIONED, path);
1366 err = replace_existing_symlink(ondisk_path,
1367 target_path, target_len);
1371 err = (*progress_cb)(progress_arg,
1372 reverting_versioned_file ?
1373 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1376 goto done; /* Nothing else to do. */
1379 if (errno == ENOENT) {
1380 char *parent = dirname(ondisk_path);
1381 if (parent == NULL) {
1382 err = got_error_from_errno2("dirname",
1386 err = add_dir_on_disk(worktree, parent);
1390 * Retry, and fall through to error handling
1391 * below if this second attempt fails.
1393 if (symlink(target_path, ondisk_path) != -1) {
1394 err = NULL; /* success */
1399 /* Handle errors from first or second creation attempt. */
1400 if (errno == ENAMETOOLONG) {
1401 /* bad target path; install as a regular file */
1402 *is_bad_symlink = 1;
1403 got_object_blob_rewind(blob);
1404 err = install_blob(worktree, ondisk_path, path,
1405 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1406 restoring_missing_file, reverting_versioned_file, 1,
1407 path_is_unversioned, repo,
1408 progress_cb, progress_arg);
1409 } else if (errno == ENOTDIR) {
1410 err = got_error_path(ondisk_path,
1411 GOT_ERR_FILE_OBSTRUCTED);
1413 err = got_error_from_errno3("symlink",
1414 target_path, ondisk_path);
1416 } else if (progress_cb)
1417 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1418 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1424 static const struct got_error *
1425 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1426 const char *path, mode_t te_mode, mode_t st_mode,
1427 struct got_blob_object *blob, int restoring_missing_file,
1428 int reverting_versioned_file, int installing_bad_symlink,
1429 int path_is_unversioned, struct got_repository *repo,
1430 got_worktree_checkout_cb progress_cb, void *progress_arg)
1432 const struct got_error *err = NULL;
1436 char *tmppath = NULL;
1438 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1439 GOT_DEFAULT_FILE_MODE);
1441 if (errno == ENOENT) {
1442 char *parent = dirname(path);
1444 return got_error_from_errno2("dirname", path);
1445 err = add_dir_on_disk(worktree, parent);
1448 fd = open(ondisk_path,
1449 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1450 GOT_DEFAULT_FILE_MODE);
1452 return got_error_from_errno2("open",
1454 } else if (errno == EEXIST) {
1455 if (path_is_unversioned) {
1456 err = (*progress_cb)(progress_arg,
1457 GOT_STATUS_UNVERSIONED, path);
1460 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1461 /* TODO file is obstructed; do something */
1462 err = got_error_path(ondisk_path,
1463 GOT_ERR_FILE_OBSTRUCTED);
1466 err = got_opentemp_named_fd(&tmppath, &fd,
1473 return got_error_from_errno2("open", ondisk_path);
1477 if (restoring_missing_file)
1478 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1480 else if (reverting_versioned_file)
1481 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1484 err = (*progress_cb)(progress_arg,
1485 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1490 hdrlen = got_object_blob_get_hdrlen(blob);
1492 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1493 err = got_object_blob_read_block(&len, blob);
1497 /* Skip blob object header first time around. */
1498 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1500 err = got_error_from_errno("write");
1502 } else if (outlen != len - hdrlen) {
1503 err = got_error(GOT_ERR_IO);
1510 if (fsync(fd) != 0) {
1511 err = got_error_from_errno("fsync");
1516 if (rename(tmppath, ondisk_path) != 0) {
1517 err = got_error_from_errno3("rename", tmppath,
1524 if (chmod(ondisk_path,
1525 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1526 err = got_error_from_errno2("chmod", ondisk_path);
1531 if (fd != -1 && close(fd) != 0 && err == NULL)
1532 err = got_error_from_errno("close");
1537 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1538 static const struct got_error *
1539 get_modified_file_content_status(unsigned char *status, FILE *f)
1541 const struct got_error *err = NULL;
1542 const char *markers[3] = {
1543 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1544 GOT_DIFF_CONFLICT_MARKER_SEP,
1545 GOT_DIFF_CONFLICT_MARKER_END
1550 const char delim[3] = {'\0', '\0', '\0'};
1552 while (*status == GOT_STATUS_MODIFY) {
1553 line = fparseln(f, &len, NULL, delim, 0);
1557 err = got_ferror(f, GOT_ERR_IO);
1561 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1562 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1564 *status = GOT_STATUS_CONFLICT;
1574 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1576 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1577 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1581 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1583 return !(ie->ctime_sec == sb->st_ctime &&
1584 ie->ctime_nsec == sb->st_ctimensec &&
1585 ie->mtime_sec == sb->st_mtime &&
1586 ie->mtime_nsec == sb->st_mtimensec &&
1587 ie->size == (sb->st_size & 0xffffffff) &&
1588 !xbit_differs(ie, sb->st_mode));
1591 static unsigned char
1592 get_staged_status(struct got_fileindex_entry *ie)
1594 switch (got_fileindex_entry_stage_get(ie)) {
1595 case GOT_FILEIDX_STAGE_ADD:
1596 return GOT_STATUS_ADD;
1597 case GOT_FILEIDX_STAGE_DELETE:
1598 return GOT_STATUS_DELETE;
1599 case GOT_FILEIDX_STAGE_MODIFY:
1600 return GOT_STATUS_MODIFY;
1602 return GOT_STATUS_NO_CHANGE;
1606 static const struct got_error *
1607 get_symlink_status(unsigned char *status, struct stat *sb,
1608 struct got_fileindex_entry *ie, const char *abspath,
1609 int dirfd, const char *de_name, struct got_blob_object *blob)
1611 const struct got_error *err = NULL;
1612 char target_path[PATH_MAX];
1613 char etarget[PATH_MAX];
1615 size_t len, target_len = 0;
1616 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1617 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1619 *status = GOT_STATUS_NO_CHANGE;
1621 /* Blob object content specifies the target path of the link. */
1623 err = got_object_blob_read_block(&len, blob);
1626 if (len + target_len >= sizeof(target_path)) {
1628 * Should not happen. The blob contents were OK
1629 * when this symlink was installed.
1631 return got_error(GOT_ERR_NO_SPACE);
1634 /* Skip blob object header first time around. */
1635 memcpy(target_path + target_len, buf + hdrlen,
1637 target_len += len - hdrlen;
1641 target_path[target_len] = '\0';
1644 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1646 return got_error_from_errno2("readlinkat", abspath);
1648 elen = readlink(abspath, etarget, sizeof(etarget));
1650 return got_error_from_errno2("readlink", abspath);
1653 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1654 *status = GOT_STATUS_MODIFY;
1659 static const struct got_error *
1660 get_file_status(unsigned char *status, struct stat *sb,
1661 struct got_fileindex_entry *ie, const char *abspath,
1662 int dirfd, const char *de_name, struct got_repository *repo)
1664 const struct got_error *err = NULL;
1665 struct got_object_id id;
1670 struct got_blob_object *blob = NULL;
1672 unsigned char staged_status = get_staged_status(ie);
1674 *status = GOT_STATUS_NO_CHANGE;
1677 * Whenever the caller provides a directory descriptor and a
1678 * directory entry name for the file, use them! This prevents
1679 * race conditions if filesystem paths change beneath our feet.
1682 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1683 if (errno == ENOENT) {
1684 if (got_fileindex_entry_has_file_on_disk(ie))
1685 *status = GOT_STATUS_MISSING;
1687 *status = GOT_STATUS_DELETE;
1690 err = got_error_from_errno2("fstatat", abspath);
1694 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1695 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1696 return got_error_from_errno2("open", abspath);
1697 else if (fd == -1 && errno == ELOOP) {
1698 if (lstat(abspath, sb) == -1)
1699 return got_error_from_errno2("lstat", abspath);
1700 } else if (fd == -1 || fstat(fd, sb) == -1) {
1701 if (errno == ENOENT) {
1702 if (got_fileindex_entry_has_file_on_disk(ie))
1703 *status = GOT_STATUS_MISSING;
1705 *status = GOT_STATUS_DELETE;
1708 err = got_error_from_errno2("fstat", abspath);
1713 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1714 *status = GOT_STATUS_OBSTRUCTED;
1718 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1719 *status = GOT_STATUS_DELETE;
1721 } else if (!got_fileindex_entry_has_blob(ie) &&
1722 staged_status != GOT_STATUS_ADD) {
1723 *status = GOT_STATUS_ADD;
1727 if (!stat_info_differs(ie, sb))
1730 if (staged_status == GOT_STATUS_MODIFY ||
1731 staged_status == GOT_STATUS_ADD)
1732 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1734 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1736 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1740 if (S_ISLNK(sb->st_mode)) {
1741 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1748 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1750 err = got_error_from_errno2("openat", abspath);
1755 f = fdopen(fd, "r");
1757 err = got_error_from_errno2("fdopen", abspath);
1761 hdrlen = got_object_blob_get_hdrlen(blob);
1763 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1764 err = got_object_blob_read_block(&blen, blob);
1767 /* Skip length of blob object header first time around. */
1768 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1769 if (flen == 0 && ferror(f)) {
1770 err = got_error_from_errno("fread");
1775 *status = GOT_STATUS_MODIFY;
1777 } else if (flen == 0) {
1779 *status = GOT_STATUS_MODIFY;
1781 } else if (blen - hdrlen == flen) {
1782 /* Skip blob object header first time around. */
1783 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1784 *status = GOT_STATUS_MODIFY;
1788 *status = GOT_STATUS_MODIFY;
1794 if (*status == GOT_STATUS_MODIFY) {
1796 err = get_modified_file_content_status(status, f);
1797 } else if (xbit_differs(ie, sb->st_mode))
1798 *status = GOT_STATUS_MODE_CHANGE;
1801 got_object_blob_close(blob);
1802 if (f != NULL && fclose(f) == EOF && err == NULL)
1803 err = got_error_from_errno2("fclose", abspath);
1804 if (fd != -1 && close(fd) == -1 && err == NULL)
1805 err = got_error_from_errno2("close", abspath);
1810 * Update timestamps in the file index if a file is unmodified and
1811 * we had to run a full content comparison to find out.
1813 static const struct got_error *
1814 sync_timestamps(char *ondisk_path, unsigned char status,
1815 struct got_fileindex_entry *ie, struct stat *sb)
1817 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1818 return got_fileindex_entry_update(ie, ondisk_path,
1819 ie->blob_sha1, ie->commit_sha1, 1);
1824 static const struct got_error *
1825 update_blob(struct got_worktree *worktree,
1826 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1827 struct got_tree_entry *te, const char *path,
1828 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1831 const struct got_error *err = NULL;
1832 struct got_blob_object *blob = NULL;
1834 unsigned char status = GOT_STATUS_NO_CHANGE;
1837 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1838 return got_error_from_errno("asprintf");
1841 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1842 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1845 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1849 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1850 sb.st_mode = got_fileindex_perms_to_st(ie);
1852 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1853 status = GOT_STATUS_UNVERSIONED;
1856 if (status == GOT_STATUS_OBSTRUCTED) {
1857 err = (*progress_cb)(progress_arg, status, path);
1860 if (status == GOT_STATUS_CONFLICT) {
1861 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1866 if (ie && status != GOT_STATUS_MISSING &&
1867 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1868 if (got_fileindex_entry_has_commit(ie) &&
1869 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1870 SHA1_DIGEST_LENGTH) == 0) {
1871 err = sync_timestamps(ondisk_path, status, ie, &sb);
1874 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1878 if (got_fileindex_entry_has_blob(ie) &&
1879 memcmp(ie->blob_sha1, te->id.sha1,
1880 SHA1_DIGEST_LENGTH) == 0) {
1881 err = sync_timestamps(ondisk_path, status, ie, &sb);
1886 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 int update_timestamps;
1892 struct got_blob_object *blob2 = NULL;
1893 char *label_orig = NULL;
1894 if (got_fileindex_entry_has_blob(ie)) {
1895 struct got_object_id id2;
1896 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 if (got_fileindex_entry_has_commit(ie)) {
1902 char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 sizeof(id_str)) == NULL) {
1905 err = got_error_path(id_str,
1906 GOT_ERR_BAD_OBJ_ID_STR);
1909 if (asprintf(&label_orig, "%s: commit %s",
1910 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 err = got_error_from_errno("asprintf");
1915 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1916 err = merge_symlink(worktree, blob2,
1917 ondisk_path, path, label_orig, blob,
1918 worktree->base_commit_id, repo,
1919 progress_cb, progress_arg);
1921 err = merge_blob(&update_timestamps, worktree, blob2,
1922 ondisk_path, path, sb.st_mode, label_orig, blob,
1923 worktree->base_commit_id, repo,
1924 progress_cb, progress_arg);
1928 got_object_blob_close(blob2);
1932 * Do not update timestamps of files with local changes.
1933 * Otherwise, a future status walk would treat them as
1934 * unmodified files again.
1936 err = got_fileindex_entry_update(ie, ondisk_path,
1937 blob->id.sha1, worktree->base_commit_id->sha1,
1939 } else if (status == GOT_STATUS_MODE_CHANGE) {
1940 err = got_fileindex_entry_update(ie, ondisk_path,
1941 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1942 } else if (status == GOT_STATUS_DELETE) {
1943 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1946 err = got_fileindex_entry_update(ie, ondisk_path,
1947 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1951 int is_bad_symlink = 0;
1952 if (S_ISLNK(te->mode)) {
1953 err = install_symlink(&is_bad_symlink, worktree,
1954 ondisk_path, path, blob,
1955 status == GOT_STATUS_MISSING, 0,
1956 status == GOT_STATUS_UNVERSIONED, repo,
1957 progress_cb, progress_arg);
1959 err = install_blob(worktree, ondisk_path, path,
1960 te->mode, sb.st_mode, blob,
1961 status == GOT_STATUS_MISSING, 0, 0,
1962 status == GOT_STATUS_UNVERSIONED, repo,
1963 progress_cb, progress_arg);
1969 err = got_fileindex_entry_update(ie, ondisk_path,
1970 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1972 err = create_fileindex_entry(&ie, fileindex,
1973 worktree->base_commit_id, ondisk_path, path,
1979 if (is_bad_symlink) {
1980 got_fileindex_entry_filetype_set(ie,
1981 GOT_FILEIDX_MODE_BAD_SYMLINK);
1984 got_object_blob_close(blob);
1990 static const struct got_error *
1991 remove_ondisk_file(const char *root_path, const char *path)
1993 const struct got_error *err = NULL;
1994 char *ondisk_path = NULL;
1996 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1997 return got_error_from_errno("asprintf");
1999 if (unlink(ondisk_path) == -1) {
2000 if (errno != ENOENT)
2001 err = got_error_from_errno2("unlink", ondisk_path);
2003 char *parent = dirname(ondisk_path);
2004 while (parent && strcmp(parent, root_path) != 0) {
2005 if (rmdir(parent) == -1) {
2006 if (errno != ENOTEMPTY)
2007 err = got_error_from_errno2("rmdir",
2011 parent = dirname(parent);
2018 static const struct got_error *
2019 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2020 struct got_fileindex_entry *ie, struct got_repository *repo,
2021 got_worktree_checkout_cb progress_cb, void *progress_arg)
2023 const struct got_error *err = NULL;
2024 unsigned char status;
2028 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2029 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2031 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2033 return got_error_from_errno("asprintf");
2035 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2039 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2040 char ondisk_target[PATH_MAX];
2041 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2042 sizeof(ondisk_target));
2043 if (ondisk_len == -1) {
2044 err = got_error_from_errno2("readlink", ondisk_path);
2047 ondisk_target[ondisk_len] = '\0';
2048 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2049 NULL, NULL, /* XXX pass common ancestor info? */
2050 ondisk_target, ondisk_path);
2053 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2058 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2059 status == GOT_STATUS_ADD) {
2060 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2064 * Preserve the working file and change the deleted blob's
2065 * entry into a schedule-add entry.
2067 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2070 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2073 if (status == GOT_STATUS_NO_CHANGE) {
2074 err = remove_ondisk_file(worktree->root_path, ie->path);
2078 got_fileindex_entry_remove(fileindex, ie);
2085 struct diff_cb_arg {
2086 struct got_fileindex *fileindex;
2087 struct got_worktree *worktree;
2088 struct got_repository *repo;
2089 got_worktree_checkout_cb progress_cb;
2091 got_cancel_cb cancel_cb;
2095 static const struct got_error *
2096 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2097 struct got_tree_entry *te, const char *parent_path)
2099 struct diff_cb_arg *a = arg;
2101 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2102 return got_error(GOT_ERR_CANCELLED);
2104 return update_blob(a->worktree, a->fileindex, ie, te,
2105 ie->path, a->repo, a->progress_cb, a->progress_arg);
2108 static const struct got_error *
2109 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2111 struct diff_cb_arg *a = arg;
2113 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2114 return got_error(GOT_ERR_CANCELLED);
2116 return delete_blob(a->worktree, a->fileindex, ie,
2117 a->repo, a->progress_cb, a->progress_arg);
2120 static const struct got_error *
2121 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2123 struct diff_cb_arg *a = arg;
2124 const struct got_error *err;
2127 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2128 return got_error(GOT_ERR_CANCELLED);
2130 if (got_object_tree_entry_is_submodule(te))
2133 if (asprintf(&path, "%s%s%s", parent_path,
2134 parent_path[0] ? "/" : "", te->name)
2136 return got_error_from_errno("asprintf");
2138 if (S_ISDIR(te->mode))
2139 err = add_dir_on_disk(a->worktree, path);
2141 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2142 a->repo, a->progress_cb, a->progress_arg);
2148 static const struct got_error *
2149 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2151 const struct got_error *err = NULL;
2152 char *uuidstr = NULL;
2153 uint32_t uuid_status;
2157 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2158 if (uuid_status != uuid_s_ok)
2159 return got_error_uuid(uuid_status, "uuid_to_string");
2161 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2163 err = got_error_from_errno("asprintf");
2170 const struct got_error *
2171 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2173 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2176 static const struct got_error *
2177 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2179 return get_ref_name(refname, worktree,
2180 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2183 static const struct got_error *
2184 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2186 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2189 static const struct got_error *
2190 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2192 return get_ref_name(refname, worktree,
2193 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2196 static const struct got_error *
2197 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2199 return get_ref_name(refname, worktree,
2200 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2203 static const struct got_error *
2204 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2206 return get_ref_name(refname, worktree,
2207 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2210 static const struct got_error *
2211 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2213 return get_ref_name(refname, worktree,
2214 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2217 static const struct got_error *
2218 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2220 return get_ref_name(refname, worktree,
2221 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2224 static const struct got_error *
2225 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2227 return get_ref_name(refname, worktree,
2228 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2231 const struct got_error *
2232 got_worktree_get_histedit_script_path(char **path,
2233 struct got_worktree *worktree)
2235 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2236 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2238 return got_error_from_errno("asprintf");
2244 * Prevent Git's garbage collector from deleting our base commit by
2245 * setting a reference to our base commit's ID.
2247 static const struct got_error *
2248 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2250 const struct got_error *err = NULL;
2251 struct got_reference *ref = NULL;
2254 err = got_worktree_get_base_ref_name(&refname, worktree);
2258 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2262 err = got_ref_write(ref, repo);
2270 static const struct got_error *
2271 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2273 const struct got_error *err = NULL;
2275 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2276 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2277 err = got_error_from_errno("asprintf");
2278 *fileindex_path = NULL;
2284 static const struct got_error *
2285 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2286 struct got_worktree *worktree)
2288 const struct got_error *err = NULL;
2291 *fileindex_path = NULL;
2292 *fileindex = got_fileindex_alloc();
2293 if (*fileindex == NULL)
2294 return got_error_from_errno("got_fileindex_alloc");
2296 err = get_fileindex_path(fileindex_path, worktree);
2300 index = fopen(*fileindex_path, "rb");
2301 if (index == NULL) {
2302 if (errno != ENOENT)
2303 err = got_error_from_errno2("fopen", *fileindex_path);
2305 err = got_fileindex_read(*fileindex, index);
2306 if (fclose(index) != 0 && err == NULL)
2307 err = got_error_from_errno("fclose");
2311 free(*fileindex_path);
2312 *fileindex_path = NULL;
2313 got_fileindex_free(*fileindex);
2319 struct bump_base_commit_id_arg {
2320 struct got_object_id *base_commit_id;
2323 const char *entry_name;
2324 got_worktree_checkout_cb progress_cb;
2328 /* Bump base commit ID of all files within an updated part of the work tree. */
2329 static const struct got_error *
2330 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2332 const struct got_error *err;
2333 struct bump_base_commit_id_arg *a = arg;
2335 if (a->entry_name) {
2336 if (strcmp(ie->path, a->path) != 0)
2338 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2341 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2342 SHA1_DIGEST_LENGTH) == 0)
2345 if (a->progress_cb) {
2346 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2351 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2355 static const struct got_error *
2356 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2358 const struct got_error *err = NULL;
2359 char *new_fileindex_path = NULL;
2360 FILE *new_index = NULL;
2361 struct timespec timeout;
2363 err = got_opentemp_named(&new_fileindex_path, &new_index,
2368 err = got_fileindex_write(fileindex, new_index);
2372 if (rename(new_fileindex_path, fileindex_path) != 0) {
2373 err = got_error_from_errno3("rename", new_fileindex_path,
2375 unlink(new_fileindex_path);
2379 * Sleep for a short amount of time to ensure that files modified after
2380 * this program exits have a different time stamp from the one which
2381 * was recorded in the file index.
2384 timeout.tv_nsec = 1;
2385 nanosleep(&timeout, NULL);
2389 free(new_fileindex_path);
2393 static const struct got_error *
2394 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2395 struct got_object_id **tree_id, const char *wt_relpath,
2396 struct got_worktree *worktree, struct got_repository *repo)
2398 const struct got_error *err = NULL;
2399 struct got_object_id *id = NULL;
2400 char *in_repo_path = NULL;
2401 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2403 *entry_type = GOT_OBJ_TYPE_ANY;
2404 *tree_relpath = NULL;
2407 if (wt_relpath[0] == '\0') {
2408 /* Check out all files within the work tree. */
2409 *entry_type = GOT_OBJ_TYPE_TREE;
2410 *tree_relpath = strdup("");
2411 if (*tree_relpath == NULL) {
2412 err = got_error_from_errno("strdup");
2415 err = got_object_id_by_path(tree_id, repo,
2416 worktree->base_commit_id, worktree->path_prefix);
2422 /* Check out a subset of files in the work tree. */
2424 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2425 is_root_wt ? "" : "/", wt_relpath) == -1) {
2426 err = got_error_from_errno("asprintf");
2430 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2436 in_repo_path = NULL;
2438 err = got_object_get_type(entry_type, repo, id);
2442 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2443 /* Check out a single file. */
2444 if (strchr(wt_relpath, '/') == NULL) {
2445 /* Check out a single file in work tree's root dir. */
2446 in_repo_path = strdup(worktree->path_prefix);
2447 if (in_repo_path == NULL) {
2448 err = got_error_from_errno("strdup");
2451 *tree_relpath = strdup("");
2452 if (*tree_relpath == NULL) {
2453 err = got_error_from_errno("strdup");
2457 /* Check out a single file in a subdirectory. */
2458 err = got_path_dirname(tree_relpath, wt_relpath);
2461 if (asprintf(&in_repo_path, "%s%s%s",
2462 worktree->path_prefix, is_root_wt ? "" : "/",
2463 *tree_relpath) == -1) {
2464 err = got_error_from_errno("asprintf");
2468 err = got_object_id_by_path(tree_id, repo,
2469 worktree->base_commit_id, in_repo_path);
2471 /* Check out all files within a subdirectory. */
2472 *tree_id = got_object_id_dup(id);
2473 if (*tree_id == NULL) {
2474 err = got_error_from_errno("got_object_id_dup");
2477 *tree_relpath = strdup(wt_relpath);
2478 if (*tree_relpath == NULL) {
2479 err = got_error_from_errno("strdup");
2487 *entry_type = GOT_OBJ_TYPE_ANY;
2488 free(*tree_relpath);
2489 *tree_relpath = NULL;
2496 static const struct got_error *
2497 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2498 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2499 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2500 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2502 const struct got_error *err = NULL;
2503 struct got_commit_object *commit = NULL;
2504 struct got_tree_object *tree = NULL;
2505 struct got_fileindex_diff_tree_cb diff_cb;
2506 struct diff_cb_arg arg;
2508 err = ref_base_commit(worktree, repo);
2510 if (!(err->code == GOT_ERR_ERRNO &&
2511 (errno == EACCES || errno == EROFS)))
2513 err = (*progress_cb)(progress_arg,
2514 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2519 err = got_object_open_as_commit(&commit, repo,
2520 worktree->base_commit_id);
2524 err = got_object_open_as_tree(&tree, repo, tree_id);
2529 got_object_tree_find_entry(tree, entry_name) == NULL) {
2530 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2534 diff_cb.diff_old_new = diff_old_new;
2535 diff_cb.diff_old = diff_old;
2536 diff_cb.diff_new = diff_new;
2537 arg.fileindex = fileindex;
2538 arg.worktree = worktree;
2540 arg.progress_cb = progress_cb;
2541 arg.progress_arg = progress_arg;
2542 arg.cancel_cb = cancel_cb;
2543 arg.cancel_arg = cancel_arg;
2544 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2545 entry_name, repo, &diff_cb, &arg);
2548 got_object_tree_close(tree);
2550 got_object_commit_close(commit);
2554 const struct got_error *
2555 got_worktree_checkout_files(struct got_worktree *worktree,
2556 struct got_pathlist_head *paths, struct got_repository *repo,
2557 got_worktree_checkout_cb progress_cb, void *progress_arg,
2558 got_cancel_cb cancel_cb, void *cancel_arg)
2560 const struct got_error *err = NULL, *sync_err, *unlockerr;
2561 struct got_commit_object *commit = NULL;
2562 struct got_tree_object *tree = NULL;
2563 struct got_fileindex *fileindex = NULL;
2564 char *fileindex_path = NULL;
2565 struct got_pathlist_entry *pe;
2566 struct tree_path_data {
2567 SIMPLEQ_ENTRY(tree_path_data) entry;
2568 struct got_object_id *tree_id;
2573 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2575 SIMPLEQ_INIT(&tree_paths);
2577 err = lock_worktree(worktree, LOCK_EX);
2581 /* Map all specified paths to in-repository trees. */
2582 TAILQ_FOREACH(pe, paths, entry) {
2583 tpd = malloc(sizeof(*tpd));
2585 err = got_error_from_errno("malloc");
2589 err = find_tree_entry_for_checkout(&tpd->entry_type,
2590 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2596 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2597 err = got_path_basename(&tpd->entry_name, pe->path);
2605 tpd->entry_name = NULL;
2607 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2611 * Read the file index.
2612 * Checking out files is supposed to be an idempotent operation.
2613 * If the on-disk file index is incomplete we will try to complete it.
2615 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2619 tpd = SIMPLEQ_FIRST(&tree_paths);
2620 TAILQ_FOREACH(pe, paths, entry) {
2621 struct bump_base_commit_id_arg bbc_arg;
2623 err = checkout_files(worktree, fileindex, tpd->relpath,
2624 tpd->tree_id, tpd->entry_name, repo,
2625 progress_cb, progress_arg, cancel_cb, cancel_arg);
2629 bbc_arg.base_commit_id = worktree->base_commit_id;
2630 bbc_arg.entry_name = tpd->entry_name;
2631 bbc_arg.path = pe->path;
2632 bbc_arg.path_len = pe->path_len;
2633 bbc_arg.progress_cb = progress_cb;
2634 bbc_arg.progress_arg = progress_arg;
2635 err = got_fileindex_for_each_entry_safe(fileindex,
2636 bump_base_commit_id, &bbc_arg);
2640 tpd = SIMPLEQ_NEXT(tpd, entry);
2642 sync_err = sync_fileindex(fileindex, fileindex_path);
2643 if (sync_err && err == NULL)
2646 free(fileindex_path);
2648 got_object_tree_close(tree);
2650 got_object_commit_close(commit);
2652 got_fileindex_free(fileindex);
2653 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2654 tpd = SIMPLEQ_FIRST(&tree_paths);
2655 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2660 unlockerr = lock_worktree(worktree, LOCK_SH);
2661 if (unlockerr && err == NULL)
2666 struct merge_file_cb_arg {
2667 struct got_worktree *worktree;
2668 struct got_fileindex *fileindex;
2669 got_worktree_checkout_cb progress_cb;
2671 got_cancel_cb cancel_cb;
2673 const char *label_orig;
2674 struct got_object_id *commit_id2;
2677 static const struct got_error *
2678 merge_file_cb(void *arg, struct got_blob_object *blob1,
2679 struct got_blob_object *blob2, struct got_object_id *id1,
2680 struct got_object_id *id2, const char *path1, const char *path2,
2681 mode_t mode1, mode_t mode2, struct got_repository *repo)
2683 static const struct got_error *err = NULL;
2684 struct merge_file_cb_arg *a = arg;
2685 struct got_fileindex_entry *ie;
2686 char *ondisk_path = NULL;
2688 unsigned char status;
2689 int local_changes_subsumed;
2691 if (blob1 && blob2) {
2692 ie = got_fileindex_entry_get(a->fileindex, path2,
2695 return (*a->progress_cb)(a->progress_arg,
2696 GOT_STATUS_MISSING, path2);
2698 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2700 return got_error_from_errno("asprintf");
2702 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2707 if (status == GOT_STATUS_DELETE) {
2708 err = (*a->progress_cb)(a->progress_arg,
2709 GOT_STATUS_MERGE, path2);
2712 if (status != GOT_STATUS_NO_CHANGE &&
2713 status != GOT_STATUS_MODIFY &&
2714 status != GOT_STATUS_CONFLICT &&
2715 status != GOT_STATUS_ADD) {
2716 err = (*a->progress_cb)(a->progress_arg, status, path2);
2720 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2721 err = merge_symlink(a->worktree, blob1, ondisk_path,
2722 path2, a->label_orig, blob2, a->commit_id2, repo,
2723 a->progress_cb, a->progress_arg);
2725 err = merge_blob(&local_changes_subsumed, a->worktree,
2726 blob1, ondisk_path, path2, sb.st_mode,
2727 a->label_orig, blob2, a->commit_id2, repo,
2728 a->progress_cb, a->progress_arg);
2731 ie = got_fileindex_entry_get(a->fileindex, path1,
2734 return (*a->progress_cb)(a->progress_arg,
2735 GOT_STATUS_MISSING, path1);
2737 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2739 return got_error_from_errno("asprintf");
2741 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2747 case GOT_STATUS_NO_CHANGE:
2748 err = (*a->progress_cb)(a->progress_arg,
2749 GOT_STATUS_DELETE, path1);
2752 err = remove_ondisk_file(a->worktree->root_path, path1);
2756 got_fileindex_entry_mark_deleted_from_disk(ie);
2758 case GOT_STATUS_DELETE:
2759 case GOT_STATUS_MISSING:
2760 err = (*a->progress_cb)(a->progress_arg,
2761 GOT_STATUS_DELETE, path1);
2765 got_fileindex_entry_mark_deleted_from_disk(ie);
2767 case GOT_STATUS_ADD:
2768 case GOT_STATUS_MODIFY:
2769 case GOT_STATUS_CONFLICT:
2770 err = (*a->progress_cb)(a->progress_arg,
2771 GOT_STATUS_CANNOT_DELETE, path1);
2775 case GOT_STATUS_OBSTRUCTED:
2776 err = (*a->progress_cb)(a->progress_arg, status, path1);
2784 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2786 return got_error_from_errno("asprintf");
2787 ie = got_fileindex_entry_get(a->fileindex, path2,
2790 err = get_file_status(&status, &sb, ie, ondisk_path,
2794 if (status != GOT_STATUS_NO_CHANGE &&
2795 status != GOT_STATUS_MODIFY &&
2796 status != GOT_STATUS_CONFLICT &&
2797 status != GOT_STATUS_ADD) {
2798 err = (*a->progress_cb)(a->progress_arg,
2802 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2803 err = merge_symlink(a->worktree, NULL,
2804 ondisk_path, path2, a->label_orig,
2805 blob2, a->commit_id2, repo,
2806 a->progress_cb, a->progress_arg);
2807 } else if (S_ISREG(sb.st_mode)) {
2808 err = merge_blob(&local_changes_subsumed,
2809 a->worktree, NULL, ondisk_path, path2,
2810 sb.st_mode, a->label_orig, blob2,
2811 a->commit_id2, repo, a->progress_cb,
2814 err = got_error_path(ondisk_path,
2815 GOT_ERR_FILE_OBSTRUCTED);
2819 if (status == GOT_STATUS_DELETE) {
2820 err = got_fileindex_entry_update(ie,
2821 ondisk_path, blob2->id.sha1,
2822 a->worktree->base_commit_id->sha1, 0);
2827 int is_bad_symlink = 0;
2828 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2829 if (S_ISLNK(mode2)) {
2830 err = install_symlink(&is_bad_symlink,
2831 a->worktree, ondisk_path, path2, blob2, 0,
2832 0, 1, repo, a->progress_cb, a->progress_arg);
2834 err = install_blob(a->worktree, ondisk_path, path2,
2835 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2836 a->progress_cb, a->progress_arg);
2840 err = got_fileindex_entry_alloc(&ie, path2);
2843 err = got_fileindex_entry_update(ie, ondisk_path,
2846 got_fileindex_entry_free(ie);
2849 err = got_fileindex_entry_add(a->fileindex, ie);
2851 got_fileindex_entry_free(ie);
2854 if (is_bad_symlink) {
2855 got_fileindex_entry_filetype_set(ie,
2856 GOT_FILEIDX_MODE_BAD_SYMLINK);
2865 struct check_merge_ok_arg {
2866 struct got_worktree *worktree;
2867 struct got_repository *repo;
2870 static const struct got_error *
2871 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2873 const struct got_error *err = NULL;
2874 struct check_merge_ok_arg *a = arg;
2875 unsigned char status;
2879 /* Reject merges into a work tree with mixed base commits. */
2880 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2881 SHA1_DIGEST_LENGTH))
2882 return got_error(GOT_ERR_MIXED_COMMITS);
2884 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2886 return got_error_from_errno("asprintf");
2888 /* Reject merges into a work tree with conflicted files. */
2889 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2892 if (status == GOT_STATUS_CONFLICT)
2893 return got_error(GOT_ERR_CONFLICTS);
2898 static const struct got_error *
2899 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2900 const char *fileindex_path, struct got_object_id *commit_id1,
2901 struct got_object_id *commit_id2, struct got_repository *repo,
2902 got_worktree_checkout_cb progress_cb, void *progress_arg,
2903 got_cancel_cb cancel_cb, void *cancel_arg)
2905 const struct got_error *err = NULL, *sync_err;
2906 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2907 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2908 struct merge_file_cb_arg arg;
2909 char *label_orig = NULL;
2914 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2915 worktree->path_prefix);
2919 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2923 err = got_object_id_str(&id_str, commit_id1);
2927 if (asprintf(&label_orig, "%s: commit %s",
2928 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2929 err = got_error_from_errno("asprintf");
2936 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2937 worktree->path_prefix);
2941 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2945 arg.worktree = worktree;
2946 arg.fileindex = fileindex;
2947 arg.progress_cb = progress_cb;
2948 arg.progress_arg = progress_arg;
2949 arg.cancel_cb = cancel_cb;
2950 arg.cancel_arg = cancel_arg;
2951 arg.label_orig = label_orig;
2952 arg.commit_id2 = commit_id2;
2953 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2954 sync_err = sync_fileindex(fileindex, fileindex_path);
2955 if (sync_err && err == NULL)
2959 got_object_tree_close(tree1);
2961 got_object_tree_close(tree2);
2966 const struct got_error *
2967 got_worktree_merge_files(struct got_worktree *worktree,
2968 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2969 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2970 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2972 const struct got_error *err, *unlockerr;
2973 char *fileindex_path = NULL;
2974 struct got_fileindex *fileindex = NULL;
2975 struct check_merge_ok_arg mok_arg;
2977 err = lock_worktree(worktree, LOCK_EX);
2981 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2985 mok_arg.worktree = worktree;
2986 mok_arg.repo = repo;
2987 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2992 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2993 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2996 got_fileindex_free(fileindex);
2997 free(fileindex_path);
2998 unlockerr = lock_worktree(worktree, LOCK_SH);
2999 if (unlockerr && err == NULL)
3004 struct diff_dir_cb_arg {
3005 struct got_fileindex *fileindex;
3006 struct got_worktree *worktree;
3007 const char *status_path;
3008 size_t status_path_len;
3009 struct got_repository *repo;
3010 got_worktree_status_cb status_cb;
3012 got_cancel_cb cancel_cb;
3014 /* A pathlist containing per-directory pathlists of ignore patterns. */
3015 struct got_pathlist_head ignores;
3016 int report_unchanged;
3020 static const struct got_error *
3021 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3022 int dirfd, const char *de_name,
3023 got_worktree_status_cb status_cb, void *status_arg,
3024 struct got_repository *repo, int report_unchanged)
3026 const struct got_error *err = NULL;
3027 unsigned char status = GOT_STATUS_NO_CHANGE;
3028 unsigned char staged_status = get_staged_status(ie);
3030 struct got_object_id blob_id, commit_id, staged_blob_id;
3031 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3032 struct got_object_id *staged_blob_idp = NULL;
3034 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3038 if (status == GOT_STATUS_NO_CHANGE &&
3039 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3042 if (got_fileindex_entry_has_blob(ie)) {
3043 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3044 blob_idp = &blob_id;
3046 if (got_fileindex_entry_has_commit(ie)) {
3047 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3048 commit_idp = &commit_id;
3050 if (staged_status == GOT_STATUS_ADD ||
3051 staged_status == GOT_STATUS_MODIFY) {
3052 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3053 SHA1_DIGEST_LENGTH);
3054 staged_blob_idp = &staged_blob_id;
3057 return (*status_cb)(status_arg, status, staged_status,
3058 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3061 static const struct got_error *
3062 status_old_new(void *arg, struct got_fileindex_entry *ie,
3063 struct dirent *de, const char *parent_path, int dirfd)
3065 const struct got_error *err = NULL;
3066 struct diff_dir_cb_arg *a = arg;
3069 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3070 return got_error(GOT_ERR_CANCELLED);
3072 if (got_path_cmp(parent_path, a->status_path,
3073 strlen(parent_path), a->status_path_len) != 0 &&
3074 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3077 if (parent_path[0]) {
3078 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3079 parent_path, de->d_name) == -1)
3080 return got_error_from_errno("asprintf");
3082 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3084 return got_error_from_errno("asprintf");
3087 err = report_file_status(ie, abspath, dirfd, de->d_name,
3088 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3093 static const struct got_error *
3094 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3096 struct diff_dir_cb_arg *a = arg;
3097 struct got_object_id blob_id, commit_id;
3098 unsigned char status;
3100 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3101 return got_error(GOT_ERR_CANCELLED);
3103 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3106 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3107 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3108 if (got_fileindex_entry_has_file_on_disk(ie))
3109 status = GOT_STATUS_MISSING;
3111 status = GOT_STATUS_DELETE;
3112 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3113 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3117 free_ignorelist(struct got_pathlist_head *ignorelist)
3119 struct got_pathlist_entry *pe;
3121 TAILQ_FOREACH(pe, ignorelist, entry)
3122 free((char *)pe->path);
3123 got_pathlist_free(ignorelist);
3127 free_ignores(struct got_pathlist_head *ignores)
3129 struct got_pathlist_entry *pe;
3131 TAILQ_FOREACH(pe, ignores, entry) {
3132 struct got_pathlist_head *ignorelist = pe->data;
3133 free_ignorelist(ignorelist);
3134 free((char *)pe->path);
3136 got_pathlist_free(ignores);
3139 static const struct got_error *
3140 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3142 const struct got_error *err = NULL;
3143 struct got_pathlist_entry *pe = NULL;
3144 struct got_pathlist_head *ignorelist;
3145 char *line = NULL, *pattern, *dirpath = NULL;
3146 size_t linesize = 0;
3149 ignorelist = calloc(1, sizeof(*ignorelist));
3150 if (ignorelist == NULL)
3151 return got_error_from_errno("calloc");
3152 TAILQ_INIT(ignorelist);
3154 while ((linelen = getline(&line, &linesize, f)) != -1) {
3155 if (linelen > 0 && line[linelen - 1] == '\n')
3156 line[linelen - 1] = '\0';
3158 /* Git's ignores may contain comments. */
3162 /* Git's negated patterns are not (yet?) supported. */
3166 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3168 err = got_error_from_errno("asprintf");
3171 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3176 err = got_error_from_errno("getline");
3180 dirpath = strdup(path);
3181 if (dirpath == NULL) {
3182 err = got_error_from_errno("strdup");
3185 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3188 if (err || pe == NULL) {
3190 free_ignorelist(ignorelist);
3196 match_ignores(struct got_pathlist_head *ignores, const char *path)
3198 struct got_pathlist_entry *pe;
3200 /* Handle patterns which match in all directories. */
3201 TAILQ_FOREACH(pe, ignores, entry) {
3202 struct got_pathlist_head *ignorelist = pe->data;
3203 struct got_pathlist_entry *pi;
3205 TAILQ_FOREACH(pi, ignorelist, entry) {
3206 const char *p, *pattern = pi->path;
3208 if (strncmp(pattern, "**/", 3) != 0)
3213 if (fnmatch(pattern, p,
3214 FNM_PATHNAME | FNM_LEADING_DIR)) {
3215 /* Retry in next directory. */
3216 while (*p && *p != '/')
3228 * The ignores pathlist contains ignore lists from children before
3229 * parents, so we can find the most specific ignorelist by walking
3230 * ignores backwards.
3232 pe = TAILQ_LAST(ignores, got_pathlist_head);
3234 if (got_path_is_child(path, pe->path, pe->path_len)) {
3235 struct got_pathlist_head *ignorelist = pe->data;
3236 struct got_pathlist_entry *pi;
3237 TAILQ_FOREACH(pi, ignorelist, entry) {
3238 const char *pattern = pi->path;
3239 int flags = FNM_LEADING_DIR;
3240 if (strstr(pattern, "/**/") == NULL)
3241 flags |= FNM_PATHNAME;
3242 if (fnmatch(pattern, path, flags))
3247 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3253 static const struct got_error *
3254 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3255 const char *path, int dirfd, const char *ignores_filename)
3257 const struct got_error *err = NULL;
3260 FILE *ignoresfile = NULL;
3262 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3263 path[0] ? "/" : "", ignores_filename) == -1)
3264 return got_error_from_errno("asprintf");
3267 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3269 if (errno != ENOENT && errno != EACCES)
3270 err = got_error_from_errno2("openat",
3273 ignoresfile = fdopen(fd, "r");
3274 if (ignoresfile == NULL)
3275 err = got_error_from_errno2("fdopen",
3279 err = read_ignores(ignores, path, ignoresfile);
3283 ignoresfile = fopen(ignorespath, "r");
3284 if (ignoresfile == NULL) {
3285 if (errno != ENOENT && errno != EACCES)
3286 err = got_error_from_errno2("fopen",
3289 err = read_ignores(ignores, path, ignoresfile);
3292 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3293 err = got_error_from_errno2("fclose", path);
3294 if (fd != -1 && close(fd) == -1 && err == NULL)
3295 err = got_error_from_errno2("close", path);
3300 static const struct got_error *
3301 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3303 const struct got_error *err = NULL;
3304 struct diff_dir_cb_arg *a = arg;
3307 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3308 return got_error(GOT_ERR_CANCELLED);
3310 if (parent_path[0]) {
3311 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3312 return got_error_from_errno("asprintf");
3317 if (de->d_type != DT_DIR &&
3318 got_path_is_child(path, a->status_path, a->status_path_len)
3319 && !match_ignores(&a->ignores, path))
3320 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3321 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3327 static const struct got_error *
3328 status_traverse(void *arg, const char *path, int dirfd)
3330 const struct got_error *err = NULL;
3331 struct diff_dir_cb_arg *a = arg;
3336 err = add_ignores(&a->ignores, a->worktree->root_path,
3337 path, dirfd, ".cvsignore");
3341 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3342 dirfd, ".gitignore");
3347 static const struct got_error *
3348 report_single_file_status(const char *path, const char *ondisk_path,
3349 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3350 void *status_arg, struct got_repository *repo, int report_unchanged)
3352 struct got_fileindex_entry *ie;
3355 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3357 return report_file_status(ie, ondisk_path, -1, NULL,
3358 status_cb, status_arg, repo, report_unchanged);
3360 if (lstat(ondisk_path, &sb) == -1) {
3361 if (errno != ENOENT)
3362 return got_error_from_errno2("lstat", ondisk_path);
3363 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3364 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3368 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3369 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3370 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3375 static const struct got_error *
3376 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3377 const char *root_path, const char *path)
3379 const struct got_error *err;
3380 char *parent_path, *next_parent_path = NULL;
3382 err = add_ignores(ignores, root_path, "", -1,
3387 err = add_ignores(ignores, root_path, "", -1,
3392 err = got_path_dirname(&parent_path, path);
3394 if (err->code == GOT_ERR_BAD_PATH)
3395 return NULL; /* cannot traverse parent */
3399 err = add_ignores(ignores, root_path, parent_path, -1,
3403 err = add_ignores(ignores, root_path, parent_path, -1,
3407 err = got_path_dirname(&next_parent_path, parent_path);
3409 if (err->code == GOT_ERR_BAD_PATH)
3410 err = NULL; /* traversed everything */
3414 parent_path = next_parent_path;
3415 next_parent_path = NULL;
3419 free(next_parent_path);
3423 static const struct got_error *
3424 worktree_status(struct got_worktree *worktree, const char *path,
3425 struct got_fileindex *fileindex, struct got_repository *repo,
3426 got_worktree_status_cb status_cb, void *status_arg,
3427 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3428 int report_unchanged)
3430 const struct got_error *err = NULL;
3432 struct got_fileindex_diff_dir_cb fdiff_cb;
3433 struct diff_dir_cb_arg arg;
3434 char *ondisk_path = NULL;
3436 TAILQ_INIT(&arg.ignores);
3438 if (asprintf(&ondisk_path, "%s%s%s",
3439 worktree->root_path, path[0] ? "/" : "", path) == -1)
3440 return got_error_from_errno("asprintf");
3442 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3444 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3446 err = got_error_from_errno2("open", ondisk_path);
3448 err = report_single_file_status(path, ondisk_path,
3449 fileindex, status_cb, status_arg, repo,
3452 fdiff_cb.diff_old_new = status_old_new;
3453 fdiff_cb.diff_old = status_old;
3454 fdiff_cb.diff_new = status_new;
3455 fdiff_cb.diff_traverse = status_traverse;
3456 arg.fileindex = fileindex;
3457 arg.worktree = worktree;
3458 arg.status_path = path;
3459 arg.status_path_len = strlen(path);
3461 arg.status_cb = status_cb;
3462 arg.status_arg = status_arg;
3463 arg.cancel_cb = cancel_cb;
3464 arg.cancel_arg = cancel_arg;
3465 arg.report_unchanged = report_unchanged;
3466 arg.no_ignores = no_ignores;
3468 err = add_ignores_from_parent_paths(&arg.ignores,
3469 worktree->root_path, path);
3473 err = got_fileindex_diff_dir(fileindex, fd,
3474 worktree->root_path, path, repo, &fdiff_cb, &arg);
3477 free_ignores(&arg.ignores);
3478 if (fd != -1 && close(fd) != 0 && err == NULL)
3479 err = got_error_from_errno("close");
3484 const struct got_error *
3485 got_worktree_status(struct got_worktree *worktree,
3486 struct got_pathlist_head *paths, struct got_repository *repo,
3487 got_worktree_status_cb status_cb, void *status_arg,
3488 got_cancel_cb cancel_cb, void *cancel_arg)
3490 const struct got_error *err = NULL;
3491 char *fileindex_path = NULL;
3492 struct got_fileindex *fileindex = NULL;
3493 struct got_pathlist_entry *pe;
3495 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3499 TAILQ_FOREACH(pe, paths, entry) {
3500 err = worktree_status(worktree, pe->path, fileindex, repo,
3501 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3505 free(fileindex_path);
3506 got_fileindex_free(fileindex);
3510 const struct got_error *
3511 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3514 const struct got_error *err = NULL;
3515 char *resolved = NULL, *cwd = NULL, *path = NULL;
3521 cwd = getcwd(NULL, 0);
3523 return got_error_from_errno("getcwd");
3525 if (lstat(arg, &sb) == -1) {
3526 if (errno != ENOENT) {
3527 err = got_error_from_errno2("lstat", arg);
3531 if (S_ISLNK(sb.st_mode)) {
3533 * We cannot use realpath(3) with symlinks since we want to
3534 * operate on the symlink itself.
3535 * But we can make the path absolute, assuming it is relative
3536 * to the current working directory, and then canonicalize it.
3538 char *abspath = NULL;
3539 char canonpath[PATH_MAX];
3540 if (!got_path_is_absolute(arg)) {
3541 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3542 err = got_error_from_errno("asprintf");
3547 err = got_canonpath(abspath ? abspath : arg, canonpath,
3551 resolved = strdup(canonpath);
3552 if (resolved == NULL) {
3553 err = got_error_from_errno("strdup");
3557 resolved = realpath(arg, NULL);
3558 if (resolved == NULL) {
3559 if (errno != ENOENT) {
3560 err = got_error_from_errno2("realpath", arg);
3563 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3564 err = got_error_from_errno("asprintf");
3570 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3571 strlen(got_worktree_get_root_path(worktree)))) {
3572 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3576 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3577 err = got_path_skip_common_ancestor(&path,
3578 got_worktree_get_root_path(worktree), resolved);
3584 err = got_error_from_errno("strdup");
3589 /* XXX status walk can't deal with trailing slash! */
3591 while (len > 0 && path[len - 1] == '/') {
3592 path[len - 1] = '\0';
3605 struct schedule_addition_args {
3606 struct got_worktree *worktree;
3607 struct got_fileindex *fileindex;
3608 got_worktree_checkout_cb progress_cb;
3610 struct got_repository *repo;
3613 static const struct got_error *
3614 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3615 const char *relpath, struct got_object_id *blob_id,
3616 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3617 int dirfd, const char *de_name)
3619 struct schedule_addition_args *a = arg;
3620 const struct got_error *err = NULL;
3621 struct got_fileindex_entry *ie;
3625 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3627 return got_error_from_errno("asprintf");
3629 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3631 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3635 /* Re-adding an existing entry is a no-op. */
3636 if (status == GOT_STATUS_ADD)
3638 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3643 if (status != GOT_STATUS_UNVERSIONED) {
3644 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3648 err = got_fileindex_entry_alloc(&ie, relpath);
3651 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3653 got_fileindex_entry_free(ie);
3656 err = got_fileindex_entry_add(a->fileindex, ie);
3658 got_fileindex_entry_free(ie);
3665 if (status == GOT_STATUS_ADD)
3667 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3670 const struct got_error *
3671 got_worktree_schedule_add(struct got_worktree *worktree,
3672 struct got_pathlist_head *paths,
3673 got_worktree_checkout_cb progress_cb, void *progress_arg,
3674 struct got_repository *repo, int no_ignores)
3676 struct got_fileindex *fileindex = NULL;
3677 char *fileindex_path = NULL;
3678 const struct got_error *err = NULL, *sync_err, *unlockerr;
3679 struct got_pathlist_entry *pe;
3680 struct schedule_addition_args saa;
3682 err = lock_worktree(worktree, LOCK_EX);
3686 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3690 saa.worktree = worktree;
3691 saa.fileindex = fileindex;
3692 saa.progress_cb = progress_cb;
3693 saa.progress_arg = progress_arg;
3696 TAILQ_FOREACH(pe, paths, entry) {
3697 err = worktree_status(worktree, pe->path, fileindex, repo,
3698 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3702 sync_err = sync_fileindex(fileindex, fileindex_path);
3703 if (sync_err && err == NULL)
3706 free(fileindex_path);
3708 got_fileindex_free(fileindex);
3709 unlockerr = lock_worktree(worktree, LOCK_SH);
3710 if (unlockerr && err == NULL)
3715 struct schedule_deletion_args {
3716 struct got_worktree *worktree;
3717 struct got_fileindex *fileindex;
3718 got_worktree_delete_cb progress_cb;
3720 struct got_repository *repo;
3721 int delete_local_mods;
3725 static const struct got_error *
3726 schedule_for_deletion(void *arg, unsigned char status,
3727 unsigned char staged_status, const char *relpath,
3728 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3729 struct got_object_id *commit_id, int dirfd, const char *de_name)
3731 struct schedule_deletion_args *a = arg;
3732 const struct got_error *err = NULL;
3733 struct got_fileindex_entry *ie = NULL;
3735 char *ondisk_path, *parent = NULL;
3737 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3739 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3741 staged_status = get_staged_status(ie);
3742 if (staged_status != GOT_STATUS_NO_CHANGE) {
3743 if (staged_status == GOT_STATUS_DELETE)
3745 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3748 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3750 return got_error_from_errno("asprintf");
3752 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3757 if (status != GOT_STATUS_NO_CHANGE) {
3758 if (status == GOT_STATUS_DELETE)
3760 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3761 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3764 if (status != GOT_STATUS_MODIFY &&
3765 status != GOT_STATUS_MISSING) {
3766 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3771 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3773 if (unlinkat(dirfd, de_name, 0) != 0) {
3774 err = got_error_from_errno2("unlinkat",
3778 } else if (unlink(ondisk_path) != 0) {
3779 err = got_error_from_errno2("unlink", ondisk_path);
3783 parent = dirname(ondisk_path);
3785 if (parent == NULL) {
3786 err = got_error_from_errno2("dirname", ondisk_path);
3789 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3790 if (rmdir(parent) == -1) {
3791 if (errno != ENOTEMPTY)
3792 err = got_error_from_errno2("rmdir",
3796 parent = dirname(parent);
3797 if (parent == NULL) {
3798 err = got_error_from_errno2("dirname", parent);
3804 got_fileindex_entry_mark_deleted_from_disk(ie);
3809 if (status == GOT_STATUS_DELETE)
3811 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3812 staged_status, relpath);
3815 const struct got_error *
3816 got_worktree_schedule_delete(struct got_worktree *worktree,
3817 struct got_pathlist_head *paths, int delete_local_mods,
3818 got_worktree_delete_cb progress_cb, void *progress_arg,
3819 struct got_repository *repo, int keep_on_disk)
3821 struct got_fileindex *fileindex = NULL;
3822 char *fileindex_path = NULL;
3823 const struct got_error *err = NULL, *sync_err, *unlockerr;
3824 struct got_pathlist_entry *pe;
3825 struct schedule_deletion_args sda;
3827 err = lock_worktree(worktree, LOCK_EX);
3831 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3835 sda.worktree = worktree;
3836 sda.fileindex = fileindex;
3837 sda.progress_cb = progress_cb;
3838 sda.progress_arg = progress_arg;
3840 sda.delete_local_mods = delete_local_mods;
3841 sda.keep_on_disk = keep_on_disk;
3843 TAILQ_FOREACH(pe, paths, entry) {
3844 err = worktree_status(worktree, pe->path, fileindex, repo,
3845 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3849 sync_err = sync_fileindex(fileindex, fileindex_path);
3850 if (sync_err && err == NULL)
3853 free(fileindex_path);
3855 got_fileindex_free(fileindex);
3856 unlockerr = lock_worktree(worktree, LOCK_SH);
3857 if (unlockerr && err == NULL)
3862 static const struct got_error *
3863 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3865 const struct got_error *err = NULL;
3867 size_t linesize = 0, n;
3870 linelen = getline(&line, &linesize, infile);
3871 if (linelen == -1) {
3872 if (ferror(infile)) {
3873 err = got_error_from_errno("getline");
3879 n = fwrite(line, 1, linelen, outfile);
3881 err = got_ferror(outfile, GOT_ERR_IO);
3886 n = fwrite(line, 1, linelen, rejectfile);
3888 err = got_ferror(outfile, GOT_ERR_IO);
3895 static const struct got_error *
3896 skip_one_line(FILE *f)
3899 size_t linesize = 0;
3902 linelen = getline(&line, &linesize, f);
3903 if (linelen == -1) {
3905 return got_error_from_errno("getline");
3912 static const struct got_error *
3913 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3914 int start_old, int end_old, int start_new, int end_new,
3915 FILE *outfile, FILE *rejectfile)
3917 const struct got_error *err;
3919 /* Copy old file's lines leading up to patch. */
3920 while (!feof(f1) && *line_cur1 < start_old) {
3921 err = copy_one_line(f1, outfile, NULL);
3926 /* Skip new file's lines leading up to patch. */
3927 while (!feof(f2) && *line_cur2 < start_new) {
3929 err = copy_one_line(f2, NULL, rejectfile);
3931 err = skip_one_line(f2);
3936 /* Copy patched lines. */
3937 while (!feof(f2) && *line_cur2 <= end_new) {
3938 err = copy_one_line(f2, outfile, NULL);
3943 /* Skip over old file's replaced lines. */
3944 while (!feof(f1) && *line_cur1 <= end_old) {
3946 err = copy_one_line(f1, NULL, rejectfile);
3948 err = skip_one_line(f1);
3957 static const struct got_error *
3958 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3959 FILE *outfile, FILE *rejectfile)
3961 const struct got_error *err;
3964 /* Copy old file's lines until EOF. */
3966 err = copy_one_line(f1, outfile, NULL);
3973 /* Copy new file's lines until EOF. */
3975 err = copy_one_line(f2, NULL, rejectfile);
3985 static const struct got_error *
3986 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3987 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3988 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3989 int *line_cur2, FILE *outfile, FILE *rejectfile,
3990 got_worktree_patch_cb patch_cb, void *patch_arg)
3992 const struct got_error *err = NULL;
3993 int start_old = change->cv.a;
3994 int end_old = change->cv.b;
3995 int start_new = change->cv.c;
3996 int end_new = change->cv.d;
4000 *choice = GOT_PATCH_CHOICE_NONE;
4002 hunkfile = got_opentemp();
4003 if (hunkfile == NULL)
4004 return got_error_from_errno("got_opentemp");
4009 /* XXX TODO needs error checking */
4010 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4012 if (fseek(f1, pos1, SEEK_SET) == -1) {
4013 err = got_ferror(f1, GOT_ERR_IO);
4016 if (fseek(f2, pos2, SEEK_SET) == -1) {
4017 err = got_ferror(f1, GOT_ERR_IO);
4020 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4021 err = got_ferror(hunkfile, GOT_ERR_IO);
4025 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4026 hunkfile, n, nchanges);
4031 case GOT_PATCH_CHOICE_YES:
4032 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4033 end_old, start_new, end_new, outfile, rejectfile);
4035 case GOT_PATCH_CHOICE_NO:
4036 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4037 end_old, start_new, end_new, rejectfile, outfile);
4039 case GOT_PATCH_CHOICE_QUIT:
4042 err = got_error(GOT_ERR_PATCH_CHOICE);
4046 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4047 err = got_error_from_errno("fclose");
4051 struct revert_file_args {
4052 struct got_worktree *worktree;
4053 struct got_fileindex *fileindex;
4054 got_worktree_checkout_cb progress_cb;
4056 got_worktree_patch_cb patch_cb;
4058 struct got_repository *repo;
4061 static const struct got_error *
4062 create_patched_content(char **path_outfile, int reverse_patch,
4063 struct got_object_id *blob_id, const char *path2,
4064 int dirfd2, const char *de_name2,
4065 const char *relpath, struct got_repository *repo,
4066 got_worktree_patch_cb patch_cb, void *patch_arg)
4068 const struct got_error *err;
4069 struct got_blob_object *blob = NULL;
4070 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4072 char link_target[PATH_MAX];
4073 ssize_t link_len = 0;
4074 char *path1 = NULL, *id_str = NULL;
4075 struct stat sb1, sb2;
4076 struct got_diff_changes *changes = NULL;
4077 struct got_diff_state *ds = NULL;
4078 struct got_diff_args *args = NULL;
4079 struct got_diff_change *change;
4080 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4083 *path_outfile = NULL;
4085 err = got_object_id_str(&id_str, blob_id);
4090 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4092 if (errno != ELOOP) {
4093 err = got_error_from_errno2("openat", path2);
4096 link_len = readlinkat(dirfd2, de_name2,
4097 link_target, sizeof(link_target));
4099 return got_error_from_errno2("readlinkat", path2);
4100 sb2.st_mode = S_IFLNK;
4101 sb2.st_size = link_len;
4104 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4106 if (errno != ELOOP) {
4107 err = got_error_from_errno2("open", path2);
4110 link_len = readlink(path2, link_target,
4111 sizeof(link_target));
4113 return got_error_from_errno2("readlink", path2);
4114 sb2.st_mode = S_IFLNK;
4115 sb2.st_size = link_len;
4119 if (fstat(fd2, &sb2) == -1) {
4120 err = got_error_from_errno2("fstat", path2);
4124 f2 = fdopen(fd2, "r");
4126 err = got_error_from_errno2("fdopen", path2);
4132 f2 = got_opentemp();
4134 err = got_error_from_errno2("got_opentemp", path2);
4137 n = fwrite(link_target, 1, link_len, f2);
4138 if (n != link_len) {
4139 err = got_ferror(f2, GOT_ERR_IO);
4142 if (fflush(f2) == EOF) {
4143 err = got_error_from_errno("fflush");
4149 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4153 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4157 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4161 if (stat(path1, &sb1) == -1) {
4162 err = got_error_from_errno2("stat", path1);
4166 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4167 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4171 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4175 if (fseek(f1, 0L, SEEK_SET) == -1)
4176 return got_ferror(f1, GOT_ERR_IO);
4177 if (fseek(f2, 0L, SEEK_SET) == -1)
4178 return got_ferror(f2, GOT_ERR_IO);
4179 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4181 err = apply_or_reject_change(&choice, change, ++n,
4182 changes->nchanges, ds, args, diff_flags, relpath,
4183 f1, f2, &line_cur1, &line_cur2,
4184 reverse_patch ? NULL : outfile,
4185 reverse_patch ? outfile : NULL,
4186 patch_cb, patch_arg);
4189 if (choice == GOT_PATCH_CHOICE_YES)
4191 else if (choice == GOT_PATCH_CHOICE_QUIT)
4195 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4196 reverse_patch ? NULL : outfile,
4197 reverse_patch ? outfile : NULL);
4201 if (!S_ISLNK(sb2.st_mode)) {
4202 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4203 err = got_error_from_errno2("chmod", path2);
4211 got_object_blob_close(blob);
4212 if (f1 && fclose(f1) == EOF && err == NULL)
4213 err = got_error_from_errno2("fclose", path1);
4214 if (f2 && fclose(f2) == EOF && err == NULL)
4215 err = got_error_from_errno2("fclose", path2);
4216 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4217 err = got_error_from_errno2("close", path2);
4218 if (outfile && fclose(outfile) == EOF && err == NULL)
4219 err = got_error_from_errno2("fclose", *path_outfile);
4220 if (path1 && unlink(path1) == -1 && err == NULL)
4221 err = got_error_from_errno2("unlink", path1);
4222 if (err || !have_content) {
4223 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4224 err = got_error_from_errno2("unlink", *path_outfile);
4225 free(*path_outfile);
4226 *path_outfile = NULL;
4230 got_diff_state_free(ds);
4234 got_diff_free_changes(changes);
4239 static const struct got_error *
4240 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4241 const char *relpath, struct got_object_id *blob_id,
4242 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4243 int dirfd, const char *de_name)
4245 struct revert_file_args *a = arg;
4246 const struct got_error *err = NULL;
4247 char *parent_path = NULL;
4248 struct got_fileindex_entry *ie;
4249 struct got_tree_object *tree = NULL;
4250 struct got_object_id *tree_id = NULL;
4251 const struct got_tree_entry *te = NULL;
4252 char *tree_path = NULL, *te_name;
4253 char *ondisk_path = NULL, *path_content = NULL;
4254 struct got_blob_object *blob = NULL;
4256 /* Reverting a staged deletion is a no-op. */
4257 if (status == GOT_STATUS_DELETE &&
4258 staged_status != GOT_STATUS_NO_CHANGE)
4261 if (status == GOT_STATUS_UNVERSIONED)
4262 return (*a->progress_cb)(a->progress_arg,
4263 GOT_STATUS_UNVERSIONED, relpath);
4265 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4267 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4269 /* Construct in-repository path of tree which contains this blob. */
4270 err = got_path_dirname(&parent_path, ie->path);
4272 if (err->code != GOT_ERR_BAD_PATH)
4274 parent_path = strdup("/");
4275 if (parent_path == NULL) {
4276 err = got_error_from_errno("strdup");
4280 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4281 tree_path = strdup(parent_path);
4282 if (tree_path == NULL) {
4283 err = got_error_from_errno("strdup");
4287 if (got_path_is_root_dir(parent_path)) {
4288 tree_path = strdup(a->worktree->path_prefix);
4289 if (tree_path == NULL) {
4290 err = got_error_from_errno("strdup");
4294 if (asprintf(&tree_path, "%s/%s",
4295 a->worktree->path_prefix, parent_path) == -1) {
4296 err = got_error_from_errno("asprintf");
4302 err = got_object_id_by_path(&tree_id, a->repo,
4303 a->worktree->base_commit_id, tree_path);
4305 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4306 (status == GOT_STATUS_ADD ||
4307 staged_status == GOT_STATUS_ADD)))
4310 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4314 te_name = basename(ie->path);
4315 if (te_name == NULL) {
4316 err = got_error_from_errno2("basename", ie->path);
4320 te = got_object_tree_find_entry(tree, te_name);
4321 if (te == NULL && status != GOT_STATUS_ADD &&
4322 staged_status != GOT_STATUS_ADD) {
4323 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4329 case GOT_STATUS_ADD:
4331 int choice = GOT_PATCH_CHOICE_NONE;
4332 err = (*a->patch_cb)(&choice, a->patch_arg,
4333 status, ie->path, NULL, 1, 1);
4336 if (choice != GOT_PATCH_CHOICE_YES)
4339 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4343 got_fileindex_entry_remove(a->fileindex, ie);
4345 case GOT_STATUS_DELETE:
4347 int choice = GOT_PATCH_CHOICE_NONE;
4348 err = (*a->patch_cb)(&choice, a->patch_arg,
4349 status, ie->path, NULL, 1, 1);
4352 if (choice != GOT_PATCH_CHOICE_YES)
4356 case GOT_STATUS_MODIFY:
4357 case GOT_STATUS_MODE_CHANGE:
4358 case GOT_STATUS_CONFLICT:
4359 case GOT_STATUS_MISSING: {
4360 struct got_object_id id;
4361 if (staged_status == GOT_STATUS_ADD ||
4362 staged_status == GOT_STATUS_MODIFY) {
4363 memcpy(id.sha1, ie->staged_blob_sha1,
4364 SHA1_DIGEST_LENGTH);
4366 memcpy(id.sha1, ie->blob_sha1,
4367 SHA1_DIGEST_LENGTH);
4368 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4372 if (asprintf(&ondisk_path, "%s/%s",
4373 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4374 err = got_error_from_errno("asprintf");
4378 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4379 status == GOT_STATUS_CONFLICT)) {
4380 int is_bad_symlink = 0;
4381 err = create_patched_content(&path_content, 1, &id,
4382 ondisk_path, dirfd, de_name, ie->path, a->repo,
4383 a->patch_cb, a->patch_arg);
4384 if (err || path_content == NULL)
4386 if (te && S_ISLNK(te->mode)) {
4387 if (unlink(path_content) == -1) {
4388 err = got_error_from_errno2("unlink",
4392 err = install_symlink(&is_bad_symlink,
4393 a->worktree, ondisk_path, ie->path,
4394 blob, 0, 1, 0, a->repo,
4395 a->progress_cb, a->progress_arg);
4397 if (rename(path_content, ondisk_path) == -1) {
4398 err = got_error_from_errno3("rename",
4399 path_content, ondisk_path);
4404 int is_bad_symlink = 0;
4405 if (te && S_ISLNK(te->mode)) {
4406 err = install_symlink(&is_bad_symlink,
4407 a->worktree, ondisk_path, ie->path,
4408 blob, 0, 1, 0, a->repo,
4409 a->progress_cb, a->progress_arg);
4411 err = install_blob(a->worktree, ondisk_path,
4413 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4414 got_fileindex_perms_to_st(ie), blob,
4415 0, 1, 0, 0, a->repo,
4416 a->progress_cb, a->progress_arg);
4420 if (status == GOT_STATUS_DELETE ||
4421 status == GOT_STATUS_MODE_CHANGE) {
4422 err = got_fileindex_entry_update(ie,
4423 ondisk_path, blob->id.sha1,
4424 a->worktree->base_commit_id->sha1, 1);
4428 if (is_bad_symlink) {
4429 got_fileindex_entry_filetype_set(ie,
4430 GOT_FILEIDX_MODE_BAD_SYMLINK);
4444 got_object_blob_close(blob);
4446 got_object_tree_close(tree);
4451 const struct got_error *
4452 got_worktree_revert(struct got_worktree *worktree,
4453 struct got_pathlist_head *paths,
4454 got_worktree_checkout_cb progress_cb, void *progress_arg,
4455 got_worktree_patch_cb patch_cb, void *patch_arg,
4456 struct got_repository *repo)
4458 struct got_fileindex *fileindex = NULL;
4459 char *fileindex_path = NULL;
4460 const struct got_error *err = NULL, *unlockerr = NULL;
4461 const struct got_error *sync_err = NULL;
4462 struct got_pathlist_entry *pe;
4463 struct revert_file_args rfa;
4465 err = lock_worktree(worktree, LOCK_EX);
4469 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4473 rfa.worktree = worktree;
4474 rfa.fileindex = fileindex;
4475 rfa.progress_cb = progress_cb;
4476 rfa.progress_arg = progress_arg;
4477 rfa.patch_cb = patch_cb;
4478 rfa.patch_arg = patch_arg;
4480 TAILQ_FOREACH(pe, paths, entry) {
4481 err = worktree_status(worktree, pe->path, fileindex, repo,
4482 revert_file, &rfa, NULL, NULL, 0, 0);
4486 sync_err = sync_fileindex(fileindex, fileindex_path);
4487 if (sync_err && err == NULL)
4490 free(fileindex_path);
4492 got_fileindex_free(fileindex);
4493 unlockerr = lock_worktree(worktree, LOCK_SH);
4494 if (unlockerr && err == NULL)
4500 free_commitable(struct got_commitable *ct)
4503 free(ct->in_repo_path);
4504 free(ct->ondisk_path);
4506 free(ct->base_blob_id);
4507 free(ct->staged_blob_id);
4508 free(ct->base_commit_id);
4512 struct collect_commitables_arg {
4513 struct got_pathlist_head *commitable_paths;
4514 struct got_repository *repo;
4515 struct got_worktree *worktree;
4516 struct got_fileindex *fileindex;
4517 int have_staged_files;
4518 int allow_bad_symlinks;
4521 static const struct got_error *
4522 collect_commitables(void *arg, unsigned char status,
4523 unsigned char staged_status, const char *relpath,
4524 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4525 struct got_object_id *commit_id, int dirfd, const char *de_name)
4527 struct collect_commitables_arg *a = arg;
4528 const struct got_error *err = NULL;
4529 struct got_commitable *ct = NULL;
4530 struct got_pathlist_entry *new = NULL;
4531 char *parent_path = NULL, *path = NULL;
4534 if (a->have_staged_files) {
4535 if (staged_status != GOT_STATUS_MODIFY &&
4536 staged_status != GOT_STATUS_ADD &&
4537 staged_status != GOT_STATUS_DELETE)
4540 if (status == GOT_STATUS_CONFLICT)
4541 return got_error(GOT_ERR_COMMIT_CONFLICT);
4543 if (status != GOT_STATUS_MODIFY &&
4544 status != GOT_STATUS_MODE_CHANGE &&
4545 status != GOT_STATUS_ADD &&
4546 status != GOT_STATUS_DELETE)
4550 if (asprintf(&path, "/%s", relpath) == -1) {
4551 err = got_error_from_errno("asprintf");
4554 if (strcmp(path, "/") == 0) {
4555 parent_path = strdup("");
4556 if (parent_path == NULL)
4557 return got_error_from_errno("strdup");
4559 err = got_path_dirname(&parent_path, path);
4564 ct = calloc(1, sizeof(*ct));
4566 err = got_error_from_errno("calloc");
4570 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4572 err = got_error_from_errno("asprintf");
4576 if (staged_status == GOT_STATUS_ADD ||
4577 staged_status == GOT_STATUS_MODIFY) {
4578 struct got_fileindex_entry *ie;
4579 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4580 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4581 case GOT_FILEIDX_MODE_REGULAR_FILE:
4582 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4585 case GOT_FILEIDX_MODE_SYMLINK:
4589 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4592 ct->mode |= got_fileindex_entry_perms_get(ie);
4593 } else if (status != GOT_STATUS_DELETE &&
4594 staged_status != GOT_STATUS_DELETE) {
4596 if (fstatat(dirfd, de_name, &sb,
4597 AT_SYMLINK_NOFOLLOW) == -1) {
4598 err = got_error_from_errno2("fstatat",
4602 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4603 err = got_error_from_errno2("lstat", ct->ondisk_path);
4606 ct->mode = sb.st_mode;
4609 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4610 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4612 err = got_error_from_errno("asprintf");
4616 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4617 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4619 char target_path[PATH_MAX];
4621 target_len = readlink(ct->ondisk_path, target_path,
4622 sizeof(target_path));
4623 if (target_len == -1) {
4624 err = got_error_from_errno2("readlink",
4628 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4629 target_len, ct->ondisk_path, a->worktree->root_path);
4632 if (is_bad_symlink) {
4633 err = got_error_path(ct->ondisk_path,
4634 GOT_ERR_BAD_SYMLINK);
4640 ct->status = status;
4641 ct->staged_status = staged_status;
4642 ct->blob_id = NULL; /* will be filled in when blob gets created */
4643 if (ct->status != GOT_STATUS_ADD &&
4644 ct->staged_status != GOT_STATUS_ADD) {
4645 ct->base_blob_id = got_object_id_dup(blob_id);
4646 if (ct->base_blob_id == NULL) {
4647 err = got_error_from_errno("got_object_id_dup");
4650 ct->base_commit_id = got_object_id_dup(commit_id);
4651 if (ct->base_commit_id == NULL) {
4652 err = got_error_from_errno("got_object_id_dup");
4656 if (ct->staged_status == GOT_STATUS_ADD ||
4657 ct->staged_status == GOT_STATUS_MODIFY) {
4658 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4659 if (ct->staged_blob_id == NULL) {
4660 err = got_error_from_errno("got_object_id_dup");
4664 ct->path = strdup(path);
4665 if (ct->path == NULL) {
4666 err = got_error_from_errno("strdup");
4669 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4671 if (ct && (err || new == NULL))
4672 free_commitable(ct);
4678 static const struct got_error *write_tree(struct got_object_id **, int *,
4679 struct got_tree_object *, const char *, struct got_pathlist_head *,
4680 got_worktree_status_cb status_cb, void *status_arg,
4681 struct got_repository *);
4683 static const struct got_error *
4684 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4685 struct got_tree_entry *te, const char *parent_path,
4686 struct got_pathlist_head *commitable_paths,
4687 got_worktree_status_cb status_cb, void *status_arg,
4688 struct got_repository *repo)
4690 const struct got_error *err = NULL;
4691 struct got_tree_object *subtree;
4694 if (asprintf(&subpath, "%s%s%s", parent_path,
4695 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4696 return got_error_from_errno("asprintf");
4698 err = got_object_open_as_tree(&subtree, repo, &te->id);
4702 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4703 commitable_paths, status_cb, status_arg, repo);
4704 got_object_tree_close(subtree);
4709 static const struct got_error *
4710 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4712 const struct got_error *err = NULL;
4713 char *ct_parent_path = NULL;
4717 if (strchr(ct->in_repo_path, '/') == NULL) {
4718 *match = got_path_is_root_dir(path);
4722 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4725 *match = (strcmp(path, ct_parent_path) == 0);
4726 free(ct_parent_path);
4731 get_ct_file_mode(struct got_commitable *ct)
4733 if (S_ISLNK(ct->mode))
4736 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4739 static const struct got_error *
4740 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4741 struct got_tree_entry *te, struct got_commitable *ct)
4743 const struct got_error *err = NULL;
4747 err = got_object_tree_entry_dup(new_te, te);
4751 (*new_te)->mode = get_ct_file_mode(ct);
4753 if (ct->staged_status == GOT_STATUS_MODIFY)
4754 memcpy(&(*new_te)->id, ct->staged_blob_id,
4755 sizeof((*new_te)->id));
4757 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4759 if (err && *new_te) {
4766 static const struct got_error *
4767 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4768 struct got_commitable *ct)
4770 const struct got_error *err = NULL;
4775 *new_te = calloc(1, sizeof(**new_te));
4776 if (*new_te == NULL)
4777 return got_error_from_errno("calloc");
4779 ct_name = basename(ct->path);
4780 if (ct_name == NULL) {
4781 err = got_error_from_errno2("basename", ct->path);
4784 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4785 sizeof((*new_te)->name)) {
4786 err = got_error(GOT_ERR_NO_SPACE);
4790 (*new_te)->mode = get_ct_file_mode(ct);
4792 if (ct->staged_status == GOT_STATUS_ADD)
4793 memcpy(&(*new_te)->id, ct->staged_blob_id,
4794 sizeof((*new_te)->id));
4796 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4798 if (err && *new_te) {
4805 static const struct got_error *
4806 insert_tree_entry(struct got_tree_entry *new_te,
4807 struct got_pathlist_head *paths)
4809 const struct got_error *err = NULL;
4810 struct got_pathlist_entry *new_pe;
4812 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4816 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4820 static const struct got_error *
4821 report_ct_status(struct got_commitable *ct,
4822 got_worktree_status_cb status_cb, void *status_arg)
4824 const char *ct_path = ct->path;
4825 unsigned char status;
4827 while (ct_path[0] == '/')
4830 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4831 status = ct->staged_status;
4833 status = ct->status;
4835 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4836 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4839 static const struct got_error *
4840 match_modified_subtree(int *modified, struct got_tree_entry *te,
4841 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4843 const struct got_error *err = NULL;
4844 struct got_pathlist_entry *pe;
4849 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4850 got_path_is_root_dir(base_tree_path) ? "" : "/",
4852 return got_error_from_errno("asprintf");
4854 TAILQ_FOREACH(pe, commitable_paths, entry) {
4855 struct got_commitable *ct = pe->data;
4856 *modified = got_path_is_child(ct->in_repo_path, te_path,
4866 static const struct got_error *
4867 match_deleted_or_modified_ct(struct got_commitable **ctp,
4868 struct got_tree_entry *te, const char *base_tree_path,
4869 struct got_pathlist_head *commitable_paths)
4871 const struct got_error *err = NULL;
4872 struct got_pathlist_entry *pe;
4876 TAILQ_FOREACH(pe, commitable_paths, entry) {
4877 struct got_commitable *ct = pe->data;
4878 char *ct_name = NULL;
4881 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4882 if (ct->status != GOT_STATUS_MODIFY &&
4883 ct->status != GOT_STATUS_MODE_CHANGE &&
4884 ct->status != GOT_STATUS_DELETE)
4887 if (ct->staged_status != GOT_STATUS_MODIFY &&
4888 ct->staged_status != GOT_STATUS_DELETE)
4892 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4895 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4901 ct_name = basename(pe->path);
4902 if (ct_name == NULL)
4903 return got_error_from_errno2("basename", pe->path);
4905 if (strcmp(te->name, ct_name) != 0)
4915 static const struct got_error *
4916 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4917 const char *child_path, const char *path_base_tree,
4918 struct got_pathlist_head *commitable_paths,
4919 got_worktree_status_cb status_cb, void *status_arg,
4920 struct got_repository *repo)
4922 const struct got_error *err = NULL;
4923 struct got_tree_entry *new_te;
4925 struct got_object_id *id = NULL;
4930 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4931 got_path_is_root_dir(path_base_tree) ? "" : "/",
4933 return got_error_from_errno("asprintf");
4935 new_te = calloc(1, sizeof(*new_te));
4937 return got_error_from_errno("calloc");
4938 new_te->mode = S_IFDIR;
4940 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4941 sizeof(new_te->name)) {
4942 err = got_error(GOT_ERR_NO_SPACE);
4945 err = write_tree(&id, &nentries, NULL, subtree_path,
4946 commitable_paths, status_cb, status_arg, repo);
4951 memcpy(&new_te->id, id, sizeof(new_te->id));
4960 static const struct got_error *
4961 write_tree(struct got_object_id **new_tree_id, int *nentries,
4962 struct got_tree_object *base_tree, const char *path_base_tree,
4963 struct got_pathlist_head *commitable_paths,
4964 got_worktree_status_cb status_cb, void *status_arg,
4965 struct got_repository *repo)
4967 const struct got_error *err = NULL;
4968 struct got_pathlist_head paths;
4969 struct got_tree_entry *te, *new_te = NULL;
4970 struct got_pathlist_entry *pe;
4975 /* Insert, and recurse into, newly added entries first. */
4976 TAILQ_FOREACH(pe, commitable_paths, entry) {
4977 struct got_commitable *ct = pe->data;
4978 char *child_path = NULL, *slash;
4980 if ((ct->status != GOT_STATUS_ADD &&
4981 ct->staged_status != GOT_STATUS_ADD) ||
4982 (ct->flags & GOT_COMMITABLE_ADDED))
4985 if (!got_path_is_child(pe->path, path_base_tree,
4986 strlen(path_base_tree)))
4989 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4994 slash = strchr(child_path, '/');
4995 if (slash == NULL) {
4996 err = alloc_added_blob_tree_entry(&new_te, ct);
4999 err = report_ct_status(ct, status_cb, status_arg);
5002 ct->flags |= GOT_COMMITABLE_ADDED;
5003 err = insert_tree_entry(new_te, &paths);
5008 *slash = '\0'; /* trim trailing path components */
5009 if (base_tree == NULL ||
5010 got_object_tree_find_entry(base_tree, child_path)
5012 err = make_subtree_for_added_blob(&new_te,
5013 child_path, path_base_tree,
5014 commitable_paths, status_cb, status_arg,
5018 err = insert_tree_entry(new_te, &paths);
5027 int i, nbase_entries;
5028 /* Handle modified and deleted entries. */
5029 nbase_entries = got_object_tree_get_nentries(base_tree);
5030 for (i = 0; i < nbase_entries; i++) {
5031 struct got_commitable *ct = NULL;
5033 te = got_object_tree_get_entry(base_tree, i);
5034 if (got_object_tree_entry_is_submodule(te)) {
5035 /* Entry is a submodule; just copy it. */
5036 err = got_object_tree_entry_dup(&new_te, te);
5039 err = insert_tree_entry(new_te, &paths);
5046 if (S_ISDIR(te->mode)) {
5048 err = got_object_tree_entry_dup(&new_te, te);
5051 err = match_modified_subtree(&modified, te,
5052 path_base_tree, commitable_paths);
5055 /* Avoid recursion into unmodified subtrees. */
5057 struct got_object_id *new_id;
5059 err = write_subtree(&new_id,
5061 path_base_tree, commitable_paths,
5062 status_cb, status_arg, repo);
5065 if (nsubentries == 0) {
5066 /* All entries were deleted. */
5070 memcpy(&new_te->id, new_id,
5071 sizeof(new_te->id));
5074 err = insert_tree_entry(new_te, &paths);
5081 err = match_deleted_or_modified_ct(&ct, te,
5082 path_base_tree, commitable_paths);
5086 /* NB: Deleted entries get dropped here. */
5087 if (ct->status == GOT_STATUS_MODIFY ||
5088 ct->status == GOT_STATUS_MODE_CHANGE ||
5089 ct->staged_status == GOT_STATUS_MODIFY) {
5090 err = alloc_modified_blob_tree_entry(
5094 err = insert_tree_entry(new_te, &paths);
5099 err = report_ct_status(ct, status_cb,
5104 /* Entry is unchanged; just copy it. */
5105 err = got_object_tree_entry_dup(&new_te, te);
5108 err = insert_tree_entry(new_te, &paths);
5116 /* Write new list of entries; deleted entries have been dropped. */
5117 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5119 got_pathlist_free(&paths);
5123 static const struct got_error *
5124 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5125 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5126 int have_staged_files)
5128 const struct got_error *err = NULL;
5129 struct got_pathlist_entry *pe;
5131 TAILQ_FOREACH(pe, commitable_paths, entry) {
5132 struct got_fileindex_entry *ie;
5133 struct got_commitable *ct = pe->data;
5135 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5137 if (ct->status == GOT_STATUS_DELETE ||
5138 ct->staged_status == GOT_STATUS_DELETE) {
5139 got_fileindex_entry_remove(fileindex, ie);
5140 } else if (ct->staged_status == GOT_STATUS_ADD ||
5141 ct->staged_status == GOT_STATUS_MODIFY) {
5142 got_fileindex_entry_stage_set(ie,
5143 GOT_FILEIDX_STAGE_NONE);
5144 err = got_fileindex_entry_update(ie,
5145 ct->ondisk_path, ct->staged_blob_id->sha1,
5146 new_base_commit_id->sha1,
5147 !have_staged_files);
5149 err = got_fileindex_entry_update(ie,
5150 ct->ondisk_path, ct->blob_id->sha1,
5151 new_base_commit_id->sha1,
5152 !have_staged_files);
5154 err = got_fileindex_entry_alloc(&ie, pe->path);
5157 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5158 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5160 got_fileindex_entry_free(ie);
5163 err = got_fileindex_entry_add(fileindex, ie);
5165 got_fileindex_entry_free(ie);
5174 static const struct got_error *
5175 check_out_of_date(const char *in_repo_path, unsigned char status,
5176 unsigned char staged_status, struct got_object_id *base_blob_id,
5177 struct got_object_id *base_commit_id,
5178 struct got_object_id *head_commit_id, struct got_repository *repo,
5181 const struct got_error *err = NULL;
5182 struct got_object_id *id = NULL;
5184 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5185 /* Trivial case: base commit == head commit */
5186 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5189 * Ensure file content which local changes were based
5190 * on matches file content in the branch head.
5192 err = got_object_id_by_path(&id, repo, head_commit_id,
5195 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5196 err = got_error(ood_errcode);
5198 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5199 err = got_error(ood_errcode);
5201 /* Require that added files don't exist in the branch head. */
5202 err = got_object_id_by_path(&id, repo, head_commit_id,
5204 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5206 err = id ? got_error(ood_errcode) : NULL;
5213 const struct got_error *
5214 commit_worktree(struct got_object_id **new_commit_id,
5215 struct got_pathlist_head *commitable_paths,
5216 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5217 const char *author, const char *committer,
5218 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5219 got_worktree_status_cb status_cb, void *status_arg,
5220 struct got_repository *repo)
5222 const struct got_error *err = NULL, *unlockerr = NULL;
5223 struct got_pathlist_entry *pe;
5224 const char *head_ref_name = NULL;
5225 struct got_commit_object *head_commit = NULL;
5226 struct got_reference *head_ref2 = NULL;
5227 struct got_object_id *head_commit_id2 = NULL;
5228 struct got_tree_object *head_tree = NULL;
5229 struct got_object_id *new_tree_id = NULL;
5231 struct got_object_id_queue parent_ids;
5232 struct got_object_qid *pid = NULL;
5233 char *logmsg = NULL;
5235 *new_commit_id = NULL;
5237 SIMPLEQ_INIT(&parent_ids);
5239 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5243 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5247 if (commit_msg_cb != NULL) {
5248 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5253 if (logmsg == NULL || strlen(logmsg) == 0) {
5254 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5258 /* Create blobs from added and modified files and record their IDs. */
5259 TAILQ_FOREACH(pe, commitable_paths, entry) {
5260 struct got_commitable *ct = pe->data;
5263 /* Blobs for staged files already exist. */
5264 if (ct->staged_status == GOT_STATUS_ADD ||
5265 ct->staged_status == GOT_STATUS_MODIFY)
5268 if (ct->status != GOT_STATUS_ADD &&
5269 ct->status != GOT_STATUS_MODIFY &&
5270 ct->status != GOT_STATUS_MODE_CHANGE)
5273 if (asprintf(&ondisk_path, "%s/%s",
5274 worktree->root_path, pe->path) == -1) {
5275 err = got_error_from_errno("asprintf");
5278 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5284 /* Recursively write new tree objects. */
5285 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5286 commitable_paths, status_cb, status_arg, repo);
5290 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5293 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5294 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5295 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5296 got_object_qid_free(pid);
5302 /* Check if a concurrent commit to our branch has occurred. */
5303 head_ref_name = got_worktree_get_head_ref_name(worktree);
5304 if (head_ref_name == NULL) {
5305 err = got_error_from_errno("got_worktree_get_head_ref_name");
5308 /* Lock the reference here to prevent concurrent modification. */
5309 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5312 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5315 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5316 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5319 /* Update branch head in repository. */
5320 err = got_ref_change_ref(head_ref2, *new_commit_id);
5323 err = got_ref_write(head_ref2, repo);
5327 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5331 err = ref_base_commit(worktree, repo);
5336 got_object_tree_close(head_tree);
5338 got_object_commit_close(head_commit);
5339 free(head_commit_id2);
5341 unlockerr = got_ref_unlock(head_ref2);
5342 if (unlockerr && err == NULL)
5344 got_ref_close(head_ref2);
5349 static const struct got_error *
5350 check_path_is_commitable(const char *path,
5351 struct got_pathlist_head *commitable_paths)
5353 struct got_pathlist_entry *cpe = NULL;
5354 size_t path_len = strlen(path);
5356 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5357 struct got_commitable *ct = cpe->data;
5358 const char *ct_path = ct->path;
5360 while (ct_path[0] == '/')
5363 if (strcmp(path, ct_path) == 0 ||
5364 got_path_is_child(ct_path, path, path_len))
5369 return got_error_path(path, GOT_ERR_BAD_PATH);
5374 static const struct got_error *
5375 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5377 int *have_staged_files = arg;
5379 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5380 *have_staged_files = 1;
5381 return got_error(GOT_ERR_CANCELLED);
5387 static const struct got_error *
5388 check_non_staged_files(struct got_fileindex *fileindex,
5389 struct got_pathlist_head *paths)
5391 struct got_pathlist_entry *pe;
5392 struct got_fileindex_entry *ie;
5394 TAILQ_FOREACH(pe, paths, entry) {
5395 if (pe->path[0] == '\0')
5397 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5399 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5400 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5401 return got_error_path(pe->path,
5402 GOT_ERR_FILE_NOT_STAGED);
5408 const struct got_error *
5409 got_worktree_commit(struct got_object_id **new_commit_id,
5410 struct got_worktree *worktree, struct got_pathlist_head *paths,
5411 const char *author, const char *committer, int allow_bad_symlinks,
5412 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5413 got_worktree_status_cb status_cb, void *status_arg,
5414 struct got_repository *repo)
5416 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5417 struct got_fileindex *fileindex = NULL;
5418 char *fileindex_path = NULL;
5419 struct got_pathlist_head commitable_paths;
5420 struct collect_commitables_arg cc_arg;
5421 struct got_pathlist_entry *pe;
5422 struct got_reference *head_ref = NULL;
5423 struct got_object_id *head_commit_id = NULL;
5424 int have_staged_files = 0;
5426 *new_commit_id = NULL;
5428 TAILQ_INIT(&commitable_paths);
5430 err = lock_worktree(worktree, LOCK_EX);
5434 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5438 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5442 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5446 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5447 &have_staged_files);
5448 if (err && err->code != GOT_ERR_CANCELLED)
5450 if (have_staged_files) {
5451 err = check_non_staged_files(fileindex, paths);
5456 cc_arg.commitable_paths = &commitable_paths;
5457 cc_arg.worktree = worktree;
5458 cc_arg.fileindex = fileindex;
5460 cc_arg.have_staged_files = have_staged_files;
5461 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5462 TAILQ_FOREACH(pe, paths, entry) {
5463 err = worktree_status(worktree, pe->path, fileindex, repo,
5464 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5469 if (TAILQ_EMPTY(&commitable_paths)) {
5470 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5474 TAILQ_FOREACH(pe, paths, entry) {
5475 err = check_path_is_commitable(pe->path, &commitable_paths);
5480 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5481 struct got_commitable *ct = pe->data;
5482 const char *ct_path = ct->in_repo_path;
5484 while (ct_path[0] == '/')
5486 err = check_out_of_date(ct_path, ct->status,
5487 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5488 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5494 err = commit_worktree(new_commit_id, &commitable_paths,
5495 head_commit_id, worktree, author, committer,
5496 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5500 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5501 fileindex, have_staged_files);
5502 sync_err = sync_fileindex(fileindex, fileindex_path);
5503 if (sync_err && err == NULL)
5507 got_fileindex_free(fileindex);
5508 free(fileindex_path);
5509 unlockerr = lock_worktree(worktree, LOCK_SH);
5510 if (unlockerr && err == NULL)
5512 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5513 struct got_commitable *ct = pe->data;
5514 free_commitable(ct);
5516 got_pathlist_free(&commitable_paths);
5521 got_commitable_get_path(struct got_commitable *ct)
5527 got_commitable_get_status(struct got_commitable *ct)
5532 struct check_rebase_ok_arg {
5533 struct got_worktree *worktree;
5534 struct got_repository *repo;
5537 static const struct got_error *
5538 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5540 const struct got_error *err = NULL;
5541 struct check_rebase_ok_arg *a = arg;
5542 unsigned char status;
5546 /* Reject rebase of a work tree with mixed base commits. */
5547 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5548 SHA1_DIGEST_LENGTH))
5549 return got_error(GOT_ERR_MIXED_COMMITS);
5551 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5553 return got_error_from_errno("asprintf");
5555 /* Reject rebase of a work tree with modified or staged files. */
5556 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5561 if (status != GOT_STATUS_NO_CHANGE)
5562 return got_error(GOT_ERR_MODIFIED);
5563 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5564 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5569 const struct got_error *
5570 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5571 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5572 struct got_worktree *worktree, struct got_reference *branch,
5573 struct got_repository *repo)
5575 const struct got_error *err = NULL;
5576 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5577 char *branch_ref_name = NULL;
5578 char *fileindex_path = NULL;
5579 struct check_rebase_ok_arg ok_arg;
5580 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5581 struct got_object_id *wt_branch_tip = NULL;
5583 *new_base_branch_ref = NULL;
5587 err = lock_worktree(worktree, LOCK_EX);
5591 err = open_fileindex(fileindex, &fileindex_path, worktree);
5595 ok_arg.worktree = worktree;
5597 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5602 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5606 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5610 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5614 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5619 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5622 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5623 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5627 err = got_ref_alloc_symref(new_base_branch_ref,
5628 new_base_branch_ref_name, wt_branch);
5631 err = got_ref_write(*new_base_branch_ref, repo);
5635 /* TODO Lock original branch's ref while rebasing? */
5637 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5641 err = got_ref_write(branch_ref, repo);
5645 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5646 worktree->base_commit_id);
5649 err = got_ref_write(*tmp_branch, repo);
5653 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5657 free(fileindex_path);
5658 free(tmp_branch_name);
5659 free(new_base_branch_ref_name);
5660 free(branch_ref_name);
5662 got_ref_close(branch_ref);
5664 got_ref_close(wt_branch);
5665 free(wt_branch_tip);
5667 if (*new_base_branch_ref) {
5668 got_ref_close(*new_base_branch_ref);
5669 *new_base_branch_ref = NULL;
5672 got_ref_close(*tmp_branch);
5676 got_fileindex_free(*fileindex);
5679 lock_worktree(worktree, LOCK_SH);
5684 const struct got_error *
5685 got_worktree_rebase_continue(struct got_object_id **commit_id,
5686 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5687 struct got_reference **branch, struct got_fileindex **fileindex,
5688 struct got_worktree *worktree, struct got_repository *repo)
5690 const struct got_error *err;
5691 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5692 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5693 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5694 char *fileindex_path = NULL;
5695 int have_staged_files = 0;
5698 *new_base_branch = NULL;
5703 err = lock_worktree(worktree, LOCK_EX);
5707 err = open_fileindex(fileindex, &fileindex_path, worktree);
5711 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5712 &have_staged_files);
5713 if (err && err->code != GOT_ERR_CANCELLED)
5715 if (have_staged_files) {
5716 err = got_error(GOT_ERR_STAGED_PATHS);
5720 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5724 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5728 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5732 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5736 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5740 err = got_ref_open(branch, repo,
5741 got_ref_get_symref_target(branch_ref), 0);
5745 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5749 err = got_ref_resolve(commit_id, repo, commit_ref);
5753 err = got_ref_open(new_base_branch, repo,
5754 new_base_branch_ref_name, 0);
5758 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5762 free(commit_ref_name);
5763 free(branch_ref_name);
5764 free(fileindex_path);
5766 got_ref_close(commit_ref);
5768 got_ref_close(branch_ref);
5773 got_ref_close(*tmp_branch);
5776 if (*new_base_branch) {
5777 got_ref_close(*new_base_branch);
5778 *new_base_branch = NULL;
5781 got_ref_close(*branch);
5785 got_fileindex_free(*fileindex);
5788 lock_worktree(worktree, LOCK_SH);
5793 const struct got_error *
5794 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5796 const struct got_error *err;
5797 char *tmp_branch_name = NULL;
5799 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5803 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5804 free(tmp_branch_name);
5808 static const struct got_error *
5809 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5810 char **logmsg, void *arg)
5816 static const struct got_error *
5817 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5818 const char *path, struct got_object_id *blob_id,
5819 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5820 int dirfd, const char *de_name)
5825 struct collect_merged_paths_arg {
5826 got_worktree_checkout_cb progress_cb;
5828 struct got_pathlist_head *merged_paths;
5831 static const struct got_error *
5832 collect_merged_paths(void *arg, unsigned char status, const char *path)
5834 const struct got_error *err;
5835 struct collect_merged_paths_arg *a = arg;
5837 struct got_pathlist_entry *new;
5839 err = (*a->progress_cb)(a->progress_arg, status, path);
5843 if (status != GOT_STATUS_MERGE &&
5844 status != GOT_STATUS_ADD &&
5845 status != GOT_STATUS_DELETE &&
5846 status != GOT_STATUS_CONFLICT)
5851 return got_error_from_errno("strdup");
5853 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5854 if (err || new == NULL)
5860 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5862 struct got_pathlist_entry *pe;
5864 TAILQ_FOREACH(pe, merged_paths, entry)
5865 free((char *)pe->path);
5867 got_pathlist_free(merged_paths);
5870 static const struct got_error *
5871 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5872 int is_rebase, struct got_repository *repo)
5874 const struct got_error *err;
5875 struct got_reference *commit_ref = NULL;
5877 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5879 if (err->code != GOT_ERR_NOT_REF)
5881 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5884 err = got_ref_write(commit_ref, repo);
5887 } else if (is_rebase) {
5888 struct got_object_id *stored_id;
5891 err = got_ref_resolve(&stored_id, repo, commit_ref);
5894 cmp = got_object_id_cmp(commit_id, stored_id);
5897 err = got_error(GOT_ERR_REBASE_COMMITID);
5903 got_ref_close(commit_ref);
5907 static const struct got_error *
5908 rebase_merge_files(struct got_pathlist_head *merged_paths,
5909 const char *commit_ref_name, struct got_worktree *worktree,
5910 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5911 struct got_object_id *commit_id, struct got_repository *repo,
5912 got_worktree_checkout_cb progress_cb, void *progress_arg,
5913 got_cancel_cb cancel_cb, void *cancel_arg)
5915 const struct got_error *err;
5916 struct got_reference *commit_ref = NULL;
5917 struct collect_merged_paths_arg cmp_arg;
5918 char *fileindex_path;
5920 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5922 err = get_fileindex_path(&fileindex_path, worktree);
5926 cmp_arg.progress_cb = progress_cb;
5927 cmp_arg.progress_arg = progress_arg;
5928 cmp_arg.merged_paths = merged_paths;
5929 err = merge_files(worktree, fileindex, fileindex_path,
5930 parent_commit_id, commit_id, repo, collect_merged_paths,
5931 &cmp_arg, cancel_cb, cancel_arg);
5933 got_ref_close(commit_ref);
5937 const struct got_error *
5938 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5939 struct got_worktree *worktree, struct got_fileindex *fileindex,
5940 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5941 struct got_repository *repo,
5942 got_worktree_checkout_cb progress_cb, void *progress_arg,
5943 got_cancel_cb cancel_cb, void *cancel_arg)
5945 const struct got_error *err;
5946 char *commit_ref_name;
5948 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5952 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5956 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5957 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5958 progress_arg, cancel_cb, cancel_arg);
5960 free(commit_ref_name);
5964 const struct got_error *
5965 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5966 struct got_worktree *worktree, struct got_fileindex *fileindex,
5967 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5968 struct got_repository *repo,
5969 got_worktree_checkout_cb progress_cb, void *progress_arg,
5970 got_cancel_cb cancel_cb, void *cancel_arg)
5972 const struct got_error *err;
5973 char *commit_ref_name;
5975 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5979 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5983 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5984 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5985 progress_arg, cancel_cb, cancel_arg);
5987 free(commit_ref_name);
5991 static const struct got_error *
5992 rebase_commit(struct got_object_id **new_commit_id,
5993 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5994 struct got_worktree *worktree, struct got_fileindex *fileindex,
5995 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5996 const char *new_logmsg, struct got_repository *repo)
5998 const struct got_error *err, *sync_err;
5999 struct got_pathlist_head commitable_paths;
6000 struct collect_commitables_arg cc_arg;
6001 char *fileindex_path = NULL;
6002 struct got_reference *head_ref = NULL;
6003 struct got_object_id *head_commit_id = NULL;
6004 char *logmsg = NULL;
6006 TAILQ_INIT(&commitable_paths);
6007 *new_commit_id = NULL;
6009 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6011 err = get_fileindex_path(&fileindex_path, worktree);
6015 cc_arg.commitable_paths = &commitable_paths;
6016 cc_arg.worktree = worktree;
6018 cc_arg.have_staged_files = 0;
6020 * If possible get the status of individual files directly to
6021 * avoid crawling the entire work tree once per rebased commit.
6022 * TODO: Ideally, merged_paths would contain a list of commitables
6023 * we could use so we could skip worktree_status() entirely.
6026 struct got_pathlist_entry *pe;
6027 TAILQ_FOREACH(pe, merged_paths, entry) {
6028 err = worktree_status(worktree, pe->path, fileindex,
6029 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6035 err = worktree_status(worktree, "", fileindex, repo,
6036 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6041 if (TAILQ_EMPTY(&commitable_paths)) {
6042 /* No-op change; commit will be elided. */
6043 err = got_ref_delete(commit_ref, repo);
6046 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6050 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6054 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6059 logmsg = strdup(new_logmsg);
6060 if (logmsg == NULL) {
6061 err = got_error_from_errno("strdup");
6065 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6070 /* NB: commit_worktree will call free(logmsg) */
6071 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6072 worktree, got_object_commit_get_author(orig_commit),
6073 got_object_commit_get_committer(orig_commit),
6074 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6078 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6082 err = got_ref_delete(commit_ref, repo);
6086 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6088 sync_err = sync_fileindex(fileindex, fileindex_path);
6089 if (sync_err && err == NULL)
6092 free(fileindex_path);
6093 free(head_commit_id);
6095 got_ref_close(head_ref);
6097 free(*new_commit_id);
6098 *new_commit_id = NULL;
6103 const struct got_error *
6104 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6105 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6106 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6107 struct got_commit_object *orig_commit,
6108 struct got_object_id *orig_commit_id, struct got_repository *repo)
6110 const struct got_error *err;
6111 char *commit_ref_name;
6112 struct got_reference *commit_ref = NULL;
6113 struct got_object_id *commit_id = NULL;
6115 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6119 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6122 err = got_ref_resolve(&commit_id, repo, commit_ref);
6125 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6126 err = got_error(GOT_ERR_REBASE_COMMITID);
6130 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6131 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6134 got_ref_close(commit_ref);
6135 free(commit_ref_name);
6140 const struct got_error *
6141 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6142 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6143 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6144 struct got_commit_object *orig_commit,
6145 struct got_object_id *orig_commit_id, const char *new_logmsg,
6146 struct got_repository *repo)
6148 const struct got_error *err;
6149 char *commit_ref_name;
6150 struct got_reference *commit_ref = NULL;
6152 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6156 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6160 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6161 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6164 got_ref_close(commit_ref);
6165 free(commit_ref_name);
6169 const struct got_error *
6170 got_worktree_rebase_postpone(struct got_worktree *worktree,
6171 struct got_fileindex *fileindex)
6174 got_fileindex_free(fileindex);
6175 return lock_worktree(worktree, LOCK_SH);
6178 static const struct got_error *
6179 delete_ref(const char *name, struct got_repository *repo)
6181 const struct got_error *err;
6182 struct got_reference *ref;
6184 err = got_ref_open(&ref, repo, name, 0);
6186 if (err->code == GOT_ERR_NOT_REF)
6191 err = got_ref_delete(ref, repo);
6196 static const struct got_error *
6197 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6199 const struct got_error *err;
6200 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6201 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6203 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6206 err = delete_ref(tmp_branch_name, repo);
6210 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6213 err = delete_ref(new_base_branch_ref_name, repo);
6217 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6220 err = delete_ref(branch_ref_name, repo);
6224 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6227 err = delete_ref(commit_ref_name, repo);
6232 free(tmp_branch_name);
6233 free(new_base_branch_ref_name);
6234 free(branch_ref_name);
6235 free(commit_ref_name);
6239 const struct got_error *
6240 got_worktree_rebase_complete(struct got_worktree *worktree,
6241 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6242 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6243 struct got_repository *repo)
6245 const struct got_error *err, *unlockerr;
6246 struct got_object_id *new_head_commit_id = NULL;
6248 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6252 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6256 err = got_ref_write(rebased_branch, repo);
6260 err = got_worktree_set_head_ref(worktree, rebased_branch);
6264 err = delete_rebase_refs(worktree, repo);
6267 got_fileindex_free(fileindex);
6268 free(new_head_commit_id);
6269 unlockerr = lock_worktree(worktree, LOCK_SH);
6270 if (unlockerr && err == NULL)
6275 const struct got_error *
6276 got_worktree_rebase_abort(struct got_worktree *worktree,
6277 struct got_fileindex *fileindex, struct got_repository *repo,
6278 struct got_reference *new_base_branch,
6279 got_worktree_checkout_cb progress_cb, void *progress_arg)
6281 const struct got_error *err, *unlockerr, *sync_err;
6282 struct got_reference *resolved = NULL;
6283 struct got_object_id *commit_id = NULL;
6284 char *fileindex_path = NULL;
6285 struct revert_file_args rfa;
6286 struct got_object_id *tree_id = NULL;
6288 err = lock_worktree(worktree, LOCK_EX);
6292 err = got_ref_open(&resolved, repo,
6293 got_ref_get_symref_target(new_base_branch), 0);
6297 err = got_worktree_set_head_ref(worktree, resolved);
6302 * XXX commits to the base branch could have happened while
6303 * we were busy rebasing; should we store the original commit ID
6304 * when rebase begins and read it back here?
6306 err = got_ref_resolve(&commit_id, repo, resolved);
6310 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6314 err = got_object_id_by_path(&tree_id, repo,
6315 worktree->base_commit_id, worktree->path_prefix);
6319 err = delete_rebase_refs(worktree, repo);
6323 err = get_fileindex_path(&fileindex_path, worktree);
6327 rfa.worktree = worktree;
6328 rfa.fileindex = fileindex;
6329 rfa.progress_cb = progress_cb;
6330 rfa.progress_arg = progress_arg;
6331 rfa.patch_cb = NULL;
6332 rfa.patch_arg = NULL;
6334 err = worktree_status(worktree, "", fileindex, repo,
6335 revert_file, &rfa, NULL, NULL, 0, 0);
6339 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6340 repo, progress_cb, progress_arg, NULL, NULL);
6342 sync_err = sync_fileindex(fileindex, fileindex_path);
6343 if (sync_err && err == NULL)
6346 got_ref_close(resolved);
6350 got_fileindex_free(fileindex);
6351 free(fileindex_path);
6353 unlockerr = lock_worktree(worktree, LOCK_SH);
6354 if (unlockerr && err == NULL)
6359 const struct got_error *
6360 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6361 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6362 struct got_fileindex **fileindex, struct got_worktree *worktree,
6363 struct got_repository *repo)
6365 const struct got_error *err = NULL;
6366 char *tmp_branch_name = NULL;
6367 char *branch_ref_name = NULL;
6368 char *base_commit_ref_name = NULL;
6369 char *fileindex_path = NULL;
6370 struct check_rebase_ok_arg ok_arg;
6371 struct got_reference *wt_branch = NULL;
6372 struct got_reference *base_commit_ref = NULL;
6376 *base_commit_id = NULL;
6379 err = lock_worktree(worktree, LOCK_EX);
6383 err = open_fileindex(fileindex, &fileindex_path, worktree);
6387 ok_arg.worktree = worktree;
6389 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6394 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6398 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6402 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6407 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6412 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6416 err = got_ref_write(*branch_ref, repo);
6420 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6421 worktree->base_commit_id);
6424 err = got_ref_write(base_commit_ref, repo);
6427 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6428 if (*base_commit_id == NULL) {
6429 err = got_error_from_errno("got_object_id_dup");
6433 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6434 worktree->base_commit_id);
6437 err = got_ref_write(*tmp_branch, repo);
6441 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6445 free(fileindex_path);
6446 free(tmp_branch_name);
6447 free(branch_ref_name);
6448 free(base_commit_ref_name);
6450 got_ref_close(wt_branch);
6453 got_ref_close(*branch_ref);
6457 got_ref_close(*tmp_branch);
6460 free(*base_commit_id);
6462 got_fileindex_free(*fileindex);
6465 lock_worktree(worktree, LOCK_SH);
6470 const struct got_error *
6471 got_worktree_histedit_postpone(struct got_worktree *worktree,
6472 struct got_fileindex *fileindex)
6475 got_fileindex_free(fileindex);
6476 return lock_worktree(worktree, LOCK_SH);
6479 const struct got_error *
6480 got_worktree_histedit_in_progress(int *in_progress,
6481 struct got_worktree *worktree)
6483 const struct got_error *err;
6484 char *tmp_branch_name = NULL;
6486 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6490 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6491 free(tmp_branch_name);
6495 const struct got_error *
6496 got_worktree_histedit_continue(struct got_object_id **commit_id,
6497 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6498 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6499 struct got_worktree *worktree, struct got_repository *repo)
6501 const struct got_error *err;
6502 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6503 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6504 struct got_reference *commit_ref = NULL;
6505 struct got_reference *base_commit_ref = NULL;
6506 char *fileindex_path = NULL;
6507 int have_staged_files = 0;
6511 *base_commit_id = NULL;
6514 err = lock_worktree(worktree, LOCK_EX);
6518 err = open_fileindex(fileindex, &fileindex_path, worktree);
6522 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6523 &have_staged_files);
6524 if (err && err->code != GOT_ERR_CANCELLED)
6526 if (have_staged_files) {
6527 err = got_error(GOT_ERR_STAGED_PATHS);
6531 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6535 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6539 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6543 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6548 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6552 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6555 err = got_ref_resolve(commit_id, repo, commit_ref);
6559 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6562 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6566 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6570 free(commit_ref_name);
6571 free(branch_ref_name);
6572 free(fileindex_path);
6574 got_ref_close(commit_ref);
6575 if (base_commit_ref)
6576 got_ref_close(base_commit_ref);
6580 free(*base_commit_id);
6581 *base_commit_id = NULL;
6583 got_ref_close(*tmp_branch);
6587 got_fileindex_free(*fileindex);
6590 lock_worktree(worktree, LOCK_EX);
6595 static const struct got_error *
6596 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6598 const struct got_error *err;
6599 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6600 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6602 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6605 err = delete_ref(tmp_branch_name, repo);
6609 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6613 err = delete_ref(base_commit_ref_name, repo);
6617 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6620 err = delete_ref(branch_ref_name, repo);
6624 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6627 err = delete_ref(commit_ref_name, repo);
6631 free(tmp_branch_name);
6632 free(base_commit_ref_name);
6633 free(branch_ref_name);
6634 free(commit_ref_name);
6638 const struct got_error *
6639 got_worktree_histedit_abort(struct got_worktree *worktree,
6640 struct got_fileindex *fileindex, struct got_repository *repo,
6641 struct got_reference *branch, struct got_object_id *base_commit_id,
6642 got_worktree_checkout_cb progress_cb, void *progress_arg)
6644 const struct got_error *err, *unlockerr, *sync_err;
6645 struct got_reference *resolved = NULL;
6646 char *fileindex_path = NULL;
6647 struct got_object_id *tree_id = NULL;
6648 struct revert_file_args rfa;
6650 err = lock_worktree(worktree, LOCK_EX);
6654 err = got_ref_open(&resolved, repo,
6655 got_ref_get_symref_target(branch), 0);
6659 err = got_worktree_set_head_ref(worktree, resolved);
6663 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6667 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6668 worktree->path_prefix);
6672 err = delete_histedit_refs(worktree, repo);
6676 err = get_fileindex_path(&fileindex_path, worktree);
6680 rfa.worktree = worktree;
6681 rfa.fileindex = fileindex;
6682 rfa.progress_cb = progress_cb;
6683 rfa.progress_arg = progress_arg;
6684 rfa.patch_cb = NULL;
6685 rfa.patch_arg = NULL;
6687 err = worktree_status(worktree, "", fileindex, repo,
6688 revert_file, &rfa, NULL, NULL, 0, 0);
6692 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6693 repo, progress_cb, progress_arg, NULL, NULL);
6695 sync_err = sync_fileindex(fileindex, fileindex_path);
6696 if (sync_err && err == NULL)
6699 got_ref_close(resolved);
6701 free(fileindex_path);
6703 unlockerr = lock_worktree(worktree, LOCK_SH);
6704 if (unlockerr && err == NULL)
6709 const struct got_error *
6710 got_worktree_histedit_complete(struct got_worktree *worktree,
6711 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6712 struct got_reference *edited_branch, struct got_repository *repo)
6714 const struct got_error *err, *unlockerr;
6715 struct got_object_id *new_head_commit_id = NULL;
6716 struct got_reference *resolved = NULL;
6718 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6722 err = got_ref_open(&resolved, repo,
6723 got_ref_get_symref_target(edited_branch), 0);
6727 err = got_ref_change_ref(resolved, new_head_commit_id);
6731 err = got_ref_write(resolved, repo);
6735 err = got_worktree_set_head_ref(worktree, resolved);
6739 err = delete_histedit_refs(worktree, repo);
6742 got_fileindex_free(fileindex);
6743 free(new_head_commit_id);
6744 unlockerr = lock_worktree(worktree, LOCK_SH);
6745 if (unlockerr && err == NULL)
6750 const struct got_error *
6751 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6752 struct got_object_id *commit_id, struct got_repository *repo)
6754 const struct got_error *err;
6755 char *commit_ref_name;
6757 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6761 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6765 err = delete_ref(commit_ref_name, repo);
6767 free(commit_ref_name);
6771 const struct got_error *
6772 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6773 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6774 struct got_worktree *worktree, const char *refname,
6775 struct got_repository *repo)
6777 const struct got_error *err = NULL;
6778 char *fileindex_path = NULL;
6779 struct check_rebase_ok_arg ok_arg;
6783 *base_branch_ref = NULL;
6785 err = lock_worktree(worktree, LOCK_EX);
6789 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6790 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6791 "cannot integrate a branch into itself; "
6792 "update -b or different branch name required");
6796 err = open_fileindex(fileindex, &fileindex_path, worktree);
6800 /* Preconditions are the same as for rebase. */
6801 ok_arg.worktree = worktree;
6803 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6808 err = got_ref_open(branch_ref, repo, refname, 1);
6812 err = got_ref_open(base_branch_ref, repo,
6813 got_worktree_get_head_ref_name(worktree), 1);
6817 got_ref_close(*branch_ref);
6820 if (*base_branch_ref) {
6821 got_ref_close(*base_branch_ref);
6822 *base_branch_ref = NULL;
6825 got_fileindex_free(*fileindex);
6828 lock_worktree(worktree, LOCK_SH);
6833 const struct got_error *
6834 got_worktree_integrate_continue(struct got_worktree *worktree,
6835 struct got_fileindex *fileindex, struct got_repository *repo,
6836 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6837 got_worktree_checkout_cb progress_cb, void *progress_arg,
6838 got_cancel_cb cancel_cb, void *cancel_arg)
6840 const struct got_error *err = NULL, *sync_err, *unlockerr;
6841 char *fileindex_path = NULL;
6842 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6844 err = get_fileindex_path(&fileindex_path, worktree);
6848 err = got_ref_resolve(&commit_id, repo, branch_ref);
6852 err = got_object_id_by_path(&tree_id, repo, commit_id,
6853 worktree->path_prefix);
6857 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6861 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6862 progress_cb, progress_arg, cancel_cb, cancel_arg);
6866 err = got_ref_change_ref(base_branch_ref, commit_id);
6870 err = got_ref_write(base_branch_ref, repo);
6872 sync_err = sync_fileindex(fileindex, fileindex_path);
6873 if (sync_err && err == NULL)
6877 unlockerr = got_ref_unlock(branch_ref);
6878 if (unlockerr && err == NULL)
6880 got_ref_close(branch_ref);
6882 unlockerr = got_ref_unlock(base_branch_ref);
6883 if (unlockerr && err == NULL)
6885 got_ref_close(base_branch_ref);
6887 got_fileindex_free(fileindex);
6888 free(fileindex_path);
6891 unlockerr = lock_worktree(worktree, LOCK_SH);
6892 if (unlockerr && err == NULL)
6897 const struct got_error *
6898 got_worktree_integrate_abort(struct got_worktree *worktree,
6899 struct got_fileindex *fileindex, struct got_repository *repo,
6900 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6902 const struct got_error *err = NULL, *unlockerr = NULL;
6904 got_fileindex_free(fileindex);
6906 err = lock_worktree(worktree, LOCK_SH);
6908 unlockerr = got_ref_unlock(branch_ref);
6909 if (unlockerr && err == NULL)
6911 got_ref_close(branch_ref);
6913 unlockerr = got_ref_unlock(base_branch_ref);
6914 if (unlockerr && err == NULL)
6916 got_ref_close(base_branch_ref);
6921 struct check_stage_ok_arg {
6922 struct got_object_id *head_commit_id;
6923 struct got_worktree *worktree;
6924 struct got_fileindex *fileindex;
6925 struct got_repository *repo;
6929 const struct got_error *
6930 check_stage_ok(void *arg, unsigned char status,
6931 unsigned char staged_status, const char *relpath,
6932 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6933 struct got_object_id *commit_id, int dirfd, const char *de_name)
6935 struct check_stage_ok_arg *a = arg;
6936 const struct got_error *err = NULL;
6937 struct got_fileindex_entry *ie;
6938 struct got_object_id base_commit_id;
6939 struct got_object_id *base_commit_idp = NULL;
6940 char *in_repo_path = NULL, *p;
6942 if (status == GOT_STATUS_UNVERSIONED ||
6943 status == GOT_STATUS_NO_CHANGE)
6945 if (status == GOT_STATUS_NONEXISTENT)
6946 return got_error_set_errno(ENOENT, relpath);
6948 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6950 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6952 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6953 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6955 return got_error_from_errno("asprintf");
6957 if (got_fileindex_entry_has_commit(ie)) {
6958 memcpy(base_commit_id.sha1, ie->commit_sha1,
6959 SHA1_DIGEST_LENGTH);
6960 base_commit_idp = &base_commit_id;
6963 if (status == GOT_STATUS_CONFLICT) {
6964 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6966 } else if (status != GOT_STATUS_ADD &&
6967 status != GOT_STATUS_MODIFY &&
6968 status != GOT_STATUS_DELETE) {
6969 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6973 a->have_changes = 1;
6978 err = check_out_of_date(p, status, staged_status,
6979 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6980 GOT_ERR_STAGE_OUT_OF_DATE);
6986 struct stage_path_arg {
6987 struct got_worktree *worktree;
6988 struct got_fileindex *fileindex;
6989 struct got_repository *repo;
6990 got_worktree_status_cb status_cb;
6992 got_worktree_patch_cb patch_cb;
6994 int staged_something;
6995 int allow_bad_symlinks;
6998 static const struct got_error *
6999 stage_path(void *arg, unsigned char status,
7000 unsigned char staged_status, const char *relpath,
7001 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7002 struct got_object_id *commit_id, int dirfd, const char *de_name)
7004 struct stage_path_arg *a = arg;
7005 const struct got_error *err = NULL;
7006 struct got_fileindex_entry *ie;
7007 char *ondisk_path = NULL, *path_content = NULL;
7009 struct got_object_id *new_staged_blob_id = NULL;
7012 if (status == GOT_STATUS_UNVERSIONED)
7015 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7017 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7019 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7021 return got_error_from_errno("asprintf");
7024 case GOT_STATUS_ADD:
7025 case GOT_STATUS_MODIFY:
7026 /* XXX could sb.st_mode be passed in by our caller? */
7027 if (lstat(ondisk_path, &sb) == -1) {
7028 err = got_error_from_errno2("lstat", ondisk_path);
7032 if (status == GOT_STATUS_ADD) {
7033 int choice = GOT_PATCH_CHOICE_NONE;
7034 err = (*a->patch_cb)(&choice, a->patch_arg,
7035 status, ie->path, NULL, 1, 1);
7038 if (choice != GOT_PATCH_CHOICE_YES)
7041 err = create_patched_content(&path_content, 0,
7042 staged_blob_id ? staged_blob_id : blob_id,
7043 ondisk_path, dirfd, de_name, ie->path,
7044 a->repo, a->patch_cb, a->patch_arg);
7045 if (err || path_content == NULL)
7049 err = got_object_blob_create(&new_staged_blob_id,
7050 path_content ? path_content : ondisk_path, a->repo);
7053 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7054 SHA1_DIGEST_LENGTH);
7055 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7056 stage = GOT_FILEIDX_STAGE_ADD;
7058 stage = GOT_FILEIDX_STAGE_MODIFY;
7059 got_fileindex_entry_stage_set(ie, stage);
7060 if (S_ISLNK(sb.st_mode)) {
7061 int is_bad_symlink = 0;
7062 if (!a->allow_bad_symlinks) {
7063 char target_path[PATH_MAX];
7065 target_len = readlink(ondisk_path, target_path,
7066 sizeof(target_path));
7067 if (target_len == -1) {
7068 err = got_error_from_errno2("readlink",
7072 err = is_bad_symlink_target(&is_bad_symlink,
7073 target_path, target_len, ondisk_path,
7074 a->worktree->root_path);
7077 if (is_bad_symlink) {
7078 err = got_error_path(ondisk_path,
7079 GOT_ERR_BAD_SYMLINK);
7084 got_fileindex_entry_staged_filetype_set(ie,
7085 GOT_FILEIDX_MODE_BAD_SYMLINK);
7087 got_fileindex_entry_staged_filetype_set(ie,
7088 GOT_FILEIDX_MODE_SYMLINK);
7090 got_fileindex_entry_staged_filetype_set(ie,
7091 GOT_FILEIDX_MODE_REGULAR_FILE);
7093 a->staged_something = 1;
7094 if (a->status_cb == NULL)
7096 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7097 get_staged_status(ie), relpath, blob_id,
7098 new_staged_blob_id, NULL, dirfd, de_name);
7100 case GOT_STATUS_DELETE:
7101 if (staged_status == GOT_STATUS_DELETE)
7104 int choice = GOT_PATCH_CHOICE_NONE;
7105 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7106 ie->path, NULL, 1, 1);
7109 if (choice == GOT_PATCH_CHOICE_NO)
7111 if (choice != GOT_PATCH_CHOICE_YES) {
7112 err = got_error(GOT_ERR_PATCH_CHOICE);
7116 stage = GOT_FILEIDX_STAGE_DELETE;
7117 got_fileindex_entry_stage_set(ie, stage);
7118 a->staged_something = 1;
7119 if (a->status_cb == NULL)
7121 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7122 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7125 case GOT_STATUS_NO_CHANGE:
7127 case GOT_STATUS_CONFLICT:
7128 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7130 case GOT_STATUS_NONEXISTENT:
7131 err = got_error_set_errno(ENOENT, relpath);
7134 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7138 if (path_content && unlink(path_content) == -1 && err == NULL)
7139 err = got_error_from_errno2("unlink", path_content);
7142 free(new_staged_blob_id);
7146 const struct got_error *
7147 got_worktree_stage(struct got_worktree *worktree,
7148 struct got_pathlist_head *paths,
7149 got_worktree_status_cb status_cb, void *status_arg,
7150 got_worktree_patch_cb patch_cb, void *patch_arg,
7151 int allow_bad_symlinks, struct got_repository *repo)
7153 const struct got_error *err = NULL, *sync_err, *unlockerr;
7154 struct got_pathlist_entry *pe;
7155 struct got_fileindex *fileindex = NULL;
7156 char *fileindex_path = NULL;
7157 struct got_reference *head_ref = NULL;
7158 struct got_object_id *head_commit_id = NULL;
7159 struct check_stage_ok_arg oka;
7160 struct stage_path_arg spa;
7162 err = lock_worktree(worktree, LOCK_EX);
7166 err = got_ref_open(&head_ref, repo,
7167 got_worktree_get_head_ref_name(worktree), 0);
7170 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7173 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7177 /* Check pre-conditions before staging anything. */
7178 oka.head_commit_id = head_commit_id;
7179 oka.worktree = worktree;
7180 oka.fileindex = fileindex;
7182 oka.have_changes = 0;
7183 TAILQ_FOREACH(pe, paths, entry) {
7184 err = worktree_status(worktree, pe->path, fileindex, repo,
7185 check_stage_ok, &oka, NULL, NULL, 0, 0);
7189 if (!oka.have_changes) {
7190 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7194 spa.worktree = worktree;
7195 spa.fileindex = fileindex;
7197 spa.patch_cb = patch_cb;
7198 spa.patch_arg = patch_arg;
7199 spa.status_cb = status_cb;
7200 spa.status_arg = status_arg;
7201 spa.staged_something = 0;
7202 spa.allow_bad_symlinks = allow_bad_symlinks;
7203 TAILQ_FOREACH(pe, paths, entry) {
7204 err = worktree_status(worktree, pe->path, fileindex, repo,
7205 stage_path, &spa, NULL, NULL, 0, 0);
7209 if (!spa.staged_something) {
7210 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7214 sync_err = sync_fileindex(fileindex, fileindex_path);
7215 if (sync_err && err == NULL)
7219 got_ref_close(head_ref);
7220 free(head_commit_id);
7221 free(fileindex_path);
7223 got_fileindex_free(fileindex);
7224 unlockerr = lock_worktree(worktree, LOCK_SH);
7225 if (unlockerr && err == NULL)
7230 struct unstage_path_arg {
7231 struct got_worktree *worktree;
7232 struct got_fileindex *fileindex;
7233 struct got_repository *repo;
7234 got_worktree_checkout_cb progress_cb;
7236 got_worktree_patch_cb patch_cb;
7240 static const struct got_error *
7241 create_unstaged_content(char **path_unstaged_content,
7242 char **path_new_staged_content, struct got_object_id *blob_id,
7243 struct got_object_id *staged_blob_id, const char *relpath,
7244 struct got_repository *repo,
7245 got_worktree_patch_cb patch_cb, void *patch_arg)
7247 const struct got_error *err;
7248 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7249 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7250 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7251 struct stat sb1, sb2;
7252 struct got_diff_changes *changes = NULL;
7253 struct got_diff_state *ds = NULL;
7254 struct got_diff_args *args = NULL;
7255 struct got_diff_change *change;
7256 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7257 int have_content = 0, have_rejected_content = 0;
7259 *path_unstaged_content = NULL;
7260 *path_new_staged_content = NULL;
7262 err = got_object_id_str(&label1, blob_id);
7265 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7269 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7273 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7277 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7281 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7285 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7289 if (stat(path1, &sb1) == -1) {
7290 err = got_error_from_errno2("stat", path1);
7294 if (stat(path2, &sb2) == -1) {
7295 err = got_error_from_errno2("stat", path2);
7299 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7300 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7304 err = got_opentemp_named(path_unstaged_content, &outfile,
7305 "got-unstaged-content");
7308 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7309 "got-new-staged-content");
7313 if (fseek(f1, 0L, SEEK_SET) == -1) {
7314 err = got_ferror(f1, GOT_ERR_IO);
7317 if (fseek(f2, 0L, SEEK_SET) == -1) {
7318 err = got_ferror(f2, GOT_ERR_IO);
7321 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7323 err = apply_or_reject_change(&choice, change, ++n,
7324 changes->nchanges, ds, args, diff_flags, relpath,
7325 f1, f2, &line_cur1, &line_cur2,
7326 outfile, rejectfile, patch_cb, patch_arg);
7329 if (choice == GOT_PATCH_CHOICE_YES)
7332 have_rejected_content = 1;
7333 if (choice == GOT_PATCH_CHOICE_QUIT)
7336 if (have_content || have_rejected_content)
7337 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7338 outfile, rejectfile);
7342 got_object_blob_close(blob);
7344 got_object_blob_close(staged_blob);
7345 if (f1 && fclose(f1) == EOF && err == NULL)
7346 err = got_error_from_errno2("fclose", path1);
7347 if (f2 && fclose(f2) == EOF && err == NULL)
7348 err = got_error_from_errno2("fclose", path2);
7349 if (outfile && fclose(outfile) == EOF && err == NULL)
7350 err = got_error_from_errno2("fclose", *path_unstaged_content);
7351 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7352 err = got_error_from_errno2("fclose", *path_new_staged_content);
7353 if (path1 && unlink(path1) == -1 && err == NULL)
7354 err = got_error_from_errno2("unlink", path1);
7355 if (path2 && unlink(path2) == -1 && err == NULL)
7356 err = got_error_from_errno2("unlink", path2);
7357 if (err || !have_content) {
7358 if (*path_unstaged_content &&
7359 unlink(*path_unstaged_content) == -1 && err == NULL)
7360 err = got_error_from_errno2("unlink",
7361 *path_unstaged_content);
7362 free(*path_unstaged_content);
7363 *path_unstaged_content = NULL;
7365 if (err || !have_rejected_content) {
7366 if (*path_new_staged_content &&
7367 unlink(*path_new_staged_content) == -1 && err == NULL)
7368 err = got_error_from_errno2("unlink",
7369 *path_new_staged_content);
7370 free(*path_new_staged_content);
7371 *path_new_staged_content = NULL;
7375 got_diff_state_free(ds);
7379 got_diff_free_changes(changes);
7385 static const struct got_error *
7386 unstage_path(void *arg, unsigned char status,
7387 unsigned char staged_status, const char *relpath,
7388 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7389 struct got_object_id *commit_id, int dirfd, const char *de_name)
7391 const struct got_error *err = NULL;
7392 struct unstage_path_arg *a = arg;
7393 struct got_fileindex_entry *ie;
7394 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7395 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7396 char *path_new_staged_content = NULL;
7397 char *id_str = NULL, *label_orig = NULL;
7398 int local_changes_subsumed;
7401 if (staged_status != GOT_STATUS_ADD &&
7402 staged_status != GOT_STATUS_MODIFY &&
7403 staged_status != GOT_STATUS_DELETE)
7406 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7408 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7410 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7412 return got_error_from_errno("asprintf");
7414 err = got_object_id_str(&id_str,
7415 commit_id ? commit_id : a->worktree->base_commit_id);
7418 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7420 err = got_error_from_errno("asprintf");
7424 switch (staged_status) {
7425 case GOT_STATUS_MODIFY:
7426 err = got_object_open_as_blob(&blob_base, a->repo,
7431 case GOT_STATUS_ADD:
7433 if (staged_status == GOT_STATUS_ADD) {
7434 int choice = GOT_PATCH_CHOICE_NONE;
7435 err = (*a->patch_cb)(&choice, a->patch_arg,
7436 staged_status, ie->path, NULL, 1, 1);
7439 if (choice != GOT_PATCH_CHOICE_YES)
7442 err = create_unstaged_content(
7443 &path_unstaged_content,
7444 &path_new_staged_content, blob_id,
7445 staged_blob_id, ie->path, a->repo,
7446 a->patch_cb, a->patch_arg);
7447 if (err || path_unstaged_content == NULL)
7449 if (path_new_staged_content) {
7450 err = got_object_blob_create(
7452 path_new_staged_content,
7456 memcpy(ie->staged_blob_sha1,
7457 staged_blob_id->sha1,
7458 SHA1_DIGEST_LENGTH);
7460 err = merge_file(&local_changes_subsumed,
7461 a->worktree, blob_base, ondisk_path,
7462 relpath, got_fileindex_perms_to_st(ie),
7463 path_unstaged_content, label_orig,
7464 "unstaged", a->repo, a->progress_cb,
7467 path_new_staged_content == NULL)
7468 got_fileindex_entry_stage_set(ie,
7469 GOT_FILEIDX_STAGE_NONE);
7470 break; /* Done with this file. */
7473 err = got_object_open_as_blob(&blob_staged, a->repo,
7474 staged_blob_id, 8192);
7477 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7478 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7479 case GOT_FILEIDX_MODE_REGULAR_FILE:
7480 err = merge_blob(&local_changes_subsumed, a->worktree,
7481 blob_base, ondisk_path, relpath,
7482 got_fileindex_perms_to_st(ie), label_orig,
7483 blob_staged, commit_id ? commit_id :
7484 a->worktree->base_commit_id, a->repo,
7485 a->progress_cb, a->progress_arg);
7487 case GOT_FILEIDX_MODE_SYMLINK:
7488 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7489 err = merge_symlink(a->worktree, blob_base,
7490 ondisk_path, relpath, label_orig,
7491 blob_staged, commit_id ? commit_id :
7492 a->worktree->base_commit_id,
7493 a->repo, a->progress_cb, a->progress_arg);
7495 err = merge_blob(&local_changes_subsumed,
7496 a->worktree, blob_base, ondisk_path,
7497 relpath, got_fileindex_perms_to_st(ie),
7498 label_orig, blob_staged,
7499 commit_id ? commit_id :
7500 a->worktree->base_commit_id, a->repo,
7501 a->progress_cb, a->progress_arg);
7505 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7509 got_fileindex_entry_stage_set(ie,
7510 GOT_FILEIDX_STAGE_NONE);
7512 case GOT_STATUS_DELETE:
7514 int choice = GOT_PATCH_CHOICE_NONE;
7515 err = (*a->patch_cb)(&choice, a->patch_arg,
7516 staged_status, ie->path, NULL, 1, 1);
7519 if (choice == GOT_PATCH_CHOICE_NO)
7521 if (choice != GOT_PATCH_CHOICE_YES) {
7522 err = got_error(GOT_ERR_PATCH_CHOICE);
7526 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7527 err = get_file_status(&status, &sb, ie, ondisk_path,
7528 dirfd, de_name, a->repo);
7531 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7536 if (path_unstaged_content &&
7537 unlink(path_unstaged_content) == -1 && err == NULL)
7538 err = got_error_from_errno2("unlink", path_unstaged_content);
7539 if (path_new_staged_content &&
7540 unlink(path_new_staged_content) == -1 && err == NULL)
7541 err = got_error_from_errno2("unlink", path_new_staged_content);
7542 free(path_unstaged_content);
7543 free(path_new_staged_content);
7545 got_object_blob_close(blob_base);
7547 got_object_blob_close(blob_staged);
7553 const struct got_error *
7554 got_worktree_unstage(struct got_worktree *worktree,
7555 struct got_pathlist_head *paths,
7556 got_worktree_checkout_cb progress_cb, void *progress_arg,
7557 got_worktree_patch_cb patch_cb, void *patch_arg,
7558 struct got_repository *repo)
7560 const struct got_error *err = NULL, *sync_err, *unlockerr;
7561 struct got_pathlist_entry *pe;
7562 struct got_fileindex *fileindex = NULL;
7563 char *fileindex_path = NULL;
7564 struct unstage_path_arg upa;
7566 err = lock_worktree(worktree, LOCK_EX);
7570 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7574 upa.worktree = worktree;
7575 upa.fileindex = fileindex;
7577 upa.progress_cb = progress_cb;
7578 upa.progress_arg = progress_arg;
7579 upa.patch_cb = patch_cb;
7580 upa.patch_arg = patch_arg;
7581 TAILQ_FOREACH(pe, paths, entry) {
7582 err = worktree_status(worktree, pe->path, fileindex, repo,
7583 unstage_path, &upa, NULL, NULL, 0, 0);
7588 sync_err = sync_fileindex(fileindex, fileindex_path);
7589 if (sync_err && err == NULL)
7592 free(fileindex_path);
7594 got_fileindex_free(fileindex);
7595 unlockerr = lock_worktree(worktree, LOCK_SH);
7596 if (unlockerr && err == NULL)