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;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
833 static const struct got_error *
834 update_symlink(const char *ondisk_path, const char *target_path,
837 /* This is not atomic but matches what 'ln -sf' does. */
838 if (unlink(ondisk_path) == -1)
839 return got_error_from_errno2("unlink", ondisk_path);
840 if (symlink(target_path, ondisk_path) == -1)
841 return got_error_from_errno3("symlink", target_path,
847 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
848 * in the work tree with a file that contains conflict markers and the
849 * conflicting target paths of the original version, a "derived version"
850 * of a symlink from an incoming change, and a local version of the symlink.
852 * The original versions's target path can be NULL if it is not available,
853 * such as if both derived versions added a new symlink at the same path.
855 * The incoming derived symlink target is NULL in case the incoming change
856 * has deleted this symlink.
858 static const struct got_error *
859 install_symlink_conflict(const char *deriv_target,
860 struct got_object_id *deriv_base_commit_id, const char *orig_target,
861 const char *label_orig, const char *local_target, const char *ondisk_path)
863 const struct got_error *err;
864 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
867 err = got_object_id_str(&id_str, deriv_base_commit_id);
869 return got_error_from_errno("asprintf");
871 if (asprintf(&label_deriv, "%s: commit %s",
872 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
873 err = got_error_from_errno("asprintf");
877 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
881 if (fprintf(f, "%s: Could not install symbolic link because of merge "
882 "conflict.\nln(1) may be used to fix the situation. If this is "
883 "intended to be a\nregular file instead then its expected "
884 "contents may be filled in.\nThe following conflicting symlink "
885 "target paths were found:\n"
886 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
887 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
888 deriv_target ? deriv_target : "(symlink was deleted)",
889 orig_target ? label_orig : "",
890 orig_target ? "\n" : "",
891 orig_target ? orig_target : "",
892 orig_target ? "\n" : "",
893 GOT_DIFF_CONFLICT_MARKER_SEP,
894 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
895 err = got_error_from_errno2("fprintf", path);
899 if (unlink(ondisk_path) == -1) {
900 err = got_error_from_errno2("unlink", ondisk_path);
903 if (rename(path, ondisk_path) == -1) {
904 err = got_error_from_errno3("rename", path, ondisk_path);
907 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
908 err = got_error_from_errno2("chmod", ondisk_path);
912 if (f != NULL && fclose(f) == EOF && err == NULL)
913 err = got_error_from_errno2("fclose", path);
920 /* forward declaration */
921 static const struct got_error *
922 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
923 const char *, const char *, uint16_t, const char *,
924 struct got_blob_object *, struct got_object_id *,
925 struct got_repository *, got_worktree_checkout_cb, void *);
928 * Merge a symlink into the work tree, where blob_orig acts as the common
929 * ancestor, blob_deriv acts as the first derived version, and the symlink
930 * on disk acts as the second derived version.
931 * Assume that contents of both blobs represent symlinks.
933 static const struct got_error *
934 merge_symlink(struct got_worktree *worktree,
935 struct got_blob_object *blob_orig, const char *ondisk_path,
936 const char *path, uint16_t st_mode, const char *label_orig,
937 struct got_blob_object *blob_deriv,
938 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
939 got_worktree_checkout_cb progress_cb, void *progress_arg)
941 const struct got_error *err = NULL;
942 char *ancestor_target = NULL, *deriv_target = NULL;
944 ssize_t ondisk_len, deriv_len;
945 char ondisk_target[PATH_MAX];
946 int have_local_change = 0;
947 int have_incoming_change = 0;
949 if (lstat(ondisk_path, &sb) == -1)
950 return got_error_from_errno2("lstat", ondisk_path);
952 if (!S_ISLNK(sb.st_mode)) {
954 * If there is a regular file on disk, merge the symlink
955 * target path into this file, which will usually cause
958 if (S_ISREG(sb.st_mode)) {
959 int local_changes_subsumed;
960 return merge_blob(&local_changes_subsumed, worktree,
961 NULL, ondisk_path, path, sb.st_mode, label_orig,
962 blob_deriv, deriv_base_commit_id,
963 repo, progress_cb, progress_arg);
966 /* TODO symlink is obstructed; do something */
967 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
970 ondisk_len = readlink(ondisk_path, ondisk_target,
971 sizeof(ondisk_target));
972 if (ondisk_len == -1) {
973 err = got_error_from_errno2("readlink",
977 ondisk_target[ondisk_len] = '\0';
980 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
985 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
989 if (ancestor_target == NULL ||
990 (ondisk_len != strlen(ancestor_target) ||
991 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
992 have_local_change = 1;
994 deriv_len = strlen(deriv_target);
995 if (ancestor_target == NULL ||
996 (deriv_len != strlen(ancestor_target) ||
997 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
998 have_incoming_change = 1;
1000 if (!have_local_change && !have_incoming_change) {
1001 if (ancestor_target) {
1002 /* Both sides made the same change. */
1003 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1005 } else if (deriv_len == ondisk_len &&
1006 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1007 /* Both sides added the same symlink. */
1008 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1011 /* Both sides added symlinks which don't match. */
1012 err = install_symlink_conflict(deriv_target,
1013 deriv_base_commit_id, ancestor_target,
1014 label_orig, ondisk_target, ondisk_path);
1017 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1020 } else if (!have_local_change && have_incoming_change) {
1021 /* Apply the incoming change. */
1022 err = update_symlink(ondisk_path, deriv_target,
1023 strlen(deriv_target));
1026 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1027 } else if (have_local_change && have_incoming_change) {
1028 err = install_symlink_conflict(deriv_target,
1029 deriv_base_commit_id, ancestor_target, label_orig,
1030 ondisk_target, ondisk_path);
1033 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1038 free(ancestor_target);
1044 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1045 * blob_deriv acts as the first derived version, and the file on disk
1046 * acts as the second derived version.
1048 static const struct got_error *
1049 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1050 struct got_blob_object *blob_orig, const char *ondisk_path,
1051 const char *path, uint16_t st_mode, const char *label_orig,
1052 struct got_blob_object *blob_deriv,
1053 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1054 got_worktree_checkout_cb progress_cb, void *progress_arg)
1056 const struct got_error *err = NULL;
1057 FILE *f_deriv = NULL;
1058 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1059 char *label_deriv = NULL, *parent;
1061 *local_changes_subsumed = 0;
1063 parent = dirname(ondisk_path);
1065 return got_error_from_errno2("dirname", ondisk_path);
1068 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1069 err = got_error_from_errno("asprintf");
1074 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1077 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1082 err = got_object_id_str(&id_str, deriv_base_commit_id);
1085 if (asprintf(&label_deriv, "%s: commit %s",
1086 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1087 err = got_error_from_errno("asprintf");
1091 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1092 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1093 label_deriv, repo, progress_cb, progress_arg);
1095 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1096 err = got_error_from_errno("fclose");
1098 if (blob_deriv_path) {
1099 unlink(blob_deriv_path);
1100 free(blob_deriv_path);
1107 static const struct got_error *
1108 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1109 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1110 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1112 const struct got_error *err = NULL;
1113 struct got_fileindex_entry *new_ie;
1117 err = got_fileindex_entry_alloc(&new_ie, path);
1121 err = got_fileindex_entry_update(new_ie, ondisk_path,
1122 blob_id->sha1, base_commit_id->sha1, 1);
1126 err = got_fileindex_entry_add(fileindex, new_ie);
1129 got_fileindex_entry_free(new_ie);
1136 get_ondisk_perms(int executable, mode_t st_mode)
1138 mode_t xbits = S_IXUSR;
1141 /* Map read bits to execute bits. */
1142 if (st_mode & S_IRGRP)
1144 if (st_mode & S_IROTH)
1146 return st_mode | xbits;
1149 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1152 /* forward declaration */
1153 static const struct got_error *
1154 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1155 const char *path, mode_t te_mode, mode_t st_mode,
1156 struct got_blob_object *blob, int restoring_missing_file,
1157 int reverting_versioned_file, int installing_bad_symlink,
1158 struct got_repository *repo,
1159 got_worktree_checkout_cb progress_cb, void *progress_arg);
1162 * This function assumes that the provided symlink target points at a
1163 * safe location in the work tree!
1165 static const struct got_error *
1166 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1169 const struct got_error *err = NULL;
1171 char etarget[PATH_MAX];
1175 * "Bad" symlinks (those pointing outside the work tree or into the
1176 * .got directory) are installed in the work tree as a regular file
1177 * which contains the bad symlink target path.
1178 * The new symlink target has already been checked for safety by our
1179 * caller. If we can successfully open a regular file then we simply
1180 * replace this file with a symlink below.
1182 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1185 return got_error_from_errno2("open", ondisk_path);
1187 /* We are updating an existing on-disk symlink. */
1188 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1190 return got_error_from_errno2("readlink", ondisk_path);
1192 if (elen == target_len &&
1193 memcmp(etarget, target_path, target_len) == 0)
1194 return NULL; /* nothing to do */
1197 err = update_symlink(ondisk_path, target_path, target_len);
1198 if (fd != -1 && close(fd) == -1 && err == NULL)
1199 err = got_error_from_errno2("close", ondisk_path);
1203 static const struct got_error *
1204 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1205 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1206 int restoring_missing_file, int reverting_versioned_file,
1207 struct got_repository *repo,
1208 got_worktree_checkout_cb progress_cb, void *progress_arg)
1210 const struct got_error *err = NULL;
1211 char target_path[PATH_MAX];
1212 size_t len, target_len = 0;
1213 char *resolved_path = NULL, *abspath = NULL;
1214 char *path_got = NULL;
1215 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1216 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1218 *is_bad_symlink = 0;
1221 * Blob object content specifies the target path of the link.
1222 * If a symbolic link cannot be installed we instead create
1223 * a regular file which contains the link target path stored
1224 * in the blob object.
1227 err = got_object_blob_read_block(&len, blob);
1228 if (len + target_len >= sizeof(target_path)) {
1229 /* Path too long; install as a regular file. */
1230 *is_bad_symlink = 1;
1231 got_object_blob_rewind(blob);
1232 return install_blob(worktree, ondisk_path, path,
1233 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1234 restoring_missing_file, reverting_versioned_file,
1235 1, repo, progress_cb, progress_arg);
1238 /* Skip blob object header first time around. */
1239 memcpy(target_path + target_len, buf + hdrlen,
1241 target_len += len - hdrlen;
1245 target_path[target_len] = '\0';
1248 * Relative symlink target lookup should begin at the directory
1249 * in which the blob object is being installed.
1251 if (!got_path_is_absolute(target_path)) {
1252 char *parent = dirname(ondisk_path);
1253 if (parent == NULL) {
1254 err = got_error_from_errno2("dirname", ondisk_path);
1257 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1258 err = got_error_from_errno("asprintf");
1264 * unveil(2) restricts our view of paths in the filesystem.
1265 * ENOENT will occur if a link target path does not exist or
1266 * if it points outside our unveiled path space.
1268 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1269 if (resolved_path == NULL) {
1270 if (errno != ENOENT) {
1271 err = got_error_from_errno2("realpath", target_path);
1276 /* Only allow symlinks pointing at paths within the work tree. */
1277 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1278 abspath : target_path), worktree->root_path,
1279 strlen(worktree->root_path))) {
1280 /* install as a regular file */
1281 *is_bad_symlink = 1;
1282 got_object_blob_rewind(blob);
1283 err = install_blob(worktree, ondisk_path, path,
1284 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1285 restoring_missing_file, reverting_versioned_file, 1,
1286 repo, progress_cb, progress_arg);
1290 /* Do not allow symlinks pointing into the .got directory. */
1291 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1292 GOT_WORKTREE_GOT_DIR) == -1) {
1293 err = got_error_from_errno("asprintf");
1296 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1297 abspath : target_path), path_got, strlen(path_got))) {
1298 /* install as a regular file */
1299 *is_bad_symlink = 1;
1300 got_object_blob_rewind(blob);
1301 err = install_blob(worktree, ondisk_path, path,
1302 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 restoring_missing_file, reverting_versioned_file, 1,
1304 repo, progress_cb, progress_arg);
1308 if (symlink(target_path, ondisk_path) == -1) {
1309 if (errno == EEXIST) {
1310 err = replace_existing_symlink(ondisk_path,
1311 target_path, target_len);
1315 err = (*progress_cb)(progress_arg,
1316 GOT_STATUS_UPDATE, path);
1318 goto done; /* Nothing else to do. */
1321 if (errno == ENOENT) {
1322 char *parent = dirname(ondisk_path);
1323 if (parent == NULL) {
1324 err = got_error_from_errno2("dirname",
1328 err = add_dir_on_disk(worktree, parent);
1332 * Retry, and fall through to error handling
1333 * below if this second attempt fails.
1335 if (symlink(target_path, ondisk_path) != -1) {
1336 err = NULL; /* success */
1341 /* Handle errors from first or second creation attempt. */
1342 if (errno == ENAMETOOLONG) {
1343 /* bad target path; install as a regular file */
1344 *is_bad_symlink = 1;
1345 got_object_blob_rewind(blob);
1346 err = install_blob(worktree, ondisk_path, path,
1347 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1348 restoring_missing_file, reverting_versioned_file, 1,
1349 repo, progress_cb, progress_arg);
1350 } else if (errno == ENOTDIR) {
1351 err = got_error_path(ondisk_path,
1352 GOT_ERR_FILE_OBSTRUCTED);
1354 err = got_error_from_errno3("symlink",
1355 target_path, ondisk_path);
1357 } else if (progress_cb)
1358 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1360 free(resolved_path);
1366 static const struct got_error *
1367 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1368 const char *path, mode_t te_mode, mode_t st_mode,
1369 struct got_blob_object *blob, int restoring_missing_file,
1370 int reverting_versioned_file, int installing_bad_symlink,
1371 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1374 const struct got_error *err = NULL;
1378 char *tmppath = NULL;
1380 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1381 GOT_DEFAULT_FILE_MODE);
1383 if (errno == ENOENT) {
1384 char *parent = dirname(path);
1386 return got_error_from_errno2("dirname", path);
1387 err = add_dir_on_disk(worktree, parent);
1390 fd = open(ondisk_path,
1391 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1392 GOT_DEFAULT_FILE_MODE);
1394 return got_error_from_errno2("open",
1396 } else if (errno == EEXIST) {
1397 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1398 /* TODO file is obstructed; do something */
1399 err = got_error_path(ondisk_path,
1400 GOT_ERR_FILE_OBSTRUCTED);
1403 err = got_opentemp_named_fd(&tmppath, &fd,
1410 return got_error_from_errno2("open", ondisk_path);
1414 if (restoring_missing_file)
1415 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1417 else if (reverting_versioned_file)
1418 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1421 err = (*progress_cb)(progress_arg,
1422 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1427 hdrlen = got_object_blob_get_hdrlen(blob);
1429 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1430 err = got_object_blob_read_block(&len, blob);
1434 /* Skip blob object header first time around. */
1435 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1437 err = got_error_from_errno("write");
1439 } else if (outlen != len - hdrlen) {
1440 err = got_error(GOT_ERR_IO);
1447 if (fsync(fd) != 0) {
1448 err = got_error_from_errno("fsync");
1453 if (rename(tmppath, ondisk_path) != 0) {
1454 err = got_error_from_errno3("rename", tmppath,
1461 if (chmod(ondisk_path,
1462 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1463 err = got_error_from_errno2("chmod", ondisk_path);
1468 if (fd != -1 && close(fd) != 0 && err == NULL)
1469 err = got_error_from_errno("close");
1474 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1475 static const struct got_error *
1476 get_modified_file_content_status(unsigned char *status, FILE *f)
1478 const struct got_error *err = NULL;
1479 const char *markers[3] = {
1480 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1481 GOT_DIFF_CONFLICT_MARKER_SEP,
1482 GOT_DIFF_CONFLICT_MARKER_END
1487 const char delim[3] = {'\0', '\0', '\0'};
1489 while (*status == GOT_STATUS_MODIFY) {
1490 line = fparseln(f, &len, NULL, delim, 0);
1494 err = got_ferror(f, GOT_ERR_IO);
1498 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1499 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1501 *status = GOT_STATUS_CONFLICT;
1511 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1513 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1514 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1518 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1520 return !(ie->ctime_sec == sb->st_ctime &&
1521 ie->ctime_nsec == sb->st_ctimensec &&
1522 ie->mtime_sec == sb->st_mtime &&
1523 ie->mtime_nsec == sb->st_mtimensec &&
1524 ie->size == (sb->st_size & 0xffffffff) &&
1525 !xbit_differs(ie, sb->st_mode));
1528 static unsigned char
1529 get_staged_status(struct got_fileindex_entry *ie)
1531 switch (got_fileindex_entry_stage_get(ie)) {
1532 case GOT_FILEIDX_STAGE_ADD:
1533 return GOT_STATUS_ADD;
1534 case GOT_FILEIDX_STAGE_DELETE:
1535 return GOT_STATUS_DELETE;
1536 case GOT_FILEIDX_STAGE_MODIFY:
1537 return GOT_STATUS_MODIFY;
1539 return GOT_STATUS_NO_CHANGE;
1543 static const struct got_error *
1544 get_symlink_status(unsigned char *status, struct stat *sb,
1545 struct got_fileindex_entry *ie, const char *abspath,
1546 int dirfd, const char *de_name, struct got_blob_object *blob)
1548 const struct got_error *err = NULL;
1549 char target_path[PATH_MAX];
1550 char etarget[PATH_MAX];
1552 size_t len, target_len = 0;
1553 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1554 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1556 *status = GOT_STATUS_NO_CHANGE;
1558 /* Blob object content specifies the target path of the link. */
1560 err = got_object_blob_read_block(&len, blob);
1563 if (len + target_len >= sizeof(target_path)) {
1565 * Should not happen. The blob contents were OK
1566 * when this symlink was installed.
1568 return got_error(GOT_ERR_NO_SPACE);
1571 /* Skip blob object header first time around. */
1572 memcpy(target_path + target_len, buf + hdrlen,
1574 target_len += len - hdrlen;
1578 target_path[target_len] = '\0';
1581 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1583 return got_error_from_errno2("readlinkat", abspath);
1585 elen = readlink(abspath, etarget, sizeof(etarget));
1587 return got_error_from_errno2("readlinkat", abspath);
1590 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1591 *status = GOT_STATUS_MODIFY;
1596 static const struct got_error *
1597 get_file_status(unsigned char *status, struct stat *sb,
1598 struct got_fileindex_entry *ie, const char *abspath,
1599 int dirfd, const char *de_name, struct got_repository *repo)
1601 const struct got_error *err = NULL;
1602 struct got_object_id id;
1607 struct got_blob_object *blob = NULL;
1609 unsigned char staged_status = get_staged_status(ie);
1611 *status = GOT_STATUS_NO_CHANGE;
1614 * Whenever the caller provides a directory descriptor and a
1615 * directory entry name for the file, use them! This prevents
1616 * race conditions if filesystem paths change beneath our feet.
1619 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1620 if (errno == ENOENT) {
1621 if (got_fileindex_entry_has_file_on_disk(ie))
1622 *status = GOT_STATUS_MISSING;
1624 *status = GOT_STATUS_DELETE;
1627 err = got_error_from_errno2("fstatat", abspath);
1631 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1632 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1633 return got_error_from_errno2("open", abspath);
1634 else if (fd == -1 && errno == ELOOP) {
1635 if (lstat(abspath, sb) == -1)
1636 return got_error_from_errno2("lstat", abspath);
1637 } else if (fd == -1 || fstat(fd, sb) == -1) {
1638 if (errno == ENOENT) {
1639 if (got_fileindex_entry_has_file_on_disk(ie))
1640 *status = GOT_STATUS_MISSING;
1642 *status = GOT_STATUS_DELETE;
1645 err = got_error_from_errno2("fstat", abspath);
1650 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1651 *status = GOT_STATUS_OBSTRUCTED;
1655 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1656 *status = GOT_STATUS_DELETE;
1658 } else if (!got_fileindex_entry_has_blob(ie) &&
1659 staged_status != GOT_STATUS_ADD) {
1660 *status = GOT_STATUS_ADD;
1664 if (!stat_info_differs(ie, sb))
1667 if (staged_status == GOT_STATUS_MODIFY ||
1668 staged_status == GOT_STATUS_ADD)
1669 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1671 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1673 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1677 if (S_ISLNK(sb->st_mode)) {
1678 /* Staging changes to symlinks is not yet(?) supported. */
1679 if (staged_status != GOT_STATUS_NO_CHANGE) {
1680 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1683 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1690 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1692 err = got_error_from_errno2("openat", abspath);
1697 f = fdopen(fd, "r");
1699 err = got_error_from_errno2("fdopen", abspath);
1703 hdrlen = got_object_blob_get_hdrlen(blob);
1705 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1706 err = got_object_blob_read_block(&blen, blob);
1709 /* Skip length of blob object header first time around. */
1710 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1711 if (flen == 0 && ferror(f)) {
1712 err = got_error_from_errno("fread");
1717 *status = GOT_STATUS_MODIFY;
1719 } else if (flen == 0) {
1721 *status = GOT_STATUS_MODIFY;
1723 } else if (blen - hdrlen == flen) {
1724 /* Skip blob object header first time around. */
1725 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1726 *status = GOT_STATUS_MODIFY;
1730 *status = GOT_STATUS_MODIFY;
1736 if (*status == GOT_STATUS_MODIFY) {
1738 err = get_modified_file_content_status(status, f);
1739 } else if (xbit_differs(ie, sb->st_mode))
1740 *status = GOT_STATUS_MODE_CHANGE;
1743 got_object_blob_close(blob);
1744 if (f != NULL && fclose(f) == EOF && err == NULL)
1745 err = got_error_from_errno2("fclose", abspath);
1746 if (fd != -1 && close(fd) == -1 && err == NULL)
1747 err = got_error_from_errno2("close", abspath);
1752 * Update timestamps in the file index if a file is unmodified and
1753 * we had to run a full content comparison to find out.
1755 static const struct got_error *
1756 sync_timestamps(char *ondisk_path, unsigned char status,
1757 struct got_fileindex_entry *ie, struct stat *sb)
1759 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1760 return got_fileindex_entry_update(ie, ondisk_path,
1761 ie->blob_sha1, ie->commit_sha1, 1);
1766 static const struct got_error *
1767 update_blob(struct got_worktree *worktree,
1768 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1769 struct got_tree_entry *te, const char *path,
1770 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1773 const struct got_error *err = NULL;
1774 struct got_blob_object *blob = NULL;
1776 unsigned char status = GOT_STATUS_NO_CHANGE;
1779 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1780 return got_error_from_errno("asprintf");
1783 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1784 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1787 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1791 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1792 sb.st_mode = got_fileindex_perms_to_st(ie);
1794 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1796 if (status == GOT_STATUS_OBSTRUCTED) {
1797 err = (*progress_cb)(progress_arg, status, path);
1800 if (status == GOT_STATUS_CONFLICT) {
1801 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1806 if (ie && status != GOT_STATUS_MISSING &&
1807 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1808 if (got_fileindex_entry_has_commit(ie) &&
1809 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1810 SHA1_DIGEST_LENGTH) == 0) {
1811 err = sync_timestamps(ondisk_path, status, ie, &sb);
1814 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1818 if (got_fileindex_entry_has_blob(ie) &&
1819 memcmp(ie->blob_sha1, te->id.sha1,
1820 SHA1_DIGEST_LENGTH) == 0) {
1821 err = sync_timestamps(ondisk_path, status, ie, &sb);
1826 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1830 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1831 int update_timestamps;
1832 struct got_blob_object *blob2 = NULL;
1833 char *label_orig = NULL;
1834 if (got_fileindex_entry_has_blob(ie)) {
1835 struct got_object_id id2;
1836 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1837 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1841 if (got_fileindex_entry_has_commit(ie)) {
1842 char id_str[SHA1_DIGEST_STRING_LENGTH];
1843 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1844 sizeof(id_str)) == NULL) {
1845 err = got_error_path(id_str,
1846 GOT_ERR_BAD_OBJ_ID_STR);
1849 if (asprintf(&label_orig, "%s: commit %s",
1850 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1851 err = got_error_from_errno("asprintf");
1855 if (S_ISLNK(te->mode)) {
1856 err = merge_symlink(worktree, blob2,
1857 ondisk_path, path, sb.st_mode, label_orig,
1858 blob, worktree->base_commit_id, repo,
1859 progress_cb, progress_arg);
1861 err = merge_blob(&update_timestamps, worktree, blob2,
1862 ondisk_path, path, sb.st_mode, label_orig, blob,
1863 worktree->base_commit_id, repo,
1864 progress_cb, progress_arg);
1868 got_object_blob_close(blob2);
1872 * Do not update timestamps of files with local changes.
1873 * Otherwise, a future status walk would treat them as
1874 * unmodified files again.
1876 err = got_fileindex_entry_update(ie, ondisk_path,
1877 blob->id.sha1, worktree->base_commit_id->sha1,
1879 } else if (status == GOT_STATUS_MODE_CHANGE) {
1880 err = got_fileindex_entry_update(ie, ondisk_path,
1881 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1882 } else if (status == GOT_STATUS_DELETE) {
1883 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1886 err = got_fileindex_entry_update(ie, ondisk_path,
1887 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1891 int is_bad_symlink = 0;
1892 if (S_ISLNK(te->mode)) {
1893 err = install_symlink(&is_bad_symlink, worktree,
1894 ondisk_path, path, blob,
1895 status == GOT_STATUS_MISSING, 0, repo,
1896 progress_cb, progress_arg);
1898 err = install_blob(worktree, ondisk_path, path,
1899 te->mode, sb.st_mode, blob,
1900 status == GOT_STATUS_MISSING, 0, 0, repo,
1901 progress_cb, progress_arg);
1907 err = got_fileindex_entry_update(ie, ondisk_path,
1908 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1910 err = create_fileindex_entry(&ie, fileindex,
1911 worktree->base_commit_id, ondisk_path, path,
1917 if (is_bad_symlink) {
1918 err = got_fileindex_entry_filetype_set(ie,
1919 GOT_FILEIDX_MODE_BAD_SYMLINK);
1924 got_object_blob_close(blob);
1930 static const struct got_error *
1931 remove_ondisk_file(const char *root_path, const char *path)
1933 const struct got_error *err = NULL;
1934 char *ondisk_path = NULL;
1936 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1937 return got_error_from_errno("asprintf");
1939 if (unlink(ondisk_path) == -1) {
1940 if (errno != ENOENT)
1941 err = got_error_from_errno2("unlink", ondisk_path);
1943 char *parent = dirname(ondisk_path);
1944 while (parent && strcmp(parent, root_path) != 0) {
1945 if (rmdir(parent) == -1) {
1946 if (errno != ENOTEMPTY)
1947 err = got_error_from_errno2("rmdir",
1951 parent = dirname(parent);
1958 static const struct got_error *
1959 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1960 struct got_fileindex_entry *ie, struct got_repository *repo,
1961 got_worktree_checkout_cb progress_cb, void *progress_arg)
1963 const struct got_error *err = NULL;
1964 unsigned char status;
1968 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1969 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1971 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1973 return got_error_from_errno("asprintf");
1975 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1979 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1980 char ondisk_target[PATH_MAX];
1981 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1982 sizeof(ondisk_target));
1983 if (ondisk_len == -1) {
1984 err = got_error_from_errno2("readlink", ondisk_path);
1987 ondisk_target[ondisk_len] = '\0';
1988 err = install_symlink_conflict(NULL, worktree->base_commit_id,
1989 NULL, NULL, /* XXX pass common ancestor info? */
1990 ondisk_target, ondisk_path);
1993 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1998 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1999 status == GOT_STATUS_ADD) {
2000 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2004 * Preserve the working file and change the deleted blob's
2005 * entry into a schedule-add entry.
2007 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2010 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2013 if (status == GOT_STATUS_NO_CHANGE) {
2014 err = remove_ondisk_file(worktree->root_path, ie->path);
2018 got_fileindex_entry_remove(fileindex, ie);
2025 struct diff_cb_arg {
2026 struct got_fileindex *fileindex;
2027 struct got_worktree *worktree;
2028 struct got_repository *repo;
2029 got_worktree_checkout_cb progress_cb;
2031 got_cancel_cb cancel_cb;
2035 static const struct got_error *
2036 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2037 struct got_tree_entry *te, const char *parent_path)
2039 struct diff_cb_arg *a = arg;
2041 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2042 return got_error(GOT_ERR_CANCELLED);
2044 return update_blob(a->worktree, a->fileindex, ie, te,
2045 ie->path, a->repo, a->progress_cb, a->progress_arg);
2048 static const struct got_error *
2049 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2051 struct diff_cb_arg *a = arg;
2053 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2054 return got_error(GOT_ERR_CANCELLED);
2056 return delete_blob(a->worktree, a->fileindex, ie,
2057 a->repo, a->progress_cb, a->progress_arg);
2060 static const struct got_error *
2061 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2063 struct diff_cb_arg *a = arg;
2064 const struct got_error *err;
2067 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2068 return got_error(GOT_ERR_CANCELLED);
2070 if (got_object_tree_entry_is_submodule(te))
2073 if (asprintf(&path, "%s%s%s", parent_path,
2074 parent_path[0] ? "/" : "", te->name)
2076 return got_error_from_errno("asprintf");
2078 if (S_ISDIR(te->mode))
2079 err = add_dir_on_disk(a->worktree, path);
2081 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2082 a->repo, a->progress_cb, a->progress_arg);
2088 static const struct got_error *
2089 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2091 const struct got_error *err = NULL;
2092 char *uuidstr = NULL;
2093 uint32_t uuid_status;
2097 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2098 if (uuid_status != uuid_s_ok)
2099 return got_error_uuid(uuid_status, "uuid_to_string");
2101 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2103 err = got_error_from_errno("asprintf");
2110 const struct got_error *
2111 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2113 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2116 static const struct got_error *
2117 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2119 return get_ref_name(refname, worktree,
2120 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2123 static const struct got_error *
2124 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2126 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2129 static const struct got_error *
2130 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2132 return get_ref_name(refname, worktree,
2133 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2136 static const struct got_error *
2137 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2139 return get_ref_name(refname, worktree,
2140 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2143 static const struct got_error *
2144 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2146 return get_ref_name(refname, worktree,
2147 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2150 static const struct got_error *
2151 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2153 return get_ref_name(refname, worktree,
2154 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2157 static const struct got_error *
2158 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2160 return get_ref_name(refname, worktree,
2161 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2164 static const struct got_error *
2165 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2167 return get_ref_name(refname, worktree,
2168 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2171 const struct got_error *
2172 got_worktree_get_histedit_script_path(char **path,
2173 struct got_worktree *worktree)
2175 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2176 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2178 return got_error_from_errno("asprintf");
2184 * Prevent Git's garbage collector from deleting our base commit by
2185 * setting a reference to our base commit's ID.
2187 static const struct got_error *
2188 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2190 const struct got_error *err = NULL;
2191 struct got_reference *ref = NULL;
2194 err = got_worktree_get_base_ref_name(&refname, worktree);
2198 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2202 err = got_ref_write(ref, repo);
2210 static const struct got_error *
2211 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2213 const struct got_error *err = NULL;
2215 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2216 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2217 err = got_error_from_errno("asprintf");
2218 *fileindex_path = NULL;
2224 static const struct got_error *
2225 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2226 struct got_worktree *worktree)
2228 const struct got_error *err = NULL;
2231 *fileindex_path = NULL;
2232 *fileindex = got_fileindex_alloc();
2233 if (*fileindex == NULL)
2234 return got_error_from_errno("got_fileindex_alloc");
2236 err = get_fileindex_path(fileindex_path, worktree);
2240 index = fopen(*fileindex_path, "rb");
2241 if (index == NULL) {
2242 if (errno != ENOENT)
2243 err = got_error_from_errno2("fopen", *fileindex_path);
2245 err = got_fileindex_read(*fileindex, index);
2246 if (fclose(index) != 0 && err == NULL)
2247 err = got_error_from_errno("fclose");
2251 free(*fileindex_path);
2252 *fileindex_path = NULL;
2253 got_fileindex_free(*fileindex);
2259 struct bump_base_commit_id_arg {
2260 struct got_object_id *base_commit_id;
2263 const char *entry_name;
2264 got_worktree_checkout_cb progress_cb;
2268 /* Bump base commit ID of all files within an updated part of the work tree. */
2269 static const struct got_error *
2270 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2272 const struct got_error *err;
2273 struct bump_base_commit_id_arg *a = arg;
2275 if (a->entry_name) {
2276 if (strcmp(ie->path, a->path) != 0)
2278 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2281 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2282 SHA1_DIGEST_LENGTH) == 0)
2285 if (a->progress_cb) {
2286 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2291 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2295 static const struct got_error *
2296 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2298 const struct got_error *err = NULL;
2299 char *new_fileindex_path = NULL;
2300 FILE *new_index = NULL;
2301 struct timespec timeout;
2303 err = got_opentemp_named(&new_fileindex_path, &new_index,
2308 err = got_fileindex_write(fileindex, new_index);
2312 if (rename(new_fileindex_path, fileindex_path) != 0) {
2313 err = got_error_from_errno3("rename", new_fileindex_path,
2315 unlink(new_fileindex_path);
2319 * Sleep for a short amount of time to ensure that files modified after
2320 * this program exits have a different time stamp from the one which
2321 * was recorded in the file index.
2324 timeout.tv_nsec = 1;
2325 nanosleep(&timeout, NULL);
2329 free(new_fileindex_path);
2333 static const struct got_error *
2334 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2335 struct got_object_id **tree_id, const char *wt_relpath,
2336 struct got_worktree *worktree, struct got_repository *repo)
2338 const struct got_error *err = NULL;
2339 struct got_object_id *id = NULL;
2340 char *in_repo_path = NULL;
2341 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2343 *entry_type = GOT_OBJ_TYPE_ANY;
2344 *tree_relpath = NULL;
2347 if (wt_relpath[0] == '\0') {
2348 /* Check out all files within the work tree. */
2349 *entry_type = GOT_OBJ_TYPE_TREE;
2350 *tree_relpath = strdup("");
2351 if (*tree_relpath == NULL) {
2352 err = got_error_from_errno("strdup");
2355 err = got_object_id_by_path(tree_id, repo,
2356 worktree->base_commit_id, worktree->path_prefix);
2362 /* Check out a subset of files in the work tree. */
2364 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2365 is_root_wt ? "" : "/", wt_relpath) == -1) {
2366 err = got_error_from_errno("asprintf");
2370 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2376 in_repo_path = NULL;
2378 err = got_object_get_type(entry_type, repo, id);
2382 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2383 /* Check out a single file. */
2384 if (strchr(wt_relpath, '/') == NULL) {
2385 /* Check out a single file in work tree's root dir. */
2386 in_repo_path = strdup(worktree->path_prefix);
2387 if (in_repo_path == NULL) {
2388 err = got_error_from_errno("strdup");
2391 *tree_relpath = strdup("");
2392 if (*tree_relpath == NULL) {
2393 err = got_error_from_errno("strdup");
2397 /* Check out a single file in a subdirectory. */
2398 err = got_path_dirname(tree_relpath, wt_relpath);
2401 if (asprintf(&in_repo_path, "%s%s%s",
2402 worktree->path_prefix, is_root_wt ? "" : "/",
2403 *tree_relpath) == -1) {
2404 err = got_error_from_errno("asprintf");
2408 err = got_object_id_by_path(tree_id, repo,
2409 worktree->base_commit_id, in_repo_path);
2411 /* Check out all files within a subdirectory. */
2412 *tree_id = got_object_id_dup(id);
2413 if (*tree_id == NULL) {
2414 err = got_error_from_errno("got_object_id_dup");
2417 *tree_relpath = strdup(wt_relpath);
2418 if (*tree_relpath == NULL) {
2419 err = got_error_from_errno("strdup");
2427 *entry_type = GOT_OBJ_TYPE_ANY;
2428 free(*tree_relpath);
2429 *tree_relpath = NULL;
2436 static const struct got_error *
2437 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2438 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2439 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2440 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2442 const struct got_error *err = NULL;
2443 struct got_commit_object *commit = NULL;
2444 struct got_tree_object *tree = NULL;
2445 struct got_fileindex_diff_tree_cb diff_cb;
2446 struct diff_cb_arg arg;
2448 err = ref_base_commit(worktree, repo);
2450 if (!(err->code == GOT_ERR_ERRNO &&
2451 (errno == EACCES || errno == EROFS)))
2453 err = (*progress_cb)(progress_arg,
2454 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2459 err = got_object_open_as_commit(&commit, repo,
2460 worktree->base_commit_id);
2464 err = got_object_open_as_tree(&tree, repo, tree_id);
2469 got_object_tree_find_entry(tree, entry_name) == NULL) {
2470 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2474 diff_cb.diff_old_new = diff_old_new;
2475 diff_cb.diff_old = diff_old;
2476 diff_cb.diff_new = diff_new;
2477 arg.fileindex = fileindex;
2478 arg.worktree = worktree;
2480 arg.progress_cb = progress_cb;
2481 arg.progress_arg = progress_arg;
2482 arg.cancel_cb = cancel_cb;
2483 arg.cancel_arg = cancel_arg;
2484 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2485 entry_name, repo, &diff_cb, &arg);
2488 got_object_tree_close(tree);
2490 got_object_commit_close(commit);
2494 const struct got_error *
2495 got_worktree_checkout_files(struct got_worktree *worktree,
2496 struct got_pathlist_head *paths, struct got_repository *repo,
2497 got_worktree_checkout_cb progress_cb, void *progress_arg,
2498 got_cancel_cb cancel_cb, void *cancel_arg)
2500 const struct got_error *err = NULL, *sync_err, *unlockerr;
2501 struct got_commit_object *commit = NULL;
2502 struct got_tree_object *tree = NULL;
2503 struct got_fileindex *fileindex = NULL;
2504 char *fileindex_path = NULL;
2505 struct got_pathlist_entry *pe;
2506 struct tree_path_data {
2507 SIMPLEQ_ENTRY(tree_path_data) entry;
2508 struct got_object_id *tree_id;
2513 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2515 SIMPLEQ_INIT(&tree_paths);
2517 err = lock_worktree(worktree, LOCK_EX);
2521 /* Map all specified paths to in-repository trees. */
2522 TAILQ_FOREACH(pe, paths, entry) {
2523 tpd = malloc(sizeof(*tpd));
2525 err = got_error_from_errno("malloc");
2529 err = find_tree_entry_for_checkout(&tpd->entry_type,
2530 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2536 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2537 err = got_path_basename(&tpd->entry_name, pe->path);
2545 tpd->entry_name = NULL;
2547 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2551 * Read the file index.
2552 * Checking out files is supposed to be an idempotent operation.
2553 * If the on-disk file index is incomplete we will try to complete it.
2555 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2559 tpd = SIMPLEQ_FIRST(&tree_paths);
2560 TAILQ_FOREACH(pe, paths, entry) {
2561 struct bump_base_commit_id_arg bbc_arg;
2563 err = checkout_files(worktree, fileindex, tpd->relpath,
2564 tpd->tree_id, tpd->entry_name, repo,
2565 progress_cb, progress_arg, cancel_cb, cancel_arg);
2569 bbc_arg.base_commit_id = worktree->base_commit_id;
2570 bbc_arg.entry_name = tpd->entry_name;
2571 bbc_arg.path = pe->path;
2572 bbc_arg.path_len = pe->path_len;
2573 bbc_arg.progress_cb = progress_cb;
2574 bbc_arg.progress_arg = progress_arg;
2575 err = got_fileindex_for_each_entry_safe(fileindex,
2576 bump_base_commit_id, &bbc_arg);
2580 tpd = SIMPLEQ_NEXT(tpd, entry);
2582 sync_err = sync_fileindex(fileindex, fileindex_path);
2583 if (sync_err && err == NULL)
2586 free(fileindex_path);
2588 got_object_tree_close(tree);
2590 got_object_commit_close(commit);
2592 got_fileindex_free(fileindex);
2593 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2594 tpd = SIMPLEQ_FIRST(&tree_paths);
2595 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2600 unlockerr = lock_worktree(worktree, LOCK_SH);
2601 if (unlockerr && err == NULL)
2606 struct merge_file_cb_arg {
2607 struct got_worktree *worktree;
2608 struct got_fileindex *fileindex;
2609 got_worktree_checkout_cb progress_cb;
2611 got_cancel_cb cancel_cb;
2613 const char *label_orig;
2614 struct got_object_id *commit_id2;
2617 static const struct got_error *
2618 merge_file_cb(void *arg, struct got_blob_object *blob1,
2619 struct got_blob_object *blob2, struct got_object_id *id1,
2620 struct got_object_id *id2, const char *path1, const char *path2,
2621 mode_t mode1, mode_t mode2, struct got_repository *repo)
2623 static const struct got_error *err = NULL;
2624 struct merge_file_cb_arg *a = arg;
2625 struct got_fileindex_entry *ie;
2626 char *ondisk_path = NULL;
2628 unsigned char status;
2629 int local_changes_subsumed;
2631 if (blob1 && blob2) {
2632 ie = got_fileindex_entry_get(a->fileindex, path2,
2635 return (*a->progress_cb)(a->progress_arg,
2636 GOT_STATUS_MISSING, path2);
2638 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2640 return got_error_from_errno("asprintf");
2642 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2647 if (status == GOT_STATUS_DELETE) {
2648 err = (*a->progress_cb)(a->progress_arg,
2649 GOT_STATUS_MERGE, path2);
2652 if (status != GOT_STATUS_NO_CHANGE &&
2653 status != GOT_STATUS_MODIFY &&
2654 status != GOT_STATUS_CONFLICT &&
2655 status != GOT_STATUS_ADD) {
2656 err = (*a->progress_cb)(a->progress_arg, status, path2);
2660 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2661 err = merge_symlink(a->worktree, blob1,
2662 ondisk_path, path2, sb.st_mode, a->label_orig,
2663 blob2, a->commit_id2, repo, a->progress_cb,
2666 err = merge_blob(&local_changes_subsumed, a->worktree,
2667 blob1, ondisk_path, path2, sb.st_mode,
2668 a->label_orig, blob2, a->commit_id2, repo,
2669 a->progress_cb, a->progress_arg);
2672 ie = got_fileindex_entry_get(a->fileindex, path1,
2675 return (*a->progress_cb)(a->progress_arg,
2676 GOT_STATUS_MISSING, path1);
2678 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2680 return got_error_from_errno("asprintf");
2682 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2688 case GOT_STATUS_NO_CHANGE:
2689 err = (*a->progress_cb)(a->progress_arg,
2690 GOT_STATUS_DELETE, path1);
2693 err = remove_ondisk_file(a->worktree->root_path, path1);
2697 got_fileindex_entry_mark_deleted_from_disk(ie);
2699 case GOT_STATUS_DELETE:
2700 case GOT_STATUS_MISSING:
2701 err = (*a->progress_cb)(a->progress_arg,
2702 GOT_STATUS_DELETE, path1);
2706 got_fileindex_entry_mark_deleted_from_disk(ie);
2708 case GOT_STATUS_ADD:
2709 case GOT_STATUS_MODIFY:
2710 case GOT_STATUS_CONFLICT:
2711 err = (*a->progress_cb)(a->progress_arg,
2712 GOT_STATUS_CANNOT_DELETE, path1);
2716 case GOT_STATUS_OBSTRUCTED:
2717 err = (*a->progress_cb)(a->progress_arg, status, path1);
2725 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2727 return got_error_from_errno("asprintf");
2728 ie = got_fileindex_entry_get(a->fileindex, path2,
2731 err = get_file_status(&status, &sb, ie, ondisk_path,
2735 if (status != GOT_STATUS_NO_CHANGE &&
2736 status != GOT_STATUS_MODIFY &&
2737 status != GOT_STATUS_CONFLICT &&
2738 status != GOT_STATUS_ADD) {
2739 err = (*a->progress_cb)(a->progress_arg,
2743 if (S_ISLNK(mode2)) {
2744 err = merge_symlink(a->worktree, NULL,
2745 ondisk_path, path2, sb.st_mode,
2746 a->label_orig, blob2, a->commit_id2,
2747 repo, a->progress_cb, a->progress_arg);
2751 err = merge_blob(&local_changes_subsumed,
2752 a->worktree, NULL, ondisk_path, path2,
2753 sb.st_mode, a->label_orig, blob2,
2754 a->commit_id2, repo, a->progress_cb,
2759 if (status == GOT_STATUS_DELETE) {
2760 err = got_fileindex_entry_update(ie,
2761 ondisk_path, blob2->id.sha1,
2762 a->worktree->base_commit_id->sha1, 0);
2767 int is_bad_symlink = 0;
2768 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2769 if (S_ISLNK(mode2)) {
2770 err = install_symlink(&is_bad_symlink,
2771 a->worktree, ondisk_path, path2, blob2, 0,
2772 0, repo, a->progress_cb, a->progress_arg);
2774 err = install_blob(a->worktree, ondisk_path, path2,
2775 mode2, sb.st_mode, blob2, 0, 0, 0, repo,
2776 a->progress_cb, a->progress_arg);
2780 err = got_fileindex_entry_alloc(&ie, path2);
2783 err = got_fileindex_entry_update(ie, ondisk_path,
2786 got_fileindex_entry_free(ie);
2789 err = got_fileindex_entry_add(a->fileindex, ie);
2791 got_fileindex_entry_free(ie);
2794 if (is_bad_symlink) {
2795 err = got_fileindex_entry_filetype_set(ie,
2796 GOT_FILEIDX_MODE_BAD_SYMLINK);
2807 struct check_merge_ok_arg {
2808 struct got_worktree *worktree;
2809 struct got_repository *repo;
2812 static const struct got_error *
2813 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2815 const struct got_error *err = NULL;
2816 struct check_merge_ok_arg *a = arg;
2817 unsigned char status;
2821 /* Reject merges into a work tree with mixed base commits. */
2822 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2823 SHA1_DIGEST_LENGTH))
2824 return got_error(GOT_ERR_MIXED_COMMITS);
2826 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2828 return got_error_from_errno("asprintf");
2830 /* Reject merges into a work tree with conflicted files. */
2831 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2834 if (status == GOT_STATUS_CONFLICT)
2835 return got_error(GOT_ERR_CONFLICTS);
2840 static const struct got_error *
2841 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2842 const char *fileindex_path, struct got_object_id *commit_id1,
2843 struct got_object_id *commit_id2, struct got_repository *repo,
2844 got_worktree_checkout_cb progress_cb, void *progress_arg,
2845 got_cancel_cb cancel_cb, void *cancel_arg)
2847 const struct got_error *err = NULL, *sync_err;
2848 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2849 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2850 struct merge_file_cb_arg arg;
2851 char *label_orig = NULL;
2856 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2857 worktree->path_prefix);
2861 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2865 err = got_object_id_str(&id_str, commit_id1);
2869 if (asprintf(&label_orig, "%s: commit %s",
2870 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2871 err = got_error_from_errno("asprintf");
2878 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2879 worktree->path_prefix);
2883 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2887 arg.worktree = worktree;
2888 arg.fileindex = fileindex;
2889 arg.progress_cb = progress_cb;
2890 arg.progress_arg = progress_arg;
2891 arg.cancel_cb = cancel_cb;
2892 arg.cancel_arg = cancel_arg;
2893 arg.label_orig = label_orig;
2894 arg.commit_id2 = commit_id2;
2895 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2896 sync_err = sync_fileindex(fileindex, fileindex_path);
2897 if (sync_err && err == NULL)
2901 got_object_tree_close(tree1);
2903 got_object_tree_close(tree2);
2908 const struct got_error *
2909 got_worktree_merge_files(struct got_worktree *worktree,
2910 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2911 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2912 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2914 const struct got_error *err, *unlockerr;
2915 char *fileindex_path = NULL;
2916 struct got_fileindex *fileindex = NULL;
2917 struct check_merge_ok_arg mok_arg;
2919 err = lock_worktree(worktree, LOCK_EX);
2923 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2927 mok_arg.worktree = worktree;
2928 mok_arg.repo = repo;
2929 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2934 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2935 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2938 got_fileindex_free(fileindex);
2939 free(fileindex_path);
2940 unlockerr = lock_worktree(worktree, LOCK_SH);
2941 if (unlockerr && err == NULL)
2946 struct diff_dir_cb_arg {
2947 struct got_fileindex *fileindex;
2948 struct got_worktree *worktree;
2949 const char *status_path;
2950 size_t status_path_len;
2951 struct got_repository *repo;
2952 got_worktree_status_cb status_cb;
2954 got_cancel_cb cancel_cb;
2956 /* A pathlist containing per-directory pathlists of ignore patterns. */
2957 struct got_pathlist_head ignores;
2958 int report_unchanged;
2962 static const struct got_error *
2963 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2964 int dirfd, const char *de_name,
2965 got_worktree_status_cb status_cb, void *status_arg,
2966 struct got_repository *repo, int report_unchanged)
2968 const struct got_error *err = NULL;
2969 unsigned char status = GOT_STATUS_NO_CHANGE;
2970 unsigned char staged_status = get_staged_status(ie);
2972 struct got_object_id blob_id, commit_id, staged_blob_id;
2973 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2974 struct got_object_id *staged_blob_idp = NULL;
2976 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2980 if (status == GOT_STATUS_NO_CHANGE &&
2981 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2984 if (got_fileindex_entry_has_blob(ie)) {
2985 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2986 blob_idp = &blob_id;
2988 if (got_fileindex_entry_has_commit(ie)) {
2989 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2990 commit_idp = &commit_id;
2992 if (staged_status == GOT_STATUS_ADD ||
2993 staged_status == GOT_STATUS_MODIFY) {
2994 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2995 SHA1_DIGEST_LENGTH);
2996 staged_blob_idp = &staged_blob_id;
2999 return (*status_cb)(status_arg, status, staged_status,
3000 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3003 static const struct got_error *
3004 status_old_new(void *arg, struct got_fileindex_entry *ie,
3005 struct dirent *de, const char *parent_path, int dirfd)
3007 const struct got_error *err = NULL;
3008 struct diff_dir_cb_arg *a = arg;
3011 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3012 return got_error(GOT_ERR_CANCELLED);
3014 if (got_path_cmp(parent_path, a->status_path,
3015 strlen(parent_path), a->status_path_len) != 0 &&
3016 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3019 if (parent_path[0]) {
3020 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3021 parent_path, de->d_name) == -1)
3022 return got_error_from_errno("asprintf");
3024 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3026 return got_error_from_errno("asprintf");
3029 err = report_file_status(ie, abspath, dirfd, de->d_name,
3030 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3035 static const struct got_error *
3036 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3038 struct diff_dir_cb_arg *a = arg;
3039 struct got_object_id blob_id, commit_id;
3040 unsigned char status;
3042 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3043 return got_error(GOT_ERR_CANCELLED);
3045 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3048 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3049 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3050 if (got_fileindex_entry_has_file_on_disk(ie))
3051 status = GOT_STATUS_MISSING;
3053 status = GOT_STATUS_DELETE;
3054 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3055 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3059 free_ignorelist(struct got_pathlist_head *ignorelist)
3061 struct got_pathlist_entry *pe;
3063 TAILQ_FOREACH(pe, ignorelist, entry)
3064 free((char *)pe->path);
3065 got_pathlist_free(ignorelist);
3069 free_ignores(struct got_pathlist_head *ignores)
3071 struct got_pathlist_entry *pe;
3073 TAILQ_FOREACH(pe, ignores, entry) {
3074 struct got_pathlist_head *ignorelist = pe->data;
3075 free_ignorelist(ignorelist);
3076 free((char *)pe->path);
3078 got_pathlist_free(ignores);
3081 static const struct got_error *
3082 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3084 const struct got_error *err = NULL;
3085 struct got_pathlist_entry *pe = NULL;
3086 struct got_pathlist_head *ignorelist;
3087 char *line = NULL, *pattern, *dirpath = NULL;
3088 size_t linesize = 0;
3091 ignorelist = calloc(1, sizeof(*ignorelist));
3092 if (ignorelist == NULL)
3093 return got_error_from_errno("calloc");
3094 TAILQ_INIT(ignorelist);
3096 while ((linelen = getline(&line, &linesize, f)) != -1) {
3097 if (linelen > 0 && line[linelen - 1] == '\n')
3098 line[linelen - 1] = '\0';
3100 /* Git's ignores may contain comments. */
3104 /* Git's negated patterns are not (yet?) supported. */
3108 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3110 err = got_error_from_errno("asprintf");
3113 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3118 err = got_error_from_errno("getline");
3122 dirpath = strdup(path);
3123 if (dirpath == NULL) {
3124 err = got_error_from_errno("strdup");
3127 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3130 if (err || pe == NULL) {
3132 free_ignorelist(ignorelist);
3138 match_ignores(struct got_pathlist_head *ignores, const char *path)
3140 struct got_pathlist_entry *pe;
3142 /* Handle patterns which match in all directories. */
3143 TAILQ_FOREACH(pe, ignores, entry) {
3144 struct got_pathlist_head *ignorelist = pe->data;
3145 struct got_pathlist_entry *pi;
3147 TAILQ_FOREACH(pi, ignorelist, entry) {
3148 const char *p, *pattern = pi->path;
3150 if (strncmp(pattern, "**/", 3) != 0)
3155 if (fnmatch(pattern, p,
3156 FNM_PATHNAME | FNM_LEADING_DIR)) {
3157 /* Retry in next directory. */
3158 while (*p && *p != '/')
3170 * The ignores pathlist contains ignore lists from children before
3171 * parents, so we can find the most specific ignorelist by walking
3172 * ignores backwards.
3174 pe = TAILQ_LAST(ignores, got_pathlist_head);
3176 if (got_path_is_child(path, pe->path, pe->path_len)) {
3177 struct got_pathlist_head *ignorelist = pe->data;
3178 struct got_pathlist_entry *pi;
3179 TAILQ_FOREACH(pi, ignorelist, entry) {
3180 const char *pattern = pi->path;
3181 int flags = FNM_LEADING_DIR;
3182 if (strstr(pattern, "/**/") == NULL)
3183 flags |= FNM_PATHNAME;
3184 if (fnmatch(pattern, path, flags))
3189 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3195 static const struct got_error *
3196 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3197 const char *path, int dirfd, const char *ignores_filename)
3199 const struct got_error *err = NULL;
3202 FILE *ignoresfile = NULL;
3204 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3205 path[0] ? "/" : "", ignores_filename) == -1)
3206 return got_error_from_errno("asprintf");
3209 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3211 if (errno != ENOENT && errno != EACCES)
3212 err = got_error_from_errno2("openat",
3215 ignoresfile = fdopen(fd, "r");
3216 if (ignoresfile == NULL)
3217 err = got_error_from_errno2("fdopen",
3221 err = read_ignores(ignores, path, ignoresfile);
3225 ignoresfile = fopen(ignorespath, "r");
3226 if (ignoresfile == NULL) {
3227 if (errno != ENOENT && errno != EACCES)
3228 err = got_error_from_errno2("fopen",
3231 err = read_ignores(ignores, path, ignoresfile);
3234 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3235 err = got_error_from_errno2("fclose", path);
3236 if (fd != -1 && close(fd) == -1 && err == NULL)
3237 err = got_error_from_errno2("close", path);
3242 static const struct got_error *
3243 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3245 const struct got_error *err = NULL;
3246 struct diff_dir_cb_arg *a = arg;
3249 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3250 return got_error(GOT_ERR_CANCELLED);
3252 if (parent_path[0]) {
3253 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3254 return got_error_from_errno("asprintf");
3259 if (de->d_type != DT_DIR &&
3260 got_path_is_child(path, a->status_path, a->status_path_len)
3261 && !match_ignores(&a->ignores, path))
3262 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3263 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3269 static const struct got_error *
3270 status_traverse(void *arg, const char *path, int dirfd)
3272 const struct got_error *err = NULL;
3273 struct diff_dir_cb_arg *a = arg;
3278 err = add_ignores(&a->ignores, a->worktree->root_path,
3279 path, dirfd, ".cvsignore");
3283 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3284 dirfd, ".gitignore");
3289 static const struct got_error *
3290 report_single_file_status(const char *path, const char *ondisk_path,
3291 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3292 void *status_arg, struct got_repository *repo, int report_unchanged)
3294 struct got_fileindex_entry *ie;
3297 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3299 return report_file_status(ie, ondisk_path, -1, NULL,
3300 status_cb, status_arg, repo, report_unchanged);
3302 if (lstat(ondisk_path, &sb) == -1) {
3303 if (errno != ENOENT)
3304 return got_error_from_errno2("lstat", ondisk_path);
3305 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3306 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3310 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3311 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3312 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3317 static const struct got_error *
3318 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3319 const char *root_path, const char *path)
3321 const struct got_error *err;
3322 char *parent_path, *next_parent_path = NULL;
3324 err = add_ignores(ignores, root_path, "", -1,
3329 err = add_ignores(ignores, root_path, "", -1,
3334 err = got_path_dirname(&parent_path, path);
3336 if (err->code == GOT_ERR_BAD_PATH)
3337 return NULL; /* cannot traverse parent */
3341 err = add_ignores(ignores, root_path, parent_path, -1,
3345 err = add_ignores(ignores, root_path, parent_path, -1,
3349 err = got_path_dirname(&next_parent_path, parent_path);
3351 if (err->code == GOT_ERR_BAD_PATH)
3352 err = NULL; /* traversed everything */
3356 parent_path = next_parent_path;
3357 next_parent_path = NULL;
3361 free(next_parent_path);
3365 static const struct got_error *
3366 worktree_status(struct got_worktree *worktree, const char *path,
3367 struct got_fileindex *fileindex, struct got_repository *repo,
3368 got_worktree_status_cb status_cb, void *status_arg,
3369 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3370 int report_unchanged)
3372 const struct got_error *err = NULL;
3374 struct got_fileindex_diff_dir_cb fdiff_cb;
3375 struct diff_dir_cb_arg arg;
3376 char *ondisk_path = NULL;
3378 TAILQ_INIT(&arg.ignores);
3380 if (asprintf(&ondisk_path, "%s%s%s",
3381 worktree->root_path, path[0] ? "/" : "", path) == -1)
3382 return got_error_from_errno("asprintf");
3384 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3386 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3388 err = got_error_from_errno2("open", ondisk_path);
3390 err = report_single_file_status(path, ondisk_path,
3391 fileindex, status_cb, status_arg, repo,
3394 fdiff_cb.diff_old_new = status_old_new;
3395 fdiff_cb.diff_old = status_old;
3396 fdiff_cb.diff_new = status_new;
3397 fdiff_cb.diff_traverse = status_traverse;
3398 arg.fileindex = fileindex;
3399 arg.worktree = worktree;
3400 arg.status_path = path;
3401 arg.status_path_len = strlen(path);
3403 arg.status_cb = status_cb;
3404 arg.status_arg = status_arg;
3405 arg.cancel_cb = cancel_cb;
3406 arg.cancel_arg = cancel_arg;
3407 arg.report_unchanged = report_unchanged;
3408 arg.no_ignores = no_ignores;
3410 err = add_ignores_from_parent_paths(&arg.ignores,
3411 worktree->root_path, path);
3415 err = got_fileindex_diff_dir(fileindex, fd,
3416 worktree->root_path, path, repo, &fdiff_cb, &arg);
3419 free_ignores(&arg.ignores);
3420 if (fd != -1 && close(fd) != 0 && err == NULL)
3421 err = got_error_from_errno("close");
3426 const struct got_error *
3427 got_worktree_status(struct got_worktree *worktree,
3428 struct got_pathlist_head *paths, struct got_repository *repo,
3429 got_worktree_status_cb status_cb, void *status_arg,
3430 got_cancel_cb cancel_cb, void *cancel_arg)
3432 const struct got_error *err = NULL;
3433 char *fileindex_path = NULL;
3434 struct got_fileindex *fileindex = NULL;
3435 struct got_pathlist_entry *pe;
3437 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3441 TAILQ_FOREACH(pe, paths, entry) {
3442 err = worktree_status(worktree, pe->path, fileindex, repo,
3443 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3447 free(fileindex_path);
3448 got_fileindex_free(fileindex);
3452 const struct got_error *
3453 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3456 const struct got_error *err = NULL;
3457 char *resolved = NULL, *cwd = NULL, *path = NULL;
3463 cwd = getcwd(NULL, 0);
3465 return got_error_from_errno("getcwd");
3467 if (lstat(arg, &sb) == -1) {
3468 if (errno != ENOENT) {
3469 err = got_error_from_errno2("lstat", arg);
3473 if (S_ISLNK(sb.st_mode)) {
3475 * We cannot use realpath(3) with symlinks since we want to
3476 * operate on the symlink itself.
3477 * But we can make the path absolute, assuming it is relative
3478 * to the current working directory, and then canonicalize it.
3480 char *abspath = NULL;
3481 char canonpath[PATH_MAX];
3482 if (!got_path_is_absolute(arg)) {
3483 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3484 err = got_error_from_errno("asprintf");
3489 err = got_canonpath(abspath ? abspath : arg, canonpath,
3493 resolved = strdup(canonpath);
3494 if (resolved == NULL) {
3495 err = got_error_from_errno("strdup");
3499 resolved = realpath(arg, NULL);
3500 if (resolved == NULL) {
3501 if (errno != ENOENT) {
3502 err = got_error_from_errno2("realpath", arg);
3505 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3506 err = got_error_from_errno("asprintf");
3512 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3513 strlen(got_worktree_get_root_path(worktree)))) {
3514 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3518 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3519 err = got_path_skip_common_ancestor(&path,
3520 got_worktree_get_root_path(worktree), resolved);
3526 err = got_error_from_errno("strdup");
3531 /* XXX status walk can't deal with trailing slash! */
3533 while (len > 0 && path[len - 1] == '/') {
3534 path[len - 1] = '\0';
3547 struct schedule_addition_args {
3548 struct got_worktree *worktree;
3549 struct got_fileindex *fileindex;
3550 got_worktree_checkout_cb progress_cb;
3552 struct got_repository *repo;
3555 static const struct got_error *
3556 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3557 const char *relpath, struct got_object_id *blob_id,
3558 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3559 int dirfd, const char *de_name)
3561 struct schedule_addition_args *a = arg;
3562 const struct got_error *err = NULL;
3563 struct got_fileindex_entry *ie;
3567 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3569 return got_error_from_errno("asprintf");
3571 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3573 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3577 /* Re-adding an existing entry is a no-op. */
3578 if (status == GOT_STATUS_ADD)
3580 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3585 if (status != GOT_STATUS_UNVERSIONED) {
3586 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3590 err = got_fileindex_entry_alloc(&ie, relpath);
3593 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3595 got_fileindex_entry_free(ie);
3598 err = got_fileindex_entry_add(a->fileindex, ie);
3600 got_fileindex_entry_free(ie);
3607 if (status == GOT_STATUS_ADD)
3609 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3612 const struct got_error *
3613 got_worktree_schedule_add(struct got_worktree *worktree,
3614 struct got_pathlist_head *paths,
3615 got_worktree_checkout_cb progress_cb, void *progress_arg,
3616 struct got_repository *repo, int no_ignores)
3618 struct got_fileindex *fileindex = NULL;
3619 char *fileindex_path = NULL;
3620 const struct got_error *err = NULL, *sync_err, *unlockerr;
3621 struct got_pathlist_entry *pe;
3622 struct schedule_addition_args saa;
3624 err = lock_worktree(worktree, LOCK_EX);
3628 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3632 saa.worktree = worktree;
3633 saa.fileindex = fileindex;
3634 saa.progress_cb = progress_cb;
3635 saa.progress_arg = progress_arg;
3638 TAILQ_FOREACH(pe, paths, entry) {
3639 err = worktree_status(worktree, pe->path, fileindex, repo,
3640 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3644 sync_err = sync_fileindex(fileindex, fileindex_path);
3645 if (sync_err && err == NULL)
3648 free(fileindex_path);
3650 got_fileindex_free(fileindex);
3651 unlockerr = lock_worktree(worktree, LOCK_SH);
3652 if (unlockerr && err == NULL)
3657 struct schedule_deletion_args {
3658 struct got_worktree *worktree;
3659 struct got_fileindex *fileindex;
3660 got_worktree_delete_cb progress_cb;
3662 struct got_repository *repo;
3663 int delete_local_mods;
3667 static const struct got_error *
3668 schedule_for_deletion(void *arg, unsigned char status,
3669 unsigned char staged_status, const char *relpath,
3670 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3671 struct got_object_id *commit_id, int dirfd, const char *de_name)
3673 struct schedule_deletion_args *a = arg;
3674 const struct got_error *err = NULL;
3675 struct got_fileindex_entry *ie = NULL;
3677 char *ondisk_path, *parent = NULL;
3679 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3681 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3683 staged_status = get_staged_status(ie);
3684 if (staged_status != GOT_STATUS_NO_CHANGE) {
3685 if (staged_status == GOT_STATUS_DELETE)
3687 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3690 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3692 return got_error_from_errno("asprintf");
3694 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3699 if (status != GOT_STATUS_NO_CHANGE) {
3700 if (status == GOT_STATUS_DELETE)
3702 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3703 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3706 if (status != GOT_STATUS_MODIFY &&
3707 status != GOT_STATUS_MISSING) {
3708 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3713 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3715 if (unlinkat(dirfd, de_name, 0) != 0) {
3716 err = got_error_from_errno2("unlinkat",
3720 } else if (unlink(ondisk_path) != 0) {
3721 err = got_error_from_errno2("unlink", ondisk_path);
3725 parent = dirname(ondisk_path);
3727 if (parent == NULL) {
3728 err = got_error_from_errno2("dirname", ondisk_path);
3731 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3732 if (rmdir(parent) == -1) {
3733 if (errno != ENOTEMPTY)
3734 err = got_error_from_errno2("rmdir",
3738 parent = dirname(parent);
3739 if (parent == NULL) {
3740 err = got_error_from_errno2("dirname", parent);
3746 got_fileindex_entry_mark_deleted_from_disk(ie);
3751 if (status == GOT_STATUS_DELETE)
3753 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3754 staged_status, relpath);
3757 const struct got_error *
3758 got_worktree_schedule_delete(struct got_worktree *worktree,
3759 struct got_pathlist_head *paths, int delete_local_mods,
3760 got_worktree_delete_cb progress_cb, void *progress_arg,
3761 struct got_repository *repo, int keep_on_disk)
3763 struct got_fileindex *fileindex = NULL;
3764 char *fileindex_path = NULL;
3765 const struct got_error *err = NULL, *sync_err, *unlockerr;
3766 struct got_pathlist_entry *pe;
3767 struct schedule_deletion_args sda;
3769 err = lock_worktree(worktree, LOCK_EX);
3773 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3777 sda.worktree = worktree;
3778 sda.fileindex = fileindex;
3779 sda.progress_cb = progress_cb;
3780 sda.progress_arg = progress_arg;
3782 sda.delete_local_mods = delete_local_mods;
3783 sda.keep_on_disk = keep_on_disk;
3785 TAILQ_FOREACH(pe, paths, entry) {
3786 err = worktree_status(worktree, pe->path, fileindex, repo,
3787 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3791 sync_err = sync_fileindex(fileindex, fileindex_path);
3792 if (sync_err && err == NULL)
3795 free(fileindex_path);
3797 got_fileindex_free(fileindex);
3798 unlockerr = lock_worktree(worktree, LOCK_SH);
3799 if (unlockerr && err == NULL)
3804 static const struct got_error *
3805 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3807 const struct got_error *err = NULL;
3809 size_t linesize = 0, n;
3812 linelen = getline(&line, &linesize, infile);
3813 if (linelen == -1) {
3814 if (ferror(infile)) {
3815 err = got_error_from_errno("getline");
3821 n = fwrite(line, 1, linelen, outfile);
3823 err = got_ferror(outfile, GOT_ERR_IO);
3828 n = fwrite(line, 1, linelen, rejectfile);
3830 err = got_ferror(outfile, GOT_ERR_IO);
3837 static const struct got_error *
3838 skip_one_line(FILE *f)
3841 size_t linesize = 0;
3844 linelen = getline(&line, &linesize, f);
3845 if (linelen == -1) {
3847 return got_error_from_errno("getline");
3854 static const struct got_error *
3855 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3856 int start_old, int end_old, int start_new, int end_new,
3857 FILE *outfile, FILE *rejectfile)
3859 const struct got_error *err;
3861 /* Copy old file's lines leading up to patch. */
3862 while (!feof(f1) && *line_cur1 < start_old) {
3863 err = copy_one_line(f1, outfile, NULL);
3868 /* Skip new file's lines leading up to patch. */
3869 while (!feof(f2) && *line_cur2 < start_new) {
3871 err = copy_one_line(f2, NULL, rejectfile);
3873 err = skip_one_line(f2);
3878 /* Copy patched lines. */
3879 while (!feof(f2) && *line_cur2 <= end_new) {
3880 err = copy_one_line(f2, outfile, NULL);
3885 /* Skip over old file's replaced lines. */
3886 while (!feof(f1) && *line_cur1 <= end_old) {
3888 err = copy_one_line(f1, NULL, rejectfile);
3890 err = skip_one_line(f1);
3899 static const struct got_error *
3900 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3901 FILE *outfile, FILE *rejectfile)
3903 const struct got_error *err;
3906 /* Copy old file's lines until EOF. */
3908 err = copy_one_line(f1, outfile, NULL);
3915 /* Copy new file's lines until EOF. */
3917 err = copy_one_line(f2, NULL, rejectfile);
3927 static const struct got_error *
3928 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3929 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3930 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3931 int *line_cur2, FILE *outfile, FILE *rejectfile,
3932 got_worktree_patch_cb patch_cb, void *patch_arg)
3934 const struct got_error *err = NULL;
3935 int start_old = change->cv.a;
3936 int end_old = change->cv.b;
3937 int start_new = change->cv.c;
3938 int end_new = change->cv.d;
3942 *choice = GOT_PATCH_CHOICE_NONE;
3944 hunkfile = got_opentemp();
3945 if (hunkfile == NULL)
3946 return got_error_from_errno("got_opentemp");
3951 /* XXX TODO needs error checking */
3952 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3954 if (fseek(f1, pos1, SEEK_SET) == -1) {
3955 err = got_ferror(f1, GOT_ERR_IO);
3958 if (fseek(f2, pos2, SEEK_SET) == -1) {
3959 err = got_ferror(f1, GOT_ERR_IO);
3962 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3963 err = got_ferror(hunkfile, GOT_ERR_IO);
3967 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3968 hunkfile, n, nchanges);
3973 case GOT_PATCH_CHOICE_YES:
3974 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3975 end_old, start_new, end_new, outfile, rejectfile);
3977 case GOT_PATCH_CHOICE_NO:
3978 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3979 end_old, start_new, end_new, rejectfile, outfile);
3981 case GOT_PATCH_CHOICE_QUIT:
3984 err = got_error(GOT_ERR_PATCH_CHOICE);
3988 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3989 err = got_error_from_errno("fclose");
3993 struct revert_file_args {
3994 struct got_worktree *worktree;
3995 struct got_fileindex *fileindex;
3996 got_worktree_checkout_cb progress_cb;
3998 got_worktree_patch_cb patch_cb;
4000 struct got_repository *repo;
4003 static const struct got_error *
4004 create_patched_content(char **path_outfile, int reverse_patch,
4005 struct got_object_id *blob_id, const char *path2,
4006 int dirfd2, const char *de_name2,
4007 const char *relpath, struct got_repository *repo,
4008 got_worktree_patch_cb patch_cb, void *patch_arg)
4010 const struct got_error *err;
4011 struct got_blob_object *blob = NULL;
4012 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4014 char *path1 = NULL, *id_str = NULL;
4015 struct stat sb1, sb2;
4016 struct got_diff_changes *changes = NULL;
4017 struct got_diff_state *ds = NULL;
4018 struct got_diff_args *args = NULL;
4019 struct got_diff_change *change;
4020 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4023 *path_outfile = NULL;
4025 err = got_object_id_str(&id_str, blob_id);
4030 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4032 err = got_error_from_errno2("openat", path2);
4036 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4038 err = got_error_from_errno2("open", path2);
4042 if (fstat(fd2, &sb2) == -1) {
4043 err = got_error_from_errno2("fstat", path2);
4047 f2 = fdopen(fd2, "r");
4049 err = got_error_from_errno2("fdopen", path2);
4054 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4058 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4062 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4066 if (stat(path1, &sb1) == -1) {
4067 err = got_error_from_errno2("stat", path1);
4071 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4072 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4076 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4080 if (fseek(f1, 0L, SEEK_SET) == -1)
4081 return got_ferror(f1, GOT_ERR_IO);
4082 if (fseek(f2, 0L, SEEK_SET) == -1)
4083 return got_ferror(f2, GOT_ERR_IO);
4084 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4086 err = apply_or_reject_change(&choice, change, ++n,
4087 changes->nchanges, ds, args, diff_flags, relpath,
4088 f1, f2, &line_cur1, &line_cur2,
4089 reverse_patch ? NULL : outfile,
4090 reverse_patch ? outfile : NULL,
4091 patch_cb, patch_arg);
4094 if (choice == GOT_PATCH_CHOICE_YES)
4096 else if (choice == GOT_PATCH_CHOICE_QUIT)
4100 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4101 reverse_patch ? NULL : outfile,
4102 reverse_patch ? outfile : NULL);
4106 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4107 err = got_error_from_errno2("chmod", path2);
4114 got_object_blob_close(blob);
4115 if (f1 && fclose(f1) == EOF && err == NULL)
4116 err = got_error_from_errno2("fclose", path1);
4117 if (f2 && fclose(f2) == EOF && err == NULL)
4118 err = got_error_from_errno2("fclose", path2);
4119 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4120 err = got_error_from_errno2("close", path2);
4121 if (outfile && fclose(outfile) == EOF && err == NULL)
4122 err = got_error_from_errno2("fclose", *path_outfile);
4123 if (path1 && unlink(path1) == -1 && err == NULL)
4124 err = got_error_from_errno2("unlink", path1);
4125 if (err || !have_content) {
4126 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4127 err = got_error_from_errno2("unlink", *path_outfile);
4128 free(*path_outfile);
4129 *path_outfile = NULL;
4133 got_diff_state_free(ds);
4137 got_diff_free_changes(changes);
4142 static const struct got_error *
4143 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4144 const char *relpath, struct got_object_id *blob_id,
4145 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4146 int dirfd, const char *de_name)
4148 struct revert_file_args *a = arg;
4149 const struct got_error *err = NULL;
4150 char *parent_path = NULL;
4151 struct got_fileindex_entry *ie;
4152 struct got_tree_object *tree = NULL;
4153 struct got_object_id *tree_id = NULL;
4154 const struct got_tree_entry *te = NULL;
4155 char *tree_path = NULL, *te_name;
4156 char *ondisk_path = NULL, *path_content = NULL;
4157 struct got_blob_object *blob = NULL;
4159 /* Reverting a staged deletion is a no-op. */
4160 if (status == GOT_STATUS_DELETE &&
4161 staged_status != GOT_STATUS_NO_CHANGE)
4164 if (status == GOT_STATUS_UNVERSIONED)
4165 return (*a->progress_cb)(a->progress_arg,
4166 GOT_STATUS_UNVERSIONED, relpath);
4168 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4170 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4172 /* Construct in-repository path of tree which contains this blob. */
4173 err = got_path_dirname(&parent_path, ie->path);
4175 if (err->code != GOT_ERR_BAD_PATH)
4177 parent_path = strdup("/");
4178 if (parent_path == NULL) {
4179 err = got_error_from_errno("strdup");
4183 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4184 tree_path = strdup(parent_path);
4185 if (tree_path == NULL) {
4186 err = got_error_from_errno("strdup");
4190 if (got_path_is_root_dir(parent_path)) {
4191 tree_path = strdup(a->worktree->path_prefix);
4192 if (tree_path == NULL) {
4193 err = got_error_from_errno("strdup");
4197 if (asprintf(&tree_path, "%s/%s",
4198 a->worktree->path_prefix, parent_path) == -1) {
4199 err = got_error_from_errno("asprintf");
4205 err = got_object_id_by_path(&tree_id, a->repo,
4206 a->worktree->base_commit_id, tree_path);
4208 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4209 (status == GOT_STATUS_ADD ||
4210 staged_status == GOT_STATUS_ADD)))
4213 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4217 te_name = basename(ie->path);
4218 if (te_name == NULL) {
4219 err = got_error_from_errno2("basename", ie->path);
4223 te = got_object_tree_find_entry(tree, te_name);
4224 if (te == NULL && status != GOT_STATUS_ADD &&
4225 staged_status != GOT_STATUS_ADD) {
4226 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4232 case GOT_STATUS_ADD:
4234 int choice = GOT_PATCH_CHOICE_NONE;
4235 err = (*a->patch_cb)(&choice, a->patch_arg,
4236 status, ie->path, NULL, 1, 1);
4239 if (choice != GOT_PATCH_CHOICE_YES)
4242 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4246 got_fileindex_entry_remove(a->fileindex, ie);
4248 case GOT_STATUS_DELETE:
4250 int choice = GOT_PATCH_CHOICE_NONE;
4251 err = (*a->patch_cb)(&choice, a->patch_arg,
4252 status, ie->path, NULL, 1, 1);
4255 if (choice != GOT_PATCH_CHOICE_YES)
4259 case GOT_STATUS_MODIFY:
4260 case GOT_STATUS_MODE_CHANGE:
4261 case GOT_STATUS_CONFLICT:
4262 case GOT_STATUS_MISSING: {
4263 struct got_object_id id;
4264 if (staged_status == GOT_STATUS_ADD ||
4265 staged_status == GOT_STATUS_MODIFY) {
4266 memcpy(id.sha1, ie->staged_blob_sha1,
4267 SHA1_DIGEST_LENGTH);
4269 memcpy(id.sha1, ie->blob_sha1,
4270 SHA1_DIGEST_LENGTH);
4271 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4275 if (asprintf(&ondisk_path, "%s/%s",
4276 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4277 err = got_error_from_errno("asprintf");
4281 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4282 status == GOT_STATUS_CONFLICT)) {
4283 err = create_patched_content(&path_content, 1, &id,
4284 ondisk_path, dirfd, de_name, ie->path, a->repo,
4285 a->patch_cb, a->patch_arg);
4286 if (err || path_content == NULL)
4288 if (rename(path_content, ondisk_path) == -1) {
4289 err = got_error_from_errno3("rename",
4290 path_content, ondisk_path);
4294 int is_bad_symlink = 0;
4295 if (te && S_ISLNK(te->mode)) {
4296 err = install_symlink(&is_bad_symlink,
4297 a->worktree, ondisk_path, ie->path,
4298 blob, 0, 1, a->repo,
4299 a->progress_cb, a->progress_arg);
4301 err = install_blob(a->worktree, ondisk_path,
4303 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4304 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4305 a->repo, a->progress_cb, a->progress_arg);
4309 if (status == GOT_STATUS_DELETE ||
4310 status == GOT_STATUS_MODE_CHANGE) {
4311 err = got_fileindex_entry_update(ie,
4312 ondisk_path, blob->id.sha1,
4313 a->worktree->base_commit_id->sha1, 1);
4317 if (is_bad_symlink) {
4318 err = got_fileindex_entry_filetype_set(ie,
4319 GOT_FILEIDX_MODE_BAD_SYMLINK);
4335 got_object_blob_close(blob);
4337 got_object_tree_close(tree);
4342 const struct got_error *
4343 got_worktree_revert(struct got_worktree *worktree,
4344 struct got_pathlist_head *paths,
4345 got_worktree_checkout_cb progress_cb, void *progress_arg,
4346 got_worktree_patch_cb patch_cb, void *patch_arg,
4347 struct got_repository *repo)
4349 struct got_fileindex *fileindex = NULL;
4350 char *fileindex_path = NULL;
4351 const struct got_error *err = NULL, *unlockerr = NULL;
4352 const struct got_error *sync_err = NULL;
4353 struct got_pathlist_entry *pe;
4354 struct revert_file_args rfa;
4356 err = lock_worktree(worktree, LOCK_EX);
4360 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4364 rfa.worktree = worktree;
4365 rfa.fileindex = fileindex;
4366 rfa.progress_cb = progress_cb;
4367 rfa.progress_arg = progress_arg;
4368 rfa.patch_cb = patch_cb;
4369 rfa.patch_arg = patch_arg;
4371 TAILQ_FOREACH(pe, paths, entry) {
4372 err = worktree_status(worktree, pe->path, fileindex, repo,
4373 revert_file, &rfa, NULL, NULL, 0, 0);
4377 sync_err = sync_fileindex(fileindex, fileindex_path);
4378 if (sync_err && err == NULL)
4381 free(fileindex_path);
4383 got_fileindex_free(fileindex);
4384 unlockerr = lock_worktree(worktree, LOCK_SH);
4385 if (unlockerr && err == NULL)
4391 free_commitable(struct got_commitable *ct)
4394 free(ct->in_repo_path);
4395 free(ct->ondisk_path);
4397 free(ct->base_blob_id);
4398 free(ct->staged_blob_id);
4399 free(ct->base_commit_id);
4403 struct collect_commitables_arg {
4404 struct got_pathlist_head *commitable_paths;
4405 struct got_repository *repo;
4406 struct got_worktree *worktree;
4407 int have_staged_files;
4410 static const struct got_error *
4411 collect_commitables(void *arg, unsigned char status,
4412 unsigned char staged_status, const char *relpath,
4413 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4414 struct got_object_id *commit_id, int dirfd, const char *de_name)
4416 struct collect_commitables_arg *a = arg;
4417 const struct got_error *err = NULL;
4418 struct got_commitable *ct = NULL;
4419 struct got_pathlist_entry *new = NULL;
4420 char *parent_path = NULL, *path = NULL;
4423 if (a->have_staged_files) {
4424 if (staged_status != GOT_STATUS_MODIFY &&
4425 staged_status != GOT_STATUS_ADD &&
4426 staged_status != GOT_STATUS_DELETE)
4429 if (status == GOT_STATUS_CONFLICT)
4430 return got_error(GOT_ERR_COMMIT_CONFLICT);
4432 if (status != GOT_STATUS_MODIFY &&
4433 status != GOT_STATUS_MODE_CHANGE &&
4434 status != GOT_STATUS_ADD &&
4435 status != GOT_STATUS_DELETE)
4439 if (asprintf(&path, "/%s", relpath) == -1) {
4440 err = got_error_from_errno("asprintf");
4443 if (strcmp(path, "/") == 0) {
4444 parent_path = strdup("");
4445 if (parent_path == NULL)
4446 return got_error_from_errno("strdup");
4448 err = got_path_dirname(&parent_path, path);
4453 ct = calloc(1, sizeof(*ct));
4455 err = got_error_from_errno("calloc");
4459 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4461 err = got_error_from_errno("asprintf");
4464 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4465 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4468 if (fstatat(dirfd, de_name, &sb,
4469 AT_SYMLINK_NOFOLLOW) == -1) {
4470 err = got_error_from_errno2("fstatat",
4474 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4475 err = got_error_from_errno2("lstat", ct->ondisk_path);
4478 ct->mode = sb.st_mode;
4481 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4482 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4484 err = got_error_from_errno("asprintf");
4488 ct->status = status;
4489 ct->staged_status = staged_status;
4490 ct->blob_id = NULL; /* will be filled in when blob gets created */
4491 if (ct->status != GOT_STATUS_ADD &&
4492 ct->staged_status != GOT_STATUS_ADD) {
4493 ct->base_blob_id = got_object_id_dup(blob_id);
4494 if (ct->base_blob_id == NULL) {
4495 err = got_error_from_errno("got_object_id_dup");
4498 ct->base_commit_id = got_object_id_dup(commit_id);
4499 if (ct->base_commit_id == NULL) {
4500 err = got_error_from_errno("got_object_id_dup");
4504 if (ct->staged_status == GOT_STATUS_ADD ||
4505 ct->staged_status == GOT_STATUS_MODIFY) {
4506 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4507 if (ct->staged_blob_id == NULL) {
4508 err = got_error_from_errno("got_object_id_dup");
4512 ct->path = strdup(path);
4513 if (ct->path == NULL) {
4514 err = got_error_from_errno("strdup");
4517 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4519 if (ct && (err || new == NULL))
4520 free_commitable(ct);
4526 static const struct got_error *write_tree(struct got_object_id **, int *,
4527 struct got_tree_object *, const char *, struct got_pathlist_head *,
4528 got_worktree_status_cb status_cb, void *status_arg,
4529 struct got_repository *);
4531 static const struct got_error *
4532 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4533 struct got_tree_entry *te, const char *parent_path,
4534 struct got_pathlist_head *commitable_paths,
4535 got_worktree_status_cb status_cb, void *status_arg,
4536 struct got_repository *repo)
4538 const struct got_error *err = NULL;
4539 struct got_tree_object *subtree;
4542 if (asprintf(&subpath, "%s%s%s", parent_path,
4543 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4544 return got_error_from_errno("asprintf");
4546 err = got_object_open_as_tree(&subtree, repo, &te->id);
4550 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4551 commitable_paths, status_cb, status_arg, repo);
4552 got_object_tree_close(subtree);
4557 static const struct got_error *
4558 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4560 const struct got_error *err = NULL;
4561 char *ct_parent_path = NULL;
4565 if (strchr(ct->in_repo_path, '/') == NULL) {
4566 *match = got_path_is_root_dir(path);
4570 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4573 *match = (strcmp(path, ct_parent_path) == 0);
4574 free(ct_parent_path);
4579 get_ct_file_mode(struct got_commitable *ct)
4581 if (S_ISLNK(ct->mode))
4584 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4587 static const struct got_error *
4588 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4589 struct got_tree_entry *te, struct got_commitable *ct)
4591 const struct got_error *err = NULL;
4595 err = got_object_tree_entry_dup(new_te, te);
4599 (*new_te)->mode = get_ct_file_mode(ct);
4601 if (ct->staged_status == GOT_STATUS_MODIFY)
4602 memcpy(&(*new_te)->id, ct->staged_blob_id,
4603 sizeof((*new_te)->id));
4605 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4607 if (err && *new_te) {
4614 static const struct got_error *
4615 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4616 struct got_commitable *ct)
4618 const struct got_error *err = NULL;
4623 *new_te = calloc(1, sizeof(**new_te));
4624 if (*new_te == NULL)
4625 return got_error_from_errno("calloc");
4627 ct_name = basename(ct->path);
4628 if (ct_name == NULL) {
4629 err = got_error_from_errno2("basename", ct->path);
4632 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4633 sizeof((*new_te)->name)) {
4634 err = got_error(GOT_ERR_NO_SPACE);
4638 (*new_te)->mode = get_ct_file_mode(ct);
4640 if (ct->staged_status == GOT_STATUS_ADD)
4641 memcpy(&(*new_te)->id, ct->staged_blob_id,
4642 sizeof((*new_te)->id));
4644 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4646 if (err && *new_te) {
4653 static const struct got_error *
4654 insert_tree_entry(struct got_tree_entry *new_te,
4655 struct got_pathlist_head *paths)
4657 const struct got_error *err = NULL;
4658 struct got_pathlist_entry *new_pe;
4660 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4664 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4668 static const struct got_error *
4669 report_ct_status(struct got_commitable *ct,
4670 got_worktree_status_cb status_cb, void *status_arg)
4672 const char *ct_path = ct->path;
4673 unsigned char status;
4675 while (ct_path[0] == '/')
4678 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4679 status = ct->staged_status;
4681 status = ct->status;
4683 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4684 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4687 static const struct got_error *
4688 match_modified_subtree(int *modified, struct got_tree_entry *te,
4689 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4691 const struct got_error *err = NULL;
4692 struct got_pathlist_entry *pe;
4697 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4698 got_path_is_root_dir(base_tree_path) ? "" : "/",
4700 return got_error_from_errno("asprintf");
4702 TAILQ_FOREACH(pe, commitable_paths, entry) {
4703 struct got_commitable *ct = pe->data;
4704 *modified = got_path_is_child(ct->in_repo_path, te_path,
4714 static const struct got_error *
4715 match_deleted_or_modified_ct(struct got_commitable **ctp,
4716 struct got_tree_entry *te, const char *base_tree_path,
4717 struct got_pathlist_head *commitable_paths)
4719 const struct got_error *err = NULL;
4720 struct got_pathlist_entry *pe;
4724 TAILQ_FOREACH(pe, commitable_paths, entry) {
4725 struct got_commitable *ct = pe->data;
4726 char *ct_name = NULL;
4729 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4730 if (ct->status != GOT_STATUS_MODIFY &&
4731 ct->status != GOT_STATUS_MODE_CHANGE &&
4732 ct->status != GOT_STATUS_DELETE)
4735 if (ct->staged_status != GOT_STATUS_MODIFY &&
4736 ct->staged_status != GOT_STATUS_DELETE)
4740 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4743 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4749 ct_name = basename(pe->path);
4750 if (ct_name == NULL)
4751 return got_error_from_errno2("basename", pe->path);
4753 if (strcmp(te->name, ct_name) != 0)
4763 static const struct got_error *
4764 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4765 const char *child_path, const char *path_base_tree,
4766 struct got_pathlist_head *commitable_paths,
4767 got_worktree_status_cb status_cb, void *status_arg,
4768 struct got_repository *repo)
4770 const struct got_error *err = NULL;
4771 struct got_tree_entry *new_te;
4773 struct got_object_id *id = NULL;
4778 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4779 got_path_is_root_dir(path_base_tree) ? "" : "/",
4781 return got_error_from_errno("asprintf");
4783 new_te = calloc(1, sizeof(*new_te));
4785 return got_error_from_errno("calloc");
4786 new_te->mode = S_IFDIR;
4788 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4789 sizeof(new_te->name)) {
4790 err = got_error(GOT_ERR_NO_SPACE);
4793 err = write_tree(&id, &nentries, NULL, subtree_path,
4794 commitable_paths, status_cb, status_arg, repo);
4799 memcpy(&new_te->id, id, sizeof(new_te->id));
4808 static const struct got_error *
4809 write_tree(struct got_object_id **new_tree_id, int *nentries,
4810 struct got_tree_object *base_tree, const char *path_base_tree,
4811 struct got_pathlist_head *commitable_paths,
4812 got_worktree_status_cb status_cb, void *status_arg,
4813 struct got_repository *repo)
4815 const struct got_error *err = NULL;
4816 struct got_pathlist_head paths;
4817 struct got_tree_entry *te, *new_te = NULL;
4818 struct got_pathlist_entry *pe;
4823 /* Insert, and recurse into, newly added entries first. */
4824 TAILQ_FOREACH(pe, commitable_paths, entry) {
4825 struct got_commitable *ct = pe->data;
4826 char *child_path = NULL, *slash;
4828 if ((ct->status != GOT_STATUS_ADD &&
4829 ct->staged_status != GOT_STATUS_ADD) ||
4830 (ct->flags & GOT_COMMITABLE_ADDED))
4833 if (!got_path_is_child(pe->path, path_base_tree,
4834 strlen(path_base_tree)))
4837 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4842 slash = strchr(child_path, '/');
4843 if (slash == NULL) {
4844 err = alloc_added_blob_tree_entry(&new_te, ct);
4847 err = report_ct_status(ct, status_cb, status_arg);
4850 ct->flags |= GOT_COMMITABLE_ADDED;
4851 err = insert_tree_entry(new_te, &paths);
4856 *slash = '\0'; /* trim trailing path components */
4857 if (base_tree == NULL ||
4858 got_object_tree_find_entry(base_tree, child_path)
4860 err = make_subtree_for_added_blob(&new_te,
4861 child_path, path_base_tree,
4862 commitable_paths, status_cb, status_arg,
4866 err = insert_tree_entry(new_te, &paths);
4875 int i, nbase_entries;
4876 /* Handle modified and deleted entries. */
4877 nbase_entries = got_object_tree_get_nentries(base_tree);
4878 for (i = 0; i < nbase_entries; i++) {
4879 struct got_commitable *ct = NULL;
4881 te = got_object_tree_get_entry(base_tree, i);
4882 if (got_object_tree_entry_is_submodule(te)) {
4883 /* Entry is a submodule; just copy it. */
4884 err = got_object_tree_entry_dup(&new_te, te);
4887 err = insert_tree_entry(new_te, &paths);
4894 if (S_ISDIR(te->mode)) {
4896 err = got_object_tree_entry_dup(&new_te, te);
4899 err = match_modified_subtree(&modified, te,
4900 path_base_tree, commitable_paths);
4903 /* Avoid recursion into unmodified subtrees. */
4905 struct got_object_id *new_id;
4907 err = write_subtree(&new_id,
4909 path_base_tree, commitable_paths,
4910 status_cb, status_arg, repo);
4913 if (nsubentries == 0) {
4914 /* All entries were deleted. */
4918 memcpy(&new_te->id, new_id,
4919 sizeof(new_te->id));
4922 err = insert_tree_entry(new_te, &paths);
4929 err = match_deleted_or_modified_ct(&ct, te,
4930 path_base_tree, commitable_paths);
4934 /* NB: Deleted entries get dropped here. */
4935 if (ct->status == GOT_STATUS_MODIFY ||
4936 ct->status == GOT_STATUS_MODE_CHANGE ||
4937 ct->staged_status == GOT_STATUS_MODIFY) {
4938 err = alloc_modified_blob_tree_entry(
4942 err = insert_tree_entry(new_te, &paths);
4947 err = report_ct_status(ct, status_cb,
4952 /* Entry is unchanged; just copy it. */
4953 err = got_object_tree_entry_dup(&new_te, te);
4956 err = insert_tree_entry(new_te, &paths);
4964 /* Write new list of entries; deleted entries have been dropped. */
4965 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4967 got_pathlist_free(&paths);
4971 static const struct got_error *
4972 reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
4973 struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
4974 struct got_repository *repo)
4976 const struct got_error *err = NULL;
4977 struct got_blob_object *blob = NULL;
4978 struct got_object_id *tree_id = NULL;
4979 char *tree_path = NULL;
4980 struct got_tree_object *tree = NULL;
4981 struct got_tree_entry *te;
4984 err = got_object_open_as_blob(&blob, repo, ct->blob_id, PATH_MAX);
4988 err = got_path_dirname(&tree_path, ct->in_repo_path);
4990 if (err->code != GOT_ERR_BAD_PATH)
4992 err = got_object_id_by_path(&tree_id, repo,
4993 new_base_commit_id, "");
4997 err = got_object_id_by_path(&tree_id, repo,
4998 new_base_commit_id, tree_path);
5003 err = got_object_open_as_tree(&tree, repo, tree_id);
5007 entry_name = basename(ct->path);
5008 if (entry_name == NULL) {
5009 err = got_error_from_errno2("basename", ct->path);
5013 te = got_object_tree_find_entry(tree, entry_name);
5015 err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5019 err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5020 ct->path, blob, 0, 0, repo, NULL, NULL);
5023 got_object_blob_close(blob);
5025 got_object_tree_close(tree);
5032 * After comitting a symlink we have a chance to convert "bad" symlinks
5033 * (those which point outside the work tree or into .got) to regular files.
5034 * This way, the post-commit work tree state matches a fresh checkout of
5035 * the tree which was just committed. We also mark such newly committed
5036 * symlinks as "bad" in the work tree's fileindex.
5038 static const struct got_error *
5039 reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5040 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5041 struct got_worktree *worktree, struct got_repository *repo)
5043 const struct got_error *err = NULL;
5044 struct got_pathlist_entry *pe;
5046 TAILQ_FOREACH(pe, commitable_paths, entry) {
5047 struct got_commitable *ct = pe->data;
5048 struct got_fileindex_entry *ie;
5049 int is_bad_symlink = 0;
5051 if (!S_ISLNK(get_ct_file_mode(ct)))
5054 err = reinstall_symlink_after_commit(&is_bad_symlink,
5055 ct, new_base_commit_id, worktree, repo);
5058 ie = got_fileindex_entry_get(fileindex, ct->path,
5060 if (ie && is_bad_symlink) {
5061 err = got_fileindex_entry_filetype_set(ie,
5062 GOT_FILEIDX_MODE_BAD_SYMLINK);
5071 static const struct got_error *
5072 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5073 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5074 int have_staged_files)
5076 const struct got_error *err = NULL;
5077 struct got_pathlist_entry *pe;
5079 TAILQ_FOREACH(pe, commitable_paths, entry) {
5080 struct got_fileindex_entry *ie;
5081 struct got_commitable *ct = pe->data;
5083 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5085 if (ct->status == GOT_STATUS_DELETE ||
5086 ct->staged_status == GOT_STATUS_DELETE) {
5087 got_fileindex_entry_remove(fileindex, ie);
5088 } else if (ct->staged_status == GOT_STATUS_ADD ||
5089 ct->staged_status == GOT_STATUS_MODIFY) {
5090 got_fileindex_entry_stage_set(ie,
5091 GOT_FILEIDX_STAGE_NONE);
5092 err = got_fileindex_entry_update(ie,
5093 ct->ondisk_path, ct->staged_blob_id->sha1,
5094 new_base_commit_id->sha1,
5095 !have_staged_files);
5097 err = got_fileindex_entry_update(ie,
5098 ct->ondisk_path, ct->blob_id->sha1,
5099 new_base_commit_id->sha1,
5100 !have_staged_files);
5102 err = got_fileindex_entry_alloc(&ie, pe->path);
5105 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5106 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5108 got_fileindex_entry_free(ie);
5111 err = got_fileindex_entry_add(fileindex, ie);
5113 got_fileindex_entry_free(ie);
5122 static const struct got_error *
5123 check_out_of_date(const char *in_repo_path, unsigned char status,
5124 unsigned char staged_status, struct got_object_id *base_blob_id,
5125 struct got_object_id *base_commit_id,
5126 struct got_object_id *head_commit_id, struct got_repository *repo,
5129 const struct got_error *err = NULL;
5130 struct got_object_id *id = NULL;
5132 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5133 /* Trivial case: base commit == head commit */
5134 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5137 * Ensure file content which local changes were based
5138 * on matches file content in the branch head.
5140 err = got_object_id_by_path(&id, repo, head_commit_id,
5143 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5144 err = got_error(ood_errcode);
5146 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5147 err = got_error(ood_errcode);
5149 /* Require that added files don't exist in the branch head. */
5150 err = got_object_id_by_path(&id, repo, head_commit_id,
5152 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5154 err = id ? got_error(ood_errcode) : NULL;
5161 const struct got_error *
5162 commit_worktree(struct got_object_id **new_commit_id,
5163 struct got_pathlist_head *commitable_paths,
5164 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5165 const char *author, const char *committer,
5166 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5167 got_worktree_status_cb status_cb, void *status_arg,
5168 struct got_repository *repo)
5170 const struct got_error *err = NULL, *unlockerr = NULL;
5171 struct got_pathlist_entry *pe;
5172 const char *head_ref_name = NULL;
5173 struct got_commit_object *head_commit = NULL;
5174 struct got_reference *head_ref2 = NULL;
5175 struct got_object_id *head_commit_id2 = NULL;
5176 struct got_tree_object *head_tree = NULL;
5177 struct got_object_id *new_tree_id = NULL;
5179 struct got_object_id_queue parent_ids;
5180 struct got_object_qid *pid = NULL;
5181 char *logmsg = NULL;
5183 *new_commit_id = NULL;
5185 SIMPLEQ_INIT(&parent_ids);
5187 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5191 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5195 if (commit_msg_cb != NULL) {
5196 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5201 if (logmsg == NULL || strlen(logmsg) == 0) {
5202 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5206 /* Create blobs from added and modified files and record their IDs. */
5207 TAILQ_FOREACH(pe, commitable_paths, entry) {
5208 struct got_commitable *ct = pe->data;
5211 /* Blobs for staged files already exist. */
5212 if (ct->staged_status == GOT_STATUS_ADD ||
5213 ct->staged_status == GOT_STATUS_MODIFY)
5216 if (ct->status != GOT_STATUS_ADD &&
5217 ct->status != GOT_STATUS_MODIFY &&
5218 ct->status != GOT_STATUS_MODE_CHANGE)
5221 if (asprintf(&ondisk_path, "%s/%s",
5222 worktree->root_path, pe->path) == -1) {
5223 err = got_error_from_errno("asprintf");
5226 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5232 /* Recursively write new tree objects. */
5233 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5234 commitable_paths, status_cb, status_arg, repo);
5238 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5241 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5242 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5243 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5244 got_object_qid_free(pid);
5250 /* Check if a concurrent commit to our branch has occurred. */
5251 head_ref_name = got_worktree_get_head_ref_name(worktree);
5252 if (head_ref_name == NULL) {
5253 err = got_error_from_errno("got_worktree_get_head_ref_name");
5256 /* Lock the reference here to prevent concurrent modification. */
5257 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5260 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5263 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5264 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5267 /* Update branch head in repository. */
5268 err = got_ref_change_ref(head_ref2, *new_commit_id);
5271 err = got_ref_write(head_ref2, repo);
5275 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5279 err = ref_base_commit(worktree, repo);
5284 got_object_tree_close(head_tree);
5286 got_object_commit_close(head_commit);
5287 free(head_commit_id2);
5289 unlockerr = got_ref_unlock(head_ref2);
5290 if (unlockerr && err == NULL)
5292 got_ref_close(head_ref2);
5297 static const struct got_error *
5298 check_path_is_commitable(const char *path,
5299 struct got_pathlist_head *commitable_paths)
5301 struct got_pathlist_entry *cpe = NULL;
5302 size_t path_len = strlen(path);
5304 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5305 struct got_commitable *ct = cpe->data;
5306 const char *ct_path = ct->path;
5308 while (ct_path[0] == '/')
5311 if (strcmp(path, ct_path) == 0 ||
5312 got_path_is_child(ct_path, path, path_len))
5317 return got_error_path(path, GOT_ERR_BAD_PATH);
5322 static const struct got_error *
5323 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5325 int *have_staged_files = arg;
5327 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5328 *have_staged_files = 1;
5329 return got_error(GOT_ERR_CANCELLED);
5335 static const struct got_error *
5336 check_non_staged_files(struct got_fileindex *fileindex,
5337 struct got_pathlist_head *paths)
5339 struct got_pathlist_entry *pe;
5340 struct got_fileindex_entry *ie;
5342 TAILQ_FOREACH(pe, paths, entry) {
5343 if (pe->path[0] == '\0')
5345 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5347 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5348 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5349 return got_error_path(pe->path,
5350 GOT_ERR_FILE_NOT_STAGED);
5356 const struct got_error *
5357 got_worktree_commit(struct got_object_id **new_commit_id,
5358 struct got_worktree *worktree, struct got_pathlist_head *paths,
5359 const char *author, const char *committer,
5360 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5361 got_worktree_status_cb status_cb, void *status_arg,
5362 struct got_repository *repo)
5364 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5365 struct got_fileindex *fileindex = NULL;
5366 char *fileindex_path = NULL;
5367 struct got_pathlist_head commitable_paths;
5368 struct collect_commitables_arg cc_arg;
5369 struct got_pathlist_entry *pe;
5370 struct got_reference *head_ref = NULL;
5371 struct got_object_id *head_commit_id = NULL;
5372 int have_staged_files = 0;
5374 *new_commit_id = NULL;
5376 TAILQ_INIT(&commitable_paths);
5378 err = lock_worktree(worktree, LOCK_EX);
5382 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5386 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5390 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5394 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5395 &have_staged_files);
5396 if (err && err->code != GOT_ERR_CANCELLED)
5398 if (have_staged_files) {
5399 err = check_non_staged_files(fileindex, paths);
5404 cc_arg.commitable_paths = &commitable_paths;
5405 cc_arg.worktree = worktree;
5407 cc_arg.have_staged_files = have_staged_files;
5408 TAILQ_FOREACH(pe, paths, entry) {
5409 err = worktree_status(worktree, pe->path, fileindex, repo,
5410 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5415 if (TAILQ_EMPTY(&commitable_paths)) {
5416 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5420 TAILQ_FOREACH(pe, paths, entry) {
5421 err = check_path_is_commitable(pe->path, &commitable_paths);
5426 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5427 struct got_commitable *ct = pe->data;
5428 const char *ct_path = ct->in_repo_path;
5430 while (ct_path[0] == '/')
5432 err = check_out_of_date(ct_path, ct->status,
5433 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5434 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5440 err = commit_worktree(new_commit_id, &commitable_paths,
5441 head_commit_id, worktree, author, committer,
5442 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5446 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5447 fileindex, have_staged_files);
5449 err = reinstall_symlinks_after_commit(&commitable_paths,
5450 *new_commit_id, fileindex, worktree, repo);
5452 sync_err = sync_fileindex(fileindex, fileindex_path);
5453 if (sync_err && err == NULL)
5457 got_fileindex_free(fileindex);
5458 free(fileindex_path);
5459 unlockerr = lock_worktree(worktree, LOCK_SH);
5460 if (unlockerr && err == NULL)
5462 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5463 struct got_commitable *ct = pe->data;
5464 free_commitable(ct);
5466 got_pathlist_free(&commitable_paths);
5471 got_commitable_get_path(struct got_commitable *ct)
5477 got_commitable_get_status(struct got_commitable *ct)
5482 struct check_rebase_ok_arg {
5483 struct got_worktree *worktree;
5484 struct got_repository *repo;
5487 static const struct got_error *
5488 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5490 const struct got_error *err = NULL;
5491 struct check_rebase_ok_arg *a = arg;
5492 unsigned char status;
5496 /* Reject rebase of a work tree with mixed base commits. */
5497 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5498 SHA1_DIGEST_LENGTH))
5499 return got_error(GOT_ERR_MIXED_COMMITS);
5501 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5503 return got_error_from_errno("asprintf");
5505 /* Reject rebase of a work tree with modified or staged files. */
5506 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5511 if (status != GOT_STATUS_NO_CHANGE)
5512 return got_error(GOT_ERR_MODIFIED);
5513 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5514 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5519 const struct got_error *
5520 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5521 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5522 struct got_worktree *worktree, struct got_reference *branch,
5523 struct got_repository *repo)
5525 const struct got_error *err = NULL;
5526 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5527 char *branch_ref_name = NULL;
5528 char *fileindex_path = NULL;
5529 struct check_rebase_ok_arg ok_arg;
5530 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5531 struct got_object_id *wt_branch_tip = NULL;
5533 *new_base_branch_ref = NULL;
5537 err = lock_worktree(worktree, LOCK_EX);
5541 err = open_fileindex(fileindex, &fileindex_path, worktree);
5545 ok_arg.worktree = worktree;
5547 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5552 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5556 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5560 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5564 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5569 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5572 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5573 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5577 err = got_ref_alloc_symref(new_base_branch_ref,
5578 new_base_branch_ref_name, wt_branch);
5581 err = got_ref_write(*new_base_branch_ref, repo);
5585 /* TODO Lock original branch's ref while rebasing? */
5587 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5591 err = got_ref_write(branch_ref, repo);
5595 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5596 worktree->base_commit_id);
5599 err = got_ref_write(*tmp_branch, repo);
5603 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5607 free(fileindex_path);
5608 free(tmp_branch_name);
5609 free(new_base_branch_ref_name);
5610 free(branch_ref_name);
5612 got_ref_close(branch_ref);
5614 got_ref_close(wt_branch);
5615 free(wt_branch_tip);
5617 if (*new_base_branch_ref) {
5618 got_ref_close(*new_base_branch_ref);
5619 *new_base_branch_ref = NULL;
5622 got_ref_close(*tmp_branch);
5626 got_fileindex_free(*fileindex);
5629 lock_worktree(worktree, LOCK_SH);
5634 const struct got_error *
5635 got_worktree_rebase_continue(struct got_object_id **commit_id,
5636 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5637 struct got_reference **branch, struct got_fileindex **fileindex,
5638 struct got_worktree *worktree, struct got_repository *repo)
5640 const struct got_error *err;
5641 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5642 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5643 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5644 char *fileindex_path = NULL;
5645 int have_staged_files = 0;
5648 *new_base_branch = NULL;
5653 err = lock_worktree(worktree, LOCK_EX);
5657 err = open_fileindex(fileindex, &fileindex_path, worktree);
5661 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5662 &have_staged_files);
5663 if (err && err->code != GOT_ERR_CANCELLED)
5665 if (have_staged_files) {
5666 err = got_error(GOT_ERR_STAGED_PATHS);
5670 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5674 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5678 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5682 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5686 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5690 err = got_ref_open(branch, repo,
5691 got_ref_get_symref_target(branch_ref), 0);
5695 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5699 err = got_ref_resolve(commit_id, repo, commit_ref);
5703 err = got_ref_open(new_base_branch, repo,
5704 new_base_branch_ref_name, 0);
5708 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5712 free(commit_ref_name);
5713 free(branch_ref_name);
5714 free(fileindex_path);
5716 got_ref_close(commit_ref);
5718 got_ref_close(branch_ref);
5723 got_ref_close(*tmp_branch);
5726 if (*new_base_branch) {
5727 got_ref_close(*new_base_branch);
5728 *new_base_branch = NULL;
5731 got_ref_close(*branch);
5735 got_fileindex_free(*fileindex);
5738 lock_worktree(worktree, LOCK_SH);
5743 const struct got_error *
5744 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5746 const struct got_error *err;
5747 char *tmp_branch_name = NULL;
5749 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5753 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5754 free(tmp_branch_name);
5758 static const struct got_error *
5759 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5760 char **logmsg, void *arg)
5766 static const struct got_error *
5767 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5768 const char *path, struct got_object_id *blob_id,
5769 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5770 int dirfd, const char *de_name)
5775 struct collect_merged_paths_arg {
5776 got_worktree_checkout_cb progress_cb;
5778 struct got_pathlist_head *merged_paths;
5781 static const struct got_error *
5782 collect_merged_paths(void *arg, unsigned char status, const char *path)
5784 const struct got_error *err;
5785 struct collect_merged_paths_arg *a = arg;
5787 struct got_pathlist_entry *new;
5789 err = (*a->progress_cb)(a->progress_arg, status, path);
5793 if (status != GOT_STATUS_MERGE &&
5794 status != GOT_STATUS_ADD &&
5795 status != GOT_STATUS_DELETE &&
5796 status != GOT_STATUS_CONFLICT)
5801 return got_error_from_errno("strdup");
5803 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5804 if (err || new == NULL)
5810 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5812 struct got_pathlist_entry *pe;
5814 TAILQ_FOREACH(pe, merged_paths, entry)
5815 free((char *)pe->path);
5817 got_pathlist_free(merged_paths);
5820 static const struct got_error *
5821 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5822 int is_rebase, struct got_repository *repo)
5824 const struct got_error *err;
5825 struct got_reference *commit_ref = NULL;
5827 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5829 if (err->code != GOT_ERR_NOT_REF)
5831 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5834 err = got_ref_write(commit_ref, repo);
5837 } else if (is_rebase) {
5838 struct got_object_id *stored_id;
5841 err = got_ref_resolve(&stored_id, repo, commit_ref);
5844 cmp = got_object_id_cmp(commit_id, stored_id);
5847 err = got_error(GOT_ERR_REBASE_COMMITID);
5853 got_ref_close(commit_ref);
5857 static const struct got_error *
5858 rebase_merge_files(struct got_pathlist_head *merged_paths,
5859 const char *commit_ref_name, struct got_worktree *worktree,
5860 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5861 struct got_object_id *commit_id, struct got_repository *repo,
5862 got_worktree_checkout_cb progress_cb, void *progress_arg,
5863 got_cancel_cb cancel_cb, void *cancel_arg)
5865 const struct got_error *err;
5866 struct got_reference *commit_ref = NULL;
5867 struct collect_merged_paths_arg cmp_arg;
5868 char *fileindex_path;
5870 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5872 err = get_fileindex_path(&fileindex_path, worktree);
5876 cmp_arg.progress_cb = progress_cb;
5877 cmp_arg.progress_arg = progress_arg;
5878 cmp_arg.merged_paths = merged_paths;
5879 err = merge_files(worktree, fileindex, fileindex_path,
5880 parent_commit_id, commit_id, repo, collect_merged_paths,
5881 &cmp_arg, cancel_cb, cancel_arg);
5883 got_ref_close(commit_ref);
5887 const struct got_error *
5888 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5889 struct got_worktree *worktree, struct got_fileindex *fileindex,
5890 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5891 struct got_repository *repo,
5892 got_worktree_checkout_cb progress_cb, void *progress_arg,
5893 got_cancel_cb cancel_cb, void *cancel_arg)
5895 const struct got_error *err;
5896 char *commit_ref_name;
5898 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5902 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5906 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5907 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5908 progress_arg, cancel_cb, cancel_arg);
5910 free(commit_ref_name);
5914 const struct got_error *
5915 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5916 struct got_worktree *worktree, struct got_fileindex *fileindex,
5917 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5918 struct got_repository *repo,
5919 got_worktree_checkout_cb progress_cb, void *progress_arg,
5920 got_cancel_cb cancel_cb, void *cancel_arg)
5922 const struct got_error *err;
5923 char *commit_ref_name;
5925 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5929 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5933 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5934 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5935 progress_arg, cancel_cb, cancel_arg);
5937 free(commit_ref_name);
5941 static const struct got_error *
5942 rebase_commit(struct got_object_id **new_commit_id,
5943 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5944 struct got_worktree *worktree, struct got_fileindex *fileindex,
5945 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5946 const char *new_logmsg, struct got_repository *repo)
5948 const struct got_error *err, *sync_err;
5949 struct got_pathlist_head commitable_paths;
5950 struct collect_commitables_arg cc_arg;
5951 char *fileindex_path = NULL;
5952 struct got_reference *head_ref = NULL;
5953 struct got_object_id *head_commit_id = NULL;
5954 char *logmsg = NULL;
5956 TAILQ_INIT(&commitable_paths);
5957 *new_commit_id = NULL;
5959 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5961 err = get_fileindex_path(&fileindex_path, worktree);
5965 cc_arg.commitable_paths = &commitable_paths;
5966 cc_arg.worktree = worktree;
5968 cc_arg.have_staged_files = 0;
5970 * If possible get the status of individual files directly to
5971 * avoid crawling the entire work tree once per rebased commit.
5972 * TODO: Ideally, merged_paths would contain a list of commitables
5973 * we could use so we could skip worktree_status() entirely.
5976 struct got_pathlist_entry *pe;
5977 TAILQ_FOREACH(pe, merged_paths, entry) {
5978 err = worktree_status(worktree, pe->path, fileindex,
5979 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5985 err = worktree_status(worktree, "", fileindex, repo,
5986 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5991 if (TAILQ_EMPTY(&commitable_paths)) {
5992 /* No-op change; commit will be elided. */
5993 err = got_ref_delete(commit_ref, repo);
5996 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6000 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6004 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6009 logmsg = strdup(new_logmsg);
6010 if (logmsg == NULL) {
6011 err = got_error_from_errno("strdup");
6015 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6020 /* NB: commit_worktree will call free(logmsg) */
6021 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6022 worktree, got_object_commit_get_author(orig_commit),
6023 got_object_commit_get_committer(orig_commit),
6024 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6028 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6032 err = got_ref_delete(commit_ref, repo);
6036 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6038 sync_err = sync_fileindex(fileindex, fileindex_path);
6039 if (sync_err && err == NULL)
6042 free(fileindex_path);
6043 free(head_commit_id);
6045 got_ref_close(head_ref);
6047 free(*new_commit_id);
6048 *new_commit_id = NULL;
6053 const struct got_error *
6054 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6055 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6056 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6057 struct got_commit_object *orig_commit,
6058 struct got_object_id *orig_commit_id, struct got_repository *repo)
6060 const struct got_error *err;
6061 char *commit_ref_name;
6062 struct got_reference *commit_ref = NULL;
6063 struct got_object_id *commit_id = NULL;
6065 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6069 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6072 err = got_ref_resolve(&commit_id, repo, commit_ref);
6075 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6076 err = got_error(GOT_ERR_REBASE_COMMITID);
6080 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6081 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6084 got_ref_close(commit_ref);
6085 free(commit_ref_name);
6090 const struct got_error *
6091 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6092 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6093 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6094 struct got_commit_object *orig_commit,
6095 struct got_object_id *orig_commit_id, const char *new_logmsg,
6096 struct got_repository *repo)
6098 const struct got_error *err;
6099 char *commit_ref_name;
6100 struct got_reference *commit_ref = NULL;
6102 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6106 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6110 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6111 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6114 got_ref_close(commit_ref);
6115 free(commit_ref_name);
6119 const struct got_error *
6120 got_worktree_rebase_postpone(struct got_worktree *worktree,
6121 struct got_fileindex *fileindex)
6124 got_fileindex_free(fileindex);
6125 return lock_worktree(worktree, LOCK_SH);
6128 static const struct got_error *
6129 delete_ref(const char *name, struct got_repository *repo)
6131 const struct got_error *err;
6132 struct got_reference *ref;
6134 err = got_ref_open(&ref, repo, name, 0);
6136 if (err->code == GOT_ERR_NOT_REF)
6141 err = got_ref_delete(ref, repo);
6146 static const struct got_error *
6147 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6149 const struct got_error *err;
6150 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6151 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6153 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6156 err = delete_ref(tmp_branch_name, repo);
6160 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6163 err = delete_ref(new_base_branch_ref_name, repo);
6167 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6170 err = delete_ref(branch_ref_name, repo);
6174 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6177 err = delete_ref(commit_ref_name, repo);
6182 free(tmp_branch_name);
6183 free(new_base_branch_ref_name);
6184 free(branch_ref_name);
6185 free(commit_ref_name);
6189 const struct got_error *
6190 got_worktree_rebase_complete(struct got_worktree *worktree,
6191 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6192 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6193 struct got_repository *repo)
6195 const struct got_error *err, *unlockerr;
6196 struct got_object_id *new_head_commit_id = NULL;
6198 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6202 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6206 err = got_ref_write(rebased_branch, repo);
6210 err = got_worktree_set_head_ref(worktree, rebased_branch);
6214 err = delete_rebase_refs(worktree, repo);
6217 got_fileindex_free(fileindex);
6218 free(new_head_commit_id);
6219 unlockerr = lock_worktree(worktree, LOCK_SH);
6220 if (unlockerr && err == NULL)
6225 const struct got_error *
6226 got_worktree_rebase_abort(struct got_worktree *worktree,
6227 struct got_fileindex *fileindex, struct got_repository *repo,
6228 struct got_reference *new_base_branch,
6229 got_worktree_checkout_cb progress_cb, void *progress_arg)
6231 const struct got_error *err, *unlockerr, *sync_err;
6232 struct got_reference *resolved = NULL;
6233 struct got_object_id *commit_id = NULL;
6234 char *fileindex_path = NULL;
6235 struct revert_file_args rfa;
6236 struct got_object_id *tree_id = NULL;
6238 err = lock_worktree(worktree, LOCK_EX);
6242 err = got_ref_open(&resolved, repo,
6243 got_ref_get_symref_target(new_base_branch), 0);
6247 err = got_worktree_set_head_ref(worktree, resolved);
6252 * XXX commits to the base branch could have happened while
6253 * we were busy rebasing; should we store the original commit ID
6254 * when rebase begins and read it back here?
6256 err = got_ref_resolve(&commit_id, repo, resolved);
6260 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6264 err = got_object_id_by_path(&tree_id, repo,
6265 worktree->base_commit_id, worktree->path_prefix);
6269 err = delete_rebase_refs(worktree, repo);
6273 err = get_fileindex_path(&fileindex_path, worktree);
6277 rfa.worktree = worktree;
6278 rfa.fileindex = fileindex;
6279 rfa.progress_cb = progress_cb;
6280 rfa.progress_arg = progress_arg;
6281 rfa.patch_cb = NULL;
6282 rfa.patch_arg = NULL;
6284 err = worktree_status(worktree, "", fileindex, repo,
6285 revert_file, &rfa, NULL, NULL, 0, 0);
6289 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6290 repo, progress_cb, progress_arg, NULL, NULL);
6292 sync_err = sync_fileindex(fileindex, fileindex_path);
6293 if (sync_err && err == NULL)
6296 got_ref_close(resolved);
6300 got_fileindex_free(fileindex);
6301 free(fileindex_path);
6303 unlockerr = lock_worktree(worktree, LOCK_SH);
6304 if (unlockerr && err == NULL)
6309 const struct got_error *
6310 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6311 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6312 struct got_fileindex **fileindex, struct got_worktree *worktree,
6313 struct got_repository *repo)
6315 const struct got_error *err = NULL;
6316 char *tmp_branch_name = NULL;
6317 char *branch_ref_name = NULL;
6318 char *base_commit_ref_name = NULL;
6319 char *fileindex_path = NULL;
6320 struct check_rebase_ok_arg ok_arg;
6321 struct got_reference *wt_branch = NULL;
6322 struct got_reference *base_commit_ref = NULL;
6326 *base_commit_id = NULL;
6329 err = lock_worktree(worktree, LOCK_EX);
6333 err = open_fileindex(fileindex, &fileindex_path, worktree);
6337 ok_arg.worktree = worktree;
6339 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6344 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6348 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6352 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6357 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6362 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6366 err = got_ref_write(*branch_ref, repo);
6370 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6371 worktree->base_commit_id);
6374 err = got_ref_write(base_commit_ref, repo);
6377 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6378 if (*base_commit_id == NULL) {
6379 err = got_error_from_errno("got_object_id_dup");
6383 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6384 worktree->base_commit_id);
6387 err = got_ref_write(*tmp_branch, repo);
6391 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6395 free(fileindex_path);
6396 free(tmp_branch_name);
6397 free(branch_ref_name);
6398 free(base_commit_ref_name);
6400 got_ref_close(wt_branch);
6403 got_ref_close(*branch_ref);
6407 got_ref_close(*tmp_branch);
6410 free(*base_commit_id);
6412 got_fileindex_free(*fileindex);
6415 lock_worktree(worktree, LOCK_SH);
6420 const struct got_error *
6421 got_worktree_histedit_postpone(struct got_worktree *worktree,
6422 struct got_fileindex *fileindex)
6425 got_fileindex_free(fileindex);
6426 return lock_worktree(worktree, LOCK_SH);
6429 const struct got_error *
6430 got_worktree_histedit_in_progress(int *in_progress,
6431 struct got_worktree *worktree)
6433 const struct got_error *err;
6434 char *tmp_branch_name = NULL;
6436 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6440 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6441 free(tmp_branch_name);
6445 const struct got_error *
6446 got_worktree_histedit_continue(struct got_object_id **commit_id,
6447 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6448 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6449 struct got_worktree *worktree, struct got_repository *repo)
6451 const struct got_error *err;
6452 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6453 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6454 struct got_reference *commit_ref = NULL;
6455 struct got_reference *base_commit_ref = NULL;
6456 char *fileindex_path = NULL;
6457 int have_staged_files = 0;
6461 *base_commit_id = NULL;
6464 err = lock_worktree(worktree, LOCK_EX);
6468 err = open_fileindex(fileindex, &fileindex_path, worktree);
6472 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6473 &have_staged_files);
6474 if (err && err->code != GOT_ERR_CANCELLED)
6476 if (have_staged_files) {
6477 err = got_error(GOT_ERR_STAGED_PATHS);
6481 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6485 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6489 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6493 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6498 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6502 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6505 err = got_ref_resolve(commit_id, repo, commit_ref);
6509 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6512 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6516 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6520 free(commit_ref_name);
6521 free(branch_ref_name);
6522 free(fileindex_path);
6524 got_ref_close(commit_ref);
6525 if (base_commit_ref)
6526 got_ref_close(base_commit_ref);
6530 free(*base_commit_id);
6531 *base_commit_id = NULL;
6533 got_ref_close(*tmp_branch);
6537 got_fileindex_free(*fileindex);
6540 lock_worktree(worktree, LOCK_EX);
6545 static const struct got_error *
6546 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6548 const struct got_error *err;
6549 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6550 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6552 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6555 err = delete_ref(tmp_branch_name, repo);
6559 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6563 err = delete_ref(base_commit_ref_name, repo);
6567 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6570 err = delete_ref(branch_ref_name, repo);
6574 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6577 err = delete_ref(commit_ref_name, repo);
6581 free(tmp_branch_name);
6582 free(base_commit_ref_name);
6583 free(branch_ref_name);
6584 free(commit_ref_name);
6588 const struct got_error *
6589 got_worktree_histedit_abort(struct got_worktree *worktree,
6590 struct got_fileindex *fileindex, struct got_repository *repo,
6591 struct got_reference *branch, struct got_object_id *base_commit_id,
6592 got_worktree_checkout_cb progress_cb, void *progress_arg)
6594 const struct got_error *err, *unlockerr, *sync_err;
6595 struct got_reference *resolved = NULL;
6596 char *fileindex_path = NULL;
6597 struct got_object_id *tree_id = NULL;
6598 struct revert_file_args rfa;
6600 err = lock_worktree(worktree, LOCK_EX);
6604 err = got_ref_open(&resolved, repo,
6605 got_ref_get_symref_target(branch), 0);
6609 err = got_worktree_set_head_ref(worktree, resolved);
6613 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6617 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6618 worktree->path_prefix);
6622 err = delete_histedit_refs(worktree, repo);
6626 err = get_fileindex_path(&fileindex_path, worktree);
6630 rfa.worktree = worktree;
6631 rfa.fileindex = fileindex;
6632 rfa.progress_cb = progress_cb;
6633 rfa.progress_arg = progress_arg;
6634 rfa.patch_cb = NULL;
6635 rfa.patch_arg = NULL;
6637 err = worktree_status(worktree, "", fileindex, repo,
6638 revert_file, &rfa, NULL, NULL, 0, 0);
6642 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6643 repo, progress_cb, progress_arg, NULL, NULL);
6645 sync_err = sync_fileindex(fileindex, fileindex_path);
6646 if (sync_err && err == NULL)
6649 got_ref_close(resolved);
6651 free(fileindex_path);
6653 unlockerr = lock_worktree(worktree, LOCK_SH);
6654 if (unlockerr && err == NULL)
6659 const struct got_error *
6660 got_worktree_histedit_complete(struct got_worktree *worktree,
6661 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6662 struct got_reference *edited_branch, struct got_repository *repo)
6664 const struct got_error *err, *unlockerr;
6665 struct got_object_id *new_head_commit_id = NULL;
6666 struct got_reference *resolved = NULL;
6668 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6672 err = got_ref_open(&resolved, repo,
6673 got_ref_get_symref_target(edited_branch), 0);
6677 err = got_ref_change_ref(resolved, new_head_commit_id);
6681 err = got_ref_write(resolved, repo);
6685 err = got_worktree_set_head_ref(worktree, resolved);
6689 err = delete_histedit_refs(worktree, repo);
6692 got_fileindex_free(fileindex);
6693 free(new_head_commit_id);
6694 unlockerr = lock_worktree(worktree, LOCK_SH);
6695 if (unlockerr && err == NULL)
6700 const struct got_error *
6701 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6702 struct got_object_id *commit_id, struct got_repository *repo)
6704 const struct got_error *err;
6705 char *commit_ref_name;
6707 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6711 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6715 err = delete_ref(commit_ref_name, repo);
6717 free(commit_ref_name);
6721 const struct got_error *
6722 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6723 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6724 struct got_worktree *worktree, const char *refname,
6725 struct got_repository *repo)
6727 const struct got_error *err = NULL;
6728 char *fileindex_path = NULL;
6729 struct check_rebase_ok_arg ok_arg;
6733 *base_branch_ref = NULL;
6735 err = lock_worktree(worktree, LOCK_EX);
6739 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6740 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6741 "cannot integrate a branch into itself; "
6742 "update -b or different branch name required");
6746 err = open_fileindex(fileindex, &fileindex_path, worktree);
6750 /* Preconditions are the same as for rebase. */
6751 ok_arg.worktree = worktree;
6753 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6758 err = got_ref_open(branch_ref, repo, refname, 1);
6762 err = got_ref_open(base_branch_ref, repo,
6763 got_worktree_get_head_ref_name(worktree), 1);
6767 got_ref_close(*branch_ref);
6770 if (*base_branch_ref) {
6771 got_ref_close(*base_branch_ref);
6772 *base_branch_ref = NULL;
6775 got_fileindex_free(*fileindex);
6778 lock_worktree(worktree, LOCK_SH);
6783 const struct got_error *
6784 got_worktree_integrate_continue(struct got_worktree *worktree,
6785 struct got_fileindex *fileindex, struct got_repository *repo,
6786 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6787 got_worktree_checkout_cb progress_cb, void *progress_arg,
6788 got_cancel_cb cancel_cb, void *cancel_arg)
6790 const struct got_error *err = NULL, *sync_err, *unlockerr;
6791 char *fileindex_path = NULL;
6792 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6794 err = get_fileindex_path(&fileindex_path, worktree);
6798 err = got_ref_resolve(&commit_id, repo, branch_ref);
6802 err = got_object_id_by_path(&tree_id, repo, commit_id,
6803 worktree->path_prefix);
6807 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6811 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6812 progress_cb, progress_arg, cancel_cb, cancel_arg);
6816 err = got_ref_change_ref(base_branch_ref, commit_id);
6820 err = got_ref_write(base_branch_ref, repo);
6822 sync_err = sync_fileindex(fileindex, fileindex_path);
6823 if (sync_err && err == NULL)
6827 unlockerr = got_ref_unlock(branch_ref);
6828 if (unlockerr && err == NULL)
6830 got_ref_close(branch_ref);
6832 unlockerr = got_ref_unlock(base_branch_ref);
6833 if (unlockerr && err == NULL)
6835 got_ref_close(base_branch_ref);
6837 got_fileindex_free(fileindex);
6838 free(fileindex_path);
6841 unlockerr = lock_worktree(worktree, LOCK_SH);
6842 if (unlockerr && err == NULL)
6847 const struct got_error *
6848 got_worktree_integrate_abort(struct got_worktree *worktree,
6849 struct got_fileindex *fileindex, struct got_repository *repo,
6850 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6852 const struct got_error *err = NULL, *unlockerr = NULL;
6854 got_fileindex_free(fileindex);
6856 err = lock_worktree(worktree, LOCK_SH);
6858 unlockerr = got_ref_unlock(branch_ref);
6859 if (unlockerr && err == NULL)
6861 got_ref_close(branch_ref);
6863 unlockerr = got_ref_unlock(base_branch_ref);
6864 if (unlockerr && err == NULL)
6866 got_ref_close(base_branch_ref);
6871 struct check_stage_ok_arg {
6872 struct got_object_id *head_commit_id;
6873 struct got_worktree *worktree;
6874 struct got_fileindex *fileindex;
6875 struct got_repository *repo;
6879 const struct got_error *
6880 check_stage_ok(void *arg, unsigned char status,
6881 unsigned char staged_status, const char *relpath,
6882 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6883 struct got_object_id *commit_id, int dirfd, const char *de_name)
6885 struct check_stage_ok_arg *a = arg;
6886 const struct got_error *err = NULL;
6887 struct got_fileindex_entry *ie;
6888 struct got_object_id base_commit_id;
6889 struct got_object_id *base_commit_idp = NULL;
6890 char *in_repo_path = NULL, *p;
6892 if (status == GOT_STATUS_UNVERSIONED ||
6893 status == GOT_STATUS_NO_CHANGE)
6895 if (status == GOT_STATUS_NONEXISTENT)
6896 return got_error_set_errno(ENOENT, relpath);
6898 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6900 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6902 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6903 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6905 return got_error_from_errno("asprintf");
6907 if (got_fileindex_entry_has_commit(ie)) {
6908 memcpy(base_commit_id.sha1, ie->commit_sha1,
6909 SHA1_DIGEST_LENGTH);
6910 base_commit_idp = &base_commit_id;
6913 if (status == GOT_STATUS_CONFLICT) {
6914 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6916 } else if (status != GOT_STATUS_ADD &&
6917 status != GOT_STATUS_MODIFY &&
6918 status != GOT_STATUS_DELETE) {
6919 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6923 a->have_changes = 1;
6928 err = check_out_of_date(p, status, staged_status,
6929 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6930 GOT_ERR_STAGE_OUT_OF_DATE);
6936 struct stage_path_arg {
6937 struct got_worktree *worktree;
6938 struct got_fileindex *fileindex;
6939 struct got_repository *repo;
6940 got_worktree_status_cb status_cb;
6942 got_worktree_patch_cb patch_cb;
6944 int staged_something;
6947 static const struct got_error *
6948 stage_path(void *arg, unsigned char status,
6949 unsigned char staged_status, const char *relpath,
6950 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6951 struct got_object_id *commit_id, int dirfd, const char *de_name)
6953 struct stage_path_arg *a = arg;
6954 const struct got_error *err = NULL;
6955 struct got_fileindex_entry *ie;
6956 char *ondisk_path = NULL, *path_content = NULL;
6958 struct got_object_id *new_staged_blob_id = NULL;
6960 if (status == GOT_STATUS_UNVERSIONED)
6963 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6965 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6967 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6969 return got_error_from_errno("asprintf");
6972 case GOT_STATUS_ADD:
6973 case GOT_STATUS_MODIFY:
6975 if (status == GOT_STATUS_ADD) {
6976 int choice = GOT_PATCH_CHOICE_NONE;
6977 err = (*a->patch_cb)(&choice, a->patch_arg,
6978 status, ie->path, NULL, 1, 1);
6981 if (choice != GOT_PATCH_CHOICE_YES)
6984 err = create_patched_content(&path_content, 0,
6985 staged_blob_id ? staged_blob_id : blob_id,
6986 ondisk_path, dirfd, de_name, ie->path,
6987 a->repo, a->patch_cb, a->patch_arg);
6988 if (err || path_content == NULL)
6992 err = got_object_blob_create(&new_staged_blob_id,
6993 path_content ? path_content : ondisk_path, a->repo);
6996 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6997 SHA1_DIGEST_LENGTH);
6998 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6999 stage = GOT_FILEIDX_STAGE_ADD;
7001 stage = GOT_FILEIDX_STAGE_MODIFY;
7002 got_fileindex_entry_stage_set(ie, stage);
7003 a->staged_something = 1;
7004 if (a->status_cb == NULL)
7006 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7007 get_staged_status(ie), relpath, blob_id,
7008 new_staged_blob_id, NULL, dirfd, de_name);
7010 case GOT_STATUS_DELETE:
7011 if (staged_status == GOT_STATUS_DELETE)
7014 int choice = GOT_PATCH_CHOICE_NONE;
7015 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7016 ie->path, NULL, 1, 1);
7019 if (choice == GOT_PATCH_CHOICE_NO)
7021 if (choice != GOT_PATCH_CHOICE_YES) {
7022 err = got_error(GOT_ERR_PATCH_CHOICE);
7026 stage = GOT_FILEIDX_STAGE_DELETE;
7027 got_fileindex_entry_stage_set(ie, stage);
7028 a->staged_something = 1;
7029 if (a->status_cb == NULL)
7031 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7032 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7035 case GOT_STATUS_NO_CHANGE:
7037 case GOT_STATUS_CONFLICT:
7038 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7040 case GOT_STATUS_NONEXISTENT:
7041 err = got_error_set_errno(ENOENT, relpath);
7044 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7048 if (path_content && unlink(path_content) == -1 && err == NULL)
7049 err = got_error_from_errno2("unlink", path_content);
7052 free(new_staged_blob_id);
7056 const struct got_error *
7057 got_worktree_stage(struct got_worktree *worktree,
7058 struct got_pathlist_head *paths,
7059 got_worktree_status_cb status_cb, void *status_arg,
7060 got_worktree_patch_cb patch_cb, void *patch_arg,
7061 struct got_repository *repo)
7063 const struct got_error *err = NULL, *sync_err, *unlockerr;
7064 struct got_pathlist_entry *pe;
7065 struct got_fileindex *fileindex = NULL;
7066 char *fileindex_path = NULL;
7067 struct got_reference *head_ref = NULL;
7068 struct got_object_id *head_commit_id = NULL;
7069 struct check_stage_ok_arg oka;
7070 struct stage_path_arg spa;
7072 err = lock_worktree(worktree, LOCK_EX);
7076 err = got_ref_open(&head_ref, repo,
7077 got_worktree_get_head_ref_name(worktree), 0);
7080 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7083 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7087 /* Check pre-conditions before staging anything. */
7088 oka.head_commit_id = head_commit_id;
7089 oka.worktree = worktree;
7090 oka.fileindex = fileindex;
7092 oka.have_changes = 0;
7093 TAILQ_FOREACH(pe, paths, entry) {
7094 err = worktree_status(worktree, pe->path, fileindex, repo,
7095 check_stage_ok, &oka, NULL, NULL, 0, 0);
7099 if (!oka.have_changes) {
7100 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7104 spa.worktree = worktree;
7105 spa.fileindex = fileindex;
7107 spa.patch_cb = patch_cb;
7108 spa.patch_arg = patch_arg;
7109 spa.status_cb = status_cb;
7110 spa.status_arg = status_arg;
7111 spa.staged_something = 0;
7112 TAILQ_FOREACH(pe, paths, entry) {
7113 err = worktree_status(worktree, pe->path, fileindex, repo,
7114 stage_path, &spa, NULL, NULL, 0, 0);
7118 if (!spa.staged_something) {
7119 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7123 sync_err = sync_fileindex(fileindex, fileindex_path);
7124 if (sync_err && err == NULL)
7128 got_ref_close(head_ref);
7129 free(head_commit_id);
7130 free(fileindex_path);
7132 got_fileindex_free(fileindex);
7133 unlockerr = lock_worktree(worktree, LOCK_SH);
7134 if (unlockerr && err == NULL)
7139 struct unstage_path_arg {
7140 struct got_worktree *worktree;
7141 struct got_fileindex *fileindex;
7142 struct got_repository *repo;
7143 got_worktree_checkout_cb progress_cb;
7145 got_worktree_patch_cb patch_cb;
7149 static const struct got_error *
7150 create_unstaged_content(char **path_unstaged_content,
7151 char **path_new_staged_content, struct got_object_id *blob_id,
7152 struct got_object_id *staged_blob_id, const char *relpath,
7153 struct got_repository *repo,
7154 got_worktree_patch_cb patch_cb, void *patch_arg)
7156 const struct got_error *err;
7157 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7158 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7159 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7160 struct stat sb1, sb2;
7161 struct got_diff_changes *changes = NULL;
7162 struct got_diff_state *ds = NULL;
7163 struct got_diff_args *args = NULL;
7164 struct got_diff_change *change;
7165 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7166 int have_content = 0, have_rejected_content = 0;
7168 *path_unstaged_content = NULL;
7169 *path_new_staged_content = NULL;
7171 err = got_object_id_str(&label1, blob_id);
7174 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7178 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7182 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7186 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7190 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7194 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7198 if (stat(path1, &sb1) == -1) {
7199 err = got_error_from_errno2("stat", path1);
7203 if (stat(path2, &sb2) == -1) {
7204 err = got_error_from_errno2("stat", path2);
7208 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7209 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7213 err = got_opentemp_named(path_unstaged_content, &outfile,
7214 "got-unstaged-content");
7217 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7218 "got-new-staged-content");
7222 if (fseek(f1, 0L, SEEK_SET) == -1) {
7223 err = got_ferror(f1, GOT_ERR_IO);
7226 if (fseek(f2, 0L, SEEK_SET) == -1) {
7227 err = got_ferror(f2, GOT_ERR_IO);
7230 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7232 err = apply_or_reject_change(&choice, change, ++n,
7233 changes->nchanges, ds, args, diff_flags, relpath,
7234 f1, f2, &line_cur1, &line_cur2,
7235 outfile, rejectfile, patch_cb, patch_arg);
7238 if (choice == GOT_PATCH_CHOICE_YES)
7241 have_rejected_content = 1;
7242 if (choice == GOT_PATCH_CHOICE_QUIT)
7245 if (have_content || have_rejected_content)
7246 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7247 outfile, rejectfile);
7251 got_object_blob_close(blob);
7253 got_object_blob_close(staged_blob);
7254 if (f1 && fclose(f1) == EOF && err == NULL)
7255 err = got_error_from_errno2("fclose", path1);
7256 if (f2 && fclose(f2) == EOF && err == NULL)
7257 err = got_error_from_errno2("fclose", path2);
7258 if (outfile && fclose(outfile) == EOF && err == NULL)
7259 err = got_error_from_errno2("fclose", *path_unstaged_content);
7260 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7261 err = got_error_from_errno2("fclose", *path_new_staged_content);
7262 if (path1 && unlink(path1) == -1 && err == NULL)
7263 err = got_error_from_errno2("unlink", path1);
7264 if (path2 && unlink(path2) == -1 && err == NULL)
7265 err = got_error_from_errno2("unlink", path2);
7266 if (err || !have_content) {
7267 if (*path_unstaged_content &&
7268 unlink(*path_unstaged_content) == -1 && err == NULL)
7269 err = got_error_from_errno2("unlink",
7270 *path_unstaged_content);
7271 free(*path_unstaged_content);
7272 *path_unstaged_content = NULL;
7274 if (err || !have_rejected_content) {
7275 if (*path_new_staged_content &&
7276 unlink(*path_new_staged_content) == -1 && err == NULL)
7277 err = got_error_from_errno2("unlink",
7278 *path_new_staged_content);
7279 free(*path_new_staged_content);
7280 *path_new_staged_content = NULL;
7284 got_diff_state_free(ds);
7288 got_diff_free_changes(changes);
7294 static const struct got_error *
7295 unstage_path(void *arg, unsigned char status,
7296 unsigned char staged_status, const char *relpath,
7297 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7298 struct got_object_id *commit_id, int dirfd, const char *de_name)
7300 const struct got_error *err = NULL;
7301 struct unstage_path_arg *a = arg;
7302 struct got_fileindex_entry *ie;
7303 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7304 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7305 char *path_new_staged_content = NULL;
7306 char *id_str = NULL, *label_orig = NULL;
7307 int local_changes_subsumed;
7310 if (staged_status != GOT_STATUS_ADD &&
7311 staged_status != GOT_STATUS_MODIFY &&
7312 staged_status != GOT_STATUS_DELETE)
7315 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7317 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7319 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7321 return got_error_from_errno("asprintf");
7323 err = got_object_id_str(&id_str,
7324 commit_id ? commit_id : a->worktree->base_commit_id);
7327 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7329 err = got_error_from_errno("asprintf");
7333 switch (staged_status) {
7334 case GOT_STATUS_MODIFY:
7335 err = got_object_open_as_blob(&blob_base, a->repo,
7340 case GOT_STATUS_ADD:
7342 if (staged_status == GOT_STATUS_ADD) {
7343 int choice = GOT_PATCH_CHOICE_NONE;
7344 err = (*a->patch_cb)(&choice, a->patch_arg,
7345 staged_status, ie->path, NULL, 1, 1);
7348 if (choice != GOT_PATCH_CHOICE_YES)
7351 err = create_unstaged_content(
7352 &path_unstaged_content,
7353 &path_new_staged_content, blob_id,
7354 staged_blob_id, ie->path, a->repo,
7355 a->patch_cb, a->patch_arg);
7356 if (err || path_unstaged_content == NULL)
7358 if (path_new_staged_content) {
7359 err = got_object_blob_create(
7361 path_new_staged_content,
7365 memcpy(ie->staged_blob_sha1,
7366 staged_blob_id->sha1,
7367 SHA1_DIGEST_LENGTH);
7369 err = merge_file(&local_changes_subsumed,
7370 a->worktree, blob_base, ondisk_path,
7371 relpath, got_fileindex_perms_to_st(ie),
7372 path_unstaged_content, label_orig,
7373 "unstaged", a->repo, a->progress_cb,
7376 path_new_staged_content == NULL)
7377 got_fileindex_entry_stage_set(ie,
7378 GOT_FILEIDX_STAGE_NONE);
7379 break; /* Done with this file. */
7382 err = got_object_open_as_blob(&blob_staged, a->repo,
7383 staged_blob_id, 8192);
7386 err = merge_blob(&local_changes_subsumed, a->worktree,
7387 blob_base, ondisk_path, relpath,
7388 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7389 commit_id ? commit_id : a->worktree->base_commit_id,
7390 a->repo, a->progress_cb, a->progress_arg);
7392 got_fileindex_entry_stage_set(ie,
7393 GOT_FILEIDX_STAGE_NONE);
7395 case GOT_STATUS_DELETE:
7397 int choice = GOT_PATCH_CHOICE_NONE;
7398 err = (*a->patch_cb)(&choice, a->patch_arg,
7399 staged_status, ie->path, NULL, 1, 1);
7402 if (choice == GOT_PATCH_CHOICE_NO)
7404 if (choice != GOT_PATCH_CHOICE_YES) {
7405 err = got_error(GOT_ERR_PATCH_CHOICE);
7409 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7410 err = get_file_status(&status, &sb, ie, ondisk_path,
7411 dirfd, de_name, a->repo);
7414 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7419 if (path_unstaged_content &&
7420 unlink(path_unstaged_content) == -1 && err == NULL)
7421 err = got_error_from_errno2("unlink", path_unstaged_content);
7422 if (path_new_staged_content &&
7423 unlink(path_new_staged_content) == -1 && err == NULL)
7424 err = got_error_from_errno2("unlink", path_new_staged_content);
7425 free(path_unstaged_content);
7426 free(path_new_staged_content);
7428 got_object_blob_close(blob_base);
7430 got_object_blob_close(blob_staged);
7436 const struct got_error *
7437 got_worktree_unstage(struct got_worktree *worktree,
7438 struct got_pathlist_head *paths,
7439 got_worktree_checkout_cb progress_cb, void *progress_arg,
7440 got_worktree_patch_cb patch_cb, void *patch_arg,
7441 struct got_repository *repo)
7443 const struct got_error *err = NULL, *sync_err, *unlockerr;
7444 struct got_pathlist_entry *pe;
7445 struct got_fileindex *fileindex = NULL;
7446 char *fileindex_path = NULL;
7447 struct unstage_path_arg upa;
7449 err = lock_worktree(worktree, LOCK_EX);
7453 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7457 upa.worktree = worktree;
7458 upa.fileindex = fileindex;
7460 upa.progress_cb = progress_cb;
7461 upa.progress_arg = progress_arg;
7462 upa.patch_cb = patch_cb;
7463 upa.patch_arg = patch_arg;
7464 TAILQ_FOREACH(pe, paths, entry) {
7465 err = worktree_status(worktree, pe->path, fileindex, repo,
7466 unstage_path, &upa, NULL, NULL, 0, 0);
7471 sync_err = sync_fileindex(fileindex, fileindex_path);
7472 if (sync_err && err == NULL)
7475 free(fileindex_path);
7477 got_fileindex_free(fileindex);
7478 unlockerr = lock_worktree(worktree, LOCK_SH);
7479 if (unlockerr && err == NULL)