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);
921 * Merge a symlink into the work tree, where blob_orig acts as the common
922 * ancestor, blob_deriv acts as the first derived version, and the symlink
923 * on disk acts as the second derived version.
924 * Assume that contents of both blobs represent symlinks.
926 static const struct got_error *
927 merge_symlink(struct got_worktree *worktree,
928 struct got_blob_object *blob_orig, const char *ondisk_path,
929 const char *path, uint16_t st_mode, const char *label_orig,
930 struct got_blob_object *blob_deriv,
931 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
932 got_worktree_checkout_cb progress_cb, void *progress_arg)
934 const struct got_error *err = NULL;
935 char *ancestor_target = NULL, *deriv_target = NULL;
937 ssize_t ondisk_len, deriv_len;
938 char ondisk_target[PATH_MAX];
939 int have_local_change = 0;
940 int have_incoming_change = 0;
942 if (lstat(ondisk_path, &sb) == -1)
943 return got_error_from_errno2("lstat", ondisk_path);
945 if (!S_ISLNK(sb.st_mode)) {
946 /* TODO symlink is obstructed; do something */
947 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
950 ondisk_len = readlink(ondisk_path, ondisk_target,
951 sizeof(ondisk_target));
952 if (ondisk_len == -1) {
953 err = got_error_from_errno2("readlink",
957 ondisk_target[ondisk_len] = '\0';
960 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
965 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
969 if (ancestor_target == NULL ||
970 (ondisk_len != strlen(ancestor_target) ||
971 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
972 have_local_change = 1;
974 deriv_len = strlen(deriv_target);
975 if (ancestor_target == NULL ||
976 (deriv_len != strlen(ancestor_target) ||
977 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
978 have_incoming_change = 1;
980 if (!have_local_change && !have_incoming_change) {
981 if (ancestor_target) {
982 /* Both sides made the same change. */
983 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
985 } else if (deriv_len == ondisk_len &&
986 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
987 /* Both sides added the same symlink. */
988 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
991 /* Both sides added symlinks which don't match. */
992 err = install_symlink_conflict(deriv_target,
993 deriv_base_commit_id, ancestor_target,
994 label_orig, ondisk_target, ondisk_path);
997 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1000 } else if (!have_local_change && have_incoming_change) {
1001 /* Apply the incoming change. */
1002 err = update_symlink(ondisk_path, deriv_target,
1003 strlen(deriv_target));
1006 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1007 } else if (have_local_change && have_incoming_change) {
1008 err = install_symlink_conflict(deriv_target,
1009 deriv_base_commit_id, ancestor_target, label_orig,
1010 ondisk_target, ondisk_path);
1013 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1018 free(ancestor_target);
1024 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1025 * blob_deriv acts as the first derived version, and the file on disk
1026 * acts as the second derived version.
1028 static const struct got_error *
1029 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1030 struct got_blob_object *blob_orig, const char *ondisk_path,
1031 const char *path, uint16_t st_mode, const char *label_orig,
1032 struct got_blob_object *blob_deriv,
1033 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1034 got_worktree_checkout_cb progress_cb, void *progress_arg)
1036 const struct got_error *err = NULL;
1037 FILE *f_deriv = NULL;
1038 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1039 char *label_deriv = NULL, *parent;
1041 *local_changes_subsumed = 0;
1043 parent = dirname(ondisk_path);
1045 return got_error_from_errno2("dirname", ondisk_path);
1048 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1049 err = got_error_from_errno("asprintf");
1054 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1057 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1062 err = got_object_id_str(&id_str, deriv_base_commit_id);
1065 if (asprintf(&label_deriv, "%s: commit %s",
1066 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1067 err = got_error_from_errno("asprintf");
1071 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1072 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1073 label_deriv, repo, progress_cb, progress_arg);
1075 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1076 err = got_error_from_errno("fclose");
1078 if (blob_deriv_path) {
1079 unlink(blob_deriv_path);
1080 free(blob_deriv_path);
1087 static const struct got_error *
1088 create_fileindex_entry(struct got_fileindex *fileindex,
1089 struct got_object_id *base_commit_id, const char *ondisk_path,
1090 const char *path, struct got_object_id *blob_id)
1092 const struct got_error *err = NULL;
1093 struct got_fileindex_entry *new_ie;
1095 err = got_fileindex_entry_alloc(&new_ie, path);
1099 err = got_fileindex_entry_update(new_ie, ondisk_path,
1100 blob_id->sha1, base_commit_id->sha1, 1);
1104 err = got_fileindex_entry_add(fileindex, new_ie);
1107 got_fileindex_entry_free(new_ie);
1112 get_ondisk_perms(int executable, mode_t st_mode)
1114 mode_t xbits = S_IXUSR;
1117 /* Map read bits to execute bits. */
1118 if (st_mode & S_IRGRP)
1120 if (st_mode & S_IROTH)
1122 return st_mode | xbits;
1125 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1128 /* forward declaration */
1129 static const struct got_error *
1130 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1131 const char *path, mode_t te_mode, mode_t st_mode,
1132 struct got_blob_object *blob, int restoring_missing_file,
1133 int reverting_versioned_file, int installing_bad_symlink,
1134 struct got_repository *repo,
1135 got_worktree_checkout_cb progress_cb, void *progress_arg);
1138 * This function assumes that the provided symlink target points at a
1139 * safe location in the work tree!
1141 static const struct got_error *
1142 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1145 const struct got_error *err = NULL;
1147 char etarget[PATH_MAX];
1151 * "Bad" symlinks (those pointing outside the work tree or into the
1152 * .got directory) are installed in the work tree as a regular file
1153 * which contains the bad symlink target path.
1154 * The new symlink target has already been checked for safety by our
1155 * caller. If we can successfully open a regular file then we simply
1156 * replace this file with a symlink below.
1158 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1161 return got_error_from_errno2("open", ondisk_path);
1163 /* We are updating an existing on-disk symlink. */
1164 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1166 return got_error_from_errno2("readlink", ondisk_path);
1168 if (elen == target_len &&
1169 memcmp(etarget, target_path, target_len) == 0)
1170 return NULL; /* nothing to do */
1173 err = update_symlink(ondisk_path, target_path, target_len);
1174 if (fd != -1 && close(fd) == -1 && err == NULL)
1175 err = got_error_from_errno2("close", ondisk_path);
1179 static const struct got_error *
1180 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1181 const char *path, mode_t te_mode, mode_t st_mode,
1182 struct got_blob_object *blob, int restoring_missing_file,
1183 int reverting_versioned_file, struct got_repository *repo,
1184 got_worktree_checkout_cb progress_cb, void *progress_arg)
1186 const struct got_error *err = NULL;
1187 char target_path[PATH_MAX];
1188 size_t len, target_len = 0;
1189 char *resolved_path = NULL, *abspath = NULL;
1190 char *path_got = NULL;
1191 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1192 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1195 * Blob object content specifies the target path of the link.
1196 * If a symbolic link cannot be installed we instead create
1197 * a regular file which contains the link target path stored
1198 * in the blob object.
1201 err = got_object_blob_read_block(&len, blob);
1202 if (len + target_len >= sizeof(target_path)) {
1203 /* Path too long; install as a regular file. */
1204 got_object_blob_rewind(blob);
1205 return install_blob(worktree, ondisk_path, path,
1206 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1207 restoring_missing_file, reverting_versioned_file,
1208 1, repo, progress_cb, progress_arg);
1211 /* Skip blob object header first time around. */
1212 memcpy(target_path + target_len, buf + hdrlen,
1214 target_len += len - hdrlen;
1218 target_path[target_len] = '\0';
1221 * Relative symlink target lookup should begin at the directory
1222 * in which the blob object is being installed.
1224 if (!got_path_is_absolute(target_path)) {
1225 char *parent = dirname(ondisk_path);
1226 if (parent == NULL) {
1227 err = got_error_from_errno2("dirname", ondisk_path);
1230 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1231 err = got_error_from_errno("asprintf");
1237 * unveil(2) restricts our view of paths in the filesystem.
1238 * ENOENT will occur if a link target path does not exist or
1239 * if it points outside our unveiled path space.
1241 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1242 if (resolved_path == NULL) {
1243 if (errno != ENOENT) {
1244 err = got_error_from_errno2("realpath", target_path);
1249 /* Only allow symlinks pointing at paths within the work tree. */
1250 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1251 abspath : target_path), worktree->root_path,
1252 strlen(worktree->root_path))) {
1253 /* install as a regular file */
1254 got_object_blob_rewind(blob);
1255 err = install_blob(worktree, ondisk_path, path,
1256 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1257 restoring_missing_file, reverting_versioned_file, 1,
1258 repo, progress_cb, progress_arg);
1262 /* Do not allow symlinks pointing into the .got directory. */
1263 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1264 GOT_WORKTREE_GOT_DIR) == -1) {
1265 err = got_error_from_errno("asprintf");
1268 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1269 abspath : target_path), path_got, strlen(path_got))) {
1270 /* install as a regular file */
1271 got_object_blob_rewind(blob);
1272 err = install_blob(worktree, ondisk_path, path,
1273 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1274 restoring_missing_file, reverting_versioned_file, 1,
1275 repo, progress_cb, progress_arg);
1279 if (symlink(target_path, ondisk_path) == -1) {
1280 if (errno == EEXIST) {
1281 err = replace_existing_symlink(ondisk_path,
1282 target_path, target_len);
1286 err = (*progress_cb)(progress_arg,
1287 GOT_STATUS_UPDATE, path);
1289 goto done; /* Nothing else to do. */
1292 if (errno == ENOENT) {
1293 char *parent = dirname(ondisk_path);
1294 if (parent == NULL) {
1295 err = got_error_from_errno2("dirname",
1299 err = add_dir_on_disk(worktree, parent);
1303 * Retry, and fall through to error handling
1304 * below if this second attempt fails.
1306 if (symlink(target_path, ondisk_path) != -1) {
1307 err = NULL; /* success */
1312 /* Handle errors from first or second creation attempt. */
1313 if (errno == ENAMETOOLONG) {
1314 /* bad target path; install as a regular file */
1315 got_object_blob_rewind(blob);
1316 err = install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1318 restoring_missing_file, reverting_versioned_file, 1,
1319 repo, progress_cb, progress_arg);
1320 } else if (errno == ENOTDIR) {
1321 err = got_error_path(ondisk_path,
1322 GOT_ERR_FILE_OBSTRUCTED);
1324 err = got_error_from_errno3("symlink",
1325 target_path, ondisk_path);
1327 } else if (progress_cb)
1328 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1330 free(resolved_path);
1336 static const struct got_error *
1337 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1338 const char *path, mode_t te_mode, mode_t st_mode,
1339 struct got_blob_object *blob, int restoring_missing_file,
1340 int reverting_versioned_file, int installing_bad_symlink,
1341 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1344 const struct got_error *err = NULL;
1348 char *tmppath = NULL;
1350 if (S_ISLNK(te_mode))
1351 return install_symlink(worktree, ondisk_path, path, te_mode,
1352 st_mode, blob, restoring_missing_file,
1353 reverting_versioned_file, repo, progress_cb, progress_arg);
1355 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1356 GOT_DEFAULT_FILE_MODE);
1358 if (errno == ENOENT) {
1359 char *parent = dirname(path);
1361 return got_error_from_errno2("dirname", path);
1362 err = add_dir_on_disk(worktree, parent);
1365 fd = open(ondisk_path,
1366 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1367 GOT_DEFAULT_FILE_MODE);
1369 return got_error_from_errno2("open",
1371 } else if (errno == EEXIST) {
1372 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1373 /* TODO file is obstructed; do something */
1374 err = got_error_path(ondisk_path,
1375 GOT_ERR_FILE_OBSTRUCTED);
1378 err = got_opentemp_named_fd(&tmppath, &fd,
1385 return got_error_from_errno2("open", ondisk_path);
1389 if (restoring_missing_file)
1390 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1392 else if (reverting_versioned_file)
1393 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1396 err = (*progress_cb)(progress_arg,
1397 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1402 hdrlen = got_object_blob_get_hdrlen(blob);
1404 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1405 err = got_object_blob_read_block(&len, blob);
1409 /* Skip blob object header first time around. */
1410 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1412 err = got_error_from_errno("write");
1414 } else if (outlen != len - hdrlen) {
1415 err = got_error(GOT_ERR_IO);
1422 if (fsync(fd) != 0) {
1423 err = got_error_from_errno("fsync");
1428 if (rename(tmppath, ondisk_path) != 0) {
1429 err = got_error_from_errno3("rename", tmppath,
1436 if (chmod(ondisk_path,
1437 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1438 err = got_error_from_errno2("chmod", ondisk_path);
1443 if (fd != -1 && close(fd) != 0 && err == NULL)
1444 err = got_error_from_errno("close");
1449 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1450 static const struct got_error *
1451 get_modified_file_content_status(unsigned char *status, FILE *f)
1453 const struct got_error *err = NULL;
1454 const char *markers[3] = {
1455 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1456 GOT_DIFF_CONFLICT_MARKER_SEP,
1457 GOT_DIFF_CONFLICT_MARKER_END
1462 const char delim[3] = {'\0', '\0', '\0'};
1464 while (*status == GOT_STATUS_MODIFY) {
1465 line = fparseln(f, &len, NULL, delim, 0);
1469 err = got_ferror(f, GOT_ERR_IO);
1473 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1474 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1476 *status = GOT_STATUS_CONFLICT;
1486 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1488 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1489 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1493 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1495 return !(ie->ctime_sec == sb->st_ctime &&
1496 ie->ctime_nsec == sb->st_ctimensec &&
1497 ie->mtime_sec == sb->st_mtime &&
1498 ie->mtime_nsec == sb->st_mtimensec &&
1499 ie->size == (sb->st_size & 0xffffffff) &&
1500 !xbit_differs(ie, sb->st_mode));
1503 static unsigned char
1504 get_staged_status(struct got_fileindex_entry *ie)
1506 switch (got_fileindex_entry_stage_get(ie)) {
1507 case GOT_FILEIDX_STAGE_ADD:
1508 return GOT_STATUS_ADD;
1509 case GOT_FILEIDX_STAGE_DELETE:
1510 return GOT_STATUS_DELETE;
1511 case GOT_FILEIDX_STAGE_MODIFY:
1512 return GOT_STATUS_MODIFY;
1514 return GOT_STATUS_NO_CHANGE;
1518 static const struct got_error *
1519 get_symlink_status(unsigned char *status, struct stat *sb,
1520 struct got_fileindex_entry *ie, const char *abspath,
1521 int dirfd, const char *de_name, struct got_blob_object *blob)
1523 const struct got_error *err = NULL;
1524 char target_path[PATH_MAX];
1525 char etarget[PATH_MAX];
1527 size_t len, target_len = 0;
1528 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1529 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1531 *status = GOT_STATUS_NO_CHANGE;
1533 /* Blob object content specifies the target path of the link. */
1535 err = got_object_blob_read_block(&len, blob);
1538 if (len + target_len >= sizeof(target_path)) {
1540 * Should not happen. The blob contents were OK
1541 * when this symlink was installed.
1543 return got_error(GOT_ERR_NO_SPACE);
1546 /* Skip blob object header first time around. */
1547 memcpy(target_path + target_len, buf + hdrlen,
1549 target_len += len - hdrlen;
1553 target_path[target_len] = '\0';
1556 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1558 return got_error_from_errno2("readlinkat", abspath);
1560 elen = readlink(abspath, etarget, sizeof(etarget));
1562 return got_error_from_errno2("readlinkat", abspath);
1565 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1566 *status = GOT_STATUS_MODIFY;
1571 static const struct got_error *
1572 get_file_status(unsigned char *status, struct stat *sb,
1573 struct got_fileindex_entry *ie, const char *abspath,
1574 int dirfd, const char *de_name, struct got_repository *repo)
1576 const struct got_error *err = NULL;
1577 struct got_object_id id;
1582 struct got_blob_object *blob = NULL;
1584 unsigned char staged_status = get_staged_status(ie);
1586 *status = GOT_STATUS_NO_CHANGE;
1589 * Whenever the caller provides a directory descriptor and a
1590 * directory entry name for the file, use them! This prevents
1591 * race conditions if filesystem paths change beneath our feet.
1594 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1595 if (errno == ENOENT) {
1596 if (got_fileindex_entry_has_file_on_disk(ie))
1597 *status = GOT_STATUS_MISSING;
1599 *status = GOT_STATUS_DELETE;
1602 err = got_error_from_errno2("fstatat", abspath);
1606 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1607 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1608 return got_error_from_errno2("open", abspath);
1609 else if (fd == -1 && errno == ELOOP) {
1610 if (lstat(abspath, sb) == -1)
1611 return got_error_from_errno2("lstat", abspath);
1612 } else if (fd == -1 || fstat(fd, sb) == -1) {
1613 if (errno == ENOENT) {
1614 if (got_fileindex_entry_has_file_on_disk(ie))
1615 *status = GOT_STATUS_MISSING;
1617 *status = GOT_STATUS_DELETE;
1620 err = got_error_from_errno2("fstat", abspath);
1625 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1626 *status = GOT_STATUS_OBSTRUCTED;
1630 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1631 *status = GOT_STATUS_DELETE;
1633 } else if (!got_fileindex_entry_has_blob(ie) &&
1634 staged_status != GOT_STATUS_ADD) {
1635 *status = GOT_STATUS_ADD;
1639 if (!stat_info_differs(ie, sb))
1642 if (staged_status == GOT_STATUS_MODIFY ||
1643 staged_status == GOT_STATUS_ADD)
1644 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1646 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1648 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1652 if (S_ISLNK(sb->st_mode)) {
1653 /* Staging changes to symlinks is not yet(?) supported. */
1654 if (staged_status != GOT_STATUS_NO_CHANGE) {
1655 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1658 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1665 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1667 err = got_error_from_errno2("openat", abspath);
1672 f = fdopen(fd, "r");
1674 err = got_error_from_errno2("fdopen", abspath);
1678 hdrlen = got_object_blob_get_hdrlen(blob);
1680 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1681 err = got_object_blob_read_block(&blen, blob);
1684 /* Skip length of blob object header first time around. */
1685 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1686 if (flen == 0 && ferror(f)) {
1687 err = got_error_from_errno("fread");
1692 *status = GOT_STATUS_MODIFY;
1694 } else if (flen == 0) {
1696 *status = GOT_STATUS_MODIFY;
1698 } else if (blen - hdrlen == flen) {
1699 /* Skip blob object header first time around. */
1700 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1701 *status = GOT_STATUS_MODIFY;
1705 *status = GOT_STATUS_MODIFY;
1711 if (*status == GOT_STATUS_MODIFY) {
1713 err = get_modified_file_content_status(status, f);
1714 } else if (xbit_differs(ie, sb->st_mode))
1715 *status = GOT_STATUS_MODE_CHANGE;
1718 got_object_blob_close(blob);
1719 if (f != NULL && fclose(f) == EOF && err == NULL)
1720 err = got_error_from_errno2("fclose", abspath);
1721 if (fd != -1 && close(fd) == -1 && err == NULL)
1722 err = got_error_from_errno2("close", abspath);
1727 * Update timestamps in the file index if a file is unmodified and
1728 * we had to run a full content comparison to find out.
1730 static const struct got_error *
1731 sync_timestamps(char *ondisk_path, unsigned char status,
1732 struct got_fileindex_entry *ie, struct stat *sb)
1734 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1735 return got_fileindex_entry_update(ie, ondisk_path,
1736 ie->blob_sha1, ie->commit_sha1, 1);
1741 static const struct got_error *
1742 update_blob(struct got_worktree *worktree,
1743 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1744 struct got_tree_entry *te, const char *path,
1745 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1748 const struct got_error *err = NULL;
1749 struct got_blob_object *blob = NULL;
1751 unsigned char status = GOT_STATUS_NO_CHANGE;
1754 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1755 return got_error_from_errno("asprintf");
1758 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1759 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1762 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1766 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1767 sb.st_mode = got_fileindex_perms_to_st(ie);
1769 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1771 if (status == GOT_STATUS_OBSTRUCTED) {
1772 err = (*progress_cb)(progress_arg, status, path);
1775 if (status == GOT_STATUS_CONFLICT) {
1776 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1781 if (ie && status != GOT_STATUS_MISSING &&
1782 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1783 if (got_fileindex_entry_has_commit(ie) &&
1784 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1785 SHA1_DIGEST_LENGTH) == 0) {
1786 err = sync_timestamps(ondisk_path, status, ie, &sb);
1789 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1793 if (got_fileindex_entry_has_blob(ie) &&
1794 memcmp(ie->blob_sha1, te->id.sha1,
1795 SHA1_DIGEST_LENGTH) == 0) {
1796 err = sync_timestamps(ondisk_path, status, ie, &sb);
1801 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1805 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1806 int update_timestamps;
1807 struct got_blob_object *blob2 = NULL;
1808 char *label_orig = NULL;
1809 if (got_fileindex_entry_has_blob(ie)) {
1810 struct got_object_id id2;
1811 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1812 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1816 if (got_fileindex_entry_has_commit(ie)) {
1817 char id_str[SHA1_DIGEST_STRING_LENGTH];
1818 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1819 sizeof(id_str)) == NULL) {
1820 err = got_error_path(id_str,
1821 GOT_ERR_BAD_OBJ_ID_STR);
1824 if (asprintf(&label_orig, "%s: commit %s",
1825 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1826 err = got_error_from_errno("asprintf");
1830 if (S_ISLNK(te->mode)) {
1831 err = merge_symlink(worktree, blob2,
1832 ondisk_path, path, sb.st_mode, label_orig,
1833 blob, worktree->base_commit_id, repo,
1834 progress_cb, progress_arg);
1836 err = merge_blob(&update_timestamps, worktree, blob2,
1837 ondisk_path, path, sb.st_mode, label_orig, blob,
1838 worktree->base_commit_id, repo,
1839 progress_cb, progress_arg);
1843 got_object_blob_close(blob2);
1847 * Do not update timestamps of files with local changes.
1848 * Otherwise, a future status walk would treat them as
1849 * unmodified files again.
1851 err = got_fileindex_entry_update(ie, ondisk_path,
1852 blob->id.sha1, worktree->base_commit_id->sha1,
1854 } else if (status == GOT_STATUS_MODE_CHANGE) {
1855 err = got_fileindex_entry_update(ie, ondisk_path,
1856 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1857 } else if (status == GOT_STATUS_DELETE) {
1858 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1861 err = got_fileindex_entry_update(ie, ondisk_path,
1862 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1866 err = install_blob(worktree, ondisk_path, path, te->mode,
1867 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1868 repo, progress_cb, progress_arg);
1872 err = got_fileindex_entry_update(ie, ondisk_path,
1873 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1875 err = create_fileindex_entry(fileindex,
1876 worktree->base_commit_id, ondisk_path, path,
1882 got_object_blob_close(blob);
1888 static const struct got_error *
1889 remove_ondisk_file(const char *root_path, const char *path)
1891 const struct got_error *err = NULL;
1892 char *ondisk_path = NULL;
1894 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1895 return got_error_from_errno("asprintf");
1897 if (unlink(ondisk_path) == -1) {
1898 if (errno != ENOENT)
1899 err = got_error_from_errno2("unlink", ondisk_path);
1901 char *parent = dirname(ondisk_path);
1902 while (parent && strcmp(parent, root_path) != 0) {
1903 if (rmdir(parent) == -1) {
1904 if (errno != ENOTEMPTY)
1905 err = got_error_from_errno2("rmdir",
1909 parent = dirname(parent);
1916 static const struct got_error *
1917 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1918 struct got_fileindex_entry *ie, struct got_repository *repo,
1919 got_worktree_checkout_cb progress_cb, void *progress_arg)
1921 const struct got_error *err = NULL;
1922 unsigned char status;
1926 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1927 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1929 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1931 return got_error_from_errno("asprintf");
1933 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1937 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1938 char ondisk_target[PATH_MAX];
1939 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1940 sizeof(ondisk_target));
1941 if (ondisk_len == -1) {
1942 err = got_error_from_errno2("readlink", ondisk_path);
1945 ondisk_target[ondisk_len] = '\0';
1946 err = install_symlink_conflict(NULL, worktree->base_commit_id,
1947 NULL, NULL, /* XXX pass common ancestor info? */
1948 ondisk_target, ondisk_path);
1951 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1956 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1957 status == GOT_STATUS_ADD) {
1958 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1962 * Preserve the working file and change the deleted blob's
1963 * entry into a schedule-add entry.
1965 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1968 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1971 if (status == GOT_STATUS_NO_CHANGE) {
1972 err = remove_ondisk_file(worktree->root_path, ie->path);
1976 got_fileindex_entry_remove(fileindex, ie);
1983 struct diff_cb_arg {
1984 struct got_fileindex *fileindex;
1985 struct got_worktree *worktree;
1986 struct got_repository *repo;
1987 got_worktree_checkout_cb progress_cb;
1989 got_cancel_cb cancel_cb;
1993 static const struct got_error *
1994 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1995 struct got_tree_entry *te, const char *parent_path)
1997 struct diff_cb_arg *a = arg;
1999 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2000 return got_error(GOT_ERR_CANCELLED);
2002 return update_blob(a->worktree, a->fileindex, ie, te,
2003 ie->path, a->repo, a->progress_cb, a->progress_arg);
2006 static const struct got_error *
2007 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2009 struct diff_cb_arg *a = arg;
2011 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2012 return got_error(GOT_ERR_CANCELLED);
2014 return delete_blob(a->worktree, a->fileindex, ie,
2015 a->repo, a->progress_cb, a->progress_arg);
2018 static const struct got_error *
2019 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2021 struct diff_cb_arg *a = arg;
2022 const struct got_error *err;
2025 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2026 return got_error(GOT_ERR_CANCELLED);
2028 if (got_object_tree_entry_is_submodule(te))
2031 if (asprintf(&path, "%s%s%s", parent_path,
2032 parent_path[0] ? "/" : "", te->name)
2034 return got_error_from_errno("asprintf");
2036 if (S_ISDIR(te->mode))
2037 err = add_dir_on_disk(a->worktree, path);
2039 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2040 a->repo, a->progress_cb, a->progress_arg);
2046 static const struct got_error *
2047 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2049 const struct got_error *err = NULL;
2050 char *uuidstr = NULL;
2051 uint32_t uuid_status;
2055 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2056 if (uuid_status != uuid_s_ok)
2057 return got_error_uuid(uuid_status, "uuid_to_string");
2059 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2061 err = got_error_from_errno("asprintf");
2068 const struct got_error *
2069 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2071 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2074 static const struct got_error *
2075 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2077 return get_ref_name(refname, worktree,
2078 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2081 static const struct got_error *
2082 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2084 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2087 static const struct got_error *
2088 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2090 return get_ref_name(refname, worktree,
2091 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2094 static const struct got_error *
2095 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2097 return get_ref_name(refname, worktree,
2098 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2101 static const struct got_error *
2102 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2104 return get_ref_name(refname, worktree,
2105 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2108 static const struct got_error *
2109 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2111 return get_ref_name(refname, worktree,
2112 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2115 static const struct got_error *
2116 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2118 return get_ref_name(refname, worktree,
2119 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2122 static const struct got_error *
2123 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2125 return get_ref_name(refname, worktree,
2126 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2129 const struct got_error *
2130 got_worktree_get_histedit_script_path(char **path,
2131 struct got_worktree *worktree)
2133 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2134 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2136 return got_error_from_errno("asprintf");
2142 * Prevent Git's garbage collector from deleting our base commit by
2143 * setting a reference to our base commit's ID.
2145 static const struct got_error *
2146 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2148 const struct got_error *err = NULL;
2149 struct got_reference *ref = NULL;
2152 err = got_worktree_get_base_ref_name(&refname, worktree);
2156 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2160 err = got_ref_write(ref, repo);
2168 static const struct got_error *
2169 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2171 const struct got_error *err = NULL;
2173 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2174 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2175 err = got_error_from_errno("asprintf");
2176 *fileindex_path = NULL;
2182 static const struct got_error *
2183 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2184 struct got_worktree *worktree)
2186 const struct got_error *err = NULL;
2189 *fileindex_path = NULL;
2190 *fileindex = got_fileindex_alloc();
2191 if (*fileindex == NULL)
2192 return got_error_from_errno("got_fileindex_alloc");
2194 err = get_fileindex_path(fileindex_path, worktree);
2198 index = fopen(*fileindex_path, "rb");
2199 if (index == NULL) {
2200 if (errno != ENOENT)
2201 err = got_error_from_errno2("fopen", *fileindex_path);
2203 err = got_fileindex_read(*fileindex, index);
2204 if (fclose(index) != 0 && err == NULL)
2205 err = got_error_from_errno("fclose");
2209 free(*fileindex_path);
2210 *fileindex_path = NULL;
2211 got_fileindex_free(*fileindex);
2217 struct bump_base_commit_id_arg {
2218 struct got_object_id *base_commit_id;
2221 const char *entry_name;
2222 got_worktree_checkout_cb progress_cb;
2226 /* Bump base commit ID of all files within an updated part of the work tree. */
2227 static const struct got_error *
2228 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2230 const struct got_error *err;
2231 struct bump_base_commit_id_arg *a = arg;
2233 if (a->entry_name) {
2234 if (strcmp(ie->path, a->path) != 0)
2236 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2239 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2240 SHA1_DIGEST_LENGTH) == 0)
2243 if (a->progress_cb) {
2244 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2249 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2253 static const struct got_error *
2254 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2256 const struct got_error *err = NULL;
2257 char *new_fileindex_path = NULL;
2258 FILE *new_index = NULL;
2259 struct timespec timeout;
2261 err = got_opentemp_named(&new_fileindex_path, &new_index,
2266 err = got_fileindex_write(fileindex, new_index);
2270 if (rename(new_fileindex_path, fileindex_path) != 0) {
2271 err = got_error_from_errno3("rename", new_fileindex_path,
2273 unlink(new_fileindex_path);
2277 * Sleep for a short amount of time to ensure that files modified after
2278 * this program exits have a different time stamp from the one which
2279 * was recorded in the file index.
2282 timeout.tv_nsec = 1;
2283 nanosleep(&timeout, NULL);
2287 free(new_fileindex_path);
2291 static const struct got_error *
2292 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2293 struct got_object_id **tree_id, const char *wt_relpath,
2294 struct got_worktree *worktree, struct got_repository *repo)
2296 const struct got_error *err = NULL;
2297 struct got_object_id *id = NULL;
2298 char *in_repo_path = NULL;
2299 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2301 *entry_type = GOT_OBJ_TYPE_ANY;
2302 *tree_relpath = NULL;
2305 if (wt_relpath[0] == '\0') {
2306 /* Check out all files within the work tree. */
2307 *entry_type = GOT_OBJ_TYPE_TREE;
2308 *tree_relpath = strdup("");
2309 if (*tree_relpath == NULL) {
2310 err = got_error_from_errno("strdup");
2313 err = got_object_id_by_path(tree_id, repo,
2314 worktree->base_commit_id, worktree->path_prefix);
2320 /* Check out a subset of files in the work tree. */
2322 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2323 is_root_wt ? "" : "/", wt_relpath) == -1) {
2324 err = got_error_from_errno("asprintf");
2328 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2334 in_repo_path = NULL;
2336 err = got_object_get_type(entry_type, repo, id);
2340 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2341 /* Check out a single file. */
2342 if (strchr(wt_relpath, '/') == NULL) {
2343 /* Check out a single file in work tree's root dir. */
2344 in_repo_path = strdup(worktree->path_prefix);
2345 if (in_repo_path == NULL) {
2346 err = got_error_from_errno("strdup");
2349 *tree_relpath = strdup("");
2350 if (*tree_relpath == NULL) {
2351 err = got_error_from_errno("strdup");
2355 /* Check out a single file in a subdirectory. */
2356 err = got_path_dirname(tree_relpath, wt_relpath);
2359 if (asprintf(&in_repo_path, "%s%s%s",
2360 worktree->path_prefix, is_root_wt ? "" : "/",
2361 *tree_relpath) == -1) {
2362 err = got_error_from_errno("asprintf");
2366 err = got_object_id_by_path(tree_id, repo,
2367 worktree->base_commit_id, in_repo_path);
2369 /* Check out all files within a subdirectory. */
2370 *tree_id = got_object_id_dup(id);
2371 if (*tree_id == NULL) {
2372 err = got_error_from_errno("got_object_id_dup");
2375 *tree_relpath = strdup(wt_relpath);
2376 if (*tree_relpath == NULL) {
2377 err = got_error_from_errno("strdup");
2385 *entry_type = GOT_OBJ_TYPE_ANY;
2386 free(*tree_relpath);
2387 *tree_relpath = NULL;
2394 static const struct got_error *
2395 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2396 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2397 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2398 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2400 const struct got_error *err = NULL;
2401 struct got_commit_object *commit = NULL;
2402 struct got_tree_object *tree = NULL;
2403 struct got_fileindex_diff_tree_cb diff_cb;
2404 struct diff_cb_arg arg;
2406 err = ref_base_commit(worktree, repo);
2408 if (!(err->code == GOT_ERR_ERRNO &&
2409 (errno == EACCES || errno == EROFS)))
2411 err = (*progress_cb)(progress_arg,
2412 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2417 err = got_object_open_as_commit(&commit, repo,
2418 worktree->base_commit_id);
2422 err = got_object_open_as_tree(&tree, repo, tree_id);
2427 got_object_tree_find_entry(tree, entry_name) == NULL) {
2428 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2432 diff_cb.diff_old_new = diff_old_new;
2433 diff_cb.diff_old = diff_old;
2434 diff_cb.diff_new = diff_new;
2435 arg.fileindex = fileindex;
2436 arg.worktree = worktree;
2438 arg.progress_cb = progress_cb;
2439 arg.progress_arg = progress_arg;
2440 arg.cancel_cb = cancel_cb;
2441 arg.cancel_arg = cancel_arg;
2442 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2443 entry_name, repo, &diff_cb, &arg);
2446 got_object_tree_close(tree);
2448 got_object_commit_close(commit);
2452 const struct got_error *
2453 got_worktree_checkout_files(struct got_worktree *worktree,
2454 struct got_pathlist_head *paths, struct got_repository *repo,
2455 got_worktree_checkout_cb progress_cb, void *progress_arg,
2456 got_cancel_cb cancel_cb, void *cancel_arg)
2458 const struct got_error *err = NULL, *sync_err, *unlockerr;
2459 struct got_commit_object *commit = NULL;
2460 struct got_tree_object *tree = NULL;
2461 struct got_fileindex *fileindex = NULL;
2462 char *fileindex_path = NULL;
2463 struct got_pathlist_entry *pe;
2464 struct tree_path_data {
2465 SIMPLEQ_ENTRY(tree_path_data) entry;
2466 struct got_object_id *tree_id;
2471 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2473 SIMPLEQ_INIT(&tree_paths);
2475 err = lock_worktree(worktree, LOCK_EX);
2479 /* Map all specified paths to in-repository trees. */
2480 TAILQ_FOREACH(pe, paths, entry) {
2481 tpd = malloc(sizeof(*tpd));
2483 err = got_error_from_errno("malloc");
2487 err = find_tree_entry_for_checkout(&tpd->entry_type,
2488 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2494 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2495 err = got_path_basename(&tpd->entry_name, pe->path);
2503 tpd->entry_name = NULL;
2505 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2509 * Read the file index.
2510 * Checking out files is supposed to be an idempotent operation.
2511 * If the on-disk file index is incomplete we will try to complete it.
2513 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2517 tpd = SIMPLEQ_FIRST(&tree_paths);
2518 TAILQ_FOREACH(pe, paths, entry) {
2519 struct bump_base_commit_id_arg bbc_arg;
2521 err = checkout_files(worktree, fileindex, tpd->relpath,
2522 tpd->tree_id, tpd->entry_name, repo,
2523 progress_cb, progress_arg, cancel_cb, cancel_arg);
2527 bbc_arg.base_commit_id = worktree->base_commit_id;
2528 bbc_arg.entry_name = tpd->entry_name;
2529 bbc_arg.path = pe->path;
2530 bbc_arg.path_len = pe->path_len;
2531 bbc_arg.progress_cb = progress_cb;
2532 bbc_arg.progress_arg = progress_arg;
2533 err = got_fileindex_for_each_entry_safe(fileindex,
2534 bump_base_commit_id, &bbc_arg);
2538 tpd = SIMPLEQ_NEXT(tpd, entry);
2540 sync_err = sync_fileindex(fileindex, fileindex_path);
2541 if (sync_err && err == NULL)
2544 free(fileindex_path);
2546 got_object_tree_close(tree);
2548 got_object_commit_close(commit);
2550 got_fileindex_free(fileindex);
2551 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2552 tpd = SIMPLEQ_FIRST(&tree_paths);
2553 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2558 unlockerr = lock_worktree(worktree, LOCK_SH);
2559 if (unlockerr && err == NULL)
2564 struct merge_file_cb_arg {
2565 struct got_worktree *worktree;
2566 struct got_fileindex *fileindex;
2567 got_worktree_checkout_cb progress_cb;
2569 got_cancel_cb cancel_cb;
2571 const char *label_orig;
2572 struct got_object_id *commit_id2;
2575 static const struct got_error *
2576 merge_file_cb(void *arg, struct got_blob_object *blob1,
2577 struct got_blob_object *blob2, struct got_object_id *id1,
2578 struct got_object_id *id2, const char *path1, const char *path2,
2579 mode_t mode1, mode_t mode2, struct got_repository *repo)
2581 static const struct got_error *err = NULL;
2582 struct merge_file_cb_arg *a = arg;
2583 struct got_fileindex_entry *ie;
2584 char *ondisk_path = NULL;
2586 unsigned char status;
2587 int local_changes_subsumed;
2589 if (blob1 && blob2) {
2590 ie = got_fileindex_entry_get(a->fileindex, path2,
2593 return (*a->progress_cb)(a->progress_arg,
2594 GOT_STATUS_MISSING, path2);
2596 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2598 return got_error_from_errno("asprintf");
2600 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2605 if (status == GOT_STATUS_DELETE) {
2606 err = (*a->progress_cb)(a->progress_arg,
2607 GOT_STATUS_MERGE, path2);
2610 if (status != GOT_STATUS_NO_CHANGE &&
2611 status != GOT_STATUS_MODIFY &&
2612 status != GOT_STATUS_CONFLICT &&
2613 status != GOT_STATUS_ADD) {
2614 err = (*a->progress_cb)(a->progress_arg, status, path2);
2618 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2619 err = merge_symlink(a->worktree, blob1,
2620 ondisk_path, path2, sb.st_mode, a->label_orig,
2621 blob2, a->commit_id2, repo, a->progress_cb,
2624 err = merge_blob(&local_changes_subsumed, a->worktree,
2625 blob1, ondisk_path, path2, sb.st_mode,
2626 a->label_orig, blob2, a->commit_id2, repo,
2627 a->progress_cb, a->progress_arg);
2630 ie = got_fileindex_entry_get(a->fileindex, path1,
2633 return (*a->progress_cb)(a->progress_arg,
2634 GOT_STATUS_MISSING, path1);
2636 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2638 return got_error_from_errno("asprintf");
2640 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2646 case GOT_STATUS_NO_CHANGE:
2647 err = (*a->progress_cb)(a->progress_arg,
2648 GOT_STATUS_DELETE, path1);
2651 err = remove_ondisk_file(a->worktree->root_path, path1);
2655 got_fileindex_entry_mark_deleted_from_disk(ie);
2657 case GOT_STATUS_DELETE:
2658 case GOT_STATUS_MISSING:
2659 err = (*a->progress_cb)(a->progress_arg,
2660 GOT_STATUS_DELETE, path1);
2664 got_fileindex_entry_mark_deleted_from_disk(ie);
2666 case GOT_STATUS_ADD:
2667 case GOT_STATUS_MODIFY:
2668 case GOT_STATUS_CONFLICT:
2669 err = (*a->progress_cb)(a->progress_arg,
2670 GOT_STATUS_CANNOT_DELETE, path1);
2674 case GOT_STATUS_OBSTRUCTED:
2675 err = (*a->progress_cb)(a->progress_arg, status, path1);
2683 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2685 return got_error_from_errno("asprintf");
2686 ie = got_fileindex_entry_get(a->fileindex, path2,
2689 err = get_file_status(&status, &sb, ie, ondisk_path,
2693 if (status != GOT_STATUS_NO_CHANGE &&
2694 status != GOT_STATUS_MODIFY &&
2695 status != GOT_STATUS_CONFLICT &&
2696 status != GOT_STATUS_ADD) {
2697 err = (*a->progress_cb)(a->progress_arg,
2701 if (S_ISLNK(mode2)) {
2702 err = merge_symlink(a->worktree, NULL,
2703 ondisk_path, path2, sb.st_mode,
2704 a->label_orig, blob2, a->commit_id2,
2705 repo, a->progress_cb, a->progress_arg);
2709 err = merge_blob(&local_changes_subsumed,
2710 a->worktree, NULL, ondisk_path, path2,
2711 sb.st_mode, a->label_orig, blob2,
2712 a->commit_id2, repo, a->progress_cb,
2717 if (status == GOT_STATUS_DELETE) {
2718 err = got_fileindex_entry_update(ie,
2719 ondisk_path, blob2->id.sha1,
2720 a->worktree->base_commit_id->sha1, 0);
2725 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2726 err = install_blob(a->worktree, ondisk_path, path2,
2727 /* XXX get this from parent tree! */
2728 GOT_DEFAULT_FILE_MODE,
2729 sb.st_mode, blob2, 0, 0, 0, repo,
2730 a->progress_cb, a->progress_arg);
2733 err = got_fileindex_entry_alloc(&ie, path2);
2736 err = got_fileindex_entry_update(ie, ondisk_path,
2739 got_fileindex_entry_free(ie);
2742 err = got_fileindex_entry_add(a->fileindex, ie);
2744 got_fileindex_entry_free(ie);
2754 struct check_merge_ok_arg {
2755 struct got_worktree *worktree;
2756 struct got_repository *repo;
2759 static const struct got_error *
2760 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2762 const struct got_error *err = NULL;
2763 struct check_merge_ok_arg *a = arg;
2764 unsigned char status;
2768 /* Reject merges into a work tree with mixed base commits. */
2769 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2770 SHA1_DIGEST_LENGTH))
2771 return got_error(GOT_ERR_MIXED_COMMITS);
2773 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2775 return got_error_from_errno("asprintf");
2777 /* Reject merges into a work tree with conflicted files. */
2778 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2781 if (status == GOT_STATUS_CONFLICT)
2782 return got_error(GOT_ERR_CONFLICTS);
2787 static const struct got_error *
2788 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2789 const char *fileindex_path, struct got_object_id *commit_id1,
2790 struct got_object_id *commit_id2, struct got_repository *repo,
2791 got_worktree_checkout_cb progress_cb, void *progress_arg,
2792 got_cancel_cb cancel_cb, void *cancel_arg)
2794 const struct got_error *err = NULL, *sync_err;
2795 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2796 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2797 struct merge_file_cb_arg arg;
2798 char *label_orig = NULL;
2803 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2804 worktree->path_prefix);
2808 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2812 err = got_object_id_str(&id_str, commit_id1);
2816 if (asprintf(&label_orig, "%s: commit %s",
2817 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2818 err = got_error_from_errno("asprintf");
2825 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2826 worktree->path_prefix);
2830 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2834 arg.worktree = worktree;
2835 arg.fileindex = fileindex;
2836 arg.progress_cb = progress_cb;
2837 arg.progress_arg = progress_arg;
2838 arg.cancel_cb = cancel_cb;
2839 arg.cancel_arg = cancel_arg;
2840 arg.label_orig = label_orig;
2841 arg.commit_id2 = commit_id2;
2842 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2843 sync_err = sync_fileindex(fileindex, fileindex_path);
2844 if (sync_err && err == NULL)
2848 got_object_tree_close(tree1);
2850 got_object_tree_close(tree2);
2855 const struct got_error *
2856 got_worktree_merge_files(struct got_worktree *worktree,
2857 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2858 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2859 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2861 const struct got_error *err, *unlockerr;
2862 char *fileindex_path = NULL;
2863 struct got_fileindex *fileindex = NULL;
2864 struct check_merge_ok_arg mok_arg;
2866 err = lock_worktree(worktree, LOCK_EX);
2870 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2874 mok_arg.worktree = worktree;
2875 mok_arg.repo = repo;
2876 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2881 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2882 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2885 got_fileindex_free(fileindex);
2886 free(fileindex_path);
2887 unlockerr = lock_worktree(worktree, LOCK_SH);
2888 if (unlockerr && err == NULL)
2893 struct diff_dir_cb_arg {
2894 struct got_fileindex *fileindex;
2895 struct got_worktree *worktree;
2896 const char *status_path;
2897 size_t status_path_len;
2898 struct got_repository *repo;
2899 got_worktree_status_cb status_cb;
2901 got_cancel_cb cancel_cb;
2903 /* A pathlist containing per-directory pathlists of ignore patterns. */
2904 struct got_pathlist_head ignores;
2905 int report_unchanged;
2909 static const struct got_error *
2910 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2911 int dirfd, const char *de_name,
2912 got_worktree_status_cb status_cb, void *status_arg,
2913 struct got_repository *repo, int report_unchanged)
2915 const struct got_error *err = NULL;
2916 unsigned char status = GOT_STATUS_NO_CHANGE;
2917 unsigned char staged_status = get_staged_status(ie);
2919 struct got_object_id blob_id, commit_id, staged_blob_id;
2920 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2921 struct got_object_id *staged_blob_idp = NULL;
2923 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2927 if (status == GOT_STATUS_NO_CHANGE &&
2928 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2931 if (got_fileindex_entry_has_blob(ie)) {
2932 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2933 blob_idp = &blob_id;
2935 if (got_fileindex_entry_has_commit(ie)) {
2936 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2937 commit_idp = &commit_id;
2939 if (staged_status == GOT_STATUS_ADD ||
2940 staged_status == GOT_STATUS_MODIFY) {
2941 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2942 SHA1_DIGEST_LENGTH);
2943 staged_blob_idp = &staged_blob_id;
2946 return (*status_cb)(status_arg, status, staged_status,
2947 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2950 static const struct got_error *
2951 status_old_new(void *arg, struct got_fileindex_entry *ie,
2952 struct dirent *de, const char *parent_path, int dirfd)
2954 const struct got_error *err = NULL;
2955 struct diff_dir_cb_arg *a = arg;
2958 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2959 return got_error(GOT_ERR_CANCELLED);
2961 if (got_path_cmp(parent_path, a->status_path,
2962 strlen(parent_path), a->status_path_len) != 0 &&
2963 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2966 if (parent_path[0]) {
2967 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2968 parent_path, de->d_name) == -1)
2969 return got_error_from_errno("asprintf");
2971 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2973 return got_error_from_errno("asprintf");
2976 err = report_file_status(ie, abspath, dirfd, de->d_name,
2977 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2982 static const struct got_error *
2983 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2985 struct diff_dir_cb_arg *a = arg;
2986 struct got_object_id blob_id, commit_id;
2987 unsigned char status;
2989 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2990 return got_error(GOT_ERR_CANCELLED);
2992 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2995 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2996 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2997 if (got_fileindex_entry_has_file_on_disk(ie))
2998 status = GOT_STATUS_MISSING;
3000 status = GOT_STATUS_DELETE;
3001 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3002 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3006 free_ignorelist(struct got_pathlist_head *ignorelist)
3008 struct got_pathlist_entry *pe;
3010 TAILQ_FOREACH(pe, ignorelist, entry)
3011 free((char *)pe->path);
3012 got_pathlist_free(ignorelist);
3016 free_ignores(struct got_pathlist_head *ignores)
3018 struct got_pathlist_entry *pe;
3020 TAILQ_FOREACH(pe, ignores, entry) {
3021 struct got_pathlist_head *ignorelist = pe->data;
3022 free_ignorelist(ignorelist);
3023 free((char *)pe->path);
3025 got_pathlist_free(ignores);
3028 static const struct got_error *
3029 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3031 const struct got_error *err = NULL;
3032 struct got_pathlist_entry *pe = NULL;
3033 struct got_pathlist_head *ignorelist;
3034 char *line = NULL, *pattern, *dirpath = NULL;
3035 size_t linesize = 0;
3038 ignorelist = calloc(1, sizeof(*ignorelist));
3039 if (ignorelist == NULL)
3040 return got_error_from_errno("calloc");
3041 TAILQ_INIT(ignorelist);
3043 while ((linelen = getline(&line, &linesize, f)) != -1) {
3044 if (linelen > 0 && line[linelen - 1] == '\n')
3045 line[linelen - 1] = '\0';
3047 /* Git's ignores may contain comments. */
3051 /* Git's negated patterns are not (yet?) supported. */
3055 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3057 err = got_error_from_errno("asprintf");
3060 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3065 err = got_error_from_errno("getline");
3069 dirpath = strdup(path);
3070 if (dirpath == NULL) {
3071 err = got_error_from_errno("strdup");
3074 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3077 if (err || pe == NULL) {
3079 free_ignorelist(ignorelist);
3085 match_ignores(struct got_pathlist_head *ignores, const char *path)
3087 struct got_pathlist_entry *pe;
3089 /* Handle patterns which match in all directories. */
3090 TAILQ_FOREACH(pe, ignores, entry) {
3091 struct got_pathlist_head *ignorelist = pe->data;
3092 struct got_pathlist_entry *pi;
3094 TAILQ_FOREACH(pi, ignorelist, entry) {
3095 const char *p, *pattern = pi->path;
3097 if (strncmp(pattern, "**/", 3) != 0)
3102 if (fnmatch(pattern, p,
3103 FNM_PATHNAME | FNM_LEADING_DIR)) {
3104 /* Retry in next directory. */
3105 while (*p && *p != '/')
3117 * The ignores pathlist contains ignore lists from children before
3118 * parents, so we can find the most specific ignorelist by walking
3119 * ignores backwards.
3121 pe = TAILQ_LAST(ignores, got_pathlist_head);
3123 if (got_path_is_child(path, pe->path, pe->path_len)) {
3124 struct got_pathlist_head *ignorelist = pe->data;
3125 struct got_pathlist_entry *pi;
3126 TAILQ_FOREACH(pi, ignorelist, entry) {
3127 const char *pattern = pi->path;
3128 int flags = FNM_LEADING_DIR;
3129 if (strstr(pattern, "/**/") == NULL)
3130 flags |= FNM_PATHNAME;
3131 if (fnmatch(pattern, path, flags))
3136 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3142 static const struct got_error *
3143 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3144 const char *path, int dirfd, const char *ignores_filename)
3146 const struct got_error *err = NULL;
3149 FILE *ignoresfile = NULL;
3151 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3152 path[0] ? "/" : "", ignores_filename) == -1)
3153 return got_error_from_errno("asprintf");
3156 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3158 if (errno != ENOENT && errno != EACCES)
3159 err = got_error_from_errno2("openat",
3162 ignoresfile = fdopen(fd, "r");
3163 if (ignoresfile == NULL)
3164 err = got_error_from_errno2("fdopen",
3168 err = read_ignores(ignores, path, ignoresfile);
3172 ignoresfile = fopen(ignorespath, "r");
3173 if (ignoresfile == NULL) {
3174 if (errno != ENOENT && errno != EACCES)
3175 err = got_error_from_errno2("fopen",
3178 err = read_ignores(ignores, path, ignoresfile);
3181 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3182 err = got_error_from_errno2("fclose", path);
3183 if (fd != -1 && close(fd) == -1 && err == NULL)
3184 err = got_error_from_errno2("close", path);
3189 static const struct got_error *
3190 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3192 const struct got_error *err = NULL;
3193 struct diff_dir_cb_arg *a = arg;
3196 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3197 return got_error(GOT_ERR_CANCELLED);
3199 if (parent_path[0]) {
3200 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3201 return got_error_from_errno("asprintf");
3206 if (de->d_type != DT_DIR &&
3207 got_path_is_child(path, a->status_path, a->status_path_len)
3208 && !match_ignores(&a->ignores, path))
3209 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3210 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3216 static const struct got_error *
3217 status_traverse(void *arg, const char *path, int dirfd)
3219 const struct got_error *err = NULL;
3220 struct diff_dir_cb_arg *a = arg;
3225 err = add_ignores(&a->ignores, a->worktree->root_path,
3226 path, dirfd, ".cvsignore");
3230 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3231 dirfd, ".gitignore");
3236 static const struct got_error *
3237 report_single_file_status(const char *path, const char *ondisk_path,
3238 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3239 void *status_arg, struct got_repository *repo, int report_unchanged)
3241 struct got_fileindex_entry *ie;
3244 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3246 return report_file_status(ie, ondisk_path, -1, NULL,
3247 status_cb, status_arg, repo, report_unchanged);
3249 if (lstat(ondisk_path, &sb) == -1) {
3250 if (errno != ENOENT)
3251 return got_error_from_errno2("lstat", ondisk_path);
3252 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3253 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3257 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3258 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3259 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3264 static const struct got_error *
3265 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3266 const char *root_path, const char *path)
3268 const struct got_error *err;
3269 char *parent_path, *next_parent_path = NULL;
3271 err = add_ignores(ignores, root_path, "", -1,
3276 err = add_ignores(ignores, root_path, "", -1,
3281 err = got_path_dirname(&parent_path, path);
3283 if (err->code == GOT_ERR_BAD_PATH)
3284 return NULL; /* cannot traverse parent */
3288 err = add_ignores(ignores, root_path, parent_path, -1,
3292 err = add_ignores(ignores, root_path, parent_path, -1,
3296 err = got_path_dirname(&next_parent_path, parent_path);
3298 if (err->code == GOT_ERR_BAD_PATH)
3299 err = NULL; /* traversed everything */
3303 parent_path = next_parent_path;
3304 next_parent_path = NULL;
3308 free(next_parent_path);
3312 static const struct got_error *
3313 worktree_status(struct got_worktree *worktree, const char *path,
3314 struct got_fileindex *fileindex, struct got_repository *repo,
3315 got_worktree_status_cb status_cb, void *status_arg,
3316 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3317 int report_unchanged)
3319 const struct got_error *err = NULL;
3321 struct got_fileindex_diff_dir_cb fdiff_cb;
3322 struct diff_dir_cb_arg arg;
3323 char *ondisk_path = NULL;
3325 TAILQ_INIT(&arg.ignores);
3327 if (asprintf(&ondisk_path, "%s%s%s",
3328 worktree->root_path, path[0] ? "/" : "", path) == -1)
3329 return got_error_from_errno("asprintf");
3331 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3333 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3335 err = got_error_from_errno2("open", ondisk_path);
3337 err = report_single_file_status(path, ondisk_path,
3338 fileindex, status_cb, status_arg, repo,
3341 fdiff_cb.diff_old_new = status_old_new;
3342 fdiff_cb.diff_old = status_old;
3343 fdiff_cb.diff_new = status_new;
3344 fdiff_cb.diff_traverse = status_traverse;
3345 arg.fileindex = fileindex;
3346 arg.worktree = worktree;
3347 arg.status_path = path;
3348 arg.status_path_len = strlen(path);
3350 arg.status_cb = status_cb;
3351 arg.status_arg = status_arg;
3352 arg.cancel_cb = cancel_cb;
3353 arg.cancel_arg = cancel_arg;
3354 arg.report_unchanged = report_unchanged;
3355 arg.no_ignores = no_ignores;
3357 err = add_ignores_from_parent_paths(&arg.ignores,
3358 worktree->root_path, path);
3362 err = got_fileindex_diff_dir(fileindex, fd,
3363 worktree->root_path, path, repo, &fdiff_cb, &arg);
3366 free_ignores(&arg.ignores);
3367 if (fd != -1 && close(fd) != 0 && err == NULL)
3368 err = got_error_from_errno("close");
3373 const struct got_error *
3374 got_worktree_status(struct got_worktree *worktree,
3375 struct got_pathlist_head *paths, struct got_repository *repo,
3376 got_worktree_status_cb status_cb, void *status_arg,
3377 got_cancel_cb cancel_cb, void *cancel_arg)
3379 const struct got_error *err = NULL;
3380 char *fileindex_path = NULL;
3381 struct got_fileindex *fileindex = NULL;
3382 struct got_pathlist_entry *pe;
3384 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3388 TAILQ_FOREACH(pe, paths, entry) {
3389 err = worktree_status(worktree, pe->path, fileindex, repo,
3390 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3394 free(fileindex_path);
3395 got_fileindex_free(fileindex);
3399 const struct got_error *
3400 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3403 const struct got_error *err = NULL;
3404 char *resolved = NULL, *cwd = NULL, *path = NULL;
3410 cwd = getcwd(NULL, 0);
3412 return got_error_from_errno("getcwd");
3414 if (lstat(arg, &sb) == -1) {
3415 if (errno != ENOENT) {
3416 err = got_error_from_errno2("lstat", arg);
3420 if (S_ISLNK(sb.st_mode)) {
3422 * We cannot use realpath(3) with symlinks since we want to
3423 * operate on the symlink itself.
3424 * But we can make the path absolute, assuming it is relative
3425 * to the current working directory, and then canonicalize it.
3427 char *abspath = NULL;
3428 char canonpath[PATH_MAX];
3429 if (!got_path_is_absolute(arg)) {
3430 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3431 err = got_error_from_errno("asprintf");
3436 err = got_canonpath(abspath ? abspath : arg, canonpath,
3440 resolved = strdup(canonpath);
3441 if (resolved == NULL) {
3442 err = got_error_from_errno("strdup");
3446 resolved = realpath(arg, NULL);
3447 if (resolved == NULL) {
3448 if (errno != ENOENT) {
3449 err = got_error_from_errno2("realpath", arg);
3452 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3453 err = got_error_from_errno("asprintf");
3459 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3460 strlen(got_worktree_get_root_path(worktree)))) {
3461 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3465 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3466 err = got_path_skip_common_ancestor(&path,
3467 got_worktree_get_root_path(worktree), resolved);
3473 err = got_error_from_errno("strdup");
3478 /* XXX status walk can't deal with trailing slash! */
3480 while (len > 0 && path[len - 1] == '/') {
3481 path[len - 1] = '\0';
3494 struct schedule_addition_args {
3495 struct got_worktree *worktree;
3496 struct got_fileindex *fileindex;
3497 got_worktree_checkout_cb progress_cb;
3499 struct got_repository *repo;
3502 static const struct got_error *
3503 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3504 const char *relpath, struct got_object_id *blob_id,
3505 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3506 int dirfd, const char *de_name)
3508 struct schedule_addition_args *a = arg;
3509 const struct got_error *err = NULL;
3510 struct got_fileindex_entry *ie;
3514 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3516 return got_error_from_errno("asprintf");
3518 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3520 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3524 /* Re-adding an existing entry is a no-op. */
3525 if (status == GOT_STATUS_ADD)
3527 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3532 if (status != GOT_STATUS_UNVERSIONED) {
3533 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3537 err = got_fileindex_entry_alloc(&ie, relpath);
3540 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3542 got_fileindex_entry_free(ie);
3545 err = got_fileindex_entry_add(a->fileindex, ie);
3547 got_fileindex_entry_free(ie);
3554 if (status == GOT_STATUS_ADD)
3556 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3559 const struct got_error *
3560 got_worktree_schedule_add(struct got_worktree *worktree,
3561 struct got_pathlist_head *paths,
3562 got_worktree_checkout_cb progress_cb, void *progress_arg,
3563 struct got_repository *repo, int no_ignores)
3565 struct got_fileindex *fileindex = NULL;
3566 char *fileindex_path = NULL;
3567 const struct got_error *err = NULL, *sync_err, *unlockerr;
3568 struct got_pathlist_entry *pe;
3569 struct schedule_addition_args saa;
3571 err = lock_worktree(worktree, LOCK_EX);
3575 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3579 saa.worktree = worktree;
3580 saa.fileindex = fileindex;
3581 saa.progress_cb = progress_cb;
3582 saa.progress_arg = progress_arg;
3585 TAILQ_FOREACH(pe, paths, entry) {
3586 err = worktree_status(worktree, pe->path, fileindex, repo,
3587 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3591 sync_err = sync_fileindex(fileindex, fileindex_path);
3592 if (sync_err && err == NULL)
3595 free(fileindex_path);
3597 got_fileindex_free(fileindex);
3598 unlockerr = lock_worktree(worktree, LOCK_SH);
3599 if (unlockerr && err == NULL)
3604 struct schedule_deletion_args {
3605 struct got_worktree *worktree;
3606 struct got_fileindex *fileindex;
3607 got_worktree_delete_cb progress_cb;
3609 struct got_repository *repo;
3610 int delete_local_mods;
3614 static const struct got_error *
3615 schedule_for_deletion(void *arg, unsigned char status,
3616 unsigned char staged_status, const char *relpath,
3617 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3618 struct got_object_id *commit_id, int dirfd, const char *de_name)
3620 struct schedule_deletion_args *a = arg;
3621 const struct got_error *err = NULL;
3622 struct got_fileindex_entry *ie = NULL;
3624 char *ondisk_path, *parent = NULL;
3626 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3628 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3630 staged_status = get_staged_status(ie);
3631 if (staged_status != GOT_STATUS_NO_CHANGE) {
3632 if (staged_status == GOT_STATUS_DELETE)
3634 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3637 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3639 return got_error_from_errno("asprintf");
3641 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3646 if (status != GOT_STATUS_NO_CHANGE) {
3647 if (status == GOT_STATUS_DELETE)
3649 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3650 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3653 if (status != GOT_STATUS_MODIFY &&
3654 status != GOT_STATUS_MISSING) {
3655 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3660 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3662 if (unlinkat(dirfd, de_name, 0) != 0) {
3663 err = got_error_from_errno2("unlinkat",
3667 } else if (unlink(ondisk_path) != 0) {
3668 err = got_error_from_errno2("unlink", ondisk_path);
3672 parent = dirname(ondisk_path);
3674 if (parent == NULL) {
3675 err = got_error_from_errno2("dirname", ondisk_path);
3678 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3679 if (rmdir(parent) == -1) {
3680 if (errno != ENOTEMPTY)
3681 err = got_error_from_errno2("rmdir",
3685 parent = dirname(parent);
3686 if (parent == NULL) {
3687 err = got_error_from_errno2("dirname", parent);
3693 got_fileindex_entry_mark_deleted_from_disk(ie);
3698 if (status == GOT_STATUS_DELETE)
3700 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3701 staged_status, relpath);
3704 const struct got_error *
3705 got_worktree_schedule_delete(struct got_worktree *worktree,
3706 struct got_pathlist_head *paths, int delete_local_mods,
3707 got_worktree_delete_cb progress_cb, void *progress_arg,
3708 struct got_repository *repo, int keep_on_disk)
3710 struct got_fileindex *fileindex = NULL;
3711 char *fileindex_path = NULL;
3712 const struct got_error *err = NULL, *sync_err, *unlockerr;
3713 struct got_pathlist_entry *pe;
3714 struct schedule_deletion_args sda;
3716 err = lock_worktree(worktree, LOCK_EX);
3720 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3724 sda.worktree = worktree;
3725 sda.fileindex = fileindex;
3726 sda.progress_cb = progress_cb;
3727 sda.progress_arg = progress_arg;
3729 sda.delete_local_mods = delete_local_mods;
3730 sda.keep_on_disk = keep_on_disk;
3732 TAILQ_FOREACH(pe, paths, entry) {
3733 err = worktree_status(worktree, pe->path, fileindex, repo,
3734 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3738 sync_err = sync_fileindex(fileindex, fileindex_path);
3739 if (sync_err && err == NULL)
3742 free(fileindex_path);
3744 got_fileindex_free(fileindex);
3745 unlockerr = lock_worktree(worktree, LOCK_SH);
3746 if (unlockerr && err == NULL)
3751 static const struct got_error *
3752 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3754 const struct got_error *err = NULL;
3756 size_t linesize = 0, n;
3759 linelen = getline(&line, &linesize, infile);
3760 if (linelen == -1) {
3761 if (ferror(infile)) {
3762 err = got_error_from_errno("getline");
3768 n = fwrite(line, 1, linelen, outfile);
3770 err = got_ferror(outfile, GOT_ERR_IO);
3775 n = fwrite(line, 1, linelen, rejectfile);
3777 err = got_ferror(outfile, GOT_ERR_IO);
3784 static const struct got_error *
3785 skip_one_line(FILE *f)
3788 size_t linesize = 0;
3791 linelen = getline(&line, &linesize, f);
3792 if (linelen == -1) {
3794 return got_error_from_errno("getline");
3801 static const struct got_error *
3802 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3803 int start_old, int end_old, int start_new, int end_new,
3804 FILE *outfile, FILE *rejectfile)
3806 const struct got_error *err;
3808 /* Copy old file's lines leading up to patch. */
3809 while (!feof(f1) && *line_cur1 < start_old) {
3810 err = copy_one_line(f1, outfile, NULL);
3815 /* Skip new file's lines leading up to patch. */
3816 while (!feof(f2) && *line_cur2 < start_new) {
3818 err = copy_one_line(f2, NULL, rejectfile);
3820 err = skip_one_line(f2);
3825 /* Copy patched lines. */
3826 while (!feof(f2) && *line_cur2 <= end_new) {
3827 err = copy_one_line(f2, outfile, NULL);
3832 /* Skip over old file's replaced lines. */
3833 while (!feof(f1) && *line_cur1 <= end_old) {
3835 err = copy_one_line(f1, NULL, rejectfile);
3837 err = skip_one_line(f1);
3846 static const struct got_error *
3847 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3848 FILE *outfile, FILE *rejectfile)
3850 const struct got_error *err;
3853 /* Copy old file's lines until EOF. */
3855 err = copy_one_line(f1, outfile, NULL);
3862 /* Copy new file's lines until EOF. */
3864 err = copy_one_line(f2, NULL, rejectfile);
3874 static const struct got_error *
3875 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3876 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3877 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3878 int *line_cur2, FILE *outfile, FILE *rejectfile,
3879 got_worktree_patch_cb patch_cb, void *patch_arg)
3881 const struct got_error *err = NULL;
3882 int start_old = change->cv.a;
3883 int end_old = change->cv.b;
3884 int start_new = change->cv.c;
3885 int end_new = change->cv.d;
3889 *choice = GOT_PATCH_CHOICE_NONE;
3891 hunkfile = got_opentemp();
3892 if (hunkfile == NULL)
3893 return got_error_from_errno("got_opentemp");
3898 /* XXX TODO needs error checking */
3899 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3901 if (fseek(f1, pos1, SEEK_SET) == -1) {
3902 err = got_ferror(f1, GOT_ERR_IO);
3905 if (fseek(f2, pos2, SEEK_SET) == -1) {
3906 err = got_ferror(f1, GOT_ERR_IO);
3909 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3910 err = got_ferror(hunkfile, GOT_ERR_IO);
3914 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3915 hunkfile, n, nchanges);
3920 case GOT_PATCH_CHOICE_YES:
3921 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3922 end_old, start_new, end_new, outfile, rejectfile);
3924 case GOT_PATCH_CHOICE_NO:
3925 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3926 end_old, start_new, end_new, rejectfile, outfile);
3928 case GOT_PATCH_CHOICE_QUIT:
3931 err = got_error(GOT_ERR_PATCH_CHOICE);
3935 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3936 err = got_error_from_errno("fclose");
3940 struct revert_file_args {
3941 struct got_worktree *worktree;
3942 struct got_fileindex *fileindex;
3943 got_worktree_checkout_cb progress_cb;
3945 got_worktree_patch_cb patch_cb;
3947 struct got_repository *repo;
3950 static const struct got_error *
3951 create_patched_content(char **path_outfile, int reverse_patch,
3952 struct got_object_id *blob_id, const char *path2,
3953 int dirfd2, const char *de_name2,
3954 const char *relpath, struct got_repository *repo,
3955 got_worktree_patch_cb patch_cb, void *patch_arg)
3957 const struct got_error *err;
3958 struct got_blob_object *blob = NULL;
3959 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3961 char *path1 = NULL, *id_str = NULL;
3962 struct stat sb1, sb2;
3963 struct got_diff_changes *changes = NULL;
3964 struct got_diff_state *ds = NULL;
3965 struct got_diff_args *args = NULL;
3966 struct got_diff_change *change;
3967 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3970 *path_outfile = NULL;
3972 err = got_object_id_str(&id_str, blob_id);
3977 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3979 err = got_error_from_errno2("openat", path2);
3983 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3985 err = got_error_from_errno2("open", path2);
3989 if (fstat(fd2, &sb2) == -1) {
3990 err = got_error_from_errno2("fstat", path2);
3994 f2 = fdopen(fd2, "r");
3996 err = got_error_from_errno2("fdopen", path2);
4001 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4005 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4009 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4013 if (stat(path1, &sb1) == -1) {
4014 err = got_error_from_errno2("stat", path1);
4018 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4019 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4023 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4027 if (fseek(f1, 0L, SEEK_SET) == -1)
4028 return got_ferror(f1, GOT_ERR_IO);
4029 if (fseek(f2, 0L, SEEK_SET) == -1)
4030 return got_ferror(f2, GOT_ERR_IO);
4031 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4033 err = apply_or_reject_change(&choice, change, ++n,
4034 changes->nchanges, ds, args, diff_flags, relpath,
4035 f1, f2, &line_cur1, &line_cur2,
4036 reverse_patch ? NULL : outfile,
4037 reverse_patch ? outfile : NULL,
4038 patch_cb, patch_arg);
4041 if (choice == GOT_PATCH_CHOICE_YES)
4043 else if (choice == GOT_PATCH_CHOICE_QUIT)
4047 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4048 reverse_patch ? NULL : outfile,
4049 reverse_patch ? outfile : NULL);
4053 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4054 err = got_error_from_errno2("chmod", path2);
4061 got_object_blob_close(blob);
4062 if (f1 && fclose(f1) == EOF && err == NULL)
4063 err = got_error_from_errno2("fclose", path1);
4064 if (f2 && fclose(f2) == EOF && err == NULL)
4065 err = got_error_from_errno2("fclose", path2);
4066 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4067 err = got_error_from_errno2("close", path2);
4068 if (outfile && fclose(outfile) == EOF && err == NULL)
4069 err = got_error_from_errno2("fclose", *path_outfile);
4070 if (path1 && unlink(path1) == -1 && err == NULL)
4071 err = got_error_from_errno2("unlink", path1);
4072 if (err || !have_content) {
4073 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4074 err = got_error_from_errno2("unlink", *path_outfile);
4075 free(*path_outfile);
4076 *path_outfile = NULL;
4080 got_diff_state_free(ds);
4084 got_diff_free_changes(changes);
4089 static const struct got_error *
4090 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4091 const char *relpath, struct got_object_id *blob_id,
4092 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4093 int dirfd, const char *de_name)
4095 struct revert_file_args *a = arg;
4096 const struct got_error *err = NULL;
4097 char *parent_path = NULL;
4098 struct got_fileindex_entry *ie;
4099 struct got_tree_object *tree = NULL;
4100 struct got_object_id *tree_id = NULL;
4101 const struct got_tree_entry *te = NULL;
4102 char *tree_path = NULL, *te_name;
4103 char *ondisk_path = NULL, *path_content = NULL;
4104 struct got_blob_object *blob = NULL;
4106 /* Reverting a staged deletion is a no-op. */
4107 if (status == GOT_STATUS_DELETE &&
4108 staged_status != GOT_STATUS_NO_CHANGE)
4111 if (status == GOT_STATUS_UNVERSIONED)
4112 return (*a->progress_cb)(a->progress_arg,
4113 GOT_STATUS_UNVERSIONED, relpath);
4115 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4117 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4119 /* Construct in-repository path of tree which contains this blob. */
4120 err = got_path_dirname(&parent_path, ie->path);
4122 if (err->code != GOT_ERR_BAD_PATH)
4124 parent_path = strdup("/");
4125 if (parent_path == NULL) {
4126 err = got_error_from_errno("strdup");
4130 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4131 tree_path = strdup(parent_path);
4132 if (tree_path == NULL) {
4133 err = got_error_from_errno("strdup");
4137 if (got_path_is_root_dir(parent_path)) {
4138 tree_path = strdup(a->worktree->path_prefix);
4139 if (tree_path == NULL) {
4140 err = got_error_from_errno("strdup");
4144 if (asprintf(&tree_path, "%s/%s",
4145 a->worktree->path_prefix, parent_path) == -1) {
4146 err = got_error_from_errno("asprintf");
4152 err = got_object_id_by_path(&tree_id, a->repo,
4153 a->worktree->base_commit_id, tree_path);
4155 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4156 (status == GOT_STATUS_ADD ||
4157 staged_status == GOT_STATUS_ADD)))
4160 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4164 te_name = basename(ie->path);
4165 if (te_name == NULL) {
4166 err = got_error_from_errno2("basename", ie->path);
4170 te = got_object_tree_find_entry(tree, te_name);
4171 if (te == NULL && status != GOT_STATUS_ADD &&
4172 staged_status != GOT_STATUS_ADD) {
4173 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4179 case GOT_STATUS_ADD:
4181 int choice = GOT_PATCH_CHOICE_NONE;
4182 err = (*a->patch_cb)(&choice, a->patch_arg,
4183 status, ie->path, NULL, 1, 1);
4186 if (choice != GOT_PATCH_CHOICE_YES)
4189 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4193 got_fileindex_entry_remove(a->fileindex, ie);
4195 case GOT_STATUS_DELETE:
4197 int choice = GOT_PATCH_CHOICE_NONE;
4198 err = (*a->patch_cb)(&choice, a->patch_arg,
4199 status, ie->path, NULL, 1, 1);
4202 if (choice != GOT_PATCH_CHOICE_YES)
4206 case GOT_STATUS_MODIFY:
4207 case GOT_STATUS_MODE_CHANGE:
4208 case GOT_STATUS_CONFLICT:
4209 case GOT_STATUS_MISSING: {
4210 struct got_object_id id;
4211 if (staged_status == GOT_STATUS_ADD ||
4212 staged_status == GOT_STATUS_MODIFY) {
4213 memcpy(id.sha1, ie->staged_blob_sha1,
4214 SHA1_DIGEST_LENGTH);
4216 memcpy(id.sha1, ie->blob_sha1,
4217 SHA1_DIGEST_LENGTH);
4218 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4222 if (asprintf(&ondisk_path, "%s/%s",
4223 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4224 err = got_error_from_errno("asprintf");
4228 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4229 status == GOT_STATUS_CONFLICT)) {
4230 err = create_patched_content(&path_content, 1, &id,
4231 ondisk_path, dirfd, de_name, ie->path, a->repo,
4232 a->patch_cb, a->patch_arg);
4233 if (err || path_content == NULL)
4235 if (rename(path_content, ondisk_path) == -1) {
4236 err = got_error_from_errno3("rename",
4237 path_content, ondisk_path);
4241 err = install_blob(a->worktree, ondisk_path, ie->path,
4242 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4243 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4244 a->repo, a->progress_cb, a->progress_arg);
4247 if (status == GOT_STATUS_DELETE ||
4248 status == GOT_STATUS_MODE_CHANGE) {
4249 err = got_fileindex_entry_update(ie,
4250 ondisk_path, blob->id.sha1,
4251 a->worktree->base_commit_id->sha1, 1);
4267 got_object_blob_close(blob);
4269 got_object_tree_close(tree);
4274 const struct got_error *
4275 got_worktree_revert(struct got_worktree *worktree,
4276 struct got_pathlist_head *paths,
4277 got_worktree_checkout_cb progress_cb, void *progress_arg,
4278 got_worktree_patch_cb patch_cb, void *patch_arg,
4279 struct got_repository *repo)
4281 struct got_fileindex *fileindex = NULL;
4282 char *fileindex_path = NULL;
4283 const struct got_error *err = NULL, *unlockerr = NULL;
4284 const struct got_error *sync_err = NULL;
4285 struct got_pathlist_entry *pe;
4286 struct revert_file_args rfa;
4288 err = lock_worktree(worktree, LOCK_EX);
4292 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4296 rfa.worktree = worktree;
4297 rfa.fileindex = fileindex;
4298 rfa.progress_cb = progress_cb;
4299 rfa.progress_arg = progress_arg;
4300 rfa.patch_cb = patch_cb;
4301 rfa.patch_arg = patch_arg;
4303 TAILQ_FOREACH(pe, paths, entry) {
4304 err = worktree_status(worktree, pe->path, fileindex, repo,
4305 revert_file, &rfa, NULL, NULL, 0, 0);
4309 sync_err = sync_fileindex(fileindex, fileindex_path);
4310 if (sync_err && err == NULL)
4313 free(fileindex_path);
4315 got_fileindex_free(fileindex);
4316 unlockerr = lock_worktree(worktree, LOCK_SH);
4317 if (unlockerr && err == NULL)
4323 free_commitable(struct got_commitable *ct)
4326 free(ct->in_repo_path);
4327 free(ct->ondisk_path);
4329 free(ct->base_blob_id);
4330 free(ct->staged_blob_id);
4331 free(ct->base_commit_id);
4335 struct collect_commitables_arg {
4336 struct got_pathlist_head *commitable_paths;
4337 struct got_repository *repo;
4338 struct got_worktree *worktree;
4339 int have_staged_files;
4342 static const struct got_error *
4343 collect_commitables(void *arg, unsigned char status,
4344 unsigned char staged_status, const char *relpath,
4345 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4346 struct got_object_id *commit_id, int dirfd, const char *de_name)
4348 struct collect_commitables_arg *a = arg;
4349 const struct got_error *err = NULL;
4350 struct got_commitable *ct = NULL;
4351 struct got_pathlist_entry *new = NULL;
4352 char *parent_path = NULL, *path = NULL;
4355 if (a->have_staged_files) {
4356 if (staged_status != GOT_STATUS_MODIFY &&
4357 staged_status != GOT_STATUS_ADD &&
4358 staged_status != GOT_STATUS_DELETE)
4361 if (status == GOT_STATUS_CONFLICT)
4362 return got_error(GOT_ERR_COMMIT_CONFLICT);
4364 if (status != GOT_STATUS_MODIFY &&
4365 status != GOT_STATUS_MODE_CHANGE &&
4366 status != GOT_STATUS_ADD &&
4367 status != GOT_STATUS_DELETE)
4371 if (asprintf(&path, "/%s", relpath) == -1) {
4372 err = got_error_from_errno("asprintf");
4375 if (strcmp(path, "/") == 0) {
4376 parent_path = strdup("");
4377 if (parent_path == NULL)
4378 return got_error_from_errno("strdup");
4380 err = got_path_dirname(&parent_path, path);
4385 ct = calloc(1, sizeof(*ct));
4387 err = got_error_from_errno("calloc");
4391 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4393 err = got_error_from_errno("asprintf");
4396 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4397 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4400 if (fstatat(dirfd, de_name, &sb,
4401 AT_SYMLINK_NOFOLLOW) == -1) {
4402 err = got_error_from_errno2("fstatat",
4406 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4407 err = got_error_from_errno2("lstat", ct->ondisk_path);
4410 ct->mode = sb.st_mode;
4413 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4414 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4416 err = got_error_from_errno("asprintf");
4420 ct->status = status;
4421 ct->staged_status = staged_status;
4422 ct->blob_id = NULL; /* will be filled in when blob gets created */
4423 if (ct->status != GOT_STATUS_ADD &&
4424 ct->staged_status != GOT_STATUS_ADD) {
4425 ct->base_blob_id = got_object_id_dup(blob_id);
4426 if (ct->base_blob_id == NULL) {
4427 err = got_error_from_errno("got_object_id_dup");
4430 ct->base_commit_id = got_object_id_dup(commit_id);
4431 if (ct->base_commit_id == NULL) {
4432 err = got_error_from_errno("got_object_id_dup");
4436 if (ct->staged_status == GOT_STATUS_ADD ||
4437 ct->staged_status == GOT_STATUS_MODIFY) {
4438 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4439 if (ct->staged_blob_id == NULL) {
4440 err = got_error_from_errno("got_object_id_dup");
4444 ct->path = strdup(path);
4445 if (ct->path == NULL) {
4446 err = got_error_from_errno("strdup");
4449 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4451 if (ct && (err || new == NULL))
4452 free_commitable(ct);
4458 static const struct got_error *write_tree(struct got_object_id **, int *,
4459 struct got_tree_object *, const char *, struct got_pathlist_head *,
4460 got_worktree_status_cb status_cb, void *status_arg,
4461 struct got_repository *);
4463 static const struct got_error *
4464 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4465 struct got_tree_entry *te, const char *parent_path,
4466 struct got_pathlist_head *commitable_paths,
4467 got_worktree_status_cb status_cb, void *status_arg,
4468 struct got_repository *repo)
4470 const struct got_error *err = NULL;
4471 struct got_tree_object *subtree;
4474 if (asprintf(&subpath, "%s%s%s", parent_path,
4475 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4476 return got_error_from_errno("asprintf");
4478 err = got_object_open_as_tree(&subtree, repo, &te->id);
4482 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4483 commitable_paths, status_cb, status_arg, repo);
4484 got_object_tree_close(subtree);
4489 static const struct got_error *
4490 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4492 const struct got_error *err = NULL;
4493 char *ct_parent_path = NULL;
4497 if (strchr(ct->in_repo_path, '/') == NULL) {
4498 *match = got_path_is_root_dir(path);
4502 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4505 *match = (strcmp(path, ct_parent_path) == 0);
4506 free(ct_parent_path);
4511 get_ct_file_mode(struct got_commitable *ct)
4513 if (S_ISLNK(ct->mode))
4516 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4519 static const struct got_error *
4520 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4521 struct got_tree_entry *te, struct got_commitable *ct)
4523 const struct got_error *err = NULL;
4527 err = got_object_tree_entry_dup(new_te, te);
4531 (*new_te)->mode = get_ct_file_mode(ct);
4533 if (ct->staged_status == GOT_STATUS_MODIFY)
4534 memcpy(&(*new_te)->id, ct->staged_blob_id,
4535 sizeof((*new_te)->id));
4537 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4539 if (err && *new_te) {
4546 static const struct got_error *
4547 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4548 struct got_commitable *ct)
4550 const struct got_error *err = NULL;
4555 *new_te = calloc(1, sizeof(**new_te));
4556 if (*new_te == NULL)
4557 return got_error_from_errno("calloc");
4559 ct_name = basename(ct->path);
4560 if (ct_name == NULL) {
4561 err = got_error_from_errno2("basename", ct->path);
4564 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4565 sizeof((*new_te)->name)) {
4566 err = got_error(GOT_ERR_NO_SPACE);
4570 (*new_te)->mode = get_ct_file_mode(ct);
4572 if (ct->staged_status == GOT_STATUS_ADD)
4573 memcpy(&(*new_te)->id, ct->staged_blob_id,
4574 sizeof((*new_te)->id));
4576 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4578 if (err && *new_te) {
4585 static const struct got_error *
4586 insert_tree_entry(struct got_tree_entry *new_te,
4587 struct got_pathlist_head *paths)
4589 const struct got_error *err = NULL;
4590 struct got_pathlist_entry *new_pe;
4592 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4596 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4600 static const struct got_error *
4601 report_ct_status(struct got_commitable *ct,
4602 got_worktree_status_cb status_cb, void *status_arg)
4604 const char *ct_path = ct->path;
4605 unsigned char status;
4607 while (ct_path[0] == '/')
4610 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4611 status = ct->staged_status;
4613 status = ct->status;
4615 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4616 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4619 static const struct got_error *
4620 match_modified_subtree(int *modified, struct got_tree_entry *te,
4621 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4623 const struct got_error *err = NULL;
4624 struct got_pathlist_entry *pe;
4629 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4630 got_path_is_root_dir(base_tree_path) ? "" : "/",
4632 return got_error_from_errno("asprintf");
4634 TAILQ_FOREACH(pe, commitable_paths, entry) {
4635 struct got_commitable *ct = pe->data;
4636 *modified = got_path_is_child(ct->in_repo_path, te_path,
4646 static const struct got_error *
4647 match_deleted_or_modified_ct(struct got_commitable **ctp,
4648 struct got_tree_entry *te, const char *base_tree_path,
4649 struct got_pathlist_head *commitable_paths)
4651 const struct got_error *err = NULL;
4652 struct got_pathlist_entry *pe;
4656 TAILQ_FOREACH(pe, commitable_paths, entry) {
4657 struct got_commitable *ct = pe->data;
4658 char *ct_name = NULL;
4661 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4662 if (ct->status != GOT_STATUS_MODIFY &&
4663 ct->status != GOT_STATUS_MODE_CHANGE &&
4664 ct->status != GOT_STATUS_DELETE)
4667 if (ct->staged_status != GOT_STATUS_MODIFY &&
4668 ct->staged_status != GOT_STATUS_DELETE)
4672 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4675 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4681 ct_name = basename(pe->path);
4682 if (ct_name == NULL)
4683 return got_error_from_errno2("basename", pe->path);
4685 if (strcmp(te->name, ct_name) != 0)
4695 static const struct got_error *
4696 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4697 const char *child_path, const char *path_base_tree,
4698 struct got_pathlist_head *commitable_paths,
4699 got_worktree_status_cb status_cb, void *status_arg,
4700 struct got_repository *repo)
4702 const struct got_error *err = NULL;
4703 struct got_tree_entry *new_te;
4705 struct got_object_id *id = NULL;
4710 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4711 got_path_is_root_dir(path_base_tree) ? "" : "/",
4713 return got_error_from_errno("asprintf");
4715 new_te = calloc(1, sizeof(*new_te));
4717 return got_error_from_errno("calloc");
4718 new_te->mode = S_IFDIR;
4720 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4721 sizeof(new_te->name)) {
4722 err = got_error(GOT_ERR_NO_SPACE);
4725 err = write_tree(&id, &nentries, NULL, subtree_path,
4726 commitable_paths, status_cb, status_arg, repo);
4731 memcpy(&new_te->id, id, sizeof(new_te->id));
4740 static const struct got_error *
4741 write_tree(struct got_object_id **new_tree_id, int *nentries,
4742 struct got_tree_object *base_tree, const char *path_base_tree,
4743 struct got_pathlist_head *commitable_paths,
4744 got_worktree_status_cb status_cb, void *status_arg,
4745 struct got_repository *repo)
4747 const struct got_error *err = NULL;
4748 struct got_pathlist_head paths;
4749 struct got_tree_entry *te, *new_te = NULL;
4750 struct got_pathlist_entry *pe;
4755 /* Insert, and recurse into, newly added entries first. */
4756 TAILQ_FOREACH(pe, commitable_paths, entry) {
4757 struct got_commitable *ct = pe->data;
4758 char *child_path = NULL, *slash;
4760 if ((ct->status != GOT_STATUS_ADD &&
4761 ct->staged_status != GOT_STATUS_ADD) ||
4762 (ct->flags & GOT_COMMITABLE_ADDED))
4765 if (!got_path_is_child(pe->path, path_base_tree,
4766 strlen(path_base_tree)))
4769 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4774 slash = strchr(child_path, '/');
4775 if (slash == NULL) {
4776 err = alloc_added_blob_tree_entry(&new_te, ct);
4779 err = report_ct_status(ct, status_cb, status_arg);
4782 ct->flags |= GOT_COMMITABLE_ADDED;
4783 err = insert_tree_entry(new_te, &paths);
4788 *slash = '\0'; /* trim trailing path components */
4789 if (base_tree == NULL ||
4790 got_object_tree_find_entry(base_tree, child_path)
4792 err = make_subtree_for_added_blob(&new_te,
4793 child_path, path_base_tree,
4794 commitable_paths, status_cb, status_arg,
4798 err = insert_tree_entry(new_te, &paths);
4807 int i, nbase_entries;
4808 /* Handle modified and deleted entries. */
4809 nbase_entries = got_object_tree_get_nentries(base_tree);
4810 for (i = 0; i < nbase_entries; i++) {
4811 struct got_commitable *ct = NULL;
4813 te = got_object_tree_get_entry(base_tree, i);
4814 if (got_object_tree_entry_is_submodule(te)) {
4815 /* Entry is a submodule; just copy it. */
4816 err = got_object_tree_entry_dup(&new_te, te);
4819 err = insert_tree_entry(new_te, &paths);
4826 if (S_ISDIR(te->mode)) {
4828 err = got_object_tree_entry_dup(&new_te, te);
4831 err = match_modified_subtree(&modified, te,
4832 path_base_tree, commitable_paths);
4835 /* Avoid recursion into unmodified subtrees. */
4837 struct got_object_id *new_id;
4839 err = write_subtree(&new_id,
4841 path_base_tree, commitable_paths,
4842 status_cb, status_arg, repo);
4845 if (nsubentries == 0) {
4846 /* All entries were deleted. */
4850 memcpy(&new_te->id, new_id,
4851 sizeof(new_te->id));
4854 err = insert_tree_entry(new_te, &paths);
4861 err = match_deleted_or_modified_ct(&ct, te,
4862 path_base_tree, commitable_paths);
4866 /* NB: Deleted entries get dropped here. */
4867 if (ct->status == GOT_STATUS_MODIFY ||
4868 ct->status == GOT_STATUS_MODE_CHANGE ||
4869 ct->staged_status == GOT_STATUS_MODIFY) {
4870 err = alloc_modified_blob_tree_entry(
4874 err = insert_tree_entry(new_te, &paths);
4879 err = report_ct_status(ct, status_cb,
4884 /* Entry is unchanged; just copy it. */
4885 err = got_object_tree_entry_dup(&new_te, te);
4888 err = insert_tree_entry(new_te, &paths);
4896 /* Write new list of entries; deleted entries have been dropped. */
4897 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4899 got_pathlist_free(&paths);
4903 static const struct got_error *
4904 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4905 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4906 int have_staged_files)
4908 const struct got_error *err = NULL;
4909 struct got_pathlist_entry *pe;
4911 TAILQ_FOREACH(pe, commitable_paths, entry) {
4912 struct got_fileindex_entry *ie;
4913 struct got_commitable *ct = pe->data;
4915 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4917 if (ct->status == GOT_STATUS_DELETE ||
4918 ct->staged_status == GOT_STATUS_DELETE) {
4919 got_fileindex_entry_remove(fileindex, ie);
4920 } else if (ct->staged_status == GOT_STATUS_ADD ||
4921 ct->staged_status == GOT_STATUS_MODIFY) {
4922 got_fileindex_entry_stage_set(ie,
4923 GOT_FILEIDX_STAGE_NONE);
4924 err = got_fileindex_entry_update(ie,
4925 ct->ondisk_path, ct->staged_blob_id->sha1,
4926 new_base_commit_id->sha1,
4927 !have_staged_files);
4929 err = got_fileindex_entry_update(ie,
4930 ct->ondisk_path, ct->blob_id->sha1,
4931 new_base_commit_id->sha1,
4932 !have_staged_files);
4934 err = got_fileindex_entry_alloc(&ie, pe->path);
4937 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4938 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4940 got_fileindex_entry_free(ie);
4943 err = got_fileindex_entry_add(fileindex, ie);
4945 got_fileindex_entry_free(ie);
4954 static const struct got_error *
4955 check_out_of_date(const char *in_repo_path, unsigned char status,
4956 unsigned char staged_status, struct got_object_id *base_blob_id,
4957 struct got_object_id *base_commit_id,
4958 struct got_object_id *head_commit_id, struct got_repository *repo,
4961 const struct got_error *err = NULL;
4962 struct got_object_id *id = NULL;
4964 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4965 /* Trivial case: base commit == head commit */
4966 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4969 * Ensure file content which local changes were based
4970 * on matches file content in the branch head.
4972 err = got_object_id_by_path(&id, repo, head_commit_id,
4975 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4976 err = got_error(ood_errcode);
4978 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4979 err = got_error(ood_errcode);
4981 /* Require that added files don't exist in the branch head. */
4982 err = got_object_id_by_path(&id, repo, head_commit_id,
4984 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4986 err = id ? got_error(ood_errcode) : NULL;
4993 const struct got_error *
4994 commit_worktree(struct got_object_id **new_commit_id,
4995 struct got_pathlist_head *commitable_paths,
4996 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4997 const char *author, const char *committer,
4998 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4999 got_worktree_status_cb status_cb, void *status_arg,
5000 struct got_repository *repo)
5002 const struct got_error *err = NULL, *unlockerr = NULL;
5003 struct got_pathlist_entry *pe;
5004 const char *head_ref_name = NULL;
5005 struct got_commit_object *head_commit = NULL;
5006 struct got_reference *head_ref2 = NULL;
5007 struct got_object_id *head_commit_id2 = NULL;
5008 struct got_tree_object *head_tree = NULL;
5009 struct got_object_id *new_tree_id = NULL;
5011 struct got_object_id_queue parent_ids;
5012 struct got_object_qid *pid = NULL;
5013 char *logmsg = NULL;
5015 *new_commit_id = NULL;
5017 SIMPLEQ_INIT(&parent_ids);
5019 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5023 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5027 if (commit_msg_cb != NULL) {
5028 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5033 if (logmsg == NULL || strlen(logmsg) == 0) {
5034 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5038 /* Create blobs from added and modified files and record their IDs. */
5039 TAILQ_FOREACH(pe, commitable_paths, entry) {
5040 struct got_commitable *ct = pe->data;
5043 /* Blobs for staged files already exist. */
5044 if (ct->staged_status == GOT_STATUS_ADD ||
5045 ct->staged_status == GOT_STATUS_MODIFY)
5048 if (ct->status != GOT_STATUS_ADD &&
5049 ct->status != GOT_STATUS_MODIFY &&
5050 ct->status != GOT_STATUS_MODE_CHANGE)
5053 if (asprintf(&ondisk_path, "%s/%s",
5054 worktree->root_path, pe->path) == -1) {
5055 err = got_error_from_errno("asprintf");
5058 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5065 * When comitting a symlink we convert "bad" symlinks (those
5066 * which point outside the work tree or into .got) to regular
5067 * files. This way, the post-commit work tree state matches
5068 * a fresh checkout of the tree which was committed.
5070 if (S_ISLNK(get_ct_file_mode(ct))) {
5071 struct got_blob_object *blob;
5072 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5078 err = install_symlink(worktree, ondisk_path, ct->path,
5079 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
5080 0, 0, repo, NULL, NULL);
5081 got_object_blob_close(blob);
5090 /* Recursively write new tree objects. */
5091 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5092 commitable_paths, status_cb, status_arg, repo);
5096 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5099 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5100 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5101 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5102 got_object_qid_free(pid);
5108 /* Check if a concurrent commit to our branch has occurred. */
5109 head_ref_name = got_worktree_get_head_ref_name(worktree);
5110 if (head_ref_name == NULL) {
5111 err = got_error_from_errno("got_worktree_get_head_ref_name");
5114 /* Lock the reference here to prevent concurrent modification. */
5115 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5118 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5121 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5122 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5125 /* Update branch head in repository. */
5126 err = got_ref_change_ref(head_ref2, *new_commit_id);
5129 err = got_ref_write(head_ref2, repo);
5133 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5137 err = ref_base_commit(worktree, repo);
5142 got_object_tree_close(head_tree);
5144 got_object_commit_close(head_commit);
5145 free(head_commit_id2);
5147 unlockerr = got_ref_unlock(head_ref2);
5148 if (unlockerr && err == NULL)
5150 got_ref_close(head_ref2);
5155 static const struct got_error *
5156 check_path_is_commitable(const char *path,
5157 struct got_pathlist_head *commitable_paths)
5159 struct got_pathlist_entry *cpe = NULL;
5160 size_t path_len = strlen(path);
5162 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5163 struct got_commitable *ct = cpe->data;
5164 const char *ct_path = ct->path;
5166 while (ct_path[0] == '/')
5169 if (strcmp(path, ct_path) == 0 ||
5170 got_path_is_child(ct_path, path, path_len))
5175 return got_error_path(path, GOT_ERR_BAD_PATH);
5180 static const struct got_error *
5181 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5183 int *have_staged_files = arg;
5185 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5186 *have_staged_files = 1;
5187 return got_error(GOT_ERR_CANCELLED);
5193 static const struct got_error *
5194 check_non_staged_files(struct got_fileindex *fileindex,
5195 struct got_pathlist_head *paths)
5197 struct got_pathlist_entry *pe;
5198 struct got_fileindex_entry *ie;
5200 TAILQ_FOREACH(pe, paths, entry) {
5201 if (pe->path[0] == '\0')
5203 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5205 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5206 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5207 return got_error_path(pe->path,
5208 GOT_ERR_FILE_NOT_STAGED);
5214 const struct got_error *
5215 got_worktree_commit(struct got_object_id **new_commit_id,
5216 struct got_worktree *worktree, struct got_pathlist_head *paths,
5217 const char *author, const char *committer,
5218 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5219 got_worktree_status_cb status_cb, void *status_arg,
5220 struct got_repository *repo)
5222 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5223 struct got_fileindex *fileindex = NULL;
5224 char *fileindex_path = NULL;
5225 struct got_pathlist_head commitable_paths;
5226 struct collect_commitables_arg cc_arg;
5227 struct got_pathlist_entry *pe;
5228 struct got_reference *head_ref = NULL;
5229 struct got_object_id *head_commit_id = NULL;
5230 int have_staged_files = 0;
5232 *new_commit_id = NULL;
5234 TAILQ_INIT(&commitable_paths);
5236 err = lock_worktree(worktree, LOCK_EX);
5240 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5244 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5248 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5252 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5253 &have_staged_files);
5254 if (err && err->code != GOT_ERR_CANCELLED)
5256 if (have_staged_files) {
5257 err = check_non_staged_files(fileindex, paths);
5262 cc_arg.commitable_paths = &commitable_paths;
5263 cc_arg.worktree = worktree;
5265 cc_arg.have_staged_files = have_staged_files;
5266 TAILQ_FOREACH(pe, paths, entry) {
5267 err = worktree_status(worktree, pe->path, fileindex, repo,
5268 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5273 if (TAILQ_EMPTY(&commitable_paths)) {
5274 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5278 TAILQ_FOREACH(pe, paths, entry) {
5279 err = check_path_is_commitable(pe->path, &commitable_paths);
5284 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5285 struct got_commitable *ct = pe->data;
5286 const char *ct_path = ct->in_repo_path;
5288 while (ct_path[0] == '/')
5290 err = check_out_of_date(ct_path, ct->status,
5291 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5292 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5298 err = commit_worktree(new_commit_id, &commitable_paths,
5299 head_commit_id, worktree, author, committer,
5300 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5304 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5305 fileindex, have_staged_files);
5306 sync_err = sync_fileindex(fileindex, fileindex_path);
5307 if (sync_err && err == NULL)
5311 got_fileindex_free(fileindex);
5312 free(fileindex_path);
5313 unlockerr = lock_worktree(worktree, LOCK_SH);
5314 if (unlockerr && err == NULL)
5316 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5317 struct got_commitable *ct = pe->data;
5318 free_commitable(ct);
5320 got_pathlist_free(&commitable_paths);
5325 got_commitable_get_path(struct got_commitable *ct)
5331 got_commitable_get_status(struct got_commitable *ct)
5336 struct check_rebase_ok_arg {
5337 struct got_worktree *worktree;
5338 struct got_repository *repo;
5341 static const struct got_error *
5342 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5344 const struct got_error *err = NULL;
5345 struct check_rebase_ok_arg *a = arg;
5346 unsigned char status;
5350 /* Reject rebase of a work tree with mixed base commits. */
5351 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5352 SHA1_DIGEST_LENGTH))
5353 return got_error(GOT_ERR_MIXED_COMMITS);
5355 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5357 return got_error_from_errno("asprintf");
5359 /* Reject rebase of a work tree with modified or staged files. */
5360 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5365 if (status != GOT_STATUS_NO_CHANGE)
5366 return got_error(GOT_ERR_MODIFIED);
5367 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5368 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5373 const struct got_error *
5374 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5375 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5376 struct got_worktree *worktree, struct got_reference *branch,
5377 struct got_repository *repo)
5379 const struct got_error *err = NULL;
5380 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5381 char *branch_ref_name = NULL;
5382 char *fileindex_path = NULL;
5383 struct check_rebase_ok_arg ok_arg;
5384 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5385 struct got_object_id *wt_branch_tip = NULL;
5387 *new_base_branch_ref = NULL;
5391 err = lock_worktree(worktree, LOCK_EX);
5395 err = open_fileindex(fileindex, &fileindex_path, worktree);
5399 ok_arg.worktree = worktree;
5401 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5406 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5410 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5414 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5418 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5423 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5426 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5427 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5431 err = got_ref_alloc_symref(new_base_branch_ref,
5432 new_base_branch_ref_name, wt_branch);
5435 err = got_ref_write(*new_base_branch_ref, repo);
5439 /* TODO Lock original branch's ref while rebasing? */
5441 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5445 err = got_ref_write(branch_ref, repo);
5449 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5450 worktree->base_commit_id);
5453 err = got_ref_write(*tmp_branch, repo);
5457 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5461 free(fileindex_path);
5462 free(tmp_branch_name);
5463 free(new_base_branch_ref_name);
5464 free(branch_ref_name);
5466 got_ref_close(branch_ref);
5468 got_ref_close(wt_branch);
5469 free(wt_branch_tip);
5471 if (*new_base_branch_ref) {
5472 got_ref_close(*new_base_branch_ref);
5473 *new_base_branch_ref = NULL;
5476 got_ref_close(*tmp_branch);
5480 got_fileindex_free(*fileindex);
5483 lock_worktree(worktree, LOCK_SH);
5488 const struct got_error *
5489 got_worktree_rebase_continue(struct got_object_id **commit_id,
5490 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5491 struct got_reference **branch, struct got_fileindex **fileindex,
5492 struct got_worktree *worktree, struct got_repository *repo)
5494 const struct got_error *err;
5495 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5496 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5497 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5498 char *fileindex_path = NULL;
5499 int have_staged_files = 0;
5502 *new_base_branch = NULL;
5507 err = lock_worktree(worktree, LOCK_EX);
5511 err = open_fileindex(fileindex, &fileindex_path, worktree);
5515 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5516 &have_staged_files);
5517 if (err && err->code != GOT_ERR_CANCELLED)
5519 if (have_staged_files) {
5520 err = got_error(GOT_ERR_STAGED_PATHS);
5524 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5528 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5532 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5536 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5540 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5544 err = got_ref_open(branch, repo,
5545 got_ref_get_symref_target(branch_ref), 0);
5549 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5553 err = got_ref_resolve(commit_id, repo, commit_ref);
5557 err = got_ref_open(new_base_branch, repo,
5558 new_base_branch_ref_name, 0);
5562 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5566 free(commit_ref_name);
5567 free(branch_ref_name);
5568 free(fileindex_path);
5570 got_ref_close(commit_ref);
5572 got_ref_close(branch_ref);
5577 got_ref_close(*tmp_branch);
5580 if (*new_base_branch) {
5581 got_ref_close(*new_base_branch);
5582 *new_base_branch = NULL;
5585 got_ref_close(*branch);
5589 got_fileindex_free(*fileindex);
5592 lock_worktree(worktree, LOCK_SH);
5597 const struct got_error *
5598 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5600 const struct got_error *err;
5601 char *tmp_branch_name = NULL;
5603 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5607 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5608 free(tmp_branch_name);
5612 static const struct got_error *
5613 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5614 char **logmsg, void *arg)
5620 static const struct got_error *
5621 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5622 const char *path, struct got_object_id *blob_id,
5623 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5624 int dirfd, const char *de_name)
5629 struct collect_merged_paths_arg {
5630 got_worktree_checkout_cb progress_cb;
5632 struct got_pathlist_head *merged_paths;
5635 static const struct got_error *
5636 collect_merged_paths(void *arg, unsigned char status, const char *path)
5638 const struct got_error *err;
5639 struct collect_merged_paths_arg *a = arg;
5641 struct got_pathlist_entry *new;
5643 err = (*a->progress_cb)(a->progress_arg, status, path);
5647 if (status != GOT_STATUS_MERGE &&
5648 status != GOT_STATUS_ADD &&
5649 status != GOT_STATUS_DELETE &&
5650 status != GOT_STATUS_CONFLICT)
5655 return got_error_from_errno("strdup");
5657 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5658 if (err || new == NULL)
5664 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5666 struct got_pathlist_entry *pe;
5668 TAILQ_FOREACH(pe, merged_paths, entry)
5669 free((char *)pe->path);
5671 got_pathlist_free(merged_paths);
5674 static const struct got_error *
5675 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5676 int is_rebase, struct got_repository *repo)
5678 const struct got_error *err;
5679 struct got_reference *commit_ref = NULL;
5681 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5683 if (err->code != GOT_ERR_NOT_REF)
5685 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5688 err = got_ref_write(commit_ref, repo);
5691 } else if (is_rebase) {
5692 struct got_object_id *stored_id;
5695 err = got_ref_resolve(&stored_id, repo, commit_ref);
5698 cmp = got_object_id_cmp(commit_id, stored_id);
5701 err = got_error(GOT_ERR_REBASE_COMMITID);
5707 got_ref_close(commit_ref);
5711 static const struct got_error *
5712 rebase_merge_files(struct got_pathlist_head *merged_paths,
5713 const char *commit_ref_name, struct got_worktree *worktree,
5714 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5715 struct got_object_id *commit_id, struct got_repository *repo,
5716 got_worktree_checkout_cb progress_cb, void *progress_arg,
5717 got_cancel_cb cancel_cb, void *cancel_arg)
5719 const struct got_error *err;
5720 struct got_reference *commit_ref = NULL;
5721 struct collect_merged_paths_arg cmp_arg;
5722 char *fileindex_path;
5724 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5726 err = get_fileindex_path(&fileindex_path, worktree);
5730 cmp_arg.progress_cb = progress_cb;
5731 cmp_arg.progress_arg = progress_arg;
5732 cmp_arg.merged_paths = merged_paths;
5733 err = merge_files(worktree, fileindex, fileindex_path,
5734 parent_commit_id, commit_id, repo, collect_merged_paths,
5735 &cmp_arg, cancel_cb, cancel_arg);
5737 got_ref_close(commit_ref);
5741 const struct got_error *
5742 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5743 struct got_worktree *worktree, struct got_fileindex *fileindex,
5744 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5745 struct got_repository *repo,
5746 got_worktree_checkout_cb progress_cb, void *progress_arg,
5747 got_cancel_cb cancel_cb, void *cancel_arg)
5749 const struct got_error *err;
5750 char *commit_ref_name;
5752 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5756 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5760 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5761 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5762 progress_arg, cancel_cb, cancel_arg);
5764 free(commit_ref_name);
5768 const struct got_error *
5769 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5770 struct got_worktree *worktree, struct got_fileindex *fileindex,
5771 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5772 struct got_repository *repo,
5773 got_worktree_checkout_cb progress_cb, void *progress_arg,
5774 got_cancel_cb cancel_cb, void *cancel_arg)
5776 const struct got_error *err;
5777 char *commit_ref_name;
5779 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5783 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5787 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5788 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5789 progress_arg, cancel_cb, cancel_arg);
5791 free(commit_ref_name);
5795 static const struct got_error *
5796 rebase_commit(struct got_object_id **new_commit_id,
5797 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5798 struct got_worktree *worktree, struct got_fileindex *fileindex,
5799 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5800 const char *new_logmsg, struct got_repository *repo)
5802 const struct got_error *err, *sync_err;
5803 struct got_pathlist_head commitable_paths;
5804 struct collect_commitables_arg cc_arg;
5805 char *fileindex_path = NULL;
5806 struct got_reference *head_ref = NULL;
5807 struct got_object_id *head_commit_id = NULL;
5808 char *logmsg = NULL;
5810 TAILQ_INIT(&commitable_paths);
5811 *new_commit_id = NULL;
5813 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5815 err = get_fileindex_path(&fileindex_path, worktree);
5819 cc_arg.commitable_paths = &commitable_paths;
5820 cc_arg.worktree = worktree;
5822 cc_arg.have_staged_files = 0;
5824 * If possible get the status of individual files directly to
5825 * avoid crawling the entire work tree once per rebased commit.
5826 * TODO: Ideally, merged_paths would contain a list of commitables
5827 * we could use so we could skip worktree_status() entirely.
5830 struct got_pathlist_entry *pe;
5831 TAILQ_FOREACH(pe, merged_paths, entry) {
5832 err = worktree_status(worktree, pe->path, fileindex,
5833 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5839 err = worktree_status(worktree, "", fileindex, repo,
5840 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5845 if (TAILQ_EMPTY(&commitable_paths)) {
5846 /* No-op change; commit will be elided. */
5847 err = got_ref_delete(commit_ref, repo);
5850 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5854 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5858 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5863 logmsg = strdup(new_logmsg);
5864 if (logmsg == NULL) {
5865 err = got_error_from_errno("strdup");
5869 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5874 /* NB: commit_worktree will call free(logmsg) */
5875 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5876 worktree, got_object_commit_get_author(orig_commit),
5877 got_object_commit_get_committer(orig_commit),
5878 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5882 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5886 err = got_ref_delete(commit_ref, repo);
5890 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5892 sync_err = sync_fileindex(fileindex, fileindex_path);
5893 if (sync_err && err == NULL)
5896 free(fileindex_path);
5897 free(head_commit_id);
5899 got_ref_close(head_ref);
5901 free(*new_commit_id);
5902 *new_commit_id = NULL;
5907 const struct got_error *
5908 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5909 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5910 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5911 struct got_commit_object *orig_commit,
5912 struct got_object_id *orig_commit_id, struct got_repository *repo)
5914 const struct got_error *err;
5915 char *commit_ref_name;
5916 struct got_reference *commit_ref = NULL;
5917 struct got_object_id *commit_id = NULL;
5919 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5923 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5926 err = got_ref_resolve(&commit_id, repo, commit_ref);
5929 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5930 err = got_error(GOT_ERR_REBASE_COMMITID);
5934 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5935 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5938 got_ref_close(commit_ref);
5939 free(commit_ref_name);
5944 const struct got_error *
5945 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5946 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5947 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5948 struct got_commit_object *orig_commit,
5949 struct got_object_id *orig_commit_id, const char *new_logmsg,
5950 struct got_repository *repo)
5952 const struct got_error *err;
5953 char *commit_ref_name;
5954 struct got_reference *commit_ref = NULL;
5956 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5960 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5964 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5965 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5968 got_ref_close(commit_ref);
5969 free(commit_ref_name);
5973 const struct got_error *
5974 got_worktree_rebase_postpone(struct got_worktree *worktree,
5975 struct got_fileindex *fileindex)
5978 got_fileindex_free(fileindex);
5979 return lock_worktree(worktree, LOCK_SH);
5982 static const struct got_error *
5983 delete_ref(const char *name, struct got_repository *repo)
5985 const struct got_error *err;
5986 struct got_reference *ref;
5988 err = got_ref_open(&ref, repo, name, 0);
5990 if (err->code == GOT_ERR_NOT_REF)
5995 err = got_ref_delete(ref, repo);
6000 static const struct got_error *
6001 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6003 const struct got_error *err;
6004 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6005 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6007 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6010 err = delete_ref(tmp_branch_name, repo);
6014 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6017 err = delete_ref(new_base_branch_ref_name, repo);
6021 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6024 err = delete_ref(branch_ref_name, repo);
6028 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6031 err = delete_ref(commit_ref_name, repo);
6036 free(tmp_branch_name);
6037 free(new_base_branch_ref_name);
6038 free(branch_ref_name);
6039 free(commit_ref_name);
6043 const struct got_error *
6044 got_worktree_rebase_complete(struct got_worktree *worktree,
6045 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6046 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6047 struct got_repository *repo)
6049 const struct got_error *err, *unlockerr;
6050 struct got_object_id *new_head_commit_id = NULL;
6052 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6056 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6060 err = got_ref_write(rebased_branch, repo);
6064 err = got_worktree_set_head_ref(worktree, rebased_branch);
6068 err = delete_rebase_refs(worktree, repo);
6071 got_fileindex_free(fileindex);
6072 free(new_head_commit_id);
6073 unlockerr = lock_worktree(worktree, LOCK_SH);
6074 if (unlockerr && err == NULL)
6079 const struct got_error *
6080 got_worktree_rebase_abort(struct got_worktree *worktree,
6081 struct got_fileindex *fileindex, struct got_repository *repo,
6082 struct got_reference *new_base_branch,
6083 got_worktree_checkout_cb progress_cb, void *progress_arg)
6085 const struct got_error *err, *unlockerr, *sync_err;
6086 struct got_reference *resolved = NULL;
6087 struct got_object_id *commit_id = NULL;
6088 char *fileindex_path = NULL;
6089 struct revert_file_args rfa;
6090 struct got_object_id *tree_id = NULL;
6092 err = lock_worktree(worktree, LOCK_EX);
6096 err = got_ref_open(&resolved, repo,
6097 got_ref_get_symref_target(new_base_branch), 0);
6101 err = got_worktree_set_head_ref(worktree, resolved);
6106 * XXX commits to the base branch could have happened while
6107 * we were busy rebasing; should we store the original commit ID
6108 * when rebase begins and read it back here?
6110 err = got_ref_resolve(&commit_id, repo, resolved);
6114 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6118 err = got_object_id_by_path(&tree_id, repo,
6119 worktree->base_commit_id, worktree->path_prefix);
6123 err = delete_rebase_refs(worktree, repo);
6127 err = get_fileindex_path(&fileindex_path, worktree);
6131 rfa.worktree = worktree;
6132 rfa.fileindex = fileindex;
6133 rfa.progress_cb = progress_cb;
6134 rfa.progress_arg = progress_arg;
6135 rfa.patch_cb = NULL;
6136 rfa.patch_arg = NULL;
6138 err = worktree_status(worktree, "", fileindex, repo,
6139 revert_file, &rfa, NULL, NULL, 0, 0);
6143 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6144 repo, progress_cb, progress_arg, NULL, NULL);
6146 sync_err = sync_fileindex(fileindex, fileindex_path);
6147 if (sync_err && err == NULL)
6150 got_ref_close(resolved);
6154 got_fileindex_free(fileindex);
6155 free(fileindex_path);
6157 unlockerr = lock_worktree(worktree, LOCK_SH);
6158 if (unlockerr && err == NULL)
6163 const struct got_error *
6164 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6165 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6166 struct got_fileindex **fileindex, struct got_worktree *worktree,
6167 struct got_repository *repo)
6169 const struct got_error *err = NULL;
6170 char *tmp_branch_name = NULL;
6171 char *branch_ref_name = NULL;
6172 char *base_commit_ref_name = NULL;
6173 char *fileindex_path = NULL;
6174 struct check_rebase_ok_arg ok_arg;
6175 struct got_reference *wt_branch = NULL;
6176 struct got_reference *base_commit_ref = NULL;
6180 *base_commit_id = NULL;
6183 err = lock_worktree(worktree, LOCK_EX);
6187 err = open_fileindex(fileindex, &fileindex_path, worktree);
6191 ok_arg.worktree = worktree;
6193 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6198 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6202 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6206 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6211 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6216 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6220 err = got_ref_write(*branch_ref, repo);
6224 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6225 worktree->base_commit_id);
6228 err = got_ref_write(base_commit_ref, repo);
6231 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6232 if (*base_commit_id == NULL) {
6233 err = got_error_from_errno("got_object_id_dup");
6237 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6238 worktree->base_commit_id);
6241 err = got_ref_write(*tmp_branch, repo);
6245 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6249 free(fileindex_path);
6250 free(tmp_branch_name);
6251 free(branch_ref_name);
6252 free(base_commit_ref_name);
6254 got_ref_close(wt_branch);
6257 got_ref_close(*branch_ref);
6261 got_ref_close(*tmp_branch);
6264 free(*base_commit_id);
6266 got_fileindex_free(*fileindex);
6269 lock_worktree(worktree, LOCK_SH);
6274 const struct got_error *
6275 got_worktree_histedit_postpone(struct got_worktree *worktree,
6276 struct got_fileindex *fileindex)
6279 got_fileindex_free(fileindex);
6280 return lock_worktree(worktree, LOCK_SH);
6283 const struct got_error *
6284 got_worktree_histedit_in_progress(int *in_progress,
6285 struct got_worktree *worktree)
6287 const struct got_error *err;
6288 char *tmp_branch_name = NULL;
6290 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6294 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6295 free(tmp_branch_name);
6299 const struct got_error *
6300 got_worktree_histedit_continue(struct got_object_id **commit_id,
6301 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6302 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6303 struct got_worktree *worktree, struct got_repository *repo)
6305 const struct got_error *err;
6306 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6307 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6308 struct got_reference *commit_ref = NULL;
6309 struct got_reference *base_commit_ref = NULL;
6310 char *fileindex_path = NULL;
6311 int have_staged_files = 0;
6315 *base_commit_id = NULL;
6318 err = lock_worktree(worktree, LOCK_EX);
6322 err = open_fileindex(fileindex, &fileindex_path, worktree);
6326 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6327 &have_staged_files);
6328 if (err && err->code != GOT_ERR_CANCELLED)
6330 if (have_staged_files) {
6331 err = got_error(GOT_ERR_STAGED_PATHS);
6335 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6339 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6343 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6347 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6352 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6356 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6359 err = got_ref_resolve(commit_id, repo, commit_ref);
6363 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6366 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6370 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6374 free(commit_ref_name);
6375 free(branch_ref_name);
6376 free(fileindex_path);
6378 got_ref_close(commit_ref);
6379 if (base_commit_ref)
6380 got_ref_close(base_commit_ref);
6384 free(*base_commit_id);
6385 *base_commit_id = NULL;
6387 got_ref_close(*tmp_branch);
6391 got_fileindex_free(*fileindex);
6394 lock_worktree(worktree, LOCK_EX);
6399 static const struct got_error *
6400 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6402 const struct got_error *err;
6403 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6404 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6406 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6409 err = delete_ref(tmp_branch_name, repo);
6413 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6417 err = delete_ref(base_commit_ref_name, repo);
6421 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6424 err = delete_ref(branch_ref_name, repo);
6428 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6431 err = delete_ref(commit_ref_name, repo);
6435 free(tmp_branch_name);
6436 free(base_commit_ref_name);
6437 free(branch_ref_name);
6438 free(commit_ref_name);
6442 const struct got_error *
6443 got_worktree_histedit_abort(struct got_worktree *worktree,
6444 struct got_fileindex *fileindex, struct got_repository *repo,
6445 struct got_reference *branch, struct got_object_id *base_commit_id,
6446 got_worktree_checkout_cb progress_cb, void *progress_arg)
6448 const struct got_error *err, *unlockerr, *sync_err;
6449 struct got_reference *resolved = NULL;
6450 char *fileindex_path = NULL;
6451 struct got_object_id *tree_id = NULL;
6452 struct revert_file_args rfa;
6454 err = lock_worktree(worktree, LOCK_EX);
6458 err = got_ref_open(&resolved, repo,
6459 got_ref_get_symref_target(branch), 0);
6463 err = got_worktree_set_head_ref(worktree, resolved);
6467 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6471 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6472 worktree->path_prefix);
6476 err = delete_histedit_refs(worktree, repo);
6480 err = get_fileindex_path(&fileindex_path, worktree);
6484 rfa.worktree = worktree;
6485 rfa.fileindex = fileindex;
6486 rfa.progress_cb = progress_cb;
6487 rfa.progress_arg = progress_arg;
6488 rfa.patch_cb = NULL;
6489 rfa.patch_arg = NULL;
6491 err = worktree_status(worktree, "", fileindex, repo,
6492 revert_file, &rfa, NULL, NULL, 0, 0);
6496 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6497 repo, progress_cb, progress_arg, NULL, NULL);
6499 sync_err = sync_fileindex(fileindex, fileindex_path);
6500 if (sync_err && err == NULL)
6503 got_ref_close(resolved);
6505 free(fileindex_path);
6507 unlockerr = lock_worktree(worktree, LOCK_SH);
6508 if (unlockerr && err == NULL)
6513 const struct got_error *
6514 got_worktree_histedit_complete(struct got_worktree *worktree,
6515 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6516 struct got_reference *edited_branch, struct got_repository *repo)
6518 const struct got_error *err, *unlockerr;
6519 struct got_object_id *new_head_commit_id = NULL;
6520 struct got_reference *resolved = NULL;
6522 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6526 err = got_ref_open(&resolved, repo,
6527 got_ref_get_symref_target(edited_branch), 0);
6531 err = got_ref_change_ref(resolved, new_head_commit_id);
6535 err = got_ref_write(resolved, repo);
6539 err = got_worktree_set_head_ref(worktree, resolved);
6543 err = delete_histedit_refs(worktree, repo);
6546 got_fileindex_free(fileindex);
6547 free(new_head_commit_id);
6548 unlockerr = lock_worktree(worktree, LOCK_SH);
6549 if (unlockerr && err == NULL)
6554 const struct got_error *
6555 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6556 struct got_object_id *commit_id, struct got_repository *repo)
6558 const struct got_error *err;
6559 char *commit_ref_name;
6561 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6565 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6569 err = delete_ref(commit_ref_name, repo);
6571 free(commit_ref_name);
6575 const struct got_error *
6576 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6577 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6578 struct got_worktree *worktree, const char *refname,
6579 struct got_repository *repo)
6581 const struct got_error *err = NULL;
6582 char *fileindex_path = NULL;
6583 struct check_rebase_ok_arg ok_arg;
6587 *base_branch_ref = NULL;
6589 err = lock_worktree(worktree, LOCK_EX);
6593 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6594 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6595 "cannot integrate a branch into itself; "
6596 "update -b or different branch name required");
6600 err = open_fileindex(fileindex, &fileindex_path, worktree);
6604 /* Preconditions are the same as for rebase. */
6605 ok_arg.worktree = worktree;
6607 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6612 err = got_ref_open(branch_ref, repo, refname, 1);
6616 err = got_ref_open(base_branch_ref, repo,
6617 got_worktree_get_head_ref_name(worktree), 1);
6621 got_ref_close(*branch_ref);
6624 if (*base_branch_ref) {
6625 got_ref_close(*base_branch_ref);
6626 *base_branch_ref = NULL;
6629 got_fileindex_free(*fileindex);
6632 lock_worktree(worktree, LOCK_SH);
6637 const struct got_error *
6638 got_worktree_integrate_continue(struct got_worktree *worktree,
6639 struct got_fileindex *fileindex, struct got_repository *repo,
6640 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6641 got_worktree_checkout_cb progress_cb, void *progress_arg,
6642 got_cancel_cb cancel_cb, void *cancel_arg)
6644 const struct got_error *err = NULL, *sync_err, *unlockerr;
6645 char *fileindex_path = NULL;
6646 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6648 err = get_fileindex_path(&fileindex_path, worktree);
6652 err = got_ref_resolve(&commit_id, repo, branch_ref);
6656 err = got_object_id_by_path(&tree_id, repo, commit_id,
6657 worktree->path_prefix);
6661 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6665 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6666 progress_cb, progress_arg, cancel_cb, cancel_arg);
6670 err = got_ref_change_ref(base_branch_ref, commit_id);
6674 err = got_ref_write(base_branch_ref, repo);
6676 sync_err = sync_fileindex(fileindex, fileindex_path);
6677 if (sync_err && err == NULL)
6681 unlockerr = got_ref_unlock(branch_ref);
6682 if (unlockerr && err == NULL)
6684 got_ref_close(branch_ref);
6686 unlockerr = got_ref_unlock(base_branch_ref);
6687 if (unlockerr && err == NULL)
6689 got_ref_close(base_branch_ref);
6691 got_fileindex_free(fileindex);
6692 free(fileindex_path);
6695 unlockerr = lock_worktree(worktree, LOCK_SH);
6696 if (unlockerr && err == NULL)
6701 const struct got_error *
6702 got_worktree_integrate_abort(struct got_worktree *worktree,
6703 struct got_fileindex *fileindex, struct got_repository *repo,
6704 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6706 const struct got_error *err = NULL, *unlockerr = NULL;
6708 got_fileindex_free(fileindex);
6710 err = lock_worktree(worktree, LOCK_SH);
6712 unlockerr = got_ref_unlock(branch_ref);
6713 if (unlockerr && err == NULL)
6715 got_ref_close(branch_ref);
6717 unlockerr = got_ref_unlock(base_branch_ref);
6718 if (unlockerr && err == NULL)
6720 got_ref_close(base_branch_ref);
6725 struct check_stage_ok_arg {
6726 struct got_object_id *head_commit_id;
6727 struct got_worktree *worktree;
6728 struct got_fileindex *fileindex;
6729 struct got_repository *repo;
6733 const struct got_error *
6734 check_stage_ok(void *arg, unsigned char status,
6735 unsigned char staged_status, const char *relpath,
6736 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6737 struct got_object_id *commit_id, int dirfd, const char *de_name)
6739 struct check_stage_ok_arg *a = arg;
6740 const struct got_error *err = NULL;
6741 struct got_fileindex_entry *ie;
6742 struct got_object_id base_commit_id;
6743 struct got_object_id *base_commit_idp = NULL;
6744 char *in_repo_path = NULL, *p;
6746 if (status == GOT_STATUS_UNVERSIONED ||
6747 status == GOT_STATUS_NO_CHANGE)
6749 if (status == GOT_STATUS_NONEXISTENT)
6750 return got_error_set_errno(ENOENT, relpath);
6752 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6754 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6756 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6757 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6759 return got_error_from_errno("asprintf");
6761 if (got_fileindex_entry_has_commit(ie)) {
6762 memcpy(base_commit_id.sha1, ie->commit_sha1,
6763 SHA1_DIGEST_LENGTH);
6764 base_commit_idp = &base_commit_id;
6767 if (status == GOT_STATUS_CONFLICT) {
6768 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6770 } else if (status != GOT_STATUS_ADD &&
6771 status != GOT_STATUS_MODIFY &&
6772 status != GOT_STATUS_DELETE) {
6773 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6777 a->have_changes = 1;
6782 err = check_out_of_date(p, status, staged_status,
6783 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6784 GOT_ERR_STAGE_OUT_OF_DATE);
6790 struct stage_path_arg {
6791 struct got_worktree *worktree;
6792 struct got_fileindex *fileindex;
6793 struct got_repository *repo;
6794 got_worktree_status_cb status_cb;
6796 got_worktree_patch_cb patch_cb;
6798 int staged_something;
6801 static const struct got_error *
6802 stage_path(void *arg, unsigned char status,
6803 unsigned char staged_status, const char *relpath,
6804 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6805 struct got_object_id *commit_id, int dirfd, const char *de_name)
6807 struct stage_path_arg *a = arg;
6808 const struct got_error *err = NULL;
6809 struct got_fileindex_entry *ie;
6810 char *ondisk_path = NULL, *path_content = NULL;
6812 struct got_object_id *new_staged_blob_id = NULL;
6814 if (status == GOT_STATUS_UNVERSIONED)
6817 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6819 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6821 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6823 return got_error_from_errno("asprintf");
6826 case GOT_STATUS_ADD:
6827 case GOT_STATUS_MODIFY:
6829 if (status == GOT_STATUS_ADD) {
6830 int choice = GOT_PATCH_CHOICE_NONE;
6831 err = (*a->patch_cb)(&choice, a->patch_arg,
6832 status, ie->path, NULL, 1, 1);
6835 if (choice != GOT_PATCH_CHOICE_YES)
6838 err = create_patched_content(&path_content, 0,
6839 staged_blob_id ? staged_blob_id : blob_id,
6840 ondisk_path, dirfd, de_name, ie->path,
6841 a->repo, a->patch_cb, a->patch_arg);
6842 if (err || path_content == NULL)
6846 err = got_object_blob_create(&new_staged_blob_id,
6847 path_content ? path_content : ondisk_path, a->repo);
6850 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6851 SHA1_DIGEST_LENGTH);
6852 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6853 stage = GOT_FILEIDX_STAGE_ADD;
6855 stage = GOT_FILEIDX_STAGE_MODIFY;
6856 got_fileindex_entry_stage_set(ie, stage);
6857 a->staged_something = 1;
6858 if (a->status_cb == NULL)
6860 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6861 get_staged_status(ie), relpath, blob_id,
6862 new_staged_blob_id, NULL, dirfd, de_name);
6864 case GOT_STATUS_DELETE:
6865 if (staged_status == GOT_STATUS_DELETE)
6868 int choice = GOT_PATCH_CHOICE_NONE;
6869 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6870 ie->path, NULL, 1, 1);
6873 if (choice == GOT_PATCH_CHOICE_NO)
6875 if (choice != GOT_PATCH_CHOICE_YES) {
6876 err = got_error(GOT_ERR_PATCH_CHOICE);
6880 stage = GOT_FILEIDX_STAGE_DELETE;
6881 got_fileindex_entry_stage_set(ie, stage);
6882 a->staged_something = 1;
6883 if (a->status_cb == NULL)
6885 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6886 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6889 case GOT_STATUS_NO_CHANGE:
6891 case GOT_STATUS_CONFLICT:
6892 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6894 case GOT_STATUS_NONEXISTENT:
6895 err = got_error_set_errno(ENOENT, relpath);
6898 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6902 if (path_content && unlink(path_content) == -1 && err == NULL)
6903 err = got_error_from_errno2("unlink", path_content);
6906 free(new_staged_blob_id);
6910 const struct got_error *
6911 got_worktree_stage(struct got_worktree *worktree,
6912 struct got_pathlist_head *paths,
6913 got_worktree_status_cb status_cb, void *status_arg,
6914 got_worktree_patch_cb patch_cb, void *patch_arg,
6915 struct got_repository *repo)
6917 const struct got_error *err = NULL, *sync_err, *unlockerr;
6918 struct got_pathlist_entry *pe;
6919 struct got_fileindex *fileindex = NULL;
6920 char *fileindex_path = NULL;
6921 struct got_reference *head_ref = NULL;
6922 struct got_object_id *head_commit_id = NULL;
6923 struct check_stage_ok_arg oka;
6924 struct stage_path_arg spa;
6926 err = lock_worktree(worktree, LOCK_EX);
6930 err = got_ref_open(&head_ref, repo,
6931 got_worktree_get_head_ref_name(worktree), 0);
6934 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6937 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6941 /* Check pre-conditions before staging anything. */
6942 oka.head_commit_id = head_commit_id;
6943 oka.worktree = worktree;
6944 oka.fileindex = fileindex;
6946 oka.have_changes = 0;
6947 TAILQ_FOREACH(pe, paths, entry) {
6948 err = worktree_status(worktree, pe->path, fileindex, repo,
6949 check_stage_ok, &oka, NULL, NULL, 0, 0);
6953 if (!oka.have_changes) {
6954 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6958 spa.worktree = worktree;
6959 spa.fileindex = fileindex;
6961 spa.patch_cb = patch_cb;
6962 spa.patch_arg = patch_arg;
6963 spa.status_cb = status_cb;
6964 spa.status_arg = status_arg;
6965 spa.staged_something = 0;
6966 TAILQ_FOREACH(pe, paths, entry) {
6967 err = worktree_status(worktree, pe->path, fileindex, repo,
6968 stage_path, &spa, NULL, NULL, 0, 0);
6972 if (!spa.staged_something) {
6973 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6977 sync_err = sync_fileindex(fileindex, fileindex_path);
6978 if (sync_err && err == NULL)
6982 got_ref_close(head_ref);
6983 free(head_commit_id);
6984 free(fileindex_path);
6986 got_fileindex_free(fileindex);
6987 unlockerr = lock_worktree(worktree, LOCK_SH);
6988 if (unlockerr && err == NULL)
6993 struct unstage_path_arg {
6994 struct got_worktree *worktree;
6995 struct got_fileindex *fileindex;
6996 struct got_repository *repo;
6997 got_worktree_checkout_cb progress_cb;
6999 got_worktree_patch_cb patch_cb;
7003 static const struct got_error *
7004 create_unstaged_content(char **path_unstaged_content,
7005 char **path_new_staged_content, struct got_object_id *blob_id,
7006 struct got_object_id *staged_blob_id, const char *relpath,
7007 struct got_repository *repo,
7008 got_worktree_patch_cb patch_cb, void *patch_arg)
7010 const struct got_error *err;
7011 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7012 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7013 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7014 struct stat sb1, sb2;
7015 struct got_diff_changes *changes = NULL;
7016 struct got_diff_state *ds = NULL;
7017 struct got_diff_args *args = NULL;
7018 struct got_diff_change *change;
7019 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7020 int have_content = 0, have_rejected_content = 0;
7022 *path_unstaged_content = NULL;
7023 *path_new_staged_content = NULL;
7025 err = got_object_id_str(&label1, blob_id);
7028 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7032 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7036 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7040 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7044 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7048 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7052 if (stat(path1, &sb1) == -1) {
7053 err = got_error_from_errno2("stat", path1);
7057 if (stat(path2, &sb2) == -1) {
7058 err = got_error_from_errno2("stat", path2);
7062 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7063 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7067 err = got_opentemp_named(path_unstaged_content, &outfile,
7068 "got-unstaged-content");
7071 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7072 "got-new-staged-content");
7076 if (fseek(f1, 0L, SEEK_SET) == -1) {
7077 err = got_ferror(f1, GOT_ERR_IO);
7080 if (fseek(f2, 0L, SEEK_SET) == -1) {
7081 err = got_ferror(f2, GOT_ERR_IO);
7084 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7086 err = apply_or_reject_change(&choice, change, ++n,
7087 changes->nchanges, ds, args, diff_flags, relpath,
7088 f1, f2, &line_cur1, &line_cur2,
7089 outfile, rejectfile, patch_cb, patch_arg);
7092 if (choice == GOT_PATCH_CHOICE_YES)
7095 have_rejected_content = 1;
7096 if (choice == GOT_PATCH_CHOICE_QUIT)
7099 if (have_content || have_rejected_content)
7100 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7101 outfile, rejectfile);
7105 got_object_blob_close(blob);
7107 got_object_blob_close(staged_blob);
7108 if (f1 && fclose(f1) == EOF && err == NULL)
7109 err = got_error_from_errno2("fclose", path1);
7110 if (f2 && fclose(f2) == EOF && err == NULL)
7111 err = got_error_from_errno2("fclose", path2);
7112 if (outfile && fclose(outfile) == EOF && err == NULL)
7113 err = got_error_from_errno2("fclose", *path_unstaged_content);
7114 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7115 err = got_error_from_errno2("fclose", *path_new_staged_content);
7116 if (path1 && unlink(path1) == -1 && err == NULL)
7117 err = got_error_from_errno2("unlink", path1);
7118 if (path2 && unlink(path2) == -1 && err == NULL)
7119 err = got_error_from_errno2("unlink", path2);
7120 if (err || !have_content) {
7121 if (*path_unstaged_content &&
7122 unlink(*path_unstaged_content) == -1 && err == NULL)
7123 err = got_error_from_errno2("unlink",
7124 *path_unstaged_content);
7125 free(*path_unstaged_content);
7126 *path_unstaged_content = NULL;
7128 if (err || !have_rejected_content) {
7129 if (*path_new_staged_content &&
7130 unlink(*path_new_staged_content) == -1 && err == NULL)
7131 err = got_error_from_errno2("unlink",
7132 *path_new_staged_content);
7133 free(*path_new_staged_content);
7134 *path_new_staged_content = NULL;
7138 got_diff_state_free(ds);
7142 got_diff_free_changes(changes);
7148 static const struct got_error *
7149 unstage_path(void *arg, unsigned char status,
7150 unsigned char staged_status, const char *relpath,
7151 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7152 struct got_object_id *commit_id, int dirfd, const char *de_name)
7154 const struct got_error *err = NULL;
7155 struct unstage_path_arg *a = arg;
7156 struct got_fileindex_entry *ie;
7157 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7158 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7159 char *path_new_staged_content = NULL;
7160 char *id_str = NULL, *label_orig = NULL;
7161 int local_changes_subsumed;
7164 if (staged_status != GOT_STATUS_ADD &&
7165 staged_status != GOT_STATUS_MODIFY &&
7166 staged_status != GOT_STATUS_DELETE)
7169 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7171 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7173 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7175 return got_error_from_errno("asprintf");
7177 err = got_object_id_str(&id_str,
7178 commit_id ? commit_id : a->worktree->base_commit_id);
7181 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7183 err = got_error_from_errno("asprintf");
7187 switch (staged_status) {
7188 case GOT_STATUS_MODIFY:
7189 err = got_object_open_as_blob(&blob_base, a->repo,
7194 case GOT_STATUS_ADD:
7196 if (staged_status == GOT_STATUS_ADD) {
7197 int choice = GOT_PATCH_CHOICE_NONE;
7198 err = (*a->patch_cb)(&choice, a->patch_arg,
7199 staged_status, ie->path, NULL, 1, 1);
7202 if (choice != GOT_PATCH_CHOICE_YES)
7205 err = create_unstaged_content(
7206 &path_unstaged_content,
7207 &path_new_staged_content, blob_id,
7208 staged_blob_id, ie->path, a->repo,
7209 a->patch_cb, a->patch_arg);
7210 if (err || path_unstaged_content == NULL)
7212 if (path_new_staged_content) {
7213 err = got_object_blob_create(
7215 path_new_staged_content,
7219 memcpy(ie->staged_blob_sha1,
7220 staged_blob_id->sha1,
7221 SHA1_DIGEST_LENGTH);
7223 err = merge_file(&local_changes_subsumed,
7224 a->worktree, blob_base, ondisk_path,
7225 relpath, got_fileindex_perms_to_st(ie),
7226 path_unstaged_content, label_orig,
7227 "unstaged", a->repo, a->progress_cb,
7230 path_new_staged_content == NULL)
7231 got_fileindex_entry_stage_set(ie,
7232 GOT_FILEIDX_STAGE_NONE);
7233 break; /* Done with this file. */
7236 err = got_object_open_as_blob(&blob_staged, a->repo,
7237 staged_blob_id, 8192);
7240 err = merge_blob(&local_changes_subsumed, a->worktree,
7241 blob_base, ondisk_path, relpath,
7242 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7243 commit_id ? commit_id : a->worktree->base_commit_id,
7244 a->repo, a->progress_cb, a->progress_arg);
7246 got_fileindex_entry_stage_set(ie,
7247 GOT_FILEIDX_STAGE_NONE);
7249 case GOT_STATUS_DELETE:
7251 int choice = GOT_PATCH_CHOICE_NONE;
7252 err = (*a->patch_cb)(&choice, a->patch_arg,
7253 staged_status, ie->path, NULL, 1, 1);
7256 if (choice == GOT_PATCH_CHOICE_NO)
7258 if (choice != GOT_PATCH_CHOICE_YES) {
7259 err = got_error(GOT_ERR_PATCH_CHOICE);
7263 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7264 err = get_file_status(&status, &sb, ie, ondisk_path,
7265 dirfd, de_name, a->repo);
7268 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7273 if (path_unstaged_content &&
7274 unlink(path_unstaged_content) == -1 && err == NULL)
7275 err = got_error_from_errno2("unlink", path_unstaged_content);
7276 if (path_new_staged_content &&
7277 unlink(path_new_staged_content) == -1 && err == NULL)
7278 err = got_error_from_errno2("unlink", path_new_staged_content);
7279 free(path_unstaged_content);
7280 free(path_new_staged_content);
7282 got_object_blob_close(blob_base);
7284 got_object_blob_close(blob_staged);
7290 const struct got_error *
7291 got_worktree_unstage(struct got_worktree *worktree,
7292 struct got_pathlist_head *paths,
7293 got_worktree_checkout_cb progress_cb, void *progress_arg,
7294 got_worktree_patch_cb patch_cb, void *patch_arg,
7295 struct got_repository *repo)
7297 const struct got_error *err = NULL, *sync_err, *unlockerr;
7298 struct got_pathlist_entry *pe;
7299 struct got_fileindex *fileindex = NULL;
7300 char *fileindex_path = NULL;
7301 struct unstage_path_arg upa;
7303 err = lock_worktree(worktree, LOCK_EX);
7307 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7311 upa.worktree = worktree;
7312 upa.fileindex = fileindex;
7314 upa.progress_cb = progress_cb;
7315 upa.progress_arg = progress_arg;
7316 upa.patch_cb = patch_cb;
7317 upa.patch_arg = patch_arg;
7318 TAILQ_FOREACH(pe, paths, entry) {
7319 err = worktree_status(worktree, pe->path, fileindex, repo,
7320 unstage_path, &upa, NULL, NULL, 0, 0);
7325 sync_err = sync_fileindex(fileindex, fileindex_path);
7326 if (sync_err && err == NULL)
7329 free(fileindex_path);
7331 got_fileindex_free(fileindex);
7332 unlockerr = lock_worktree(worktree, LOCK_SH);
7333 if (unlockerr && err == NULL)