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 *fileindex,
1109 struct got_object_id *base_commit_id, const char *ondisk_path,
1110 const char *path, struct got_object_id *blob_id)
1112 const struct got_error *err = NULL;
1113 struct got_fileindex_entry *new_ie;
1115 err = got_fileindex_entry_alloc(&new_ie, path);
1119 err = got_fileindex_entry_update(new_ie, ondisk_path,
1120 blob_id->sha1, base_commit_id->sha1, 1);
1124 err = got_fileindex_entry_add(fileindex, new_ie);
1127 got_fileindex_entry_free(new_ie);
1132 get_ondisk_perms(int executable, mode_t st_mode)
1134 mode_t xbits = S_IXUSR;
1137 /* Map read bits to execute bits. */
1138 if (st_mode & S_IRGRP)
1140 if (st_mode & S_IROTH)
1142 return st_mode | xbits;
1145 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1148 /* forward declaration */
1149 static const struct got_error *
1150 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1151 const char *path, mode_t te_mode, mode_t st_mode,
1152 struct got_blob_object *blob, int restoring_missing_file,
1153 int reverting_versioned_file, int installing_bad_symlink,
1154 struct got_repository *repo,
1155 got_worktree_checkout_cb progress_cb, void *progress_arg);
1158 * This function assumes that the provided symlink target points at a
1159 * safe location in the work tree!
1161 static const struct got_error *
1162 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1165 const struct got_error *err = NULL;
1167 char etarget[PATH_MAX];
1171 * "Bad" symlinks (those pointing outside the work tree or into the
1172 * .got directory) are installed in the work tree as a regular file
1173 * which contains the bad symlink target path.
1174 * The new symlink target has already been checked for safety by our
1175 * caller. If we can successfully open a regular file then we simply
1176 * replace this file with a symlink below.
1178 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1181 return got_error_from_errno2("open", ondisk_path);
1183 /* We are updating an existing on-disk symlink. */
1184 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1186 return got_error_from_errno2("readlink", ondisk_path);
1188 if (elen == target_len &&
1189 memcmp(etarget, target_path, target_len) == 0)
1190 return NULL; /* nothing to do */
1193 err = update_symlink(ondisk_path, target_path, target_len);
1194 if (fd != -1 && close(fd) == -1 && err == NULL)
1195 err = got_error_from_errno2("close", ondisk_path);
1199 static const struct got_error *
1200 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1201 const char *path, mode_t te_mode, mode_t st_mode,
1202 struct got_blob_object *blob, int restoring_missing_file,
1203 int reverting_versioned_file, struct got_repository *repo,
1204 got_worktree_checkout_cb progress_cb, void *progress_arg)
1206 const struct got_error *err = NULL;
1207 char target_path[PATH_MAX];
1208 size_t len, target_len = 0;
1209 char *resolved_path = NULL, *abspath = NULL;
1210 char *path_got = NULL;
1211 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1212 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1215 * Blob object content specifies the target path of the link.
1216 * If a symbolic link cannot be installed we instead create
1217 * a regular file which contains the link target path stored
1218 * in the blob object.
1221 err = got_object_blob_read_block(&len, blob);
1222 if (len + target_len >= sizeof(target_path)) {
1223 /* Path too long; install as a regular file. */
1224 got_object_blob_rewind(blob);
1225 return install_blob(worktree, ondisk_path, path,
1226 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1227 restoring_missing_file, reverting_versioned_file,
1228 1, repo, progress_cb, progress_arg);
1231 /* Skip blob object header first time around. */
1232 memcpy(target_path + target_len, buf + hdrlen,
1234 target_len += len - hdrlen;
1238 target_path[target_len] = '\0';
1241 * Relative symlink target lookup should begin at the directory
1242 * in which the blob object is being installed.
1244 if (!got_path_is_absolute(target_path)) {
1245 char *parent = dirname(ondisk_path);
1246 if (parent == NULL) {
1247 err = got_error_from_errno2("dirname", ondisk_path);
1250 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1251 err = got_error_from_errno("asprintf");
1257 * unveil(2) restricts our view of paths in the filesystem.
1258 * ENOENT will occur if a link target path does not exist or
1259 * if it points outside our unveiled path space.
1261 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1262 if (resolved_path == NULL) {
1263 if (errno != ENOENT) {
1264 err = got_error_from_errno2("realpath", target_path);
1269 /* Only allow symlinks pointing at paths within the work tree. */
1270 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1271 abspath : target_path), worktree->root_path,
1272 strlen(worktree->root_path))) {
1273 /* install as a regular file */
1274 got_object_blob_rewind(blob);
1275 err = install_blob(worktree, ondisk_path, path,
1276 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1277 restoring_missing_file, reverting_versioned_file, 1,
1278 repo, progress_cb, progress_arg);
1282 /* Do not allow symlinks pointing into the .got directory. */
1283 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1284 GOT_WORKTREE_GOT_DIR) == -1) {
1285 err = got_error_from_errno("asprintf");
1288 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1289 abspath : target_path), path_got, strlen(path_got))) {
1290 /* install as a regular file */
1291 got_object_blob_rewind(blob);
1292 err = install_blob(worktree, ondisk_path, path,
1293 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1294 restoring_missing_file, reverting_versioned_file, 1,
1295 repo, progress_cb, progress_arg);
1299 if (symlink(target_path, ondisk_path) == -1) {
1300 if (errno == EEXIST) {
1301 err = replace_existing_symlink(ondisk_path,
1302 target_path, target_len);
1306 err = (*progress_cb)(progress_arg,
1307 GOT_STATUS_UPDATE, path);
1309 goto done; /* Nothing else to do. */
1312 if (errno == ENOENT) {
1313 char *parent = dirname(ondisk_path);
1314 if (parent == NULL) {
1315 err = got_error_from_errno2("dirname",
1319 err = add_dir_on_disk(worktree, parent);
1323 * Retry, and fall through to error handling
1324 * below if this second attempt fails.
1326 if (symlink(target_path, ondisk_path) != -1) {
1327 err = NULL; /* success */
1332 /* Handle errors from first or second creation attempt. */
1333 if (errno == ENAMETOOLONG) {
1334 /* bad target path; install as a regular file */
1335 got_object_blob_rewind(blob);
1336 err = install_blob(worktree, ondisk_path, path,
1337 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1338 restoring_missing_file, reverting_versioned_file, 1,
1339 repo, progress_cb, progress_arg);
1340 } else if (errno == ENOTDIR) {
1341 err = got_error_path(ondisk_path,
1342 GOT_ERR_FILE_OBSTRUCTED);
1344 err = got_error_from_errno3("symlink",
1345 target_path, ondisk_path);
1347 } else if (progress_cb)
1348 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1350 free(resolved_path);
1356 static const struct got_error *
1357 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1358 const char *path, mode_t te_mode, mode_t st_mode,
1359 struct got_blob_object *blob, int restoring_missing_file,
1360 int reverting_versioned_file, int installing_bad_symlink,
1361 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1364 const struct got_error *err = NULL;
1368 char *tmppath = NULL;
1370 if (S_ISLNK(te_mode))
1371 return install_symlink(worktree, ondisk_path, path, te_mode,
1372 st_mode, blob, restoring_missing_file,
1373 reverting_versioned_file, repo, progress_cb, progress_arg);
1375 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1376 GOT_DEFAULT_FILE_MODE);
1378 if (errno == ENOENT) {
1379 char *parent = dirname(path);
1381 return got_error_from_errno2("dirname", path);
1382 err = add_dir_on_disk(worktree, parent);
1385 fd = open(ondisk_path,
1386 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1387 GOT_DEFAULT_FILE_MODE);
1389 return got_error_from_errno2("open",
1391 } else if (errno == EEXIST) {
1392 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1393 /* TODO file is obstructed; do something */
1394 err = got_error_path(ondisk_path,
1395 GOT_ERR_FILE_OBSTRUCTED);
1398 err = got_opentemp_named_fd(&tmppath, &fd,
1405 return got_error_from_errno2("open", ondisk_path);
1409 if (restoring_missing_file)
1410 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1412 else if (reverting_versioned_file)
1413 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1416 err = (*progress_cb)(progress_arg,
1417 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1422 hdrlen = got_object_blob_get_hdrlen(blob);
1424 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1425 err = got_object_blob_read_block(&len, blob);
1429 /* Skip blob object header first time around. */
1430 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1432 err = got_error_from_errno("write");
1434 } else if (outlen != len - hdrlen) {
1435 err = got_error(GOT_ERR_IO);
1442 if (fsync(fd) != 0) {
1443 err = got_error_from_errno("fsync");
1448 if (rename(tmppath, ondisk_path) != 0) {
1449 err = got_error_from_errno3("rename", tmppath,
1456 if (chmod(ondisk_path,
1457 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1458 err = got_error_from_errno2("chmod", ondisk_path);
1463 if (fd != -1 && close(fd) != 0 && err == NULL)
1464 err = got_error_from_errno("close");
1469 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1470 static const struct got_error *
1471 get_modified_file_content_status(unsigned char *status, FILE *f)
1473 const struct got_error *err = NULL;
1474 const char *markers[3] = {
1475 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1476 GOT_DIFF_CONFLICT_MARKER_SEP,
1477 GOT_DIFF_CONFLICT_MARKER_END
1482 const char delim[3] = {'\0', '\0', '\0'};
1484 while (*status == GOT_STATUS_MODIFY) {
1485 line = fparseln(f, &len, NULL, delim, 0);
1489 err = got_ferror(f, GOT_ERR_IO);
1493 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1494 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1496 *status = GOT_STATUS_CONFLICT;
1506 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1508 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1509 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1513 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1515 return !(ie->ctime_sec == sb->st_ctime &&
1516 ie->ctime_nsec == sb->st_ctimensec &&
1517 ie->mtime_sec == sb->st_mtime &&
1518 ie->mtime_nsec == sb->st_mtimensec &&
1519 ie->size == (sb->st_size & 0xffffffff) &&
1520 !xbit_differs(ie, sb->st_mode));
1523 static unsigned char
1524 get_staged_status(struct got_fileindex_entry *ie)
1526 switch (got_fileindex_entry_stage_get(ie)) {
1527 case GOT_FILEIDX_STAGE_ADD:
1528 return GOT_STATUS_ADD;
1529 case GOT_FILEIDX_STAGE_DELETE:
1530 return GOT_STATUS_DELETE;
1531 case GOT_FILEIDX_STAGE_MODIFY:
1532 return GOT_STATUS_MODIFY;
1534 return GOT_STATUS_NO_CHANGE;
1538 static const struct got_error *
1539 get_symlink_status(unsigned char *status, struct stat *sb,
1540 struct got_fileindex_entry *ie, const char *abspath,
1541 int dirfd, const char *de_name, struct got_blob_object *blob)
1543 const struct got_error *err = NULL;
1544 char target_path[PATH_MAX];
1545 char etarget[PATH_MAX];
1547 size_t len, target_len = 0;
1548 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1549 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1551 *status = GOT_STATUS_NO_CHANGE;
1553 /* Blob object content specifies the target path of the link. */
1555 err = got_object_blob_read_block(&len, blob);
1558 if (len + target_len >= sizeof(target_path)) {
1560 * Should not happen. The blob contents were OK
1561 * when this symlink was installed.
1563 return got_error(GOT_ERR_NO_SPACE);
1566 /* Skip blob object header first time around. */
1567 memcpy(target_path + target_len, buf + hdrlen,
1569 target_len += len - hdrlen;
1573 target_path[target_len] = '\0';
1576 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1578 return got_error_from_errno2("readlinkat", abspath);
1580 elen = readlink(abspath, etarget, sizeof(etarget));
1582 return got_error_from_errno2("readlinkat", abspath);
1585 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1586 *status = GOT_STATUS_MODIFY;
1591 static const struct got_error *
1592 get_file_status(unsigned char *status, struct stat *sb,
1593 struct got_fileindex_entry *ie, const char *abspath,
1594 int dirfd, const char *de_name, struct got_repository *repo)
1596 const struct got_error *err = NULL;
1597 struct got_object_id id;
1602 struct got_blob_object *blob = NULL;
1604 unsigned char staged_status = get_staged_status(ie);
1606 *status = GOT_STATUS_NO_CHANGE;
1609 * Whenever the caller provides a directory descriptor and a
1610 * directory entry name for the file, use them! This prevents
1611 * race conditions if filesystem paths change beneath our feet.
1614 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1615 if (errno == ENOENT) {
1616 if (got_fileindex_entry_has_file_on_disk(ie))
1617 *status = GOT_STATUS_MISSING;
1619 *status = GOT_STATUS_DELETE;
1622 err = got_error_from_errno2("fstatat", abspath);
1626 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1627 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1628 return got_error_from_errno2("open", abspath);
1629 else if (fd == -1 && errno == ELOOP) {
1630 if (lstat(abspath, sb) == -1)
1631 return got_error_from_errno2("lstat", abspath);
1632 } else if (fd == -1 || fstat(fd, sb) == -1) {
1633 if (errno == ENOENT) {
1634 if (got_fileindex_entry_has_file_on_disk(ie))
1635 *status = GOT_STATUS_MISSING;
1637 *status = GOT_STATUS_DELETE;
1640 err = got_error_from_errno2("fstat", abspath);
1645 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1646 *status = GOT_STATUS_OBSTRUCTED;
1650 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1651 *status = GOT_STATUS_DELETE;
1653 } else if (!got_fileindex_entry_has_blob(ie) &&
1654 staged_status != GOT_STATUS_ADD) {
1655 *status = GOT_STATUS_ADD;
1659 if (!stat_info_differs(ie, sb))
1662 if (staged_status == GOT_STATUS_MODIFY ||
1663 staged_status == GOT_STATUS_ADD)
1664 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1666 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1668 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1672 if (S_ISLNK(sb->st_mode)) {
1673 /* Staging changes to symlinks is not yet(?) supported. */
1674 if (staged_status != GOT_STATUS_NO_CHANGE) {
1675 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1678 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1685 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1687 err = got_error_from_errno2("openat", abspath);
1692 f = fdopen(fd, "r");
1694 err = got_error_from_errno2("fdopen", abspath);
1698 hdrlen = got_object_blob_get_hdrlen(blob);
1700 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1701 err = got_object_blob_read_block(&blen, blob);
1704 /* Skip length of blob object header first time around. */
1705 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1706 if (flen == 0 && ferror(f)) {
1707 err = got_error_from_errno("fread");
1712 *status = GOT_STATUS_MODIFY;
1714 } else if (flen == 0) {
1716 *status = GOT_STATUS_MODIFY;
1718 } else if (blen - hdrlen == flen) {
1719 /* Skip blob object header first time around. */
1720 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1721 *status = GOT_STATUS_MODIFY;
1725 *status = GOT_STATUS_MODIFY;
1731 if (*status == GOT_STATUS_MODIFY) {
1733 err = get_modified_file_content_status(status, f);
1734 } else if (xbit_differs(ie, sb->st_mode))
1735 *status = GOT_STATUS_MODE_CHANGE;
1738 got_object_blob_close(blob);
1739 if (f != NULL && fclose(f) == EOF && err == NULL)
1740 err = got_error_from_errno2("fclose", abspath);
1741 if (fd != -1 && close(fd) == -1 && err == NULL)
1742 err = got_error_from_errno2("close", abspath);
1747 * Update timestamps in the file index if a file is unmodified and
1748 * we had to run a full content comparison to find out.
1750 static const struct got_error *
1751 sync_timestamps(char *ondisk_path, unsigned char status,
1752 struct got_fileindex_entry *ie, struct stat *sb)
1754 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1755 return got_fileindex_entry_update(ie, ondisk_path,
1756 ie->blob_sha1, ie->commit_sha1, 1);
1761 static const struct got_error *
1762 update_blob(struct got_worktree *worktree,
1763 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1764 struct got_tree_entry *te, const char *path,
1765 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1768 const struct got_error *err = NULL;
1769 struct got_blob_object *blob = NULL;
1771 unsigned char status = GOT_STATUS_NO_CHANGE;
1774 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1775 return got_error_from_errno("asprintf");
1778 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1779 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1782 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1786 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1787 sb.st_mode = got_fileindex_perms_to_st(ie);
1789 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1791 if (status == GOT_STATUS_OBSTRUCTED) {
1792 err = (*progress_cb)(progress_arg, status, path);
1795 if (status == GOT_STATUS_CONFLICT) {
1796 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1801 if (ie && status != GOT_STATUS_MISSING &&
1802 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1803 if (got_fileindex_entry_has_commit(ie) &&
1804 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1805 SHA1_DIGEST_LENGTH) == 0) {
1806 err = sync_timestamps(ondisk_path, status, ie, &sb);
1809 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1813 if (got_fileindex_entry_has_blob(ie) &&
1814 memcmp(ie->blob_sha1, te->id.sha1,
1815 SHA1_DIGEST_LENGTH) == 0) {
1816 err = sync_timestamps(ondisk_path, status, ie, &sb);
1821 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1825 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1826 int update_timestamps;
1827 struct got_blob_object *blob2 = NULL;
1828 char *label_orig = NULL;
1829 if (got_fileindex_entry_has_blob(ie)) {
1830 struct got_object_id id2;
1831 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1832 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1836 if (got_fileindex_entry_has_commit(ie)) {
1837 char id_str[SHA1_DIGEST_STRING_LENGTH];
1838 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1839 sizeof(id_str)) == NULL) {
1840 err = got_error_path(id_str,
1841 GOT_ERR_BAD_OBJ_ID_STR);
1844 if (asprintf(&label_orig, "%s: commit %s",
1845 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1846 err = got_error_from_errno("asprintf");
1850 if (S_ISLNK(te->mode)) {
1851 err = merge_symlink(worktree, blob2,
1852 ondisk_path, path, sb.st_mode, label_orig,
1853 blob, worktree->base_commit_id, repo,
1854 progress_cb, progress_arg);
1856 err = merge_blob(&update_timestamps, worktree, blob2,
1857 ondisk_path, path, sb.st_mode, label_orig, blob,
1858 worktree->base_commit_id, repo,
1859 progress_cb, progress_arg);
1863 got_object_blob_close(blob2);
1867 * Do not update timestamps of files with local changes.
1868 * Otherwise, a future status walk would treat them as
1869 * unmodified files again.
1871 err = got_fileindex_entry_update(ie, ondisk_path,
1872 blob->id.sha1, worktree->base_commit_id->sha1,
1874 } else if (status == GOT_STATUS_MODE_CHANGE) {
1875 err = got_fileindex_entry_update(ie, ondisk_path,
1876 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1877 } else if (status == GOT_STATUS_DELETE) {
1878 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1881 err = got_fileindex_entry_update(ie, ondisk_path,
1882 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1886 err = install_blob(worktree, ondisk_path, path, te->mode,
1887 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1888 repo, progress_cb, progress_arg);
1892 err = got_fileindex_entry_update(ie, ondisk_path,
1893 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1895 err = create_fileindex_entry(fileindex,
1896 worktree->base_commit_id, ondisk_path, path,
1902 got_object_blob_close(blob);
1908 static const struct got_error *
1909 remove_ondisk_file(const char *root_path, const char *path)
1911 const struct got_error *err = NULL;
1912 char *ondisk_path = NULL;
1914 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1915 return got_error_from_errno("asprintf");
1917 if (unlink(ondisk_path) == -1) {
1918 if (errno != ENOENT)
1919 err = got_error_from_errno2("unlink", ondisk_path);
1921 char *parent = dirname(ondisk_path);
1922 while (parent && strcmp(parent, root_path) != 0) {
1923 if (rmdir(parent) == -1) {
1924 if (errno != ENOTEMPTY)
1925 err = got_error_from_errno2("rmdir",
1929 parent = dirname(parent);
1936 static const struct got_error *
1937 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1938 struct got_fileindex_entry *ie, struct got_repository *repo,
1939 got_worktree_checkout_cb progress_cb, void *progress_arg)
1941 const struct got_error *err = NULL;
1942 unsigned char status;
1946 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1947 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1949 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1951 return got_error_from_errno("asprintf");
1953 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1957 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1958 char ondisk_target[PATH_MAX];
1959 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1960 sizeof(ondisk_target));
1961 if (ondisk_len == -1) {
1962 err = got_error_from_errno2("readlink", ondisk_path);
1965 ondisk_target[ondisk_len] = '\0';
1966 err = install_symlink_conflict(NULL, worktree->base_commit_id,
1967 NULL, NULL, /* XXX pass common ancestor info? */
1968 ondisk_target, ondisk_path);
1971 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1976 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1977 status == GOT_STATUS_ADD) {
1978 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1982 * Preserve the working file and change the deleted blob's
1983 * entry into a schedule-add entry.
1985 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1988 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1991 if (status == GOT_STATUS_NO_CHANGE) {
1992 err = remove_ondisk_file(worktree->root_path, ie->path);
1996 got_fileindex_entry_remove(fileindex, ie);
2003 struct diff_cb_arg {
2004 struct got_fileindex *fileindex;
2005 struct got_worktree *worktree;
2006 struct got_repository *repo;
2007 got_worktree_checkout_cb progress_cb;
2009 got_cancel_cb cancel_cb;
2013 static const struct got_error *
2014 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2015 struct got_tree_entry *te, const char *parent_path)
2017 struct diff_cb_arg *a = arg;
2019 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2020 return got_error(GOT_ERR_CANCELLED);
2022 return update_blob(a->worktree, a->fileindex, ie, te,
2023 ie->path, a->repo, a->progress_cb, a->progress_arg);
2026 static const struct got_error *
2027 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2029 struct diff_cb_arg *a = arg;
2031 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2032 return got_error(GOT_ERR_CANCELLED);
2034 return delete_blob(a->worktree, a->fileindex, ie,
2035 a->repo, a->progress_cb, a->progress_arg);
2038 static const struct got_error *
2039 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2041 struct diff_cb_arg *a = arg;
2042 const struct got_error *err;
2045 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2046 return got_error(GOT_ERR_CANCELLED);
2048 if (got_object_tree_entry_is_submodule(te))
2051 if (asprintf(&path, "%s%s%s", parent_path,
2052 parent_path[0] ? "/" : "", te->name)
2054 return got_error_from_errno("asprintf");
2056 if (S_ISDIR(te->mode))
2057 err = add_dir_on_disk(a->worktree, path);
2059 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2060 a->repo, a->progress_cb, a->progress_arg);
2066 static const struct got_error *
2067 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2069 const struct got_error *err = NULL;
2070 char *uuidstr = NULL;
2071 uint32_t uuid_status;
2075 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2076 if (uuid_status != uuid_s_ok)
2077 return got_error_uuid(uuid_status, "uuid_to_string");
2079 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2081 err = got_error_from_errno("asprintf");
2088 const struct got_error *
2089 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2091 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2094 static const struct got_error *
2095 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2097 return get_ref_name(refname, worktree,
2098 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2101 static const struct got_error *
2102 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2104 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2107 static const struct got_error *
2108 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2110 return get_ref_name(refname, worktree,
2111 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2114 static const struct got_error *
2115 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2117 return get_ref_name(refname, worktree,
2118 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2121 static const struct got_error *
2122 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2124 return get_ref_name(refname, worktree,
2125 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2128 static const struct got_error *
2129 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2131 return get_ref_name(refname, worktree,
2132 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2135 static const struct got_error *
2136 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2138 return get_ref_name(refname, worktree,
2139 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2142 static const struct got_error *
2143 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2145 return get_ref_name(refname, worktree,
2146 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2149 const struct got_error *
2150 got_worktree_get_histedit_script_path(char **path,
2151 struct got_worktree *worktree)
2153 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2154 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2156 return got_error_from_errno("asprintf");
2162 * Prevent Git's garbage collector from deleting our base commit by
2163 * setting a reference to our base commit's ID.
2165 static const struct got_error *
2166 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2168 const struct got_error *err = NULL;
2169 struct got_reference *ref = NULL;
2172 err = got_worktree_get_base_ref_name(&refname, worktree);
2176 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2180 err = got_ref_write(ref, repo);
2188 static const struct got_error *
2189 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2191 const struct got_error *err = NULL;
2193 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2194 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2195 err = got_error_from_errno("asprintf");
2196 *fileindex_path = NULL;
2202 static const struct got_error *
2203 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2204 struct got_worktree *worktree)
2206 const struct got_error *err = NULL;
2209 *fileindex_path = NULL;
2210 *fileindex = got_fileindex_alloc();
2211 if (*fileindex == NULL)
2212 return got_error_from_errno("got_fileindex_alloc");
2214 err = get_fileindex_path(fileindex_path, worktree);
2218 index = fopen(*fileindex_path, "rb");
2219 if (index == NULL) {
2220 if (errno != ENOENT)
2221 err = got_error_from_errno2("fopen", *fileindex_path);
2223 err = got_fileindex_read(*fileindex, index);
2224 if (fclose(index) != 0 && err == NULL)
2225 err = got_error_from_errno("fclose");
2229 free(*fileindex_path);
2230 *fileindex_path = NULL;
2231 got_fileindex_free(*fileindex);
2237 struct bump_base_commit_id_arg {
2238 struct got_object_id *base_commit_id;
2241 const char *entry_name;
2242 got_worktree_checkout_cb progress_cb;
2246 /* Bump base commit ID of all files within an updated part of the work tree. */
2247 static const struct got_error *
2248 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2250 const struct got_error *err;
2251 struct bump_base_commit_id_arg *a = arg;
2253 if (a->entry_name) {
2254 if (strcmp(ie->path, a->path) != 0)
2256 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2259 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2260 SHA1_DIGEST_LENGTH) == 0)
2263 if (a->progress_cb) {
2264 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2269 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2273 static const struct got_error *
2274 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2276 const struct got_error *err = NULL;
2277 char *new_fileindex_path = NULL;
2278 FILE *new_index = NULL;
2279 struct timespec timeout;
2281 err = got_opentemp_named(&new_fileindex_path, &new_index,
2286 err = got_fileindex_write(fileindex, new_index);
2290 if (rename(new_fileindex_path, fileindex_path) != 0) {
2291 err = got_error_from_errno3("rename", new_fileindex_path,
2293 unlink(new_fileindex_path);
2297 * Sleep for a short amount of time to ensure that files modified after
2298 * this program exits have a different time stamp from the one which
2299 * was recorded in the file index.
2302 timeout.tv_nsec = 1;
2303 nanosleep(&timeout, NULL);
2307 free(new_fileindex_path);
2311 static const struct got_error *
2312 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2313 struct got_object_id **tree_id, const char *wt_relpath,
2314 struct got_worktree *worktree, struct got_repository *repo)
2316 const struct got_error *err = NULL;
2317 struct got_object_id *id = NULL;
2318 char *in_repo_path = NULL;
2319 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2321 *entry_type = GOT_OBJ_TYPE_ANY;
2322 *tree_relpath = NULL;
2325 if (wt_relpath[0] == '\0') {
2326 /* Check out all files within the work tree. */
2327 *entry_type = GOT_OBJ_TYPE_TREE;
2328 *tree_relpath = strdup("");
2329 if (*tree_relpath == NULL) {
2330 err = got_error_from_errno("strdup");
2333 err = got_object_id_by_path(tree_id, repo,
2334 worktree->base_commit_id, worktree->path_prefix);
2340 /* Check out a subset of files in the work tree. */
2342 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2343 is_root_wt ? "" : "/", wt_relpath) == -1) {
2344 err = got_error_from_errno("asprintf");
2348 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2354 in_repo_path = NULL;
2356 err = got_object_get_type(entry_type, repo, id);
2360 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2361 /* Check out a single file. */
2362 if (strchr(wt_relpath, '/') == NULL) {
2363 /* Check out a single file in work tree's root dir. */
2364 in_repo_path = strdup(worktree->path_prefix);
2365 if (in_repo_path == NULL) {
2366 err = got_error_from_errno("strdup");
2369 *tree_relpath = strdup("");
2370 if (*tree_relpath == NULL) {
2371 err = got_error_from_errno("strdup");
2375 /* Check out a single file in a subdirectory. */
2376 err = got_path_dirname(tree_relpath, wt_relpath);
2379 if (asprintf(&in_repo_path, "%s%s%s",
2380 worktree->path_prefix, is_root_wt ? "" : "/",
2381 *tree_relpath) == -1) {
2382 err = got_error_from_errno("asprintf");
2386 err = got_object_id_by_path(tree_id, repo,
2387 worktree->base_commit_id, in_repo_path);
2389 /* Check out all files within a subdirectory. */
2390 *tree_id = got_object_id_dup(id);
2391 if (*tree_id == NULL) {
2392 err = got_error_from_errno("got_object_id_dup");
2395 *tree_relpath = strdup(wt_relpath);
2396 if (*tree_relpath == NULL) {
2397 err = got_error_from_errno("strdup");
2405 *entry_type = GOT_OBJ_TYPE_ANY;
2406 free(*tree_relpath);
2407 *tree_relpath = NULL;
2414 static const struct got_error *
2415 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2416 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2417 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2418 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2420 const struct got_error *err = NULL;
2421 struct got_commit_object *commit = NULL;
2422 struct got_tree_object *tree = NULL;
2423 struct got_fileindex_diff_tree_cb diff_cb;
2424 struct diff_cb_arg arg;
2426 err = ref_base_commit(worktree, repo);
2428 if (!(err->code == GOT_ERR_ERRNO &&
2429 (errno == EACCES || errno == EROFS)))
2431 err = (*progress_cb)(progress_arg,
2432 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2437 err = got_object_open_as_commit(&commit, repo,
2438 worktree->base_commit_id);
2442 err = got_object_open_as_tree(&tree, repo, tree_id);
2447 got_object_tree_find_entry(tree, entry_name) == NULL) {
2448 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2452 diff_cb.diff_old_new = diff_old_new;
2453 diff_cb.diff_old = diff_old;
2454 diff_cb.diff_new = diff_new;
2455 arg.fileindex = fileindex;
2456 arg.worktree = worktree;
2458 arg.progress_cb = progress_cb;
2459 arg.progress_arg = progress_arg;
2460 arg.cancel_cb = cancel_cb;
2461 arg.cancel_arg = cancel_arg;
2462 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2463 entry_name, repo, &diff_cb, &arg);
2466 got_object_tree_close(tree);
2468 got_object_commit_close(commit);
2472 const struct got_error *
2473 got_worktree_checkout_files(struct got_worktree *worktree,
2474 struct got_pathlist_head *paths, struct got_repository *repo,
2475 got_worktree_checkout_cb progress_cb, void *progress_arg,
2476 got_cancel_cb cancel_cb, void *cancel_arg)
2478 const struct got_error *err = NULL, *sync_err, *unlockerr;
2479 struct got_commit_object *commit = NULL;
2480 struct got_tree_object *tree = NULL;
2481 struct got_fileindex *fileindex = NULL;
2482 char *fileindex_path = NULL;
2483 struct got_pathlist_entry *pe;
2484 struct tree_path_data {
2485 SIMPLEQ_ENTRY(tree_path_data) entry;
2486 struct got_object_id *tree_id;
2491 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2493 SIMPLEQ_INIT(&tree_paths);
2495 err = lock_worktree(worktree, LOCK_EX);
2499 /* Map all specified paths to in-repository trees. */
2500 TAILQ_FOREACH(pe, paths, entry) {
2501 tpd = malloc(sizeof(*tpd));
2503 err = got_error_from_errno("malloc");
2507 err = find_tree_entry_for_checkout(&tpd->entry_type,
2508 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2514 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2515 err = got_path_basename(&tpd->entry_name, pe->path);
2523 tpd->entry_name = NULL;
2525 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2529 * Read the file index.
2530 * Checking out files is supposed to be an idempotent operation.
2531 * If the on-disk file index is incomplete we will try to complete it.
2533 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2537 tpd = SIMPLEQ_FIRST(&tree_paths);
2538 TAILQ_FOREACH(pe, paths, entry) {
2539 struct bump_base_commit_id_arg bbc_arg;
2541 err = checkout_files(worktree, fileindex, tpd->relpath,
2542 tpd->tree_id, tpd->entry_name, repo,
2543 progress_cb, progress_arg, cancel_cb, cancel_arg);
2547 bbc_arg.base_commit_id = worktree->base_commit_id;
2548 bbc_arg.entry_name = tpd->entry_name;
2549 bbc_arg.path = pe->path;
2550 bbc_arg.path_len = pe->path_len;
2551 bbc_arg.progress_cb = progress_cb;
2552 bbc_arg.progress_arg = progress_arg;
2553 err = got_fileindex_for_each_entry_safe(fileindex,
2554 bump_base_commit_id, &bbc_arg);
2558 tpd = SIMPLEQ_NEXT(tpd, entry);
2560 sync_err = sync_fileindex(fileindex, fileindex_path);
2561 if (sync_err && err == NULL)
2564 free(fileindex_path);
2566 got_object_tree_close(tree);
2568 got_object_commit_close(commit);
2570 got_fileindex_free(fileindex);
2571 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2572 tpd = SIMPLEQ_FIRST(&tree_paths);
2573 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2578 unlockerr = lock_worktree(worktree, LOCK_SH);
2579 if (unlockerr && err == NULL)
2584 struct merge_file_cb_arg {
2585 struct got_worktree *worktree;
2586 struct got_fileindex *fileindex;
2587 got_worktree_checkout_cb progress_cb;
2589 got_cancel_cb cancel_cb;
2591 const char *label_orig;
2592 struct got_object_id *commit_id2;
2595 static const struct got_error *
2596 merge_file_cb(void *arg, struct got_blob_object *blob1,
2597 struct got_blob_object *blob2, struct got_object_id *id1,
2598 struct got_object_id *id2, const char *path1, const char *path2,
2599 mode_t mode1, mode_t mode2, struct got_repository *repo)
2601 static const struct got_error *err = NULL;
2602 struct merge_file_cb_arg *a = arg;
2603 struct got_fileindex_entry *ie;
2604 char *ondisk_path = NULL;
2606 unsigned char status;
2607 int local_changes_subsumed;
2609 if (blob1 && blob2) {
2610 ie = got_fileindex_entry_get(a->fileindex, path2,
2613 return (*a->progress_cb)(a->progress_arg,
2614 GOT_STATUS_MISSING, path2);
2616 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2618 return got_error_from_errno("asprintf");
2620 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2625 if (status == GOT_STATUS_DELETE) {
2626 err = (*a->progress_cb)(a->progress_arg,
2627 GOT_STATUS_MERGE, path2);
2630 if (status != GOT_STATUS_NO_CHANGE &&
2631 status != GOT_STATUS_MODIFY &&
2632 status != GOT_STATUS_CONFLICT &&
2633 status != GOT_STATUS_ADD) {
2634 err = (*a->progress_cb)(a->progress_arg, status, path2);
2638 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2639 err = merge_symlink(a->worktree, blob1,
2640 ondisk_path, path2, sb.st_mode, a->label_orig,
2641 blob2, a->commit_id2, repo, a->progress_cb,
2644 err = merge_blob(&local_changes_subsumed, a->worktree,
2645 blob1, ondisk_path, path2, sb.st_mode,
2646 a->label_orig, blob2, a->commit_id2, repo,
2647 a->progress_cb, a->progress_arg);
2650 ie = got_fileindex_entry_get(a->fileindex, path1,
2653 return (*a->progress_cb)(a->progress_arg,
2654 GOT_STATUS_MISSING, path1);
2656 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2658 return got_error_from_errno("asprintf");
2660 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2666 case GOT_STATUS_NO_CHANGE:
2667 err = (*a->progress_cb)(a->progress_arg,
2668 GOT_STATUS_DELETE, path1);
2671 err = remove_ondisk_file(a->worktree->root_path, path1);
2675 got_fileindex_entry_mark_deleted_from_disk(ie);
2677 case GOT_STATUS_DELETE:
2678 case GOT_STATUS_MISSING:
2679 err = (*a->progress_cb)(a->progress_arg,
2680 GOT_STATUS_DELETE, path1);
2684 got_fileindex_entry_mark_deleted_from_disk(ie);
2686 case GOT_STATUS_ADD:
2687 case GOT_STATUS_MODIFY:
2688 case GOT_STATUS_CONFLICT:
2689 err = (*a->progress_cb)(a->progress_arg,
2690 GOT_STATUS_CANNOT_DELETE, path1);
2694 case GOT_STATUS_OBSTRUCTED:
2695 err = (*a->progress_cb)(a->progress_arg, status, path1);
2703 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2705 return got_error_from_errno("asprintf");
2706 ie = got_fileindex_entry_get(a->fileindex, path2,
2709 err = get_file_status(&status, &sb, ie, ondisk_path,
2713 if (status != GOT_STATUS_NO_CHANGE &&
2714 status != GOT_STATUS_MODIFY &&
2715 status != GOT_STATUS_CONFLICT &&
2716 status != GOT_STATUS_ADD) {
2717 err = (*a->progress_cb)(a->progress_arg,
2721 if (S_ISLNK(mode2)) {
2722 err = merge_symlink(a->worktree, NULL,
2723 ondisk_path, path2, sb.st_mode,
2724 a->label_orig, blob2, a->commit_id2,
2725 repo, a->progress_cb, a->progress_arg);
2729 err = merge_blob(&local_changes_subsumed,
2730 a->worktree, NULL, ondisk_path, path2,
2731 sb.st_mode, a->label_orig, blob2,
2732 a->commit_id2, repo, a->progress_cb,
2737 if (status == GOT_STATUS_DELETE) {
2738 err = got_fileindex_entry_update(ie,
2739 ondisk_path, blob2->id.sha1,
2740 a->worktree->base_commit_id->sha1, 0);
2745 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2746 err = install_blob(a->worktree, ondisk_path, path2,
2747 mode2, sb.st_mode, blob2, 0, 0, 0, repo,
2748 a->progress_cb, a->progress_arg);
2751 err = got_fileindex_entry_alloc(&ie, path2);
2754 err = got_fileindex_entry_update(ie, ondisk_path,
2757 got_fileindex_entry_free(ie);
2760 err = got_fileindex_entry_add(a->fileindex, ie);
2762 got_fileindex_entry_free(ie);
2772 struct check_merge_ok_arg {
2773 struct got_worktree *worktree;
2774 struct got_repository *repo;
2777 static const struct got_error *
2778 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2780 const struct got_error *err = NULL;
2781 struct check_merge_ok_arg *a = arg;
2782 unsigned char status;
2786 /* Reject merges into a work tree with mixed base commits. */
2787 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2788 SHA1_DIGEST_LENGTH))
2789 return got_error(GOT_ERR_MIXED_COMMITS);
2791 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2793 return got_error_from_errno("asprintf");
2795 /* Reject merges into a work tree with conflicted files. */
2796 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2799 if (status == GOT_STATUS_CONFLICT)
2800 return got_error(GOT_ERR_CONFLICTS);
2805 static const struct got_error *
2806 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2807 const char *fileindex_path, struct got_object_id *commit_id1,
2808 struct got_object_id *commit_id2, struct got_repository *repo,
2809 got_worktree_checkout_cb progress_cb, void *progress_arg,
2810 got_cancel_cb cancel_cb, void *cancel_arg)
2812 const struct got_error *err = NULL, *sync_err;
2813 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2814 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2815 struct merge_file_cb_arg arg;
2816 char *label_orig = NULL;
2821 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2822 worktree->path_prefix);
2826 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2830 err = got_object_id_str(&id_str, commit_id1);
2834 if (asprintf(&label_orig, "%s: commit %s",
2835 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2836 err = got_error_from_errno("asprintf");
2843 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2844 worktree->path_prefix);
2848 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2852 arg.worktree = worktree;
2853 arg.fileindex = fileindex;
2854 arg.progress_cb = progress_cb;
2855 arg.progress_arg = progress_arg;
2856 arg.cancel_cb = cancel_cb;
2857 arg.cancel_arg = cancel_arg;
2858 arg.label_orig = label_orig;
2859 arg.commit_id2 = commit_id2;
2860 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2861 sync_err = sync_fileindex(fileindex, fileindex_path);
2862 if (sync_err && err == NULL)
2866 got_object_tree_close(tree1);
2868 got_object_tree_close(tree2);
2873 const struct got_error *
2874 got_worktree_merge_files(struct got_worktree *worktree,
2875 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2876 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2877 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2879 const struct got_error *err, *unlockerr;
2880 char *fileindex_path = NULL;
2881 struct got_fileindex *fileindex = NULL;
2882 struct check_merge_ok_arg mok_arg;
2884 err = lock_worktree(worktree, LOCK_EX);
2888 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2892 mok_arg.worktree = worktree;
2893 mok_arg.repo = repo;
2894 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2899 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2900 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2903 got_fileindex_free(fileindex);
2904 free(fileindex_path);
2905 unlockerr = lock_worktree(worktree, LOCK_SH);
2906 if (unlockerr && err == NULL)
2911 struct diff_dir_cb_arg {
2912 struct got_fileindex *fileindex;
2913 struct got_worktree *worktree;
2914 const char *status_path;
2915 size_t status_path_len;
2916 struct got_repository *repo;
2917 got_worktree_status_cb status_cb;
2919 got_cancel_cb cancel_cb;
2921 /* A pathlist containing per-directory pathlists of ignore patterns. */
2922 struct got_pathlist_head ignores;
2923 int report_unchanged;
2927 static const struct got_error *
2928 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2929 int dirfd, const char *de_name,
2930 got_worktree_status_cb status_cb, void *status_arg,
2931 struct got_repository *repo, int report_unchanged)
2933 const struct got_error *err = NULL;
2934 unsigned char status = GOT_STATUS_NO_CHANGE;
2935 unsigned char staged_status = get_staged_status(ie);
2937 struct got_object_id blob_id, commit_id, staged_blob_id;
2938 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2939 struct got_object_id *staged_blob_idp = NULL;
2941 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2945 if (status == GOT_STATUS_NO_CHANGE &&
2946 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2949 if (got_fileindex_entry_has_blob(ie)) {
2950 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2951 blob_idp = &blob_id;
2953 if (got_fileindex_entry_has_commit(ie)) {
2954 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2955 commit_idp = &commit_id;
2957 if (staged_status == GOT_STATUS_ADD ||
2958 staged_status == GOT_STATUS_MODIFY) {
2959 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2960 SHA1_DIGEST_LENGTH);
2961 staged_blob_idp = &staged_blob_id;
2964 return (*status_cb)(status_arg, status, staged_status,
2965 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2968 static const struct got_error *
2969 status_old_new(void *arg, struct got_fileindex_entry *ie,
2970 struct dirent *de, const char *parent_path, int dirfd)
2972 const struct got_error *err = NULL;
2973 struct diff_dir_cb_arg *a = arg;
2976 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2977 return got_error(GOT_ERR_CANCELLED);
2979 if (got_path_cmp(parent_path, a->status_path,
2980 strlen(parent_path), a->status_path_len) != 0 &&
2981 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2984 if (parent_path[0]) {
2985 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2986 parent_path, de->d_name) == -1)
2987 return got_error_from_errno("asprintf");
2989 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2991 return got_error_from_errno("asprintf");
2994 err = report_file_status(ie, abspath, dirfd, de->d_name,
2995 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3000 static const struct got_error *
3001 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3003 struct diff_dir_cb_arg *a = arg;
3004 struct got_object_id blob_id, commit_id;
3005 unsigned char status;
3007 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3008 return got_error(GOT_ERR_CANCELLED);
3010 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3013 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3014 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3015 if (got_fileindex_entry_has_file_on_disk(ie))
3016 status = GOT_STATUS_MISSING;
3018 status = GOT_STATUS_DELETE;
3019 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3020 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3024 free_ignorelist(struct got_pathlist_head *ignorelist)
3026 struct got_pathlist_entry *pe;
3028 TAILQ_FOREACH(pe, ignorelist, entry)
3029 free((char *)pe->path);
3030 got_pathlist_free(ignorelist);
3034 free_ignores(struct got_pathlist_head *ignores)
3036 struct got_pathlist_entry *pe;
3038 TAILQ_FOREACH(pe, ignores, entry) {
3039 struct got_pathlist_head *ignorelist = pe->data;
3040 free_ignorelist(ignorelist);
3041 free((char *)pe->path);
3043 got_pathlist_free(ignores);
3046 static const struct got_error *
3047 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3049 const struct got_error *err = NULL;
3050 struct got_pathlist_entry *pe = NULL;
3051 struct got_pathlist_head *ignorelist;
3052 char *line = NULL, *pattern, *dirpath = NULL;
3053 size_t linesize = 0;
3056 ignorelist = calloc(1, sizeof(*ignorelist));
3057 if (ignorelist == NULL)
3058 return got_error_from_errno("calloc");
3059 TAILQ_INIT(ignorelist);
3061 while ((linelen = getline(&line, &linesize, f)) != -1) {
3062 if (linelen > 0 && line[linelen - 1] == '\n')
3063 line[linelen - 1] = '\0';
3065 /* Git's ignores may contain comments. */
3069 /* Git's negated patterns are not (yet?) supported. */
3073 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3075 err = got_error_from_errno("asprintf");
3078 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3083 err = got_error_from_errno("getline");
3087 dirpath = strdup(path);
3088 if (dirpath == NULL) {
3089 err = got_error_from_errno("strdup");
3092 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3095 if (err || pe == NULL) {
3097 free_ignorelist(ignorelist);
3103 match_ignores(struct got_pathlist_head *ignores, const char *path)
3105 struct got_pathlist_entry *pe;
3107 /* Handle patterns which match in all directories. */
3108 TAILQ_FOREACH(pe, ignores, entry) {
3109 struct got_pathlist_head *ignorelist = pe->data;
3110 struct got_pathlist_entry *pi;
3112 TAILQ_FOREACH(pi, ignorelist, entry) {
3113 const char *p, *pattern = pi->path;
3115 if (strncmp(pattern, "**/", 3) != 0)
3120 if (fnmatch(pattern, p,
3121 FNM_PATHNAME | FNM_LEADING_DIR)) {
3122 /* Retry in next directory. */
3123 while (*p && *p != '/')
3135 * The ignores pathlist contains ignore lists from children before
3136 * parents, so we can find the most specific ignorelist by walking
3137 * ignores backwards.
3139 pe = TAILQ_LAST(ignores, got_pathlist_head);
3141 if (got_path_is_child(path, pe->path, pe->path_len)) {
3142 struct got_pathlist_head *ignorelist = pe->data;
3143 struct got_pathlist_entry *pi;
3144 TAILQ_FOREACH(pi, ignorelist, entry) {
3145 const char *pattern = pi->path;
3146 int flags = FNM_LEADING_DIR;
3147 if (strstr(pattern, "/**/") == NULL)
3148 flags |= FNM_PATHNAME;
3149 if (fnmatch(pattern, path, flags))
3154 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3160 static const struct got_error *
3161 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3162 const char *path, int dirfd, const char *ignores_filename)
3164 const struct got_error *err = NULL;
3167 FILE *ignoresfile = NULL;
3169 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3170 path[0] ? "/" : "", ignores_filename) == -1)
3171 return got_error_from_errno("asprintf");
3174 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3176 if (errno != ENOENT && errno != EACCES)
3177 err = got_error_from_errno2("openat",
3180 ignoresfile = fdopen(fd, "r");
3181 if (ignoresfile == NULL)
3182 err = got_error_from_errno2("fdopen",
3186 err = read_ignores(ignores, path, ignoresfile);
3190 ignoresfile = fopen(ignorespath, "r");
3191 if (ignoresfile == NULL) {
3192 if (errno != ENOENT && errno != EACCES)
3193 err = got_error_from_errno2("fopen",
3196 err = read_ignores(ignores, path, ignoresfile);
3199 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3200 err = got_error_from_errno2("fclose", path);
3201 if (fd != -1 && close(fd) == -1 && err == NULL)
3202 err = got_error_from_errno2("close", path);
3207 static const struct got_error *
3208 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3210 const struct got_error *err = NULL;
3211 struct diff_dir_cb_arg *a = arg;
3214 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3215 return got_error(GOT_ERR_CANCELLED);
3217 if (parent_path[0]) {
3218 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3219 return got_error_from_errno("asprintf");
3224 if (de->d_type != DT_DIR &&
3225 got_path_is_child(path, a->status_path, a->status_path_len)
3226 && !match_ignores(&a->ignores, path))
3227 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3228 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3234 static const struct got_error *
3235 status_traverse(void *arg, const char *path, int dirfd)
3237 const struct got_error *err = NULL;
3238 struct diff_dir_cb_arg *a = arg;
3243 err = add_ignores(&a->ignores, a->worktree->root_path,
3244 path, dirfd, ".cvsignore");
3248 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3249 dirfd, ".gitignore");
3254 static const struct got_error *
3255 report_single_file_status(const char *path, const char *ondisk_path,
3256 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3257 void *status_arg, struct got_repository *repo, int report_unchanged)
3259 struct got_fileindex_entry *ie;
3262 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3264 return report_file_status(ie, ondisk_path, -1, NULL,
3265 status_cb, status_arg, repo, report_unchanged);
3267 if (lstat(ondisk_path, &sb) == -1) {
3268 if (errno != ENOENT)
3269 return got_error_from_errno2("lstat", ondisk_path);
3270 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3271 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3275 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3276 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3277 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3282 static const struct got_error *
3283 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3284 const char *root_path, const char *path)
3286 const struct got_error *err;
3287 char *parent_path, *next_parent_path = NULL;
3289 err = add_ignores(ignores, root_path, "", -1,
3294 err = add_ignores(ignores, root_path, "", -1,
3299 err = got_path_dirname(&parent_path, path);
3301 if (err->code == GOT_ERR_BAD_PATH)
3302 return NULL; /* cannot traverse parent */
3306 err = add_ignores(ignores, root_path, parent_path, -1,
3310 err = add_ignores(ignores, root_path, parent_path, -1,
3314 err = got_path_dirname(&next_parent_path, parent_path);
3316 if (err->code == GOT_ERR_BAD_PATH)
3317 err = NULL; /* traversed everything */
3321 parent_path = next_parent_path;
3322 next_parent_path = NULL;
3326 free(next_parent_path);
3330 static const struct got_error *
3331 worktree_status(struct got_worktree *worktree, const char *path,
3332 struct got_fileindex *fileindex, struct got_repository *repo,
3333 got_worktree_status_cb status_cb, void *status_arg,
3334 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3335 int report_unchanged)
3337 const struct got_error *err = NULL;
3339 struct got_fileindex_diff_dir_cb fdiff_cb;
3340 struct diff_dir_cb_arg arg;
3341 char *ondisk_path = NULL;
3343 TAILQ_INIT(&arg.ignores);
3345 if (asprintf(&ondisk_path, "%s%s%s",
3346 worktree->root_path, path[0] ? "/" : "", path) == -1)
3347 return got_error_from_errno("asprintf");
3349 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3351 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3353 err = got_error_from_errno2("open", ondisk_path);
3355 err = report_single_file_status(path, ondisk_path,
3356 fileindex, status_cb, status_arg, repo,
3359 fdiff_cb.diff_old_new = status_old_new;
3360 fdiff_cb.diff_old = status_old;
3361 fdiff_cb.diff_new = status_new;
3362 fdiff_cb.diff_traverse = status_traverse;
3363 arg.fileindex = fileindex;
3364 arg.worktree = worktree;
3365 arg.status_path = path;
3366 arg.status_path_len = strlen(path);
3368 arg.status_cb = status_cb;
3369 arg.status_arg = status_arg;
3370 arg.cancel_cb = cancel_cb;
3371 arg.cancel_arg = cancel_arg;
3372 arg.report_unchanged = report_unchanged;
3373 arg.no_ignores = no_ignores;
3375 err = add_ignores_from_parent_paths(&arg.ignores,
3376 worktree->root_path, path);
3380 err = got_fileindex_diff_dir(fileindex, fd,
3381 worktree->root_path, path, repo, &fdiff_cb, &arg);
3384 free_ignores(&arg.ignores);
3385 if (fd != -1 && close(fd) != 0 && err == NULL)
3386 err = got_error_from_errno("close");
3391 const struct got_error *
3392 got_worktree_status(struct got_worktree *worktree,
3393 struct got_pathlist_head *paths, struct got_repository *repo,
3394 got_worktree_status_cb status_cb, void *status_arg,
3395 got_cancel_cb cancel_cb, void *cancel_arg)
3397 const struct got_error *err = NULL;
3398 char *fileindex_path = NULL;
3399 struct got_fileindex *fileindex = NULL;
3400 struct got_pathlist_entry *pe;
3402 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3406 TAILQ_FOREACH(pe, paths, entry) {
3407 err = worktree_status(worktree, pe->path, fileindex, repo,
3408 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3412 free(fileindex_path);
3413 got_fileindex_free(fileindex);
3417 const struct got_error *
3418 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3421 const struct got_error *err = NULL;
3422 char *resolved = NULL, *cwd = NULL, *path = NULL;
3428 cwd = getcwd(NULL, 0);
3430 return got_error_from_errno("getcwd");
3432 if (lstat(arg, &sb) == -1) {
3433 if (errno != ENOENT) {
3434 err = got_error_from_errno2("lstat", arg);
3438 if (S_ISLNK(sb.st_mode)) {
3440 * We cannot use realpath(3) with symlinks since we want to
3441 * operate on the symlink itself.
3442 * But we can make the path absolute, assuming it is relative
3443 * to the current working directory, and then canonicalize it.
3445 char *abspath = NULL;
3446 char canonpath[PATH_MAX];
3447 if (!got_path_is_absolute(arg)) {
3448 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3449 err = got_error_from_errno("asprintf");
3454 err = got_canonpath(abspath ? abspath : arg, canonpath,
3458 resolved = strdup(canonpath);
3459 if (resolved == NULL) {
3460 err = got_error_from_errno("strdup");
3464 resolved = realpath(arg, NULL);
3465 if (resolved == NULL) {
3466 if (errno != ENOENT) {
3467 err = got_error_from_errno2("realpath", arg);
3470 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3471 err = got_error_from_errno("asprintf");
3477 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3478 strlen(got_worktree_get_root_path(worktree)))) {
3479 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3483 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3484 err = got_path_skip_common_ancestor(&path,
3485 got_worktree_get_root_path(worktree), resolved);
3491 err = got_error_from_errno("strdup");
3496 /* XXX status walk can't deal with trailing slash! */
3498 while (len > 0 && path[len - 1] == '/') {
3499 path[len - 1] = '\0';
3512 struct schedule_addition_args {
3513 struct got_worktree *worktree;
3514 struct got_fileindex *fileindex;
3515 got_worktree_checkout_cb progress_cb;
3517 struct got_repository *repo;
3520 static const struct got_error *
3521 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3522 const char *relpath, struct got_object_id *blob_id,
3523 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3524 int dirfd, const char *de_name)
3526 struct schedule_addition_args *a = arg;
3527 const struct got_error *err = NULL;
3528 struct got_fileindex_entry *ie;
3532 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3534 return got_error_from_errno("asprintf");
3536 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3538 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3542 /* Re-adding an existing entry is a no-op. */
3543 if (status == GOT_STATUS_ADD)
3545 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3550 if (status != GOT_STATUS_UNVERSIONED) {
3551 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3555 err = got_fileindex_entry_alloc(&ie, relpath);
3558 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3560 got_fileindex_entry_free(ie);
3563 err = got_fileindex_entry_add(a->fileindex, ie);
3565 got_fileindex_entry_free(ie);
3572 if (status == GOT_STATUS_ADD)
3574 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3577 const struct got_error *
3578 got_worktree_schedule_add(struct got_worktree *worktree,
3579 struct got_pathlist_head *paths,
3580 got_worktree_checkout_cb progress_cb, void *progress_arg,
3581 struct got_repository *repo, int no_ignores)
3583 struct got_fileindex *fileindex = NULL;
3584 char *fileindex_path = NULL;
3585 const struct got_error *err = NULL, *sync_err, *unlockerr;
3586 struct got_pathlist_entry *pe;
3587 struct schedule_addition_args saa;
3589 err = lock_worktree(worktree, LOCK_EX);
3593 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3597 saa.worktree = worktree;
3598 saa.fileindex = fileindex;
3599 saa.progress_cb = progress_cb;
3600 saa.progress_arg = progress_arg;
3603 TAILQ_FOREACH(pe, paths, entry) {
3604 err = worktree_status(worktree, pe->path, fileindex, repo,
3605 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3609 sync_err = sync_fileindex(fileindex, fileindex_path);
3610 if (sync_err && err == NULL)
3613 free(fileindex_path);
3615 got_fileindex_free(fileindex);
3616 unlockerr = lock_worktree(worktree, LOCK_SH);
3617 if (unlockerr && err == NULL)
3622 struct schedule_deletion_args {
3623 struct got_worktree *worktree;
3624 struct got_fileindex *fileindex;
3625 got_worktree_delete_cb progress_cb;
3627 struct got_repository *repo;
3628 int delete_local_mods;
3632 static const struct got_error *
3633 schedule_for_deletion(void *arg, unsigned char status,
3634 unsigned char staged_status, const char *relpath,
3635 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3636 struct got_object_id *commit_id, int dirfd, const char *de_name)
3638 struct schedule_deletion_args *a = arg;
3639 const struct got_error *err = NULL;
3640 struct got_fileindex_entry *ie = NULL;
3642 char *ondisk_path, *parent = NULL;
3644 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3646 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3648 staged_status = get_staged_status(ie);
3649 if (staged_status != GOT_STATUS_NO_CHANGE) {
3650 if (staged_status == GOT_STATUS_DELETE)
3652 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3655 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3657 return got_error_from_errno("asprintf");
3659 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3664 if (status != GOT_STATUS_NO_CHANGE) {
3665 if (status == GOT_STATUS_DELETE)
3667 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3668 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3671 if (status != GOT_STATUS_MODIFY &&
3672 status != GOT_STATUS_MISSING) {
3673 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3678 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3680 if (unlinkat(dirfd, de_name, 0) != 0) {
3681 err = got_error_from_errno2("unlinkat",
3685 } else if (unlink(ondisk_path) != 0) {
3686 err = got_error_from_errno2("unlink", ondisk_path);
3690 parent = dirname(ondisk_path);
3692 if (parent == NULL) {
3693 err = got_error_from_errno2("dirname", ondisk_path);
3696 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3697 if (rmdir(parent) == -1) {
3698 if (errno != ENOTEMPTY)
3699 err = got_error_from_errno2("rmdir",
3703 parent = dirname(parent);
3704 if (parent == NULL) {
3705 err = got_error_from_errno2("dirname", parent);
3711 got_fileindex_entry_mark_deleted_from_disk(ie);
3716 if (status == GOT_STATUS_DELETE)
3718 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3719 staged_status, relpath);
3722 const struct got_error *
3723 got_worktree_schedule_delete(struct got_worktree *worktree,
3724 struct got_pathlist_head *paths, int delete_local_mods,
3725 got_worktree_delete_cb progress_cb, void *progress_arg,
3726 struct got_repository *repo, int keep_on_disk)
3728 struct got_fileindex *fileindex = NULL;
3729 char *fileindex_path = NULL;
3730 const struct got_error *err = NULL, *sync_err, *unlockerr;
3731 struct got_pathlist_entry *pe;
3732 struct schedule_deletion_args sda;
3734 err = lock_worktree(worktree, LOCK_EX);
3738 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3742 sda.worktree = worktree;
3743 sda.fileindex = fileindex;
3744 sda.progress_cb = progress_cb;
3745 sda.progress_arg = progress_arg;
3747 sda.delete_local_mods = delete_local_mods;
3748 sda.keep_on_disk = keep_on_disk;
3750 TAILQ_FOREACH(pe, paths, entry) {
3751 err = worktree_status(worktree, pe->path, fileindex, repo,
3752 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3756 sync_err = sync_fileindex(fileindex, fileindex_path);
3757 if (sync_err && err == NULL)
3760 free(fileindex_path);
3762 got_fileindex_free(fileindex);
3763 unlockerr = lock_worktree(worktree, LOCK_SH);
3764 if (unlockerr && err == NULL)
3769 static const struct got_error *
3770 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3772 const struct got_error *err = NULL;
3774 size_t linesize = 0, n;
3777 linelen = getline(&line, &linesize, infile);
3778 if (linelen == -1) {
3779 if (ferror(infile)) {
3780 err = got_error_from_errno("getline");
3786 n = fwrite(line, 1, linelen, outfile);
3788 err = got_ferror(outfile, GOT_ERR_IO);
3793 n = fwrite(line, 1, linelen, rejectfile);
3795 err = got_ferror(outfile, GOT_ERR_IO);
3802 static const struct got_error *
3803 skip_one_line(FILE *f)
3806 size_t linesize = 0;
3809 linelen = getline(&line, &linesize, f);
3810 if (linelen == -1) {
3812 return got_error_from_errno("getline");
3819 static const struct got_error *
3820 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3821 int start_old, int end_old, int start_new, int end_new,
3822 FILE *outfile, FILE *rejectfile)
3824 const struct got_error *err;
3826 /* Copy old file's lines leading up to patch. */
3827 while (!feof(f1) && *line_cur1 < start_old) {
3828 err = copy_one_line(f1, outfile, NULL);
3833 /* Skip new file's lines leading up to patch. */
3834 while (!feof(f2) && *line_cur2 < start_new) {
3836 err = copy_one_line(f2, NULL, rejectfile);
3838 err = skip_one_line(f2);
3843 /* Copy patched lines. */
3844 while (!feof(f2) && *line_cur2 <= end_new) {
3845 err = copy_one_line(f2, outfile, NULL);
3850 /* Skip over old file's replaced lines. */
3851 while (!feof(f1) && *line_cur1 <= end_old) {
3853 err = copy_one_line(f1, NULL, rejectfile);
3855 err = skip_one_line(f1);
3864 static const struct got_error *
3865 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3866 FILE *outfile, FILE *rejectfile)
3868 const struct got_error *err;
3871 /* Copy old file's lines until EOF. */
3873 err = copy_one_line(f1, outfile, NULL);
3880 /* Copy new file's lines until EOF. */
3882 err = copy_one_line(f2, NULL, rejectfile);
3892 static const struct got_error *
3893 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3894 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3895 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3896 int *line_cur2, FILE *outfile, FILE *rejectfile,
3897 got_worktree_patch_cb patch_cb, void *patch_arg)
3899 const struct got_error *err = NULL;
3900 int start_old = change->cv.a;
3901 int end_old = change->cv.b;
3902 int start_new = change->cv.c;
3903 int end_new = change->cv.d;
3907 *choice = GOT_PATCH_CHOICE_NONE;
3909 hunkfile = got_opentemp();
3910 if (hunkfile == NULL)
3911 return got_error_from_errno("got_opentemp");
3916 /* XXX TODO needs error checking */
3917 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3919 if (fseek(f1, pos1, SEEK_SET) == -1) {
3920 err = got_ferror(f1, GOT_ERR_IO);
3923 if (fseek(f2, pos2, SEEK_SET) == -1) {
3924 err = got_ferror(f1, GOT_ERR_IO);
3927 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3928 err = got_ferror(hunkfile, GOT_ERR_IO);
3932 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3933 hunkfile, n, nchanges);
3938 case GOT_PATCH_CHOICE_YES:
3939 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3940 end_old, start_new, end_new, outfile, rejectfile);
3942 case GOT_PATCH_CHOICE_NO:
3943 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3944 end_old, start_new, end_new, rejectfile, outfile);
3946 case GOT_PATCH_CHOICE_QUIT:
3949 err = got_error(GOT_ERR_PATCH_CHOICE);
3953 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3954 err = got_error_from_errno("fclose");
3958 struct revert_file_args {
3959 struct got_worktree *worktree;
3960 struct got_fileindex *fileindex;
3961 got_worktree_checkout_cb progress_cb;
3963 got_worktree_patch_cb patch_cb;
3965 struct got_repository *repo;
3968 static const struct got_error *
3969 create_patched_content(char **path_outfile, int reverse_patch,
3970 struct got_object_id *blob_id, const char *path2,
3971 int dirfd2, const char *de_name2,
3972 const char *relpath, struct got_repository *repo,
3973 got_worktree_patch_cb patch_cb, void *patch_arg)
3975 const struct got_error *err;
3976 struct got_blob_object *blob = NULL;
3977 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3979 char *path1 = NULL, *id_str = NULL;
3980 struct stat sb1, sb2;
3981 struct got_diff_changes *changes = NULL;
3982 struct got_diff_state *ds = NULL;
3983 struct got_diff_args *args = NULL;
3984 struct got_diff_change *change;
3985 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3988 *path_outfile = NULL;
3990 err = got_object_id_str(&id_str, blob_id);
3995 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3997 err = got_error_from_errno2("openat", path2);
4001 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4003 err = got_error_from_errno2("open", path2);
4007 if (fstat(fd2, &sb2) == -1) {
4008 err = got_error_from_errno2("fstat", path2);
4012 f2 = fdopen(fd2, "r");
4014 err = got_error_from_errno2("fdopen", path2);
4019 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4023 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4027 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4031 if (stat(path1, &sb1) == -1) {
4032 err = got_error_from_errno2("stat", path1);
4036 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4037 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4041 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4045 if (fseek(f1, 0L, SEEK_SET) == -1)
4046 return got_ferror(f1, GOT_ERR_IO);
4047 if (fseek(f2, 0L, SEEK_SET) == -1)
4048 return got_ferror(f2, GOT_ERR_IO);
4049 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4051 err = apply_or_reject_change(&choice, change, ++n,
4052 changes->nchanges, ds, args, diff_flags, relpath,
4053 f1, f2, &line_cur1, &line_cur2,
4054 reverse_patch ? NULL : outfile,
4055 reverse_patch ? outfile : NULL,
4056 patch_cb, patch_arg);
4059 if (choice == GOT_PATCH_CHOICE_YES)
4061 else if (choice == GOT_PATCH_CHOICE_QUIT)
4065 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4066 reverse_patch ? NULL : outfile,
4067 reverse_patch ? outfile : NULL);
4071 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4072 err = got_error_from_errno2("chmod", path2);
4079 got_object_blob_close(blob);
4080 if (f1 && fclose(f1) == EOF && err == NULL)
4081 err = got_error_from_errno2("fclose", path1);
4082 if (f2 && fclose(f2) == EOF && err == NULL)
4083 err = got_error_from_errno2("fclose", path2);
4084 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4085 err = got_error_from_errno2("close", path2);
4086 if (outfile && fclose(outfile) == EOF && err == NULL)
4087 err = got_error_from_errno2("fclose", *path_outfile);
4088 if (path1 && unlink(path1) == -1 && err == NULL)
4089 err = got_error_from_errno2("unlink", path1);
4090 if (err || !have_content) {
4091 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4092 err = got_error_from_errno2("unlink", *path_outfile);
4093 free(*path_outfile);
4094 *path_outfile = NULL;
4098 got_diff_state_free(ds);
4102 got_diff_free_changes(changes);
4107 static const struct got_error *
4108 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4109 const char *relpath, struct got_object_id *blob_id,
4110 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4111 int dirfd, const char *de_name)
4113 struct revert_file_args *a = arg;
4114 const struct got_error *err = NULL;
4115 char *parent_path = NULL;
4116 struct got_fileindex_entry *ie;
4117 struct got_tree_object *tree = NULL;
4118 struct got_object_id *tree_id = NULL;
4119 const struct got_tree_entry *te = NULL;
4120 char *tree_path = NULL, *te_name;
4121 char *ondisk_path = NULL, *path_content = NULL;
4122 struct got_blob_object *blob = NULL;
4124 /* Reverting a staged deletion is a no-op. */
4125 if (status == GOT_STATUS_DELETE &&
4126 staged_status != GOT_STATUS_NO_CHANGE)
4129 if (status == GOT_STATUS_UNVERSIONED)
4130 return (*a->progress_cb)(a->progress_arg,
4131 GOT_STATUS_UNVERSIONED, relpath);
4133 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4135 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4137 /* Construct in-repository path of tree which contains this blob. */
4138 err = got_path_dirname(&parent_path, ie->path);
4140 if (err->code != GOT_ERR_BAD_PATH)
4142 parent_path = strdup("/");
4143 if (parent_path == NULL) {
4144 err = got_error_from_errno("strdup");
4148 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4149 tree_path = strdup(parent_path);
4150 if (tree_path == NULL) {
4151 err = got_error_from_errno("strdup");
4155 if (got_path_is_root_dir(parent_path)) {
4156 tree_path = strdup(a->worktree->path_prefix);
4157 if (tree_path == NULL) {
4158 err = got_error_from_errno("strdup");
4162 if (asprintf(&tree_path, "%s/%s",
4163 a->worktree->path_prefix, parent_path) == -1) {
4164 err = got_error_from_errno("asprintf");
4170 err = got_object_id_by_path(&tree_id, a->repo,
4171 a->worktree->base_commit_id, tree_path);
4173 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4174 (status == GOT_STATUS_ADD ||
4175 staged_status == GOT_STATUS_ADD)))
4178 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4182 te_name = basename(ie->path);
4183 if (te_name == NULL) {
4184 err = got_error_from_errno2("basename", ie->path);
4188 te = got_object_tree_find_entry(tree, te_name);
4189 if (te == NULL && status != GOT_STATUS_ADD &&
4190 staged_status != GOT_STATUS_ADD) {
4191 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4197 case GOT_STATUS_ADD:
4199 int choice = GOT_PATCH_CHOICE_NONE;
4200 err = (*a->patch_cb)(&choice, a->patch_arg,
4201 status, ie->path, NULL, 1, 1);
4204 if (choice != GOT_PATCH_CHOICE_YES)
4207 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4211 got_fileindex_entry_remove(a->fileindex, ie);
4213 case GOT_STATUS_DELETE:
4215 int choice = GOT_PATCH_CHOICE_NONE;
4216 err = (*a->patch_cb)(&choice, a->patch_arg,
4217 status, ie->path, NULL, 1, 1);
4220 if (choice != GOT_PATCH_CHOICE_YES)
4224 case GOT_STATUS_MODIFY:
4225 case GOT_STATUS_MODE_CHANGE:
4226 case GOT_STATUS_CONFLICT:
4227 case GOT_STATUS_MISSING: {
4228 struct got_object_id id;
4229 if (staged_status == GOT_STATUS_ADD ||
4230 staged_status == GOT_STATUS_MODIFY) {
4231 memcpy(id.sha1, ie->staged_blob_sha1,
4232 SHA1_DIGEST_LENGTH);
4234 memcpy(id.sha1, ie->blob_sha1,
4235 SHA1_DIGEST_LENGTH);
4236 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4240 if (asprintf(&ondisk_path, "%s/%s",
4241 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4242 err = got_error_from_errno("asprintf");
4246 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4247 status == GOT_STATUS_CONFLICT)) {
4248 err = create_patched_content(&path_content, 1, &id,
4249 ondisk_path, dirfd, de_name, ie->path, a->repo,
4250 a->patch_cb, a->patch_arg);
4251 if (err || path_content == NULL)
4253 if (rename(path_content, ondisk_path) == -1) {
4254 err = got_error_from_errno3("rename",
4255 path_content, ondisk_path);
4259 err = install_blob(a->worktree, ondisk_path, ie->path,
4260 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4261 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4262 a->repo, a->progress_cb, a->progress_arg);
4265 if (status == GOT_STATUS_DELETE ||
4266 status == GOT_STATUS_MODE_CHANGE) {
4267 err = got_fileindex_entry_update(ie,
4268 ondisk_path, blob->id.sha1,
4269 a->worktree->base_commit_id->sha1, 1);
4285 got_object_blob_close(blob);
4287 got_object_tree_close(tree);
4292 const struct got_error *
4293 got_worktree_revert(struct got_worktree *worktree,
4294 struct got_pathlist_head *paths,
4295 got_worktree_checkout_cb progress_cb, void *progress_arg,
4296 got_worktree_patch_cb patch_cb, void *patch_arg,
4297 struct got_repository *repo)
4299 struct got_fileindex *fileindex = NULL;
4300 char *fileindex_path = NULL;
4301 const struct got_error *err = NULL, *unlockerr = NULL;
4302 const struct got_error *sync_err = NULL;
4303 struct got_pathlist_entry *pe;
4304 struct revert_file_args rfa;
4306 err = lock_worktree(worktree, LOCK_EX);
4310 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4314 rfa.worktree = worktree;
4315 rfa.fileindex = fileindex;
4316 rfa.progress_cb = progress_cb;
4317 rfa.progress_arg = progress_arg;
4318 rfa.patch_cb = patch_cb;
4319 rfa.patch_arg = patch_arg;
4321 TAILQ_FOREACH(pe, paths, entry) {
4322 err = worktree_status(worktree, pe->path, fileindex, repo,
4323 revert_file, &rfa, NULL, NULL, 0, 0);
4327 sync_err = sync_fileindex(fileindex, fileindex_path);
4328 if (sync_err && err == NULL)
4331 free(fileindex_path);
4333 got_fileindex_free(fileindex);
4334 unlockerr = lock_worktree(worktree, LOCK_SH);
4335 if (unlockerr && err == NULL)
4341 free_commitable(struct got_commitable *ct)
4344 free(ct->in_repo_path);
4345 free(ct->ondisk_path);
4347 free(ct->base_blob_id);
4348 free(ct->staged_blob_id);
4349 free(ct->base_commit_id);
4353 struct collect_commitables_arg {
4354 struct got_pathlist_head *commitable_paths;
4355 struct got_repository *repo;
4356 struct got_worktree *worktree;
4357 int have_staged_files;
4360 static const struct got_error *
4361 collect_commitables(void *arg, unsigned char status,
4362 unsigned char staged_status, const char *relpath,
4363 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4364 struct got_object_id *commit_id, int dirfd, const char *de_name)
4366 struct collect_commitables_arg *a = arg;
4367 const struct got_error *err = NULL;
4368 struct got_commitable *ct = NULL;
4369 struct got_pathlist_entry *new = NULL;
4370 char *parent_path = NULL, *path = NULL;
4373 if (a->have_staged_files) {
4374 if (staged_status != GOT_STATUS_MODIFY &&
4375 staged_status != GOT_STATUS_ADD &&
4376 staged_status != GOT_STATUS_DELETE)
4379 if (status == GOT_STATUS_CONFLICT)
4380 return got_error(GOT_ERR_COMMIT_CONFLICT);
4382 if (status != GOT_STATUS_MODIFY &&
4383 status != GOT_STATUS_MODE_CHANGE &&
4384 status != GOT_STATUS_ADD &&
4385 status != GOT_STATUS_DELETE)
4389 if (asprintf(&path, "/%s", relpath) == -1) {
4390 err = got_error_from_errno("asprintf");
4393 if (strcmp(path, "/") == 0) {
4394 parent_path = strdup("");
4395 if (parent_path == NULL)
4396 return got_error_from_errno("strdup");
4398 err = got_path_dirname(&parent_path, path);
4403 ct = calloc(1, sizeof(*ct));
4405 err = got_error_from_errno("calloc");
4409 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4411 err = got_error_from_errno("asprintf");
4414 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4415 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4418 if (fstatat(dirfd, de_name, &sb,
4419 AT_SYMLINK_NOFOLLOW) == -1) {
4420 err = got_error_from_errno2("fstatat",
4424 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4425 err = got_error_from_errno2("lstat", ct->ondisk_path);
4428 ct->mode = sb.st_mode;
4431 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4432 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4434 err = got_error_from_errno("asprintf");
4438 ct->status = status;
4439 ct->staged_status = staged_status;
4440 ct->blob_id = NULL; /* will be filled in when blob gets created */
4441 if (ct->status != GOT_STATUS_ADD &&
4442 ct->staged_status != GOT_STATUS_ADD) {
4443 ct->base_blob_id = got_object_id_dup(blob_id);
4444 if (ct->base_blob_id == NULL) {
4445 err = got_error_from_errno("got_object_id_dup");
4448 ct->base_commit_id = got_object_id_dup(commit_id);
4449 if (ct->base_commit_id == NULL) {
4450 err = got_error_from_errno("got_object_id_dup");
4454 if (ct->staged_status == GOT_STATUS_ADD ||
4455 ct->staged_status == GOT_STATUS_MODIFY) {
4456 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4457 if (ct->staged_blob_id == NULL) {
4458 err = got_error_from_errno("got_object_id_dup");
4462 ct->path = strdup(path);
4463 if (ct->path == NULL) {
4464 err = got_error_from_errno("strdup");
4467 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4469 if (ct && (err || new == NULL))
4470 free_commitable(ct);
4476 static const struct got_error *write_tree(struct got_object_id **, int *,
4477 struct got_tree_object *, const char *, struct got_pathlist_head *,
4478 got_worktree_status_cb status_cb, void *status_arg,
4479 struct got_repository *);
4481 static const struct got_error *
4482 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4483 struct got_tree_entry *te, const char *parent_path,
4484 struct got_pathlist_head *commitable_paths,
4485 got_worktree_status_cb status_cb, void *status_arg,
4486 struct got_repository *repo)
4488 const struct got_error *err = NULL;
4489 struct got_tree_object *subtree;
4492 if (asprintf(&subpath, "%s%s%s", parent_path,
4493 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4494 return got_error_from_errno("asprintf");
4496 err = got_object_open_as_tree(&subtree, repo, &te->id);
4500 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4501 commitable_paths, status_cb, status_arg, repo);
4502 got_object_tree_close(subtree);
4507 static const struct got_error *
4508 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4510 const struct got_error *err = NULL;
4511 char *ct_parent_path = NULL;
4515 if (strchr(ct->in_repo_path, '/') == NULL) {
4516 *match = got_path_is_root_dir(path);
4520 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4523 *match = (strcmp(path, ct_parent_path) == 0);
4524 free(ct_parent_path);
4529 get_ct_file_mode(struct got_commitable *ct)
4531 if (S_ISLNK(ct->mode))
4534 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4537 static const struct got_error *
4538 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4539 struct got_tree_entry *te, struct got_commitable *ct)
4541 const struct got_error *err = NULL;
4545 err = got_object_tree_entry_dup(new_te, te);
4549 (*new_te)->mode = get_ct_file_mode(ct);
4551 if (ct->staged_status == GOT_STATUS_MODIFY)
4552 memcpy(&(*new_te)->id, ct->staged_blob_id,
4553 sizeof((*new_te)->id));
4555 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4557 if (err && *new_te) {
4564 static const struct got_error *
4565 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4566 struct got_commitable *ct)
4568 const struct got_error *err = NULL;
4573 *new_te = calloc(1, sizeof(**new_te));
4574 if (*new_te == NULL)
4575 return got_error_from_errno("calloc");
4577 ct_name = basename(ct->path);
4578 if (ct_name == NULL) {
4579 err = got_error_from_errno2("basename", ct->path);
4582 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4583 sizeof((*new_te)->name)) {
4584 err = got_error(GOT_ERR_NO_SPACE);
4588 (*new_te)->mode = get_ct_file_mode(ct);
4590 if (ct->staged_status == GOT_STATUS_ADD)
4591 memcpy(&(*new_te)->id, ct->staged_blob_id,
4592 sizeof((*new_te)->id));
4594 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4596 if (err && *new_te) {
4603 static const struct got_error *
4604 insert_tree_entry(struct got_tree_entry *new_te,
4605 struct got_pathlist_head *paths)
4607 const struct got_error *err = NULL;
4608 struct got_pathlist_entry *new_pe;
4610 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4614 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4618 static const struct got_error *
4619 report_ct_status(struct got_commitable *ct,
4620 got_worktree_status_cb status_cb, void *status_arg)
4622 const char *ct_path = ct->path;
4623 unsigned char status;
4625 while (ct_path[0] == '/')
4628 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4629 status = ct->staged_status;
4631 status = ct->status;
4633 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4634 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4637 static const struct got_error *
4638 match_modified_subtree(int *modified, struct got_tree_entry *te,
4639 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4641 const struct got_error *err = NULL;
4642 struct got_pathlist_entry *pe;
4647 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4648 got_path_is_root_dir(base_tree_path) ? "" : "/",
4650 return got_error_from_errno("asprintf");
4652 TAILQ_FOREACH(pe, commitable_paths, entry) {
4653 struct got_commitable *ct = pe->data;
4654 *modified = got_path_is_child(ct->in_repo_path, te_path,
4664 static const struct got_error *
4665 match_deleted_or_modified_ct(struct got_commitable **ctp,
4666 struct got_tree_entry *te, const char *base_tree_path,
4667 struct got_pathlist_head *commitable_paths)
4669 const struct got_error *err = NULL;
4670 struct got_pathlist_entry *pe;
4674 TAILQ_FOREACH(pe, commitable_paths, entry) {
4675 struct got_commitable *ct = pe->data;
4676 char *ct_name = NULL;
4679 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4680 if (ct->status != GOT_STATUS_MODIFY &&
4681 ct->status != GOT_STATUS_MODE_CHANGE &&
4682 ct->status != GOT_STATUS_DELETE)
4685 if (ct->staged_status != GOT_STATUS_MODIFY &&
4686 ct->staged_status != GOT_STATUS_DELETE)
4690 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4693 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4699 ct_name = basename(pe->path);
4700 if (ct_name == NULL)
4701 return got_error_from_errno2("basename", pe->path);
4703 if (strcmp(te->name, ct_name) != 0)
4713 static const struct got_error *
4714 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4715 const char *child_path, const char *path_base_tree,
4716 struct got_pathlist_head *commitable_paths,
4717 got_worktree_status_cb status_cb, void *status_arg,
4718 struct got_repository *repo)
4720 const struct got_error *err = NULL;
4721 struct got_tree_entry *new_te;
4723 struct got_object_id *id = NULL;
4728 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4729 got_path_is_root_dir(path_base_tree) ? "" : "/",
4731 return got_error_from_errno("asprintf");
4733 new_te = calloc(1, sizeof(*new_te));
4735 return got_error_from_errno("calloc");
4736 new_te->mode = S_IFDIR;
4738 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4739 sizeof(new_te->name)) {
4740 err = got_error(GOT_ERR_NO_SPACE);
4743 err = write_tree(&id, &nentries, NULL, subtree_path,
4744 commitable_paths, status_cb, status_arg, repo);
4749 memcpy(&new_te->id, id, sizeof(new_te->id));
4758 static const struct got_error *
4759 write_tree(struct got_object_id **new_tree_id, int *nentries,
4760 struct got_tree_object *base_tree, const char *path_base_tree,
4761 struct got_pathlist_head *commitable_paths,
4762 got_worktree_status_cb status_cb, void *status_arg,
4763 struct got_repository *repo)
4765 const struct got_error *err = NULL;
4766 struct got_pathlist_head paths;
4767 struct got_tree_entry *te, *new_te = NULL;
4768 struct got_pathlist_entry *pe;
4773 /* Insert, and recurse into, newly added entries first. */
4774 TAILQ_FOREACH(pe, commitable_paths, entry) {
4775 struct got_commitable *ct = pe->data;
4776 char *child_path = NULL, *slash;
4778 if ((ct->status != GOT_STATUS_ADD &&
4779 ct->staged_status != GOT_STATUS_ADD) ||
4780 (ct->flags & GOT_COMMITABLE_ADDED))
4783 if (!got_path_is_child(pe->path, path_base_tree,
4784 strlen(path_base_tree)))
4787 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4792 slash = strchr(child_path, '/');
4793 if (slash == NULL) {
4794 err = alloc_added_blob_tree_entry(&new_te, ct);
4797 err = report_ct_status(ct, status_cb, status_arg);
4800 ct->flags |= GOT_COMMITABLE_ADDED;
4801 err = insert_tree_entry(new_te, &paths);
4806 *slash = '\0'; /* trim trailing path components */
4807 if (base_tree == NULL ||
4808 got_object_tree_find_entry(base_tree, child_path)
4810 err = make_subtree_for_added_blob(&new_te,
4811 child_path, path_base_tree,
4812 commitable_paths, status_cb, status_arg,
4816 err = insert_tree_entry(new_te, &paths);
4825 int i, nbase_entries;
4826 /* Handle modified and deleted entries. */
4827 nbase_entries = got_object_tree_get_nentries(base_tree);
4828 for (i = 0; i < nbase_entries; i++) {
4829 struct got_commitable *ct = NULL;
4831 te = got_object_tree_get_entry(base_tree, i);
4832 if (got_object_tree_entry_is_submodule(te)) {
4833 /* Entry is a submodule; just copy it. */
4834 err = got_object_tree_entry_dup(&new_te, te);
4837 err = insert_tree_entry(new_te, &paths);
4844 if (S_ISDIR(te->mode)) {
4846 err = got_object_tree_entry_dup(&new_te, te);
4849 err = match_modified_subtree(&modified, te,
4850 path_base_tree, commitable_paths);
4853 /* Avoid recursion into unmodified subtrees. */
4855 struct got_object_id *new_id;
4857 err = write_subtree(&new_id,
4859 path_base_tree, commitable_paths,
4860 status_cb, status_arg, repo);
4863 if (nsubentries == 0) {
4864 /* All entries were deleted. */
4868 memcpy(&new_te->id, new_id,
4869 sizeof(new_te->id));
4872 err = insert_tree_entry(new_te, &paths);
4879 err = match_deleted_or_modified_ct(&ct, te,
4880 path_base_tree, commitable_paths);
4884 /* NB: Deleted entries get dropped here. */
4885 if (ct->status == GOT_STATUS_MODIFY ||
4886 ct->status == GOT_STATUS_MODE_CHANGE ||
4887 ct->staged_status == GOT_STATUS_MODIFY) {
4888 err = alloc_modified_blob_tree_entry(
4892 err = insert_tree_entry(new_te, &paths);
4897 err = report_ct_status(ct, status_cb,
4902 /* Entry is unchanged; just copy it. */
4903 err = got_object_tree_entry_dup(&new_te, te);
4906 err = insert_tree_entry(new_te, &paths);
4914 /* Write new list of entries; deleted entries have been dropped. */
4915 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4917 got_pathlist_free(&paths);
4921 static const struct got_error *
4922 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4923 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4924 int have_staged_files)
4926 const struct got_error *err = NULL;
4927 struct got_pathlist_entry *pe;
4929 TAILQ_FOREACH(pe, commitable_paths, entry) {
4930 struct got_fileindex_entry *ie;
4931 struct got_commitable *ct = pe->data;
4933 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4935 if (ct->status == GOT_STATUS_DELETE ||
4936 ct->staged_status == GOT_STATUS_DELETE) {
4937 got_fileindex_entry_remove(fileindex, ie);
4938 } else if (ct->staged_status == GOT_STATUS_ADD ||
4939 ct->staged_status == GOT_STATUS_MODIFY) {
4940 got_fileindex_entry_stage_set(ie,
4941 GOT_FILEIDX_STAGE_NONE);
4942 err = got_fileindex_entry_update(ie,
4943 ct->ondisk_path, ct->staged_blob_id->sha1,
4944 new_base_commit_id->sha1,
4945 !have_staged_files);
4947 err = got_fileindex_entry_update(ie,
4948 ct->ondisk_path, ct->blob_id->sha1,
4949 new_base_commit_id->sha1,
4950 !have_staged_files);
4952 err = got_fileindex_entry_alloc(&ie, pe->path);
4955 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4956 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4958 got_fileindex_entry_free(ie);
4961 err = got_fileindex_entry_add(fileindex, ie);
4963 got_fileindex_entry_free(ie);
4972 static const struct got_error *
4973 check_out_of_date(const char *in_repo_path, unsigned char status,
4974 unsigned char staged_status, struct got_object_id *base_blob_id,
4975 struct got_object_id *base_commit_id,
4976 struct got_object_id *head_commit_id, struct got_repository *repo,
4979 const struct got_error *err = NULL;
4980 struct got_object_id *id = NULL;
4982 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4983 /* Trivial case: base commit == head commit */
4984 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4987 * Ensure file content which local changes were based
4988 * on matches file content in the branch head.
4990 err = got_object_id_by_path(&id, repo, head_commit_id,
4993 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4994 err = got_error(ood_errcode);
4996 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4997 err = got_error(ood_errcode);
4999 /* Require that added files don't exist in the branch head. */
5000 err = got_object_id_by_path(&id, repo, head_commit_id,
5002 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5004 err = id ? got_error(ood_errcode) : NULL;
5011 const struct got_error *
5012 commit_worktree(struct got_object_id **new_commit_id,
5013 struct got_pathlist_head *commitable_paths,
5014 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5015 const char *author, const char *committer,
5016 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5017 got_worktree_status_cb status_cb, void *status_arg,
5018 struct got_repository *repo)
5020 const struct got_error *err = NULL, *unlockerr = NULL;
5021 struct got_pathlist_entry *pe;
5022 const char *head_ref_name = NULL;
5023 struct got_commit_object *head_commit = NULL;
5024 struct got_reference *head_ref2 = NULL;
5025 struct got_object_id *head_commit_id2 = NULL;
5026 struct got_tree_object *head_tree = NULL;
5027 struct got_object_id *new_tree_id = NULL;
5029 struct got_object_id_queue parent_ids;
5030 struct got_object_qid *pid = NULL;
5031 char *logmsg = NULL;
5033 *new_commit_id = NULL;
5035 SIMPLEQ_INIT(&parent_ids);
5037 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5041 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5045 if (commit_msg_cb != NULL) {
5046 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5051 if (logmsg == NULL || strlen(logmsg) == 0) {
5052 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5056 /* Create blobs from added and modified files and record their IDs. */
5057 TAILQ_FOREACH(pe, commitable_paths, entry) {
5058 struct got_commitable *ct = pe->data;
5061 /* Blobs for staged files already exist. */
5062 if (ct->staged_status == GOT_STATUS_ADD ||
5063 ct->staged_status == GOT_STATUS_MODIFY)
5066 if (ct->status != GOT_STATUS_ADD &&
5067 ct->status != GOT_STATUS_MODIFY &&
5068 ct->status != GOT_STATUS_MODE_CHANGE)
5071 if (asprintf(&ondisk_path, "%s/%s",
5072 worktree->root_path, pe->path) == -1) {
5073 err = got_error_from_errno("asprintf");
5076 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5083 * When comitting a symlink we convert "bad" symlinks (those
5084 * which point outside the work tree or into .got) to regular
5085 * files. This way, the post-commit work tree state matches
5086 * a fresh checkout of the tree which was committed.
5088 if (S_ISLNK(get_ct_file_mode(ct))) {
5089 struct got_blob_object *blob;
5090 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5096 err = install_symlink(worktree, ondisk_path, ct->path,
5097 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
5098 0, 0, repo, NULL, NULL);
5099 got_object_blob_close(blob);
5108 /* Recursively write new tree objects. */
5109 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5110 commitable_paths, status_cb, status_arg, repo);
5114 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5117 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5118 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5119 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5120 got_object_qid_free(pid);
5126 /* Check if a concurrent commit to our branch has occurred. */
5127 head_ref_name = got_worktree_get_head_ref_name(worktree);
5128 if (head_ref_name == NULL) {
5129 err = got_error_from_errno("got_worktree_get_head_ref_name");
5132 /* Lock the reference here to prevent concurrent modification. */
5133 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5136 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5139 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5140 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5143 /* Update branch head in repository. */
5144 err = got_ref_change_ref(head_ref2, *new_commit_id);
5147 err = got_ref_write(head_ref2, repo);
5151 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5155 err = ref_base_commit(worktree, repo);
5160 got_object_tree_close(head_tree);
5162 got_object_commit_close(head_commit);
5163 free(head_commit_id2);
5165 unlockerr = got_ref_unlock(head_ref2);
5166 if (unlockerr && err == NULL)
5168 got_ref_close(head_ref2);
5173 static const struct got_error *
5174 check_path_is_commitable(const char *path,
5175 struct got_pathlist_head *commitable_paths)
5177 struct got_pathlist_entry *cpe = NULL;
5178 size_t path_len = strlen(path);
5180 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5181 struct got_commitable *ct = cpe->data;
5182 const char *ct_path = ct->path;
5184 while (ct_path[0] == '/')
5187 if (strcmp(path, ct_path) == 0 ||
5188 got_path_is_child(ct_path, path, path_len))
5193 return got_error_path(path, GOT_ERR_BAD_PATH);
5198 static const struct got_error *
5199 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5201 int *have_staged_files = arg;
5203 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5204 *have_staged_files = 1;
5205 return got_error(GOT_ERR_CANCELLED);
5211 static const struct got_error *
5212 check_non_staged_files(struct got_fileindex *fileindex,
5213 struct got_pathlist_head *paths)
5215 struct got_pathlist_entry *pe;
5216 struct got_fileindex_entry *ie;
5218 TAILQ_FOREACH(pe, paths, entry) {
5219 if (pe->path[0] == '\0')
5221 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5223 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5224 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5225 return got_error_path(pe->path,
5226 GOT_ERR_FILE_NOT_STAGED);
5232 const struct got_error *
5233 got_worktree_commit(struct got_object_id **new_commit_id,
5234 struct got_worktree *worktree, struct got_pathlist_head *paths,
5235 const char *author, const char *committer,
5236 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5237 got_worktree_status_cb status_cb, void *status_arg,
5238 struct got_repository *repo)
5240 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5241 struct got_fileindex *fileindex = NULL;
5242 char *fileindex_path = NULL;
5243 struct got_pathlist_head commitable_paths;
5244 struct collect_commitables_arg cc_arg;
5245 struct got_pathlist_entry *pe;
5246 struct got_reference *head_ref = NULL;
5247 struct got_object_id *head_commit_id = NULL;
5248 int have_staged_files = 0;
5250 *new_commit_id = NULL;
5252 TAILQ_INIT(&commitable_paths);
5254 err = lock_worktree(worktree, LOCK_EX);
5258 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5262 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5266 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5270 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5271 &have_staged_files);
5272 if (err && err->code != GOT_ERR_CANCELLED)
5274 if (have_staged_files) {
5275 err = check_non_staged_files(fileindex, paths);
5280 cc_arg.commitable_paths = &commitable_paths;
5281 cc_arg.worktree = worktree;
5283 cc_arg.have_staged_files = have_staged_files;
5284 TAILQ_FOREACH(pe, paths, entry) {
5285 err = worktree_status(worktree, pe->path, fileindex, repo,
5286 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5291 if (TAILQ_EMPTY(&commitable_paths)) {
5292 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5296 TAILQ_FOREACH(pe, paths, entry) {
5297 err = check_path_is_commitable(pe->path, &commitable_paths);
5302 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5303 struct got_commitable *ct = pe->data;
5304 const char *ct_path = ct->in_repo_path;
5306 while (ct_path[0] == '/')
5308 err = check_out_of_date(ct_path, ct->status,
5309 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5310 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5316 err = commit_worktree(new_commit_id, &commitable_paths,
5317 head_commit_id, worktree, author, committer,
5318 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5322 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5323 fileindex, have_staged_files);
5324 sync_err = sync_fileindex(fileindex, fileindex_path);
5325 if (sync_err && err == NULL)
5329 got_fileindex_free(fileindex);
5330 free(fileindex_path);
5331 unlockerr = lock_worktree(worktree, LOCK_SH);
5332 if (unlockerr && err == NULL)
5334 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5335 struct got_commitable *ct = pe->data;
5336 free_commitable(ct);
5338 got_pathlist_free(&commitable_paths);
5343 got_commitable_get_path(struct got_commitable *ct)
5349 got_commitable_get_status(struct got_commitable *ct)
5354 struct check_rebase_ok_arg {
5355 struct got_worktree *worktree;
5356 struct got_repository *repo;
5359 static const struct got_error *
5360 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5362 const struct got_error *err = NULL;
5363 struct check_rebase_ok_arg *a = arg;
5364 unsigned char status;
5368 /* Reject rebase of a work tree with mixed base commits. */
5369 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5370 SHA1_DIGEST_LENGTH))
5371 return got_error(GOT_ERR_MIXED_COMMITS);
5373 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5375 return got_error_from_errno("asprintf");
5377 /* Reject rebase of a work tree with modified or staged files. */
5378 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5383 if (status != GOT_STATUS_NO_CHANGE)
5384 return got_error(GOT_ERR_MODIFIED);
5385 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5386 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5391 const struct got_error *
5392 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5393 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5394 struct got_worktree *worktree, struct got_reference *branch,
5395 struct got_repository *repo)
5397 const struct got_error *err = NULL;
5398 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5399 char *branch_ref_name = NULL;
5400 char *fileindex_path = NULL;
5401 struct check_rebase_ok_arg ok_arg;
5402 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5403 struct got_object_id *wt_branch_tip = NULL;
5405 *new_base_branch_ref = NULL;
5409 err = lock_worktree(worktree, LOCK_EX);
5413 err = open_fileindex(fileindex, &fileindex_path, worktree);
5417 ok_arg.worktree = worktree;
5419 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5424 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5428 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5432 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5436 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5441 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5444 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5445 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5449 err = got_ref_alloc_symref(new_base_branch_ref,
5450 new_base_branch_ref_name, wt_branch);
5453 err = got_ref_write(*new_base_branch_ref, repo);
5457 /* TODO Lock original branch's ref while rebasing? */
5459 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5463 err = got_ref_write(branch_ref, repo);
5467 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5468 worktree->base_commit_id);
5471 err = got_ref_write(*tmp_branch, repo);
5475 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5479 free(fileindex_path);
5480 free(tmp_branch_name);
5481 free(new_base_branch_ref_name);
5482 free(branch_ref_name);
5484 got_ref_close(branch_ref);
5486 got_ref_close(wt_branch);
5487 free(wt_branch_tip);
5489 if (*new_base_branch_ref) {
5490 got_ref_close(*new_base_branch_ref);
5491 *new_base_branch_ref = NULL;
5494 got_ref_close(*tmp_branch);
5498 got_fileindex_free(*fileindex);
5501 lock_worktree(worktree, LOCK_SH);
5506 const struct got_error *
5507 got_worktree_rebase_continue(struct got_object_id **commit_id,
5508 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5509 struct got_reference **branch, struct got_fileindex **fileindex,
5510 struct got_worktree *worktree, struct got_repository *repo)
5512 const struct got_error *err;
5513 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5514 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5515 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5516 char *fileindex_path = NULL;
5517 int have_staged_files = 0;
5520 *new_base_branch = NULL;
5525 err = lock_worktree(worktree, LOCK_EX);
5529 err = open_fileindex(fileindex, &fileindex_path, worktree);
5533 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5534 &have_staged_files);
5535 if (err && err->code != GOT_ERR_CANCELLED)
5537 if (have_staged_files) {
5538 err = got_error(GOT_ERR_STAGED_PATHS);
5542 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5546 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5550 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5554 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5558 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5562 err = got_ref_open(branch, repo,
5563 got_ref_get_symref_target(branch_ref), 0);
5567 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5571 err = got_ref_resolve(commit_id, repo, commit_ref);
5575 err = got_ref_open(new_base_branch, repo,
5576 new_base_branch_ref_name, 0);
5580 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5584 free(commit_ref_name);
5585 free(branch_ref_name);
5586 free(fileindex_path);
5588 got_ref_close(commit_ref);
5590 got_ref_close(branch_ref);
5595 got_ref_close(*tmp_branch);
5598 if (*new_base_branch) {
5599 got_ref_close(*new_base_branch);
5600 *new_base_branch = NULL;
5603 got_ref_close(*branch);
5607 got_fileindex_free(*fileindex);
5610 lock_worktree(worktree, LOCK_SH);
5615 const struct got_error *
5616 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5618 const struct got_error *err;
5619 char *tmp_branch_name = NULL;
5621 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5625 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5626 free(tmp_branch_name);
5630 static const struct got_error *
5631 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5632 char **logmsg, void *arg)
5638 static const struct got_error *
5639 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5640 const char *path, struct got_object_id *blob_id,
5641 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5642 int dirfd, const char *de_name)
5647 struct collect_merged_paths_arg {
5648 got_worktree_checkout_cb progress_cb;
5650 struct got_pathlist_head *merged_paths;
5653 static const struct got_error *
5654 collect_merged_paths(void *arg, unsigned char status, const char *path)
5656 const struct got_error *err;
5657 struct collect_merged_paths_arg *a = arg;
5659 struct got_pathlist_entry *new;
5661 err = (*a->progress_cb)(a->progress_arg, status, path);
5665 if (status != GOT_STATUS_MERGE &&
5666 status != GOT_STATUS_ADD &&
5667 status != GOT_STATUS_DELETE &&
5668 status != GOT_STATUS_CONFLICT)
5673 return got_error_from_errno("strdup");
5675 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5676 if (err || new == NULL)
5682 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5684 struct got_pathlist_entry *pe;
5686 TAILQ_FOREACH(pe, merged_paths, entry)
5687 free((char *)pe->path);
5689 got_pathlist_free(merged_paths);
5692 static const struct got_error *
5693 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5694 int is_rebase, struct got_repository *repo)
5696 const struct got_error *err;
5697 struct got_reference *commit_ref = NULL;
5699 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5701 if (err->code != GOT_ERR_NOT_REF)
5703 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5706 err = got_ref_write(commit_ref, repo);
5709 } else if (is_rebase) {
5710 struct got_object_id *stored_id;
5713 err = got_ref_resolve(&stored_id, repo, commit_ref);
5716 cmp = got_object_id_cmp(commit_id, stored_id);
5719 err = got_error(GOT_ERR_REBASE_COMMITID);
5725 got_ref_close(commit_ref);
5729 static const struct got_error *
5730 rebase_merge_files(struct got_pathlist_head *merged_paths,
5731 const char *commit_ref_name, struct got_worktree *worktree,
5732 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5733 struct got_object_id *commit_id, struct got_repository *repo,
5734 got_worktree_checkout_cb progress_cb, void *progress_arg,
5735 got_cancel_cb cancel_cb, void *cancel_arg)
5737 const struct got_error *err;
5738 struct got_reference *commit_ref = NULL;
5739 struct collect_merged_paths_arg cmp_arg;
5740 char *fileindex_path;
5742 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5744 err = get_fileindex_path(&fileindex_path, worktree);
5748 cmp_arg.progress_cb = progress_cb;
5749 cmp_arg.progress_arg = progress_arg;
5750 cmp_arg.merged_paths = merged_paths;
5751 err = merge_files(worktree, fileindex, fileindex_path,
5752 parent_commit_id, commit_id, repo, collect_merged_paths,
5753 &cmp_arg, cancel_cb, cancel_arg);
5755 got_ref_close(commit_ref);
5759 const struct got_error *
5760 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5761 struct got_worktree *worktree, struct got_fileindex *fileindex,
5762 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5763 struct got_repository *repo,
5764 got_worktree_checkout_cb progress_cb, void *progress_arg,
5765 got_cancel_cb cancel_cb, void *cancel_arg)
5767 const struct got_error *err;
5768 char *commit_ref_name;
5770 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5774 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5778 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5779 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5780 progress_arg, cancel_cb, cancel_arg);
5782 free(commit_ref_name);
5786 const struct got_error *
5787 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5788 struct got_worktree *worktree, struct got_fileindex *fileindex,
5789 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5790 struct got_repository *repo,
5791 got_worktree_checkout_cb progress_cb, void *progress_arg,
5792 got_cancel_cb cancel_cb, void *cancel_arg)
5794 const struct got_error *err;
5795 char *commit_ref_name;
5797 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5801 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5805 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5806 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5807 progress_arg, cancel_cb, cancel_arg);
5809 free(commit_ref_name);
5813 static const struct got_error *
5814 rebase_commit(struct got_object_id **new_commit_id,
5815 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5816 struct got_worktree *worktree, struct got_fileindex *fileindex,
5817 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5818 const char *new_logmsg, struct got_repository *repo)
5820 const struct got_error *err, *sync_err;
5821 struct got_pathlist_head commitable_paths;
5822 struct collect_commitables_arg cc_arg;
5823 char *fileindex_path = NULL;
5824 struct got_reference *head_ref = NULL;
5825 struct got_object_id *head_commit_id = NULL;
5826 char *logmsg = NULL;
5828 TAILQ_INIT(&commitable_paths);
5829 *new_commit_id = NULL;
5831 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5833 err = get_fileindex_path(&fileindex_path, worktree);
5837 cc_arg.commitable_paths = &commitable_paths;
5838 cc_arg.worktree = worktree;
5840 cc_arg.have_staged_files = 0;
5842 * If possible get the status of individual files directly to
5843 * avoid crawling the entire work tree once per rebased commit.
5844 * TODO: Ideally, merged_paths would contain a list of commitables
5845 * we could use so we could skip worktree_status() entirely.
5848 struct got_pathlist_entry *pe;
5849 TAILQ_FOREACH(pe, merged_paths, entry) {
5850 err = worktree_status(worktree, pe->path, fileindex,
5851 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5857 err = worktree_status(worktree, "", fileindex, repo,
5858 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5863 if (TAILQ_EMPTY(&commitable_paths)) {
5864 /* No-op change; commit will be elided. */
5865 err = got_ref_delete(commit_ref, repo);
5868 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5872 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5876 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5881 logmsg = strdup(new_logmsg);
5882 if (logmsg == NULL) {
5883 err = got_error_from_errno("strdup");
5887 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5892 /* NB: commit_worktree will call free(logmsg) */
5893 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5894 worktree, got_object_commit_get_author(orig_commit),
5895 got_object_commit_get_committer(orig_commit),
5896 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5900 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5904 err = got_ref_delete(commit_ref, repo);
5908 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5910 sync_err = sync_fileindex(fileindex, fileindex_path);
5911 if (sync_err && err == NULL)
5914 free(fileindex_path);
5915 free(head_commit_id);
5917 got_ref_close(head_ref);
5919 free(*new_commit_id);
5920 *new_commit_id = NULL;
5925 const struct got_error *
5926 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5927 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5928 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5929 struct got_commit_object *orig_commit,
5930 struct got_object_id *orig_commit_id, struct got_repository *repo)
5932 const struct got_error *err;
5933 char *commit_ref_name;
5934 struct got_reference *commit_ref = NULL;
5935 struct got_object_id *commit_id = NULL;
5937 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5941 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5944 err = got_ref_resolve(&commit_id, repo, commit_ref);
5947 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5948 err = got_error(GOT_ERR_REBASE_COMMITID);
5952 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5953 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5956 got_ref_close(commit_ref);
5957 free(commit_ref_name);
5962 const struct got_error *
5963 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5964 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5965 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5966 struct got_commit_object *orig_commit,
5967 struct got_object_id *orig_commit_id, const char *new_logmsg,
5968 struct got_repository *repo)
5970 const struct got_error *err;
5971 char *commit_ref_name;
5972 struct got_reference *commit_ref = NULL;
5974 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5978 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5982 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5983 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5986 got_ref_close(commit_ref);
5987 free(commit_ref_name);
5991 const struct got_error *
5992 got_worktree_rebase_postpone(struct got_worktree *worktree,
5993 struct got_fileindex *fileindex)
5996 got_fileindex_free(fileindex);
5997 return lock_worktree(worktree, LOCK_SH);
6000 static const struct got_error *
6001 delete_ref(const char *name, struct got_repository *repo)
6003 const struct got_error *err;
6004 struct got_reference *ref;
6006 err = got_ref_open(&ref, repo, name, 0);
6008 if (err->code == GOT_ERR_NOT_REF)
6013 err = got_ref_delete(ref, repo);
6018 static const struct got_error *
6019 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6021 const struct got_error *err;
6022 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6023 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6025 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6028 err = delete_ref(tmp_branch_name, repo);
6032 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6035 err = delete_ref(new_base_branch_ref_name, repo);
6039 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6042 err = delete_ref(branch_ref_name, repo);
6046 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6049 err = delete_ref(commit_ref_name, repo);
6054 free(tmp_branch_name);
6055 free(new_base_branch_ref_name);
6056 free(branch_ref_name);
6057 free(commit_ref_name);
6061 const struct got_error *
6062 got_worktree_rebase_complete(struct got_worktree *worktree,
6063 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6064 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6065 struct got_repository *repo)
6067 const struct got_error *err, *unlockerr;
6068 struct got_object_id *new_head_commit_id = NULL;
6070 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6074 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6078 err = got_ref_write(rebased_branch, repo);
6082 err = got_worktree_set_head_ref(worktree, rebased_branch);
6086 err = delete_rebase_refs(worktree, repo);
6089 got_fileindex_free(fileindex);
6090 free(new_head_commit_id);
6091 unlockerr = lock_worktree(worktree, LOCK_SH);
6092 if (unlockerr && err == NULL)
6097 const struct got_error *
6098 got_worktree_rebase_abort(struct got_worktree *worktree,
6099 struct got_fileindex *fileindex, struct got_repository *repo,
6100 struct got_reference *new_base_branch,
6101 got_worktree_checkout_cb progress_cb, void *progress_arg)
6103 const struct got_error *err, *unlockerr, *sync_err;
6104 struct got_reference *resolved = NULL;
6105 struct got_object_id *commit_id = NULL;
6106 char *fileindex_path = NULL;
6107 struct revert_file_args rfa;
6108 struct got_object_id *tree_id = NULL;
6110 err = lock_worktree(worktree, LOCK_EX);
6114 err = got_ref_open(&resolved, repo,
6115 got_ref_get_symref_target(new_base_branch), 0);
6119 err = got_worktree_set_head_ref(worktree, resolved);
6124 * XXX commits to the base branch could have happened while
6125 * we were busy rebasing; should we store the original commit ID
6126 * when rebase begins and read it back here?
6128 err = got_ref_resolve(&commit_id, repo, resolved);
6132 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6136 err = got_object_id_by_path(&tree_id, repo,
6137 worktree->base_commit_id, worktree->path_prefix);
6141 err = delete_rebase_refs(worktree, repo);
6145 err = get_fileindex_path(&fileindex_path, worktree);
6149 rfa.worktree = worktree;
6150 rfa.fileindex = fileindex;
6151 rfa.progress_cb = progress_cb;
6152 rfa.progress_arg = progress_arg;
6153 rfa.patch_cb = NULL;
6154 rfa.patch_arg = NULL;
6156 err = worktree_status(worktree, "", fileindex, repo,
6157 revert_file, &rfa, NULL, NULL, 0, 0);
6161 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6162 repo, progress_cb, progress_arg, NULL, NULL);
6164 sync_err = sync_fileindex(fileindex, fileindex_path);
6165 if (sync_err && err == NULL)
6168 got_ref_close(resolved);
6172 got_fileindex_free(fileindex);
6173 free(fileindex_path);
6175 unlockerr = lock_worktree(worktree, LOCK_SH);
6176 if (unlockerr && err == NULL)
6181 const struct got_error *
6182 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6183 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6184 struct got_fileindex **fileindex, struct got_worktree *worktree,
6185 struct got_repository *repo)
6187 const struct got_error *err = NULL;
6188 char *tmp_branch_name = NULL;
6189 char *branch_ref_name = NULL;
6190 char *base_commit_ref_name = NULL;
6191 char *fileindex_path = NULL;
6192 struct check_rebase_ok_arg ok_arg;
6193 struct got_reference *wt_branch = NULL;
6194 struct got_reference *base_commit_ref = NULL;
6198 *base_commit_id = NULL;
6201 err = lock_worktree(worktree, LOCK_EX);
6205 err = open_fileindex(fileindex, &fileindex_path, worktree);
6209 ok_arg.worktree = worktree;
6211 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6216 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6220 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6224 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6229 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6234 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6238 err = got_ref_write(*branch_ref, repo);
6242 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6243 worktree->base_commit_id);
6246 err = got_ref_write(base_commit_ref, repo);
6249 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6250 if (*base_commit_id == NULL) {
6251 err = got_error_from_errno("got_object_id_dup");
6255 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6256 worktree->base_commit_id);
6259 err = got_ref_write(*tmp_branch, repo);
6263 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6267 free(fileindex_path);
6268 free(tmp_branch_name);
6269 free(branch_ref_name);
6270 free(base_commit_ref_name);
6272 got_ref_close(wt_branch);
6275 got_ref_close(*branch_ref);
6279 got_ref_close(*tmp_branch);
6282 free(*base_commit_id);
6284 got_fileindex_free(*fileindex);
6287 lock_worktree(worktree, LOCK_SH);
6292 const struct got_error *
6293 got_worktree_histedit_postpone(struct got_worktree *worktree,
6294 struct got_fileindex *fileindex)
6297 got_fileindex_free(fileindex);
6298 return lock_worktree(worktree, LOCK_SH);
6301 const struct got_error *
6302 got_worktree_histedit_in_progress(int *in_progress,
6303 struct got_worktree *worktree)
6305 const struct got_error *err;
6306 char *tmp_branch_name = NULL;
6308 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6312 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6313 free(tmp_branch_name);
6317 const struct got_error *
6318 got_worktree_histedit_continue(struct got_object_id **commit_id,
6319 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6320 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6321 struct got_worktree *worktree, struct got_repository *repo)
6323 const struct got_error *err;
6324 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6325 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6326 struct got_reference *commit_ref = NULL;
6327 struct got_reference *base_commit_ref = NULL;
6328 char *fileindex_path = NULL;
6329 int have_staged_files = 0;
6333 *base_commit_id = NULL;
6336 err = lock_worktree(worktree, LOCK_EX);
6340 err = open_fileindex(fileindex, &fileindex_path, worktree);
6344 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6345 &have_staged_files);
6346 if (err && err->code != GOT_ERR_CANCELLED)
6348 if (have_staged_files) {
6349 err = got_error(GOT_ERR_STAGED_PATHS);
6353 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6357 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6361 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6365 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6370 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6374 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6377 err = got_ref_resolve(commit_id, repo, commit_ref);
6381 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6384 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6388 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6392 free(commit_ref_name);
6393 free(branch_ref_name);
6394 free(fileindex_path);
6396 got_ref_close(commit_ref);
6397 if (base_commit_ref)
6398 got_ref_close(base_commit_ref);
6402 free(*base_commit_id);
6403 *base_commit_id = NULL;
6405 got_ref_close(*tmp_branch);
6409 got_fileindex_free(*fileindex);
6412 lock_worktree(worktree, LOCK_EX);
6417 static const struct got_error *
6418 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6420 const struct got_error *err;
6421 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6422 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6424 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6427 err = delete_ref(tmp_branch_name, repo);
6431 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6435 err = delete_ref(base_commit_ref_name, repo);
6439 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6442 err = delete_ref(branch_ref_name, repo);
6446 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6449 err = delete_ref(commit_ref_name, repo);
6453 free(tmp_branch_name);
6454 free(base_commit_ref_name);
6455 free(branch_ref_name);
6456 free(commit_ref_name);
6460 const struct got_error *
6461 got_worktree_histedit_abort(struct got_worktree *worktree,
6462 struct got_fileindex *fileindex, struct got_repository *repo,
6463 struct got_reference *branch, struct got_object_id *base_commit_id,
6464 got_worktree_checkout_cb progress_cb, void *progress_arg)
6466 const struct got_error *err, *unlockerr, *sync_err;
6467 struct got_reference *resolved = NULL;
6468 char *fileindex_path = NULL;
6469 struct got_object_id *tree_id = NULL;
6470 struct revert_file_args rfa;
6472 err = lock_worktree(worktree, LOCK_EX);
6476 err = got_ref_open(&resolved, repo,
6477 got_ref_get_symref_target(branch), 0);
6481 err = got_worktree_set_head_ref(worktree, resolved);
6485 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6489 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6490 worktree->path_prefix);
6494 err = delete_histedit_refs(worktree, repo);
6498 err = get_fileindex_path(&fileindex_path, worktree);
6502 rfa.worktree = worktree;
6503 rfa.fileindex = fileindex;
6504 rfa.progress_cb = progress_cb;
6505 rfa.progress_arg = progress_arg;
6506 rfa.patch_cb = NULL;
6507 rfa.patch_arg = NULL;
6509 err = worktree_status(worktree, "", fileindex, repo,
6510 revert_file, &rfa, NULL, NULL, 0, 0);
6514 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6515 repo, progress_cb, progress_arg, NULL, NULL);
6517 sync_err = sync_fileindex(fileindex, fileindex_path);
6518 if (sync_err && err == NULL)
6521 got_ref_close(resolved);
6523 free(fileindex_path);
6525 unlockerr = lock_worktree(worktree, LOCK_SH);
6526 if (unlockerr && err == NULL)
6531 const struct got_error *
6532 got_worktree_histedit_complete(struct got_worktree *worktree,
6533 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6534 struct got_reference *edited_branch, struct got_repository *repo)
6536 const struct got_error *err, *unlockerr;
6537 struct got_object_id *new_head_commit_id = NULL;
6538 struct got_reference *resolved = NULL;
6540 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6544 err = got_ref_open(&resolved, repo,
6545 got_ref_get_symref_target(edited_branch), 0);
6549 err = got_ref_change_ref(resolved, new_head_commit_id);
6553 err = got_ref_write(resolved, repo);
6557 err = got_worktree_set_head_ref(worktree, resolved);
6561 err = delete_histedit_refs(worktree, repo);
6564 got_fileindex_free(fileindex);
6565 free(new_head_commit_id);
6566 unlockerr = lock_worktree(worktree, LOCK_SH);
6567 if (unlockerr && err == NULL)
6572 const struct got_error *
6573 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6574 struct got_object_id *commit_id, struct got_repository *repo)
6576 const struct got_error *err;
6577 char *commit_ref_name;
6579 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6583 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6587 err = delete_ref(commit_ref_name, repo);
6589 free(commit_ref_name);
6593 const struct got_error *
6594 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6595 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6596 struct got_worktree *worktree, const char *refname,
6597 struct got_repository *repo)
6599 const struct got_error *err = NULL;
6600 char *fileindex_path = NULL;
6601 struct check_rebase_ok_arg ok_arg;
6605 *base_branch_ref = NULL;
6607 err = lock_worktree(worktree, LOCK_EX);
6611 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6612 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6613 "cannot integrate a branch into itself; "
6614 "update -b or different branch name required");
6618 err = open_fileindex(fileindex, &fileindex_path, worktree);
6622 /* Preconditions are the same as for rebase. */
6623 ok_arg.worktree = worktree;
6625 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6630 err = got_ref_open(branch_ref, repo, refname, 1);
6634 err = got_ref_open(base_branch_ref, repo,
6635 got_worktree_get_head_ref_name(worktree), 1);
6639 got_ref_close(*branch_ref);
6642 if (*base_branch_ref) {
6643 got_ref_close(*base_branch_ref);
6644 *base_branch_ref = NULL;
6647 got_fileindex_free(*fileindex);
6650 lock_worktree(worktree, LOCK_SH);
6655 const struct got_error *
6656 got_worktree_integrate_continue(struct got_worktree *worktree,
6657 struct got_fileindex *fileindex, struct got_repository *repo,
6658 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6659 got_worktree_checkout_cb progress_cb, void *progress_arg,
6660 got_cancel_cb cancel_cb, void *cancel_arg)
6662 const struct got_error *err = NULL, *sync_err, *unlockerr;
6663 char *fileindex_path = NULL;
6664 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6666 err = get_fileindex_path(&fileindex_path, worktree);
6670 err = got_ref_resolve(&commit_id, repo, branch_ref);
6674 err = got_object_id_by_path(&tree_id, repo, commit_id,
6675 worktree->path_prefix);
6679 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6683 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6684 progress_cb, progress_arg, cancel_cb, cancel_arg);
6688 err = got_ref_change_ref(base_branch_ref, commit_id);
6692 err = got_ref_write(base_branch_ref, repo);
6694 sync_err = sync_fileindex(fileindex, fileindex_path);
6695 if (sync_err && err == NULL)
6699 unlockerr = got_ref_unlock(branch_ref);
6700 if (unlockerr && err == NULL)
6702 got_ref_close(branch_ref);
6704 unlockerr = got_ref_unlock(base_branch_ref);
6705 if (unlockerr && err == NULL)
6707 got_ref_close(base_branch_ref);
6709 got_fileindex_free(fileindex);
6710 free(fileindex_path);
6713 unlockerr = lock_worktree(worktree, LOCK_SH);
6714 if (unlockerr && err == NULL)
6719 const struct got_error *
6720 got_worktree_integrate_abort(struct got_worktree *worktree,
6721 struct got_fileindex *fileindex, struct got_repository *repo,
6722 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6724 const struct got_error *err = NULL, *unlockerr = NULL;
6726 got_fileindex_free(fileindex);
6728 err = lock_worktree(worktree, LOCK_SH);
6730 unlockerr = got_ref_unlock(branch_ref);
6731 if (unlockerr && err == NULL)
6733 got_ref_close(branch_ref);
6735 unlockerr = got_ref_unlock(base_branch_ref);
6736 if (unlockerr && err == NULL)
6738 got_ref_close(base_branch_ref);
6743 struct check_stage_ok_arg {
6744 struct got_object_id *head_commit_id;
6745 struct got_worktree *worktree;
6746 struct got_fileindex *fileindex;
6747 struct got_repository *repo;
6751 const struct got_error *
6752 check_stage_ok(void *arg, unsigned char status,
6753 unsigned char staged_status, const char *relpath,
6754 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6755 struct got_object_id *commit_id, int dirfd, const char *de_name)
6757 struct check_stage_ok_arg *a = arg;
6758 const struct got_error *err = NULL;
6759 struct got_fileindex_entry *ie;
6760 struct got_object_id base_commit_id;
6761 struct got_object_id *base_commit_idp = NULL;
6762 char *in_repo_path = NULL, *p;
6764 if (status == GOT_STATUS_UNVERSIONED ||
6765 status == GOT_STATUS_NO_CHANGE)
6767 if (status == GOT_STATUS_NONEXISTENT)
6768 return got_error_set_errno(ENOENT, relpath);
6770 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6772 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6774 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6775 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6777 return got_error_from_errno("asprintf");
6779 if (got_fileindex_entry_has_commit(ie)) {
6780 memcpy(base_commit_id.sha1, ie->commit_sha1,
6781 SHA1_DIGEST_LENGTH);
6782 base_commit_idp = &base_commit_id;
6785 if (status == GOT_STATUS_CONFLICT) {
6786 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6788 } else if (status != GOT_STATUS_ADD &&
6789 status != GOT_STATUS_MODIFY &&
6790 status != GOT_STATUS_DELETE) {
6791 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6795 a->have_changes = 1;
6800 err = check_out_of_date(p, status, staged_status,
6801 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6802 GOT_ERR_STAGE_OUT_OF_DATE);
6808 struct stage_path_arg {
6809 struct got_worktree *worktree;
6810 struct got_fileindex *fileindex;
6811 struct got_repository *repo;
6812 got_worktree_status_cb status_cb;
6814 got_worktree_patch_cb patch_cb;
6816 int staged_something;
6819 static const struct got_error *
6820 stage_path(void *arg, unsigned char status,
6821 unsigned char staged_status, const char *relpath,
6822 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6823 struct got_object_id *commit_id, int dirfd, const char *de_name)
6825 struct stage_path_arg *a = arg;
6826 const struct got_error *err = NULL;
6827 struct got_fileindex_entry *ie;
6828 char *ondisk_path = NULL, *path_content = NULL;
6830 struct got_object_id *new_staged_blob_id = NULL;
6832 if (status == GOT_STATUS_UNVERSIONED)
6835 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6837 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6839 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6841 return got_error_from_errno("asprintf");
6844 case GOT_STATUS_ADD:
6845 case GOT_STATUS_MODIFY:
6847 if (status == GOT_STATUS_ADD) {
6848 int choice = GOT_PATCH_CHOICE_NONE;
6849 err = (*a->patch_cb)(&choice, a->patch_arg,
6850 status, ie->path, NULL, 1, 1);
6853 if (choice != GOT_PATCH_CHOICE_YES)
6856 err = create_patched_content(&path_content, 0,
6857 staged_blob_id ? staged_blob_id : blob_id,
6858 ondisk_path, dirfd, de_name, ie->path,
6859 a->repo, a->patch_cb, a->patch_arg);
6860 if (err || path_content == NULL)
6864 err = got_object_blob_create(&new_staged_blob_id,
6865 path_content ? path_content : ondisk_path, a->repo);
6868 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6869 SHA1_DIGEST_LENGTH);
6870 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6871 stage = GOT_FILEIDX_STAGE_ADD;
6873 stage = GOT_FILEIDX_STAGE_MODIFY;
6874 got_fileindex_entry_stage_set(ie, stage);
6875 a->staged_something = 1;
6876 if (a->status_cb == NULL)
6878 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6879 get_staged_status(ie), relpath, blob_id,
6880 new_staged_blob_id, NULL, dirfd, de_name);
6882 case GOT_STATUS_DELETE:
6883 if (staged_status == GOT_STATUS_DELETE)
6886 int choice = GOT_PATCH_CHOICE_NONE;
6887 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6888 ie->path, NULL, 1, 1);
6891 if (choice == GOT_PATCH_CHOICE_NO)
6893 if (choice != GOT_PATCH_CHOICE_YES) {
6894 err = got_error(GOT_ERR_PATCH_CHOICE);
6898 stage = GOT_FILEIDX_STAGE_DELETE;
6899 got_fileindex_entry_stage_set(ie, stage);
6900 a->staged_something = 1;
6901 if (a->status_cb == NULL)
6903 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6904 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6907 case GOT_STATUS_NO_CHANGE:
6909 case GOT_STATUS_CONFLICT:
6910 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6912 case GOT_STATUS_NONEXISTENT:
6913 err = got_error_set_errno(ENOENT, relpath);
6916 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6920 if (path_content && unlink(path_content) == -1 && err == NULL)
6921 err = got_error_from_errno2("unlink", path_content);
6924 free(new_staged_blob_id);
6928 const struct got_error *
6929 got_worktree_stage(struct got_worktree *worktree,
6930 struct got_pathlist_head *paths,
6931 got_worktree_status_cb status_cb, void *status_arg,
6932 got_worktree_patch_cb patch_cb, void *patch_arg,
6933 struct got_repository *repo)
6935 const struct got_error *err = NULL, *sync_err, *unlockerr;
6936 struct got_pathlist_entry *pe;
6937 struct got_fileindex *fileindex = NULL;
6938 char *fileindex_path = NULL;
6939 struct got_reference *head_ref = NULL;
6940 struct got_object_id *head_commit_id = NULL;
6941 struct check_stage_ok_arg oka;
6942 struct stage_path_arg spa;
6944 err = lock_worktree(worktree, LOCK_EX);
6948 err = got_ref_open(&head_ref, repo,
6949 got_worktree_get_head_ref_name(worktree), 0);
6952 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6955 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6959 /* Check pre-conditions before staging anything. */
6960 oka.head_commit_id = head_commit_id;
6961 oka.worktree = worktree;
6962 oka.fileindex = fileindex;
6964 oka.have_changes = 0;
6965 TAILQ_FOREACH(pe, paths, entry) {
6966 err = worktree_status(worktree, pe->path, fileindex, repo,
6967 check_stage_ok, &oka, NULL, NULL, 0, 0);
6971 if (!oka.have_changes) {
6972 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6976 spa.worktree = worktree;
6977 spa.fileindex = fileindex;
6979 spa.patch_cb = patch_cb;
6980 spa.patch_arg = patch_arg;
6981 spa.status_cb = status_cb;
6982 spa.status_arg = status_arg;
6983 spa.staged_something = 0;
6984 TAILQ_FOREACH(pe, paths, entry) {
6985 err = worktree_status(worktree, pe->path, fileindex, repo,
6986 stage_path, &spa, NULL, NULL, 0, 0);
6990 if (!spa.staged_something) {
6991 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6995 sync_err = sync_fileindex(fileindex, fileindex_path);
6996 if (sync_err && err == NULL)
7000 got_ref_close(head_ref);
7001 free(head_commit_id);
7002 free(fileindex_path);
7004 got_fileindex_free(fileindex);
7005 unlockerr = lock_worktree(worktree, LOCK_SH);
7006 if (unlockerr && err == NULL)
7011 struct unstage_path_arg {
7012 struct got_worktree *worktree;
7013 struct got_fileindex *fileindex;
7014 struct got_repository *repo;
7015 got_worktree_checkout_cb progress_cb;
7017 got_worktree_patch_cb patch_cb;
7021 static const struct got_error *
7022 create_unstaged_content(char **path_unstaged_content,
7023 char **path_new_staged_content, struct got_object_id *blob_id,
7024 struct got_object_id *staged_blob_id, const char *relpath,
7025 struct got_repository *repo,
7026 got_worktree_patch_cb patch_cb, void *patch_arg)
7028 const struct got_error *err;
7029 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7030 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7031 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7032 struct stat sb1, sb2;
7033 struct got_diff_changes *changes = NULL;
7034 struct got_diff_state *ds = NULL;
7035 struct got_diff_args *args = NULL;
7036 struct got_diff_change *change;
7037 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7038 int have_content = 0, have_rejected_content = 0;
7040 *path_unstaged_content = NULL;
7041 *path_new_staged_content = NULL;
7043 err = got_object_id_str(&label1, blob_id);
7046 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7050 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7054 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7058 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7062 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7066 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7070 if (stat(path1, &sb1) == -1) {
7071 err = got_error_from_errno2("stat", path1);
7075 if (stat(path2, &sb2) == -1) {
7076 err = got_error_from_errno2("stat", path2);
7080 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7081 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7085 err = got_opentemp_named(path_unstaged_content, &outfile,
7086 "got-unstaged-content");
7089 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7090 "got-new-staged-content");
7094 if (fseek(f1, 0L, SEEK_SET) == -1) {
7095 err = got_ferror(f1, GOT_ERR_IO);
7098 if (fseek(f2, 0L, SEEK_SET) == -1) {
7099 err = got_ferror(f2, GOT_ERR_IO);
7102 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7104 err = apply_or_reject_change(&choice, change, ++n,
7105 changes->nchanges, ds, args, diff_flags, relpath,
7106 f1, f2, &line_cur1, &line_cur2,
7107 outfile, rejectfile, patch_cb, patch_arg);
7110 if (choice == GOT_PATCH_CHOICE_YES)
7113 have_rejected_content = 1;
7114 if (choice == GOT_PATCH_CHOICE_QUIT)
7117 if (have_content || have_rejected_content)
7118 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7119 outfile, rejectfile);
7123 got_object_blob_close(blob);
7125 got_object_blob_close(staged_blob);
7126 if (f1 && fclose(f1) == EOF && err == NULL)
7127 err = got_error_from_errno2("fclose", path1);
7128 if (f2 && fclose(f2) == EOF && err == NULL)
7129 err = got_error_from_errno2("fclose", path2);
7130 if (outfile && fclose(outfile) == EOF && err == NULL)
7131 err = got_error_from_errno2("fclose", *path_unstaged_content);
7132 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7133 err = got_error_from_errno2("fclose", *path_new_staged_content);
7134 if (path1 && unlink(path1) == -1 && err == NULL)
7135 err = got_error_from_errno2("unlink", path1);
7136 if (path2 && unlink(path2) == -1 && err == NULL)
7137 err = got_error_from_errno2("unlink", path2);
7138 if (err || !have_content) {
7139 if (*path_unstaged_content &&
7140 unlink(*path_unstaged_content) == -1 && err == NULL)
7141 err = got_error_from_errno2("unlink",
7142 *path_unstaged_content);
7143 free(*path_unstaged_content);
7144 *path_unstaged_content = NULL;
7146 if (err || !have_rejected_content) {
7147 if (*path_new_staged_content &&
7148 unlink(*path_new_staged_content) == -1 && err == NULL)
7149 err = got_error_from_errno2("unlink",
7150 *path_new_staged_content);
7151 free(*path_new_staged_content);
7152 *path_new_staged_content = NULL;
7156 got_diff_state_free(ds);
7160 got_diff_free_changes(changes);
7166 static const struct got_error *
7167 unstage_path(void *arg, unsigned char status,
7168 unsigned char staged_status, const char *relpath,
7169 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7170 struct got_object_id *commit_id, int dirfd, const char *de_name)
7172 const struct got_error *err = NULL;
7173 struct unstage_path_arg *a = arg;
7174 struct got_fileindex_entry *ie;
7175 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7176 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7177 char *path_new_staged_content = NULL;
7178 char *id_str = NULL, *label_orig = NULL;
7179 int local_changes_subsumed;
7182 if (staged_status != GOT_STATUS_ADD &&
7183 staged_status != GOT_STATUS_MODIFY &&
7184 staged_status != GOT_STATUS_DELETE)
7187 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7189 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7191 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7193 return got_error_from_errno("asprintf");
7195 err = got_object_id_str(&id_str,
7196 commit_id ? commit_id : a->worktree->base_commit_id);
7199 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7201 err = got_error_from_errno("asprintf");
7205 switch (staged_status) {
7206 case GOT_STATUS_MODIFY:
7207 err = got_object_open_as_blob(&blob_base, a->repo,
7212 case GOT_STATUS_ADD:
7214 if (staged_status == GOT_STATUS_ADD) {
7215 int choice = GOT_PATCH_CHOICE_NONE;
7216 err = (*a->patch_cb)(&choice, a->patch_arg,
7217 staged_status, ie->path, NULL, 1, 1);
7220 if (choice != GOT_PATCH_CHOICE_YES)
7223 err = create_unstaged_content(
7224 &path_unstaged_content,
7225 &path_new_staged_content, blob_id,
7226 staged_blob_id, ie->path, a->repo,
7227 a->patch_cb, a->patch_arg);
7228 if (err || path_unstaged_content == NULL)
7230 if (path_new_staged_content) {
7231 err = got_object_blob_create(
7233 path_new_staged_content,
7237 memcpy(ie->staged_blob_sha1,
7238 staged_blob_id->sha1,
7239 SHA1_DIGEST_LENGTH);
7241 err = merge_file(&local_changes_subsumed,
7242 a->worktree, blob_base, ondisk_path,
7243 relpath, got_fileindex_perms_to_st(ie),
7244 path_unstaged_content, label_orig,
7245 "unstaged", a->repo, a->progress_cb,
7248 path_new_staged_content == NULL)
7249 got_fileindex_entry_stage_set(ie,
7250 GOT_FILEIDX_STAGE_NONE);
7251 break; /* Done with this file. */
7254 err = got_object_open_as_blob(&blob_staged, a->repo,
7255 staged_blob_id, 8192);
7258 err = merge_blob(&local_changes_subsumed, a->worktree,
7259 blob_base, ondisk_path, relpath,
7260 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7261 commit_id ? commit_id : a->worktree->base_commit_id,
7262 a->repo, a->progress_cb, a->progress_arg);
7264 got_fileindex_entry_stage_set(ie,
7265 GOT_FILEIDX_STAGE_NONE);
7267 case GOT_STATUS_DELETE:
7269 int choice = GOT_PATCH_CHOICE_NONE;
7270 err = (*a->patch_cb)(&choice, a->patch_arg,
7271 staged_status, ie->path, NULL, 1, 1);
7274 if (choice == GOT_PATCH_CHOICE_NO)
7276 if (choice != GOT_PATCH_CHOICE_YES) {
7277 err = got_error(GOT_ERR_PATCH_CHOICE);
7281 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7282 err = get_file_status(&status, &sb, ie, ondisk_path,
7283 dirfd, de_name, a->repo);
7286 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7291 if (path_unstaged_content &&
7292 unlink(path_unstaged_content) == -1 && err == NULL)
7293 err = got_error_from_errno2("unlink", path_unstaged_content);
7294 if (path_new_staged_content &&
7295 unlink(path_new_staged_content) == -1 && err == NULL)
7296 err = got_error_from_errno2("unlink", path_new_staged_content);
7297 free(path_unstaged_content);
7298 free(path_new_staged_content);
7300 got_object_blob_close(blob_base);
7302 got_object_blob_close(blob_staged);
7308 const struct got_error *
7309 got_worktree_unstage(struct got_worktree *worktree,
7310 struct got_pathlist_head *paths,
7311 got_worktree_checkout_cb progress_cb, void *progress_arg,
7312 got_worktree_patch_cb patch_cb, void *patch_arg,
7313 struct got_repository *repo)
7315 const struct got_error *err = NULL, *sync_err, *unlockerr;
7316 struct got_pathlist_entry *pe;
7317 struct got_fileindex *fileindex = NULL;
7318 char *fileindex_path = NULL;
7319 struct unstage_path_arg upa;
7321 err = lock_worktree(worktree, LOCK_EX);
7325 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7329 upa.worktree = worktree;
7330 upa.fileindex = fileindex;
7332 upa.progress_cb = progress_cb;
7333 upa.progress_arg = progress_arg;
7334 upa.patch_cb = patch_cb;
7335 upa.patch_arg = patch_arg;
7336 TAILQ_FOREACH(pe, paths, entry) {
7337 err = worktree_status(worktree, pe->path, fileindex, repo,
7338 unstage_path, &upa, NULL, NULL, 0, 0);
7343 sync_err = sync_fileindex(fileindex, fileindex_path);
7344 if (sync_err && err == NULL)
7347 free(fileindex_path);
7349 got_fileindex_free(fileindex);
7350 unlockerr = lock_worktree(worktree, LOCK_SH);
7351 if (unlockerr && err == NULL)