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 %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
928 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
929 deriv_target ? deriv_target : "(symlink was deleted)",
930 orig_target ? label_orig : "",
931 orig_target ? "\n" : "",
932 orig_target ? orig_target : "",
933 orig_target ? "\n" : "",
934 GOT_DIFF_CONFLICT_MARKER_SEP,
935 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
936 err = got_error_from_errno2("fprintf", path);
940 if (unlink(ondisk_path) == -1) {
941 err = got_error_from_errno2("unlink", ondisk_path);
944 if (rename(path, ondisk_path) == -1) {
945 err = got_error_from_errno3("rename", path, ondisk_path);
948 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
949 err = got_error_from_errno2("chmod", ondisk_path);
953 if (f != NULL && fclose(f) == EOF && err == NULL)
954 err = got_error_from_errno2("fclose", path);
961 /* forward declaration */
962 static const struct got_error *
963 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
964 const char *, const char *, uint16_t, const char *,
965 struct got_blob_object *, struct got_object_id *,
966 struct got_repository *, got_worktree_checkout_cb, void *);
969 * Merge a symlink into the work tree, where blob_orig acts as the common
970 * ancestor, deriv_target is the link target of the first derived version,
971 * and the symlink on disk acts as the second derived version.
972 * Assume that contents of both blobs represent symlinks.
974 static const struct got_error *
975 merge_symlink(struct got_worktree *worktree,
976 struct got_blob_object *blob_orig, const char *ondisk_path,
977 const char *path, const char *label_orig, const char *deriv_target,
978 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
979 got_worktree_checkout_cb progress_cb, void *progress_arg)
981 const struct got_error *err = NULL;
982 char *ancestor_target = NULL;
984 ssize_t ondisk_len, deriv_len;
985 char ondisk_target[PATH_MAX];
986 int have_local_change = 0;
987 int have_incoming_change = 0;
989 if (lstat(ondisk_path, &sb) == -1)
990 return got_error_from_errno2("lstat", ondisk_path);
992 ondisk_len = readlink(ondisk_path, ondisk_target,
993 sizeof(ondisk_target));
994 if (ondisk_len == -1) {
995 err = got_error_from_errno2("readlink",
999 ondisk_target[ondisk_len] = '\0';
1002 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1007 if (ancestor_target == NULL ||
1008 (ondisk_len != strlen(ancestor_target) ||
1009 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1010 have_local_change = 1;
1012 deriv_len = strlen(deriv_target);
1013 if (ancestor_target == NULL ||
1014 (deriv_len != strlen(ancestor_target) ||
1015 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1016 have_incoming_change = 1;
1018 if (!have_local_change && !have_incoming_change) {
1019 if (ancestor_target) {
1020 /* Both sides made the same change. */
1021 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1023 } else if (deriv_len == ondisk_len &&
1024 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1025 /* Both sides added the same symlink. */
1026 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1029 /* Both sides added symlinks which don't match. */
1030 err = install_symlink_conflict(deriv_target,
1031 deriv_base_commit_id, ancestor_target,
1032 label_orig, ondisk_target, ondisk_path);
1035 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1038 } else if (!have_local_change && have_incoming_change) {
1039 /* Apply the incoming change. */
1040 err = update_symlink(ondisk_path, deriv_target,
1041 strlen(deriv_target));
1044 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1045 } else if (have_local_change && have_incoming_change) {
1046 if (deriv_len == ondisk_len &&
1047 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1048 /* Both sides made the same change. */
1049 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1052 err = install_symlink_conflict(deriv_target,
1053 deriv_base_commit_id, ancestor_target, label_orig,
1054 ondisk_target, ondisk_path);
1057 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1063 free(ancestor_target);
1068 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1069 * blob_deriv acts as the first derived version, and the file on disk
1070 * acts as the second derived version.
1072 static const struct got_error *
1073 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1074 struct got_blob_object *blob_orig, const char *ondisk_path,
1075 const char *path, uint16_t st_mode, const char *label_orig,
1076 struct got_blob_object *blob_deriv,
1077 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1078 got_worktree_checkout_cb progress_cb, void *progress_arg)
1080 const struct got_error *err = NULL;
1081 FILE *f_deriv = NULL;
1082 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1083 char *label_deriv = NULL, *parent;
1085 *local_changes_subsumed = 0;
1087 parent = dirname(ondisk_path);
1089 return got_error_from_errno2("dirname", ondisk_path);
1092 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1093 err = got_error_from_errno("asprintf");
1098 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1101 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1106 err = got_object_id_str(&id_str, deriv_base_commit_id);
1109 if (asprintf(&label_deriv, "%s: commit %s",
1110 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1111 err = got_error_from_errno("asprintf");
1115 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1116 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1117 label_deriv, repo, progress_cb, progress_arg);
1119 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1120 err = got_error_from_errno("fclose");
1122 if (blob_deriv_path) {
1123 unlink(blob_deriv_path);
1124 free(blob_deriv_path);
1131 static const struct got_error *
1132 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1133 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1134 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1136 const struct got_error *err = NULL;
1137 struct got_fileindex_entry *new_ie;
1141 err = got_fileindex_entry_alloc(&new_ie, path);
1145 err = got_fileindex_entry_update(new_ie, ondisk_path,
1146 blob_id->sha1, base_commit_id->sha1, 1);
1150 err = got_fileindex_entry_add(fileindex, new_ie);
1153 got_fileindex_entry_free(new_ie);
1160 get_ondisk_perms(int executable, mode_t st_mode)
1162 mode_t xbits = S_IXUSR;
1165 /* Map read bits to execute bits. */
1166 if (st_mode & S_IRGRP)
1168 if (st_mode & S_IROTH)
1170 return st_mode | xbits;
1173 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1176 /* forward declaration */
1177 static const struct got_error *
1178 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1179 const char *path, mode_t te_mode, mode_t st_mode,
1180 struct got_blob_object *blob, int restoring_missing_file,
1181 int reverting_versioned_file, int installing_bad_symlink,
1182 int path_is_unversioned, struct got_repository *repo,
1183 got_worktree_checkout_cb progress_cb, void *progress_arg);
1186 * This function assumes that the provided symlink target points at a
1187 * safe location in the work tree!
1189 static const struct got_error *
1190 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1193 const struct got_error *err = NULL;
1195 char etarget[PATH_MAX];
1199 * "Bad" symlinks (those pointing outside the work tree or into the
1200 * .got directory) are installed in the work tree as a regular file
1201 * which contains the bad symlink target path.
1202 * The new symlink target has already been checked for safety by our
1203 * caller. If we can successfully open a regular file then we simply
1204 * replace this file with a symlink below.
1206 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1209 return got_error_from_errno2("open", ondisk_path);
1211 /* We are updating an existing on-disk symlink. */
1212 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1214 return got_error_from_errno2("readlink", ondisk_path);
1216 if (elen == target_len &&
1217 memcmp(etarget, target_path, target_len) == 0)
1218 return NULL; /* nothing to do */
1221 err = update_symlink(ondisk_path, target_path, target_len);
1222 if (fd != -1 && close(fd) == -1 && err == NULL)
1223 err = got_error_from_errno2("close", ondisk_path);
1227 static const struct got_error *
1228 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1229 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1231 const struct got_error *err = NULL;
1232 char canonpath[PATH_MAX];
1233 char *path_got = NULL;
1235 *is_bad_symlink = 0;
1237 if (target_len >= sizeof(canonpath)) {
1238 *is_bad_symlink = 1;
1243 * We do not use realpath(3) to resolve the symlink's target
1244 * path because we don't want to resolve symlinks recursively.
1245 * Instead we make the path absolute and then canonicalize it.
1246 * Relative symlink target lookup should begin at the directory
1247 * in which the blob object is being installed.
1249 if (!got_path_is_absolute(target_path)) {
1251 char *parent = dirname(ondisk_path);
1253 return got_error_from_errno2("dirname", ondisk_path);
1254 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1255 return got_error_from_errno("asprintf");
1256 if (strlen(abspath) >= sizeof(canonpath)) {
1257 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1261 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1266 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1271 /* Only allow symlinks pointing at paths within the work tree. */
1272 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1273 *is_bad_symlink = 1;
1277 /* Do not allow symlinks pointing into the .got directory. */
1278 if (asprintf(&path_got, "%s/%s", wtroot_path,
1279 GOT_WORKTREE_GOT_DIR) == -1)
1280 return got_error_from_errno("asprintf");
1281 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1282 *is_bad_symlink = 1;
1288 static const struct got_error *
1289 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1290 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1291 int restoring_missing_file, int reverting_versioned_file,
1292 int path_is_unversioned, struct got_repository *repo,
1293 got_worktree_checkout_cb progress_cb, void *progress_arg)
1295 const struct got_error *err = NULL;
1296 char target_path[PATH_MAX];
1297 size_t len, target_len = 0;
1298 char *path_got = NULL;
1299 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1300 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1302 *is_bad_symlink = 0;
1305 * Blob object content specifies the target path of the link.
1306 * If a symbolic link cannot be installed we instead create
1307 * a regular file which contains the link target path stored
1308 * in the blob object.
1311 err = got_object_blob_read_block(&len, blob);
1312 if (len + target_len >= sizeof(target_path)) {
1313 /* Path too long; install as a regular file. */
1314 *is_bad_symlink = 1;
1315 got_object_blob_rewind(blob);
1316 return install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 restoring_missing_file, reverting_versioned_file,
1319 1, path_is_unversioned, repo, progress_cb,
1323 /* Skip blob object header first time around. */
1324 memcpy(target_path + target_len, buf + hdrlen,
1326 target_len += len - hdrlen;
1330 target_path[target_len] = '\0';
1332 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1333 ondisk_path, worktree->root_path);
1337 if (*is_bad_symlink) {
1338 /* install as a regular file */
1339 *is_bad_symlink = 1;
1340 got_object_blob_rewind(blob);
1341 err = install_blob(worktree, ondisk_path, path,
1342 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1343 restoring_missing_file, reverting_versioned_file, 1,
1344 path_is_unversioned, repo, progress_cb, progress_arg);
1348 if (symlink(target_path, ondisk_path) == -1) {
1349 if (errno == EEXIST) {
1350 if (path_is_unversioned) {
1351 err = (*progress_cb)(progress_arg,
1352 GOT_STATUS_UNVERSIONED, path);
1355 err = replace_existing_symlink(ondisk_path,
1356 target_path, target_len);
1360 err = (*progress_cb)(progress_arg,
1361 reverting_versioned_file ?
1362 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1365 goto done; /* Nothing else to do. */
1368 if (errno == ENOENT) {
1369 char *parent = dirname(ondisk_path);
1370 if (parent == NULL) {
1371 err = got_error_from_errno2("dirname",
1375 err = add_dir_on_disk(worktree, parent);
1379 * Retry, and fall through to error handling
1380 * below if this second attempt fails.
1382 if (symlink(target_path, ondisk_path) != -1) {
1383 err = NULL; /* success */
1388 /* Handle errors from first or second creation attempt. */
1389 if (errno == ENAMETOOLONG) {
1390 /* bad target path; install as a regular file */
1391 *is_bad_symlink = 1;
1392 got_object_blob_rewind(blob);
1393 err = install_blob(worktree, ondisk_path, path,
1394 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1395 restoring_missing_file, reverting_versioned_file, 1,
1396 path_is_unversioned, repo,
1397 progress_cb, progress_arg);
1398 } else if (errno == ENOTDIR) {
1399 err = got_error_path(ondisk_path,
1400 GOT_ERR_FILE_OBSTRUCTED);
1402 err = got_error_from_errno3("symlink",
1403 target_path, ondisk_path);
1405 } else if (progress_cb)
1406 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1407 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1413 static const struct got_error *
1414 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1415 const char *path, mode_t te_mode, mode_t st_mode,
1416 struct got_blob_object *blob, int restoring_missing_file,
1417 int reverting_versioned_file, int installing_bad_symlink,
1418 int path_is_unversioned, struct got_repository *repo,
1419 got_worktree_checkout_cb progress_cb, void *progress_arg)
1421 const struct got_error *err = NULL;
1425 char *tmppath = NULL;
1427 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1428 GOT_DEFAULT_FILE_MODE);
1430 if (errno == ENOENT) {
1431 char *parent = dirname(path);
1433 return got_error_from_errno2("dirname", path);
1434 err = add_dir_on_disk(worktree, parent);
1437 fd = open(ondisk_path,
1438 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1439 GOT_DEFAULT_FILE_MODE);
1441 return got_error_from_errno2("open",
1443 } else if (errno == EEXIST) {
1444 if (path_is_unversioned) {
1445 err = (*progress_cb)(progress_arg,
1446 GOT_STATUS_UNVERSIONED, path);
1449 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1450 /* TODO file is obstructed; do something */
1451 err = got_error_path(ondisk_path,
1452 GOT_ERR_FILE_OBSTRUCTED);
1455 err = got_opentemp_named_fd(&tmppath, &fd,
1462 return got_error_from_errno2("open", ondisk_path);
1466 if (restoring_missing_file)
1467 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1469 else if (reverting_versioned_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1473 err = (*progress_cb)(progress_arg,
1474 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1479 hdrlen = got_object_blob_get_hdrlen(blob);
1481 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 err = got_object_blob_read_block(&len, blob);
1486 /* Skip blob object header first time around. */
1487 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1489 err = got_error_from_errno("write");
1491 } else if (outlen != len - hdrlen) {
1492 err = got_error(GOT_ERR_IO);
1499 if (fsync(fd) != 0) {
1500 err = got_error_from_errno("fsync");
1505 if (rename(tmppath, ondisk_path) != 0) {
1506 err = got_error_from_errno3("rename", tmppath,
1513 if (chmod(ondisk_path,
1514 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1515 err = got_error_from_errno2("chmod", ondisk_path);
1520 if (fd != -1 && close(fd) != 0 && err == NULL)
1521 err = got_error_from_errno("close");
1526 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1527 static const struct got_error *
1528 get_modified_file_content_status(unsigned char *status, FILE *f)
1530 const struct got_error *err = NULL;
1531 const char *markers[3] = {
1532 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1533 GOT_DIFF_CONFLICT_MARKER_SEP,
1534 GOT_DIFF_CONFLICT_MARKER_END
1539 const char delim[3] = {'\0', '\0', '\0'};
1541 while (*status == GOT_STATUS_MODIFY) {
1542 line = fparseln(f, &len, NULL, delim, 0);
1546 err = got_ferror(f, GOT_ERR_IO);
1550 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1551 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1553 *status = GOT_STATUS_CONFLICT;
1563 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1565 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1566 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1570 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1572 return !(ie->ctime_sec == sb->st_ctime &&
1573 ie->ctime_nsec == sb->st_ctimensec &&
1574 ie->mtime_sec == sb->st_mtime &&
1575 ie->mtime_nsec == sb->st_mtimensec &&
1576 ie->size == (sb->st_size & 0xffffffff) &&
1577 !xbit_differs(ie, sb->st_mode));
1580 static unsigned char
1581 get_staged_status(struct got_fileindex_entry *ie)
1583 switch (got_fileindex_entry_stage_get(ie)) {
1584 case GOT_FILEIDX_STAGE_ADD:
1585 return GOT_STATUS_ADD;
1586 case GOT_FILEIDX_STAGE_DELETE:
1587 return GOT_STATUS_DELETE;
1588 case GOT_FILEIDX_STAGE_MODIFY:
1589 return GOT_STATUS_MODIFY;
1591 return GOT_STATUS_NO_CHANGE;
1595 static const struct got_error *
1596 get_symlink_modification_status(unsigned char *status,
1597 struct got_fileindex_entry *ie, const char *abspath,
1598 int dirfd, const char *de_name, struct got_blob_object *blob)
1600 const struct got_error *err = NULL;
1601 char target_path[PATH_MAX];
1602 char etarget[PATH_MAX];
1604 size_t len, target_len = 0;
1605 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1606 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1608 *status = GOT_STATUS_NO_CHANGE;
1610 /* Blob object content specifies the target path of the link. */
1612 err = got_object_blob_read_block(&len, blob);
1615 if (len + target_len >= sizeof(target_path)) {
1617 * Should not happen. The blob contents were OK
1618 * when this symlink was installed.
1620 return got_error(GOT_ERR_NO_SPACE);
1623 /* Skip blob object header first time around. */
1624 memcpy(target_path + target_len, buf + hdrlen,
1626 target_len += len - hdrlen;
1630 target_path[target_len] = '\0';
1633 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1635 return got_error_from_errno2("readlinkat", abspath);
1637 elen = readlink(abspath, etarget, sizeof(etarget));
1639 return got_error_from_errno2("readlink", abspath);
1642 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1643 *status = GOT_STATUS_MODIFY;
1648 static const struct got_error *
1649 get_file_status(unsigned char *status, struct stat *sb,
1650 struct got_fileindex_entry *ie, const char *abspath,
1651 int dirfd, const char *de_name, struct got_repository *repo)
1653 const struct got_error *err = NULL;
1654 struct got_object_id id;
1659 struct got_blob_object *blob = NULL;
1661 unsigned char staged_status = get_staged_status(ie);
1663 *status = GOT_STATUS_NO_CHANGE;
1666 * Whenever the caller provides a directory descriptor and a
1667 * directory entry name for the file, use them! This prevents
1668 * race conditions if filesystem paths change beneath our feet.
1671 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1672 if (errno == ENOENT) {
1673 if (got_fileindex_entry_has_file_on_disk(ie))
1674 *status = GOT_STATUS_MISSING;
1676 *status = GOT_STATUS_DELETE;
1679 err = got_error_from_errno2("fstatat", abspath);
1683 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1684 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1685 return got_error_from_errno2("open", abspath);
1686 else if (fd == -1 && errno == ELOOP) {
1687 if (lstat(abspath, sb) == -1)
1688 return got_error_from_errno2("lstat", abspath);
1689 } else if (fd == -1 || fstat(fd, sb) == -1) {
1690 if (errno == ENOENT) {
1691 if (got_fileindex_entry_has_file_on_disk(ie))
1692 *status = GOT_STATUS_MISSING;
1694 *status = GOT_STATUS_DELETE;
1697 err = got_error_from_errno2("fstat", abspath);
1702 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1703 *status = GOT_STATUS_OBSTRUCTED;
1707 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1708 *status = GOT_STATUS_DELETE;
1710 } else if (!got_fileindex_entry_has_blob(ie) &&
1711 staged_status != GOT_STATUS_ADD) {
1712 *status = GOT_STATUS_ADD;
1716 if (!stat_info_differs(ie, sb))
1719 if (S_ISLNK(sb->st_mode) &&
1720 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1721 *status = GOT_STATUS_MODIFY;
1725 if (staged_status == GOT_STATUS_MODIFY ||
1726 staged_status == GOT_STATUS_ADD)
1727 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1729 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1731 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1735 if (S_ISLNK(sb->st_mode)) {
1736 err = get_symlink_modification_status(status, ie,
1737 abspath, dirfd, de_name, blob);
1742 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1744 err = got_error_from_errno2("openat", abspath);
1749 f = fdopen(fd, "r");
1751 err = got_error_from_errno2("fdopen", abspath);
1755 hdrlen = got_object_blob_get_hdrlen(blob);
1757 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1758 err = got_object_blob_read_block(&blen, blob);
1761 /* Skip length of blob object header first time around. */
1762 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1763 if (flen == 0 && ferror(f)) {
1764 err = got_error_from_errno("fread");
1769 *status = GOT_STATUS_MODIFY;
1771 } else if (flen == 0) {
1773 *status = GOT_STATUS_MODIFY;
1775 } else if (blen - hdrlen == flen) {
1776 /* Skip blob object header first time around. */
1777 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1778 *status = GOT_STATUS_MODIFY;
1782 *status = GOT_STATUS_MODIFY;
1788 if (*status == GOT_STATUS_MODIFY) {
1790 err = get_modified_file_content_status(status, f);
1791 } else if (xbit_differs(ie, sb->st_mode))
1792 *status = GOT_STATUS_MODE_CHANGE;
1795 got_object_blob_close(blob);
1796 if (f != NULL && fclose(f) == EOF && err == NULL)
1797 err = got_error_from_errno2("fclose", abspath);
1798 if (fd != -1 && close(fd) == -1 && err == NULL)
1799 err = got_error_from_errno2("close", abspath);
1804 * Update timestamps in the file index if a file is unmodified and
1805 * we had to run a full content comparison to find out.
1807 static const struct got_error *
1808 sync_timestamps(char *ondisk_path, unsigned char status,
1809 struct got_fileindex_entry *ie, struct stat *sb)
1811 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1812 return got_fileindex_entry_update(ie, ondisk_path,
1813 ie->blob_sha1, ie->commit_sha1, 1);
1818 static const struct got_error *
1819 update_blob(struct got_worktree *worktree,
1820 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1821 struct got_tree_entry *te, const char *path,
1822 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1825 const struct got_error *err = NULL;
1826 struct got_blob_object *blob = NULL;
1828 unsigned char status = GOT_STATUS_NO_CHANGE;
1831 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 return got_error_from_errno("asprintf");
1835 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1839 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1843 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 sb.st_mode = got_fileindex_perms_to_st(ie);
1846 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1847 status = GOT_STATUS_UNVERSIONED;
1850 if (status == GOT_STATUS_OBSTRUCTED) {
1851 err = (*progress_cb)(progress_arg, status, path);
1854 if (status == GOT_STATUS_CONFLICT) {
1855 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1860 if (ie && status != GOT_STATUS_MISSING &&
1861 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1862 if (got_fileindex_entry_has_commit(ie) &&
1863 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1864 SHA1_DIGEST_LENGTH) == 0) {
1865 err = sync_timestamps(ondisk_path, status, ie, &sb);
1868 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1872 if (got_fileindex_entry_has_blob(ie) &&
1873 memcmp(ie->blob_sha1, te->id.sha1,
1874 SHA1_DIGEST_LENGTH) == 0) {
1875 err = sync_timestamps(ondisk_path, status, ie, &sb);
1880 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1884 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1885 int update_timestamps;
1886 struct got_blob_object *blob2 = NULL;
1887 char *label_orig = NULL;
1888 if (got_fileindex_entry_has_blob(ie)) {
1889 struct got_object_id id2;
1890 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1891 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1895 if (got_fileindex_entry_has_commit(ie)) {
1896 char id_str[SHA1_DIGEST_STRING_LENGTH];
1897 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1898 sizeof(id_str)) == NULL) {
1899 err = got_error_path(id_str,
1900 GOT_ERR_BAD_OBJ_ID_STR);
1903 if (asprintf(&label_orig, "%s: commit %s",
1904 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1905 err = got_error_from_errno("asprintf");
1909 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1911 err = got_object_blob_read_to_str(&link_target, blob);
1914 err = merge_symlink(worktree, blob2, ondisk_path, path,
1915 label_orig, link_target, worktree->base_commit_id,
1916 repo, progress_cb, progress_arg);
1919 err = merge_blob(&update_timestamps, worktree, blob2,
1920 ondisk_path, path, sb.st_mode, label_orig, blob,
1921 worktree->base_commit_id, repo,
1922 progress_cb, progress_arg);
1926 got_object_blob_close(blob2);
1930 * Do not update timestamps of files with local changes.
1931 * Otherwise, a future status walk would treat them as
1932 * unmodified files again.
1934 err = got_fileindex_entry_update(ie, ondisk_path,
1935 blob->id.sha1, worktree->base_commit_id->sha1,
1937 } else if (status == GOT_STATUS_MODE_CHANGE) {
1938 err = got_fileindex_entry_update(ie, ondisk_path,
1939 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1940 } else if (status == GOT_STATUS_DELETE) {
1941 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1944 err = got_fileindex_entry_update(ie, ondisk_path,
1945 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1949 int is_bad_symlink = 0;
1950 if (S_ISLNK(te->mode)) {
1951 err = install_symlink(&is_bad_symlink, worktree,
1952 ondisk_path, path, blob,
1953 status == GOT_STATUS_MISSING, 0,
1954 status == GOT_STATUS_UNVERSIONED, repo,
1955 progress_cb, progress_arg);
1957 err = install_blob(worktree, ondisk_path, path,
1958 te->mode, sb.st_mode, blob,
1959 status == GOT_STATUS_MISSING, 0, 0,
1960 status == GOT_STATUS_UNVERSIONED, repo,
1961 progress_cb, progress_arg);
1967 err = got_fileindex_entry_update(ie, ondisk_path,
1968 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1970 err = create_fileindex_entry(&ie, fileindex,
1971 worktree->base_commit_id, ondisk_path, path,
1977 if (is_bad_symlink) {
1978 got_fileindex_entry_filetype_set(ie,
1979 GOT_FILEIDX_MODE_BAD_SYMLINK);
1982 got_object_blob_close(blob);
1988 static const struct got_error *
1989 remove_ondisk_file(const char *root_path, const char *path)
1991 const struct got_error *err = NULL;
1992 char *ondisk_path = NULL;
1994 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1995 return got_error_from_errno("asprintf");
1997 if (unlink(ondisk_path) == -1) {
1998 if (errno != ENOENT)
1999 err = got_error_from_errno2("unlink", ondisk_path);
2001 char *parent = dirname(ondisk_path);
2002 while (parent && strcmp(parent, root_path) != 0) {
2003 if (rmdir(parent) == -1) {
2004 if (errno != ENOTEMPTY)
2005 err = got_error_from_errno2("rmdir",
2009 parent = dirname(parent);
2016 static const struct got_error *
2017 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2018 struct got_fileindex_entry *ie, struct got_repository *repo,
2019 got_worktree_checkout_cb progress_cb, void *progress_arg)
2021 const struct got_error *err = NULL;
2022 unsigned char status;
2026 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2027 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2029 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2031 return got_error_from_errno("asprintf");
2033 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2037 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2038 char ondisk_target[PATH_MAX];
2039 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2040 sizeof(ondisk_target));
2041 if (ondisk_len == -1) {
2042 err = got_error_from_errno2("readlink", ondisk_path);
2045 ondisk_target[ondisk_len] = '\0';
2046 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2047 NULL, NULL, /* XXX pass common ancestor info? */
2048 ondisk_target, ondisk_path);
2051 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2056 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2057 status == GOT_STATUS_ADD) {
2058 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2062 * Preserve the working file and change the deleted blob's
2063 * entry into a schedule-add entry.
2065 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2068 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2071 if (status == GOT_STATUS_NO_CHANGE) {
2072 err = remove_ondisk_file(worktree->root_path, ie->path);
2076 got_fileindex_entry_remove(fileindex, ie);
2083 struct diff_cb_arg {
2084 struct got_fileindex *fileindex;
2085 struct got_worktree *worktree;
2086 struct got_repository *repo;
2087 got_worktree_checkout_cb progress_cb;
2089 got_cancel_cb cancel_cb;
2093 static const struct got_error *
2094 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2095 struct got_tree_entry *te, const char *parent_path)
2097 struct diff_cb_arg *a = arg;
2099 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2100 return got_error(GOT_ERR_CANCELLED);
2102 return update_blob(a->worktree, a->fileindex, ie, te,
2103 ie->path, a->repo, a->progress_cb, a->progress_arg);
2106 static const struct got_error *
2107 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2109 struct diff_cb_arg *a = arg;
2111 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2112 return got_error(GOT_ERR_CANCELLED);
2114 return delete_blob(a->worktree, a->fileindex, ie,
2115 a->repo, a->progress_cb, a->progress_arg);
2118 static const struct got_error *
2119 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2121 struct diff_cb_arg *a = arg;
2122 const struct got_error *err;
2125 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2126 return got_error(GOT_ERR_CANCELLED);
2128 if (got_object_tree_entry_is_submodule(te))
2131 if (asprintf(&path, "%s%s%s", parent_path,
2132 parent_path[0] ? "/" : "", te->name)
2134 return got_error_from_errno("asprintf");
2136 if (S_ISDIR(te->mode))
2137 err = add_dir_on_disk(a->worktree, path);
2139 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2140 a->repo, a->progress_cb, a->progress_arg);
2146 static const struct got_error *
2147 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2149 const struct got_error *err = NULL;
2150 char *uuidstr = NULL;
2151 uint32_t uuid_status;
2155 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2156 if (uuid_status != uuid_s_ok)
2157 return got_error_uuid(uuid_status, "uuid_to_string");
2159 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2161 err = got_error_from_errno("asprintf");
2168 const struct got_error *
2169 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2171 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2174 static const struct got_error *
2175 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2177 return get_ref_name(refname, worktree,
2178 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2181 static const struct got_error *
2182 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2184 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2187 static const struct got_error *
2188 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2190 return get_ref_name(refname, worktree,
2191 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2194 static const struct got_error *
2195 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2197 return get_ref_name(refname, worktree,
2198 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2201 static const struct got_error *
2202 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2204 return get_ref_name(refname, worktree,
2205 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2208 static const struct got_error *
2209 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2211 return get_ref_name(refname, worktree,
2212 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2215 static const struct got_error *
2216 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2218 return get_ref_name(refname, worktree,
2219 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2222 static const struct got_error *
2223 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2225 return get_ref_name(refname, worktree,
2226 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2229 const struct got_error *
2230 got_worktree_get_histedit_script_path(char **path,
2231 struct got_worktree *worktree)
2233 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2234 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2236 return got_error_from_errno("asprintf");
2242 * Prevent Git's garbage collector from deleting our base commit by
2243 * setting a reference to our base commit's ID.
2245 static const struct got_error *
2246 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2248 const struct got_error *err = NULL;
2249 struct got_reference *ref = NULL;
2252 err = got_worktree_get_base_ref_name(&refname, worktree);
2256 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2260 err = got_ref_write(ref, repo);
2268 static const struct got_error *
2269 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2271 const struct got_error *err = NULL;
2273 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2274 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2275 err = got_error_from_errno("asprintf");
2276 *fileindex_path = NULL;
2282 static const struct got_error *
2283 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2284 struct got_worktree *worktree)
2286 const struct got_error *err = NULL;
2289 *fileindex_path = NULL;
2290 *fileindex = got_fileindex_alloc();
2291 if (*fileindex == NULL)
2292 return got_error_from_errno("got_fileindex_alloc");
2294 err = get_fileindex_path(fileindex_path, worktree);
2298 index = fopen(*fileindex_path, "rb");
2299 if (index == NULL) {
2300 if (errno != ENOENT)
2301 err = got_error_from_errno2("fopen", *fileindex_path);
2303 err = got_fileindex_read(*fileindex, index);
2304 if (fclose(index) != 0 && err == NULL)
2305 err = got_error_from_errno("fclose");
2309 free(*fileindex_path);
2310 *fileindex_path = NULL;
2311 got_fileindex_free(*fileindex);
2317 struct bump_base_commit_id_arg {
2318 struct got_object_id *base_commit_id;
2321 const char *entry_name;
2322 got_worktree_checkout_cb progress_cb;
2326 /* Bump base commit ID of all files within an updated part of the work tree. */
2327 static const struct got_error *
2328 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2330 const struct got_error *err;
2331 struct bump_base_commit_id_arg *a = arg;
2333 if (a->entry_name) {
2334 if (strcmp(ie->path, a->path) != 0)
2336 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2339 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2340 SHA1_DIGEST_LENGTH) == 0)
2343 if (a->progress_cb) {
2344 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2349 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2353 static const struct got_error *
2354 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2356 const struct got_error *err = NULL;
2357 char *new_fileindex_path = NULL;
2358 FILE *new_index = NULL;
2359 struct timespec timeout;
2361 err = got_opentemp_named(&new_fileindex_path, &new_index,
2366 err = got_fileindex_write(fileindex, new_index);
2370 if (rename(new_fileindex_path, fileindex_path) != 0) {
2371 err = got_error_from_errno3("rename", new_fileindex_path,
2373 unlink(new_fileindex_path);
2377 * Sleep for a short amount of time to ensure that files modified after
2378 * this program exits have a different time stamp from the one which
2379 * was recorded in the file index.
2382 timeout.tv_nsec = 1;
2383 nanosleep(&timeout, NULL);
2387 free(new_fileindex_path);
2391 static const struct got_error *
2392 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2393 struct got_object_id **tree_id, const char *wt_relpath,
2394 struct got_worktree *worktree, struct got_repository *repo)
2396 const struct got_error *err = NULL;
2397 struct got_object_id *id = NULL;
2398 char *in_repo_path = NULL;
2399 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2401 *entry_type = GOT_OBJ_TYPE_ANY;
2402 *tree_relpath = NULL;
2405 if (wt_relpath[0] == '\0') {
2406 /* Check out all files within the work tree. */
2407 *entry_type = GOT_OBJ_TYPE_TREE;
2408 *tree_relpath = strdup("");
2409 if (*tree_relpath == NULL) {
2410 err = got_error_from_errno("strdup");
2413 err = got_object_id_by_path(tree_id, repo,
2414 worktree->base_commit_id, worktree->path_prefix);
2420 /* Check out a subset of files in the work tree. */
2422 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2423 is_root_wt ? "" : "/", wt_relpath) == -1) {
2424 err = got_error_from_errno("asprintf");
2428 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2434 in_repo_path = NULL;
2436 err = got_object_get_type(entry_type, repo, id);
2440 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2441 /* Check out a single file. */
2442 if (strchr(wt_relpath, '/') == NULL) {
2443 /* Check out a single file in work tree's root dir. */
2444 in_repo_path = strdup(worktree->path_prefix);
2445 if (in_repo_path == NULL) {
2446 err = got_error_from_errno("strdup");
2449 *tree_relpath = strdup("");
2450 if (*tree_relpath == NULL) {
2451 err = got_error_from_errno("strdup");
2455 /* Check out a single file in a subdirectory. */
2456 err = got_path_dirname(tree_relpath, wt_relpath);
2459 if (asprintf(&in_repo_path, "%s%s%s",
2460 worktree->path_prefix, is_root_wt ? "" : "/",
2461 *tree_relpath) == -1) {
2462 err = got_error_from_errno("asprintf");
2466 err = got_object_id_by_path(tree_id, repo,
2467 worktree->base_commit_id, in_repo_path);
2469 /* Check out all files within a subdirectory. */
2470 *tree_id = got_object_id_dup(id);
2471 if (*tree_id == NULL) {
2472 err = got_error_from_errno("got_object_id_dup");
2475 *tree_relpath = strdup(wt_relpath);
2476 if (*tree_relpath == NULL) {
2477 err = got_error_from_errno("strdup");
2485 *entry_type = GOT_OBJ_TYPE_ANY;
2486 free(*tree_relpath);
2487 *tree_relpath = NULL;
2494 static const struct got_error *
2495 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2496 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2497 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2498 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2500 const struct got_error *err = NULL;
2501 struct got_commit_object *commit = NULL;
2502 struct got_tree_object *tree = NULL;
2503 struct got_fileindex_diff_tree_cb diff_cb;
2504 struct diff_cb_arg arg;
2506 err = ref_base_commit(worktree, repo);
2508 if (!(err->code == GOT_ERR_ERRNO &&
2509 (errno == EACCES || errno == EROFS)))
2511 err = (*progress_cb)(progress_arg,
2512 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2517 err = got_object_open_as_commit(&commit, repo,
2518 worktree->base_commit_id);
2522 err = got_object_open_as_tree(&tree, repo, tree_id);
2527 got_object_tree_find_entry(tree, entry_name) == NULL) {
2528 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2532 diff_cb.diff_old_new = diff_old_new;
2533 diff_cb.diff_old = diff_old;
2534 diff_cb.diff_new = diff_new;
2535 arg.fileindex = fileindex;
2536 arg.worktree = worktree;
2538 arg.progress_cb = progress_cb;
2539 arg.progress_arg = progress_arg;
2540 arg.cancel_cb = cancel_cb;
2541 arg.cancel_arg = cancel_arg;
2542 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2543 entry_name, repo, &diff_cb, &arg);
2546 got_object_tree_close(tree);
2548 got_object_commit_close(commit);
2552 const struct got_error *
2553 got_worktree_checkout_files(struct got_worktree *worktree,
2554 struct got_pathlist_head *paths, struct got_repository *repo,
2555 got_worktree_checkout_cb progress_cb, void *progress_arg,
2556 got_cancel_cb cancel_cb, void *cancel_arg)
2558 const struct got_error *err = NULL, *sync_err, *unlockerr;
2559 struct got_commit_object *commit = NULL;
2560 struct got_tree_object *tree = NULL;
2561 struct got_fileindex *fileindex = NULL;
2562 char *fileindex_path = NULL;
2563 struct got_pathlist_entry *pe;
2564 struct tree_path_data {
2565 SIMPLEQ_ENTRY(tree_path_data) entry;
2566 struct got_object_id *tree_id;
2571 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2573 SIMPLEQ_INIT(&tree_paths);
2575 err = lock_worktree(worktree, LOCK_EX);
2579 /* Map all specified paths to in-repository trees. */
2580 TAILQ_FOREACH(pe, paths, entry) {
2581 tpd = malloc(sizeof(*tpd));
2583 err = got_error_from_errno("malloc");
2587 err = find_tree_entry_for_checkout(&tpd->entry_type,
2588 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2594 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2595 err = got_path_basename(&tpd->entry_name, pe->path);
2603 tpd->entry_name = NULL;
2605 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2609 * Read the file index.
2610 * Checking out files is supposed to be an idempotent operation.
2611 * If the on-disk file index is incomplete we will try to complete it.
2613 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2617 tpd = SIMPLEQ_FIRST(&tree_paths);
2618 TAILQ_FOREACH(pe, paths, entry) {
2619 struct bump_base_commit_id_arg bbc_arg;
2621 err = checkout_files(worktree, fileindex, tpd->relpath,
2622 tpd->tree_id, tpd->entry_name, repo,
2623 progress_cb, progress_arg, cancel_cb, cancel_arg);
2627 bbc_arg.base_commit_id = worktree->base_commit_id;
2628 bbc_arg.entry_name = tpd->entry_name;
2629 bbc_arg.path = pe->path;
2630 bbc_arg.path_len = pe->path_len;
2631 bbc_arg.progress_cb = progress_cb;
2632 bbc_arg.progress_arg = progress_arg;
2633 err = got_fileindex_for_each_entry_safe(fileindex,
2634 bump_base_commit_id, &bbc_arg);
2638 tpd = SIMPLEQ_NEXT(tpd, entry);
2640 sync_err = sync_fileindex(fileindex, fileindex_path);
2641 if (sync_err && err == NULL)
2644 free(fileindex_path);
2646 got_object_tree_close(tree);
2648 got_object_commit_close(commit);
2650 got_fileindex_free(fileindex);
2651 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2652 tpd = SIMPLEQ_FIRST(&tree_paths);
2653 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2658 unlockerr = lock_worktree(worktree, LOCK_SH);
2659 if (unlockerr && err == NULL)
2664 struct merge_file_cb_arg {
2665 struct got_worktree *worktree;
2666 struct got_fileindex *fileindex;
2667 got_worktree_checkout_cb progress_cb;
2669 got_cancel_cb cancel_cb;
2671 const char *label_orig;
2672 struct got_object_id *commit_id2;
2675 static const struct got_error *
2676 merge_file_cb(void *arg, struct got_blob_object *blob1,
2677 struct got_blob_object *blob2, struct got_object_id *id1,
2678 struct got_object_id *id2, const char *path1, const char *path2,
2679 mode_t mode1, mode_t mode2, struct got_repository *repo)
2681 static const struct got_error *err = NULL;
2682 struct merge_file_cb_arg *a = arg;
2683 struct got_fileindex_entry *ie;
2684 char *ondisk_path = NULL;
2686 unsigned char status;
2687 int local_changes_subsumed;
2689 if (blob1 && blob2) {
2690 ie = got_fileindex_entry_get(a->fileindex, path2,
2693 return (*a->progress_cb)(a->progress_arg,
2694 GOT_STATUS_MISSING, path2);
2696 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2698 return got_error_from_errno("asprintf");
2700 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2705 if (status == GOT_STATUS_DELETE) {
2706 err = (*a->progress_cb)(a->progress_arg,
2707 GOT_STATUS_MERGE, path2);
2710 if (status != GOT_STATUS_NO_CHANGE &&
2711 status != GOT_STATUS_MODIFY &&
2712 status != GOT_STATUS_CONFLICT &&
2713 status != GOT_STATUS_ADD) {
2714 err = (*a->progress_cb)(a->progress_arg, status, path2);
2718 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2720 err = got_object_blob_read_to_str(&link_target2, blob2);
2723 err = merge_symlink(a->worktree, blob1, ondisk_path,
2724 path2, a->label_orig, link_target2, a->commit_id2,
2725 repo, a->progress_cb, a->progress_arg);
2728 err = merge_blob(&local_changes_subsumed, a->worktree,
2729 blob1, ondisk_path, path2, sb.st_mode,
2730 a->label_orig, blob2, a->commit_id2, repo,
2731 a->progress_cb, a->progress_arg);
2734 ie = got_fileindex_entry_get(a->fileindex, path1,
2737 return (*a->progress_cb)(a->progress_arg,
2738 GOT_STATUS_MISSING, path1);
2740 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2742 return got_error_from_errno("asprintf");
2744 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2750 case GOT_STATUS_NO_CHANGE:
2751 err = (*a->progress_cb)(a->progress_arg,
2752 GOT_STATUS_DELETE, path1);
2755 err = remove_ondisk_file(a->worktree->root_path, path1);
2759 got_fileindex_entry_mark_deleted_from_disk(ie);
2761 case GOT_STATUS_DELETE:
2762 case GOT_STATUS_MISSING:
2763 err = (*a->progress_cb)(a->progress_arg,
2764 GOT_STATUS_DELETE, path1);
2768 got_fileindex_entry_mark_deleted_from_disk(ie);
2770 case GOT_STATUS_ADD:
2771 case GOT_STATUS_MODIFY:
2772 case GOT_STATUS_CONFLICT:
2773 err = (*a->progress_cb)(a->progress_arg,
2774 GOT_STATUS_CANNOT_DELETE, path1);
2778 case GOT_STATUS_OBSTRUCTED:
2779 err = (*a->progress_cb)(a->progress_arg, status, path1);
2787 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2789 return got_error_from_errno("asprintf");
2790 ie = got_fileindex_entry_get(a->fileindex, path2,
2793 err = get_file_status(&status, &sb, ie, ondisk_path,
2797 if (status != GOT_STATUS_NO_CHANGE &&
2798 status != GOT_STATUS_MODIFY &&
2799 status != GOT_STATUS_CONFLICT &&
2800 status != GOT_STATUS_ADD) {
2801 err = (*a->progress_cb)(a->progress_arg,
2805 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2807 err = got_object_blob_read_to_str(&link_target2,
2811 err = merge_symlink(a->worktree, NULL,
2812 ondisk_path, path2, a->label_orig,
2813 link_target2, a->commit_id2, repo,
2814 a->progress_cb, a->progress_arg);
2816 } else if (S_ISREG(sb.st_mode)) {
2817 err = merge_blob(&local_changes_subsumed,
2818 a->worktree, NULL, ondisk_path, path2,
2819 sb.st_mode, a->label_orig, blob2,
2820 a->commit_id2, repo, a->progress_cb,
2823 err = got_error_path(ondisk_path,
2824 GOT_ERR_FILE_OBSTRUCTED);
2828 if (status == GOT_STATUS_DELETE) {
2829 err = got_fileindex_entry_update(ie,
2830 ondisk_path, blob2->id.sha1,
2831 a->worktree->base_commit_id->sha1, 0);
2836 int is_bad_symlink = 0;
2837 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2838 if (S_ISLNK(mode2)) {
2839 err = install_symlink(&is_bad_symlink,
2840 a->worktree, ondisk_path, path2, blob2, 0,
2841 0, 1, repo, a->progress_cb, a->progress_arg);
2843 err = install_blob(a->worktree, ondisk_path, path2,
2844 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2845 a->progress_cb, a->progress_arg);
2849 err = got_fileindex_entry_alloc(&ie, path2);
2852 err = got_fileindex_entry_update(ie, ondisk_path,
2855 got_fileindex_entry_free(ie);
2858 err = got_fileindex_entry_add(a->fileindex, ie);
2860 got_fileindex_entry_free(ie);
2863 if (is_bad_symlink) {
2864 got_fileindex_entry_filetype_set(ie,
2865 GOT_FILEIDX_MODE_BAD_SYMLINK);
2874 struct check_merge_ok_arg {
2875 struct got_worktree *worktree;
2876 struct got_repository *repo;
2879 static const struct got_error *
2880 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2882 const struct got_error *err = NULL;
2883 struct check_merge_ok_arg *a = arg;
2884 unsigned char status;
2888 /* Reject merges into a work tree with mixed base commits. */
2889 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2890 SHA1_DIGEST_LENGTH))
2891 return got_error(GOT_ERR_MIXED_COMMITS);
2893 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2895 return got_error_from_errno("asprintf");
2897 /* Reject merges into a work tree with conflicted files. */
2898 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2901 if (status == GOT_STATUS_CONFLICT)
2902 return got_error(GOT_ERR_CONFLICTS);
2907 static const struct got_error *
2908 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2909 const char *fileindex_path, struct got_object_id *commit_id1,
2910 struct got_object_id *commit_id2, struct got_repository *repo,
2911 got_worktree_checkout_cb progress_cb, void *progress_arg,
2912 got_cancel_cb cancel_cb, void *cancel_arg)
2914 const struct got_error *err = NULL, *sync_err;
2915 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2916 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2917 struct merge_file_cb_arg arg;
2918 char *label_orig = NULL;
2923 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2924 worktree->path_prefix);
2928 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2932 err = got_object_id_str(&id_str, commit_id1);
2936 if (asprintf(&label_orig, "%s: commit %s",
2937 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2938 err = got_error_from_errno("asprintf");
2945 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2946 worktree->path_prefix);
2950 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2954 arg.worktree = worktree;
2955 arg.fileindex = fileindex;
2956 arg.progress_cb = progress_cb;
2957 arg.progress_arg = progress_arg;
2958 arg.cancel_cb = cancel_cb;
2959 arg.cancel_arg = cancel_arg;
2960 arg.label_orig = label_orig;
2961 arg.commit_id2 = commit_id2;
2962 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2963 sync_err = sync_fileindex(fileindex, fileindex_path);
2964 if (sync_err && err == NULL)
2968 got_object_tree_close(tree1);
2970 got_object_tree_close(tree2);
2975 const struct got_error *
2976 got_worktree_merge_files(struct got_worktree *worktree,
2977 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2978 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2979 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2981 const struct got_error *err, *unlockerr;
2982 char *fileindex_path = NULL;
2983 struct got_fileindex *fileindex = NULL;
2984 struct check_merge_ok_arg mok_arg;
2986 err = lock_worktree(worktree, LOCK_EX);
2990 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2994 mok_arg.worktree = worktree;
2995 mok_arg.repo = repo;
2996 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3001 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3002 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3005 got_fileindex_free(fileindex);
3006 free(fileindex_path);
3007 unlockerr = lock_worktree(worktree, LOCK_SH);
3008 if (unlockerr && err == NULL)
3013 struct diff_dir_cb_arg {
3014 struct got_fileindex *fileindex;
3015 struct got_worktree *worktree;
3016 const char *status_path;
3017 size_t status_path_len;
3018 struct got_repository *repo;
3019 got_worktree_status_cb status_cb;
3021 got_cancel_cb cancel_cb;
3023 /* A pathlist containing per-directory pathlists of ignore patterns. */
3024 struct got_pathlist_head ignores;
3025 int report_unchanged;
3029 static const struct got_error *
3030 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3031 int dirfd, const char *de_name,
3032 got_worktree_status_cb status_cb, void *status_arg,
3033 struct got_repository *repo, int report_unchanged)
3035 const struct got_error *err = NULL;
3036 unsigned char status = GOT_STATUS_NO_CHANGE;
3037 unsigned char staged_status = get_staged_status(ie);
3039 struct got_object_id blob_id, commit_id, staged_blob_id;
3040 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3041 struct got_object_id *staged_blob_idp = NULL;
3043 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3047 if (status == GOT_STATUS_NO_CHANGE &&
3048 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3051 if (got_fileindex_entry_has_blob(ie)) {
3052 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3053 blob_idp = &blob_id;
3055 if (got_fileindex_entry_has_commit(ie)) {
3056 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3057 commit_idp = &commit_id;
3059 if (staged_status == GOT_STATUS_ADD ||
3060 staged_status == GOT_STATUS_MODIFY) {
3061 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3062 SHA1_DIGEST_LENGTH);
3063 staged_blob_idp = &staged_blob_id;
3066 return (*status_cb)(status_arg, status, staged_status,
3067 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3070 static const struct got_error *
3071 status_old_new(void *arg, struct got_fileindex_entry *ie,
3072 struct dirent *de, const char *parent_path, int dirfd)
3074 const struct got_error *err = NULL;
3075 struct diff_dir_cb_arg *a = arg;
3078 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3079 return got_error(GOT_ERR_CANCELLED);
3081 if (got_path_cmp(parent_path, a->status_path,
3082 strlen(parent_path), a->status_path_len) != 0 &&
3083 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3086 if (parent_path[0]) {
3087 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3088 parent_path, de->d_name) == -1)
3089 return got_error_from_errno("asprintf");
3091 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3093 return got_error_from_errno("asprintf");
3096 err = report_file_status(ie, abspath, dirfd, de->d_name,
3097 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3102 static const struct got_error *
3103 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3105 struct diff_dir_cb_arg *a = arg;
3106 struct got_object_id blob_id, commit_id;
3107 unsigned char status;
3109 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3110 return got_error(GOT_ERR_CANCELLED);
3112 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3115 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3116 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3117 if (got_fileindex_entry_has_file_on_disk(ie))
3118 status = GOT_STATUS_MISSING;
3120 status = GOT_STATUS_DELETE;
3121 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3122 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3126 free_ignorelist(struct got_pathlist_head *ignorelist)
3128 struct got_pathlist_entry *pe;
3130 TAILQ_FOREACH(pe, ignorelist, entry)
3131 free((char *)pe->path);
3132 got_pathlist_free(ignorelist);
3136 free_ignores(struct got_pathlist_head *ignores)
3138 struct got_pathlist_entry *pe;
3140 TAILQ_FOREACH(pe, ignores, entry) {
3141 struct got_pathlist_head *ignorelist = pe->data;
3142 free_ignorelist(ignorelist);
3143 free((char *)pe->path);
3145 got_pathlist_free(ignores);
3148 static const struct got_error *
3149 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3151 const struct got_error *err = NULL;
3152 struct got_pathlist_entry *pe = NULL;
3153 struct got_pathlist_head *ignorelist;
3154 char *line = NULL, *pattern, *dirpath = NULL;
3155 size_t linesize = 0;
3158 ignorelist = calloc(1, sizeof(*ignorelist));
3159 if (ignorelist == NULL)
3160 return got_error_from_errno("calloc");
3161 TAILQ_INIT(ignorelist);
3163 while ((linelen = getline(&line, &linesize, f)) != -1) {
3164 if (linelen > 0 && line[linelen - 1] == '\n')
3165 line[linelen - 1] = '\0';
3167 /* Git's ignores may contain comments. */
3171 /* Git's negated patterns are not (yet?) supported. */
3175 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3177 err = got_error_from_errno("asprintf");
3180 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3185 err = got_error_from_errno("getline");
3189 dirpath = strdup(path);
3190 if (dirpath == NULL) {
3191 err = got_error_from_errno("strdup");
3194 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3197 if (err || pe == NULL) {
3199 free_ignorelist(ignorelist);
3205 match_ignores(struct got_pathlist_head *ignores, const char *path)
3207 struct got_pathlist_entry *pe;
3209 /* Handle patterns which match in all directories. */
3210 TAILQ_FOREACH(pe, ignores, entry) {
3211 struct got_pathlist_head *ignorelist = pe->data;
3212 struct got_pathlist_entry *pi;
3214 TAILQ_FOREACH(pi, ignorelist, entry) {
3215 const char *p, *pattern = pi->path;
3217 if (strncmp(pattern, "**/", 3) != 0)
3222 if (fnmatch(pattern, p,
3223 FNM_PATHNAME | FNM_LEADING_DIR)) {
3224 /* Retry in next directory. */
3225 while (*p && *p != '/')
3237 * The ignores pathlist contains ignore lists from children before
3238 * parents, so we can find the most specific ignorelist by walking
3239 * ignores backwards.
3241 pe = TAILQ_LAST(ignores, got_pathlist_head);
3243 if (got_path_is_child(path, pe->path, pe->path_len)) {
3244 struct got_pathlist_head *ignorelist = pe->data;
3245 struct got_pathlist_entry *pi;
3246 TAILQ_FOREACH(pi, ignorelist, entry) {
3247 const char *pattern = pi->path;
3248 int flags = FNM_LEADING_DIR;
3249 if (strstr(pattern, "/**/") == NULL)
3250 flags |= FNM_PATHNAME;
3251 if (fnmatch(pattern, path, flags))
3256 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3262 static const struct got_error *
3263 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3264 const char *path, int dirfd, const char *ignores_filename)
3266 const struct got_error *err = NULL;
3269 FILE *ignoresfile = NULL;
3271 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3272 path[0] ? "/" : "", ignores_filename) == -1)
3273 return got_error_from_errno("asprintf");
3276 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3278 if (errno != ENOENT && errno != EACCES)
3279 err = got_error_from_errno2("openat",
3282 ignoresfile = fdopen(fd, "r");
3283 if (ignoresfile == NULL)
3284 err = got_error_from_errno2("fdopen",
3288 err = read_ignores(ignores, path, ignoresfile);
3292 ignoresfile = fopen(ignorespath, "r");
3293 if (ignoresfile == NULL) {
3294 if (errno != ENOENT && errno != EACCES)
3295 err = got_error_from_errno2("fopen",
3298 err = read_ignores(ignores, path, ignoresfile);
3301 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3302 err = got_error_from_errno2("fclose", path);
3303 if (fd != -1 && close(fd) == -1 && err == NULL)
3304 err = got_error_from_errno2("close", path);
3309 static const struct got_error *
3310 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3312 const struct got_error *err = NULL;
3313 struct diff_dir_cb_arg *a = arg;
3316 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3317 return got_error(GOT_ERR_CANCELLED);
3319 if (parent_path[0]) {
3320 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3321 return got_error_from_errno("asprintf");
3326 if (de->d_type != DT_DIR &&
3327 got_path_is_child(path, a->status_path, a->status_path_len)
3328 && !match_ignores(&a->ignores, path))
3329 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3330 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3336 static const struct got_error *
3337 status_traverse(void *arg, const char *path, int dirfd)
3339 const struct got_error *err = NULL;
3340 struct diff_dir_cb_arg *a = arg;
3345 err = add_ignores(&a->ignores, a->worktree->root_path,
3346 path, dirfd, ".cvsignore");
3350 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3351 dirfd, ".gitignore");
3356 static const struct got_error *
3357 report_single_file_status(const char *path, const char *ondisk_path,
3358 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3359 void *status_arg, struct got_repository *repo, int report_unchanged)
3361 struct got_fileindex_entry *ie;
3364 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3366 return report_file_status(ie, ondisk_path, -1, NULL,
3367 status_cb, status_arg, repo, report_unchanged);
3369 if (lstat(ondisk_path, &sb) == -1) {
3370 if (errno != ENOENT)
3371 return got_error_from_errno2("lstat", ondisk_path);
3372 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3373 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3377 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3378 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3379 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3384 static const struct got_error *
3385 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3386 const char *root_path, const char *path)
3388 const struct got_error *err;
3389 char *parent_path, *next_parent_path = NULL;
3391 err = add_ignores(ignores, root_path, "", -1,
3396 err = add_ignores(ignores, root_path, "", -1,
3401 err = got_path_dirname(&parent_path, path);
3403 if (err->code == GOT_ERR_BAD_PATH)
3404 return NULL; /* cannot traverse parent */
3408 err = add_ignores(ignores, root_path, parent_path, -1,
3412 err = add_ignores(ignores, root_path, parent_path, -1,
3416 err = got_path_dirname(&next_parent_path, parent_path);
3418 if (err->code == GOT_ERR_BAD_PATH)
3419 err = NULL; /* traversed everything */
3423 parent_path = next_parent_path;
3424 next_parent_path = NULL;
3428 free(next_parent_path);
3432 static const struct got_error *
3433 worktree_status(struct got_worktree *worktree, const char *path,
3434 struct got_fileindex *fileindex, struct got_repository *repo,
3435 got_worktree_status_cb status_cb, void *status_arg,
3436 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3437 int report_unchanged)
3439 const struct got_error *err = NULL;
3441 struct got_fileindex_diff_dir_cb fdiff_cb;
3442 struct diff_dir_cb_arg arg;
3443 char *ondisk_path = NULL;
3445 TAILQ_INIT(&arg.ignores);
3447 if (asprintf(&ondisk_path, "%s%s%s",
3448 worktree->root_path, path[0] ? "/" : "", path) == -1)
3449 return got_error_from_errno("asprintf");
3451 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3453 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3455 err = got_error_from_errno2("open", ondisk_path);
3457 err = report_single_file_status(path, ondisk_path,
3458 fileindex, status_cb, status_arg, repo,
3461 fdiff_cb.diff_old_new = status_old_new;
3462 fdiff_cb.diff_old = status_old;
3463 fdiff_cb.diff_new = status_new;
3464 fdiff_cb.diff_traverse = status_traverse;
3465 arg.fileindex = fileindex;
3466 arg.worktree = worktree;
3467 arg.status_path = path;
3468 arg.status_path_len = strlen(path);
3470 arg.status_cb = status_cb;
3471 arg.status_arg = status_arg;
3472 arg.cancel_cb = cancel_cb;
3473 arg.cancel_arg = cancel_arg;
3474 arg.report_unchanged = report_unchanged;
3475 arg.no_ignores = no_ignores;
3477 err = add_ignores_from_parent_paths(&arg.ignores,
3478 worktree->root_path, path);
3482 err = got_fileindex_diff_dir(fileindex, fd,
3483 worktree->root_path, path, repo, &fdiff_cb, &arg);
3486 free_ignores(&arg.ignores);
3487 if (fd != -1 && close(fd) != 0 && err == NULL)
3488 err = got_error_from_errno("close");
3493 const struct got_error *
3494 got_worktree_status(struct got_worktree *worktree,
3495 struct got_pathlist_head *paths, struct got_repository *repo,
3496 got_worktree_status_cb status_cb, void *status_arg,
3497 got_cancel_cb cancel_cb, void *cancel_arg)
3499 const struct got_error *err = NULL;
3500 char *fileindex_path = NULL;
3501 struct got_fileindex *fileindex = NULL;
3502 struct got_pathlist_entry *pe;
3504 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3508 TAILQ_FOREACH(pe, paths, entry) {
3509 err = worktree_status(worktree, pe->path, fileindex, repo,
3510 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3514 free(fileindex_path);
3515 got_fileindex_free(fileindex);
3519 const struct got_error *
3520 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3523 const struct got_error *err = NULL;
3524 char *resolved = NULL, *cwd = NULL, *path = NULL;
3530 cwd = getcwd(NULL, 0);
3532 return got_error_from_errno("getcwd");
3534 if (lstat(arg, &sb) == -1) {
3535 if (errno != ENOENT) {
3536 err = got_error_from_errno2("lstat", arg);
3540 if (S_ISLNK(sb.st_mode)) {
3542 * We cannot use realpath(3) with symlinks since we want to
3543 * operate on the symlink itself.
3544 * But we can make the path absolute, assuming it is relative
3545 * to the current working directory, and then canonicalize it.
3547 char *abspath = NULL;
3548 char canonpath[PATH_MAX];
3549 if (!got_path_is_absolute(arg)) {
3550 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3551 err = got_error_from_errno("asprintf");
3556 err = got_canonpath(abspath ? abspath : arg, canonpath,
3560 resolved = strdup(canonpath);
3561 if (resolved == NULL) {
3562 err = got_error_from_errno("strdup");
3566 resolved = realpath(arg, NULL);
3567 if (resolved == NULL) {
3568 if (errno != ENOENT) {
3569 err = got_error_from_errno2("realpath", arg);
3572 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3573 err = got_error_from_errno("asprintf");
3579 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3580 strlen(got_worktree_get_root_path(worktree)))) {
3581 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3585 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3586 err = got_path_skip_common_ancestor(&path,
3587 got_worktree_get_root_path(worktree), resolved);
3593 err = got_error_from_errno("strdup");
3598 /* XXX status walk can't deal with trailing slash! */
3600 while (len > 0 && path[len - 1] == '/') {
3601 path[len - 1] = '\0';
3614 struct schedule_addition_args {
3615 struct got_worktree *worktree;
3616 struct got_fileindex *fileindex;
3617 got_worktree_checkout_cb progress_cb;
3619 struct got_repository *repo;
3622 static const struct got_error *
3623 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3624 const char *relpath, struct got_object_id *blob_id,
3625 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3626 int dirfd, const char *de_name)
3628 struct schedule_addition_args *a = arg;
3629 const struct got_error *err = NULL;
3630 struct got_fileindex_entry *ie;
3634 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3636 return got_error_from_errno("asprintf");
3638 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3640 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3644 /* Re-adding an existing entry is a no-op. */
3645 if (status == GOT_STATUS_ADD)
3647 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3652 if (status != GOT_STATUS_UNVERSIONED) {
3653 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3657 err = got_fileindex_entry_alloc(&ie, relpath);
3660 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3662 got_fileindex_entry_free(ie);
3665 err = got_fileindex_entry_add(a->fileindex, ie);
3667 got_fileindex_entry_free(ie);
3674 if (status == GOT_STATUS_ADD)
3676 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3679 const struct got_error *
3680 got_worktree_schedule_add(struct got_worktree *worktree,
3681 struct got_pathlist_head *paths,
3682 got_worktree_checkout_cb progress_cb, void *progress_arg,
3683 struct got_repository *repo, int no_ignores)
3685 struct got_fileindex *fileindex = NULL;
3686 char *fileindex_path = NULL;
3687 const struct got_error *err = NULL, *sync_err, *unlockerr;
3688 struct got_pathlist_entry *pe;
3689 struct schedule_addition_args saa;
3691 err = lock_worktree(worktree, LOCK_EX);
3695 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3699 saa.worktree = worktree;
3700 saa.fileindex = fileindex;
3701 saa.progress_cb = progress_cb;
3702 saa.progress_arg = progress_arg;
3705 TAILQ_FOREACH(pe, paths, entry) {
3706 err = worktree_status(worktree, pe->path, fileindex, repo,
3707 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3711 sync_err = sync_fileindex(fileindex, fileindex_path);
3712 if (sync_err && err == NULL)
3715 free(fileindex_path);
3717 got_fileindex_free(fileindex);
3718 unlockerr = lock_worktree(worktree, LOCK_SH);
3719 if (unlockerr && err == NULL)
3724 struct schedule_deletion_args {
3725 struct got_worktree *worktree;
3726 struct got_fileindex *fileindex;
3727 got_worktree_delete_cb progress_cb;
3729 struct got_repository *repo;
3730 int delete_local_mods;
3734 static const struct got_error *
3735 schedule_for_deletion(void *arg, unsigned char status,
3736 unsigned char staged_status, const char *relpath,
3737 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3738 struct got_object_id *commit_id, int dirfd, const char *de_name)
3740 struct schedule_deletion_args *a = arg;
3741 const struct got_error *err = NULL;
3742 struct got_fileindex_entry *ie = NULL;
3744 char *ondisk_path, *parent = NULL;
3746 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3748 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3750 staged_status = get_staged_status(ie);
3751 if (staged_status != GOT_STATUS_NO_CHANGE) {
3752 if (staged_status == GOT_STATUS_DELETE)
3754 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3757 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3759 return got_error_from_errno("asprintf");
3761 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3766 if (status != GOT_STATUS_NO_CHANGE) {
3767 if (status == GOT_STATUS_DELETE)
3769 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3770 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3773 if (status != GOT_STATUS_MODIFY &&
3774 status != GOT_STATUS_MISSING) {
3775 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3780 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3782 if (unlinkat(dirfd, de_name, 0) != 0) {
3783 err = got_error_from_errno2("unlinkat",
3787 } else if (unlink(ondisk_path) != 0) {
3788 err = got_error_from_errno2("unlink", ondisk_path);
3792 parent = dirname(ondisk_path);
3794 if (parent == NULL) {
3795 err = got_error_from_errno2("dirname", ondisk_path);
3798 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3799 if (rmdir(parent) == -1) {
3800 if (errno != ENOTEMPTY)
3801 err = got_error_from_errno2("rmdir",
3805 parent = dirname(parent);
3806 if (parent == NULL) {
3807 err = got_error_from_errno2("dirname", parent);
3813 got_fileindex_entry_mark_deleted_from_disk(ie);
3818 if (status == GOT_STATUS_DELETE)
3820 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3821 staged_status, relpath);
3824 const struct got_error *
3825 got_worktree_schedule_delete(struct got_worktree *worktree,
3826 struct got_pathlist_head *paths, int delete_local_mods,
3827 got_worktree_delete_cb progress_cb, void *progress_arg,
3828 struct got_repository *repo, int keep_on_disk)
3830 struct got_fileindex *fileindex = NULL;
3831 char *fileindex_path = NULL;
3832 const struct got_error *err = NULL, *sync_err, *unlockerr;
3833 struct got_pathlist_entry *pe;
3834 struct schedule_deletion_args sda;
3836 err = lock_worktree(worktree, LOCK_EX);
3840 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3844 sda.worktree = worktree;
3845 sda.fileindex = fileindex;
3846 sda.progress_cb = progress_cb;
3847 sda.progress_arg = progress_arg;
3849 sda.delete_local_mods = delete_local_mods;
3850 sda.keep_on_disk = keep_on_disk;
3852 TAILQ_FOREACH(pe, paths, entry) {
3853 err = worktree_status(worktree, pe->path, fileindex, repo,
3854 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3858 sync_err = sync_fileindex(fileindex, fileindex_path);
3859 if (sync_err && err == NULL)
3862 free(fileindex_path);
3864 got_fileindex_free(fileindex);
3865 unlockerr = lock_worktree(worktree, LOCK_SH);
3866 if (unlockerr && err == NULL)
3871 static const struct got_error *
3872 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3874 const struct got_error *err = NULL;
3876 size_t linesize = 0, n;
3879 linelen = getline(&line, &linesize, infile);
3880 if (linelen == -1) {
3881 if (ferror(infile)) {
3882 err = got_error_from_errno("getline");
3888 n = fwrite(line, 1, linelen, outfile);
3890 err = got_ferror(outfile, GOT_ERR_IO);
3895 n = fwrite(line, 1, linelen, rejectfile);
3897 err = got_ferror(outfile, GOT_ERR_IO);
3904 static const struct got_error *
3905 skip_one_line(FILE *f)
3908 size_t linesize = 0;
3911 linelen = getline(&line, &linesize, f);
3912 if (linelen == -1) {
3914 return got_error_from_errno("getline");
3921 static const struct got_error *
3922 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3923 int start_old, int end_old, int start_new, int end_new,
3924 FILE *outfile, FILE *rejectfile)
3926 const struct got_error *err;
3928 /* Copy old file's lines leading up to patch. */
3929 while (!feof(f1) && *line_cur1 < start_old) {
3930 err = copy_one_line(f1, outfile, NULL);
3935 /* Skip new file's lines leading up to patch. */
3936 while (!feof(f2) && *line_cur2 < start_new) {
3938 err = copy_one_line(f2, NULL, rejectfile);
3940 err = skip_one_line(f2);
3945 /* Copy patched lines. */
3946 while (!feof(f2) && *line_cur2 <= end_new) {
3947 err = copy_one_line(f2, outfile, NULL);
3952 /* Skip over old file's replaced lines. */
3953 while (!feof(f1) && *line_cur1 <= end_old) {
3955 err = copy_one_line(f1, NULL, rejectfile);
3957 err = skip_one_line(f1);
3966 static const struct got_error *
3967 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3968 FILE *outfile, FILE *rejectfile)
3970 const struct got_error *err;
3973 /* Copy old file's lines until EOF. */
3975 err = copy_one_line(f1, outfile, NULL);
3982 /* Copy new file's lines until EOF. */
3984 err = copy_one_line(f2, NULL, rejectfile);
3994 static const struct got_error *
3995 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3996 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3997 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3998 int *line_cur2, FILE *outfile, FILE *rejectfile,
3999 got_worktree_patch_cb patch_cb, void *patch_arg)
4001 const struct got_error *err = NULL;
4002 int start_old = change->cv.a;
4003 int end_old = change->cv.b;
4004 int start_new = change->cv.c;
4005 int end_new = change->cv.d;
4009 *choice = GOT_PATCH_CHOICE_NONE;
4011 hunkfile = got_opentemp();
4012 if (hunkfile == NULL)
4013 return got_error_from_errno("got_opentemp");
4018 /* XXX TODO needs error checking */
4019 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4021 if (fseek(f1, pos1, SEEK_SET) == -1) {
4022 err = got_ferror(f1, GOT_ERR_IO);
4025 if (fseek(f2, pos2, SEEK_SET) == -1) {
4026 err = got_ferror(f1, GOT_ERR_IO);
4029 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4030 err = got_ferror(hunkfile, GOT_ERR_IO);
4034 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4035 hunkfile, n, nchanges);
4040 case GOT_PATCH_CHOICE_YES:
4041 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4042 end_old, start_new, end_new, outfile, rejectfile);
4044 case GOT_PATCH_CHOICE_NO:
4045 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4046 end_old, start_new, end_new, rejectfile, outfile);
4048 case GOT_PATCH_CHOICE_QUIT:
4051 err = got_error(GOT_ERR_PATCH_CHOICE);
4055 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4056 err = got_error_from_errno("fclose");
4060 struct revert_file_args {
4061 struct got_worktree *worktree;
4062 struct got_fileindex *fileindex;
4063 got_worktree_checkout_cb progress_cb;
4065 got_worktree_patch_cb patch_cb;
4067 struct got_repository *repo;
4070 static const struct got_error *
4071 create_patched_content(char **path_outfile, int reverse_patch,
4072 struct got_object_id *blob_id, const char *path2,
4073 int dirfd2, const char *de_name2,
4074 const char *relpath, struct got_repository *repo,
4075 got_worktree_patch_cb patch_cb, void *patch_arg)
4077 const struct got_error *err;
4078 struct got_blob_object *blob = NULL;
4079 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4081 char link_target[PATH_MAX];
4082 ssize_t link_len = 0;
4083 char *path1 = NULL, *id_str = NULL;
4084 struct stat sb1, sb2;
4085 struct got_diff_changes *changes = NULL;
4086 struct got_diff_state *ds = NULL;
4087 struct got_diff_args *args = NULL;
4088 struct got_diff_change *change;
4089 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4092 *path_outfile = NULL;
4094 err = got_object_id_str(&id_str, blob_id);
4099 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4101 if (errno != ELOOP) {
4102 err = got_error_from_errno2("openat", path2);
4105 link_len = readlinkat(dirfd2, de_name2,
4106 link_target, sizeof(link_target));
4108 return got_error_from_errno2("readlinkat", path2);
4109 sb2.st_mode = S_IFLNK;
4110 sb2.st_size = link_len;
4113 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4115 if (errno != ELOOP) {
4116 err = got_error_from_errno2("open", path2);
4119 link_len = readlink(path2, link_target,
4120 sizeof(link_target));
4122 return got_error_from_errno2("readlink", path2);
4123 sb2.st_mode = S_IFLNK;
4124 sb2.st_size = link_len;
4128 if (fstat(fd2, &sb2) == -1) {
4129 err = got_error_from_errno2("fstat", path2);
4133 f2 = fdopen(fd2, "r");
4135 err = got_error_from_errno2("fdopen", path2);
4141 f2 = got_opentemp();
4143 err = got_error_from_errno2("got_opentemp", path2);
4146 n = fwrite(link_target, 1, link_len, f2);
4147 if (n != link_len) {
4148 err = got_ferror(f2, GOT_ERR_IO);
4151 if (fflush(f2) == EOF) {
4152 err = got_error_from_errno("fflush");
4158 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4162 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4166 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4170 if (stat(path1, &sb1) == -1) {
4171 err = got_error_from_errno2("stat", path1);
4175 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4176 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4180 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4184 if (fseek(f1, 0L, SEEK_SET) == -1)
4185 return got_ferror(f1, GOT_ERR_IO);
4186 if (fseek(f2, 0L, SEEK_SET) == -1)
4187 return got_ferror(f2, GOT_ERR_IO);
4188 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4190 err = apply_or_reject_change(&choice, change, ++n,
4191 changes->nchanges, ds, args, diff_flags, relpath,
4192 f1, f2, &line_cur1, &line_cur2,
4193 reverse_patch ? NULL : outfile,
4194 reverse_patch ? outfile : NULL,
4195 patch_cb, patch_arg);
4198 if (choice == GOT_PATCH_CHOICE_YES)
4200 else if (choice == GOT_PATCH_CHOICE_QUIT)
4204 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4205 reverse_patch ? NULL : outfile,
4206 reverse_patch ? outfile : NULL);
4210 if (!S_ISLNK(sb2.st_mode)) {
4211 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4212 err = got_error_from_errno2("chmod", path2);
4220 got_object_blob_close(blob);
4221 if (f1 && fclose(f1) == EOF && err == NULL)
4222 err = got_error_from_errno2("fclose", path1);
4223 if (f2 && fclose(f2) == EOF && err == NULL)
4224 err = got_error_from_errno2("fclose", path2);
4225 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4226 err = got_error_from_errno2("close", path2);
4227 if (outfile && fclose(outfile) == EOF && err == NULL)
4228 err = got_error_from_errno2("fclose", *path_outfile);
4229 if (path1 && unlink(path1) == -1 && err == NULL)
4230 err = got_error_from_errno2("unlink", path1);
4231 if (err || !have_content) {
4232 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4233 err = got_error_from_errno2("unlink", *path_outfile);
4234 free(*path_outfile);
4235 *path_outfile = NULL;
4239 got_diff_state_free(ds);
4243 got_diff_free_changes(changes);
4248 static const struct got_error *
4249 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4250 const char *relpath, struct got_object_id *blob_id,
4251 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4252 int dirfd, const char *de_name)
4254 struct revert_file_args *a = arg;
4255 const struct got_error *err = NULL;
4256 char *parent_path = NULL;
4257 struct got_fileindex_entry *ie;
4258 struct got_tree_object *tree = NULL;
4259 struct got_object_id *tree_id = NULL;
4260 const struct got_tree_entry *te = NULL;
4261 char *tree_path = NULL, *te_name;
4262 char *ondisk_path = NULL, *path_content = NULL;
4263 struct got_blob_object *blob = NULL;
4265 /* Reverting a staged deletion is a no-op. */
4266 if (status == GOT_STATUS_DELETE &&
4267 staged_status != GOT_STATUS_NO_CHANGE)
4270 if (status == GOT_STATUS_UNVERSIONED)
4271 return (*a->progress_cb)(a->progress_arg,
4272 GOT_STATUS_UNVERSIONED, relpath);
4274 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4276 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4278 /* Construct in-repository path of tree which contains this blob. */
4279 err = got_path_dirname(&parent_path, ie->path);
4281 if (err->code != GOT_ERR_BAD_PATH)
4283 parent_path = strdup("/");
4284 if (parent_path == NULL) {
4285 err = got_error_from_errno("strdup");
4289 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4290 tree_path = strdup(parent_path);
4291 if (tree_path == NULL) {
4292 err = got_error_from_errno("strdup");
4296 if (got_path_is_root_dir(parent_path)) {
4297 tree_path = strdup(a->worktree->path_prefix);
4298 if (tree_path == NULL) {
4299 err = got_error_from_errno("strdup");
4303 if (asprintf(&tree_path, "%s/%s",
4304 a->worktree->path_prefix, parent_path) == -1) {
4305 err = got_error_from_errno("asprintf");
4311 err = got_object_id_by_path(&tree_id, a->repo,
4312 a->worktree->base_commit_id, tree_path);
4314 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4315 (status == GOT_STATUS_ADD ||
4316 staged_status == GOT_STATUS_ADD)))
4319 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4323 te_name = basename(ie->path);
4324 if (te_name == NULL) {
4325 err = got_error_from_errno2("basename", ie->path);
4329 te = got_object_tree_find_entry(tree, te_name);
4330 if (te == NULL && status != GOT_STATUS_ADD &&
4331 staged_status != GOT_STATUS_ADD) {
4332 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4338 case GOT_STATUS_ADD:
4340 int choice = GOT_PATCH_CHOICE_NONE;
4341 err = (*a->patch_cb)(&choice, a->patch_arg,
4342 status, ie->path, NULL, 1, 1);
4345 if (choice != GOT_PATCH_CHOICE_YES)
4348 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4352 got_fileindex_entry_remove(a->fileindex, ie);
4354 case GOT_STATUS_DELETE:
4356 int choice = GOT_PATCH_CHOICE_NONE;
4357 err = (*a->patch_cb)(&choice, a->patch_arg,
4358 status, ie->path, NULL, 1, 1);
4361 if (choice != GOT_PATCH_CHOICE_YES)
4365 case GOT_STATUS_MODIFY:
4366 case GOT_STATUS_MODE_CHANGE:
4367 case GOT_STATUS_CONFLICT:
4368 case GOT_STATUS_MISSING: {
4369 struct got_object_id id;
4370 if (staged_status == GOT_STATUS_ADD ||
4371 staged_status == GOT_STATUS_MODIFY) {
4372 memcpy(id.sha1, ie->staged_blob_sha1,
4373 SHA1_DIGEST_LENGTH);
4375 memcpy(id.sha1, ie->blob_sha1,
4376 SHA1_DIGEST_LENGTH);
4377 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4381 if (asprintf(&ondisk_path, "%s/%s",
4382 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4383 err = got_error_from_errno("asprintf");
4387 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4388 status == GOT_STATUS_CONFLICT)) {
4389 int is_bad_symlink = 0;
4390 err = create_patched_content(&path_content, 1, &id,
4391 ondisk_path, dirfd, de_name, ie->path, a->repo,
4392 a->patch_cb, a->patch_arg);
4393 if (err || path_content == NULL)
4395 if (te && S_ISLNK(te->mode)) {
4396 if (unlink(path_content) == -1) {
4397 err = got_error_from_errno2("unlink",
4401 err = install_symlink(&is_bad_symlink,
4402 a->worktree, ondisk_path, ie->path,
4403 blob, 0, 1, 0, a->repo,
4404 a->progress_cb, a->progress_arg);
4406 if (rename(path_content, ondisk_path) == -1) {
4407 err = got_error_from_errno3("rename",
4408 path_content, ondisk_path);
4413 int is_bad_symlink = 0;
4414 if (te && S_ISLNK(te->mode)) {
4415 err = install_symlink(&is_bad_symlink,
4416 a->worktree, ondisk_path, ie->path,
4417 blob, 0, 1, 0, a->repo,
4418 a->progress_cb, a->progress_arg);
4420 err = install_blob(a->worktree, ondisk_path,
4422 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4423 got_fileindex_perms_to_st(ie), blob,
4424 0, 1, 0, 0, a->repo,
4425 a->progress_cb, a->progress_arg);
4429 if (status == GOT_STATUS_DELETE ||
4430 status == GOT_STATUS_MODE_CHANGE) {
4431 err = got_fileindex_entry_update(ie,
4432 ondisk_path, blob->id.sha1,
4433 a->worktree->base_commit_id->sha1, 1);
4437 if (is_bad_symlink) {
4438 got_fileindex_entry_filetype_set(ie,
4439 GOT_FILEIDX_MODE_BAD_SYMLINK);
4453 got_object_blob_close(blob);
4455 got_object_tree_close(tree);
4460 const struct got_error *
4461 got_worktree_revert(struct got_worktree *worktree,
4462 struct got_pathlist_head *paths,
4463 got_worktree_checkout_cb progress_cb, void *progress_arg,
4464 got_worktree_patch_cb patch_cb, void *patch_arg,
4465 struct got_repository *repo)
4467 struct got_fileindex *fileindex = NULL;
4468 char *fileindex_path = NULL;
4469 const struct got_error *err = NULL, *unlockerr = NULL;
4470 const struct got_error *sync_err = NULL;
4471 struct got_pathlist_entry *pe;
4472 struct revert_file_args rfa;
4474 err = lock_worktree(worktree, LOCK_EX);
4478 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4482 rfa.worktree = worktree;
4483 rfa.fileindex = fileindex;
4484 rfa.progress_cb = progress_cb;
4485 rfa.progress_arg = progress_arg;
4486 rfa.patch_cb = patch_cb;
4487 rfa.patch_arg = patch_arg;
4489 TAILQ_FOREACH(pe, paths, entry) {
4490 err = worktree_status(worktree, pe->path, fileindex, repo,
4491 revert_file, &rfa, NULL, NULL, 0, 0);
4495 sync_err = sync_fileindex(fileindex, fileindex_path);
4496 if (sync_err && err == NULL)
4499 free(fileindex_path);
4501 got_fileindex_free(fileindex);
4502 unlockerr = lock_worktree(worktree, LOCK_SH);
4503 if (unlockerr && err == NULL)
4509 free_commitable(struct got_commitable *ct)
4512 free(ct->in_repo_path);
4513 free(ct->ondisk_path);
4515 free(ct->base_blob_id);
4516 free(ct->staged_blob_id);
4517 free(ct->base_commit_id);
4521 struct collect_commitables_arg {
4522 struct got_pathlist_head *commitable_paths;
4523 struct got_repository *repo;
4524 struct got_worktree *worktree;
4525 struct got_fileindex *fileindex;
4526 int have_staged_files;
4527 int allow_bad_symlinks;
4530 static const struct got_error *
4531 collect_commitables(void *arg, unsigned char status,
4532 unsigned char staged_status, const char *relpath,
4533 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4534 struct got_object_id *commit_id, int dirfd, const char *de_name)
4536 struct collect_commitables_arg *a = arg;
4537 const struct got_error *err = NULL;
4538 struct got_commitable *ct = NULL;
4539 struct got_pathlist_entry *new = NULL;
4540 char *parent_path = NULL, *path = NULL;
4543 if (a->have_staged_files) {
4544 if (staged_status != GOT_STATUS_MODIFY &&
4545 staged_status != GOT_STATUS_ADD &&
4546 staged_status != GOT_STATUS_DELETE)
4549 if (status == GOT_STATUS_CONFLICT)
4550 return got_error(GOT_ERR_COMMIT_CONFLICT);
4552 if (status != GOT_STATUS_MODIFY &&
4553 status != GOT_STATUS_MODE_CHANGE &&
4554 status != GOT_STATUS_ADD &&
4555 status != GOT_STATUS_DELETE)
4559 if (asprintf(&path, "/%s", relpath) == -1) {
4560 err = got_error_from_errno("asprintf");
4563 if (strcmp(path, "/") == 0) {
4564 parent_path = strdup("");
4565 if (parent_path == NULL)
4566 return got_error_from_errno("strdup");
4568 err = got_path_dirname(&parent_path, path);
4573 ct = calloc(1, sizeof(*ct));
4575 err = got_error_from_errno("calloc");
4579 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4581 err = got_error_from_errno("asprintf");
4585 if (staged_status == GOT_STATUS_ADD ||
4586 staged_status == GOT_STATUS_MODIFY) {
4587 struct got_fileindex_entry *ie;
4588 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4589 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4590 case GOT_FILEIDX_MODE_REGULAR_FILE:
4591 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4594 case GOT_FILEIDX_MODE_SYMLINK:
4598 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4601 ct->mode |= got_fileindex_entry_perms_get(ie);
4602 } else if (status != GOT_STATUS_DELETE &&
4603 staged_status != GOT_STATUS_DELETE) {
4605 if (fstatat(dirfd, de_name, &sb,
4606 AT_SYMLINK_NOFOLLOW) == -1) {
4607 err = got_error_from_errno2("fstatat",
4611 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4612 err = got_error_from_errno2("lstat", ct->ondisk_path);
4615 ct->mode = sb.st_mode;
4618 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4619 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4621 err = got_error_from_errno("asprintf");
4625 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4626 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4628 char target_path[PATH_MAX];
4630 target_len = readlink(ct->ondisk_path, target_path,
4631 sizeof(target_path));
4632 if (target_len == -1) {
4633 err = got_error_from_errno2("readlink",
4637 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4638 target_len, ct->ondisk_path, a->worktree->root_path);
4641 if (is_bad_symlink) {
4642 err = got_error_path(ct->ondisk_path,
4643 GOT_ERR_BAD_SYMLINK);
4649 ct->status = status;
4650 ct->staged_status = staged_status;
4651 ct->blob_id = NULL; /* will be filled in when blob gets created */
4652 if (ct->status != GOT_STATUS_ADD &&
4653 ct->staged_status != GOT_STATUS_ADD) {
4654 ct->base_blob_id = got_object_id_dup(blob_id);
4655 if (ct->base_blob_id == NULL) {
4656 err = got_error_from_errno("got_object_id_dup");
4659 ct->base_commit_id = got_object_id_dup(commit_id);
4660 if (ct->base_commit_id == NULL) {
4661 err = got_error_from_errno("got_object_id_dup");
4665 if (ct->staged_status == GOT_STATUS_ADD ||
4666 ct->staged_status == GOT_STATUS_MODIFY) {
4667 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4668 if (ct->staged_blob_id == NULL) {
4669 err = got_error_from_errno("got_object_id_dup");
4673 ct->path = strdup(path);
4674 if (ct->path == NULL) {
4675 err = got_error_from_errno("strdup");
4678 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4680 if (ct && (err || new == NULL))
4681 free_commitable(ct);
4687 static const struct got_error *write_tree(struct got_object_id **, int *,
4688 struct got_tree_object *, const char *, struct got_pathlist_head *,
4689 got_worktree_status_cb status_cb, void *status_arg,
4690 struct got_repository *);
4692 static const struct got_error *
4693 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4694 struct got_tree_entry *te, const char *parent_path,
4695 struct got_pathlist_head *commitable_paths,
4696 got_worktree_status_cb status_cb, void *status_arg,
4697 struct got_repository *repo)
4699 const struct got_error *err = NULL;
4700 struct got_tree_object *subtree;
4703 if (asprintf(&subpath, "%s%s%s", parent_path,
4704 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4705 return got_error_from_errno("asprintf");
4707 err = got_object_open_as_tree(&subtree, repo, &te->id);
4711 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4712 commitable_paths, status_cb, status_arg, repo);
4713 got_object_tree_close(subtree);
4718 static const struct got_error *
4719 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4721 const struct got_error *err = NULL;
4722 char *ct_parent_path = NULL;
4726 if (strchr(ct->in_repo_path, '/') == NULL) {
4727 *match = got_path_is_root_dir(path);
4731 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4734 *match = (strcmp(path, ct_parent_path) == 0);
4735 free(ct_parent_path);
4740 get_ct_file_mode(struct got_commitable *ct)
4742 if (S_ISLNK(ct->mode))
4745 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4748 static const struct got_error *
4749 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4750 struct got_tree_entry *te, struct got_commitable *ct)
4752 const struct got_error *err = NULL;
4756 err = got_object_tree_entry_dup(new_te, te);
4760 (*new_te)->mode = get_ct_file_mode(ct);
4762 if (ct->staged_status == GOT_STATUS_MODIFY)
4763 memcpy(&(*new_te)->id, ct->staged_blob_id,
4764 sizeof((*new_te)->id));
4766 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4768 if (err && *new_te) {
4775 static const struct got_error *
4776 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4777 struct got_commitable *ct)
4779 const struct got_error *err = NULL;
4784 *new_te = calloc(1, sizeof(**new_te));
4785 if (*new_te == NULL)
4786 return got_error_from_errno("calloc");
4788 ct_name = basename(ct->path);
4789 if (ct_name == NULL) {
4790 err = got_error_from_errno2("basename", ct->path);
4793 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4794 sizeof((*new_te)->name)) {
4795 err = got_error(GOT_ERR_NO_SPACE);
4799 (*new_te)->mode = get_ct_file_mode(ct);
4801 if (ct->staged_status == GOT_STATUS_ADD)
4802 memcpy(&(*new_te)->id, ct->staged_blob_id,
4803 sizeof((*new_te)->id));
4805 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4807 if (err && *new_te) {
4814 static const struct got_error *
4815 insert_tree_entry(struct got_tree_entry *new_te,
4816 struct got_pathlist_head *paths)
4818 const struct got_error *err = NULL;
4819 struct got_pathlist_entry *new_pe;
4821 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4825 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4829 static const struct got_error *
4830 report_ct_status(struct got_commitable *ct,
4831 got_worktree_status_cb status_cb, void *status_arg)
4833 const char *ct_path = ct->path;
4834 unsigned char status;
4836 while (ct_path[0] == '/')
4839 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4840 status = ct->staged_status;
4842 status = ct->status;
4844 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4845 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4848 static const struct got_error *
4849 match_modified_subtree(int *modified, struct got_tree_entry *te,
4850 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4852 const struct got_error *err = NULL;
4853 struct got_pathlist_entry *pe;
4858 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4859 got_path_is_root_dir(base_tree_path) ? "" : "/",
4861 return got_error_from_errno("asprintf");
4863 TAILQ_FOREACH(pe, commitable_paths, entry) {
4864 struct got_commitable *ct = pe->data;
4865 *modified = got_path_is_child(ct->in_repo_path, te_path,
4875 static const struct got_error *
4876 match_deleted_or_modified_ct(struct got_commitable **ctp,
4877 struct got_tree_entry *te, const char *base_tree_path,
4878 struct got_pathlist_head *commitable_paths)
4880 const struct got_error *err = NULL;
4881 struct got_pathlist_entry *pe;
4885 TAILQ_FOREACH(pe, commitable_paths, entry) {
4886 struct got_commitable *ct = pe->data;
4887 char *ct_name = NULL;
4890 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4891 if (ct->status != GOT_STATUS_MODIFY &&
4892 ct->status != GOT_STATUS_MODE_CHANGE &&
4893 ct->status != GOT_STATUS_DELETE)
4896 if (ct->staged_status != GOT_STATUS_MODIFY &&
4897 ct->staged_status != GOT_STATUS_DELETE)
4901 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4904 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4910 ct_name = basename(pe->path);
4911 if (ct_name == NULL)
4912 return got_error_from_errno2("basename", pe->path);
4914 if (strcmp(te->name, ct_name) != 0)
4924 static const struct got_error *
4925 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4926 const char *child_path, const char *path_base_tree,
4927 struct got_pathlist_head *commitable_paths,
4928 got_worktree_status_cb status_cb, void *status_arg,
4929 struct got_repository *repo)
4931 const struct got_error *err = NULL;
4932 struct got_tree_entry *new_te;
4934 struct got_object_id *id = NULL;
4939 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4940 got_path_is_root_dir(path_base_tree) ? "" : "/",
4942 return got_error_from_errno("asprintf");
4944 new_te = calloc(1, sizeof(*new_te));
4946 return got_error_from_errno("calloc");
4947 new_te->mode = S_IFDIR;
4949 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4950 sizeof(new_te->name)) {
4951 err = got_error(GOT_ERR_NO_SPACE);
4954 err = write_tree(&id, &nentries, NULL, subtree_path,
4955 commitable_paths, status_cb, status_arg, repo);
4960 memcpy(&new_te->id, id, sizeof(new_te->id));
4969 static const struct got_error *
4970 write_tree(struct got_object_id **new_tree_id, int *nentries,
4971 struct got_tree_object *base_tree, const char *path_base_tree,
4972 struct got_pathlist_head *commitable_paths,
4973 got_worktree_status_cb status_cb, void *status_arg,
4974 struct got_repository *repo)
4976 const struct got_error *err = NULL;
4977 struct got_pathlist_head paths;
4978 struct got_tree_entry *te, *new_te = NULL;
4979 struct got_pathlist_entry *pe;
4984 /* Insert, and recurse into, newly added entries first. */
4985 TAILQ_FOREACH(pe, commitable_paths, entry) {
4986 struct got_commitable *ct = pe->data;
4987 char *child_path = NULL, *slash;
4989 if ((ct->status != GOT_STATUS_ADD &&
4990 ct->staged_status != GOT_STATUS_ADD) ||
4991 (ct->flags & GOT_COMMITABLE_ADDED))
4994 if (!got_path_is_child(pe->path, path_base_tree,
4995 strlen(path_base_tree)))
4998 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5003 slash = strchr(child_path, '/');
5004 if (slash == NULL) {
5005 err = alloc_added_blob_tree_entry(&new_te, ct);
5008 err = report_ct_status(ct, status_cb, status_arg);
5011 ct->flags |= GOT_COMMITABLE_ADDED;
5012 err = insert_tree_entry(new_te, &paths);
5017 *slash = '\0'; /* trim trailing path components */
5018 if (base_tree == NULL ||
5019 got_object_tree_find_entry(base_tree, child_path)
5021 err = make_subtree_for_added_blob(&new_te,
5022 child_path, path_base_tree,
5023 commitable_paths, status_cb, status_arg,
5027 err = insert_tree_entry(new_te, &paths);
5036 int i, nbase_entries;
5037 /* Handle modified and deleted entries. */
5038 nbase_entries = got_object_tree_get_nentries(base_tree);
5039 for (i = 0; i < nbase_entries; i++) {
5040 struct got_commitable *ct = NULL;
5042 te = got_object_tree_get_entry(base_tree, i);
5043 if (got_object_tree_entry_is_submodule(te)) {
5044 /* Entry is a submodule; just copy it. */
5045 err = got_object_tree_entry_dup(&new_te, te);
5048 err = insert_tree_entry(new_te, &paths);
5055 if (S_ISDIR(te->mode)) {
5057 err = got_object_tree_entry_dup(&new_te, te);
5060 err = match_modified_subtree(&modified, te,
5061 path_base_tree, commitable_paths);
5064 /* Avoid recursion into unmodified subtrees. */
5066 struct got_object_id *new_id;
5068 err = write_subtree(&new_id,
5070 path_base_tree, commitable_paths,
5071 status_cb, status_arg, repo);
5074 if (nsubentries == 0) {
5075 /* All entries were deleted. */
5079 memcpy(&new_te->id, new_id,
5080 sizeof(new_te->id));
5083 err = insert_tree_entry(new_te, &paths);
5090 err = match_deleted_or_modified_ct(&ct, te,
5091 path_base_tree, commitable_paths);
5095 /* NB: Deleted entries get dropped here. */
5096 if (ct->status == GOT_STATUS_MODIFY ||
5097 ct->status == GOT_STATUS_MODE_CHANGE ||
5098 ct->staged_status == GOT_STATUS_MODIFY) {
5099 err = alloc_modified_blob_tree_entry(
5103 err = insert_tree_entry(new_te, &paths);
5108 err = report_ct_status(ct, status_cb,
5113 /* Entry is unchanged; just copy it. */
5114 err = got_object_tree_entry_dup(&new_te, te);
5117 err = insert_tree_entry(new_te, &paths);
5125 /* Write new list of entries; deleted entries have been dropped. */
5126 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5128 got_pathlist_free(&paths);
5132 static const struct got_error *
5133 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5134 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5135 int have_staged_files)
5137 const struct got_error *err = NULL;
5138 struct got_pathlist_entry *pe;
5140 TAILQ_FOREACH(pe, commitable_paths, entry) {
5141 struct got_fileindex_entry *ie;
5142 struct got_commitable *ct = pe->data;
5144 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5146 if (ct->status == GOT_STATUS_DELETE ||
5147 ct->staged_status == GOT_STATUS_DELETE) {
5148 got_fileindex_entry_remove(fileindex, ie);
5149 } else if (ct->staged_status == GOT_STATUS_ADD ||
5150 ct->staged_status == GOT_STATUS_MODIFY) {
5151 got_fileindex_entry_stage_set(ie,
5152 GOT_FILEIDX_STAGE_NONE);
5153 err = got_fileindex_entry_update(ie,
5154 ct->ondisk_path, ct->staged_blob_id->sha1,
5155 new_base_commit_id->sha1,
5156 !have_staged_files);
5158 err = got_fileindex_entry_update(ie,
5159 ct->ondisk_path, ct->blob_id->sha1,
5160 new_base_commit_id->sha1,
5161 !have_staged_files);
5163 err = got_fileindex_entry_alloc(&ie, pe->path);
5166 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5167 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5169 got_fileindex_entry_free(ie);
5172 err = got_fileindex_entry_add(fileindex, ie);
5174 got_fileindex_entry_free(ie);
5183 static const struct got_error *
5184 check_out_of_date(const char *in_repo_path, unsigned char status,
5185 unsigned char staged_status, struct got_object_id *base_blob_id,
5186 struct got_object_id *base_commit_id,
5187 struct got_object_id *head_commit_id, struct got_repository *repo,
5190 const struct got_error *err = NULL;
5191 struct got_object_id *id = NULL;
5193 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5194 /* Trivial case: base commit == head commit */
5195 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5198 * Ensure file content which local changes were based
5199 * on matches file content in the branch head.
5201 err = got_object_id_by_path(&id, repo, head_commit_id,
5204 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5205 err = got_error(ood_errcode);
5207 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5208 err = got_error(ood_errcode);
5210 /* Require that added files don't exist in the branch head. */
5211 err = got_object_id_by_path(&id, repo, head_commit_id,
5213 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5215 err = id ? got_error(ood_errcode) : NULL;
5222 const struct got_error *
5223 commit_worktree(struct got_object_id **new_commit_id,
5224 struct got_pathlist_head *commitable_paths,
5225 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5226 const char *author, const char *committer,
5227 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5228 got_worktree_status_cb status_cb, void *status_arg,
5229 struct got_repository *repo)
5231 const struct got_error *err = NULL, *unlockerr = NULL;
5232 struct got_pathlist_entry *pe;
5233 const char *head_ref_name = NULL;
5234 struct got_commit_object *head_commit = NULL;
5235 struct got_reference *head_ref2 = NULL;
5236 struct got_object_id *head_commit_id2 = NULL;
5237 struct got_tree_object *head_tree = NULL;
5238 struct got_object_id *new_tree_id = NULL;
5240 struct got_object_id_queue parent_ids;
5241 struct got_object_qid *pid = NULL;
5242 char *logmsg = NULL;
5244 *new_commit_id = NULL;
5246 SIMPLEQ_INIT(&parent_ids);
5248 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5252 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5256 if (commit_msg_cb != NULL) {
5257 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5262 if (logmsg == NULL || strlen(logmsg) == 0) {
5263 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5267 /* Create blobs from added and modified files and record their IDs. */
5268 TAILQ_FOREACH(pe, commitable_paths, entry) {
5269 struct got_commitable *ct = pe->data;
5272 /* Blobs for staged files already exist. */
5273 if (ct->staged_status == GOT_STATUS_ADD ||
5274 ct->staged_status == GOT_STATUS_MODIFY)
5277 if (ct->status != GOT_STATUS_ADD &&
5278 ct->status != GOT_STATUS_MODIFY &&
5279 ct->status != GOT_STATUS_MODE_CHANGE)
5282 if (asprintf(&ondisk_path, "%s/%s",
5283 worktree->root_path, pe->path) == -1) {
5284 err = got_error_from_errno("asprintf");
5287 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5293 /* Recursively write new tree objects. */
5294 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5295 commitable_paths, status_cb, status_arg, repo);
5299 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5302 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5303 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5304 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5305 got_object_qid_free(pid);
5311 /* Check if a concurrent commit to our branch has occurred. */
5312 head_ref_name = got_worktree_get_head_ref_name(worktree);
5313 if (head_ref_name == NULL) {
5314 err = got_error_from_errno("got_worktree_get_head_ref_name");
5317 /* Lock the reference here to prevent concurrent modification. */
5318 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5321 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5324 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5325 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5328 /* Update branch head in repository. */
5329 err = got_ref_change_ref(head_ref2, *new_commit_id);
5332 err = got_ref_write(head_ref2, repo);
5336 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5340 err = ref_base_commit(worktree, repo);
5345 got_object_tree_close(head_tree);
5347 got_object_commit_close(head_commit);
5348 free(head_commit_id2);
5350 unlockerr = got_ref_unlock(head_ref2);
5351 if (unlockerr && err == NULL)
5353 got_ref_close(head_ref2);
5358 static const struct got_error *
5359 check_path_is_commitable(const char *path,
5360 struct got_pathlist_head *commitable_paths)
5362 struct got_pathlist_entry *cpe = NULL;
5363 size_t path_len = strlen(path);
5365 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5366 struct got_commitable *ct = cpe->data;
5367 const char *ct_path = ct->path;
5369 while (ct_path[0] == '/')
5372 if (strcmp(path, ct_path) == 0 ||
5373 got_path_is_child(ct_path, path, path_len))
5378 return got_error_path(path, GOT_ERR_BAD_PATH);
5383 static const struct got_error *
5384 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5386 int *have_staged_files = arg;
5388 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5389 *have_staged_files = 1;
5390 return got_error(GOT_ERR_CANCELLED);
5396 static const struct got_error *
5397 check_non_staged_files(struct got_fileindex *fileindex,
5398 struct got_pathlist_head *paths)
5400 struct got_pathlist_entry *pe;
5401 struct got_fileindex_entry *ie;
5403 TAILQ_FOREACH(pe, paths, entry) {
5404 if (pe->path[0] == '\0')
5406 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5408 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5409 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5410 return got_error_path(pe->path,
5411 GOT_ERR_FILE_NOT_STAGED);
5417 const struct got_error *
5418 got_worktree_commit(struct got_object_id **new_commit_id,
5419 struct got_worktree *worktree, struct got_pathlist_head *paths,
5420 const char *author, const char *committer, int allow_bad_symlinks,
5421 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5422 got_worktree_status_cb status_cb, void *status_arg,
5423 struct got_repository *repo)
5425 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5426 struct got_fileindex *fileindex = NULL;
5427 char *fileindex_path = NULL;
5428 struct got_pathlist_head commitable_paths;
5429 struct collect_commitables_arg cc_arg;
5430 struct got_pathlist_entry *pe;
5431 struct got_reference *head_ref = NULL;
5432 struct got_object_id *head_commit_id = NULL;
5433 int have_staged_files = 0;
5435 *new_commit_id = NULL;
5437 TAILQ_INIT(&commitable_paths);
5439 err = lock_worktree(worktree, LOCK_EX);
5443 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5447 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5451 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5455 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5456 &have_staged_files);
5457 if (err && err->code != GOT_ERR_CANCELLED)
5459 if (have_staged_files) {
5460 err = check_non_staged_files(fileindex, paths);
5465 cc_arg.commitable_paths = &commitable_paths;
5466 cc_arg.worktree = worktree;
5467 cc_arg.fileindex = fileindex;
5469 cc_arg.have_staged_files = have_staged_files;
5470 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5471 TAILQ_FOREACH(pe, paths, entry) {
5472 err = worktree_status(worktree, pe->path, fileindex, repo,
5473 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5478 if (TAILQ_EMPTY(&commitable_paths)) {
5479 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5483 TAILQ_FOREACH(pe, paths, entry) {
5484 err = check_path_is_commitable(pe->path, &commitable_paths);
5489 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5490 struct got_commitable *ct = pe->data;
5491 const char *ct_path = ct->in_repo_path;
5493 while (ct_path[0] == '/')
5495 err = check_out_of_date(ct_path, ct->status,
5496 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5497 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5503 err = commit_worktree(new_commit_id, &commitable_paths,
5504 head_commit_id, worktree, author, committer,
5505 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5509 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5510 fileindex, have_staged_files);
5511 sync_err = sync_fileindex(fileindex, fileindex_path);
5512 if (sync_err && err == NULL)
5516 got_fileindex_free(fileindex);
5517 free(fileindex_path);
5518 unlockerr = lock_worktree(worktree, LOCK_SH);
5519 if (unlockerr && err == NULL)
5521 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5522 struct got_commitable *ct = pe->data;
5523 free_commitable(ct);
5525 got_pathlist_free(&commitable_paths);
5530 got_commitable_get_path(struct got_commitable *ct)
5536 got_commitable_get_status(struct got_commitable *ct)
5541 struct check_rebase_ok_arg {
5542 struct got_worktree *worktree;
5543 struct got_repository *repo;
5546 static const struct got_error *
5547 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5549 const struct got_error *err = NULL;
5550 struct check_rebase_ok_arg *a = arg;
5551 unsigned char status;
5555 /* Reject rebase of a work tree with mixed base commits. */
5556 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5557 SHA1_DIGEST_LENGTH))
5558 return got_error(GOT_ERR_MIXED_COMMITS);
5560 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5562 return got_error_from_errno("asprintf");
5564 /* Reject rebase of a work tree with modified or staged files. */
5565 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5570 if (status != GOT_STATUS_NO_CHANGE)
5571 return got_error(GOT_ERR_MODIFIED);
5572 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5573 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5578 const struct got_error *
5579 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5580 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5581 struct got_worktree *worktree, struct got_reference *branch,
5582 struct got_repository *repo)
5584 const struct got_error *err = NULL;
5585 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5586 char *branch_ref_name = NULL;
5587 char *fileindex_path = NULL;
5588 struct check_rebase_ok_arg ok_arg;
5589 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5590 struct got_object_id *wt_branch_tip = NULL;
5592 *new_base_branch_ref = NULL;
5596 err = lock_worktree(worktree, LOCK_EX);
5600 err = open_fileindex(fileindex, &fileindex_path, worktree);
5604 ok_arg.worktree = worktree;
5606 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5611 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5615 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5619 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5623 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5628 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5631 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5632 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5636 err = got_ref_alloc_symref(new_base_branch_ref,
5637 new_base_branch_ref_name, wt_branch);
5640 err = got_ref_write(*new_base_branch_ref, repo);
5644 /* TODO Lock original branch's ref while rebasing? */
5646 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5650 err = got_ref_write(branch_ref, repo);
5654 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5655 worktree->base_commit_id);
5658 err = got_ref_write(*tmp_branch, repo);
5662 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5666 free(fileindex_path);
5667 free(tmp_branch_name);
5668 free(new_base_branch_ref_name);
5669 free(branch_ref_name);
5671 got_ref_close(branch_ref);
5673 got_ref_close(wt_branch);
5674 free(wt_branch_tip);
5676 if (*new_base_branch_ref) {
5677 got_ref_close(*new_base_branch_ref);
5678 *new_base_branch_ref = NULL;
5681 got_ref_close(*tmp_branch);
5685 got_fileindex_free(*fileindex);
5688 lock_worktree(worktree, LOCK_SH);
5693 const struct got_error *
5694 got_worktree_rebase_continue(struct got_object_id **commit_id,
5695 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5696 struct got_reference **branch, struct got_fileindex **fileindex,
5697 struct got_worktree *worktree, struct got_repository *repo)
5699 const struct got_error *err;
5700 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5701 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5702 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5703 char *fileindex_path = NULL;
5704 int have_staged_files = 0;
5707 *new_base_branch = NULL;
5712 err = lock_worktree(worktree, LOCK_EX);
5716 err = open_fileindex(fileindex, &fileindex_path, worktree);
5720 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5721 &have_staged_files);
5722 if (err && err->code != GOT_ERR_CANCELLED)
5724 if (have_staged_files) {
5725 err = got_error(GOT_ERR_STAGED_PATHS);
5729 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5733 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5737 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5741 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5745 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5749 err = got_ref_open(branch, repo,
5750 got_ref_get_symref_target(branch_ref), 0);
5754 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5758 err = got_ref_resolve(commit_id, repo, commit_ref);
5762 err = got_ref_open(new_base_branch, repo,
5763 new_base_branch_ref_name, 0);
5767 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5771 free(commit_ref_name);
5772 free(branch_ref_name);
5773 free(fileindex_path);
5775 got_ref_close(commit_ref);
5777 got_ref_close(branch_ref);
5782 got_ref_close(*tmp_branch);
5785 if (*new_base_branch) {
5786 got_ref_close(*new_base_branch);
5787 *new_base_branch = NULL;
5790 got_ref_close(*branch);
5794 got_fileindex_free(*fileindex);
5797 lock_worktree(worktree, LOCK_SH);
5802 const struct got_error *
5803 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5805 const struct got_error *err;
5806 char *tmp_branch_name = NULL;
5808 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5812 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5813 free(tmp_branch_name);
5817 static const struct got_error *
5818 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5819 char **logmsg, void *arg)
5825 static const struct got_error *
5826 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5827 const char *path, struct got_object_id *blob_id,
5828 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5829 int dirfd, const char *de_name)
5834 struct collect_merged_paths_arg {
5835 got_worktree_checkout_cb progress_cb;
5837 struct got_pathlist_head *merged_paths;
5840 static const struct got_error *
5841 collect_merged_paths(void *arg, unsigned char status, const char *path)
5843 const struct got_error *err;
5844 struct collect_merged_paths_arg *a = arg;
5846 struct got_pathlist_entry *new;
5848 err = (*a->progress_cb)(a->progress_arg, status, path);
5852 if (status != GOT_STATUS_MERGE &&
5853 status != GOT_STATUS_ADD &&
5854 status != GOT_STATUS_DELETE &&
5855 status != GOT_STATUS_CONFLICT)
5860 return got_error_from_errno("strdup");
5862 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5863 if (err || new == NULL)
5869 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5871 struct got_pathlist_entry *pe;
5873 TAILQ_FOREACH(pe, merged_paths, entry)
5874 free((char *)pe->path);
5876 got_pathlist_free(merged_paths);
5879 static const struct got_error *
5880 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5881 int is_rebase, struct got_repository *repo)
5883 const struct got_error *err;
5884 struct got_reference *commit_ref = NULL;
5886 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5888 if (err->code != GOT_ERR_NOT_REF)
5890 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5893 err = got_ref_write(commit_ref, repo);
5896 } else if (is_rebase) {
5897 struct got_object_id *stored_id;
5900 err = got_ref_resolve(&stored_id, repo, commit_ref);
5903 cmp = got_object_id_cmp(commit_id, stored_id);
5906 err = got_error(GOT_ERR_REBASE_COMMITID);
5912 got_ref_close(commit_ref);
5916 static const struct got_error *
5917 rebase_merge_files(struct got_pathlist_head *merged_paths,
5918 const char *commit_ref_name, struct got_worktree *worktree,
5919 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5920 struct got_object_id *commit_id, struct got_repository *repo,
5921 got_worktree_checkout_cb progress_cb, void *progress_arg,
5922 got_cancel_cb cancel_cb, void *cancel_arg)
5924 const struct got_error *err;
5925 struct got_reference *commit_ref = NULL;
5926 struct collect_merged_paths_arg cmp_arg;
5927 char *fileindex_path;
5929 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5931 err = get_fileindex_path(&fileindex_path, worktree);
5935 cmp_arg.progress_cb = progress_cb;
5936 cmp_arg.progress_arg = progress_arg;
5937 cmp_arg.merged_paths = merged_paths;
5938 err = merge_files(worktree, fileindex, fileindex_path,
5939 parent_commit_id, commit_id, repo, collect_merged_paths,
5940 &cmp_arg, cancel_cb, cancel_arg);
5942 got_ref_close(commit_ref);
5946 const struct got_error *
5947 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5948 struct got_worktree *worktree, struct got_fileindex *fileindex,
5949 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5950 struct got_repository *repo,
5951 got_worktree_checkout_cb progress_cb, void *progress_arg,
5952 got_cancel_cb cancel_cb, void *cancel_arg)
5954 const struct got_error *err;
5955 char *commit_ref_name;
5957 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5961 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5965 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5966 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5967 progress_arg, cancel_cb, cancel_arg);
5969 free(commit_ref_name);
5973 const struct got_error *
5974 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5975 struct got_worktree *worktree, struct got_fileindex *fileindex,
5976 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5977 struct got_repository *repo,
5978 got_worktree_checkout_cb progress_cb, void *progress_arg,
5979 got_cancel_cb cancel_cb, void *cancel_arg)
5981 const struct got_error *err;
5982 char *commit_ref_name;
5984 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5988 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5992 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5993 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5994 progress_arg, cancel_cb, cancel_arg);
5996 free(commit_ref_name);
6000 static const struct got_error *
6001 rebase_commit(struct got_object_id **new_commit_id,
6002 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6003 struct got_worktree *worktree, struct got_fileindex *fileindex,
6004 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6005 const char *new_logmsg, struct got_repository *repo)
6007 const struct got_error *err, *sync_err;
6008 struct got_pathlist_head commitable_paths;
6009 struct collect_commitables_arg cc_arg;
6010 char *fileindex_path = NULL;
6011 struct got_reference *head_ref = NULL;
6012 struct got_object_id *head_commit_id = NULL;
6013 char *logmsg = NULL;
6015 TAILQ_INIT(&commitable_paths);
6016 *new_commit_id = NULL;
6018 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6020 err = get_fileindex_path(&fileindex_path, worktree);
6024 cc_arg.commitable_paths = &commitable_paths;
6025 cc_arg.worktree = worktree;
6027 cc_arg.have_staged_files = 0;
6029 * If possible get the status of individual files directly to
6030 * avoid crawling the entire work tree once per rebased commit.
6031 * TODO: Ideally, merged_paths would contain a list of commitables
6032 * we could use so we could skip worktree_status() entirely.
6035 struct got_pathlist_entry *pe;
6036 TAILQ_FOREACH(pe, merged_paths, entry) {
6037 err = worktree_status(worktree, pe->path, fileindex,
6038 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6044 err = worktree_status(worktree, "", fileindex, repo,
6045 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6050 if (TAILQ_EMPTY(&commitable_paths)) {
6051 /* No-op change; commit will be elided. */
6052 err = got_ref_delete(commit_ref, repo);
6055 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6059 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6063 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6068 logmsg = strdup(new_logmsg);
6069 if (logmsg == NULL) {
6070 err = got_error_from_errno("strdup");
6074 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6079 /* NB: commit_worktree will call free(logmsg) */
6080 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6081 worktree, got_object_commit_get_author(orig_commit),
6082 got_object_commit_get_committer(orig_commit),
6083 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6087 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6091 err = got_ref_delete(commit_ref, repo);
6095 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6097 sync_err = sync_fileindex(fileindex, fileindex_path);
6098 if (sync_err && err == NULL)
6101 free(fileindex_path);
6102 free(head_commit_id);
6104 got_ref_close(head_ref);
6106 free(*new_commit_id);
6107 *new_commit_id = NULL;
6112 const struct got_error *
6113 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6114 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6115 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6116 struct got_commit_object *orig_commit,
6117 struct got_object_id *orig_commit_id, struct got_repository *repo)
6119 const struct got_error *err;
6120 char *commit_ref_name;
6121 struct got_reference *commit_ref = NULL;
6122 struct got_object_id *commit_id = NULL;
6124 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6128 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6131 err = got_ref_resolve(&commit_id, repo, commit_ref);
6134 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6135 err = got_error(GOT_ERR_REBASE_COMMITID);
6139 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6140 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6143 got_ref_close(commit_ref);
6144 free(commit_ref_name);
6149 const struct got_error *
6150 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6151 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6152 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6153 struct got_commit_object *orig_commit,
6154 struct got_object_id *orig_commit_id, const char *new_logmsg,
6155 struct got_repository *repo)
6157 const struct got_error *err;
6158 char *commit_ref_name;
6159 struct got_reference *commit_ref = NULL;
6161 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6165 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6169 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6170 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6173 got_ref_close(commit_ref);
6174 free(commit_ref_name);
6178 const struct got_error *
6179 got_worktree_rebase_postpone(struct got_worktree *worktree,
6180 struct got_fileindex *fileindex)
6183 got_fileindex_free(fileindex);
6184 return lock_worktree(worktree, LOCK_SH);
6187 static const struct got_error *
6188 delete_ref(const char *name, struct got_repository *repo)
6190 const struct got_error *err;
6191 struct got_reference *ref;
6193 err = got_ref_open(&ref, repo, name, 0);
6195 if (err->code == GOT_ERR_NOT_REF)
6200 err = got_ref_delete(ref, repo);
6205 static const struct got_error *
6206 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6208 const struct got_error *err;
6209 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6210 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6212 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6215 err = delete_ref(tmp_branch_name, repo);
6219 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6222 err = delete_ref(new_base_branch_ref_name, repo);
6226 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6229 err = delete_ref(branch_ref_name, repo);
6233 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6236 err = delete_ref(commit_ref_name, repo);
6241 free(tmp_branch_name);
6242 free(new_base_branch_ref_name);
6243 free(branch_ref_name);
6244 free(commit_ref_name);
6248 const struct got_error *
6249 got_worktree_rebase_complete(struct got_worktree *worktree,
6250 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6251 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6252 struct got_repository *repo)
6254 const struct got_error *err, *unlockerr;
6255 struct got_object_id *new_head_commit_id = NULL;
6257 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6261 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6265 err = got_ref_write(rebased_branch, repo);
6269 err = got_worktree_set_head_ref(worktree, rebased_branch);
6273 err = delete_rebase_refs(worktree, repo);
6276 got_fileindex_free(fileindex);
6277 free(new_head_commit_id);
6278 unlockerr = lock_worktree(worktree, LOCK_SH);
6279 if (unlockerr && err == NULL)
6284 const struct got_error *
6285 got_worktree_rebase_abort(struct got_worktree *worktree,
6286 struct got_fileindex *fileindex, struct got_repository *repo,
6287 struct got_reference *new_base_branch,
6288 got_worktree_checkout_cb progress_cb, void *progress_arg)
6290 const struct got_error *err, *unlockerr, *sync_err;
6291 struct got_reference *resolved = NULL;
6292 struct got_object_id *commit_id = NULL;
6293 char *fileindex_path = NULL;
6294 struct revert_file_args rfa;
6295 struct got_object_id *tree_id = NULL;
6297 err = lock_worktree(worktree, LOCK_EX);
6301 err = got_ref_open(&resolved, repo,
6302 got_ref_get_symref_target(new_base_branch), 0);
6306 err = got_worktree_set_head_ref(worktree, resolved);
6311 * XXX commits to the base branch could have happened while
6312 * we were busy rebasing; should we store the original commit ID
6313 * when rebase begins and read it back here?
6315 err = got_ref_resolve(&commit_id, repo, resolved);
6319 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6323 err = got_object_id_by_path(&tree_id, repo,
6324 worktree->base_commit_id, worktree->path_prefix);
6328 err = delete_rebase_refs(worktree, repo);
6332 err = get_fileindex_path(&fileindex_path, worktree);
6336 rfa.worktree = worktree;
6337 rfa.fileindex = fileindex;
6338 rfa.progress_cb = progress_cb;
6339 rfa.progress_arg = progress_arg;
6340 rfa.patch_cb = NULL;
6341 rfa.patch_arg = NULL;
6343 err = worktree_status(worktree, "", fileindex, repo,
6344 revert_file, &rfa, NULL, NULL, 0, 0);
6348 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6349 repo, progress_cb, progress_arg, NULL, NULL);
6351 sync_err = sync_fileindex(fileindex, fileindex_path);
6352 if (sync_err && err == NULL)
6355 got_ref_close(resolved);
6359 got_fileindex_free(fileindex);
6360 free(fileindex_path);
6362 unlockerr = lock_worktree(worktree, LOCK_SH);
6363 if (unlockerr && err == NULL)
6368 const struct got_error *
6369 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6370 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6371 struct got_fileindex **fileindex, struct got_worktree *worktree,
6372 struct got_repository *repo)
6374 const struct got_error *err = NULL;
6375 char *tmp_branch_name = NULL;
6376 char *branch_ref_name = NULL;
6377 char *base_commit_ref_name = NULL;
6378 char *fileindex_path = NULL;
6379 struct check_rebase_ok_arg ok_arg;
6380 struct got_reference *wt_branch = NULL;
6381 struct got_reference *base_commit_ref = NULL;
6385 *base_commit_id = NULL;
6388 err = lock_worktree(worktree, LOCK_EX);
6392 err = open_fileindex(fileindex, &fileindex_path, worktree);
6396 ok_arg.worktree = worktree;
6398 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6403 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6407 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6411 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6416 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6421 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6425 err = got_ref_write(*branch_ref, repo);
6429 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6430 worktree->base_commit_id);
6433 err = got_ref_write(base_commit_ref, repo);
6436 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6437 if (*base_commit_id == NULL) {
6438 err = got_error_from_errno("got_object_id_dup");
6442 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6443 worktree->base_commit_id);
6446 err = got_ref_write(*tmp_branch, repo);
6450 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6454 free(fileindex_path);
6455 free(tmp_branch_name);
6456 free(branch_ref_name);
6457 free(base_commit_ref_name);
6459 got_ref_close(wt_branch);
6462 got_ref_close(*branch_ref);
6466 got_ref_close(*tmp_branch);
6469 free(*base_commit_id);
6471 got_fileindex_free(*fileindex);
6474 lock_worktree(worktree, LOCK_SH);
6479 const struct got_error *
6480 got_worktree_histedit_postpone(struct got_worktree *worktree,
6481 struct got_fileindex *fileindex)
6484 got_fileindex_free(fileindex);
6485 return lock_worktree(worktree, LOCK_SH);
6488 const struct got_error *
6489 got_worktree_histedit_in_progress(int *in_progress,
6490 struct got_worktree *worktree)
6492 const struct got_error *err;
6493 char *tmp_branch_name = NULL;
6495 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6499 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6500 free(tmp_branch_name);
6504 const struct got_error *
6505 got_worktree_histedit_continue(struct got_object_id **commit_id,
6506 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6507 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6508 struct got_worktree *worktree, struct got_repository *repo)
6510 const struct got_error *err;
6511 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6512 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6513 struct got_reference *commit_ref = NULL;
6514 struct got_reference *base_commit_ref = NULL;
6515 char *fileindex_path = NULL;
6516 int have_staged_files = 0;
6520 *base_commit_id = NULL;
6523 err = lock_worktree(worktree, LOCK_EX);
6527 err = open_fileindex(fileindex, &fileindex_path, worktree);
6531 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6532 &have_staged_files);
6533 if (err && err->code != GOT_ERR_CANCELLED)
6535 if (have_staged_files) {
6536 err = got_error(GOT_ERR_STAGED_PATHS);
6540 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6544 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6548 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6552 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6557 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6561 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6564 err = got_ref_resolve(commit_id, repo, commit_ref);
6568 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6571 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6575 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6579 free(commit_ref_name);
6580 free(branch_ref_name);
6581 free(fileindex_path);
6583 got_ref_close(commit_ref);
6584 if (base_commit_ref)
6585 got_ref_close(base_commit_ref);
6589 free(*base_commit_id);
6590 *base_commit_id = NULL;
6592 got_ref_close(*tmp_branch);
6596 got_fileindex_free(*fileindex);
6599 lock_worktree(worktree, LOCK_EX);
6604 static const struct got_error *
6605 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6607 const struct got_error *err;
6608 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6609 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6611 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6614 err = delete_ref(tmp_branch_name, repo);
6618 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6622 err = delete_ref(base_commit_ref_name, repo);
6626 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6629 err = delete_ref(branch_ref_name, repo);
6633 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6636 err = delete_ref(commit_ref_name, repo);
6640 free(tmp_branch_name);
6641 free(base_commit_ref_name);
6642 free(branch_ref_name);
6643 free(commit_ref_name);
6647 const struct got_error *
6648 got_worktree_histedit_abort(struct got_worktree *worktree,
6649 struct got_fileindex *fileindex, struct got_repository *repo,
6650 struct got_reference *branch, struct got_object_id *base_commit_id,
6651 got_worktree_checkout_cb progress_cb, void *progress_arg)
6653 const struct got_error *err, *unlockerr, *sync_err;
6654 struct got_reference *resolved = NULL;
6655 char *fileindex_path = NULL;
6656 struct got_object_id *tree_id = NULL;
6657 struct revert_file_args rfa;
6659 err = lock_worktree(worktree, LOCK_EX);
6663 err = got_ref_open(&resolved, repo,
6664 got_ref_get_symref_target(branch), 0);
6668 err = got_worktree_set_head_ref(worktree, resolved);
6672 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6676 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6677 worktree->path_prefix);
6681 err = delete_histedit_refs(worktree, repo);
6685 err = get_fileindex_path(&fileindex_path, worktree);
6689 rfa.worktree = worktree;
6690 rfa.fileindex = fileindex;
6691 rfa.progress_cb = progress_cb;
6692 rfa.progress_arg = progress_arg;
6693 rfa.patch_cb = NULL;
6694 rfa.patch_arg = NULL;
6696 err = worktree_status(worktree, "", fileindex, repo,
6697 revert_file, &rfa, NULL, NULL, 0, 0);
6701 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6702 repo, progress_cb, progress_arg, NULL, NULL);
6704 sync_err = sync_fileindex(fileindex, fileindex_path);
6705 if (sync_err && err == NULL)
6708 got_ref_close(resolved);
6710 free(fileindex_path);
6712 unlockerr = lock_worktree(worktree, LOCK_SH);
6713 if (unlockerr && err == NULL)
6718 const struct got_error *
6719 got_worktree_histedit_complete(struct got_worktree *worktree,
6720 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6721 struct got_reference *edited_branch, struct got_repository *repo)
6723 const struct got_error *err, *unlockerr;
6724 struct got_object_id *new_head_commit_id = NULL;
6725 struct got_reference *resolved = NULL;
6727 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6731 err = got_ref_open(&resolved, repo,
6732 got_ref_get_symref_target(edited_branch), 0);
6736 err = got_ref_change_ref(resolved, new_head_commit_id);
6740 err = got_ref_write(resolved, repo);
6744 err = got_worktree_set_head_ref(worktree, resolved);
6748 err = delete_histedit_refs(worktree, repo);
6751 got_fileindex_free(fileindex);
6752 free(new_head_commit_id);
6753 unlockerr = lock_worktree(worktree, LOCK_SH);
6754 if (unlockerr && err == NULL)
6759 const struct got_error *
6760 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6761 struct got_object_id *commit_id, struct got_repository *repo)
6763 const struct got_error *err;
6764 char *commit_ref_name;
6766 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6770 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6774 err = delete_ref(commit_ref_name, repo);
6776 free(commit_ref_name);
6780 const struct got_error *
6781 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6782 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6783 struct got_worktree *worktree, const char *refname,
6784 struct got_repository *repo)
6786 const struct got_error *err = NULL;
6787 char *fileindex_path = NULL;
6788 struct check_rebase_ok_arg ok_arg;
6792 *base_branch_ref = NULL;
6794 err = lock_worktree(worktree, LOCK_EX);
6798 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6799 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6800 "cannot integrate a branch into itself; "
6801 "update -b or different branch name required");
6805 err = open_fileindex(fileindex, &fileindex_path, worktree);
6809 /* Preconditions are the same as for rebase. */
6810 ok_arg.worktree = worktree;
6812 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6817 err = got_ref_open(branch_ref, repo, refname, 1);
6821 err = got_ref_open(base_branch_ref, repo,
6822 got_worktree_get_head_ref_name(worktree), 1);
6826 got_ref_close(*branch_ref);
6829 if (*base_branch_ref) {
6830 got_ref_close(*base_branch_ref);
6831 *base_branch_ref = NULL;
6834 got_fileindex_free(*fileindex);
6837 lock_worktree(worktree, LOCK_SH);
6842 const struct got_error *
6843 got_worktree_integrate_continue(struct got_worktree *worktree,
6844 struct got_fileindex *fileindex, struct got_repository *repo,
6845 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6846 got_worktree_checkout_cb progress_cb, void *progress_arg,
6847 got_cancel_cb cancel_cb, void *cancel_arg)
6849 const struct got_error *err = NULL, *sync_err, *unlockerr;
6850 char *fileindex_path = NULL;
6851 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6853 err = get_fileindex_path(&fileindex_path, worktree);
6857 err = got_ref_resolve(&commit_id, repo, branch_ref);
6861 err = got_object_id_by_path(&tree_id, repo, commit_id,
6862 worktree->path_prefix);
6866 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6870 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6871 progress_cb, progress_arg, cancel_cb, cancel_arg);
6875 err = got_ref_change_ref(base_branch_ref, commit_id);
6879 err = got_ref_write(base_branch_ref, repo);
6881 sync_err = sync_fileindex(fileindex, fileindex_path);
6882 if (sync_err && err == NULL)
6886 unlockerr = got_ref_unlock(branch_ref);
6887 if (unlockerr && err == NULL)
6889 got_ref_close(branch_ref);
6891 unlockerr = got_ref_unlock(base_branch_ref);
6892 if (unlockerr && err == NULL)
6894 got_ref_close(base_branch_ref);
6896 got_fileindex_free(fileindex);
6897 free(fileindex_path);
6900 unlockerr = lock_worktree(worktree, LOCK_SH);
6901 if (unlockerr && err == NULL)
6906 const struct got_error *
6907 got_worktree_integrate_abort(struct got_worktree *worktree,
6908 struct got_fileindex *fileindex, struct got_repository *repo,
6909 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6911 const struct got_error *err = NULL, *unlockerr = NULL;
6913 got_fileindex_free(fileindex);
6915 err = lock_worktree(worktree, LOCK_SH);
6917 unlockerr = got_ref_unlock(branch_ref);
6918 if (unlockerr && err == NULL)
6920 got_ref_close(branch_ref);
6922 unlockerr = got_ref_unlock(base_branch_ref);
6923 if (unlockerr && err == NULL)
6925 got_ref_close(base_branch_ref);
6930 struct check_stage_ok_arg {
6931 struct got_object_id *head_commit_id;
6932 struct got_worktree *worktree;
6933 struct got_fileindex *fileindex;
6934 struct got_repository *repo;
6938 const struct got_error *
6939 check_stage_ok(void *arg, unsigned char status,
6940 unsigned char staged_status, const char *relpath,
6941 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6942 struct got_object_id *commit_id, int dirfd, const char *de_name)
6944 struct check_stage_ok_arg *a = arg;
6945 const struct got_error *err = NULL;
6946 struct got_fileindex_entry *ie;
6947 struct got_object_id base_commit_id;
6948 struct got_object_id *base_commit_idp = NULL;
6949 char *in_repo_path = NULL, *p;
6951 if (status == GOT_STATUS_UNVERSIONED ||
6952 status == GOT_STATUS_NO_CHANGE)
6954 if (status == GOT_STATUS_NONEXISTENT)
6955 return got_error_set_errno(ENOENT, relpath);
6957 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6959 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6961 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6962 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6964 return got_error_from_errno("asprintf");
6966 if (got_fileindex_entry_has_commit(ie)) {
6967 memcpy(base_commit_id.sha1, ie->commit_sha1,
6968 SHA1_DIGEST_LENGTH);
6969 base_commit_idp = &base_commit_id;
6972 if (status == GOT_STATUS_CONFLICT) {
6973 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6975 } else if (status != GOT_STATUS_ADD &&
6976 status != GOT_STATUS_MODIFY &&
6977 status != GOT_STATUS_DELETE) {
6978 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6982 a->have_changes = 1;
6987 err = check_out_of_date(p, status, staged_status,
6988 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6989 GOT_ERR_STAGE_OUT_OF_DATE);
6995 struct stage_path_arg {
6996 struct got_worktree *worktree;
6997 struct got_fileindex *fileindex;
6998 struct got_repository *repo;
6999 got_worktree_status_cb status_cb;
7001 got_worktree_patch_cb patch_cb;
7003 int staged_something;
7004 int allow_bad_symlinks;
7007 static const struct got_error *
7008 stage_path(void *arg, unsigned char status,
7009 unsigned char staged_status, const char *relpath,
7010 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7011 struct got_object_id *commit_id, int dirfd, const char *de_name)
7013 struct stage_path_arg *a = arg;
7014 const struct got_error *err = NULL;
7015 struct got_fileindex_entry *ie;
7016 char *ondisk_path = NULL, *path_content = NULL;
7018 struct got_object_id *new_staged_blob_id = NULL;
7021 if (status == GOT_STATUS_UNVERSIONED)
7024 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7026 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7028 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7030 return got_error_from_errno("asprintf");
7033 case GOT_STATUS_ADD:
7034 case GOT_STATUS_MODIFY:
7035 /* XXX could sb.st_mode be passed in by our caller? */
7036 if (lstat(ondisk_path, &sb) == -1) {
7037 err = got_error_from_errno2("lstat", ondisk_path);
7041 if (status == GOT_STATUS_ADD) {
7042 int choice = GOT_PATCH_CHOICE_NONE;
7043 err = (*a->patch_cb)(&choice, a->patch_arg,
7044 status, ie->path, NULL, 1, 1);
7047 if (choice != GOT_PATCH_CHOICE_YES)
7050 err = create_patched_content(&path_content, 0,
7051 staged_blob_id ? staged_blob_id : blob_id,
7052 ondisk_path, dirfd, de_name, ie->path,
7053 a->repo, a->patch_cb, a->patch_arg);
7054 if (err || path_content == NULL)
7058 err = got_object_blob_create(&new_staged_blob_id,
7059 path_content ? path_content : ondisk_path, a->repo);
7062 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7063 SHA1_DIGEST_LENGTH);
7064 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7065 stage = GOT_FILEIDX_STAGE_ADD;
7067 stage = GOT_FILEIDX_STAGE_MODIFY;
7068 got_fileindex_entry_stage_set(ie, stage);
7069 if (S_ISLNK(sb.st_mode)) {
7070 int is_bad_symlink = 0;
7071 if (!a->allow_bad_symlinks) {
7072 char target_path[PATH_MAX];
7074 target_len = readlink(ondisk_path, target_path,
7075 sizeof(target_path));
7076 if (target_len == -1) {
7077 err = got_error_from_errno2("readlink",
7081 err = is_bad_symlink_target(&is_bad_symlink,
7082 target_path, target_len, ondisk_path,
7083 a->worktree->root_path);
7086 if (is_bad_symlink) {
7087 err = got_error_path(ondisk_path,
7088 GOT_ERR_BAD_SYMLINK);
7093 got_fileindex_entry_staged_filetype_set(ie,
7094 GOT_FILEIDX_MODE_BAD_SYMLINK);
7096 got_fileindex_entry_staged_filetype_set(ie,
7097 GOT_FILEIDX_MODE_SYMLINK);
7099 got_fileindex_entry_staged_filetype_set(ie,
7100 GOT_FILEIDX_MODE_REGULAR_FILE);
7102 a->staged_something = 1;
7103 if (a->status_cb == NULL)
7105 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7106 get_staged_status(ie), relpath, blob_id,
7107 new_staged_blob_id, NULL, dirfd, de_name);
7109 case GOT_STATUS_DELETE:
7110 if (staged_status == GOT_STATUS_DELETE)
7113 int choice = GOT_PATCH_CHOICE_NONE;
7114 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7115 ie->path, NULL, 1, 1);
7118 if (choice == GOT_PATCH_CHOICE_NO)
7120 if (choice != GOT_PATCH_CHOICE_YES) {
7121 err = got_error(GOT_ERR_PATCH_CHOICE);
7125 stage = GOT_FILEIDX_STAGE_DELETE;
7126 got_fileindex_entry_stage_set(ie, stage);
7127 a->staged_something = 1;
7128 if (a->status_cb == NULL)
7130 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7131 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7134 case GOT_STATUS_NO_CHANGE:
7136 case GOT_STATUS_CONFLICT:
7137 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7139 case GOT_STATUS_NONEXISTENT:
7140 err = got_error_set_errno(ENOENT, relpath);
7143 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7147 if (path_content && unlink(path_content) == -1 && err == NULL)
7148 err = got_error_from_errno2("unlink", path_content);
7151 free(new_staged_blob_id);
7155 const struct got_error *
7156 got_worktree_stage(struct got_worktree *worktree,
7157 struct got_pathlist_head *paths,
7158 got_worktree_status_cb status_cb, void *status_arg,
7159 got_worktree_patch_cb patch_cb, void *patch_arg,
7160 int allow_bad_symlinks, struct got_repository *repo)
7162 const struct got_error *err = NULL, *sync_err, *unlockerr;
7163 struct got_pathlist_entry *pe;
7164 struct got_fileindex *fileindex = NULL;
7165 char *fileindex_path = NULL;
7166 struct got_reference *head_ref = NULL;
7167 struct got_object_id *head_commit_id = NULL;
7168 struct check_stage_ok_arg oka;
7169 struct stage_path_arg spa;
7171 err = lock_worktree(worktree, LOCK_EX);
7175 err = got_ref_open(&head_ref, repo,
7176 got_worktree_get_head_ref_name(worktree), 0);
7179 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7182 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7186 /* Check pre-conditions before staging anything. */
7187 oka.head_commit_id = head_commit_id;
7188 oka.worktree = worktree;
7189 oka.fileindex = fileindex;
7191 oka.have_changes = 0;
7192 TAILQ_FOREACH(pe, paths, entry) {
7193 err = worktree_status(worktree, pe->path, fileindex, repo,
7194 check_stage_ok, &oka, NULL, NULL, 0, 0);
7198 if (!oka.have_changes) {
7199 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7203 spa.worktree = worktree;
7204 spa.fileindex = fileindex;
7206 spa.patch_cb = patch_cb;
7207 spa.patch_arg = patch_arg;
7208 spa.status_cb = status_cb;
7209 spa.status_arg = status_arg;
7210 spa.staged_something = 0;
7211 spa.allow_bad_symlinks = allow_bad_symlinks;
7212 TAILQ_FOREACH(pe, paths, entry) {
7213 err = worktree_status(worktree, pe->path, fileindex, repo,
7214 stage_path, &spa, NULL, NULL, 0, 0);
7218 if (!spa.staged_something) {
7219 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7223 sync_err = sync_fileindex(fileindex, fileindex_path);
7224 if (sync_err && err == NULL)
7228 got_ref_close(head_ref);
7229 free(head_commit_id);
7230 free(fileindex_path);
7232 got_fileindex_free(fileindex);
7233 unlockerr = lock_worktree(worktree, LOCK_SH);
7234 if (unlockerr && err == NULL)
7239 struct unstage_path_arg {
7240 struct got_worktree *worktree;
7241 struct got_fileindex *fileindex;
7242 struct got_repository *repo;
7243 got_worktree_checkout_cb progress_cb;
7245 got_worktree_patch_cb patch_cb;
7249 static const struct got_error *
7250 create_unstaged_content(char **path_unstaged_content,
7251 char **path_new_staged_content, struct got_object_id *blob_id,
7252 struct got_object_id *staged_blob_id, const char *relpath,
7253 struct got_repository *repo,
7254 got_worktree_patch_cb patch_cb, void *patch_arg)
7256 const struct got_error *err;
7257 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7258 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7259 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7260 struct stat sb1, sb2;
7261 struct got_diff_changes *changes = NULL;
7262 struct got_diff_state *ds = NULL;
7263 struct got_diff_args *args = NULL;
7264 struct got_diff_change *change;
7265 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7266 int have_content = 0, have_rejected_content = 0;
7268 *path_unstaged_content = NULL;
7269 *path_new_staged_content = NULL;
7271 err = got_object_id_str(&label1, blob_id);
7274 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7278 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7282 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7286 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7290 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7294 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7298 if (stat(path1, &sb1) == -1) {
7299 err = got_error_from_errno2("stat", path1);
7303 if (stat(path2, &sb2) == -1) {
7304 err = got_error_from_errno2("stat", path2);
7308 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7309 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7313 err = got_opentemp_named(path_unstaged_content, &outfile,
7314 "got-unstaged-content");
7317 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7318 "got-new-staged-content");
7322 if (fseek(f1, 0L, SEEK_SET) == -1) {
7323 err = got_ferror(f1, GOT_ERR_IO);
7326 if (fseek(f2, 0L, SEEK_SET) == -1) {
7327 err = got_ferror(f2, GOT_ERR_IO);
7330 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7332 err = apply_or_reject_change(&choice, change, ++n,
7333 changes->nchanges, ds, args, diff_flags, relpath,
7334 f1, f2, &line_cur1, &line_cur2,
7335 outfile, rejectfile, patch_cb, patch_arg);
7338 if (choice == GOT_PATCH_CHOICE_YES)
7341 have_rejected_content = 1;
7342 if (choice == GOT_PATCH_CHOICE_QUIT)
7345 if (have_content || have_rejected_content)
7346 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7347 outfile, rejectfile);
7351 got_object_blob_close(blob);
7353 got_object_blob_close(staged_blob);
7354 if (f1 && fclose(f1) == EOF && err == NULL)
7355 err = got_error_from_errno2("fclose", path1);
7356 if (f2 && fclose(f2) == EOF && err == NULL)
7357 err = got_error_from_errno2("fclose", path2);
7358 if (outfile && fclose(outfile) == EOF && err == NULL)
7359 err = got_error_from_errno2("fclose", *path_unstaged_content);
7360 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7361 err = got_error_from_errno2("fclose", *path_new_staged_content);
7362 if (path1 && unlink(path1) == -1 && err == NULL)
7363 err = got_error_from_errno2("unlink", path1);
7364 if (path2 && unlink(path2) == -1 && err == NULL)
7365 err = got_error_from_errno2("unlink", path2);
7366 if (err || !have_content) {
7367 if (*path_unstaged_content &&
7368 unlink(*path_unstaged_content) == -1 && err == NULL)
7369 err = got_error_from_errno2("unlink",
7370 *path_unstaged_content);
7371 free(*path_unstaged_content);
7372 *path_unstaged_content = NULL;
7374 if (err || !have_content || !have_rejected_content) {
7375 if (*path_new_staged_content &&
7376 unlink(*path_new_staged_content) == -1 && err == NULL)
7377 err = got_error_from_errno2("unlink",
7378 *path_new_staged_content);
7379 free(*path_new_staged_content);
7380 *path_new_staged_content = NULL;
7384 got_diff_state_free(ds);
7388 got_diff_free_changes(changes);
7394 static const struct got_error *
7395 unstage_hunks(struct got_object_id *staged_blob_id,
7396 struct got_blob_object *blob_base,
7397 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7398 const char *ondisk_path, const char *label_orig,
7399 struct got_worktree *worktree, struct got_repository *repo,
7400 got_worktree_patch_cb patch_cb, void *patch_arg,
7401 got_worktree_checkout_cb progress_cb, void *progress_arg)
7403 const struct got_error *err = NULL;
7404 char *path_unstaged_content = NULL;
7405 char *path_new_staged_content = NULL;
7406 struct got_object_id *new_staged_blob_id = NULL;
7410 err = create_unstaged_content(&path_unstaged_content,
7411 &path_new_staged_content, blob_id, staged_blob_id,
7412 ie->path, repo, patch_cb, patch_arg);
7416 if (path_unstaged_content == NULL)
7419 if (path_new_staged_content) {
7420 err = got_object_blob_create(&new_staged_blob_id,
7421 path_new_staged_content, repo);
7426 f = fopen(path_unstaged_content, "r");
7428 err = got_error_from_errno2("fopen",
7429 path_unstaged_content);
7432 if (fstat(fileno(f), &sb) == -1) {
7433 err = got_error_from_errno2("fstat", path_unstaged_content);
7436 if (got_fileindex_entry_staged_filetype_get(ie) ==
7437 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7438 char link_target[PATH_MAX];
7440 r = fread(link_target, 1, sizeof(link_target), f);
7441 if (r == 0 && ferror(f)) {
7442 err = got_error_from_errno("fread");
7445 if (r >= sizeof(link_target)) { /* should not happen */
7446 err = got_error(GOT_ERR_NO_SPACE);
7449 link_target[r] = '\0';
7450 err = merge_symlink(worktree, blob_base,
7451 ondisk_path, ie->path, label_orig, link_target,
7452 worktree->base_commit_id, repo, progress_cb,
7455 int local_changes_subsumed;
7456 err = merge_file(&local_changes_subsumed, worktree,
7457 blob_base, ondisk_path, ie->path,
7458 got_fileindex_perms_to_st(ie),
7459 path_unstaged_content, label_orig, "unstaged",
7460 repo, progress_cb, progress_arg);
7465 if (new_staged_blob_id) {
7466 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7467 SHA1_DIGEST_LENGTH);
7469 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7471 free(new_staged_blob_id);
7472 if (path_unstaged_content &&
7473 unlink(path_unstaged_content) == -1 && err == NULL)
7474 err = got_error_from_errno2("unlink", path_unstaged_content);
7475 if (path_new_staged_content &&
7476 unlink(path_new_staged_content) == -1 && err == NULL)
7477 err = got_error_from_errno2("unlink", path_new_staged_content);
7478 if (f && fclose(f) != 0 && err == NULL)
7479 err = got_error_from_errno2("fclose", path_unstaged_content);
7480 free(path_unstaged_content);
7481 free(path_new_staged_content);
7485 static const struct got_error *
7486 unstage_path(void *arg, unsigned char status,
7487 unsigned char staged_status, const char *relpath,
7488 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7489 struct got_object_id *commit_id, int dirfd, const char *de_name)
7491 const struct got_error *err = NULL;
7492 struct unstage_path_arg *a = arg;
7493 struct got_fileindex_entry *ie;
7494 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7495 char *ondisk_path = NULL;
7496 char *id_str = NULL, *label_orig = NULL;
7497 int local_changes_subsumed;
7500 if (staged_status != GOT_STATUS_ADD &&
7501 staged_status != GOT_STATUS_MODIFY &&
7502 staged_status != GOT_STATUS_DELETE)
7505 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7507 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7509 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7511 return got_error_from_errno("asprintf");
7513 err = got_object_id_str(&id_str,
7514 commit_id ? commit_id : a->worktree->base_commit_id);
7517 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7519 err = got_error_from_errno("asprintf");
7523 switch (staged_status) {
7524 case GOT_STATUS_MODIFY:
7525 err = got_object_open_as_blob(&blob_base, a->repo,
7530 case GOT_STATUS_ADD:
7532 if (staged_status == GOT_STATUS_ADD) {
7533 int choice = GOT_PATCH_CHOICE_NONE;
7534 err = (*a->patch_cb)(&choice, a->patch_arg,
7535 staged_status, ie->path, NULL, 1, 1);
7538 if (choice != GOT_PATCH_CHOICE_YES)
7541 err = unstage_hunks(staged_blob_id,
7542 blob_base, blob_id, ie, ondisk_path,
7543 label_orig, a->worktree, a->repo,
7544 a->patch_cb, a->patch_arg,
7545 a->progress_cb, a->progress_arg);
7546 break; /* Done with this file. */
7549 err = got_object_open_as_blob(&blob_staged, a->repo,
7550 staged_blob_id, 8192);
7553 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7554 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7555 case GOT_FILEIDX_MODE_REGULAR_FILE:
7556 err = merge_blob(&local_changes_subsumed, a->worktree,
7557 blob_base, ondisk_path, relpath,
7558 got_fileindex_perms_to_st(ie), label_orig,
7559 blob_staged, commit_id ? commit_id :
7560 a->worktree->base_commit_id, a->repo,
7561 a->progress_cb, a->progress_arg);
7563 case GOT_FILEIDX_MODE_SYMLINK:
7564 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7565 char *staged_target;
7566 err = got_object_blob_read_to_str(
7567 &staged_target, blob_staged);
7570 err = merge_symlink(a->worktree, blob_base,
7571 ondisk_path, relpath, label_orig,
7572 staged_target, commit_id ? commit_id :
7573 a->worktree->base_commit_id,
7574 a->repo, a->progress_cb, a->progress_arg);
7575 free(staged_target);
7577 err = merge_blob(&local_changes_subsumed,
7578 a->worktree, blob_base, ondisk_path,
7579 relpath, got_fileindex_perms_to_st(ie),
7580 label_orig, blob_staged,
7581 commit_id ? commit_id :
7582 a->worktree->base_commit_id, a->repo,
7583 a->progress_cb, a->progress_arg);
7587 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7591 got_fileindex_entry_stage_set(ie,
7592 GOT_FILEIDX_STAGE_NONE);
7594 case GOT_STATUS_DELETE:
7596 int choice = GOT_PATCH_CHOICE_NONE;
7597 err = (*a->patch_cb)(&choice, a->patch_arg,
7598 staged_status, ie->path, NULL, 1, 1);
7601 if (choice == GOT_PATCH_CHOICE_NO)
7603 if (choice != GOT_PATCH_CHOICE_YES) {
7604 err = got_error(GOT_ERR_PATCH_CHOICE);
7608 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7609 err = get_file_status(&status, &sb, ie, ondisk_path,
7610 dirfd, de_name, a->repo);
7613 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7619 got_object_blob_close(blob_base);
7621 got_object_blob_close(blob_staged);
7627 const struct got_error *
7628 got_worktree_unstage(struct got_worktree *worktree,
7629 struct got_pathlist_head *paths,
7630 got_worktree_checkout_cb progress_cb, void *progress_arg,
7631 got_worktree_patch_cb patch_cb, void *patch_arg,
7632 struct got_repository *repo)
7634 const struct got_error *err = NULL, *sync_err, *unlockerr;
7635 struct got_pathlist_entry *pe;
7636 struct got_fileindex *fileindex = NULL;
7637 char *fileindex_path = NULL;
7638 struct unstage_path_arg upa;
7640 err = lock_worktree(worktree, LOCK_EX);
7644 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7648 upa.worktree = worktree;
7649 upa.fileindex = fileindex;
7651 upa.progress_cb = progress_cb;
7652 upa.progress_arg = progress_arg;
7653 upa.patch_cb = patch_cb;
7654 upa.patch_arg = patch_arg;
7655 TAILQ_FOREACH(pe, paths, entry) {
7656 err = worktree_status(worktree, pe->path, fileindex, repo,
7657 unstage_path, &upa, NULL, NULL, 0, 0);
7662 sync_err = sync_fileindex(fileindex, fileindex_path);
7663 if (sync_err && err == NULL)
7666 free(fileindex_path);
7668 got_fileindex_free(fileindex);
7669 unlockerr = lock_worktree(worktree, LOCK_SH);
7670 if (unlockerr && err == NULL)